Code

git-notify: Remove unused tag notification code
[nagiosplug.git] / tools / git-notify
1 #!/usr/bin/perl -w
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/post-receive.
14 #
15 # Usage: git-notify [options] [--] old-sha1 new-sha1 refname
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 #   -i branch If at least one -i is given, report only for specified branches
24 #   -x branch Exclude changes to the specified branch from reports
25 #   -X        Exclude merge commits
26 #
28 use strict;
29 use open ':utf8';
30 use Encode 'encode';
31 use Cwd 'realpath';
33 binmode STDIN, ':utf8';
34 binmode STDOUT, ':utf8';
36 sub git_config($);
37 sub get_repos_name();
39 # some parameters you may want to change
41 # set this to something that takes "-s"
42 my $mailer = "/usr/bin/mail";
44 # CIA notification address
45 my $cia_address = "cia\@cia.navi.cx";
47 # debug mode
48 my $debug = 0;
50 # configuration parameters
52 # base URL of the gitweb repository browser (can be set with the -u option)
53 my $gitweb_url = git_config( "notify.baseurl" );
55 # default repository name (can be changed with the -r option)
56 my $repos_name = git_config( "notify.repository" ) || get_repos_name();
58 # max size of diffs in bytes (can be changed with the -s option)
59 my $max_diff_size = git_config( "notify.maxdiff" ) || 10000;
61 # address for mail notices (can be set with -m option)
62 my $commitlist_address = git_config( "notify.mail" );
64 # project name for CIA notices (can be set with -c option)
65 my $cia_project_name = git_config( "notify.cia" );
67 # max number of individual notices before falling back to a single global notice (can be set with -n option)
68 my $max_individual_notices = git_config( "notify.maxnotices" ) || 100;
70 # branches to include
71 my @include_list = split /\s+/, git_config( "notify.include" ) || "";
73 # branches to exclude
74 my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || "";
76 # Extra options to git rev-list
77 my @revlist_options;
79 sub usage()
80 {
81     print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n";
82     print "   -c name   Send CIA notifications under specified project name\n";
83     print "   -m addr   Send mail notifications to specified address\n";
84     print "   -n max    Set max number of individual mails to send\n";
85     print "   -r name   Set the git repository name\n";
86     print "   -s bytes  Set the maximum diff size in bytes (-1 for no limit)\n";
87     print "   -u url    Set the URL to the gitweb browser\n";
88     print "   -i branch If at least one -i is given, report only for specified branches\n";
89     print "   -x branch Exclude changes to the specified branch from reports\n";
90     print "   -X        Exclude merge commits\n";
91     exit 1;
92 }
94 sub xml_escape($)
95 {
96     my $str = shift;
97     $str =~ s/&/&/g;
98     $str =~ s/</&lt;/g;
99     $str =~ s/>/&gt;/g;
100     my @chars = unpack "U*", $str;
101     $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars;
102     return $str;
105 # format an integer date + timezone as string
106 # algorithm taken from git's date.c
107 sub format_date($$)
109     my ($time,$tz) = @_;
111     if ($tz < 0)
112     {
113         my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
114         $time -= $minutes * 60;
115     }
116     else
117     {
118         my $minutes = ($tz / 100) * 60 + ($tz % 100);
119         $time += $minutes * 60;
120     }
121     return gmtime($time) . sprintf " %+05d", $tz;
124 # fetch a parameter from the git config file
125 sub git_config($)
127     my ($param) = @_;
129     open CONFIG, "-|" or exec "git", "config", $param;
130     my $ret = <CONFIG>;
131     chomp $ret if $ret;
132     close CONFIG or $ret = undef;
133     return $ret;
136 # parse command line options
137 sub parse_options()
139     while (@ARGV && $ARGV[0] =~ /^-/)
140     {
141         my $arg = shift @ARGV;
143         if ($arg eq '--') { last; }
144         elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
145         elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
146         elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
147         elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
148         elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
149         elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
150         elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
151         elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
152         elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; }
153         elsif ($arg eq '-d') { $debug++; }
154         else { usage(); }
155     }
156     if (@ARGV && $#ARGV != 2) { usage(); }
157     @exclude_list = map { "^$_"; } @exclude_list;
160 # send an email notification
161 sub mail_notification($$$@)
163     my ($name, $subject, $content_type, @text) = @_;
164     $subject = encode("MIME-Q",$subject);
165     if ($debug)
166     {
167         print "---------------------\n";
168         print "To: $name\n";
169         print "Subject: $subject\n";
170         print "Content-Type: $content_type\n";
171         print "\n", join("\n", @text), "\n";
172     }
173     else
174     {
175         my $pid = open MAIL, "|-";
176         return unless defined $pid;
177         if (!$pid)
178         {
179             exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
180         }
181         print MAIL join("\n", @text), "\n";
182         close MAIL;
183     }
186 # get the default repository name
187 sub get_repos_name()
189     my $dir = `git rev-parse --git-dir`;
190     chomp $dir;
191     my $repos = realpath($dir);
192     $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/;
193     $repos =~ s/(.*)\/([^\/]+)\/?$/$2/;
194     return $repos;
197 # extract the information from a commit object and return a hash containing the various fields
198 sub get_object_info($)
200     my $obj = shift;
201     my %info = ();
202     my @log = ();
203     my $do_log = 0;
205     open OBJ, "-|" or exec "git", "cat-file", "commit", $obj or die "cannot run git-cat-file";
206     while (<OBJ>)
207     {
208         chomp;
209         if ($do_log) { push @log, $_; }
210         elsif (/^$/) { $do_log = 1; }
211         elsif (/^(author|committer) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
212         {
213             $info{$1} = $2;
214             $info{$1 . "_name"} = $3;
215             $info{$1 . "_email"} = $4;
216             $info{$1 . "_date"} = $5;
217             $info{$1 . "_tz"} = $6;
218         }
219     }
220     close OBJ;
222     $info{"log"} = \@log;
223     return %info;
226 # send a commit notice to a mailing list
227 sub send_commit_notice($$)
229     my ($ref,$obj) = @_;
230     my %info = get_object_info($obj);
231     my @notice = ();
233     push @notice,
234         "Module: $repos_name",
235         "Branch: $ref",
236         "Commit: $obj",
237         $gitweb_url ? "URL:    $gitweb_url/?a=commit;h=$obj\n" : "",
238         "Author: " . $info{"author"},
239         "Date:   " . format_date($info{"author_date"},$info{"author_tz"}),
240         "",
241         join "\n", @{$info{"log"}},
242         "",
243         "---",
244         "";
246     open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
247     push @notice, join("", <STAT>);
248     close STAT;
250     open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
251     my $diff = join( "", <DIFF> );
252     close DIFF;
254     if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
255     {
256         push @notice, $diff;
257     }
258     else
259     {
260         push @notice, "Diff:   $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
261     }
263     mail_notification($commitlist_address,
264         $info{"author_name"} . ": " . ${$info{"log"}}[0],
265         "text/plain; charset=UTF-8", @notice);
268 # send a commit notice to the CIA server
269 sub send_cia_notice($$)
271     my ($ref,$commit) = @_;
272     my %info = get_object_info($commit);
273     my @cia_text = ();
275     push @cia_text,
276         "<message>",
277         "  <generator>",
278         "    <name>git-notify script for CIA</name>",
279         "  </generator>",
280         "  <source>",
281         "    <project>" . xml_escape($cia_project_name) . "</project>",
282         "    <module>" . xml_escape($repos_name) . "</module>",
283         "    <branch>" . xml_escape($ref). "</branch>",
284         "  </source>",
285         "  <body>",
286         "    <commit>",
287         "      <revision>" . substr($commit,0,10) . "</revision>",
288         "      <author>" . xml_escape($info{"author"}) . "</author>",
289         "      <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
290         "      <files>";
292     open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
293     while (<COMMIT>)
294     {
295         chomp;
296         if (/^([AMD])\t(.*)$/)
297         {
298             my ($action, $file) = ($1, $2);
299             my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
300             next unless defined $actions{$action};
301             push @cia_text, "        <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
302         }
303         elsif (/^R\d+\t(.*)\t(.*)$/)
304         {
305             my ($old, $new) = ($1, $2);
306             push @cia_text, "        <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
307         }
308     }
309     close COMMIT;
311     push @cia_text,
312         "      </files>",
313         $gitweb_url ? "      <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>" : "",
314         "    </commit>",
315         "  </body>",
316         "  <timestamp>" . $info{"author_date"} . "</timestamp>",
317         "</message>";
319     mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
322 # send a global commit notice when there are too many commits for individual mails
323 sub send_global_notice($$$)
325     my ($ref, $old_sha1, $new_sha1) = @_;
326     my @notice = ();
328     push @revlist_options, "--pretty";
329     open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
330     while (<LIST>)
331     {
332         chomp;
333         s/^commit /URL:    $gitweb_url\/?a=commit;h=/ if $gitweb_url;
334         push @notice, $_;
335     }
336     close LIST;
338     mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
341 # send all the notices
342 sub send_all_notices($$$)
344     my ($old_sha1, $new_sha1, $ref) = @_;
346     $ref =~ s/^refs\/heads\///;
348     return if (@include_list && !grep {$_ eq $ref} @include_list);
350     if ($old_sha1 eq '0' x 40)  # new ref
351     {
352         send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
353         return;
354     }
356     my @commits = ();
358     open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
359     while (<LIST>)
360     {
361         chomp;
362         die "invalid commit $_" unless /^[0-9a-f]{40}$/;
363         unshift @commits, $_;
364     }
365     close LIST;
367     if (@commits > $max_individual_notices)
368     {
369         send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
370         return;
371     }
373     foreach my $commit (@commits)
374     {
375         send_commit_notice( $ref, $commit ) if $commitlist_address;
376         send_cia_notice( $ref, $commit ) if $cia_project_name;
377     }
380 parse_options();
382 # append repository path to URL
383 $gitweb_url .= "/$repos_name.git" if $gitweb_url;
385 if (@ARGV)
387     send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
389 else  # read them from stdin
391     while (<>)
392     {
393         chomp;
394         if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }
395     }
398 exit 0;