Skip to content

Instantly share code, notes, and snippets.

@rpavlik
Created January 16, 2012 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpavlik/1622031 to your computer and use it in GitHub Desktop.
Save rpavlik/1622031 to your computer and use it in GitHub Desktop.
Script to back/forward-port a patch that has its own topic branch. Intended for VR Juggler, but can be adapted.
#!/bin/bash
# Example:
# Assume you have a fix for the 3.0 stable branch on a topic branch called
# my-fix-3.0 that would also be useful to have on the master branch.
# If your workflow isn't such that you could just merge it into both,
# use this script to transplant your my-fix-3.0 onto master in a branch
# named my-fix-master. If you're on my-fix-3.0 already, then it's as easy
# as ./port-patch.sh
# Call like ./port-patch.sh for the current branch or ./port-patch
# somebranch anotherbranch .. to port multiple branches at once.
# Set the REMOTE environment variable if 'origin' isn't your upstream.
# If you're not working on VR Juggler, you'll probably need to change all
# mentions of 3.0 (aka the stable branch) and master (as well as its pre-Git
# name, trunk) to what you want.
# You might need to resolve some merge conflicts manually during the rebase -
# just follow the instructions and you'll be fine.
# Original Author:
# 2012 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2012.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
remote=${REMOTE:=origin}
function port_branch() {
source_branch=$1
source_root_indicator="$(echo ${source_branch}|grep --only-matching -e '3.0' -e 'master' -e 'trunk')"
case "${source_root_indicator}" in
3.0)
source_root=3.0
target_root=master
;;
master,trunk)
source_root=master
target_root=3.0
;;
*)
echo "Neither 3.0 nor master were in the current branch name, so I don't know which way to port. Try using git branch -m to rename your branch, then try again."
exit 1
;;
esac
target_branch=$(echo "${source_branch}" | sed "s:${source_root_indicator}:${target_root}:")
full_target_root="${remote}/${target_root}"
full_source_root="${remote}/${source_root}"
echo
echo "Porting from ${source_branch} to ${target_branch}, using ${full_target_root} as the new base..."
git branch "${target_branch}" "${source_branch}" && git rebase --onto "${full_target_root}" "${full_source_root}" "${target_branch}"
# This part is very specific to Ryan's needs - it automatically prints the compare URLs to paste in bug reports.
# Of course, it doesn't actually push the branches for you.
if [ "${USER}" == "rpavlik" ]; then
echo "**** COMPARE URLS ****"
echo "https://github.com/rpavlik/vrjuggler/compare/${source_root}...${source_branch}"
echo "https://github.com/rpavlik/vrjuggler/compare/${target_root}...${target_branch}"
echo
fi
}
if [ $# -gt 1 ]; then
branches=$*
else
branches=$(git symbolic-ref -q HEAD | sed 's_^refs/heads/__')
fi
for b in ${branches}; do
port_branch "${b}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment