git-notify 14 KB

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