PowerShell/Scripts/cherry-picker.ps1

59 lines
2.2 KiB
PowerShell
Raw Normal View History

2021-03-16 15:27:00 +01:00
<#
2021-04-07 15:17:49 +02:00
.SYNTAX cherry-picker.ps1 [<commit-id>] [<commit-message>] [<branches>] [<repo-dir>]
2021-03-16 15:27:00 +01:00
.DESCRIPTION cherry-picks a Git commit into multiple branches
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param($CommitID = "", $CommitMessage = "", $Branches = "", $RepoDir = "$PWD")
2021-04-21 19:53:52 +02:00
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" }
2021-03-16 14:27:55 +01:00
try {
2021-03-16 15:27:00 +01:00
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
set-location "$RepoDir"
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
foreach($Branch in $Branches) {
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
"STEP: Switching to branch $Branch (git checkout)..."
& git checkout --recurse-submodules --force $Branch
if ($lastExitCode -ne "0") { throw "'git checkout' failed" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
"STEP: Updating submodules (git submodule update)..."
& git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
"STEP: Cleaning the repository (git clean -fdx)..."
& git clean -fdx
if ($lastExitCode -ne "0") { throw "'git clean -fdx' failed" }
2021-03-16 14:27:55 +01:00
& git submodule foreach --recursive git clean -fdx
if ($lastExitCode -ne "0") { throw "'git clean -fdx' in submodules failed" }
2021-03-16 15:27:00 +01:00
"STEP: Pulling latest updates (git pull)..."
& git pull --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
"STEP: Checking the status (git status)..."
$Result = (git status)
if ($lastExitCode -ne "0") { throw "'git status' failed" }
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Branch is NOT clean: $Result" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
& git cherry-pick --no-commit "$CommitID"
if ($lastExitCode -ne "0") { throw "'git cherry-pick $CommitID' failed" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
& git commit -m "$CommitMessage"
if ($lastExitCode -ne "0") { throw "'git commit' failed" }
2021-03-16 14:27:55 +01:00
2021-03-16 15:27:00 +01:00
& git push
if ($lastExitCode -ne "0") { throw "'git push' failed" }
}
"DONE."
exit 0
2021-03-16 14:27:55 +01:00
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-16 14:27:55 +01:00
exit 1
2021-03-16 15:27:00 +01:00
}