Skip to content

Instantly share code, notes, and snippets.

@trevren11
Created August 4, 2020 19:23
Show Gist options
  • Save trevren11/822adadb7b0ff3e55efc6501aa8e04e2 to your computer and use it in GitHub Desktop.
Save trevren11/822adadb7b0ff3e55efc6501aa8e04e2 to your computer and use it in GitHub Desktop.
Cherry-pick commits into servicing
function CherryPickCommitsToServicing() {
param
(
[Parameter(Mandatory=$true, HelpMessage="Servicing branch to base off of, should be in the format 'servicing/20xx-1.1'")] [string] $ServicingBranch,
[Parameter(Mandatory=$true, HelpMessage="Branch name to make cherry pick to, should be in the format 'user/<user>/<branchName>'")] [string] $CherryPickBranch,
[Parameter(Mandatory=$true, HelpMessage="Message commit for pull request")] [string] $CommitMessage,
[Parameter(Mandatory=$true, HelpMessage="One or multiple commit hashes, all encased by quotations marks and space separated, hash will be applied first to last -> 'hash1 hash2' ")] [string] $CommitHashes,
[Parameter(HelpMessage="Add flag to disable opening chrome to branches tab to create a pr")] [switch] $DontOpenChrome,
[Parameter(HelpMessage="Add flag to disable automatically pushing to hotfix branch")] [switch] $DontPushToBranch
)
git checkout master
git pull
$s = $(git checkout $ServicingBranch) 2>&1
if ($s | findstr.exe "error"){
write-host -ForegroundColor Red "Could not find branch $ServicingBranch, please check the name and try again"
return
}
git pull
git checkout -B $CherryPickBranch
$commits = $CommitHashes.Split(' ')
write-host "Applying the following commits in this order" -ForegroundColor Blue
write-host $commits -ForegroundColor Green
foreach($commit in $commits){
write-host "Applying $commit" -ForegroundColor Blue
$cleanCherryPick = $(git cherry-pick $commit) 2>&1
write-host $cleanCherryPick
if ( $($cleanCherryPick | findstr.exe "CONFLICT") -or $($cleanCherryPick | findstr.exe "error") -or $($cleanCherryPick | findstr.exe "fatal")){
write-host -ForegroundColor Red "Conflict with cherry-picking $commit, please resolve manually, do not use this function for further cherry-picks in the commit list"
write-host -ForegroundColor Red "Since there are problems, there is likely some other code missing from the cherry-pick, please look at the history to ensure all code is present"
return
}
}
write-host "Succesfully cherry-picked commits" -ForegroundColor Green
git commit -m $CommitMessage
if (!$DontPushToBranch){
git push --set-upstream origin $(git branch --show-current)
}
if (!$DontOpenChrome){
write-host "open chrome"
& start chrome "$(git config --get remote.origin.url)/branches"
}
}
New-Alias -Name cherryPick -Value CherryPickCommitsToServicing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment