git-notify 12 KB

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