aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-19 19:33:39 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-19 19:33:39 +0200
commit9364d710205903ac387e3991b742ddfec842d142 (patch)
tree73577752e29dd170e4a7d5d1af813cc2df7c97d9
parent447405c38386167db4921b0e46d1cf8d2dc47c84 (diff)
downloadpatches-9364d710205903ac387e3991b742ddfec842d142.tar.gz
vipatch: Check for lines incorrectly added/removed
-rwxr-xr-xvipatch60
1 files changed, 27 insertions, 33 deletions
diff --git a/vipatch b/vipatch
index 0a8f54d..4711388 100755
--- a/vipatch
+++ b/vipatch
@@ -4,39 +4,32 @@
# 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() {
+# number of added/removed lines.
+
+ep | awk -vname=${0##*/} '
+/^@@ / { head = $0 "\n"; next }
+head && /^---/ { printpatch(); 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]+/)
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"
+ head = ""
+ exit 1
+ }
+
match(head, /^@@ -[0-9]+,[0-9]+ \+[0-9]+,/)
prefix = substr(head, RSTART, RLENGTH)
@@ -46,9 +39,10 @@ function end() {
printf "%s%d%s", prefix, new, suffix
printf "%s", body
- head = ""
- body = ""
- plus = 0
- minus = 0
+ head = ""; body = ""; plus = 0; minus = 0; same = 0
+}
+
+function abs(n) {
+ return (n > 0) ? n : -n;
}
'