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