michael@516: #!/usr/bin/perl -w michael@516: # michael@516: # Tool to send git commit notifications michael@516: # michael@516: # Copyright 2005 Alexandre Julliard michael@516: # michael@516: # This program is free software; you can redistribute it and/or michael@516: # modify it under the terms of the GNU General Public License as michael@516: # published by the Free Software Foundation; either version 2 of michael@516: # the License, or (at your option) any later version. michael@516: # michael@516: # michael@516: # This script is meant to be called from .git/hooks/post-receive. michael@516: # michael@516: # Usage: git-notify [options] [--] old-sha1 new-sha1 refname michael@516: # michael@516: # -c name Send CIA notifications under specified project name michael@516: # -m addr Send mail notifications to specified address michael@516: # -n max Set max number of individual mails to send michael@516: # -r name Set the git repository name michael@516: # -s bytes Set the maximum diff size in bytes (-1 for no limit) michael@516: # -u url Set the URL to the gitweb browser michael@516: # -i branch If at least one -i is given, report only for specified branches michael@516: # -x branch Exclude changes to the specified branch from reports michael@516: # -X Exclude merge commits michael@516: # michael@516: michael@516: use strict; michael@516: use open ':utf8'; michael@516: use Encode 'encode'; michael@516: use Cwd 'realpath'; michael@516: michael@516: binmode STDIN, ':utf8'; michael@516: binmode STDOUT, ':utf8'; michael@516: michael@516: sub git_config($); michael@516: sub get_repos_name(); michael@516: michael@516: # some parameters you may want to change michael@516: michael@516: # CIA notification address michael@516: my $cia_address = "cia\@cia.navi.cx"; michael@516: michael@516: # debug mode michael@516: my $debug = 0; michael@516: michael@516: # configuration parameters michael@516: michael@516: # base URL of the gitweb repository browser (can be set with the -u option) michael@516: my $gitweb_url = git_config( "notify.baseurl" ); michael@516: michael@516: # default repository name (can be changed with the -r option) michael@516: my $repos_name = git_config( "notify.repository" ) || get_repos_name(); michael@516: michael@516: # max size of diffs in bytes (can be changed with the -s option) michael@516: my $max_diff_size = git_config( "notify.maxdiff" ) || 10000; michael@516: michael@516: # address for mail notices (can be set with -m option) michael@516: my $commitlist_address = git_config( "notify.mail" ); michael@516: michael@516: # project name for CIA notices (can be set with -c option) michael@516: my $cia_project_name = git_config( "notify.cia" ); michael@516: michael@516: # max number of individual notices before falling back to a single global notice (can be set with -n option) michael@516: my $max_individual_notices = git_config( "notify.maxnotices" ) || 100; michael@516: michael@516: # branches to include michael@516: my @include_list = split /\s+/, git_config( "notify.include" ) || ""; michael@516: michael@516: # branches to exclude michael@516: my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || ""; michael@516: michael@516: # set this to something that takes "-s" michael@516: my $mailer = git_config( "notify.mailer" ) || "/usr/bin/mail"; michael@516: michael@516: # Extra options to git rev-list michael@516: my @revlist_options; michael@516: michael@516: sub usage() michael@516: { michael@516: print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n"; michael@516: print " -c name Send CIA notifications under specified project name\n"; michael@516: print " -m addr Send mail notifications to specified address\n"; michael@516: print " -n max Set max number of individual mails to send\n"; michael@516: print " -r name Set the git repository name\n"; michael@516: print " -s bytes Set the maximum diff size in bytes (-1 for no limit)\n"; michael@516: print " -u url Set the URL to the gitweb browser\n"; michael@516: print " -i branch If at least one -i is given, report only for specified branches\n"; michael@516: print " -x branch Exclude changes to the specified branch from reports\n"; michael@516: print " -X Exclude merge commits\n"; michael@516: exit 1; michael@516: } michael@516: michael@516: sub xml_escape($) michael@516: { michael@516: my $str = shift; michael@516: $str =~ s/&/&/g; michael@516: $str =~ s//>/g; michael@516: my @chars = unpack "U*", $str; michael@516: $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars; michael@516: return $str; michael@516: } michael@516: michael@516: # format an integer date + timezone as string michael@516: # algorithm taken from git's date.c michael@516: sub format_date($$) michael@516: { michael@516: my ($time,$tz) = @_; michael@516: michael@516: if ($tz < 0) michael@516: { michael@516: my $minutes = (-$tz / 100) * 60 + (-$tz % 100); michael@516: $time -= $minutes * 60; michael@516: } michael@516: else michael@516: { michael@516: my $minutes = ($tz / 100) * 60 + ($tz % 100); michael@516: $time += $minutes * 60; michael@516: } michael@516: return gmtime($time) . sprintf " %+05d", $tz; michael@516: } michael@516: michael@516: # fetch a parameter from the git config file michael@516: sub git_config($) michael@516: { michael@516: my ($param) = @_; michael@516: michael@516: open CONFIG, "-|" or exec "git", "config", $param; michael@516: my $ret = ; michael@516: chomp $ret if $ret; michael@516: close CONFIG or $ret = undef; michael@516: return $ret; michael@516: } michael@516: michael@516: # parse command line options michael@516: sub parse_options() michael@516: { michael@516: while (@ARGV && $ARGV[0] =~ /^-/) michael@516: { michael@516: my $arg = shift @ARGV; michael@516: michael@516: if ($arg eq '--') { last; } michael@516: elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; } michael@516: elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; } michael@516: elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; } michael@516: elsif ($arg eq '-r') { $repos_name = shift @ARGV; } michael@516: elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; } michael@516: elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; } michael@516: elsif ($arg eq '-i') { push @include_list, shift @ARGV; } michael@516: elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; } michael@516: elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; } michael@516: elsif ($arg eq '-d') { $debug++; } michael@516: else { usage(); } michael@516: } michael@516: if (@ARGV && $#ARGV != 2) { usage(); } michael@516: @exclude_list = map { "^$_"; } @exclude_list; michael@516: } michael@516: michael@516: # send an email notification michael@516: sub mail_notification($$$@) michael@516: { michael@516: my ($name, $subject, $content_type, @text) = @_; michael@516: $subject = encode("MIME-Q",$subject); michael@516: if ($debug) michael@516: { michael@516: print "---------------------\n"; michael@516: print "To: $name\n"; michael@516: print "Subject: $subject\n"; michael@516: print "Content-Type: $content_type\n"; michael@516: print "\n", join("\n", @text), "\n"; michael@516: } michael@516: else michael@516: { michael@516: my $pid = open MAIL, "|-"; michael@516: return unless defined $pid; michael@516: if (!$pid) michael@516: { michael@516: exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer"; michael@516: } michael@516: print MAIL join("\n", @text), "\n"; michael@516: close MAIL; michael@516: } michael@516: } michael@516: michael@516: # get the default repository name michael@516: sub get_repos_name() michael@516: { michael@516: my $dir = `git rev-parse --git-dir`; michael@516: chomp $dir; michael@516: my $repos = realpath($dir); michael@516: $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/; michael@516: $repos =~ s/(.*)\/([^\/]+)\/?$/$2/; michael@516: return $repos; michael@516: } michael@516: michael@516: # extract the information from a commit or tag object and return a hash containing the various fields michael@516: sub get_object_info($) michael@516: { michael@516: my $obj = shift; michael@516: my %info = (); michael@516: my @log = (); michael@516: my $do_log = 0; michael@516: michael@516: open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file"; michael@516: my $type = ; michael@516: chomp $type; michael@516: close TYPE; michael@516: michael@516: open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file"; michael@516: while () michael@516: { michael@516: chomp; michael@516: if ($do_log) michael@516: { michael@516: last if /^-----BEGIN PGP SIGNATURE-----/; michael@516: push @log, $_; michael@516: } michael@516: elsif (/^(author|committer|tagger) ((.*)(<.*>)) (\d+) ([+-]\d+)$/) michael@516: { michael@516: $info{$1} = $2; michael@516: $info{$1 . "_name"} = $3; michael@516: $info{$1 . "_email"} = $4; michael@516: $info{$1 . "_date"} = $5; michael@516: $info{$1 . "_tz"} = $6; michael@516: } michael@516: elsif (/^tag (.*)$/) michael@516: { michael@516: $info{"tag"} = $1; michael@516: } michael@516: elsif (/^$/) { $do_log = 1; } michael@516: } michael@516: close OBJ; michael@516: michael@516: $info{"type"} = $type; michael@516: $info{"log"} = \@log; michael@516: return %info; michael@516: } michael@516: michael@516: # send a commit notice to a mailing list michael@516: sub send_commit_notice($$) michael@516: { michael@516: my ($ref,$obj) = @_; michael@516: my %info = get_object_info($obj); michael@516: my @notice = (); michael@516: my $subject; michael@516: michael@516: if ($info{"type"} eq "tag") michael@516: { michael@516: push @notice, michael@516: "Module: $repos_name", michael@516: "Branch: $ref", michael@516: "Tag: $obj", michael@516: $gitweb_url ? "URL: $gitweb_url/?a=tag;h=$obj\n" : "", michael@516: "Tagger: " . $info{"tagger"}, michael@516: "Date: " . format_date($info{"tagger_date"},$info{"tagger_tz"}), michael@516: "", michael@516: join "\n", @{$info{"log"}}; michael@516: $subject = "Tag " . $info{"tag"} . " : " . $info{"tagger_name"} . ": " . ${$info{"log"}}[0]; michael@516: } michael@516: else michael@516: { michael@516: push @notice, michael@516: "Module: $repos_name", michael@516: "Branch: $ref", michael@516: "Commit: $obj", michael@516: $gitweb_url ? "URL: $gitweb_url/?a=commit;h=$obj\n" : "", michael@516: "Author: " . $info{"author"}, michael@516: "Date: " . format_date($info{"author_date"},$info{"author_tz"}), michael@516: "", michael@516: join "\n", @{$info{"log"}}, michael@516: "", michael@516: "---", michael@516: ""; michael@516: michael@516: open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree"; michael@516: push @notice, join("", ); michael@516: close STAT; michael@516: michael@516: open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree"; michael@516: my $diff = join( "", ); michael@516: close DIFF; michael@516: michael@516: if (($max_diff_size == -1) || (length($diff) < $max_diff_size)) michael@516: { michael@516: push @notice, $diff; michael@516: } michael@516: else michael@516: { michael@516: push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url; michael@516: } michael@516: michael@516: $subject = $info{"author_name"} . ": " . ${$info{"log"}}[0]; michael@516: } michael@516: michael@516: mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice); michael@516: } michael@516: michael@516: # send a commit notice to the CIA server michael@516: sub send_cia_notice($$) michael@516: { michael@516: my ($ref,$commit) = @_; michael@516: my %info = get_object_info($commit); michael@516: my @cia_text = (); michael@516: michael@516: return if $info{"type"} ne "commit"; michael@516: michael@516: push @cia_text, michael@516: "", michael@516: " ", michael@516: " git-notify script for CIA", michael@516: " ", michael@516: " ", michael@516: " " . xml_escape($cia_project_name) . "", michael@516: " " . xml_escape($repos_name) . "", michael@516: " " . xml_escape($ref). "", michael@516: " ", michael@516: " ", michael@516: " ", michael@516: " " . substr($commit,0,10) . "", michael@516: " " . xml_escape($info{"author"}) . "", michael@516: " " . xml_escape(join "\n", @{$info{"log"}}) . "", michael@516: " "; michael@516: michael@516: open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree"; michael@516: while () michael@516: { michael@516: chomp; michael@516: if (/^([AMD])\t(.*)$/) michael@516: { michael@516: my ($action, $file) = ($1, $2); michael@516: my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" ); michael@516: next unless defined $actions{$action}; michael@516: push @cia_text, " " . xml_escape($file) . ""; michael@516: } michael@516: elsif (/^R\d+\t(.*)\t(.*)$/) michael@516: { michael@516: my ($old, $new) = ($1, $2); michael@516: push @cia_text, " " . xml_escape($old) . ""; michael@516: } michael@516: } michael@516: close COMMIT; michael@516: michael@516: push @cia_text, michael@516: " ", michael@516: $gitweb_url ? " " . xml_escape("$gitweb_url/?a=commit;h=$commit") . "" : "", michael@516: " ", michael@516: " ", michael@516: " " . $info{"author_date"} . "", michael@516: ""; michael@516: michael@516: mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text); michael@516: } michael@516: michael@516: # send a global commit notice when there are too many commits for individual mails michael@516: sub send_global_notice($$$) michael@516: { michael@516: my ($ref, $old_sha1, $new_sha1) = @_; michael@516: my @notice = (); michael@516: michael@516: push @revlist_options, "--pretty"; michael@516: open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list"; michael@516: while () michael@516: { michael@516: chomp; michael@516: s/^commit /URL: $gitweb_url\/?a=commit;h=/ if $gitweb_url; michael@516: push @notice, $_; michael@516: } michael@516: close LIST; michael@516: michael@516: mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice); michael@516: } michael@516: michael@516: # send all the notices michael@516: sub send_all_notices($$$) michael@516: { michael@516: my ($old_sha1, $new_sha1, $ref) = @_; michael@516: michael@516: $ref =~ s/^refs\/heads\///; michael@516: michael@516: return if (@include_list && !grep {$_ eq $ref} @include_list); michael@516: michael@516: if ($old_sha1 eq '0' x 40) # new ref michael@516: { michael@516: send_commit_notice( $ref, $new_sha1 ) if $commitlist_address; michael@516: return; michael@516: } michael@516: michael@516: my @commits = (); michael@516: michael@516: open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list"; michael@516: while () michael@516: { michael@516: chomp; michael@516: die "invalid commit $_" unless /^[0-9a-f]{40}$/; michael@516: unshift @commits, $_; michael@516: } michael@516: close LIST; michael@516: michael@516: if (@commits > $max_individual_notices) michael@516: { michael@516: send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address; michael@516: return; michael@516: } michael@516: michael@516: foreach my $commit (@commits) michael@516: { michael@516: send_commit_notice( $ref, $commit ) if $commitlist_address; michael@516: send_cia_notice( $ref, $commit ) if $cia_project_name; michael@516: } michael@516: } michael@516: michael@516: parse_options(); michael@516: michael@516: # append repository path to URL michael@516: $gitweb_url .= "/$repos_name.git" if $gitweb_url; michael@516: michael@516: if (@ARGV) michael@516: { michael@516: send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] ); michael@516: } michael@516: else # read them from stdin michael@516: { michael@516: while (<>) michael@516: { michael@516: chomp; michael@516: if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); } michael@516: } michael@516: } michael@516: michael@516: exit 0;