3 # Tool to send git commit notifications
5 # Copyright 2005 Alexandre Julliard
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.
13 # This script is meant to be called from .git/hooks/update.
15 # Usage: git-notify [options] [--] refname old-sha1 new-sha1
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
31 binmode STDIN, ':utf8';
32 binmode STDOUT, ':utf8';
34 # some parameters you may want to change
36 # base URL of the gitweb repository browser (can be set with the -u option)
37 my $gitweb_url = "http://source.winehq.org/git";
39 # set this to something that takes "-s"
40 my $mailer = "/usr/bin/mail";
42 # default repository name (can be changed with the -r option)
45 # max size of diffs in bytes (can be changed with the -s option)
46 my $max_diff_size = 10000;
48 # address for mail notices (can be set with -m option)
49 my $commitlist_address;
51 # project name for CIA notices (can be set with -c option)
54 # CIA notification address
55 my $cia_address = "cia\@cia.navi.cx";
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;
64 my @exclude_list = ();
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";
85 my @chars = unpack "U*", $str;
86 $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars;
90 # format an integer date + timezone as string
91 # algorithm taken from git's date.c
98 my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
99 $time -= $minutes * 60;
103 my $minutes = ($tz / 100) * 60 + ($tz % 100);
104 $time += $minutes * 60;
106 return gmtime($time) . sprintf " %+05d", $tz;
109 # parse command line options
112 while (@ARGV && $ARGV[0] =~ /^-/)
114 my $arg = shift @ARGV;
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++; }
127 if (@ARGV && $#ARGV != 2) { usage(); }
130 # send an email notification
131 sub mail_notification($$$@)
133 my ($name, $subject, $content_type, @text) = @_;
134 $subject = encode("MIME-Q",$subject);
137 print "---------------------\n";
139 print "Subject: $subject\n";
140 print "Content-Type: $content_type\n";
141 print "\n", join("\n", @text), "\n";
145 my $pid = open MAIL, "|-";
146 return unless defined $pid;
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";
152 print MAIL join("\n", @text), "\n";
157 # get the default repository name
160 my $dir = `git rev-parse --git-dir`;
162 my $repos = realpath($dir);
163 $repos =~ s/(.*?)((\.git\/)?\.git)$/\1/;
164 $repos =~ s/(.*)\/([^\/]+)\/?$/\2/;
168 # extract the information from a commit or tag object and return a hash containing the various fields
169 sub get_object_info($)
176 open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
181 open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
187 last if /^-----BEGIN PGP SIGNATURE-----/;
190 elsif (/^(author|committer|tagger) ((.*)(<.*>)) (\d+) ([+-]\d+)$/)
193 $info{$1 . "_name"} = $3;
194 $info{$1 . "_email"} = $4;
195 $info{$1 . "_date"} = $5;
196 $info{$1 . "_tz"} = $6;
202 elsif (/^$/) { $do_log = 1; }
206 $info{"type"} = $type;
207 $info{"log"} = \@log;
211 # send a commit notice to a mailing list
212 sub send_commit_notice($$)
215 my %info = get_object_info($obj);
219 if ($info{"type"} eq "tag")
222 "Module: $repos_name",
225 "URL: $gitweb_url/?a=tag;h=$obj",
227 "Tagger: " . $info{"tagger"},
228 "Date: " . format_date($info{"tagger_date"},$info{"tagger_tz"}),
230 join "\n", @{$info{"log"}};
231 $subject = "Tag " . $info{"tag"} . " : " . $info{"tagger_name"} . ": " . ${$info{"log"}}[0];
236 "Module: $repos_name",
239 "URL: $gitweb_url/?a=commit;h=$obj",
241 "Author: " . $info{"author"},
242 "Date: " . format_date($info{"author_date"},$info{"author_tz"}),
244 join "\n", @{$info{"log"}},
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>);
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> );
257 if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
263 push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj",
266 $subject = $info{"author_name"} . ": " . ${$info{"log"}}[0];
269 mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
272 # send a commit notice to the CIA server
273 sub send_cia_notice($$)
275 my ($ref,$commit) = @_;
276 my %info = get_object_info($commit);
279 return if $info{"type"} ne "commit";
284 " <name>git-notify script for CIA</name>",
287 " <project>" . xml_escape($cia_project_name) . "</project>",
288 " <module>" . xml_escape($repos_name) . "</module>",
289 " <branch>" . xml_escape($ref). "</branch>",
293 " <revision>" . substr($commit,0,10) . "</revision>",
294 " <author>" . xml_escape($info{"author"}) . "</author>",
295 " <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
298 open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
302 if (/^([AMD])\t(.*)$/)
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>";
309 elsif (/^R\d+\t(.*)\t(.*)$/)
311 my ($old, $new) = ($1, $2);
312 push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
319 " <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>",
322 " <timestamp>" . $info{"author_date"} . "</timestamp>",
325 mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
328 # send a global commit notice when there are too many commits for individual mails
329 sub send_global_notice($$$)
331 my ($ref, $old_sha1, $new_sha1) = @_;
334 open LIST, "-|" or exec "git", "rev-list", "--pretty", "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
338 s/^commit /URL: $gitweb_url\/?a=commit;h=/;
343 mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
346 # send all the notices
347 sub send_all_notices($$$)
349 my ($ref, $old_sha1, $new_sha1) = @_;
351 $ref =~ s/^refs\/heads\///;
353 if ($old_sha1 eq '0' x 40) # new ref
355 send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
361 open LIST, "-|" or exec "git", "rev-list", "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
365 die "invalid commit $_" unless /^[0-9a-f]{40}$/;
366 unshift @commits, $_;
370 if (@commits > $max_individual_notices)
372 send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
376 foreach my $commit (@commits)
378 send_commit_notice( $ref, $commit ) if $commitlist_address;
379 send_cia_notice( $ref, $commit ) if $cia_project_name;
383 $repos_name = get_repos_name();
386 # append repository path to URL
387 $gitweb_url .= "/$repos_name.git";
391 send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
393 else # read them from stdin
398 if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $3, $1, $2 ); }