diff --git a/tools/git-notify b/tools/git-notify
index 9ab012e34d36a7847b335ffcf2b5a3010ef6ab99..d53461c8af7f8c08ee83f3d9859253520d4784f9 100755 (executable)
--- a/tools/git-notify
+++ b/tools/git-notify
# Tool to send git commit notifications
#
# Copyright 2005 Alexandre Julliard
+# Copyright 2009 Nagios Plugins Development Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# -n max Set max number of individual mails to send
# -r name Set the git repository name
# -s bytes Set the maximum diff size in bytes (-1 for no limit)
+# -t file Set the file to use for reading and saving state
+# -U mask Set the umask for creating the state file
# -u url Set the URL to the gitweb browser
# -i branch If at least one -i is given, report only for specified branches
# -x branch Exclude changes to the specified branch from reports
#
use strict;
-use open ':utf8';
-use Encode 'encode';
+use Fcntl ':flock';
+use Encode qw(encode decode);
use Cwd 'realpath';
-binmode STDIN, ':utf8';
-binmode STDOUT, ':utf8';
-
sub git_config($);
sub get_repos_name();
# branches to exclude
my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || "";
+# the state file we use (can be changed with the -t option)
+my $state_file = git_config( "notify.statefile" ) || "/var/tmp/git-notify.state";
+
+# umask for creating the state file (can be set with -U option)
+my $mode_mask = git_config( "notify.umask" ) || 002;
+
# Extra options to git rev-list
my @revlist_options;
print " -n max Set max number of individual mails to send\n";
print " -r name Set the git repository name\n";
print " -s bytes Set the maximum diff size in bytes (-1 for no limit)\n";
+ print " -t file Set the file to use for reading and saving state\n";
+ print " -U mask Set the umask for creating the state file\n";
print " -u url Set the URL to the gitweb browser\n";
print " -i branch If at least one -i is given, report only for specified branches\n";
print " -x branch Exclude changes to the specified branch from reports\n";
return $revlist;
}
+# append the given commit hashes to the state file
+sub save_commits($)
+{
+ my $commits = shift;
+
+ open STATE, ">>", $state_file or die "Cannot open $state_file: $!";
+ flock STATE, LOCK_EX or die "Cannot lock $state_file";
+ print STATE "$_\n" for @$commits;
+ flock STATE, LOCK_UN or die "Cannot unlock $state_file";
+ close STATE or die "Cannot close $state_file: $!";
+}
+
+# for the given range, return the new hashes and append them to the state file
+sub get_new_commits($$)
+{
+ my ($old_sha1, $new_sha1) = @_;
+ my ($seen, @args);
+ my $newrevs = [];
+
+ @args = ( "^$old_sha1" ) unless $old_sha1 eq '0' x 40;
+ push @args, $new_sha1, @exclude_list;
+
+ my $revlist = git_rev_list(@args);
+
+ if (not -e $state_file) # initialize the state file with all hashes
+ {
+ save_commits(git_rev_list("--all", "--full-history"));
+ return $revlist;
+ }
+
+ open STATE, $state_file or die "Cannot open $state_file: $!";
+ flock STATE, LOCK_SH or die "Cannot lock $state_file";
+ while (<STATE>)
+ {
+ chomp;
+ die "Invalid commit: $_" if not /^[0-9a-f]{40}$/;
+ $seen->{$_} = 1;
+ }
+ flock STATE, LOCK_UN or die "Cannot unlock $state_file";
+ close STATE or die "Cannot close $state_file: $!";
+
+ # FIXME: if another git-notify process reads the $state_file at *this*
+ # point, that process might generate duplicates of our notifications.
+
+ save_commits($revlist);
+
+ foreach my $commit (@$revlist)
+ {
+ push @$newrevs, $commit unless $seen->{$commit};
+ }
+ return $newrevs;
+}
+
+# truncate the given string if it exceeds the specified number of characters
+sub truncate_str($$)
+{
+ my ($str, $max) = @_;
+
+ if (length($str) > $max)
+ {
+ $str = substr($str, 0, $max);
+ $str =~ s/\s+\S+$//;
+ $str .= " ...";
+ }
+ return $str;
+}
+
# right-justify the left column of "left: right" elements, omit undefined elements
sub format_table(@)
{
elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
+ elsif ($arg eq '-t') { $state_file = shift @ARGV; }
+ elsif ($arg eq '-U') { $mode_mask = shift @ARGV; }
elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
$subject = encode("MIME-Q",$subject);
if ($debug)
{
+ binmode STDOUT, ":utf8";
print "---------------------\n";
print "To: $name\n";
print "Subject: $subject\n";
{
exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
}
+ binmode MAIL, ":utf8";
print MAIL join("\n", @text), "\n";
- close MAIL;
+ close MAIL or die $! ? "Cannot execute $mailer: $!" : "$mailer exited with status: $?";
}
}
return $repos;
}
-# extract the information from a commit object and return a hash containing the various fields
+# extract the information from a commit or tag object and return a hash containing the various fields
sub get_object_info($)
{
my $obj = shift;
my @log = ();
my $do_log = 0;
- open OBJ, "-|" or exec "git", "cat-file", "commit", $obj or die "cannot run git-cat-file";
+ $info{"encoding"} = "utf-8";
+
+ open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
+ my $type = <TYPE>;
+ chomp $type;
+ close TYPE or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
+
+ open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
while (<OBJ>)
{
chomp;
- if ($do_log) { push @log, $_; }
- elsif (/^$/) { $do_log = 1; }
- elsif (/^(author|committer) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
+ if ($do_log)
+ {
+ last if /^-----BEGIN PGP SIGNATURE-----/;
+ push @log, $_;
+ }
+ elsif (/^(author|committer|tagger) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
{
$info{$1} = $2;
$info{$1 . "_name"} = $3;
$info{$1 . "_date"} = $5;
$info{$1 . "_tz"} = $6;
}
+ elsif (/^tag (.+)/)
+ {
+ $info{"tag"} = $1;
+ }
+ elsif (/^encoding (.+)/)
+ {
+ $info{"encoding"} = $1;
+ }
+ elsif (/^$/) { $do_log = 1; }
}
- close OBJ;
+ close OBJ or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
+ $info{"type"} = $type;
$info{"log"} = \@log;
return %info;
}
my ($ref,$obj) = @_;
my %info = get_object_info($obj);
my @notice = ();
+ my ($url,$subject);
- open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
- my $diff = join("", <DIFF>);
- close DIFF;
-
- return if length($diff) == 0;
-
- push @notice, format_table(
- "Module: $repos_name",
- "Branch: $ref",
- "Commit: $obj",
- $gitweb_url ? "URL: $gitweb_url/?a=commit;h=$obj" : undef),
- "Author:" . $info{"author"},
- $info{"committer"} ne $info{"author"} ? "Committer:" . $info{"committer"} : undef,
- "Date:" . format_date($info{"author_date"},$info{"author_tz"}),
- "",
- @{$info{"log"}},
- "",
- "---",
- "";
+ if ($gitweb_url)
+ {
+ open REVPARSE, "-|" or exec "git", "rev-parse", "--short", $obj or die "cannot exec git-rev-parse";
+ my $short_obj = <REVPARSE>;
+ close REVPARSE or die $! ? "Cannot execute rev-parse: $!" : "rev-parse exited with status: $?";
- open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
- push @notice, join("", <STAT>);
- close STAT;
+ $short_obj = $obj if not defined $short_obj;
+ chomp $short_obj;
+ $url = "$gitweb_url/?a=$info{type};h=$short_obj";
+ }
- if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
+ if ($info{"type"} eq "tag")
{
- push @notice, $diff;
+ push @notice, format_table(
+ "Module: $repos_name",
+ "Branch: $ref",
+ "Tag: $obj",
+ "Tagger:" . $info{"tagger"},
+ "Date:" . format_date($info{"tagger_date"},$info{"tagger_tz"}),
+ $url ? "URL: $url" : undef),
+ "",
+ join "\n", @{$info{"log"}};
+
+ $subject = "Tag " . $info{"tag"} . ": " . $info{"tagger_name"};
}
else
{
- push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
+ push @notice, format_table(
+ "Module: $repos_name",
+ "Branch: $ref",
+ "Commit: $obj",
+ "Author:" . $info{"author"},
+ $info{"committer"} ne $info{"author"} ? "Committer:" . $info{"committer"} : undef,
+ "Date:" . format_date($info{"author_date"},$info{"author_tz"}),
+ $url ? "URL: $url" : undef),
+ "",
+ @{$info{"log"}},
+ "",
+ "---",
+ "";
+
+ open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
+ push @notice, join("", <STAT>);
+ close STAT or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
+
+ open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
+ my $diff = join("", <DIFF>);
+ close DIFF or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
+
+ if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
+ {
+ push @notice, $diff;
+ }
+ else
+ {
+ push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
+ }
+ $subject = $info{"author_name"};
}
- mail_notification($commitlist_address,
- $info{"author_name"} . ": " . ${$info{"log"}}[0],
- "text/plain; charset=UTF-8", @notice);
+ $subject .= ": " . truncate_str(${$info{"log"}}[0],50);
+ $_ = decode($info{"encoding"}, $_) for @notice;
+ mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
$sent_notices++;
}
my %info = get_object_info($commit);
my @cia_text = ();
+ return if $info{"type"} ne "commit";
+
push @cia_text,
"<message>",
" <generator>",
push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
}
}
- close COMMIT;
+ close COMMIT or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
push @cia_text,
" </files>",
parse_options();
+umask( $mode_mask );
+
# append repository path to URL
$gitweb_url .= "/$repos_name.git" if $gitweb_url;