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