#! /bin/bash function usage { echo "Usage: pick-to-branch [] Cherry-pick a commit on the given release target branch. If this is not the current branch, the current branch and its state are preserved. The commit can be given in the form of a branch name. If no arg is given, use the commit id of the HEAD of the master. The arg must match a release branch or start with 'm' for master. A release branch may be given simply as 102, 110, 111, 30, 31." } case $# in 2) id=$1 b=$2 ;; 1) id=`git show -s --format="%H" master` b=$1 ;; *) usage exit 1 ;; esac case $b in *1*0*2*) branch=OpenSSL_1_0_2-stable ;; *1*1*0*) branch=OpenSSL_1_1_0-stable ;; *1*1*1*) branch=OpenSSL_1_1_1-stable ;; *3*0*) branch=openssl-3.0 ;; *3*1*) branch=openssl-3.1 ;; m*) branch=master ;; *) echo Unknown release target branch \'$b\' exit 1 ;; esac echo "Commit to chery-pick is $id:" git show $id | head -n 5 echo echo "Target branch is: $branch" echo "Are both of these correct?" while true do echo -n "Enter 'y'/'yes' to continue or 'n'/'no' to abort: " read x x="`echo $x | tr A-Z a-z`" if [ "$x" = "y" -o "$x" = "yes" -o "$x" = "n" -o "$x" = "no" ] then break fi done if [ "$x" = "n" -o "$x" = "no" ] then exit 1 fi ORIG_REF=`git rev-parse --abbrev-ref HEAD` # usually this will be 'master' if [ "$branch" != "$ORIG_REF" ]; then STASH_OUT=`git stash` fi function cleanup { rv=$? echo # make sure to enter new line, needed, e.g., after Ctrl-C [ $rv -ne 0 ] && echo -e "pick-to-branch failed" if [ "$CHERRYPICKING" == 1 ] ; then git cherry-pick --abort 2>/dev/null || true fi if [ "$branch" != "$ORIG_REF" ]; then echo Returning to previous branch $ORIG_REF git checkout -q $ORIG_REF if [ "$STASH_OUT" != "No local changes to save" ]; then git stash pop -q # restore original state, pruning any leftover commits added locally fi fi } set -o errexit trap 'cleanup' EXIT git checkout --quiet master git checkout $branch git pull --ff-only CHERRYPICKING=1 git cherry-pick -e -x $id || (git cherry-pick --abort; exit 1) CHERRYPICKING= while true do echo -n "Enter 'y'/'yes' to push or 'n'/'no' to abort: " read x x="`echo $x | tr A-Z a-z`" if [ "$x" = "y" -o "$x" = "yes" -o "$x" = "n" -o "$x" = "no" ] then break fi done if [ "$x" = "y" -o "$x" = "yes" ] then git push else git reset --hard @~1 fi