92be7b1df1caec6aefaabd3973da1e72e4d1ffa4
[git-grep-footer.git] / git-grep-footer
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5 use open ':std', OUT => ':utf8';
6 use Encode 'decode';
7 use Data::Dump 'pp';
8 use Getopt::Long qw(:config bundling);
9
10 GetOptions(\my %opt,
11         'debug!',
12         '',  # stdin
13         'count|c!',
14         'simplify|s:s',
15         'ignore-case|i!',
16         'fuzzy!',
17         'grep|S=s',
18         'min|min-count|unique|u:i',
19         'max|max-count|show|n:i',
20         'hash|H!',
21         'version|V'  => sub { Getopt::Long::VersionMessage() },
22         'usage|h'    => sub { Getopt::Long::HelpMessage() },
23         'help|man|?' => sub { Getopt::Long::HelpMessage(-verbose => 2) },
24 ) or exit 129;
25
26 my $inputstream = $opt{''} ? \*ARGV : eval {
27         require Git;
28         Git::command_output_pipe('log', '-z', '--pretty=format:%h%n%b', @ARGV);
29 } || die "Automatic git log failed: $@";
30
31 local $| = 1;
32 local $/ = "\0";
33
34 my $HEADERMATCH = qr/ [a-z]+ (?: (?:-\w+)+ | \ by ) | cc | reference /imsx;
35
36 my (%headercount, @headercache);
37
38 while (readline $inputstream) {
39         s/^ ([0-9a-f]{4,40}) \n//msx;
40         my $hash = $opt{hash} ? $1 : undef;
41
42         # strip commit seperator
43         chomp;
44         # skip expensive checks without potential identifier
45         m/:/ or next;
46         # try to parse as UTF-8
47         eval { $_ = decode(utf8   => $_, Encode::FB_CROAK()); return 1 }
48         # if invalid, assume it's latin1
49             or $_ = decode(cp1252 => $_);
50
51         BLOCK:
52         for (reverse split /\n\n/) {
53                 my @headers;
54                 my $prefix = 0;
55
56                 LINE:
57                 for (split /\n/) {
58                         next if not m/\S/;
59                         my @header = m{
60                                 ^
61                                 (?<key> $HEADERMATCH)
62                                 : \s*
63                                 (?<val> \S [^\n]+)
64                                 $
65                         }imsx or do {
66                                 $prefix++;
67                                 next LINE;
68                         };
69
70                         push @header, $_ if defined $opt{max};
71
72                         if ($opt{fuzzy}) {
73                                 for ($header[0]) {
74                                         tr/ _/-/;
75
76                                         state $BY = qr{ (?: -? b[yu] )? \Z }imsx;
77                                         s{\A si (?:ge?n|n?g) (?:e?[dt])? -? (?:of+)? $BY}{Signed-off-by}imsx;
78                                         s{\A ack (?:ed|de)?  $BY}{Acked-by}imsx;
79                                         s{\A review (?:e?d)? $BY}{Reviewed-by}imsx;
80                                         s{\A teste[dt]       $BY}{Tested-by}imsx;
81                                 }
82                         }
83
84                         if (defined $opt{grep}) {
85                                 $_ ~~ qr/$opt{grep}/im or next LINE;
86                         }
87
88                         given ($opt{simplify} // 'none') {
89                                 when (['email', 'authors']) {
90                                         $header[1] =~ s{
91                                                 \A
92                                                 (?: [^:;]+ )?
93                                                 < [^@>]+ (?: @ | \h?\W? at \W?\h? ) [a-z0-9.-]+ >
94                                                 \Z
95                                         }{<...>}imsx;
96                                 }
97                                 when (['var', 'vars', '']) {
98                                         when ($header[0] =~ m/[ _-] (?: by | to ) $ | ^cc$/imsx) {
99                                                 $header[1] = undef;
100                                         }
101                                         for ($header[1]) {
102                                                 s{\b (https?)://\S+ }{[$1]}gmsx;  # url
103                                                 s{(?: < | \A ) [^@>\s]+ @ [^>]+ (?: > | \Z )}{<...>}igmsx;  # address
104                                                 s{\b [0-9]+ \b}{[num]}gmsx;  # number
105                                                 s{\b [Ig]? [0-9a-f]{  40} \b}{[sha1]}gmsx;  # hash
106                                                 s{\b [Ig]? [0-9a-f]{6,40} \b}{[hash]}gmsx;  # abbrev
107                                         }
108                                 }
109                                 when (['all', 'contents']) {
110                                         $header[1] = undef;
111                                 }
112                                 when (['none', 'no', '0']) {
113                                 }
114                                 default {
115                                         die "Unknown simplify option: '$_'\n";
116                                 }
117                         }
118
119                         if ($opt{'ignore-case'}) {
120                                 $_ = lc for $header[0], $header[1] // ();
121                         }
122
123                         pop @header if not defined $header[-1];
124
125                         push @headers, \@header;
126                 }
127
128                 next BLOCK if not @headers;
129
130                 if ($opt{debug} and $prefix) {
131                         say sprintf ': invalid lines in %s (%s)', $hash // 'block', $prefix;
132                 }
133
134                 for (@headers) {
135                         my $line = $_->[2] // join(': ', @$_);
136                         $line =~ s/\A/$hash /msx if defined $hash;
137
138                         if (defined $opt{min} or $opt{max} or $opt{count}) {
139                                 my $counter = \$headercount{ $_->[0] }->{ $_->[1] // '' };
140                                 my $excess = ${$counter}++ - ($opt{min} // 0);
141                                 next if $excess >= ($opt{max} || 1);
142                                 next if $excess <  0;
143                                 if ($opt{count}) {
144                                         push @headercache, [ $line, $excess ? \undef : $counter ];
145                                         next;
146                                 }
147                         }
148                         say $line;
149                 }
150
151                 last BLOCK;
152         }
153 }
154
155 for (@headercache) {
156         say ${$_->[1]} // '', "\t", $_->[0];
157 }
158
159 __END__
160
161 =head1 NAME
162
163 git-grep-footer - Find custom header lines in commit messages
164
165 =head1 SYNOPSIS
166
167 F<git-grep-footer> [OPTIONS] [-- <git log options>]
168
169 F<git> log -z --pretty=format:%b | F<git-grep-footer> [OPTIONS] -
170
171 =head1 DESCRIPTION
172
173 Filters out header sections near the end of a commit body,
174 a common convention to list custom metadata such as
175 C<Signed-off-by> and C<Acked-by>.
176
177 Sections are identified by at least one leading keyword containing a dash
178 (or exceptionally recognised)
179 followed by a colon.
180
181 =head1 OPTIONS
182
183 =over
184
185 =item -i, --ignore-case
186
187 Lowercases everything.
188
189 =item -s, --simplify[=<rule>]
190
191 Modifies values to hide specific details.
192 Several different rules are supported:
193
194 =over
195
196 =item I<var> (default)
197
198 Replaces highly variable contents such as numbers, hashes, and addresses,
199 leaving only exceptional annotations as distinct text.
200 Attributes ending in I<-to> or I<-by> are assumed variable author names
201 and omitted entirely,
202 unless they contain a colon indicating possible attribute exceptions.
203
204 =item I<email>
205
206 Filters out author lines following the git signoff convention,
207 i.e. an <email address> optionally preceded by a name.
208
209 =item I<all>
210
211 Values will be hidden entirely, so only attribute names remain.
212
213 =back
214
215 =item --grep=<pattern>
216
217 Only include lines matching the specified regular expression.
218 Case insensitivity can be disabled by prepending C<(?-i)>.
219
220 =item -u, --unique[=<threshold>]
221
222 Each match is only shown once,
223 optionally after it has already occurred a given amount of times.
224
225 =item -n, --show[=<limit>]
226
227 The original line is given for each match,
228 but simplifications still apply for duplicate determination.
229 Additional samples are optionally given upto the given maximum.
230
231 =item -c, --count
232
233 Prefixes (unique) lines by the number of occurrences.
234 Causes output to be buffered until all input has been read (obviously).
235
236 =item -H, --hash
237
238 Prefixes the SHA1 hash of the (or a) matching commit.
239
240 =back
241
242 =head1 EXAMPLES
243
244 =over
245
246 =item git-grep-footer --grep=^ack v2.6.32..v2.6.33
247
248 Search for I<Acked-by> lines for version I<v2.6.33>.
249 Append C<-uin> to skip reoccurrences.
250
251 =item git-grep-footer -u --grep=junio
252
253 Show distinct lines mentioning a specific author.
254
255 =item git-grep-footer -c --simplify --grep=^si
256
257 Compare various capitalisations and (mis)spellings of signoffs.
258
259 =item git-grep-footer -c --simplify=all -i | sort -n -r | head -n10
260
261 List the ten most frequently used attribute names.
262
263 =item git-grep-footer -n2 -i -s --hash -- --reverse
264
265 The earliest two usages of each distinct identifier.
266
267 =back
268
269 =head1 AUTHOR
270
271 Mischa POSLAWSKY <perl@shiar.org>
272
273 =head1 LICENSE
274
275 This software is free software;
276 you can redistribute and/or modify it under the terms of the GNU GPL
277 version 2 or later.
278