PowerShell/Scripts/cherry-picker.ps1

74 lines
2.7 KiB
PowerShell
Raw Normal View History

<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
cherry-picker.ps1 [<CommitID>] [<CommitMessage>] [<Branches>] [<RepoDir>]
2021-07-13 21:10:02 +02:00
.DESCRIPTION
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!
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\cherry-picker.ps1 93849f889 "Fix typo" "v1 v2 v3"
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
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-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
if ($CommitID -eq "") { $CommitID = read-host "Enter the Git 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 branches (separated by spaces)" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$BranchArray = $Branches.Split(" ")
$NumBranches = $BranchArray.Count
foreach($Branch in $BranchArray) {
2021-03-16 14:27:55 +01:00
"🍒 Switching to branch $Branch ..."
2021-03-16 15:27:00 +01:00
& git checkout --recurse-submodules --force $Branch
if ($lastExitCode -ne "0") { throw "'git checkout $Branch' failed" }
2021-03-16 14:27:55 +01:00
"🍒 Updating submodules..."
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
"🍒 Cleaning the repository from untracked files..."
2021-07-07 11:06:45 +02:00
& git clean -fdx -f
if ($lastExitCode -ne "0") { throw "'git clean -fdx -f' 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 -fdx -f' in submodules failed" }
2021-03-16 14:27:55 +01:00
"🍒 Pulling latest updates..."
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
"🍒 Checking the 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
"🍒 Cherry picking..."
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
"🍒 Committing..."
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
"🍒 Pushing..."
2021-03-16 15:27:00 +01:00
& git push
if ($lastExitCode -ne "0") { throw "'git push' failed" }
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ cherry picked $CommitID into $NumBranches branches in $Elapsed sec"
2021-03-16 15:27:00 +01:00
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
}