Skip to content

Instantly share code, notes, and snippets.

@juliusknorr
Last active December 5, 2019 12:03
Show Gist options
  • Save juliusknorr/32d45f02033cbc3dcd5e39af5a3783f9 to your computer and use it in GitHub Desktop.
Save juliusknorr/32d45f02033cbc3dcd5e39af5a3783f9 to your computer and use it in GitHub Desktop.

To be called with:

git diff --text --word-diff=porcelain | awk -v red="$(tput setaf 1)" -v green="$(tput setaf 2)" -v reset="$(tput sgr0)" -f ~/.dotfiles/scripts/worddiff.awk
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["+"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment