Skip to content

Instantly share code, notes, and snippets.

@paxan
Last active December 11, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxan/4676526 to your computer and use it in GitHub Desktop.
Save paxan/4676526 to your computer and use it in GitHub Desktop.
A script that launches OS X FileMerge to inspect Git diffs

Intro

Sometimes you may want a better way to inspect/review Git diffs than what GitX and other tools provide (which is basically a glorified pretty printed linear view of diff hunks).

If you're on a Mac, try "gdiff" shell script. It's a wrapper that launches OS X FileMerge application to inspect diffs.

Installation

Just place gdiff somewhere in your PATH, chmod +x it too.

Usage

You should invoke gdiff just like git diff from inside a repo directory. Here are some things you can do with it:

  1. see the changes staged for commit: gdiff --cached
  2. compare two versions of the source tree: gdiff branch1 branch2

So basically it either accepts one argument (--cached) or two arguments (which both are whatever Git considers as names of commits, e.g. commit SHA1-s, branch names, tag names, &c.)

Here's another example. Say you want to see what's new in master:

git fetch
gdiff master origin/master
#!/bin/sh
#
# I think this is a variation of a script discussed at:
# http://kerneltrap.org/mailarchive/git/2007/11/21/435536
#
# Filemerge.app must not already be open before running
# this script, or opendiff below will return immediately,
# and the TMPDIRs deleted before it gets the chance to read
# them.
if test $# = 0; then
OLD=`git write-tree`
elif test "$1" = --cached; then
OLD=HEAD
NEW=`git write-tree`
shift
fi
if test $# -gt 0; then
OLD="$1"; shift
fi
test $# -gt 0 && test -z "$CACHED" && NEW="$1"
TMPDIR1=`mktemp -d /tmp/gdiff-XXXXX`
git archive --format=tar $OLD | (cd $TMPDIR1; tar xf -)
if test -z "$NEW"; then
TMPDIR2=$(git rev-parse --show-cdup)
test -z "$cdup" && TMPDIR2=.
else
TMPDIR2=`mktemp -d /tmp/gdiff-XXXXX`
git archive --format=tar $NEW | (cd $TMPDIR2; tar xf -)
fi
opendiff $TMPDIR1 $TMPDIR2 | cat
rm -rf $TMPDIR1
test ! -z "$NEW" && rm -rf $TMPDIR2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment