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