Skip to content

Instantly share code, notes, and snippets.

@patrick-higgins
Created February 26, 2014 00:09
Show Gist options
  • Save patrick-higgins/9220711 to your computer and use it in GitHub Desktop.
Save patrick-higgins/9220711 to your computer and use it in GitHub Desktop.
#!/bin/bash
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
OPTIONS_SPEC="\
release2 --old <old> --new <new> --base <base> <project1> <project2>...
release2 --continue
release2 recreates an integration branch, making it easy to remove branches
which proved unstable. Merge conflict resolutions are learned from the
existing branch and used when creating the new branch.
--
o,old= name of old branch to recreate
n,new= name of new branch to create
b,base= commit to base new branch on
continue resume merges
train train rerere first
"
. $(git --exec-path)/git-sh-setup
require_work_tree_exists
cd_to_toplevel
ensure_branch() {
branch="$1"
if ! git rev-parse -q --verify "refs/heads/$branch" >/dev/null ; then
if ! git rev-parse -q --verify "refs/remotes/origin/$branch" >/dev/null ; then
echo >&2 "Unknown branch: $branch"
exit 1
fi
git branch --track "$branch" "origin/$branch"
fi
}
old=
new=
base=
continue=
train=
while test $# != 0 ; do
case "$1" in
-b|--base)
test 2 -le "$#" || usage
base="$2"
shift
;;
-n|--new)
test 2 -le "$#" || usage
new="$2"
shift
;;
-o|--old)
test 2 -le "$#" || usage
old="$2"
shift
;;
--continue)
continue=1
;;
--train)
train=1
;;
--)
shift; break ;;
*)
echo "unknown opt $1"
usage ;;
esac
shift
done
# Make sure index and tree are clean
git diff-index --quiet HEAD || {
echo >&2 "You have uncommitted work. Exiting."
exit 1
}
if [ -n "$continue" ] ; then
new="$(cat $GIT_DIR/RELEASE2_NEW)"
set -- $(cat $GIT_DIR/RELEASE2_REMAINING)
fi
test $# -eq 0 && usage
# Check the branches
for prj in "$old" "$@" ; do
ensure_branch "$prj"
done
if [ -n "$continue" ] ; then
new="$(cat $GIT_DIR/RELEASE2_NEW)"
if [ "$(git symbolic-ref HEAD)" != "$new" ] ; then
echo >&2 "You are not on the branch you started on ($new)."
fi
else
# Make sure new branch does not exist
( git rev-parse -q --verify "refs/heads/$new" >/dev/null || \
git rev-parse -q --verify "refs/remotes/origin/$new" >/dev/null ) && {
echo >&2 "branch $new already exists"
exit 1
}
# Create new branch and check it out
git checkout -b "$new" "$base"
if [ -n "$train" ] ; then
# Record existing resolutions
git rerere-train "$old" "^$base"
fi
fi
# Merge each one
while test $# != 0 ; do
git merge "$1" || {
echo $* > $GIT_DIR/RELEASE2_NEW
echo $* > $GIT_DIR/RELEASE2_REMAINING
echo >&2 "Re-run with --continue the conflicts are fixed."
exit 1
}
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment