l10n: fr.po 2194/1294 messages translated
[git/gitweb.git] / contrib / contacts / git-contacts
blob428cc1a9a1367e36c2739cb226a0ffdd2704ce82
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, $from, $ranges) = @_;
63 return unless @$ranges;
64 open my $f, '-|',
65 qw(git blame --porcelain -C),
66 map({"-L$_->[0],+$_->[1]"} @$ranges),
67 '--since', $since, "$from^", '--', $source or die;
68 while (<$f>) {
69 if (/^([0-9a-f]{40}) \d+ \d+ \d+$/) {
70 my $id = $1;
71 $commits->{$id} = { id => $id, contacts => {} }
72 unless $seen{$id};
73 $seen{$id} = 1;
76 close $f;
79 sub blame_sources {
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});
88 sub scan_patches {
89 my ($sources, $id, $f) = @_;
90 my $source;
91 while (<$f>) {
92 if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
93 $id = $1;
94 $seen{$id} = 1;
96 next unless $id;
97 if (m{^--- (?:a/(.+)|/dev/null)$}) {
98 $source = $1;
99 } elsif (/^--- /) {
100 die "Cannot parse hunk source: $_\n";
101 } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
102 my $len = defined($2) ? $2 : 1;
103 push @{$sources->{$source}{$id}}, [$1, $len] if $len;
108 sub scan_patch_file {
109 my ($commits, $file) = @_;
110 open my $f, '<', $file or die "read failure: $file: $!\n";
111 scan_patches($commits, undef, $f);
112 close $f;
115 sub parse_rev_args {
116 my @args = @_;
117 open my $f, '-|',
118 qw(git rev-parse --revs-only --default HEAD --symbolic), @args
119 or die;
120 my @revs;
121 while (<$f>) {
122 chomp;
123 push @revs, $_;
125 close $f;
126 return @revs if scalar(@revs) != 1;
127 return "^$revs[0]", 'HEAD' unless $revs[0] =~ /^-/;
128 return $revs[0], 'HEAD';
131 sub scan_rev_args {
132 my ($commits, $args) = @_;
133 my @revs = parse_rev_args(@$args);
134 open my $f, '-|', qw(git rev-list --reverse), @revs or die;
135 while (<$f>) {
136 chomp;
137 my $id = $_;
138 $seen{$id} = 1;
139 open my $g, '-|', qw(git show -C --oneline), $id or die;
140 scan_patches($commits, $id, $g);
141 close $g;
143 close $f;
146 sub mailmap_contacts {
147 my ($contacts) = @_;
148 my %mapped;
149 my $pid = open2 my $reader, my $writer, qw(git check-mailmap --stdin);
150 for my $contact (keys(%$contacts)) {
151 print $writer "$contact\n";
152 my $canonical = <$reader>;
153 chomp $canonical;
154 $mapped{$canonical} += $contacts->{$contact};
156 close $reader;
157 close $writer;
158 waitpid($pid, 0);
159 die "git-check-mailmap error: $?\n" if $?;
160 return \%mapped;
163 if (!@ARGV) {
164 die "No input revisions or patch files\n";
167 my (@files, @rev_args);
168 for (@ARGV) {
169 if (-e) {
170 push @files, $_;
171 } else {
172 push @rev_args, $_;
176 my %sources;
177 for (@files) {
178 scan_patch_file(\%sources, $_);
180 if (@rev_args) {
181 scan_rev_args(\%sources, \@rev_args)
184 my $toplevel = `git rev-parse --show-toplevel`;
185 chomp $toplevel;
186 chdir($toplevel) or die "chdir failure: $toplevel: $!\n";
188 my %commits;
189 blame_sources(\%sources, \%commits);
190 import_commits(\%commits);
192 my $contacts = {};
193 for my $commit (values %commits) {
194 for my $contact (keys %{$commit->{contacts}}) {
195 $contacts->{$contact}++;
198 $contacts = mailmap_contacts($contacts);
200 my $ncommits = scalar(keys %commits);
201 for my $contact (keys %$contacts) {
202 my $percent = $contacts->{$contact} * 100 / $ncommits;
203 next if $percent < $min_percent;
204 print "$contact\n";