git-notify 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 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 OBJ, "-|" or exec "git", "cat-file", "commit", $obj or die "cannot run git-cat-file";
  177. while (<OBJ>)
  178. {
  179. chomp;
  180. if ($do_log) { push @log, $_; }
  181. elsif (/^$/) { $do_log = 1; }
  182. elsif (/^(author|committer) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
  183. {
  184. $info{$1} = $2;
  185. $info{$1 . "_name"} = $3;
  186. $info{$1 . "_email"} = $4;
  187. $info{$1 . "_date"} = $5;
  188. $info{$1 . "_tz"} = $6;
  189. }
  190. }
  191. close OBJ;
  192. $info{"log"} = \@log;
  193. return %info;
  194. }
  195. # send a commit notice to a mailing list
  196. sub send_commit_notice($$)
  197. {
  198. my ($ref,$obj) = @_;
  199. my %info = get_object_info($obj);
  200. my @notice = ();
  201. open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  202. my $diff = join("", <DIFF>);
  203. close DIFF;
  204. return if length($diff) == 0;
  205. push @notice,
  206. "Module: $repos_name",
  207. "Branch: $ref",
  208. "Commit: $obj",
  209. $gitweb_url ? "URL: $gitweb_url/?a=commit;h=$obj\n" : "",
  210. "Author: " . $info{"author"},
  211. "Date: " . format_date($info{"author_date"},$info{"author_tz"}),
  212. "",
  213. join "\n", @{$info{"log"}},
  214. "",
  215. "---",
  216. "";
  217. open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  218. push @notice, join("", <STAT>);
  219. close STAT;
  220. if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
  221. {
  222. push @notice, $diff;
  223. }
  224. else
  225. {
  226. push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
  227. }
  228. mail_notification($commitlist_address,
  229. $info{"author_name"} . ": " . ${$info{"log"}}[0],
  230. "text/plain; charset=UTF-8", @notice);
  231. }
  232. # send a commit notice to the CIA server
  233. sub send_cia_notice($$)
  234. {
  235. my ($ref,$commit) = @_;
  236. my %info = get_object_info($commit);
  237. my @cia_text = ();
  238. push @cia_text,
  239. "<message>",
  240. " <generator>",
  241. " <name>git-notify script for CIA</name>",
  242. " </generator>",
  243. " <source>",
  244. " <project>" . xml_escape($cia_project_name) . "</project>",
  245. " <module>" . xml_escape($repos_name) . "</module>",
  246. " <branch>" . xml_escape($ref). "</branch>",
  247. " </source>",
  248. " <body>",
  249. " <commit>",
  250. " <revision>" . substr($commit,0,10) . "</revision>",
  251. " <author>" . xml_escape($info{"author"}) . "</author>",
  252. " <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
  253. " <files>";
  254. open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
  255. while (<COMMIT>)
  256. {
  257. chomp;
  258. if (/^([AMD])\t(.*)$/)
  259. {
  260. my ($action, $file) = ($1, $2);
  261. my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
  262. next unless defined $actions{$action};
  263. push @cia_text, " <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
  264. }
  265. elsif (/^R\d+\t(.*)\t(.*)$/)
  266. {
  267. my ($old, $new) = ($1, $2);
  268. push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
  269. }
  270. }
  271. close COMMIT;
  272. push @cia_text,
  273. " </files>",
  274. $gitweb_url ? " <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>" : "",
  275. " </commit>",
  276. " </body>",
  277. " <timestamp>" . $info{"author_date"} . "</timestamp>",
  278. "</message>";
  279. mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
  280. }
  281. # send a global commit notice when there are too many commits for individual mails
  282. sub send_global_notice($$$)
  283. {
  284. my ($ref, $old_sha1, $new_sha1) = @_;
  285. my @notice = ();
  286. push @revlist_options, "--pretty";
  287. open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
  288. while (<LIST>)
  289. {
  290. chomp;
  291. s/^commit /URL: $gitweb_url\/?a=commit;h=/ if $gitweb_url;
  292. push @notice, $_;
  293. }
  294. close LIST;
  295. mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
  296. }
  297. # send all the notices
  298. sub send_all_notices($$$)
  299. {
  300. my ($old_sha1, $new_sha1, $ref) = @_;
  301. $ref =~ s/^refs\/heads\///;
  302. return if (@include_list && !grep {$_ eq $ref} @include_list);
  303. if ($old_sha1 eq '0' x 40) # new ref
  304. {
  305. send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
  306. return;
  307. }
  308. my @commits = ();
  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. die "invalid commit $_" unless /^[0-9a-f]{40}$/;
  314. unshift @commits, $_;
  315. }
  316. close LIST;
  317. if (@commits > $max_individual_notices)
  318. {
  319. send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
  320. return;
  321. }
  322. foreach my $commit (@commits)
  323. {
  324. send_commit_notice( $ref, $commit ) if $commitlist_address;
  325. send_cia_notice( $ref, $commit ) if $cia_project_name;
  326. }
  327. }
  328. parse_options();
  329. # append repository path to URL
  330. $gitweb_url .= "/$repos_name.git" if $gitweb_url;
  331. if (@ARGV)
  332. {
  333. send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
  334. }
  335. else # read them from stdin
  336. {
  337. while (<>)
  338. {
  339. chomp;
  340. if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }
  341. }
  342. }
  343. exit 0;