Add check-repo.ps1

This commit is contained in:
Markus Fleschutz 2022-10-03 18:48:12 +02:00
parent 482b5b86ac
commit 46e582913c
2 changed files with 54 additions and 1 deletions

View File

@ -234,6 +234,7 @@ Mega Collection of PowerShell Scripts
| ---------------------------------------------------- | ------------------------------------------------------------------ | --------------------------------------- |
| [build-repo.ps1](Scripts/build-repo.ps1) | Builds a Git repository | [Help](Docs/build-repo.md) |
| [build-repos.ps1](Scripts/build-repos.ps1) | Builds all Git repositories in a folder | [Help](Docs/build-repos.md) |
| [check-repo.ps1](Scripts/check-repo.ps1) | Checks a Git repository | [Help](Docs/check-repo.md) |
| [clean-repo.ps1](Scripts/clean-repo.ps1) | Cleans a Git repository from untracked files | [Help](Docs/clean-repo.md) |
| [clean-repos.ps1](Scripts/clean-repos.ps1) | Cleans all Git repositories in a folder from untracked files | [Help](Docs/clean-repos.md) |
| [clone-repos.ps1](Scripts/clone-repos.ps1) | Clones well-known Git repositories | [Help](Docs/clone-repos.md) |
@ -244,7 +245,7 @@ Mega Collection of PowerShell Scripts
| [list-commits.ps1](Scripts/list-commits.ps1) | Lists all commits in a Git repository | [Help](Docs/list-commits.md) |
| [list-latest-tag.ps1](Scripts/list-latest-tag.ps1) | Lists the latest tag on the current branch in a Git repository | [Help](Docs/list-latest-tag.md) |
| [list-latest-tags.ps1](Scripts/list-latest-tags.ps1) | Lists the latests tags in all Git repositories under a directory | [Help](Docs/list-latest-tags.md) |
| [list-repos.ps1](Scripts/list-repos.ps1) | Lists Git repositories | [Help](Docs/list-repos.md) |
| [list-repos.ps1](Scripts/list-repos.ps1) | Lists the Git repositories in a folder | [Help](Docs/list-repos.md) |
| [list-submodules.ps1](Scripts/list-submodules.ps1) | Lists the submodules in a Git repository | [Help](Docs/list-submodules.md) |
| [list-tags.ps1](Scripts/list-tags.ps1) | Lists all tags in a Git repository | [Help](Docs/list-tags.md) |
| [new-branch.ps1](Scripts/new-branch.ps1) | Creates a new branch in a Git repository | [Help](Docs/new-branch.md) |

52
Scripts/check-repo.ps1 Normal file
View File

@ -0,0 +1,52 @@
<#
.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()
Write-Host "⏳ (1/6) Searching for Git... " -noNewline
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
$RepoDirName = (Get-Item "$RepoDir").Name
Write-Host "⏳ (2/6) Checking folder 📂$RepoDirName... " -noNewline
if (!(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" }
"OK"
Write-Host "⏳ (3/6) Searching for .git subfolder... " -noNewline
if (!(Test-Path "$RepoDir/.git" -pathType container)) { throw "Can't access folder: $RepoDir" }
"OK"
Write-Host "⏳ (4/6) Querying remote URL... " -noNewline
& git -C "$RepoDir" remote get-url origin
if ($lastExitCode -ne "0") { throw "'git status' failed with exit code $lastExitCode" }
Write-Host "⏳ (5/6) Verifying data integrity... "
& git -C "$RepoDir" fsck
if ($lastExitCode -ne "0") { throw "'git fsck' failed with exit code $lastExitCode" }
Write-Host "⏳ (6/6) Checking status... " -noNewline
& git -C "$RepoDir" status --short
if ($lastExitCode -ne "0") { throw "'git status' failed with exit code $lastExitCode" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ checked 📂$RepoDirName repo in $Elapsed sec"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}