Merge branch 'master' of github.com:fleschutz/PowerShell

This commit is contained in:
Markus Fleschutz 2023-04-19 08:42:30 +02:00
commit cbe5c46556
6 changed files with 32 additions and 34 deletions

View File

@ -109,6 +109,11 @@ Good PowerShell scripts are both user-friendly and platform-independent. I recom
9. On success exit with error code 0 (`exit 0`), otherwise print the error with keyword "ERROR:" (to support log parsers) and exit the error code (mostly 1) 9. On success exit with error code 0 (`exit 0`), otherwise print the error with keyword "ERROR:" (to support log parsers) and exit the error code (mostly 1)
Where's the Star History?
-------------------------
[![Star History Chart](https://api.star-history.com/svg?repos=fleschutz/PowerShell&type=Date)](https://star-history.com/#fleschutz/PowerShell&Date)
Want to contribute or found a bug or issue? Want to contribute or found a bug or issue?
------------------------------------------- -------------------------------------------
If you find something bad (like a bug, error, or any issue), please report it here by open an Issue. If you find something bad (like a bug, error, or any issue), please report it here by open an Issue.

View File

@ -18,58 +18,51 @@ param([string]$RepoDir = "$PWD")
try { try {
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
Write-Host "⏳ (1/11) Searching for Git executable... " -noNewline Write-Host "⏳ (1/10) Searching for Git executable... " -noNewline
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Host "⏳ (2/11) Checking file path... " -noNewline Write-Host "⏳ (2/10) Checking repository... " -noNewline
$FullPath = Resolve-Path "$RepoDir" $FullPath = Resolve-Path "$RepoDir"
if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" } if (!(Test-Path "$FullPath" -pathType Container)) { throw "Can't access folder: $FullPath" }
"📂$FullPath" "📂$FullPath"
Write-Host "⏳ (3/11) Searching for 📂.git... " -noNewline Write-Host "⏳ (3/10) Querying remote URL... " -noNewline
if (!(Test-Path "$FullPath/.git" -pathType container)) { throw "Can't access folder: $FullPath/.git" }
"OK"
Write-Host "⏳ (4/11) Querying remote URL... " -noNewline
& git -C "$FullPath" remote get-url origin & git -C "$FullPath" remote get-url origin
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
Write-Host "⏳ (5/11) Querying current branch... " -noNewline Write-Host "⏳ (4/10) Querying current branch... " -noNewline
& git -C "$FullPath" branch --show-current & git -C "$FullPath" branch --show-current
if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" }
Write-Host "⏳ (6/11) Trying to fetch... " -noNewline Write-Host "⏳ (5/10) Fetching updates..."
& git -C "$FullPath" fetch & git -C "$FullPath" fetch
if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git branch --show-current' failed with exit code $lastExitCode" }
Write-Host "OK"
Write-Host "⏳ (7/11) Querying latest tag... " -noNewline Write-Host "⏳ (6/10) Querying latest tag... " -noNewline
$LatestTagCommitID = (git -C "$FullPath" rev-list --tags --max-count=1) $LatestTagCommitID = (git -C "$FullPath" rev-list --tags --max-count=1)
$LatestTagName = (git -C "$FullPath" describe --tags $LatestTagCommitID) $LatestTagName = (git -C "$FullPath" describe --tags $LatestTagCommitID)
Write-Host "$LatestTagName (commit $LatestTagCommitID)" Write-Host "$LatestTagName (commit $LatestTagCommitID)"
Write-Host "⏳ (8/11) Verifying data integrity..." Write-Host "⏳ (7/10) Verifying data integrity..."
& git -C "$FullPath" fsck & git -C "$FullPath" fsck
if ($lastExitCode -ne "0") { throw "'git fsck' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git fsck' failed with exit code $lastExitCode" }
Write-Host "⏳ (9/11) Running maintenance tasks..." Write-Host "⏳ (8/10) Running maintenance tasks..."
& git -C "$FullPath" maintenance run & git -C "$FullPath" maintenance run
if ($lastExitCode -ne "0") { throw "'git maintenance run' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git maintenance run' failed with exit code $lastExitCode" }
Write-Host "⏳ (10/11) Querying submodule status... " -noNewline Write-Host "⏳ (9/10) Checking submodule status..."
& git -C "$FullPath" submodule status & git -C "$FullPath" submodule status
if ($lastExitCode -ne "0") { throw "'git submodule status' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git submodule status' failed with exit code $lastExitCode" }
" "
Write-Host "⏳ (11/11) Querying repo status... " -noNewline Write-Host "⏳ (10/10) Checking repo status... " -noNewline
& git -C "$FullPath" status --short & git -C "$FullPath" status
if ($lastExitCode -ne "0") { throw "'git status --short' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git status --short' failed with exit code $lastExitCode" }
" "
$RepoDirName = (Get-Item "$FullPath").Name $RepoDirName = (Get-Item "$FullPath").Name
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ checked repo 📂$RepoDirName in $Elapsed sec" "✔️ successfully checked repo 📂$RepoDirName in $Elapsed sec"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -23,11 +23,11 @@ try {
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
"⏳ (2/4) Checking local repository... 📂$RepoDir" "⏳ (2/4) Checking repository... 📂$RepoDir"
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder '$RepoDir' - maybe a typo or missing folder permissions?" } if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder '$RepoDir' - maybe a typo or missing folder permissions?" }
$RepoDirName = (Get-Item "$RepoDir").Name $RepoDirName = (Get-Item "$RepoDir").Name
"⏳ (3/4) Removing untracked files in the repository..." "⏳ (3/4) Removing repo's untracked files..."
& git -C "$RepoDir" clean -xfd -f # to delete all untracked files in the main repo & git -C "$RepoDir" clean -xfd -f # to delete all untracked files in the main repo
if ($lastExitCode -ne "0") { if ($lastExitCode -ne "0") {
Write-Warning "'git clean' failed with exit code $lastExitCode, retrying once..." Write-Warning "'git clean' failed with exit code $lastExitCode, retrying once..."
@ -35,7 +35,7 @@ try {
if ($lastExitCode -ne "0") { throw "'git clean' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git clean' failed with exit code $lastExitCode" }
} }
"⏳ (4/4) Removing untracked files in the submodules..." "⏳ (4/4) Removing submodules' untracked files..."
& git -C "$RepoDir" submodule foreach --recursive git clean -xfd -f # to delete all untracked files in the submodules & git -C "$RepoDir" submodule foreach --recursive git clean -xfd -f # to delete all untracked files in the submodules
if ($lastExitCode -ne "0") { throw "'git clean' in the submodules failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git clean' in the submodules failed with exit code $lastExitCode" }

View File

@ -1,10 +1,10 @@
<# <#
.SYNOPSIS .SYNOPSIS
Fetches repo updates Fetches Git repo updates
.DESCRIPTION .DESCRIPTION
This PowerShell script fetches the latest updates into a local Git repository (including submodules). This PowerShell script fetches the latest updates into a local Git repository (including submodules).
.PARAMETER RepoDir .PARAMETER RepoDir
Specifies the path to the Git repository. Specifies the file path to the local Git repository (default is working directory).
.EXAMPLE .EXAMPLE
PS> ./fetch-repo C:\MyRepo PS> ./fetch-repo C:\MyRepo
.LINK .LINK
@ -22,11 +22,11 @@ try {
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Host "⏳ (2/3) Checking local repository... 📂$RepoDir" Write-Host "⏳ (2/3) Checking repository... 📂$RepoDir"
if (!(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" } if (!(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" }
$RepoDirName = (Get-Item "$RepoDir").Name $RepoDirName = (Get-Item "$RepoDir").Name
Write-Host "⏳ (3/3) Fetching latest updates... " Write-Host "⏳ (3/3) Fetching updates... "
& git -C "$RepoDir" fetch --all --recurse-submodules --prune --prune-tags --force & git -C "$RepoDir" fetch --all --recurse-submodules --prune --prune-tags --force
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }

View File

@ -26,9 +26,9 @@ try {
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } 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 repository... 📂$RepoDir"
Write-Host "⏳ (2/6) Checking Git repository... 📂$RepoDirName"
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" } if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
$RepoDirName = (Get-Item "$RepoDir").Name
"⏳ (3/6) Fetching latest updates..." "⏳ (3/6) Fetching latest updates..."
& git -C "$RepoDir" fetch --all --recurse-submodules --prune --prune-tags --force & git -C "$RepoDir" fetch --all --recurse-submodules --prune --prune-tags --force
@ -37,7 +37,7 @@ try {
$CurrentBranchName = (git -C "$RepoDir" rev-parse --abbrev-ref HEAD) $CurrentBranchName = (git -C "$RepoDir" rev-parse --abbrev-ref HEAD)
if ($lastExitCode -ne "0") { throw "'git rev-parse' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git rev-parse' failed with exit code $lastExitCode" }
"⏳ (4/6) Creating new branch..." "⏳ (4/6) Creating branch..."
& git -C "$RepoDir" checkout -b "$NewBranchName" & git -C "$RepoDir" checkout -b "$NewBranchName"
if ($lastExitCode -ne "0") { throw "'git checkout -b $NewBranchName' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git checkout -b $NewBranchName' failed with exit code $lastExitCode" }
@ -50,7 +50,7 @@ try {
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ created new branch '$NewBranchName' in 📂$RepoDirName repository (based on '$CurrentBranchName') in $Elapsed sec." "✔️ created branch '$NewBranchName' in repo 📂$RepoDirName (based on '$CurrentBranchName') in $Elapsed sec"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -1,6 +1,6 @@
<# <#
.SYNOPSIS .SYNOPSIS
Pulls repo updates Pulls Git repo updates
.DESCRIPTION .DESCRIPTION
This PowerShell script pulls the latest updates into a local Git repository (including submodules). This PowerShell script pulls the latest updates into a local Git repository (including submodules).
.PARAMETER RepoDir .PARAMETER RepoDir
@ -22,13 +22,13 @@ try {
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Host "⏳ (2/4) Checking local repository... 📂$RepoDir" Write-Host "⏳ (2/4) Checking repository... 📂$RepoDir"
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" } if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" }
$Result = (git -C "$RepoDir" status) $Result = (git -C "$RepoDir" status)
if ("$Result" -match "HEAD detached at ") { throw "Currently in detached HEAD state (not on a branch!), so nothing to pull" } if ("$Result" -match "HEAD detached at ") { throw "Currently in detached HEAD state (not on a branch!), so nothing to pull" }
$RepoDirName = (Get-Item "$RepoDir").Name $RepoDirName = (Get-Item "$RepoDir").Name
Write-Host "⏳ (3/4) Pulling latest updates..." Write-Host "⏳ (3/4) Pulling updates..."
& git -C "$RepoDir" pull --recurse-submodules=yes & git -C "$RepoDir" pull --recurse-submodules=yes
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
@ -37,7 +37,7 @@ try {
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ pulled updates into repo 📂$RepoDirName in $Elapsed sec" "✔️ updated repo 📂$RepoDirName in $Elapsed sec"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"