PowerShell/docs/check-repo.md

133 lines
4.8 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *check-repo.ps1* Script
===========================
2022-11-17 19:46:02 +01:00
2024-05-19 10:25:56 +02:00
This PowerShell script verifies the integrity of a local Git repository and performs maintenance tasks.
2022-11-17 19:46:02 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2022-11-17 19:46:02 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/check-repo.ps1 [[-pathToRepo] <String>] [<CommonParameters>]
2022-11-17 19:46:02 +01:00
2024-05-19 10:25:56 +02:00
-pathToRepo <String>
Specifies the file path to the local Git repository (current working directory by default)
2022-11-17 19:46:02 +01:00
Required? false
Position? 1
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2022-11-17 19:46:02 +01:00
```powershell
2023-07-29 09:45:37 +02:00
PS> ./check-repo.ps1 C:\MyRepo
⏳ (1/10) Searching for Git executable... git version 2.41.0.windows.3
2024-08-15 09:51:46 +02:00
⏳ (2/10) Checking local repository... C:\MyRepo
2023-07-29 09:45:37 +02:00
⏳ (3/10) Querying remote URL... git@github.com:fleschutz/PowerShell.git
⏳ (4/10) Querying current branch... main
2024-05-19 10:25:56 +02:00
⏳ (5/10) Fetching remote updates... OK
⏳ (6/10) Querying latest tag... v0.8 (at commit 02171a401d83b01a0cda0af426840b605e617f08)
2023-07-29 09:45:37 +02:00
⏳ (7/10) Verifying data integrity...
...
2022-11-17 19:46:02 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2022-11-17 19:46:02 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2024-05-19 10:25:56 +02:00
Checks a Git repository
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2024-05-19 10:25:56 +02:00
This PowerShell script verifies the integrity of a local Git repository and performs maintenance tasks.
.PARAMETER pathToRepo
Specifies the file path to the local Git repository (current working directory by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-07-29 09:45:37 +02:00
PS> ./check-repo.ps1 C:\MyRepo
⏳ (1/10) Searching for Git executable... git version 2.41.0.windows.3
2024-08-15 09:51:46 +02:00
⏳ (2/10) Checking local repository... C:\MyRepo
2023-07-29 09:45:37 +02:00
⏳ (3/10) Querying remote URL... git@github.com:fleschutz/PowerShell.git
⏳ (4/10) Querying current branch... main
2024-05-19 10:25:56 +02:00
⏳ (5/10) Fetching remote updates... OK
⏳ (6/10) Querying latest tag... v0.8 (at commit 02171a401d83b01a0cda0af426840b605e617f08)
2023-07-29 09:45:37 +02:00
⏳ (7/10) Verifying data integrity...
...
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-05-19 10:25:56 +02:00
param([string]$pathToRepo = "$PWD")
2022-11-17 20:02:26 +01:00
try {
2024-05-19 10:25:56 +02:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (1/10) Searching for Git executable... " -noNewline
2022-11-17 20:02:26 +01:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2024-05-19 10:25:56 +02:00
Write-Host "⏳ (2/10) Checking local repository... " -noNewline
$FullPath = Resolve-Path "$pathToRepo"
2022-11-17 20:02:26 +01:00
if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" }
2024-08-15 09:51:46 +02:00
"$FullPath"
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (3/10) Querying remote URL... " -noNewline
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" remote get-url origin
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (4/10) Querying current branch... " -noNewline
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" branch --show-current
if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" }
2024-05-19 10:25:56 +02:00
Write-Host "⏳ (5/10) Fetching remote updates... " -noNewline
& git -C "$FullPath" fetch --all --recurse-submodules --tags --force --quiet
2023-07-29 09:45:37 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
2024-05-19 10:25:56 +02:00
Write-Host "OK"
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (6/10) Querying latest tag... " -noNewline
2024-05-19 10:25:56 +02:00
$latestTagCommitID = (git -C "$FullPath" rev-list --tags --max-count=1)
$latestTagName = (git -C "$FullPath" describe --tags $latestTagCommitID)
Write-Host "$latestTagName (at commit $latestTagCommitID)"
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (7/10) Verifying data integrity..."
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" fsck
if ($lastExitCode -ne "0") { throw "'git fsck' failed with exit code $lastExitCode" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (8/10) Running maintenance tasks..."
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" maintenance run
if ($lastExitCode -ne "0") { throw "'git maintenance run' failed with exit code $lastExitCode" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (9/10) Checking submodule status..."
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" submodule status
if ($lastExitCode -ne "0") { throw "'git submodule status' failed with exit code $lastExitCode" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (10/10) Checking repo status... " -noNewline
& git -C "$FullPath" status
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git status --short' failed with exit code $lastExitCode" }
2024-05-19 10:25:56 +02:00
$repoDirName = (Get-Item "$FullPath").Name
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-11-08 12:35:11 +01:00
"✅ Checked 📂$repoDirName repo in $($elapsed)s."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*