3 0f4372e2 2022-10-03 op Show diff statistics.
5 0f4372e2 2022-10-03 op #!/usr/bin/awk -f
7 c71d3308 2023-01-03 op AWK is great. All hail AWK!
9 36294a93 2023-01-09 op First, some utility functions. parsehdr extracts the number of lines
10 36294a93 2023-01-09 op (old or new) in the given hunk header line.
12 c71d3308 2023-01-03 op function parsehdr(s) {
13 c71d3308 2023-01-03 op s = gensub(".*,", "", 1, s)
14 c71d3308 2023-01-03 op s = gensub("^-", "", 1, s)
18 36294a93 2023-01-09 op filename extracts the name of the file from a "+++ path" or "--- path"
21 c94e7f28 2023-01-09 op function filename(s) {
22 c94e7f28 2023-01-09 op s = gensub("^... ", "", 1, s)
24 c94e7f28 2023-01-09 op These lines have an optional tab followed by extra informations (the
25 c94e7f28 2023-01-09 op date for example) that needs to be removed too.
27 c94e7f28 2023-01-09 op s = gensub("\t.*", "", 1, s)
31 c71d3308 2023-01-03 op Switches the current file to the one provided. It's a great place where
32 c71d3308 2023-01-03 op accumulate part of the summary showed at the end and to reset the
33 c71d3308 2023-01-03 op per-file counters.
35 c71d3308 2023-01-03 op function switchfile(newfile) {
36 c71d3308 2023-01-03 op if (file != "") {
37 e03fd2f8 2023-01-09 op summary = sprintf("%s%4d+ %4d-\t%s\n",
38 c71d3308 2023-01-03 op summary, add, rem, file)
43 c71d3308 2023-01-03 op file = newfile
46 36294a93 2023-01-09 op Now, the real "parser". Initialize the state to "out" since we're
47 36294a93 2023-01-09 op looking for the start of a diff.
53 36294a93 2023-01-09 op Parse the changed file.
55 c71d3308 2023-01-03 op state == "out" && /^\+\+\+ / {
56 c94e7f28 2023-01-09 op nfile = filename($0)
57 c71d3308 2023-01-03 op if (nfile == "/dev/null") {
59 36294a93 2023-01-09 op When deleting a file, the name will be "/dev/null", but it's not a great
60 36294a93 2023-01-09 op name for the stats. Let's use the "old" name instead.
62 c71d3308 2023-01-03 op nfile = delfile
65 c71d3308 2023-01-03 op switchfile(nfile)
69 36294a93 2023-01-09 op Similarly, extract the "old" file name for when it's needed.
71 c71d3308 2023-01-03 op state == "out" && /^--- / && file == "" {
72 c94e7f28 2023-01-09 op delfile = filename($0)
75 36294a93 2023-01-09 op Match the start of a hunk
77 c71d3308 2023-01-03 op state == "out" && /^@@ / {
79 c71d3308 2023-01-03 op This part is a bit complicated, but all it does is extracting the number
80 c71d3308 2023-01-03 op of "new" and "old" lines showed in the hunk. A hunk header looks like this
81 c71d3308 2023-01-03 op (except for the initial '#' character)
83 c71d3308 2023-01-03 op # @@ -55,7 +55,19 @@ ...
85 c71d3308 2023-01-03 op So first extract the text inside the pair of "@@"
87 c71d3308 2023-01-03 op s = gensub("@@ ", "", 1)
88 c71d3308 2023-01-03 op s = gensub(" @@.*", "", 1, s)
90 c71d3308 2023-01-03 op and then parse each number.
92 c71d3308 2023-01-03 op old = gensub(" .*", "", 1, s)
93 c71d3308 2023-01-03 op old = parsehdr(old)
95 c71d3308 2023-01-03 op new = gensub(".* ", "", 1, s)
96 c71d3308 2023-01-03 op new = parsehdr(new)
98 c71d3308 2023-01-03 op Don't forget to switch the state of the parser, now we're reading a
104 c71d3308 2023-01-03 op Keep count of the added and removed line. Also, decrement the "old" and
105 c71d3308 2023-01-03 op "new" lines when needed, to know when we're done with the hunk.
107 c71d3308 2023-01-03 op state == "in" && /^ / {
112 c71d3308 2023-01-03 op state == "in" && /^-/ {
118 c71d3308 2023-01-03 op state == "in" && /^\+/ {
124 c71d3308 2023-01-03 op When there are no more "new" and "old" lines to read, go back to the
125 c71d3308 2023-01-03 op "out" state, ready to read another hunk or another file.
127 c71d3308 2023-01-03 op state == "in" && old <= 0 && new <= 0 {
128 c71d3308 2023-01-03 op state = "out"
131 0f4372e2 2022-10-03 op Don't be a sink! Continue the pipeline so we can further save or apply
134 0f4372e2 2022-10-03 op // { print $0 }
136 36294a93 2023-01-09 op At the end, print the stats.
140 36294a93 2023-01-09 op It's better to flush the output here, otherwise the stats (printed to
141 36294a93 2023-01-09 op stderr and unbuffered) may be interleaved with the output (on stdout,
146 36294a93 2023-01-09 op Generate the stat summary for the last processed file
148 c71d3308 2023-01-03 op switchfile("")
150 36294a93 2023-01-09 op Print the stat to the standard error, to avoid "changing" the patch.
152 36294a93 2023-01-09 op Unfortunately, there doesn't seem to be a "built-in" way of printing to
153 36294a93 2023-01-09 op stderr other than using the pseudo-device "/dev/stderr".
155 c71d3308 2023-01-03 op printf("%s", summary) > "/dev/stderr"
156 e03fd2f8 2023-01-09 op printf("%4d+ %4d-\ttotal\n", totadd, totrem) > "/dev/stderr"
159 36294a93 2023-01-09 op Some example usages:
161 ec64d77b 2023-01-09 op * cvs -q diff | diffstat > /tmp/diff
162 ec64d77b 2023-01-09 op * got diff | diffstat | ssh foo 'cd xyz && got patch'