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