Last active
August 29, 2015 14:06
-
-
Save deryni/8aa8d0164f620a8dcb7e to your computer and use it in GitHub Desktop.
Line+word diff for git changes. Created for http://stackoverflow.com/a/25620599/258523
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
BEGIN { | |
RS="\n?~\n" | |
FS="\n" | |
} | |
# Special case the diff header/chunk header lines. | |
/^diff --git/ { | |
next | |
} | |
{ | |
delete outs | |
for (i=1; i<=NF; i++) { | |
if ($i ~ /^[-+]/) { | |
mode = substr($i, 1, 1) | |
$i = ((mode=="-")?red:green) substr($i, 2) reset | |
outs[mode] = outs[mode] $i reset | |
outs["set" mode]++ | |
} else { | |
gsub(/^ /, "", $i) | |
outs["-"] = outs["-"] $i | |
outs["+"] = outs["+"] $i | |
} | |
} | |
# If we didn't have any changes then this is a context line and we need to | |
# print it out. | |
if (!outs["set-"] && !outs["set+"]) { | |
print " " outs["-"] | |
next | |
} | |
if (outs["set-"]) { | |
print red "-" reset outs["-"] | |
} | |
if (outs["set+"]) { | |
print green "+" reset outs["+"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The updated version handles things as best I believe can be handled given the fact that
--word-diff=porcelain
is known to have limitations surrounding whitespace and newlines.