PowerShell/Scripts/check-repo.ps1

55 lines
1.8 KiB
PowerShell
Raw Normal View History

2022-10-03 18:48:12 +02:00
<#
.SYNOPSIS
Checks a Git repository
.DESCRIPTION
This PowerShell script verifies the integrity of a local Git repository.
.PARAMETER RepoDir
Specifies the path to the Git repository (current working dir by default)
.EXAMPLE
PS> ./check-repo
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$RepoDir = "$PWD")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (1/6) Searching for Git... " -noNewline
2022-10-03 18:48:12 +02:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2022-10-03 19:02:48 +02:00
$FullPath = Resolve-Path "$RepoDir"
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (2/6) Checking path... " -noNewline
2022-10-03 19:02:48 +02:00
if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" }
"$FullPath"
2022-10-03 18:48:12 +02:00
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (3/6) Searching for subfolder 📂.git..." -noNewline
2022-10-03 19:02:48 +02:00
if (!(Test-Path "$FullPath/.git" -pathType container)) { throw "Can't access folder: $FullPath/.git" }
2022-10-03 18:48:12 +02:00
"OK"
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (4/6) Querying remote URL... " -noNewline
2022-10-03 19:02:48 +02:00
& git -C "$FullPath" remote get-url origin
2022-10-03 18:48:12 +02:00
if ($lastExitCode -ne "0") { throw "'git status' failed with exit code $lastExitCode" }
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (5/6) Verifying data integrity..."
2022-10-03 19:02:48 +02:00
& git -C "$FullPath" fsck
2022-10-03 18:48:12 +02:00
if ($lastExitCode -ne "0") { throw "'git fsck' failed with exit code $lastExitCode" }
2022-10-12 13:24:19 +02:00
Write-Host "⏳ (6/6) Checking status... " -noNewline
2022-10-03 19:02:48 +02:00
& git -C "$FullPath" status --short
2022-10-03 18:48:12 +02:00
if ($lastExitCode -ne "0") { throw "'git status' failed with exit code $lastExitCode" }
2022-10-03 19:02:48 +02:00
" "
$RepoDirName = (Get-Item "$FullPath").Name
2022-10-03 18:48:12 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ checked 📂$RepoDirName repo in $Elapsed sec"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}