contrib: contacts: add ability to parse from committish
[git/gitweb.git] / contrib / contacts / git-contacts
blob1686ff340ad0dbc7eff53411107858a08f4024b9
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 $len = 1 unless defined($len);
64 return if $len == 0;
65 open my $f, '-|',
66 qw(git blame --porcelain -C), '-L', "$start,+$len",
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 scan_patches {
80 my ($commits, $id, $f) = @_;
81 my $source;
82 while (<$f>) {
83 if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
84 $id = $1;
85 $seen{$id} = 1;
87 next unless $id;
88 if (m{^--- (?:a/(.+)|/dev/null)$}) {
89 $source = $1;
90 } elsif (/^--- /) {
91 die "Cannot parse hunk source: $_\n";
92 } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
93 get_blame($commits, $source, $1, $2, $id);
98 sub scan_patch_file {
99 my ($commits, $file) = @_;
100 open my $f, '<', $file or die "read failure: $file: $!\n";
101 scan_patches($commits, undef, $f);
102 close $f;
105 sub scan_rev_args {
106 my ($commits, $args) = @_;
107 open my $f, '-|', qw(git rev-list --reverse), @$args or die;
108 while (<$f>) {
109 chomp;
110 my $id = $_;
111 $seen{$id} = 1;
112 open my $g, '-|', qw(git show -C --oneline), $id or die;
113 scan_patches($commits, $id, $g);
114 close $g;
116 close $f;
119 if (!@ARGV) {
120 die "No input revisions or patch files\n";
123 my (@files, @rev_args);
124 for (@ARGV) {
125 if (-e) {
126 push @files, $_;
127 } else {
128 push @rev_args, $_;
132 my %commits;
133 for (@files) {
134 scan_patch_file(\%commits, $_);
136 if (@rev_args) {
137 scan_rev_args(\%commits, \@rev_args)
139 import_commits(\%commits);
141 my $contacts = {};
142 for my $commit (values %commits) {
143 for my $contact (keys %{$commit->{contacts}}) {
144 $contacts->{$contact}++;
148 my $ncommits = scalar(keys %commits);
149 for my $contact (keys %$contacts) {
150 my $percent = $contacts->{$contact} * 100 / $ncommits;
151 next if $percent < $min_percent;
152 print "$contact\n";