What's cooking (2021/06 #06)
[git] / add-by
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Getopt::Long;
6
7 sub parsing () { 1; }
8 sub waiting () { 2; }
9 my $state = parsing;
10
11 my @more;
12 my $append;
13 my $debug;
14
15 sub find_author {
16         my $map = shift;
17         for my $name (@_) {
18                 my $fh;
19                 print STDERR "Checking <$name>..."
20                     if ($debug);
21                 if (!open($fh, "-|",
22                           qw(git log -1 --no-merges),
23                           '--format=%an <%ae>',
24                           "--author=$name",
25                           "--all")) {
26                         print STDERR "opening pipe to read from git log failed\n"
27                             if ($debug);
28                         $map->{$name} = $name;
29                         next;
30                 }
31                 my $line = <$fh>;
32                 if ($line) {
33                         chomp $line;
34                         print STDERR "read <$line> from git log\n"
35                             if ($debug);
36                         $map->{$name} = $line;
37                 } else {
38                         print STDERR "read false ($line) from git log\n"
39                             if ($debug);
40                         $map->{$name} = $name;
41                 }
42         }
43 }
44
45 sub accumulate {
46         push @more, [@_];
47 }
48
49 sub add_more_bylines {
50         if (!defined $append) {
51                 my %names = map { $_->[1] => 1 } @more;
52                 my %map = ();
53                 my @append;
54                 find_author(\%map, keys (%names));
55                 for (@more) {
56                         my ($tag, $name) = @$_;
57                         $tag = ucfirst($tag);
58                         push @append, "$tag: $map{$name}";
59                 }
60                 if (@append) {
61                         $append = join("\n", @append) . "\n";
62                 } else {
63                         $append = "";
64                 }
65         }
66         print $append;
67 }
68
69 my $check_only;
70
71 exit 1 unless (GetOptions("signed-off-by=s" => \&accumulate,
72                           "acked-by=s" => \&accumulate,
73                           "reviewed-by=s" => \&accumulate,
74                           "tested-by=s" => \&accumulate,
75                           "helped-by=s" => \&accumulate,
76                           "check-only!" => \$check_only,
77                           "debug!" => \$debug,
78                ));
79
80 if ($check_only) {
81         add_more_bylines();
82         exit 0;
83 }
84
85 while (<>) {
86         if ($state == parsing) {
87                 if (/^[-A-Za-z]+-by: /i || /^Cc: /i) {
88                         $state = waiting;
89                 }
90         } elsif ($state == waiting) {
91                 if (/^[-A-Za-z]+-by: /i || /^Cc: /i) {
92                         $state = waiting;
93                 } else {
94                         add_more_bylines();
95                         $state = parsing;
96                 }
97         }
98         print;
99 }