Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script for easy cherry-picking to version branches #2011

Closed
adamlazik1 opened this issue Feb 21, 2023 · 2 comments
Closed

Script for easy cherry-picking to version branches #2011

adamlazik1 opened this issue Feb 21, 2023 · 2 comments

Comments

@adamlazik1
Copy link
Contributor

I am going to leave this here for a week in case anyone finds it useful.
I have created a simple script for myself and I am sharing it here in case anyone wants to use it. The script was inspired and built upon @ekohl's cherry-picking script.

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
@ekohl
Copy link
Member

ekohl commented Feb 21, 2023

Interesting approach for the number of commits. I always like to review them before I pick. Consider adding git log --oneline $COMMITS.

I also use set -e so it fails hard. Right you when there is a conflict you may end up with partial cherry picks. Usually when you fail to pick to 3.x, 3.(x-1) also fails.

And for Wayland users there's wl-copy instead of xclip.

@adamlazik1
Copy link
Contributor Author

Moved to https://gist.github.com/adamlazik1/41662f8dbdf125b0d4b7289241a7ad9b if anyone ever needs it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants