Skip to content

Instantly share code, notes, and snippets.

@aschrab
Created November 20, 2012 15:30
Show Gist options
  • Save aschrab/4118615 to your computer and use it in GitHub Desktop.
Save aschrab/4118615 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# Switch to top level of repository
base=$(git rev-parse --show-toplevel 2> /dev/null || true)
if [ -z "$base" ]; then
echo "Not in a git repository" >&2
exit 1
fi
cd $base
# Check for any uncommitted changes
# Even though they won't be included in the final commit,
# make sure that they'll be included in the history.
if git diff --quiet HEAD; then
:
else
echo "Uncommitted changes, aborting" >&2
exit 1
fi
# Get info about the branch that is to be merged
to_merge=$1
if [ "x$to_merge" = "x" ]; then
echo "No branch to merge specified" >&2
exit 1
fi
tree=$(git rev-parse --verify "$to_merge^{tree}")
# Ensure that there's something to merge
if [ -z "$(git rev-list -n 1 ..$to_merge)" ]; then
echo "Branch is up to date" >&2
exit
fi
# Generate commit message
commit_msg=.git/COMMIT_EDITMSG
echo "Updating to $to_merge" > $commit_msg
echo "" >> $commit_msg
git log --reverse --format=' * %s' "..$to_merge" >> $commit_msg
# Provide opportunity to edit commit message
editor $commit_msg
# Remove comment lines, and ensure that there's a commit message
perl -ni \
-e 'next if /^#/;' \
-e '$msg = 1 unless /^\s*$/;' \
-e 'print;' \
-e 'END { unless($msg) { print "No commit message, aborting\n"; exit 1 } }' \
$commit_msg
# Create the merge commit
merged=$(git commit-tree $tree -p HEAD -p $to_merge < $commit_msg)
# Update current branch to the new commit
git reset --hard $merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment