Skip to content

Instantly share code, notes, and snippets.

@jmarshall
Created February 10, 2025 00:05
Show Gist options
  • Save jmarshall/6185cb7f90b78a908f7174de41cf4ef7 to your computer and use it in GitHub Desktop.
Save jmarshall/6185cb7f90b78a908f7174de41cf4ef7 to your computer and use it in GitHub Desktop.
Git-enabling wrapper script for opendiff
#!/bin/bash
tmpdir=/tmp/opendiff-repo
fetch () {
case $1 in
*:*)
mkdir -p $tmpdir
commit=${1%%:*}
filename=$(git ls-files --full-name --with-tree $commit ${1#*:})
hash=$(git rev-list -n1 --abbrev-commit $commit)
case `git rev-parse --symbolic-full-name $commit`//$commit in
refs/tags/*) desc=${commit//\//_} ;;
refs/*//HEAD) desc=$hash ;;
refs/*) desc=${commit//\//_}@$hash ;;
*) desc=$hash ;;
esac
tmp=$tmpdir/$desc:${1##*[:/]}
git show $commit:$filename > $tmp
echo $tmp
;;
*) if [ -e "$1" ]
then
echo $1
else
mkdir -p $tmpdir
commit=$1
tmp=$tmpdir/commit:${commit//\//_}
git show --format=fuller $commit > $tmp
echo $tmp
fi
;;
esac
}
if [ $# -eq 1 ]
then
/usr/bin/opendiff $(fetch "HEAD:$1") "$1"
else
/usr/bin/opendiff $(fetch "$1") $(fetch "$2")
fi
@jmarshall
Copy link
Author

This can be used to compare current or different versions of a file or to compare two commits:

  • opendiff FILE compares the file in the current HEAD commit with its current state on disk.
  • opendiff COMMIT1:FILE COMMIT2:FILE compares the file in the specified commits — either prefix may be left off to compare the current file on disk, and different files can be compared if you really want to.
  • opendiff COMMIT1 COMMIT2 compares two extant commits, useful when rebasing or cherry-picking, etc.

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