PowerShell/Docs/check-repo.md

110 lines
3.7 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *check-repo.ps1* Script
2022-11-17 19:46:02 +01:00
This PowerShell script verifies the integrity of a local Git repository.
## Parameters
```powershell
2023-05-26 12:20:18 +02:00
/home/mf/Repos/PowerShell/Scripts/check-repo.ps1 [[-RepoDir] <String>] [<CommonParameters>]
2022-11-17 19:46:02 +01:00
-RepoDir <String>
Specifies the path to the Git repository (current working dir by default)
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.
```
## Example
```powershell
2023-05-26 12:20:18 +02:00
PS> ./check-repo C:\MyRepo
2022-11-17 19:46:02 +01:00
```
## Notes
Author: Markus Fleschutz | License: CC0
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Checks a repo
2022-11-17 20:02:26 +01:00
.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
2023-05-26 12:20:18 +02:00
PS> ./check-repo C:\MyRepo
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$RepoDir = "$PWD")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
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" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (2/10) Checking repository... " -noNewline
2022-11-17 20:02:26 +01:00
$FullPath = Resolve-Path "$RepoDir"
if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" }
2023-05-26 12:20:18 +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" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (5/10) Fetching updates..."
2022-11-17 20:02:26 +01:00
& git -C "$FullPath" fetch
if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" }
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (6/10) Querying latest tag... " -noNewline
2022-11-17 20:02:26 +01:00
$LatestTagCommitID = (git -C "$FullPath" rev-list --tags --max-count=1)
$LatestTagName = (git -C "$FullPath" describe --tags $LatestTagCommitID)
Write-Host "$LatestTagName (commit $LatestTagCommitID)"
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" }
$RepoDirName = (Get-Item "$FullPath").Name
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-05-26 12:20:18 +02:00
"✔️ successfully checked repo 📂$RepoDirName in $Elapsed sec"
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
2022-11-17 19:46:02 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of check-repo.ps1*