git-notify 15 KB

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