First test for emails.
[git-central.git] / wine-git-notify
1 #!/bin/env perl
2 #
3 # Tool to send git commit notifications
4 #
5 # Copyright 2005 Alexandre Julliard
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
11 #
12 #
13 # This script is meant to be called from .git/hooks/update.
14 #
15 # Usage: git-notify [options] [--] refname old-sha1 new-sha1
16 #
17 #   -c name   Send CIA notifications under specified project name
18 #   -m addr   Send mail notifications to specified address
19 #   -n max    Set max number of individual mails to send
20 #   -r name   Set the git repository name
21 #   -s bytes  Set the maximum diff size in bytes (-1 for no limit)
22 #   -u url    Set the URL to the gitweb browser
23 #   -x branch Exclude changes to the specified branch from reports
24 #
25
26 use strict;
27 use open ':utf8';
28 use Encode 'encode';
29 use Cwd 'realpath';
30
31 binmode STDIN, ':utf8';
32 binmode STDOUT, ':utf8';
33
34 # some parameters you may want to change
35
36 # base URL of the gitweb repository browser (can be set with the -u option)
37 my $gitweb_url = "http://source.winehq.org/git";
38
39 # set this to something that takes "-s"
40 my $mailer = "/usr/bin/mail";
41
42 # default repository name (can be changed with the -r option)
43 my $repos_name = "";
44
45 # max size of diffs in bytes (can be changed with the -s option)
46 my $max_diff_size = 10000;
47
48 # address for mail notices (can be set with -m option)
49 my $commitlist_address;
50
51 # project name for CIA notices (can be set with -c option)
52 my $cia_project_name;
53
54 # CIA notification address
55 my $cia_address = "cia\@cia.navi.cx";
56
57 # max number of individual notices before falling back to a single global notice (can be set with -n option)
58 my $max_individual_notices = 100;
59
60 # debug mode
61 my $debug = 0;
62
63 # branches to exclude
64 my @exclude_list = ();
65
66 sub usage()
67 {
68     print "Usage: $0 [options] [--] refname old-sha1 new-sha1\n";
69     print "   -c name   Send CIA notifications under specified project name\n";
70     print "   -m addr   Send mail notifications to specified address\n";
71     print "   -n max    Set max number of individual mails to send\n";
72     print "   -r name   Set the git repository name\n";
73     print "   -s bytes  Set the maximum diff size in bytes (-1 for no limit)\n";
74     print "   -u url    Set the URL to the gitweb browser\n";
75     print "   -x branch Exclude changes to the specified branch from reports\n";
76     exit 1;
77 }
78
79 sub xml_escape($)
80 {
81     my $str = shift;
82     $str =~ s/&/&/g;
83     $str =~ s/</&lt;/g;
84     $str =~ s/>/&gt;/g;
85     my @chars = unpack "U*", $str;
86     $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars;
87     return $str;
88 }
89
90 # format an integer date + timezone as string
91 # algorithm taken from git's date.c
92 sub format_date($$)
93 {
94     my ($time,$tz) = @_;
95
96     if ($tz < 0)
97     {
98         my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
99         $time -= $minutes * 60;
100     }
101     else
102     {
103         my $minutes = ($tz / 100) * 60 + ($tz % 100);
104         $time += $minutes * 60;
105     }
106     return gmtime($time) . sprintf " %+05d", $tz;
107 }
108
109 # parse command line options
110 sub parse_options()
111 {
112     while (@ARGV && $ARGV[0] =~ /^-/)
113     {
114         my $arg = shift @ARGV;
115
116         if ($arg eq '--') { last; }
117         elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
118         elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
119         elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
120         elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
121         elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
122         elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
123         elsif ($arg eq '-x') { push @exclude_list, "^" . shift @ARGV; }
124         elsif ($arg eq '-d') { $debug++; }
125         else { usage(); }
126     }
127     if (@ARGV && $#ARGV != 2) { usage(); }
128 }
129
130 # send an email notification
131 sub mail_notification($$$@)
132 {
133     my ($name, $subject, $content_type, @text) = @_;
134     $subject = encode("MIME-Q",$subject);
135     if ($debug)
136     {
137         print "---------------------\n";
138         print "To: $name\n";
139         print "Subject: $subject\n";
140         print "Content-Type: $content_type\n";
141         print "\n", join("\n", @text), "\n";
142     }
143     else
144     {
145         my $pid = open MAIL, "|-";
146         return unless defined $pid;
147         if (!$pid)
148         {
149             # exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
150             exec "/home/BIPFS/shaberman/local/bin/msmtp", "--file=/srv/git/hooks/msmtp.conf", "-t", "shaberman@exigencecorp.com", "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
151         }
152         print MAIL join("\n", @text), "\n";
153         close MAIL;
154     }
155 }
156
157 # get the default repository name
158 sub get_repos_name()
159 {
160     my $dir = `git rev-parse --git-dir`;
161     chomp $dir;
162     my $repos = realpath($dir);
163     $repos =~ s/(.*?)((\.git\/)?\.git)$/\1/;
164     $repos =~ s/(.*)\/([^\/]+)\/?$/\2/;
165     return $repos;
166 }
167
168 # extract the information from a commit or tag object and return a hash containing the various fields
169 sub get_object_info($)
170 {
171     my $obj = shift;
172     my %info = ();
173     my @log = ();
174     my $do_log = 0;
175
176     open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
177     my $type = <TYPE>;
178     chomp $type;
179     close TYPE;
180
181     open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
182     while (<OBJ>)
183     {
184         chomp;
185         if ($do_log)
186         {
187             last if /^-----BEGIN PGP SIGNATURE-----/;
188             push @log, $_;
189         }
190         elsif (/^(author|committer|tagger) ((.*)(<.*>)) (\d+) ([+-]\d+)$/)
191         {
192             $info{$1} = $2;
193             $info{$1 . "_name"} = $3;
194             $info{$1 . "_email"} = $4;
195             $info{$1 . "_date"} = $5;
196             $info{$1 . "_tz"} = $6;
197         }
198         elsif (/^tag (.*)$/)
199         {
200             $info{"tag"} = $1;
201         }
202         elsif (/^$/) { $do_log = 1; }
203     }
204     close OBJ;
205
206     $info{"type"} = $type;
207     $info{"log"} = \@log;
208     return %info;
209 }
210
211 # send a commit notice to a mailing list
212 sub send_commit_notice($$)
213 {
214     my ($ref,$obj) = @_;
215     my %info = get_object_info($obj);
216     my @notice = ();
217     my $subject;
218
219     if ($info{"type"} eq "tag")
220     {
221         push @notice,
222         "Module: $repos_name",
223         "Branch: $ref",
224         "Tag:    $obj",
225         "URL:    $gitweb_url/?a=tag;h=$obj",
226         "",
227         "Tagger: " . $info{"tagger"},
228         "Date:   " . format_date($info{"tagger_date"},$info{"tagger_tz"}),
229         "",
230         join "\n", @{$info{"log"}};
231         $subject = "Tag " . $info{"tag"} . " : " . $info{"tagger_name"} . ": " . ${$info{"log"}}[0];
232     }
233     else
234     {
235         push @notice,
236         "Module: $repos_name",
237         "Branch: $ref",
238         "Commit: $obj",
239         "URL:    $gitweb_url/?a=commit;h=$obj",
240         "",
241         "Author: " . $info{"author"},
242         "Date:   " . format_date($info{"author_date"},$info{"author_tz"}),
243         "",
244         join "\n", @{$info{"log"}},
245         "",
246         "---",
247         "";
248
249         open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
250         push @notice, join("", <STAT>);
251         close STAT;
252
253         open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
254         my $diff = join( "", <DIFF> );
255         close DIFF;
256
257         if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
258         {
259             push @notice, $diff;
260         }
261         else
262         {
263             push @notice, "Diff:   $gitweb_url/?a=commitdiff;h=$obj",
264         }
265
266         $subject = $info{"author_name"} . ": " . ${$info{"log"}}[0];
267     }
268
269     mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
270 }
271
272 # send a commit notice to the CIA server
273 sub send_cia_notice($$)
274 {
275     my ($ref,$commit) = @_;
276     my %info = get_object_info($commit);
277     my @cia_text = ();
278
279     return if $info{"type"} ne "commit";
280
281     push @cia_text,
282         "<message>",
283         "  <generator>",
284         "    <name>git-notify script for CIA</name>",
285         "  </generator>",
286         "  <source>",
287         "    <project>" . xml_escape($cia_project_name) . "</project>",
288         "    <module>" . xml_escape($repos_name) . "</module>",
289         "    <branch>" . xml_escape($ref). "</branch>",
290         "  </source>",
291         "  <body>",
292         "    <commit>",
293         "      <revision>" . substr($commit,0,10) . "</revision>",
294         "      <author>" . xml_escape($info{"author"}) . "</author>",
295         "      <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
296         "      <files>";
297
298     open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
299     while (<COMMIT>)
300     {
301         chomp;
302         if (/^([AMD])\t(.*)$/)
303         {
304             my ($action, $file) = ($1, $2);
305             my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
306             next unless defined $actions{$action};
307             push @cia_text, "        <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
308         }
309         elsif (/^R\d+\t(.*)\t(.*)$/)
310         {
311             my ($old, $new) = ($1, $2);
312             push @cia_text, "        <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
313         }
314     }
315     close COMMIT;
316
317     push @cia_text,
318         "      </files>",
319         "      <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>",
320         "    </commit>",
321         "  </body>",
322         "  <timestamp>" . $info{"author_date"} . "</timestamp>",
323         "</message>";
324
325     mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
326 }
327
328 # send a global commit notice when there are too many commits for individual mails
329 sub send_global_notice($$$)
330 {
331     my ($ref, $old_sha1, $new_sha1) = @_;
332     my @notice = ();
333
334     open LIST, "-|" or exec "git", "rev-list", "--pretty", "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
335     while (<LIST>)
336     {
337         chomp;
338         s/^commit /URL:    $gitweb_url\/?a=commit;h=/;
339         push @notice, $_;
340     }
341     close LIST;
342
343     mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
344 }
345
346 # send all the notices
347 sub send_all_notices($$$)
348 {
349     my ($ref, $old_sha1, $new_sha1) = @_;
350
351     $ref =~ s/^refs\/heads\///;
352
353     if ($old_sha1 eq '0' x 40)  # new ref
354     {
355         send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
356         return;
357     }
358
359     my @commits = ();
360
361     open LIST, "-|" or exec "git", "rev-list", "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
362     while (<LIST>)
363     {
364         chomp;
365         die "invalid commit $_" unless /^[0-9a-f]{40}$/;
366         unshift @commits, $_;
367     }
368     close LIST;
369
370     if (@commits > $max_individual_notices)
371     {
372         send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
373         return;
374     }
375
376     foreach my $commit (@commits)
377     {
378         send_commit_notice( $ref, $commit ) if $commitlist_address;
379         send_cia_notice( $ref, $commit ) if $cia_project_name;
380     }
381 }
382
383 $repos_name = get_repos_name();
384 parse_options();
385
386 # append repository path to URL
387 $gitweb_url .= "/$repos_name.git";
388
389 if (@ARGV)
390 {
391     send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
392 }
393 else  # read them from stdin
394 {
395     while (<>)
396     {
397         chomp;
398         if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $3, $1, $2 ); }
399     }
400 }
401
402 exit 0;
403