M-A's

technology blog

Monday, 14 June 2010

automatic git mergetool selection based on X availability

I usually edit code over ssh, when merging, vimdiff is sufficient to me most of the time but when the merge is too complex, kdiff3 does a better job so I NX into the box and start the merge from there. I got tired of using git mergetool=kdiff3 so I wrote this tool. Note that vimdiff is started in a different configuration that would be done in a normal git config merge.tool = vimdiff; cursor is reset to line 0 and the viewport is modified in a more useful format, plus all the 4 buffers are loaded.
#!/bin/sh
# Copyright (c) 2010 Marc-Antoine Ruel
# Automatically select the right merge tool based on X session availability.

if [ ! "$(git config --global --get mergetool.autodiff.cmd)" ]; then
  echo Setting git config --global merge.tool = autodiff
  git config --global mergetool.autodiff.cmd "`pwd`/autodiff.sh \$BASE \$LOCAL \$REMOTE \$MERGED"
  git config --global merge.tool autodiff
  echo Done.
  exit
fi

if [ -z "$DISPLAY" ]; then
  # Start in diff mode, move the viewport down and move up to first line.
  vimdiff -c "wincmd J" -c "0" $4 $2 $1 $3
else
  # Inside an X session, redirect stderr.
  kdiff3 --auto --L1 "$4 (Base)" --L2 "$4 (Local)" --L3 "$4 (Remote)" -o $4 $1 $2 $3 2> /dev/null
fi

No comments: