PowerShell/Scripts/check-repo.ps1

79 lines
3.2 KiB
PowerShell
Raw Normal View History

2022-10-03 18:48:12 +02:00
<#
.SYNOPSIS
2023-04-12 11:49:21 +02:00
Checks a repo
2022-10-03 18:48:12 +02:00
.DESCRIPTION
This PowerShell script verifies the integrity of a local Git repository.
.PARAMETER RepoDir
2023-07-27 16:58:21 +02:00
Specifies the path to the Git repository (current working directory by default)
2022-10-03 18:48:12 +02:00
.EXAMPLE
2023-07-27 16:58:21 +02:00
PS> ./check-repo.ps1 C:\MyRepo
(1/10) Searching for Git executable... git version 2.41.0.windows.3
(2/10) Checking local folder... 📂C:\MyRepo
(3/10) Querying remote URL... git@github.com:fleschutz/PowerShell.git
(4/10) Querying current branch... main
(5/10) Fetching remote updates...
(6/10) Querying latest tag... v0.8 (commit 02171a401d83b01a0cda0af426840b605e617f08)
(7/10) Verifying data integrity...
...
2022-10-03 18:48:12 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$RepoDir = "$PWD")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (1/10) Searching for Git executable... " -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" }
2023-07-27 16:58:21 +02:00
Write-Host "⏳ (2/10) Checking local folder... " -noNewline
2022-10-03 19:02:48 +02:00
$FullPath = Resolve-Path "$RepoDir"
if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" }
2023-04-12 11:49:21 +02:00
"📂$FullPath"
2022-10-03 18:48:12 +02:00
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (3/10) Querying remote URL... " -noNewline
2022-10-03 19:02:48 +02:00
& git -C "$FullPath" remote get-url origin
2022-10-24 13:38:14 +02:00
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
2022-10-03 18:48:12 +02:00
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (4/10) Querying current branch... " -noNewline
2022-10-24 13:38:14 +02:00
& git -C "$FullPath" branch --show-current
if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" }
2023-07-27 16:58:21 +02:00
Write-Host "⏳ (5/10) Fetching remote updates..."
2022-10-26 10:30:12 +02:00
& git -C "$FullPath" fetch
2023-07-27 16:58:21 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
2022-10-26 10:30:12 +02:00
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (6/10) Querying latest tag... " -noNewline
2022-10-26 10:30:12 +02:00
$LatestTagCommitID = (git -C "$FullPath" rev-list --tags --max-count=1)
$LatestTagName = (git -C "$FullPath" describe --tags $LatestTagCommitID)
Write-Host "$LatestTagName (commit $LatestTagCommitID)"
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (7/10) 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" }
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (8/10) Running maintenance tasks..."
2022-10-24 13:59:41 +02:00
& git -C "$FullPath" maintenance run
if ($lastExitCode -ne "0") { throw "'git maintenance run' failed with exit code $lastExitCode" }
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (9/10) Checking submodule status..."
2022-10-24 13:38:14 +02:00
& git -C "$FullPath" submodule status
if ($lastExitCode -ne "0") { throw "'git submodule status' failed with exit code $lastExitCode" }
2023-04-18 14:42:59 +02:00
Write-Host "⏳ (10/10) Checking repo status... " -noNewline
& git -C "$FullPath" status
2022-10-24 13:38:14 +02:00
if ($lastExitCode -ne "0") { throw "'git status --short' 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
2023-09-11 15:57:46 +02:00
"✔️ Checked repo 📂$RepoDirName in $Elapsed sec"
2022-10-03 18:48:12 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}