git-notify 13 KB

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