2021-09-27 10:38:12 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2021-09-01 09:51:32 +02:00
|
|
|
|
cherry-picker.ps1 [<CommitID>] [<CommitMessage>] [<Branches>] [<RepoDir>]
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.DESCRIPTION
|
2021-09-24 17:19:49 +02:00
|
|
|
|
Cherry-picks a Git commit into one or more branches (branch names need to be separated by spaces)
|
2021-07-28 14:48:57 +02:00
|
|
|
|
NOTE: in case of merge conflicts the script stops immediately!
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2021-09-24 17:19:49 +02:00
|
|
|
|
PS> ./cherry-picker 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
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02:00
|
|
|
|
"🍒 Switching to branch $Branch ..."
|
2021-03-16 15:27:00 +01:00
|
|
|
|
& git checkout --recurse-submodules --force $Branch
|
2021-07-28 14:48:57 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git checkout $Branch' failed" }
|
2021-03-16 14:27:55 +01:00
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02:00
|
|
|
|
"🍒 Cleaning the repository from untracked files..."
|
2021-07-07 11:06:45 +02:00
|
|
|
|
& git clean -fdx -f
|
2021-07-28 14:48:57 +02:00
|
|
|
|
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
|
2021-07-28 14:48:57 +02:00
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git clean -fdx -f' in submodules failed" }
|
2021-03-16 14:27:55 +01:00
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02: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
|
|
|
|
|
2021-07-28 14:48:57 +02:00
|
|
|
|
"🍒 Pushing..."
|
2021-03-16 15:27:00 +01:00
|
|
|
|
& git push
|
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git push' failed" }
|
|
|
|
|
}
|
2021-07-28 14:48:57 +02:00
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
|
|
|
|
"✔️ cherry picked $CommitID into $NumBranches branches in $Elapsed sec"
|
2021-09-27 10:09:45 +02:00
|
|
|
|
exit 0 # success
|
2021-03-16 14:27:55 +01:00
|
|
|
|
} catch {
|
2021-09-16 20:19:10 +02:00
|
|
|
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
2021-03-16 14:27:55 +01:00
|
|
|
|
exit 1
|
2021-03-16 15:27:00 +01:00
|
|
|
|
}
|