fuzzy option to replace typos
[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         'count|c!',
13         'simplify|s:s',
14         'ignore-case|i!',
15         'fuzzy!',
16         'min|min-count|unique|u:i',
17         'max|max-count|show|n:i',
18         'version|V'  => sub { Getopt::Long::VersionMessage() },
19         'usage|h'    => sub { Getopt::Long::HelpMessage() },
20         'help|man|?' => sub { Getopt::Long::HelpMessage(-verbose => 2) },
21 ) or exit 129;
22
23 local $| = 1;
24 local $/ = "\0";
25
26 my $HEADERMATCH = qr/ [a-z]+ (?: (?:-\w+)+ | \ by ) /ix;
27
28 my (%headercount, @headercache);
29
30 while (readline) {
31         s/(.+)\n//m;
32         my $hash = $1;
33
34         # strip commit seperator
35         chomp;
36         # skip expensive checks without potential identifier
37         m/:/ or next;
38         # try to parse as UTF-8
39         eval { $_ = decode(utf8   => $_, Encode::FB_CROAK()) };
40         # if invalid, assume it's latin1
41                $_ = decode(cp1252 => $_) if $@;
42
43         my $prefix = 0;
44         my %attr;
45
46         BLOCK:
47         for (reverse split /\n\n/) {
48                 my @headers;
49
50                 LINE:
51                 for (split /\n/) {
52                         next if not /\S/;
53                         my @header = m{
54                                 ^
55                                 (?<key> $HEADERMATCH)
56                                 : \s*
57                                 (?<val> \S .+)
58                                 $
59                         }imx or do {
60                                 $prefix++;
61                                 next LINE;
62                         };
63
64                         push @header, $_ if defined $opt{max};
65
66                         if ($opt{fuzzy}) {
67                                 for ($header[0]) {
68                                         tr/ _/-/;
69
70                                         state $BY = qr{ (?: -? b[yu] )? \Z }ix;
71                                         s{^ si (?:ge?n|n?g) (?:e?[dt])? -? (?:of+)? $BY}{Signed-off-by}ix;
72                                         s{^ ack (?:ed|de)?  $BY}{Acked-by}ix;
73                                         s{^ review (?:e?d)? $BY}{Reviewed-by}ix;
74                                         s{^ teste[dt]       $BY}{Tested-by}ix;
75                                 }
76                         }
77
78                         given ($opt{simplify} // 'none') {
79                                 when (['email', 'authors']) {
80                                         $header[1] =~ s{
81                                                 \A
82                                                 (?: [^:;]+ )?
83                                                 < [^@>]+ (?: @ | \h?\W? at \W?\h? ) [a-z0-9.-]+ >
84                                                 \Z
85                                         }{<...>}imsx;
86                                 }
87                                 when (['var', 'vars', '']) {
88                                         when ($header[0] =~ /[ _-] (?: by | to ) $/imsx) {
89                                                 $header[1] = undef;
90                                         }
91                                         for ($header[1]) {
92                                                 s{\b (https?)://\S+ }{[$1]}gmsx;  # url
93                                                 s{(?: < | \A ) [^@>\s]+ @ [^>]+ (?: > | \Z )}{<...>}igmsx;  # address
94                                                 s{\b [0-9]+ \b}{[num]}gmsx;  # number
95                                                 s{\b [Ig]? [0-9a-f]{  40} \b}{[sha1]}gmsx;  # hash
96                                                 s{\b [Ig]? [0-9a-f]{6,40} \b}{[hash]}gmsx;  # abbrev
97                                         }
98                                 }
99                                 when (['all', 'contents']) {
100                                         $header[1] = undef;
101                                 }
102                                 when (['none', 'no', '0']) {
103                                 }
104                                 default {
105                                         die "Unknown simplify option: '$_'\n";
106                                 }
107                         }
108
109                         if ($opt{'ignore-case'}) {
110                                 $_ = lc for $header[0], $header[1] // ();
111                         }
112
113                         pop @header if not defined $header[-1];
114
115                         push @headers, \@header;
116                 }
117
118                 next BLOCK if not @headers;
119
120                 if ($opt{debug} and $prefix) {
121                         say "infix junk in commit $hash";
122                 }
123
124                 for (@headers) {
125                         my $line = $_->[2] // join(': ', @$_);
126                         if (defined $opt{min} or $opt{max}) {
127                                 my $counter = \$headercount{ $_->[0] }->{ $_->[1] // '' };
128                                 my $excess = $$counter++ - ($opt{min} // 0);
129                                 next if $excess >= ($opt{max} || 1);
130                                 next if $excess <  0;
131                                 if ($opt{count}) {
132                                         push @headercache, [ $line, $excess ? \undef : $counter ];
133                                         next;
134                                 }
135                         }
136                         say $line;
137                 }
138
139                 last BLOCK;
140         }
141 }
142
143 for (@headercache) {
144         say ${$_->[1]} // '', "\t", $_->[0];
145 }
146
147 __END__
148
149 =head1 NAME
150
151 git-grep-footer - Find custom header lines in commit messages
152
153 =head1 SYNOPSIS
154
155 F<git> log --pretty=%b%x00 | F<git-grep-footer> [OPTIONS]
156
157 =head1 DESCRIPTION
158
159 Filters out header sections near the end of a commit body,
160 a common convention to list custom metadata such as
161 C<Signed-off-by> and C<Acked-by>.
162
163 Sections are identified by at least one leading keyword containing a dash
164 followed by a colon.
165
166 =head1 OPTIONS
167
168 =over
169
170 =item -i, --ignore-case
171
172 Lowercases everything.
173
174 =item -s, --simplify[=<rule>]
175
176 Modifies values to hide specific details.
177 Several different rules are supported:
178
179 =over
180
181 =item I<var> (default)
182
183 Replaces highly variable contents such as numbers, hashes, and addresses,
184 leaving only exceptional annotations as distinct text.
185 Attributes ending in I<-to> or I<-by> are assumed variable author names
186 and omitted entirely,
187 unless they contain a colon indicating possible attribute exceptions.
188
189 =item I<email>
190
191 Filters out author lines following the git signoff convention,
192 i.e. an <email address> optionally preceded by a name.
193
194 =item I<all>
195
196 Values will be hidden entirely, so only attribute names remain.
197
198 =back
199
200 =item -u, --unique[=<threshold>]
201
202 Each match is only shown once,
203 optionally after it has already occurred a given amount of times.
204
205 =item -n, --show[=<limit>]
206
207 The original line is given for each match,
208 but simplifications still apply for duplicate determination.
209 Additional samples are optionally given upto the given maximum.
210
211 =back
212
213 =head1 AUTHOR
214
215 Mischa POSLAWSKY <perl@shiar.org>
216
217 =head1 LICENSE
218
219 Copyright. All rights reserved.
220