Last active
July 30, 2019 07:50
-
-
Save brutella/fed36973a44c04ae1d16 to your computer and use it in GitHub Desktop.
Git diffs in your favourite diff tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples
git-diff master develop
git-diff 1.0.0 1.0.1
git-diff 089fe25 0357e69
git-diff 1.0.1 develop
git-diff --cached