From 46e582913c7548269a00682411d1c19313120a4a Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 3 Oct 2022 18:48:12 +0200 Subject: [PATCH] Add check-repo.ps1 --- README.md | 3 ++- Scripts/check-repo.ps1 | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 Scripts/check-repo.ps1 diff --git a/README.md b/README.md index 259fc170..e3148fa1 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/Scripts/check-repo.ps1 b/Scripts/check-repo.ps1 new file mode 100644 index 00000000..c4f8fc39 --- /dev/null +++ b/Scripts/check-repo.ps1 @@ -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 +}