Blame


1 0f4372e2 2022-10-03 op # diffstat
2 0f4372e2 2022-10-03 op
3 0f4372e2 2022-10-03 op Show diff statistics.
4 0f4372e2 2022-10-03 op
5 0f4372e2 2022-10-03 op #!/usr/bin/awk -f
6 0f4372e2 2022-10-03 op
7 c71d3308 2023-01-03 op AWK is great. All hail AWK!
8 0f4372e2 2022-10-03 op
9 c71d3308 2023-01-03 op Now, some utility functions. parsehdr parse extracts the number of
10 c71d3308 2023-01-03 op lines (old or new) in the following hunk.
11 0f4372e2 2022-10-03 op
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)
15 c71d3308 2023-01-03 op return s + 0
16 c71d3308 2023-01-03 op }
17 c71d3308 2023-01-03 op
18 c94e7f28 2023-01-09 op Extracts the name of the file from a "+++ path" or "--- path" line.
19 c94e7f28 2023-01-09 op
20 c94e7f28 2023-01-09 op function filename(s) {
21 c94e7f28 2023-01-09 op s = gensub("^... ", "", 1, s)
22 c94e7f28 2023-01-09 op
23 c94e7f28 2023-01-09 op These lines have an optional tab followed by extra informations (the
24 c94e7f28 2023-01-09 op date for example) that needs to be removed too.
25 c94e7f28 2023-01-09 op
26 c94e7f28 2023-01-09 op s = gensub("\t.*", "", 1, s)
27 c94e7f28 2023-01-09 op return s
28 c94e7f28 2023-01-09 op }
29 c94e7f28 2023-01-09 op
30 c71d3308 2023-01-03 op Switches the current file to the one provided. It's a great place where
31 c71d3308 2023-01-03 op accumulate part of the summary showed at the end and to reset the
32 c71d3308 2023-01-03 op per-file counters.
33 c71d3308 2023-01-03 op
34 c71d3308 2023-01-03 op function switchfile(newfile) {
35 c71d3308 2023-01-03 op if (file != "") {
36 e03fd2f8 2023-01-09 op summary = sprintf("%s%4d+ %4d-\t%s\n",
37 c71d3308 2023-01-03 op summary, add, rem, file)
38 c71d3308 2023-01-03 op }
39 c71d3308 2023-01-03 op
40 c71d3308 2023-01-03 op add = 0
41 c71d3308 2023-01-03 op rem = 0
42 c71d3308 2023-01-03 op file = newfile
43 c71d3308 2023-01-03 op }
44 c71d3308 2023-01-03 op
45 c71d3308 2023-01-03 op Now, the real "parser". It start in the "out" state
46 c71d3308 2023-01-03 op
47 c71d3308 2023-01-03 op BEGIN {
48 c71d3308 2023-01-03 op state = "out"
49 c71d3308 2023-01-03 op }
50 c71d3308 2023-01-03 op
51 c71d3308 2023-01-03 op Match the start of a diff on the "+++" line.
52 c71d3308 2023-01-03 op
53 c71d3308 2023-01-03 op state == "out" && /^\+\+\+ / {
54 c94e7f28 2023-01-09 op nfile = filename($0)
55 c71d3308 2023-01-03 op if (nfile == "/dev/null") {
56 c71d3308 2023-01-03 op
57 c71d3308 2023-01-03 op When deleting a file, the name will be "/dev/null", but that's not a
58 c71d3308 2023-01-03 op great name for the stats. Let's use the "old" name instead.
59 c71d3308 2023-01-03 op
60 c71d3308 2023-01-03 op nfile = delfile
61 c71d3308 2023-01-03 op }
62 c71d3308 2023-01-03 op
63 c71d3308 2023-01-03 op switchfile(nfile)
64 c71d3308 2023-01-03 op delfile = ""
65 c71d3308 2023-01-03 op }
66 c71d3308 2023-01-03 op
67 c71d3308 2023-01-03 op Let's save the old name in case it's needed.
68 c71d3308 2023-01-03 op
69 c71d3308 2023-01-03 op state == "out" && /^--- / && file == "" {
70 c94e7f28 2023-01-09 op delfile = filename($0)
71 c71d3308 2023-01-03 op }
72 c71d3308 2023-01-03 op
73 c71d3308 2023-01-03 op Match the start of a hunk and switch the state to "in"
74 c71d3308 2023-01-03 op
75 c71d3308 2023-01-03 op state == "out" && /^@@ / {
76 c71d3308 2023-01-03 op
77 c71d3308 2023-01-03 op This part is a bit complicated, but all it does is extracting the number
78 c71d3308 2023-01-03 op of "new" and "old" lines showed in the hunk. A hunk header looks like this
79 c71d3308 2023-01-03 op (except for the initial '#' character)
80 c71d3308 2023-01-03 op
81 c71d3308 2023-01-03 op # @@ -55,7 +55,19 @@ ...
82 c71d3308 2023-01-03 op
83 c71d3308 2023-01-03 op So first extract the text inside the pair of "@@"
84 c71d3308 2023-01-03 op
85 c71d3308 2023-01-03 op s = gensub("@@ ", "", 1)
86 c71d3308 2023-01-03 op s = gensub(" @@.*", "", 1, s)
87 c71d3308 2023-01-03 op
88 c71d3308 2023-01-03 op and then parse each number.
89 c71d3308 2023-01-03 op
90 c71d3308 2023-01-03 op old = gensub(" .*", "", 1, s)
91 c71d3308 2023-01-03 op old = parsehdr(old)
92 c71d3308 2023-01-03 op
93 c71d3308 2023-01-03 op new = gensub(".* ", "", 1, s)
94 c71d3308 2023-01-03 op new = parsehdr(new)
95 c71d3308 2023-01-03 op
96 c71d3308 2023-01-03 op Don't forget to switch the state of the parser, now we're reading a
97 c71d3308 2023-01-03 op hunk.
98 c71d3308 2023-01-03 op
99 c71d3308 2023-01-03 op state = "in"
100 c71d3308 2023-01-03 op }
101 c71d3308 2023-01-03 op
102 c71d3308 2023-01-03 op Keep count of the added and removed line. Also, decrement the "old" and
103 c71d3308 2023-01-03 op "new" lines when needed, to know when we're done with the hunk.
104 c71d3308 2023-01-03 op
105 c71d3308 2023-01-03 op state == "in" && /^ / {
106 c71d3308 2023-01-03 op old--
107 c71d3308 2023-01-03 op new--
108 c71d3308 2023-01-03 op }
109 c71d3308 2023-01-03 op
110 c71d3308 2023-01-03 op state == "in" && /^-/ {
111 c71d3308 2023-01-03 op old--
112 c71d3308 2023-01-03 op rem++
113 c71d3308 2023-01-03 op totrem++
114 c71d3308 2023-01-03 op }
115 c71d3308 2023-01-03 op
116 c71d3308 2023-01-03 op state == "in" && /^\+/ {
117 c71d3308 2023-01-03 op new--
118 c71d3308 2023-01-03 op add++
119 c71d3308 2023-01-03 op totadd++
120 c71d3308 2023-01-03 op }
121 c71d3308 2023-01-03 op
122 c71d3308 2023-01-03 op When there are no more "new" and "old" lines to read, go back to the
123 c71d3308 2023-01-03 op "out" state, ready to read another hunk or another file.
124 c71d3308 2023-01-03 op
125 c71d3308 2023-01-03 op state == "in" && old <= 0 && new <= 0 {
126 c71d3308 2023-01-03 op state = "out"
127 c71d3308 2023-01-03 op }
128 c71d3308 2023-01-03 op
129 0f4372e2 2022-10-03 op Don't be a sink! Continue the pipeline so we can further save or apply
130 0f4372e2 2022-10-03 op the diff.
131 0f4372e2 2022-10-03 op
132 0f4372e2 2022-10-03 op // { print $0 }
133 0f4372e2 2022-10-03 op
134 0f4372e2 2022-10-03 op At the end, print the stats to standard error to avoid mangling the
135 0f4372e2 2022-10-03 op input. Unfortunately, there doesn't seem to be a "built-in" way of
136 0f4372e2 2022-10-03 op printing to stderr other than using the pseudo-device.
137 0f4372e2 2022-10-03 op
138 0f4372e2 2022-10-03 op END {
139 c71d3308 2023-01-03 op fflush()
140 c71d3308 2023-01-03 op switchfile("")
141 c71d3308 2023-01-03 op
142 c71d3308 2023-01-03 op printf("%s", summary) > "/dev/stderr"
143 e03fd2f8 2023-01-09 op printf("%4d+ %4d-\ttotal\n", totadd, totrem) > "/dev/stderr"
144 0f4372e2 2022-10-03 op }
145 0f4372e2 2022-10-03 op
146 0f4372e2 2022-10-03 op some example usages:
147 0f4372e2 2022-10-03 op
148 ec64d77b 2023-01-09 op * cvs -q diff | diffstat > /tmp/diff
149 ec64d77b 2023-01-09 op * got diff | diffstat | ssh foo 'cd xyz && got patch'