git-notify 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. # right-justify the left column of "left: right" elements, omit undefined elements
  86. sub format_table(@)
  87. {
  88. my @lines = @_;
  89. my @table;
  90. my $max = 0;
  91. foreach my $line (@lines)
  92. {
  93. next if not defined $line;
  94. my $pos = index($line, ":");
  95. $max = $pos if $pos > $max;
  96. }
  97. foreach my $line (@lines)
  98. {
  99. next if not defined $line;
  100. my ($left, $right) = split(/: */, $line, 2);
  101. push @table, (defined $left and defined $right)
  102. ? sprintf("%*s: %s", $max + 1, $left, $right)
  103. : $line;
  104. }
  105. return @table;
  106. }
  107. # format an integer date + timezone as string
  108. # algorithm taken from git's date.c
  109. sub format_date($$)
  110. {
  111. my ($time,$tz) = @_;
  112. if ($tz < 0)
  113. {
  114. my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
  115. $time -= $minutes * 60;
  116. }
  117. else
  118. {
  119. my $minutes = ($tz / 100) * 60 + ($tz % 100);
  120. $time += $minutes * 60;
  121. }
  122. return gmtime($time) . sprintf " %+05d", $tz;
  123. }
  124. # fetch a parameter from the git config file
  125. sub git_config($)
  126. {
  127. my ($param) = @_;
  128. open CONFIG, "-|" or exec "git", "config", $param;
  129. my $ret = <CONFIG>;
  130. chomp $ret if $ret;
  131. close CONFIG or $ret = undef;
  132. return $ret;
  133. }
  134. # parse command line options
  135. sub parse_options()
  136. {
  137. while (@ARGV && $ARGV[0] =~ /^-/)
  138. {
  139. my $arg = shift @ARGV;
  140. if ($arg eq '--') { last; }
  141. elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
  142. elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
  143. elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
  144. elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
  145. elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
  146. elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
  147. elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
  148. elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
  149. elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; }
  150. elsif ($arg eq '-d') { $debug++; }
  151. else { usage(); }
  152. }
  153. if (@ARGV && $#ARGV != 2) { usage(); }
  154. @exclude_list = map { "^$_"; } @exclude_list;
  155. }
  156. # send an email notification
  157. sub mail_notification($$$@)
  158. {
  159. my ($name, $subject, $content_type, @text) = @_;
  160. $subject = encode("MIME-Q",$subject);
  161. if ($debug)
  162. {
  163. print "---------------------\n";
  164. print "To: $name\n";
  165. print "Subject: $subject\n";
  166. print "Content-Type: $content_type\n";
  167. print "\n", join("\n", @text), "\n";
  168. }
  169. else
  170. {
  171. my $pid = open MAIL, "|-";
  172. return unless defined $pid;
  173. if (!$pid)
  174. {
  175. exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
  176. }
  177. print MAIL join("\n", @text), "\n";
  178. close MAIL;
  179. }
  180. }
  181. # get the default repository name
  182. sub get_repos_name()
  183. {
  184. my $dir = `git rev-parse --git-dir`;
  185. chomp $dir;
  186. my $repos = realpath($dir);
  187. $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/;
  188. $repos =~ s/(.*)\/([^\/]+)\/?$/$2/;
  189. return $repos;
  190. }
  191. # extract the information from a commit object and return a hash containing the various fields
  192. sub get_object_info($)
  193. {
  194. my $obj = shift;
  195. my %info = ();
  196. my @log = ();
  197. my $do_log = 0;
  198. open OBJ, "-|" or exec "git", "cat-file", "commit", $obj or die "cannot run git-cat-file";
  199. while (<OBJ>)
  200. {
  201. chomp;
  202. if ($do_log) { push @log, $_; }
  203. elsif (/^$/) { $do_log = 1; }
  204. elsif (/^(author|committer) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
  205. {
  206. $info{$1} = $2;
  207. $info{$1 . "_name"} = $3;
  208. $info{$1 . "_email"} = $4;
  209. $info{$1 . "_date"} = $5;
  210. $info{$1 . "_tz"} = $6;
  211. }
  212. }
  213. close OBJ;
  214. $info{"log"} = \@log;
  215. return %info;
  216. }
  217. # send a commit notice to a mailing list
  218. sub send_commit_notice($$)
  219. {
  220. my ($ref,$obj) = @_;
  221. my %info = get_object_info($obj);
  222. my @notice = ();
  223. open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  224. my $diff = join("", <DIFF>);
  225. close DIFF;
  226. return if length($diff) == 0;
  227. push @notice, format_table(
  228. "Module: $repos_name",
  229. "Branch: $ref",
  230. "Commit: $obj",
  231. $gitweb_url ? "URL: $gitweb_url/?a=commit;h=$obj" : undef),
  232. "Author:" . $info{"author"},
  233. "Date:" . format_date($info{"author_date"},$info{"author_tz"}),
  234. "",
  235. @{$info{"log"}},
  236. "",
  237. "---",
  238. "";
  239. open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  240. push @notice, join("", <STAT>);
  241. close STAT;
  242. if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
  243. {
  244. push @notice, $diff;
  245. }
  246. else
  247. {
  248. push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
  249. }
  250. mail_notification($commitlist_address,
  251. $info{"author_name"} . ": " . ${$info{"log"}}[0],
  252. "text/plain; charset=UTF-8", @notice);
  253. }
  254. # send a commit notice to the CIA server
  255. sub send_cia_notice($$)
  256. {
  257. my ($ref,$commit) = @_;
  258. my %info = get_object_info($commit);
  259. my @cia_text = ();
  260. push @cia_text,
  261. "<message>",
  262. " <generator>",
  263. " <name>git-notify script for CIA</name>",
  264. " </generator>",
  265. " <source>",
  266. " <project>" . xml_escape($cia_project_name) . "</project>",
  267. " <module>" . xml_escape($repos_name) . "</module>",
  268. " <branch>" . xml_escape($ref). "</branch>",
  269. " </source>",
  270. " <body>",
  271. " <commit>",
  272. " <revision>" . substr($commit,0,10) . "</revision>",
  273. " <author>" . xml_escape($info{"author"}) . "</author>",
  274. " <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
  275. " <files>";
  276. open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
  277. while (<COMMIT>)
  278. {
  279. chomp;
  280. if (/^([AMD])\t(.*)$/)
  281. {
  282. my ($action, $file) = ($1, $2);
  283. my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
  284. next unless defined $actions{$action};
  285. push @cia_text, " <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
  286. }
  287. elsif (/^R\d+\t(.*)\t(.*)$/)
  288. {
  289. my ($old, $new) = ($1, $2);
  290. push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
  291. }
  292. }
  293. close COMMIT;
  294. push @cia_text,
  295. " </files>",
  296. $gitweb_url ? " <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>" : "",
  297. " </commit>",
  298. " </body>",
  299. " <timestamp>" . $info{"author_date"} . "</timestamp>",
  300. "</message>";
  301. mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
  302. }
  303. # send a global commit notice when there are too many commits for individual mails
  304. sub send_global_notice($$$)
  305. {
  306. my ($ref, $old_sha1, $new_sha1) = @_;
  307. my @notice = ();
  308. push @revlist_options, "--pretty";
  309. open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
  310. while (<LIST>)
  311. {
  312. chomp;
  313. s/^commit /URL: $gitweb_url\/?a=commit;h=/ if $gitweb_url;
  314. push @notice, $_;
  315. }
  316. close LIST;
  317. mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
  318. }
  319. # send all the notices
  320. sub send_all_notices($$$)
  321. {
  322. my ($old_sha1, $new_sha1, $ref) = @_;
  323. $ref =~ s/^refs\/heads\///;
  324. return if (@include_list && !grep {$_ eq $ref} @include_list);
  325. if ($old_sha1 eq '0' x 40) # new ref
  326. {
  327. send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
  328. return;
  329. }
  330. my @commits = ();
  331. open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
  332. while (<LIST>)
  333. {
  334. chomp;
  335. die "invalid commit $_" unless /^[0-9a-f]{40}$/;
  336. unshift @commits, $_;
  337. }
  338. close LIST;
  339. if (@commits > $max_individual_notices)
  340. {
  341. send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
  342. return;
  343. }
  344. foreach my $commit (@commits)
  345. {
  346. send_commit_notice( $ref, $commit ) if $commitlist_address;
  347. send_cia_notice( $ref, $commit ) if $cia_project_name;
  348. }
  349. }
  350. parse_options();
  351. # append repository path to URL
  352. $gitweb_url .= "/$repos_name.git" if $gitweb_url;
  353. if (@ARGV)
  354. {
  355. send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
  356. }
  357. else # read them from stdin
  358. {
  359. while (<>)
  360. {
  361. chomp;
  362. if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }
  363. }
  364. }
  365. exit 0;