Skip to content

Instantly share code, notes, and snippets.

@adamlazik1
Created February 27, 2023 17:48
Show Gist options
  • Save adamlazik1/41662f8dbdf125b0d4b7289241a7ad9b to your computer and use it in GitHub Desktop.
Save adamlazik1/41662f8dbdf125b0d4b7289241a7ad9b to your computer and use it in GitHub Desktop.
Script for cherry-picking to version branches on foreman-documentation repository

What it does

The script cherry-picks a required number of the latest commits from the master branch to required branches. Additionally, the script copies a command to your clipboard, which when used, will push the branches with cherry-picked commits to upstream repository and will store a formatted message in your clipboard, which you then just have to paste in a comment when merging and cherry-picking a PR.

The formatted message has the following format:

Merged and cherry-picked:
  <BRANCH1_OLD_LATEST_COMMIT>..<BRANCH1_NEW_LATEST_COMMIT> -> <BRANCH1_NAME>
  <BRANCH2_OLD_LATEST_COMMIT>..<BRANCH2_NEW_LATEST_COMMIT> -> <BRANCH2_NAME>
  ...

Prerequisites

  • The script uses the xclip utility to copy first the push command and then the formatted message to your clipboard. This utility is normally not included in the base Linux installation so you'll need to install it if you haven't done so yet.
  • The script uses the bash shell

Issues

The script stops if the was a merge-conflict when cherry-picking, but will still paste the push command to your clipboard. That being said, the push command does not do anything to a specific branch if new changes were not committed to it.

Installation

Create a file in your ~/bin/ directory a paste the contents of the script to it. You can name it whatever you'd like.

Usage

  1. Run the script
$ SCRIPT NCOMMITS BRANCHES
  • SCRIPT is how you name the script. As I am only pasting a code here, not the file, you can name it whatever you want.
  • NCOMMITS - number of the latest commits you want to cherry-pick
  • BRANCHES - branches you want to cherry-pick to
  1. If the cherry-picking is executed successfully (no merge conflicts), paste the command from your clipboard to the terminal (CTRL + SHIFT + V) and run the command.
  2. Paste the formatted message from your clipboard to the comment of the merged PR.

Example use

  • Cherry-pick two latest commits from master to 3.5 and 3.4:
    foreman-cherry-pick 2 3.5 3.4
    
  • Cherry-pick the latest commit from master to branches 3.5 through 3.1:
    foreman-cherry-pick 1 3.{5..1}
    

Disclaimer

Script is in no way optimized nor written in a pretty way, but it works for me and maybe it can help you too. You can easily optimize it to suit your needs. Enjoy!

#!/bin/bash
i=1
BOTTOM=master
while [ $i -le $1 ]
do
BOTTOM+=^
i=$(( $i + 1 ))
done
COMMITS="${BOTTOM}..master" ; shift
BRANCHES=$*
REMOTE=upstream
if [[ -z $COMMITS ]] ; then
echo "Usage: $0 NCOMMITS BRANCHES"
echo " NCOMMITS number of commits"
echo " BRANCHES branches to cherry-pick the commits to"
exit 1
fi
git checkout master && git pull
for branch in $BRANCHES ; do
git checkout $branch
git pull
git cherry-pick -x $COMMITS
done
echo The following has been copied to you clipboard:
echo "git push $REMOTE $BRANCHES 2>&1 | tail -n $# | awk '{if (NR == 1) printf(\"Merged and cherry-picked:\n\"); print}' | xclip -sel clip"
echo "git push $REMOTE $BRANCHES 2>&1 | tail -n $# | awk '{if (NR == 1) printf(\"Merged and cherry-picked:\n\"); print}' | xclip -sel clip" | xclip -sel clip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment