Improve cherry-picker.ps1 and list-cli-tools.ps1

This commit is contained in:
Markus Fleschutz 2021-07-28 14:48:57 +02:00
parent 1da9bf6cb3
commit f7d5d366f7
2 changed files with 34 additions and 24 deletions

View File

@ -1,10 +1,11 @@
<# <#
.SYNOPSIS .SYNOPSIS
cherry-picker.ps1 [<commit-id>] [<commit-message>] [<branches>] [<repo-dir>] cherry-picker.ps1 [<commit-id>] [<commit-message>] [<branches>] [<repo-dir>]
.DESCRIPTION .DESCRIPTION
Cherry-picks a Git commit into multiple branches Cherry-picks a Git commit into one or more branches (branch names need to be separated by spaces).
NOTE: in case of merge conflicts the script stops immediately!
.EXAMPLE .EXAMPLE
PS> .\cherry-picker.ps1 93849f889 "Fix typo" v1 v2 v3 PS> .\cherry-picker.ps1 93849f889 "Fix typo" "v1 v2 v3"
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -14,51 +15,57 @@
param([string]$CommitID = "", [string]$CommitMessage = "", [string]$Branches = "", [string]$RepoDir = "$PWD") param([string]$CommitID = "", [string]$CommitMessage = "", [string]$Branches = "", [string]$RepoDir = "$PWD")
try { try {
if ($CommitID -eq "") { $CommitID = read-host "Enter the commit id to cherry-pick" }
if ($CommitMessage -eq "") { $CommitMessage = read-host "Enter the commit message to use" }
if ($Branches -eq "") { $Branches = read-host "Enter the target branches separated by spaces" }
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" }
set-location "$RepoDir" set-location "$RepoDir"
$Branches = $Branches.Split(' ') if ($CommitID -eq "") { $CommitID = read-host "Enter the Git commit id to cherry-pick" }
foreach($Branch in $Branches) { if ($CommitMessage -eq "") { $CommitMessage = read-host "Enter the commit message to use" }
if ($Branches -eq "") { $Branches = read-host "Enter the branches (separated by spaces)" }
"Switching to branch $Branch (git checkout)..." $StopWatch = [system.diagnostics.stopwatch]::startNew()
$BranchArray = $Branches.Split(" ")
$NumBranches = $BranchArray.Count
foreach($Branch in $BranchArray) {
"🍒 Switching to branch $Branch ..."
& git checkout --recurse-submodules --force $Branch & git checkout --recurse-submodules --force $Branch
if ($lastExitCode -ne "0") { throw "'git checkout' failed" } if ($lastExitCode -ne "0") { throw "'git checkout $Branch' failed" }
"Updating submodules (git submodule update)..." "🍒 Updating submodules..."
& git submodule update --init --recursive & git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" } if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
"Cleaning the repository (git clean -fdx)..." "🍒 Cleaning the repository from untracked files..."
& git clean -fdx -f & git clean -fdx -f
if ($lastExitCode -ne "0") { throw "'git clean' failed" } if ($lastExitCode -ne "0") { throw "'git clean -fdx -f' failed" }
& git submodule foreach --recursive git clean -fdx -f & git submodule foreach --recursive git clean -fdx -f
if ($lastExitCode -ne "0") { throw "'git clean' in submodules failed" } if ($lastExitCode -ne "0") { throw "'git clean -fdx -f' in submodules failed" }
"Pulling latest updates (git pull)..." "🍒 Pulling latest updates..."
& git pull --recurse-submodules & git pull --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git pull' failed" } if ($lastExitCode -ne "0") { throw "'git pull' failed" }
"Checking the status (git status)..." "🍒 Checking the status..."
$Result = (git status) $Result = (git status)
if ($lastExitCode -ne "0") { throw "'git status' failed" } if ($lastExitCode -ne "0") { throw "'git status' failed" }
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Branch is NOT clean: $Result" } if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Branch is NOT clean: $Result" }
"Cherry-picking and committing..." "🍒 Cherry picking..."
& git cherry-pick --no-commit "$CommitID" & git cherry-pick --no-commit "$CommitID"
if ($lastExitCode -ne "0") { throw "'git cherry-pick $CommitID' failed" } if ($lastExitCode -ne "0") { throw "'git cherry-pick $CommitID' failed" }
"🍒 Committing..."
& git commit -m "$CommitMessage" & git commit -m "$CommitMessage"
if ($lastExitCode -ne "0") { throw "'git commit' failed" } if ($lastExitCode -ne "0") { throw "'git commit' failed" }
"🍒 Pushing..."
& git push & git push
if ($lastExitCode -ne "0") { throw "'git push' failed" } if ($lastExitCode -ne "0") { throw "'git push' failed" }
} }
"DONE." [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ cherry picked $CommitID into $NumBranches branches in $Elapsed sec"
exit 0 exit 0
} catch { } catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -17,10 +17,13 @@ function CheckFor { param([string]$Cmd, [string]$VersionArg)
$Version = $Info.Version $Version = $Info.Version
$Location = $Info.Source $Location = $Info.Source
if ($Version -eq "0.0.0.0") { if ($Version -eq "0.0.0.0") {
$Result = invoke-expression "$Location $VersionArg" if ("$VersionArg" -ne "") {
if ($Result -match '\d+\.\d+') { $Result = invoke-expression "$Location $VersionArg"
$Version = "$($Matches[0])" if ($Result -match '\d+\.\d+') {
} else { $Version = "$($Matches[0])"
} else {
$Version = "?"
} else
$Version = "?" $Version = "?"
} }
} }
@ -85,7 +88,7 @@ function ListTools {
CheckFor regedit "--version" CheckFor regedit "--version"
CheckFor replace "--version" CheckFor replace "--version"
CheckFor robocopy "--version" CheckFor robocopy "--version"
CheckFor rsh "--version" CheckFor rsh ""
CheckFor rsync "--version" CheckFor rsync "--version"
CheckFor rundll32 "--version" CheckFor rundll32 "--version"
CheckFor scp "--version" CheckFor scp "--version"