summaryrefslogtreecommitdiff
path: root/git-authors-gen.pl
blob: aae7a65fd833687af897331ba2e2d99fe1c8af5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env perl
# -*- perl -*-

#
# git-authors-gen
#
# This program is based on gitlog-to-changelog by Jim Meyering
#

use strict;
use warnings;
use Getopt::Long;

(my $ME = $0) =~ s|.*/||;

my $BUGREPORT = 'autoconf-archive-maintainers@nongnu.org';

sub usage ($) {
    my ($exit_code) = @_;
    my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
    if ($exit_code != 0) {
	print $STREAM "Try `$ME --help' for more information.\n";
    } else {
	print $STREAM <<EOF;
Usage: $ME [OPTIONS]

Output git committers.

OPTIONS:

      --since=DATE    convert only the logs since DATE;
                      the default is to convert all log entries.
  -h, --help          display this help and exit

EXAMPLE:

  $ME --since=2008-01-02 > AUTHORS

Report bugs to <${BUGREPORT}>
EOF
    }
    exit($exit_code);
}

# If the string $S is a well-behaved file name, simply return it.
# If it contains white space, quotes, etc., quote it, and return the new string.
sub shell_quote($) {
    my ($s) = @_;
    if ($s =~ m![^\w+/.,-]!) {
	# Convert each single quote to '\''
	$s =~ s/\'/\'\\\'\'/g;
	# Then single quote the string.
	$s = "'$s'";
    }
    return $s;
}

sub quoted_cmd(@) {
    return join (' ', map {shell_quote $_} @_);
}

# Main
{
    my $since_date = '1970-01-01 UTC';
    GetOptions(
	'h|help'  => sub { usage 0 },
	'since=s' => \$since_date,
	) or usage 1;

    @ARGV
	and (warn "$ME: too many arguments\n"), usage 1;

    my @cmd = (qw (git shortlog --summary), "--since=$since_date");
    open PIPE, '-|', @cmd
	or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n" .
		"(Is your Git too old?  Version 1.5.1 or later required.)\n");

    my %committers;

    while (<PIPE>) {
	my $line = $_;

	defined($line) or last;
	chomp($line);

	if ($line =~ /^\s*\d+\s+(.*)\s*$/) {
	    my $name = $1;

	    if (defined($committers{$name})) {
		die "$ME: Duplicated name found, fix your .mailmap";
	    }
	    $committers{$name} = 1;
	} else {
	    die "$ME: Unhandled line found ('$line')";
	}
    }

    close PIPE
	or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";

    my @keys = sort(keys(%committers));
    foreach my $name (@keys) {
	print "$name\n";
    }
}