summaryrefslogtreecommitdiffstats
path: root/robotfmt.pl
blob: 764bb4c26e0cfcf93064a502b833bad0f7fc4445 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/perl
#
# Take a commit and generate an email from it
#

use Email::Valid;
use URI::Escape;
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);

$sender = 'tipbot@zytor.com';

$format = <<EOF;
Sender: tip tree robot <${sender}>
From: \"tip-bot for %an\" <${sender}>
To: linux-tip-commits\@vger.kernel.org
Subject: [tip:${branch}] %s
Git-Commit-ID: %H
X-Mailer: tip-git-log-daemon
Robot-ID: <tip-bot.git.kernel.org>
Robot-Unsubscribe: Contact <mailto:hpa\@kernel.org> to get blacklisted from these emails
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Precedence: bulk

Commit-ID:  %H
Gitweb:     http://git.kernel.org/tip/%H
Author:     %an <%ae>
AuthorDate: %aD
Committer:  %cn <%ce>
CommitDate: %cD

%s

%b
EOF
$format =~ s/\n/%n/g;
$format =~ s/\%b\%n/\%b/;		# Remove newline after %b

@message = ();
@references = ();
%cclist = ('mingo@kernel.org' => 1,
	   'tglx@linutronix.de' => 1,
	   'hpa@zytor.com' => 1);
@blacklist = ();
$lkml = 'linux-kernel@vger.kernel.org';
#$test_only = 'hpa+tiptest@zytor.com';

# Form used for inital-submission emails
$nullref = '<tip-*@git.kernel.org>';

open(LOG, "-|", "git log -M -C -C --pretty=format:\Q${format}\E -p --stat \Q${rev}^..${rev}\E") or die;
binmode(LOG, ':utf8');
$header = 1;
$message_id = "<tip-${rev}\@git.kernel.org>";
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 =~ /^Link:\s*http:\/\/lkml.kernel.org\/([rn])\/\<?([^\;\?\<\>\#]+)\>?$/i) {
	    my $type = $1;
	    my $ref = uri_unescape($2);
	    if ($ref ne '' && $ref !~ /[^!#\$\%&'()*+,-.\/0-9:;=?\@A-Z\[\]_`a-z{|}~]/) {
		$ref = '<'.$ref.'>';
		$cclist{$lkml}++;
		if ($type eq 'n' && $ref =~ /^\<tip-[0-9a-z]+\@git\.kernel\.org\>$/) {
		    # New submission magic
		    $message_id = $ref;
		} elsif ($type eq 'r' && $ref ne $nullref) {
		    push(@references, $ref);
		}
	    }
	} elsif ($l =~ /^LKML-Reference:\s*(\<.+\>)\s*$/i) {
	    # We have a thread in which to autopost to LKML...
	    # Let's be very careful with this...
	    $cclist{$lkml}++;
	    if ($1 ne $nullref) {
		push(@references, $1);
	    }
	}
    }
    push(@message, $l."\n");
}
close(LOG);

# Remove blacklisted addresses
foreach $b (@blacklist) {
    delete $cclist{$b};
}

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

@moreheaders =
(
    "Message-ID: ".$message_id."\n",
    "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);
$target = $test_only ? "\Q$test_only" : '-t';
open(SENDMAIL, "|-", "sendmail -f \Q$sender\E -i -O DeliveryMode=b $target") or die;
binmode(SENDMAIL, ':utf8');
print SENDMAIL @message;
close(SENDMAIL);