3 # List people who might be interested in a patch.  Useful as the argument to
 
   4 # git-send-email --cc-cmd option, and in other situations.
 
   6 # Usage: git contacts <file | rev-list option> ...
 
  12 my $since = '5-years-ago';
 
  14 my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc/i;
 
  18         my ($name, $email) = @_;
 
  19         return "$name <$email>";
 
  23         my ($commit, $data) = @_;
 
  24         my $contacts = $commit->{contacts};
 
  26         for (split(/^/m, $data)) {
 
  28                         if (/^author ([^<>]+) <(\S+)> .+$/) {
 
  29                                 $contacts->{format_contact($1, $2)} = 1;
 
  33                 } elsif (/^$labels_rx:\s+([^<>]+)\s+<(\S+?)>$/o) {
 
  34                         $contacts->{format_contact($1, $2)} = 1;
 
  41         return unless %$commits;
 
  42         my $pid = open2 my $reader, my $writer, qw(git cat-file --batch);
 
  43         for my $id (keys(%$commits)) {
 
  44                 print $writer "$id\n";
 
  46                 if ($line =~ /^([0-9a-f]{40}) commit (\d+)/) {
 
  47                         my ($cid, $len) = ($1, $2);
 
  48                         die "expected $id but got $cid\n" unless $id eq $cid;
 
  50                         # cat-file emits newline after data, so read len+1
 
  51                         read $reader, $data, $len + 1;
 
  52                         parse_commit($commits->{$id}, $data);
 
  58         die "git-cat-file error: $?\n" if $?;
 
  62         my ($commits, $source, $from, $ranges) = @_;
 
  63         return unless @$ranges;
 
  65                 qw(git blame --porcelain -C),
 
  66                 map({"-L$_->[0],+$_->[1]"} @$ranges),
 
  67                 '--since', $since, "$from^", '--', $source or die;
 
  69                 if (/^([0-9a-f]{40}) \d+ \d+ \d+$/) {
 
  71                         $commits->{$id} = { id => $id, contacts => {} }
 
  80         my ($sources, $commits) = @_;
 
  81         for my $s (keys %$sources) {
 
  82                 for my $id (keys %{$sources->{$s}}) {
 
  83                         get_blame($commits, $s, $id, $sources->{$s}{$id});
 
  89         my ($sources, $id, $f) = @_;
 
  92                 if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
 
  97                 if (m{^--- (?:a/(.+)|/dev/null)$}) {
 
  99                 } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
 
 100                         my $len = defined($2) ? $2 : 1;
 
 101                         push @{$sources->{$source}{$id}}, [$1, $len] if $len;
 
 106 sub scan_patch_file {
 
 107         my ($commits, $file) = @_;
 
 108         open my $f, '<', $file or die "read failure: $file: $!\n";
 
 109         scan_patches($commits, undef, $f);
 
 116                 qw(git rev-parse --revs-only --default HEAD --symbolic), @args
 
 124         return @revs if scalar(@revs) != 1;
 
 125         return "^$revs[0]", 'HEAD' unless $revs[0] =~ /^-/;
 
 126         return $revs[0], 'HEAD';
 
 130         my ($commits, $args) = @_;
 
 131         my @revs = parse_rev_args(@$args);
 
 132         open my $f, '-|', qw(git rev-list --reverse), @revs or die;
 
 137                 open my $g, '-|', qw(git show -C --oneline), $id or die;
 
 138                 scan_patches($commits, $id, $g);
 
 144 sub mailmap_contacts {
 
 147         my $pid = open2 my $reader, my $writer, qw(git check-mailmap --stdin);
 
 148         for my $contact (keys(%$contacts)) {
 
 149                 print $writer "$contact\n";
 
 150                 my $canonical = <$reader>;
 
 152                 $mapped{$canonical} += $contacts->{$contact};
 
 157         die "git-check-mailmap error: $?\n" if $?;
 
 162         die "No input revisions or patch files\n";
 
 165 my (@files, @rev_args);
 
 176         scan_patch_file(\%sources, $_);
 
 179         scan_rev_args(\%sources, \@rev_args)
 
 182 my $toplevel = `git rev-parse --show-toplevel`;
 
 184 chdir($toplevel) or die "chdir failure: $toplevel: $!\n";
 
 187 blame_sources(\%sources, \%commits);
 
 188 import_commits(\%commits);
 
 191 for my $commit (values %commits) {
 
 192         for my $contact (keys %{$commit->{contacts}}) {
 
 193                 $contacts->{$contact}++;
 
 196 $contacts = mailmap_contacts($contacts);
 
 198 my $ncommits = scalar(keys %commits);
 
 199 for my $contact (keys %$contacts) {
 
 200         my $percent = $contacts->{$contact} * 100 / $ncommits;
 
 201         next if $percent < $min_percent;