PowerShell/Scripts/cherry-picker.ps1

67 lines
2.3 KiB
PowerShell
Raw Normal View History

2021-03-16 15:27:00 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
cherry-picker.ps1 [<commit-id>] [<commit-message>] [<branches>] [<repo-dir>]
.DESCRIPTION
Cherry-picks a Git commit into multiple branches
.EXAMPLE
PS> .\cherry-picker.ps1 93849f889 "Fix typo" v1 v2 v3
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz / License: CC0
2021-03-16 15:27:00 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$CommitID = "", [string]$CommitMessage = "", [string]$Branches = "", [string]$RepoDir = "$PWD")
2021-03-16 14:27:55 +01:00
try {
2021-07-15 15:51:22 +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 separated by spaces" }
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-07-07 11:06:45 +02:00
$Branches = $Branches.Split(' ')
2021-03-16 15:27:00 +01:00
foreach($Branch in $Branches) {
2021-03-16 14:27:55 +01:00
2021-07-07 11:06:45 +02:00
"Switching to branch $Branch (git checkout)..."
2021-03-16 15:27:00 +01:00
& git checkout --recurse-submodules --force $Branch
if ($lastExitCode -ne "0") { throw "'git checkout' failed" }
2021-03-16 14:27:55 +01:00
2021-07-07 11:06:45 +02:00
"Updating submodules (git submodule update)..."
2021-03-16 15:27:00 +01:00
& git submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed" }
2021-03-16 14:27:55 +01:00
2021-07-07 11:06:45 +02:00
"Cleaning the repository (git clean -fdx)..."
& git clean -fdx -f
if ($lastExitCode -ne "0") { throw "'git clean' failed" }
2021-03-16 15:27:00 +01:00
2021-07-07 11:06:45 +02:00
& git submodule foreach --recursive git clean -fdx -f
if ($lastExitCode -ne "0") { throw "'git clean' in submodules failed" }
2021-03-16 14:27:55 +01:00
2021-07-07 11:06:45 +02:00
"Pulling latest updates (git pull)..."
2021-03-16 15:27:00 +01:00
& git pull --recurse-submodules
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
2021-03-16 14:27:55 +01:00
2021-07-07 11:06:45 +02:00
"Checking the status (git status)..."
2021-03-16 15:27:00 +01:00
$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-07-07 11:06:45 +02:00
"Cherry-picking and committing..."
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
}