Skip to content

Instantly share code, notes, and snippets.

@brutella
Last active July 30, 2019 07:50
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 brutella/fed36973a44c04ae1d16 to your computer and use it in GitHub Desktop.
Save brutella/fed36973a44c04ae1d16 to your computer and use it in GitHub Desktop.
Git diffs in your favourite diff tool
#!/bin/sh
function print_example() {
echo "Example"
echo " git-diff develop master"
}
function print_usage() {
echo "Usage"
echo " git-diff <branch|tag|commit> <branch|tag|commit>"
echo " OR"
echo " git-diff --cached"
}
if [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
print_usage
exit 0
fi
FROM="$1"
FROM_DESC="$FROM"
TO="$2"
TO_DESC="$TO"
if [ "$1" = "--cached" ] || [ "$1" = "-c" ] ; then
FROM=""
FROM_DESC="staged"
TO="HEAD"
TO_DESC="working dir"
elif [ -z "$FROM" ] || [ -z "$TO" ]; then
echo "Error: missing arguments"
echo ""
print_usage
echo ""
print_example
exit 1
fi
if ! [ -d .git ]; then # .git folder exists?
echo "Error: current directory is not a git root directory"
exit 1
fi
DIR=${TMPDIR%/}/gitdiff # `%/` removes trailing slash
rm -rf "$DIR"
for i in $(git diff --name-only $FROM $TO) ; do
# Setup folder structure
mkdir -p $DIR/"$FROM_DESC"/"$(dirname $i)";
mkdir -p $DIR/"$TO_DESC"/"$(dirname $i)";
# Get revision of files
git show "$FROM":"$i" > "$DIR"/"$FROM_DESC"/$i;
git show "$TO":"$i" > "$DIR"/"$TO_DESC"/$i;
done
# Use git's difftool or opendiff as alternative
DIFFTOOL=$(expr "$(git config --get diff.tool)" '|' "opendiff")
$($DIFFTOOL "$DIR"/"$FROM_DESC" "$DIR"/"$TO_DESC")
@brutella
Copy link
Author

brutella commented Feb 1, 2015

Examples

  • Branches diff git-diff master develop
  • Tags diff git-diff 1.0.0 1.0.1
  • Commits diff git-diff 089fe25 0357e69
  • Mixed diff git-diff 1.0.1 develop
  • Staged diff git-diff --cached

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