contacts: gather all blame sources prior to invoking git-blame
[git.git] / contrib / contacts / git-contacts
blobb4d3526a4666bb737db56f64771fcddf3e7afca7
1 #!/usr/bin/perl
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> ...
8 use strict;
9 use warnings;
10 use IPC::Open2;
12 my $since = '5-years-ago';
13 my $min_percent = 10;
14 my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc/i;
15 my %seen;
17 sub format_contact {
18 my ($name, $email) = @_;
19 return "$name <$email>";
22 sub parse_commit {
23 my ($commit, $data) = @_;
24 my $contacts = $commit->{contacts};
25 my $inbody = 0;
26 for (split(/^/m, $data)) {
27 if (not $inbody) {
28 if (/^author ([^<>]+) <(\S+)> .+$/) {
29 $contacts->{format_contact($1, $2)} = 1;
30 } elsif (/^$/) {
31 $inbody = 1;
33 } elsif (/^$labels_rx:\s+([^<>]+)\s+<(\S+?)>$/o) {
34 $contacts->{format_contact($1, $2)} = 1;
39 sub import_commits {
40 my ($commits) = @_;
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";
45 my $line = <$reader>;
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;
49 my $data;
50 # cat-file emits newline after data, so read len+1
51 read $reader, $data, $len + 1;
52 parse_commit($commits->{$id}, $data);
55 close $reader;
56 close $writer;
57 waitpid($pid, 0);
58 die "git-cat-file error: $?\n" if $?;
61 sub get_blame {
62 my ($commits, $source, $start, $len, $from) = @_;
63 open my $f, '-|',
64 qw(git blame --porcelain -C), '-L', "$start,+$len",
65 '--since', $since, "$from^", '--', $source or die;
66 while (<$f>) {
67 if (/^([0-9a-f]{40}) \d+ \d+ \d+$/) {
68 my $id = $1;
69 $commits->{$id} = { id => $id, contacts => {} }
70 unless $seen{$id};
71 $seen{$id} = 1;
74 close $f;
77 sub blame_sources {
78 my ($sources, $commits) = @_;
79 for my $s (keys %$sources) {
80 for my $id (keys %{$sources->{$s}}) {
81 for my $range (@{$sources->{$s}{$id}}) {
82 get_blame($commits, $s,
83 $range->[0], $range->[1], $id);
89 sub scan_patches {
90 my ($sources, $id, $f) = @_;
91 my $source;
92 while (<$f>) {
93 if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
94 $id = $1;
95 $seen{$id} = 1;
97 next unless $id;
98 if (m{^--- (?:a/(.+)|/dev/null)$}) {
99 $source = $1;
100 } elsif (/^--- /) {
101 die "Cannot parse hunk source: $_\n";
102 } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
103 my $len = defined($2) ? $2 : 1;
104 push @{$sources->{$source}{$id}}, [$1, $len] if $len;
109 sub scan_patch_file {
110 my ($commits, $file) = @_;
111 open my $f, '<', $file or die "read failure: $file: $!\n";
112 scan_patches($commits, undef, $f);
113 close $f;
116 sub parse_rev_args {
117 my @args = @_;
118 open my $f, '-|',
119 qw(git rev-parse --revs-only --default HEAD --symbolic), @args
120 or die;
121 my @revs;
122 while (<$f>) {
123 chomp;
124 push @revs, $_;
126 close $f;
127 return @revs if scalar(@revs) != 1;
128 return "^$revs[0]", 'HEAD' unless $revs[0] =~ /^-/;
129 return $revs[0], 'HEAD';
132 sub scan_rev_args {
133 my ($commits, $args) = @_;
134 my @revs = parse_rev_args(@$args);
135 open my $f, '-|', qw(git rev-list --reverse), @revs or die;
136 while (<$f>) {
137 chomp;
138 my $id = $_;
139 $seen{$id} = 1;
140 open my $g, '-|', qw(git show -C --oneline), $id or die;
141 scan_patches($commits, $id, $g);
142 close $g;
144 close $f;
147 sub mailmap_contacts {
148 my ($contacts) = @_;
149 my %mapped;
150 my $pid = open2 my $reader, my $writer, qw(git check-mailmap --stdin);
151 for my $contact (keys(%$contacts)) {
152 print $writer "$contact\n";
153 my $canonical = <$reader>;
154 chomp $canonical;
155 $mapped{$canonical} += $contacts->{$contact};
157 close $reader;
158 close $writer;
159 waitpid($pid, 0);
160 die "git-check-mailmap error: $?\n" if $?;
161 return \%mapped;
164 if (!@ARGV) {
165 die "No input revisions or patch files\n";
168 my (@files, @rev_args);
169 for (@ARGV) {
170 if (-e) {
171 push @files, $_;
172 } else {
173 push @rev_args, $_;
177 my %sources;
178 for (@files) {
179 scan_patch_file(\%sources, $_);
181 if (@rev_args) {
182 scan_rev_args(\%sources, \@rev_args)
185 my %commits;
186 blame_sources(\%sources, \%commits);
187 import_commits(\%commits);
189 my $contacts = {};
190 for my $commit (values %commits) {
191 for my $contact (keys %{$commit->{contacts}}) {
192 $contacts->{$contact}++;
195 $contacts = mailmap_contacts($contacts);
197 my $ncommits = scalar(keys %commits);
198 for my $contact (keys %$contacts) {
199 my $percent = $contacts->{$contact} * 100 / $ncommits;
200 next if $percent < $min_percent;
201 print "$contact\n";