Skip to content

Instantly share code, notes, and snippets.

@deryni
Last active August 29, 2015 14:06
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 deryni/8aa8d0164f620a8dcb7e to your computer and use it in GitHub Desktop.
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
BEGIN {
RS="\n?~\n"
FS="\n"
}
# Special case the diff header/chunk header lines.
/^diff --git/ {
print
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["+"]
}
}
@deryni
Copy link
Author

deryni commented Sep 3, 2014

Used as:

git diff --word-diff=porcelain | awk -v red="$(tput setaf 1)" -v green="$(tput setaf 2)" -v reset="$(tput sgr0)" -f worddiff.awk

or

git diff --word-diff-regex='([^[:alnum:]]|[^[:space:]])' --word-diff=porcelain | awk -v red="$(tput setaf 1)" -v green="$(tput setaf 2)" -v reset="$(tput sgr0)" -f worddiff.awk

@deryni
Copy link
Author

deryni commented Sep 17, 2014

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.

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