aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2016-03-17 18:56:24 +0100
committerDuncaen <mail@duncano.de>2016-03-17 19:01:46 +0100
commitaa356d0a618937e5ee3b0953b8ec1525e7c6a0e8 (patch)
tree8a78ab7c0beaa26c1bd92e00a1a26b42102e6ae5
parent48cb37d6c05ad05c1803e81369315f65d8ae276b (diff)
downloaddotfiles-aa356d0a618937e5ee3b0953b8ec1525e7c6a0e8.tar.gz
bin: add diffed thx @chneukirchen
-rwxr-xr-xbin/diffed90
1 files changed, 90 insertions, 0 deletions
diff --git a/bin/diffed b/bin/diffed
new file mode 100755
index 0000000..2235ea3
--- /dev/null
+++ b/bin/diffed
@@ -0,0 +1,90 @@
+#!/bin/sh
+# diffed [-CHT] [-pN] [-s SUBST -r REPL] [-x EXCLUDE] - filter for unified diffs
+#
+# -C remove all non-diff lines
+# -H remove diff headers
+# -T remove file timestamps
+# -p N strip N levels of directories from file names
+# -s SUBST -r REPL replace regexp SUBST with REPL in filenames
+# -x EXCLUDE exclude diffs touching files matching regexp EXCLUDE
+#
+# To the extent possible under law, Christian Neukirchen has waived
+# all copyright and related or neighboring rights to this work.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+striptime=0
+stripheader=0
+stripprefix=0
+clean=0
+subst=
+repl=
+exclude=
+nl='
+'
+
+while getopts CHTp:s:r:x: name
+do
+ case $name in
+ C) clean=1;;
+ H) stripheader=1;;
+ T) striptime=1;;
+ p) stripprefix=$((0+OPTARG));;
+ s) subst=$OPTARG;;
+ r) repl=$OPTARG;;
+ x) exclude="$exclude$nl$OPTARG";;
+ ?) printf "Usage: %s: [-CHT] [-pN] [-s SUBST -r REPL] [-x EXCLUDE]\n" $0 1>&2
+ exit 1;;
+ esac
+done
+
+exec awk -v "striptime=$striptime" \
+ -v "stripheader=$stripheader" \
+ -v "stripprefix=$stripprefix" \
+ -v "subst=$subst" \
+ -v "repl=$repl" \
+ -v "excludes=$exclude" \
+ -v "clean=$clean" '
+BEGIN {
+ excludes = substr(excludes, 2)
+ split(excludes, exclude, "\n");
+}
+
+inpatch && /^[^ +-@]/ { inpatch = 0; strippatch = 0 }
+
+/^diff |^Index: / { if (!inheader) header = $0; inheader = 1; next }
+
+/^---|\+\+\+/ {
+ inheader = 0
+ inpatch = 1
+ s = substr($0, 1, 4)
+ f = substr($0, 5)
+ if (i = index(f, "\t")) {
+ t = substr(f, i)
+ f = substr(f, 0, i-1)
+ }
+ if (striptime)
+ t = ""
+ for (i = 0; i < stripprefix; i++)
+ sub("[^/]*\\/", "", f)
+ if (subst && repl)
+ gsub(subst, repl, f)
+ $0 = s f t
+ for (e in exclude)
+ if (match(f, exclude[e]))
+ strippatch = 1
+ if (!strippatch) {
+ if (header && !stripheader)
+ print(header)
+ print($0)
+ }
+ header = ""
+ next
+}
+
+inheader { header = header "\n" $0; next }
+inpatch && strippatch { next }
+inpatch { print; next }
+
+!clean { print }
+'
+