aboutsummaryrefslogtreecommitdiff
path: root/bin/vipatch
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-26 19:32:37 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-26 19:32:37 +0200
commit09c0720ee7f4d949f3f779bb26ccb60faad56cd5 (patch)
tree30fc911ffc70a78454002e969f31e9b95050b695 /bin/vipatch
parent4ad8ed80a6e8969fd8a2af4ef7e29929126aeaad (diff)
downloadpatches-09c0720ee7f4d949f3f779bb26ccb60faad56cd5.tar.gz
Move patch utilities to bin/
Diffstat (limited to 'bin/vipatch')
-rwxr-xr-xbin/vipatch50
1 files changed, 50 insertions, 0 deletions
diff --git a/bin/vipatch b/bin/vipatch
new file mode 100755
index 0000000..d860f93
--- /dev/null
+++ b/bin/vipatch
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+# vipatch -- edit unified diff
+
+# This is a simple awk script that re-adjusts headers in
+# a patch generated by diff -u, depending on the actual
+# number of added/removed lines.
+
+ep | awk -vname=${0##*/} '
+/^@@ / { if (head) printpatch(); head = $0 "\n"; next }
+head && /^---/ { print; next }
+head && /^\+\+\+/ { print; next }
+head && /^\+/ { plus++ }
+head && /^-/ { minus++ }
+head && /^ / { same++ }
+head { body = body $0 "\n"; next }
+ { print }
+END { if (head) printpatch() }
+
+function printpatch() {
+ match(head, /^@@ -[0-9]+,[0-9]+ \+[0-9]+,/)
+ prefix = substr(head, RSTART, RLENGTH)
+
+ match(head, / @@.*/)
+ suffix = substr(head, RSTART, RLENGTH)
+
+ match(head, /,[0-9]+/)
+ old = substr(head, RSTART+1, RLENGTH-1)
+ new = old + plus - minus
+
+ diff = old - same - minus
+ if (diff != 0) {
+ printf "%s: %d %s incorrectly %s\n", name, abs(diff),
+ (abs(diff) > 1) ? "lines" : "line",
+ (diff > 0) ? "removed" : "added" > "/dev/stderr"
+ printf "under header%s\n", substr(suffix, 4) > "/dev/stderr"
+ head = ""
+ exit 1
+ }
+
+ printf "%s%d%s", prefix, new, suffix
+ printf "%s", body
+
+ head = ""; body = ""; plus = 0; minus = 0; same = 0
+}
+
+function abs(n) {
+ return (n > 0) ? n : -n;
+}
+'