aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-18 21:11:04 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-18 21:11:04 +0200
commit447405c38386167db4921b0e46d1cf8d2dc47c84 (patch)
tree299f190c7ce593c67babedbd79351b09becaf724
parent2deabc98f6ca8fd36165bbcb800affea8d990b63 (diff)
downloadpatches-447405c38386167db4921b0e46d1cf8d2dc47c84.tar.gz
Add vipatch script
-rwxr-xr-xvipatch54
1 files changed, 54 insertions, 0 deletions
diff --git a/vipatch b/vipatch
new file mode 100755
index 0000000..0a8f54d
--- /dev/null
+++ b/vipatch
@@ -0,0 +1,54 @@
+#!/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. As the script performs no
+# error checks, it is up to the user to edit (and verify)
+# the patch correctly.
+
+ep | awk '
+/^@@ / {
+ head = $0 "\n"
+ next
+}
+
+head && /^---/ {
+ end()
+ print
+ next
+}
+
+head && /^\+/ { plus++ }
+head && /^-/ { minus++ }
+
+head {
+ body = body $0 "\n"
+ next
+}
+
+{ print }
+
+END { end() }
+
+function end() {
+ match(head, /,[0-9]+/)
+ old = substr(head, RSTART+1, RLENGTH-1)
+ new = old + plus - minus
+
+ match(head, /^@@ -[0-9]+,[0-9]+ \+[0-9]+,/)
+ prefix = substr(head, RSTART, RLENGTH)
+
+ match(head, / @@.*/)
+ suffix = substr(head, RSTART, RLENGTH)
+
+ printf "%s%d%s", prefix, new, suffix
+ printf "%s", body
+
+ head = ""
+ body = ""
+ plus = 0
+ minus = 0
+}
+'