summaryrefslogtreecommitdiffstats
path: root/gitmirror/robotfmt.pl
blob: 0009d5e0870a3af4a33a2cb22839e4c59f755ceb (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/perl
#
# Take a commit and generate an email from it
#

use Email::Valid;
use MIME::EncWords;

($rev, $branches) = @ARGV;

sub count_commits($$) {
    my($f, $t) = @_;
    my($rl, $l);
    my $count = 0;

    open($rl, "-|", "git rev-list \Q${f}\E..\Q${t}\E") or die;
    while (defined($l = <$rl>)) {
	$count++;
    }
    close($rl);
    return $count;
}

sub best_branch($$) {
    my($rev, $branches) = @_;
    my($r, $best, $best_metric);
    my($branch_fd, $present_fd);

    my %branches = ();
    open($branch_fd, '<', $branches) or die;
    while (defined($r = <$branch_fd>)) {
	chomp $r;
	$branches{$r}++;
    }
    close($branch_fd);

    $best = 'branch?';
    undef $best_metric;
    open($present_fd, '-|', "git branch -a --contains \"\Q${rev}\E\"") or die;
    while (defined($l = <$present_fd>)) {
	chomp $l;
	if ($l =~ m:^[ \*] (remotes/|)origin/(.*)$:) {
	    $l = $2;
	    if ($branches{$l}) {
		$metric = count_commits("refs/remotes/origin/$l", $rev);
		if (!defined($best_metric) || $metric < $best_metric) {
		    $best = $l;
		    $best_metric = $metric;
		}
	    }
	}
    }
    close($present_fd);

    return $best;
}

$branch = best_branch($rev, $branches);
$format = <<EOF;
From: \"nasm-bot for %an\" <%ae>
To: NASM commits mailing list <nasm-cvs\@lists.sourceforge.net>
Subject: [nasm:${branch}] %s
Message-ID: <nasm-%H\@git.nasm.us>
Git-Commit-ID: %H
X-Mailer: NASM-git-log-daemon
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Precedence: bulk

Commit-ID:  %H
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=%H
Author:     %an <%ae>
AuthorDate: %aD
Committer:  %cn <%ce>
CommitDate: %cD

%s

%b

EOF
$format =~ s/\n/%n/g;

@message = ();
@references = ();
%cclist = ();
$lkml = 'nasm-devel@lists.sourceforge.net';
open(LOG, "-|", "git log -M -C -C --pretty=format:\Q${format}\E -p --stat \Q${rev}^..${rev}\E") or die;
$header = 1;
while (defined($l = <LOG>)) {
    chomp $l;
    $header = 0 if ($l eq '');
    if ($header) {
	if ($l =~ /^From: \"([a-zA-Z0-9_\-\. ]*)\" (\<[^\<\>]*\>)$/i) {
	    # Strip quotes in From: if author string seems safe
	    $l = "From: $1 $2";
	}
	$l = MIME::EncWords::encode_mimewords($l, Charset => 'UTF-8');
    } else {
	if ($l =~ /^([a-z-]+-by|Cc|Author):\s.*<([^\>]+)>\s*$/i) {
	    $e = $2;
	    if (Email::Valid->address($e)) {
		$cclist{$e}++;
	    }
	} elsif ($l =~ /^NASM-Devel-Reference:\s*(\<.+\>)\s*$/i) {
	    # We have a thread in which to autopost to LKML...
	    # Let's be very careful with this...
	    $cclist{$lkml}++;
	    push(@references, $1);
	}
    }
    push(@message, $l."\n");
}
close(LOG);

# We don't actually want to send this to anyone else...
%cclist = ();

# WARNING: make sure all the moreheaders are newline-terminated!!!!
%replylist = %cclist;
%replylist = ();
$replylist{$lkml}++;		# Always include LKML in Reply-To:

@moreheaders =
(
    "Cc: ".join(",\n\t", keys(%cclist))."\n",
    "Reply-To: ".join(",\n\t", keys(%replylist))."\n"
);
if (scalar(@references)) {
    push(@moreheaders,
	 ("In-Reply-To: ".$references[scalar(@references)-1]."\n",
	  "References: ".join(",\n\t", @references)."\n"));
}

splice(@message, 2, 0, @moreheaders);
open(SENDMAIL, "|-", "sendmail -i -t -O DeliveryMode=b") or die;
print SENDMAIL @message;
close(SENDMAIL);