Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Last active December 26, 2015 13:39
Show Gist options
  • Save Idorobots/7160066 to your computer and use it in GitHub Desktop.
Save Idorobots/7160066 to your computer and use it in GitHub Desktop.
A handy replacement for plain `git merge`.
#! /bin/sh
# This little script will automatically test feature-branch code
# before it is actually merged into the master branch.
# Just alias it as so:
# git config --global alias.safe-merge '!exec $HOME/git_safe_merge.sh'
LAST_BRANCH=`git rev-parse --abbrev-ref HEAD`
TEMP_BRANCH=`date +"$LAST_BRANCH-%Y%m%d%H%M"`
# Store any working changes.
# git stash
# Create a new, temporary branch.
git checkout -b $TEMP_BRANCH
# Merge new changes...
if git merge --no-ff $@
then
# ...run tests to make sure new code is ok.
if [ ! -f ./runtests.sh ] || ./runtests.sh
then
# If so, fast-forward the main branch.
git checkout $LAST_BRANCH
git rebase $TEMP_BRANCH
else
# Otherwise discard the changes.
git checkout -q -f $LAST_BRANCH
fi
else
# Otherwise discard the changes.
git checkout -q -f $LAST_BRANCH
fi
# Remove the temporary branch.
git branch -D $TEMP_BRANCH
# Restore working changes.
# git stash pop
@Idorobots
Copy link
Author

Success output:

Switched to a new branch 'master-201310252114'
Merge made by recursive.
 baz |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
[PASSED] Running tests!
Switched to branch 'master'
First, rewinding head to replay your work on top of it...
Fast-forwarded master to master-201310252114.
Deleted branch master-201310252114 (was cc28c3c).

Merge conflict:

Switched to a new branch 'master-201310252114'
Auto-merging baz
CONFLICT (content): Merge conflict in baz
Automatic merge failed; fix conflicts and then commit the result.
Deleted branch master-201310252114 (was cc28c3c).

Test failure:

Switched to a new branch 'master-201310252115'
Merge made by recursive.
 baz         |    2 +-
 runtests.sh |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
[FAILED] Running tests!
Deleted branch master-201310252115 (was 70b1b4f).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment