PowerShell/Scripts/pick-commit.ps1

82 lines
2.9 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Cherry-picks a Git commit into one or more branches
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)
NOTE: in case of merge conflicts the script stops immediately!
2021-10-16 16:50:10 +02:00
.PARAMETER CommitID
Specifies the commit ID
.PARAMETER CommitMessage
Specifies the commit message to use
.PARAMETER Branches
Specifies the list of branches, separated by spaces
.PARAMETER RepoDir
Specifies the path to the Git repository
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-10-06 09:33:45 +02:00
PS> ./pick-commit 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-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
}