Merge branch 'js/add-i-mingw'
[gitweb.git] / contrib / contacts / git-contacts
index 3e6cce810679cbc0d4d1316babd5da42d7197f5d..fb6429b64be3cf7011ce69950987a8b328e0e758 100755 (executable)
@@ -3,7 +3,7 @@
 # List people who might be interested in a patch.  Useful as the argument to
 # git-send-email --cc-cmd option, and in other situations.
 #
-# Usage: git contacts <file> ...
+# Usage: git contacts <file | rev-list option> ...
 
 use strict;
 use warnings;
@@ -59,11 +59,11 @@ sub import_commits {
 }
 
 sub get_blame {
-       my ($commits, $source, $start, $len, $from) = @_;
-       $len = 1 unless defined($len);
-       return if $len == 0;
+       my ($commits, $source, $from, $ranges) = @_;
+       return unless @$ranges;
        open my $f, '-|',
-               qw(git blame --porcelain -C), '-L', "$start,+$len",
+               qw(git blame --porcelain -C),
+               map({"-L$_->[0],+$_->[1]"} @$ranges),
                '--since', $since, "$from^", '--', $source or die;
        while (<$f>) {
                if (/^([0-9a-f]{40}) \d+ \d+ \d+$/) {
@@ -76,9 +76,18 @@ sub get_blame {
        close $f;
 }
 
+sub blame_sources {
+       my ($sources, $commits) = @_;
+       for my $s (keys %$sources) {
+               for my $id (keys %{$sources->{$s}}) {
+                       get_blame($commits, $s, $id, $sources->{$s}{$id});
+               }
+       }
+}
+
 sub scan_patches {
-       my ($commits, $f) = @_;
-       my ($id, $source);
+       my ($sources, $id, $f) = @_;
+       my $source;
        while (<$f>) {
                if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
                        $id = $1;
@@ -90,7 +99,8 @@ sub scan_patches {
                } elsif (/^--- /) {
                        die "Cannot parse hunk source: $_\n";
                } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
-                       get_blame($commits, $source, $1, $2, $id);
+                       my $len = defined($2) ? $2 : 1;
+                       push @{$sources->{$source}{$id}}, [$1, $len] if $len;
                }
        }
 }
@@ -98,18 +108,81 @@ sub scan_patches {
 sub scan_patch_file {
        my ($commits, $file) = @_;
        open my $f, '<', $file or die "read failure: $file: $!\n";
-       scan_patches($commits, $f);
+       scan_patches($commits, undef, $f);
+       close $f;
+}
+
+sub parse_rev_args {
+       my @args = @_;
+       open my $f, '-|',
+               qw(git rev-parse --revs-only --default HEAD --symbolic), @args
+               or die;
+       my @revs;
+       while (<$f>) {
+               chomp;
+               push @revs, $_;
+       }
+       close $f;
+       return @revs if scalar(@revs) != 1;
+       return "^$revs[0]", 'HEAD' unless $revs[0] =~ /^-/;
+       return $revs[0], 'HEAD';
+}
+
+sub scan_rev_args {
+       my ($commits, $args) = @_;
+       my @revs = parse_rev_args(@$args);
+       open my $f, '-|', qw(git rev-list --reverse), @revs or die;
+       while (<$f>) {
+               chomp;
+               my $id = $_;
+               $seen{$id} = 1;
+               open my $g, '-|', qw(git show -C --oneline), $id or die;
+               scan_patches($commits, $id, $g);
+               close $g;
+       }
        close $f;
 }
 
+sub mailmap_contacts {
+       my ($contacts) = @_;
+       my %mapped;
+       my $pid = open2 my $reader, my $writer, qw(git check-mailmap --stdin);
+       for my $contact (keys(%$contacts)) {
+               print $writer "$contact\n";
+               my $canonical = <$reader>;
+               chomp $canonical;
+               $mapped{$canonical} += $contacts->{$contact};
+       }
+       close $reader;
+       close $writer;
+       waitpid($pid, 0);
+       die "git-check-mailmap error: $?\n" if $?;
+       return \%mapped;
+}
+
 if (!@ARGV) {
-       die "No input patch files\n";
+       die "No input revisions or patch files\n";
 }
 
-my %commits;
+my (@files, @rev_args);
 for (@ARGV) {
-       scan_patch_file(\%commits, $_);
+       if (-e) {
+               push @files, $_;
+       } else {
+               push @rev_args, $_;
+       }
+}
+
+my %sources;
+for (@files) {
+       scan_patch_file(\%sources, $_);
 }
+if (@rev_args) {
+       scan_rev_args(\%sources, \@rev_args)
+}
+
+my %commits;
+blame_sources(\%sources, \%commits);
 import_commits(\%commits);
 
 my $contacts = {};
@@ -118,6 +191,7 @@ for my $commit (values %commits) {
                $contacts->{$contact}++;
        }
 }
+$contacts = mailmap_contacts($contacts);
 
 my $ncommits = scalar(keys %commits);
 for my $contact (keys %$contacts) {