contrib / contacts / git-contactson commit contacts: validate hunk length earlier (9ae9ca1)
   1#!/usr/bin/perl
   2
   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.
   5#
   6# Usage: git contacts <file | rev-list option> ...
   7
   8use strict;
   9use warnings;
  10use IPC::Open2;
  11
  12my $since = '5-years-ago';
  13my $min_percent = 10;
  14my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc/i;
  15my %seen;
  16
  17sub format_contact {
  18        my ($name, $email) = @_;
  19        return "$name <$email>";
  20}
  21
  22sub 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;
  32                        }
  33                } elsif (/^$labels_rx:\s+([^<>]+)\s+<(\S+?)>$/o) {
  34                        $contacts->{format_contact($1, $2)} = 1;
  35                }
  36        }
  37}
  38
  39sub 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);
  53                }
  54        }
  55        close $reader;
  56        close $writer;
  57        waitpid($pid, 0);
  58        die "git-cat-file error: $?\n" if $?;
  59}
  60
  61sub 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;
  72                }
  73        }
  74        close $f;
  75}
  76
  77sub scan_patches {
  78        my ($commits, $id, $f) = @_;
  79        my $source;
  80        while (<$f>) {
  81                if (/^From ([0-9a-f]{40}) Mon Sep 17 00:00:00 2001$/) {
  82                        $id = $1;
  83                        $seen{$id} = 1;
  84                }
  85                next unless $id;
  86                if (m{^--- (?:a/(.+)|/dev/null)$}) {
  87                        $source = $1;
  88                } elsif (/^--- /) {
  89                        die "Cannot parse hunk source: $_\n";
  90                } elsif (/^@@ -(\d+)(?:,(\d+))?/ && $source) {
  91                        my $len = defined($2) ? $2 : 1;
  92                        get_blame($commits, $source, $1, $len, $id) if $len;
  93                }
  94        }
  95}
  96
  97sub scan_patch_file {
  98        my ($commits, $file) = @_;
  99        open my $f, '<', $file or die "read failure: $file: $!\n";
 100        scan_patches($commits, undef, $f);
 101        close $f;
 102}
 103
 104sub parse_rev_args {
 105        my @args = @_;
 106        open my $f, '-|',
 107                qw(git rev-parse --revs-only --default HEAD --symbolic), @args
 108                or die;
 109        my @revs;
 110        while (<$f>) {
 111                chomp;
 112                push @revs, $_;
 113        }
 114        close $f;
 115        return @revs if scalar(@revs) != 1;
 116        return "^$revs[0]", 'HEAD' unless $revs[0] =~ /^-/;
 117        return $revs[0], 'HEAD';
 118}
 119
 120sub scan_rev_args {
 121        my ($commits, $args) = @_;
 122        my @revs = parse_rev_args(@$args);
 123        open my $f, '-|', qw(git rev-list --reverse), @revs or die;
 124        while (<$f>) {
 125                chomp;
 126                my $id = $_;
 127                $seen{$id} = 1;
 128                open my $g, '-|', qw(git show -C --oneline), $id or die;
 129                scan_patches($commits, $id, $g);
 130                close $g;
 131        }
 132        close $f;
 133}
 134
 135sub mailmap_contacts {
 136        my ($contacts) = @_;
 137        my %mapped;
 138        my $pid = open2 my $reader, my $writer, qw(git check-mailmap --stdin);
 139        for my $contact (keys(%$contacts)) {
 140                print $writer "$contact\n";
 141                my $canonical = <$reader>;
 142                chomp $canonical;
 143                $mapped{$canonical} += $contacts->{$contact};
 144        }
 145        close $reader;
 146        close $writer;
 147        waitpid($pid, 0);
 148        die "git-check-mailmap error: $?\n" if $?;
 149        return \%mapped;
 150}
 151
 152if (!@ARGV) {
 153        die "No input revisions or patch files\n";
 154}
 155
 156my (@files, @rev_args);
 157for (@ARGV) {
 158        if (-e) {
 159                push @files, $_;
 160        } else {
 161                push @rev_args, $_;
 162        }
 163}
 164
 165my %commits;
 166for (@files) {
 167        scan_patch_file(\%commits, $_);
 168}
 169if (@rev_args) {
 170        scan_rev_args(\%commits, \@rev_args)
 171}
 172import_commits(\%commits);
 173
 174my $contacts = {};
 175for my $commit (values %commits) {
 176        for my $contact (keys %{$commit->{contacts}}) {
 177                $contacts->{$contact}++;
 178        }
 179}
 180$contacts = mailmap_contacts($contacts);
 181
 182my $ncommits = scalar(keys %commits);
 183for my $contact (keys %$contacts) {
 184        my $percent = $contacts->{$contact} * 100 / $ncommits;
 185        next if $percent < $min_percent;
 186        print "$contact\n";
 187}