Skip to content

Instantly share code, notes, and snippets.

@smunilla
Last active January 13, 2017 15:27
Show Gist options
  • Save smunilla/9509dedb0414b801bb63737e4ebd4eb0 to your computer and use it in GitHub Desktop.
Save smunilla/9509dedb0414b801bb63737e4ebd4eb0 to your computer and use it in GitHub Desktop.
#!/bin/bash
######################################################################
# ocp-merge: Merge origin to enterprise in preparation for a release
#
# Usage: Takes no options. Just run it.
#
# Note: This is heavily based on versionmerge which was originally
# written by Tim Bielawa.
#
# smunilla@redhat.com
######################################################################
if [ "${1}" = "--help" -o "${1}" = "-h" ]; then
cat <<EOF
usage: ${0}
default behavior - parenting fixed, prompted to review diffs at end
usage: ${0} {GIT-DIFF ARGS}
pass arbitrary args/options to the git-diff at the end
Attempts to maintain the proper qa->stage->master merge history that
we want to see.
In other words: fix 'current stage is not a child of..' errors.
Your branches must be named 'qa', 'stage', and 'master' for this to
work. The correct remote will be figured out automatically.
You may optionally set/erase the default git-diff options:
SET default diff options:
${0} --set-diff-options -b
${0} --set-diff-options --no-color --stat
ERASE default diff options:
${0} --erase-diff-options
EOF
exit 0;
fi
function get_diff_options() {
git config --get merge.versionmerge.diffoptions
}
function has-branch {
branch=$1
git branch --no-color | grep -q " ${branch}$"
}
thisbranch() {
git branch --no-color | sed -r -n "s/(\* )(.*)/\2/p"
}
function fix-branch {
branch=$1
parent=$2
git checkout --quiet ${1}
${RESET}${1}
git merge -m "merging ${2} into ${1}" --quiet ${2}
break_on_conflict $?
}
RESET=": "
hr() {
cat <<EOF
#############################################
EOF
}
if [ "${1}" = "--set-diff-options" ]; then
shift
git config --global --unset merge.versionmerge.diffoptions
echo "Setting default diff options to: '${*}'"
git config --global merge.versionmerge.diffoptions "$*"
echo "Default diff options are now set to: '`get_diff_options`'"
exit 0
elif [ "${1}" = "--erase-diff-options" ]; then
git config --global --unset merge.versionmerge.diffoptions
echo "Default diff options have been erased"
exit 0
fi
if [[ ! -z ${1} ]]; then
shift
to_merge=${1}
else
echo "Must specify a remote repo to merge"
exit 1
fi
list_conflicts() {
git status | grep "both modified:" | tr "#" " "
}
break_on_conflict() {
if [[ "${1}" -ne "0" ]]; then
cat <<EOF
>>>>> Merge Conflict Detected! <<<<<
To continue:
1. resolve these conflicts:
`list_conflicts`
2. 'git add' the changed files
3. 'git commit'
4. 'ocp-merge'
You may have to repeat this process if content is conflicting.
THIS IS EXPECTED BEHAVIOR.
Continue until ocp-merge runs straight through.
EOF
exit 1
fi
}
STARTBRANCH=`thisbranch`
git fetch --all
echo "Aligning all your branches now..."
hr
if has-branch master; then
echo "Aligning master"
fix-branch master $to_merge
fi
hr
git checkout --quiet ${STARTBRANCH}
cat <<EOF
Back on ${STARTBRANCH} and all lined up!
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment