X-Git-Url: http://git.shiar.net/git-grep-footer.git/blobdiff_plain/784eaf272112bcfcd72257efb7bb796d34b4f922..511a2ca1ce6002f6f133d3b681993bba390d77df:/git-grep-footer diff --git a/git-grep-footer b/git-grep-footer index 1e60d4e..906ae92 100755 --- a/git-grep-footer +++ b/git-grep-footer @@ -1,12 +1,41 @@ -#!/usr/bin/perl -0 -CS +#!/usr/bin/perl use 5.010; use strict; use warnings; +use open ':std', OUT => ':utf8'; +use Encode 'decode'; use Data::Dump 'pp'; +use Getopt::Long; + +GetOptions(\my %opt, + 'debug!', + 'simplify|s:s', + 'ignore-case|i!', + 'min|min-count|unique|u:i', + 'max|max-count|show|n:i', +) or die; + +local $| = 1; +local $/ = "\0"; my $HEADERMATCH = qr/ [a-z]+ (?: (?:-\w+)+ | \ by ) /ix; while (readline) { + s/(.+)\n//m; + my $hash = $1; + + # strip commit seperator + chomp; + # skip expensive checks without potential identifier + m/:/ or next; + # try to parse as UTF-8 + eval { $_ = decode(utf8 => $_, Encode::FB_CROAK()) }; + # if invalid, assume it's latin1 + $_ = decode(cp1252 => $_) if $@; + + my $prefix = 0; + my %attr; + BLOCK: for (reverse split /\n\n/) { my @headers; @@ -20,15 +49,66 @@ while (readline) { : \s* (? \S .+) $ - }imx or next LINE; + }imx or do { + $prefix++; + next LINE; + }; + + push @header, $_ if defined $opt{max}; + + given ($opt{simplify} // 'no') { + when ('strict') { + $header[1] =~ s{ + \A + (?: [^:]+ )? + < [^@>]+ (?: @ | \h?\W? at \W?\h? ) [a-z0-9.-]+ > + \Z + }{<...>}imsx; + } + when (['text', '']) { + when ($header[0] =~ /[ _-] (?: by | to ) $/imsx) { + $header[1] = undef; + } + for ($header[1]) { + s{\b (https?)://\S+ }{[$1]}gmsx; # url + s{(?: < | \A ) [^@>\s]+ @ [^>]+ (?: > | \Z )}{<...>}igmsx; # address + s{\b [0-9]+ \b}{[num]}gmsx; # number + s{\b I? [0-9a-f]{40} \b}{[sha1]}gmsx; # hash + } + } + when (['all', 'any']) { + $header[1] = undef; + } + when ('no') { + } + default { + die "Unknown simplify option: '$_'\n"; + } + } + + if ($opt{'ignore-case'}) { + $_ = lc for $header[0], $header[1] // (); + } + + pop @header if not defined $header[-1]; push @headers, \@header; } next BLOCK if not @headers; + if ($opt{debug} and $prefix) { + say "infix junk in commit $hash"; + } + for (@headers) { - say join ': ', @$_; + if (defined $opt{min} or $opt{max}) { + state $seen; + my $count = $seen->{ $_->[0] }->{ $_->[1] // '' }++; + next if $count >= ($opt{min} // 0) + ($opt{max} || 1); + next if $count < ($opt{min} // 0); + } + say $_->[2] // join(': ', @$_); } last BLOCK;