Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Merge helper script for using 'git mergetool' with meld, fixes
# Handles proper use of Meld from Git.
# Instead of launching meld with $MERGED as the base revision, this
# script makes a copy of $BASE and handles copying the result back
# to $MERGED if the result was saved.
# As an extra tweak, it also presents branch names (if known) as
# the file name prefix, instead of just "LOCAL" and "REMOTE".
# To use, place this script in your path, make it executable, then run
# git config --global merge.tool meldscript
# git config --global mergetool.meldscript.cmd $LOCAL $BASE $REMOTE $MERGED
WORK=$(mktemp -d)
FILE="$(basename $MERGED)"
mkdir "$OUTPUT"
# Creates temp file names which are prefixed with the branch name.
# TODO(mrenouf): If branches have no names (merging directly with a
# revision), then fall back to "REMOTE".
WORK_LOCAL="$WORK/$(git name-rev ORIG_HEAD | cut -d' ' -f 2)"
mkdir "$WORK_LOCAL"
WORK_REMOTE="$WORK/$(git name-rev MERGE_HEAD | cut -d' ' -f 2)"
mkdir "$WORK_REMOTE"
cp "$BASE" "$OUTPUT/${FILE}"
# meld doesn't appear to have any meaningful exit status
# so just check whether the target was saved, then give
# git an indication of this via exit status from this script
if [[ "$OUTPUT/${FILE}" -nt "$MERGED/${FILE}" ]]; then
rm -rf "$WORK"
exit 0
rm -rf "$WORK"
exit 255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment