git-notify 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. #!/usr/bin/perl -w
  2. #
  3. # Tool to send git commit notifications
  4. #
  5. # Copyright 2005 Alexandre Julliard
  6. # Copyright 2009 Nagios Plugins Development Team
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation; either version 2 of
  11. # the License, or (at your option) any later version.
  12. #
  13. #
  14. # This script is meant to be called from .git/hooks/post-receive.
  15. #
  16. # Usage: git-notify [options] [--] old-sha1 new-sha1 refname
  17. #
  18. # -A Omit the author name from the mail subject
  19. # -C Show committer in the body if different from the author
  20. # -c name Send CIA notifications under specified project name
  21. # -H The mail(1) utility doesn't accept headers via "-a"
  22. # -m addr Send mail notifications to specified address
  23. # -n max Set max number of individual mails to send
  24. # -r name Set the git repository name
  25. # -S Enable compatibility with SourceForge's gitweb URLs
  26. # -s bytes Set the maximum diff size in bytes (-1 for no limit)
  27. # -T Prefix the mail subject with a [repository name] tag
  28. # -t file Prevent duplicate notifications by saving state to this file
  29. # -U mask Set the umask for creating the state file
  30. # -u url Set the URL to the gitweb browser
  31. # -i branch If at least one -i is given, report only for specified branches
  32. # -x branch Exclude changes to the specified branch from reports
  33. # -X Exclude merge commits
  34. # -z Try to abbreviate the SHA1 name within gitweb URLs (unsafe)
  35. #
  36. use strict;
  37. use Fcntl ':flock';
  38. use Encode qw(encode decode);
  39. use Cwd 'realpath';
  40. sub git_config($);
  41. sub get_repos_name();
  42. # some parameters you may want to change
  43. # set this to something that takes "-s"
  44. my $mailer = "/usr/bin/mail";
  45. # CIA notification address
  46. my $cia_address = "cia\@cia.navi.cx";
  47. # debug mode
  48. my $debug = 0;
  49. # configuration parameters
  50. # the $mailer doesn't accept headers via "-a" (can be set with the -H option)
  51. my $legacy_mail = git_config( "notify.legacymail" );
  52. # omit the author from the mail subject (can be set with the -A option)
  53. my $omit_author = git_config( "notify.omitauthor" );
  54. # prefix the mail subject with a [repository name] tag (can be set with the -T option)
  55. my $emit_repo = git_config( "notify.emitrepository" );
  56. # show the committer if different from the author (can be set with the -C option)
  57. my $show_committer = git_config( "notify.showcommitter" );
  58. # base URL of the gitweb repository browser (can be set with the -u option)
  59. my $gitweb_url = git_config( "notify.baseurl" );
  60. # abbreviate the SHA1 name within gitweb URLs (can be set with the -z option)
  61. my $abbreviate_url = git_config( "notify.shorturls" );
  62. # enable compatibility with SourceForge's gitweb (can be set with the -S option)
  63. my $sourceforge = git_config( "notify.sourceforge" );
  64. # default repository name (can be changed with the -r option)
  65. my $repos_name = git_config( "notify.repository" ) || get_repos_name();
  66. # max size of diffs in bytes (can be changed with the -s option)
  67. my $max_diff_size = git_config( "notify.maxdiff" ) || 10000;
  68. # address for mail notices (can be set with -m option)
  69. my $commitlist_address = git_config( "notify.mail" );
  70. # project name for CIA notices (can be set with -c option)
  71. my $cia_project_name = git_config( "notify.cia" );
  72. # max number of individual notices before falling back to a single global notice (can be set with -n option)
  73. my $max_individual_notices = git_config( "notify.maxnotices" ) || 100;
  74. # branches to include
  75. my @include_list = split /\s+/, git_config( "notify.include" ) || "";
  76. # branches to exclude
  77. my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || "";
  78. # the state file we use (can be set with the -t option)
  79. my $state_file = git_config( "notify.statefile" );
  80. # umask for creating the state file (can be set with -U option)
  81. my $mode_mask = git_config( "notify.umask" ) || 002;
  82. # Extra options to git rev-list
  83. my @revlist_options;
  84. sub usage()
  85. {
  86. print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n";
  87. print " -A Omit the author name from the mail subject\n";
  88. print " -C Show committer in the body if different from the author\n";
  89. print " -c name Send CIA notifications under specified project name\n";
  90. print " -H The mail(1) utility doesn't accept headers via `-a'\n";
  91. print " -m addr Send mail notifications to specified address\n";
  92. print " -n max Set max number of individual mails to send\n";
  93. print " -r name Set the git repository name\n";
  94. print " -S Enable compatibility with SourceForge's gitweb URLs\n";
  95. print " -s bytes Set the maximum diff size in bytes (-1 for no limit)\n";
  96. print " -T Prefix the mail subject with a [repository name] tag\n";
  97. print " -t file Prevent duplicate notifications by saving state to this file\n";
  98. print " -U mask Set the umask for creating the state file\n";
  99. print " -u url Set the URL to the gitweb browser\n";
  100. print " -i branch If at least one -i is given, report only for specified branches\n";
  101. print " -x branch Exclude changes to the specified branch from reports\n";
  102. print " -X Exclude merge commits\n";
  103. print " -z Try to abbreviate the SHA1 name within gitweb URLs (unsafe)\n";
  104. exit 1;
  105. }
  106. sub xml_escape($)
  107. {
  108. my $str = shift;
  109. $str =~ s/&/&/g;
  110. $str =~ s/</&lt;/g;
  111. $str =~ s/>/&gt;/g;
  112. my @chars = unpack "U*", $str;
  113. $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars;
  114. return $str;
  115. }
  116. # execute git-rev-list(1) with the given parameters and return the output
  117. sub git_rev_list(@)
  118. {
  119. my @args = @_;
  120. my $revlist = [];
  121. my $pid = open REVLIST, "-|";
  122. die "Cannot open pipe: $!" if not defined $pid;
  123. if (!$pid)
  124. {
  125. exec "git", "rev-list", @revlist_options, @args or die "Cannot execute rev-list: $!";
  126. }
  127. while (<REVLIST>)
  128. {
  129. chomp;
  130. unless (grep {$_ eq "--pretty"} @args)
  131. {
  132. die "Invalid commit: $_" if not /^[0-9a-f]{40}$/;
  133. }
  134. push @$revlist, $_;
  135. }
  136. close REVLIST or die $! ? "Cannot execute rev-list: $!" : "rev-list exited with status: $?";
  137. return $revlist;
  138. }
  139. # append the given commit hashes to the state file
  140. sub save_commits($)
  141. {
  142. my $commits = shift;
  143. open STATE, ">>", $state_file or die "Cannot open $state_file: $!";
  144. flock STATE, LOCK_EX or die "Cannot lock $state_file";
  145. print STATE "$_\n" for @$commits;
  146. flock STATE, LOCK_UN or die "Cannot unlock $state_file";
  147. close STATE or die "Cannot close $state_file: $!";
  148. }
  149. # for the given range, return the new hashes (and append them to the state file)
  150. sub get_new_commits($$)
  151. {
  152. my ($old_sha1, $new_sha1) = @_;
  153. my ($seen, @args);
  154. my $newrevs = [];
  155. @args = ( "^$old_sha1" ) unless $old_sha1 eq '0' x 40;
  156. push @args, $new_sha1, @exclude_list;
  157. my $revlist = git_rev_list(@args);
  158. if (not defined $state_file or not -e $state_file)
  159. {
  160. save_commits(git_rev_list("--all", "--full-history")) if defined $state_file;
  161. return $revlist;
  162. }
  163. open STATE, $state_file or die "Cannot open $state_file: $!";
  164. flock STATE, LOCK_SH or die "Cannot lock $state_file";
  165. while (<STATE>)
  166. {
  167. chomp;
  168. die "Invalid commit: $_" if not /^[0-9a-f]{40}$/;
  169. $seen->{$_} = 1;
  170. }
  171. flock STATE, LOCK_UN or die "Cannot unlock $state_file";
  172. close STATE or die "Cannot close $state_file: $!";
  173. # FIXME: if another git-notify process reads the $state_file at *this*
  174. # point, that process might generate duplicates of our notifications.
  175. save_commits($revlist);
  176. foreach my $commit (@$revlist)
  177. {
  178. push @$newrevs, $commit unless $seen->{$commit};
  179. }
  180. return $newrevs;
  181. }
  182. # truncate the given string if it exceeds the specified number of characters
  183. sub truncate_str($$)
  184. {
  185. my ($str, $max) = @_;
  186. if (length($str) > $max)
  187. {
  188. $str = substr($str, 0, $max);
  189. $str =~ s/\s+\S+$//;
  190. $str .= " ...";
  191. }
  192. return $str;
  193. }
  194. # right-justify the left column of "left: right" elements, omit undefined elements
  195. sub format_table(@)
  196. {
  197. my @lines = @_;
  198. my @table;
  199. my $max = 0;
  200. foreach my $line (@lines)
  201. {
  202. next if not defined $line;
  203. my $pos = index($line, ":");
  204. $max = $pos if $pos > $max;
  205. }
  206. foreach my $line (@lines)
  207. {
  208. next if not defined $line;
  209. my ($left, $right) = split(/: */, $line, 2);
  210. push @table, (defined $left and defined $right)
  211. ? sprintf("%*s: %s", $max + 1, $left, $right)
  212. : $line;
  213. }
  214. return @table;
  215. }
  216. # format an integer date + timezone as string
  217. # algorithm taken from git's date.c
  218. sub format_date($$)
  219. {
  220. my ($time,$tz) = @_;
  221. if ($tz < 0)
  222. {
  223. my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
  224. $time -= $minutes * 60;
  225. }
  226. else
  227. {
  228. my $minutes = ($tz / 100) * 60 + ($tz % 100);
  229. $time += $minutes * 60;
  230. }
  231. return gmtime($time) . sprintf " %+05d", $tz;
  232. }
  233. # fetch a parameter from the git config file
  234. sub git_config($)
  235. {
  236. my ($param) = @_;
  237. open CONFIG, "-|" or exec "git", "config", $param;
  238. my $ret = <CONFIG>;
  239. chomp $ret if $ret;
  240. close CONFIG or $ret = undef;
  241. return $ret;
  242. }
  243. # parse command line options
  244. sub parse_options()
  245. {
  246. while (@ARGV && $ARGV[0] =~ /^-/)
  247. {
  248. my $arg = shift @ARGV;
  249. if ($arg eq '--') { last; }
  250. elsif ($arg eq '-A') { $omit_author = 1; }
  251. elsif ($arg eq '-C') { $show_committer = 1; }
  252. elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
  253. elsif ($arg eq '-H') { $legacy_mail = 1; }
  254. elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
  255. elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
  256. elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
  257. elsif ($arg eq '-S') { $sourceforge = 1; }
  258. elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
  259. elsif ($arg eq '-T') { $emit_repo = 1; }
  260. elsif ($arg eq '-t') { $state_file = shift @ARGV; }
  261. elsif ($arg eq '-U') { $mode_mask = shift @ARGV; }
  262. elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
  263. elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
  264. elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
  265. elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; }
  266. elsif ($arg eq '-z') { $abbreviate_url = 1; }
  267. elsif ($arg eq '-d') { $debug++; }
  268. else { usage(); }
  269. }
  270. if (@ARGV && $#ARGV != 2) { usage(); }
  271. @exclude_list = map { "^$_"; } @exclude_list;
  272. }
  273. # send an email notification
  274. sub mail_notification($$$@)
  275. {
  276. my ($name, $subject, $content_type, @text) = @_;
  277. $subject = "[$repos_name] $subject" if $emit_repo;
  278. $subject = encode("MIME-Q",$subject);
  279. if ($debug)
  280. {
  281. binmode STDOUT, ":utf8";
  282. print "---------------------\n";
  283. print "To: $name\n";
  284. print "Subject: $subject\n";
  285. print "Content-Type: $content_type\n";
  286. print "\n", join("\n", @text), "\n";
  287. }
  288. else
  289. {
  290. my $pid = open MAIL, "|-";
  291. return unless defined $pid;
  292. if (!$pid)
  293. {
  294. my @mailer_options = ( "-s", $subject );
  295. unless ($legacy_mail)
  296. {
  297. push @mailer_options, "-a", "Content-Type: $content_type";
  298. }
  299. exec $mailer, @mailer_options, $name or die "Cannot exec $mailer";
  300. }
  301. binmode MAIL, ":utf8";
  302. print MAIL join("\n", @text), "\n";
  303. close MAIL or warn $! ? "Cannot execute $mailer: $!" : "$mailer exited with status: $?";
  304. }
  305. }
  306. # get the default repository name
  307. sub get_repos_name()
  308. {
  309. my $dir = `git rev-parse --git-dir`;
  310. chomp $dir;
  311. my $repos = realpath($dir);
  312. $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/;
  313. $repos =~ s/(.*)\/([^\/]+)\/?$/$2/;
  314. return $repos;
  315. }
  316. # return the type of the given object
  317. sub get_object_type($)
  318. {
  319. my $obj = shift;
  320. open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
  321. my $type = <TYPE>;
  322. chomp $type;
  323. close TYPE or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
  324. return $type;
  325. }
  326. # extract the information from a commit or tag object and return a hash containing the various fields
  327. sub get_object_info($)
  328. {
  329. my $obj = shift;
  330. my %info = ();
  331. my @log = ();
  332. my $do_log = 0;
  333. $info{"encoding"} = "utf-8";
  334. my $type = get_object_type($obj);
  335. open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
  336. while (<OBJ>)
  337. {
  338. chomp;
  339. if ($do_log)
  340. {
  341. last if /^-----BEGIN PGP SIGNATURE-----/;
  342. push @log, $_;
  343. }
  344. elsif (/^(author|committer|tagger) ((.*) (<.*>)) (\d+) ([+-]\d+)$/)
  345. {
  346. $info{$1} = $2;
  347. $info{$1 . "_name"} = $3;
  348. $info{$1 . "_email"} = $4;
  349. $info{$1 . "_date"} = $5;
  350. $info{$1 . "_tz"} = $6;
  351. }
  352. elsif (/^tag (.+)/)
  353. {
  354. $info{"tag"} = $1;
  355. }
  356. elsif (/^encoding (.+)/)
  357. {
  358. $info{"encoding"} = $1;
  359. }
  360. elsif (/^$/) { $do_log = 1; }
  361. }
  362. close OBJ or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
  363. $info{"type"} = $type;
  364. $info{"log"} = \@log;
  365. return %info;
  366. }
  367. # send a ref change notice to a mailing list
  368. sub send_ref_notice($$@)
  369. {
  370. my ($ref, $action, @notice) = @_;
  371. my ($reftype, $refname) = ($ref =~ /^refs\/(head|tag)s\/(.+)/);
  372. $reftype =~ s/^head$/branch/;
  373. @notice = (format_table(
  374. "Module: $repos_name",
  375. ($reftype eq "tag" ? "Tag:" : "Branch:") . $refname,
  376. @notice,
  377. ($action ne "removed" and $gitweb_url)
  378. ? "URL: ${gitweb_url}a=shortlog;h=$ref" : undef),
  379. "",
  380. "The $refname $reftype has been $action.");
  381. mail_notification($commitlist_address, "$refname $reftype $action",
  382. "text/plain; charset=us-ascii", @notice);
  383. }
  384. # send a commit notice to a mailing list
  385. sub send_commit_notice($$)
  386. {
  387. my ($ref,$obj) = @_;
  388. my %info = get_object_info($obj);
  389. my @notice = ();
  390. my ($url,$subject,$obj_string);
  391. if ($gitweb_url)
  392. {
  393. if ($abbreviate_url)
  394. {
  395. open REVPARSE, "-|" or exec "git", "rev-parse", "--short", $obj or die "cannot exec git-rev-parse";
  396. $obj_string = <REVPARSE>;
  397. chomp $obj_string if defined $obj_string;
  398. close REVPARSE or die $! ? "Cannot execute rev-parse: $!" : "rev-parse exited with status: $?";
  399. }
  400. $obj_string = $obj if not defined $obj_string;
  401. $url = "${gitweb_url}a=$info{type};h=$obj_string";
  402. }
  403. if ($info{"type"} eq "tag")
  404. {
  405. push @notice, format_table(
  406. "Module: $repos_name",
  407. "Tag: $ref",
  408. "SHA1: $obj",
  409. "Tagger:" . $info{"tagger"},
  410. "Date:" . format_date($info{"tagger_date"},$info{"tagger_tz"}),
  411. $url ? "URL: $url" : undef),
  412. "",
  413. join "\n", @{$info{"log"}};
  414. $subject = "Tag " . $info{"tag"} . ": ";
  415. $subject .= $info{"tagger_name"} . ": " unless $omit_author;
  416. }
  417. else
  418. {
  419. push @notice, format_table(
  420. "Module: $repos_name",
  421. "Branch: $ref",
  422. "Commit: $obj",
  423. "Author:" . $info{"author"},
  424. $show_committer && $info{"committer"} ne $info{"author"} ? "Committer:" . $info{"committer"} : undef,
  425. "Date:" . format_date($info{"author_date"},$info{"author_tz"}),
  426. $url ? "URL: $url" : undef),
  427. "",
  428. @{$info{"log"}},
  429. "",
  430. "---",
  431. "";
  432. open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  433. push @notice, join("", <STAT>);
  434. close STAT or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
  435. open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
  436. my $diff = join("", <DIFF>);
  437. close DIFF or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
  438. if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
  439. {
  440. push @notice, $diff;
  441. }
  442. else
  443. {
  444. push @notice, "Diff: ${gitweb_url}a=commitdiff;h=$obj_string" if $gitweb_url;
  445. }
  446. $subject = $info{"author_name"} . ": " unless $omit_author;
  447. }
  448. $subject .= truncate_str(${$info{"log"}}[0],50);
  449. $_ = decode($info{"encoding"}, $_) for @notice;
  450. mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
  451. }
  452. # send a commit notice to the CIA server
  453. sub send_cia_notice($$)
  454. {
  455. my ($ref,$commit) = @_;
  456. my %info = get_object_info($commit);
  457. my @cia_text = ();
  458. return if $info{"type"} ne "commit";
  459. push @cia_text,
  460. "<message>",
  461. " <generator>",
  462. " <name>git-notify script for CIA</name>",
  463. " </generator>",
  464. " <source>",
  465. " <project>" . xml_escape($cia_project_name) . "</project>",
  466. " <module>" . xml_escape($repos_name) . "</module>",
  467. " <branch>" . xml_escape($ref). "</branch>",
  468. " </source>",
  469. " <body>",
  470. " <commit>",
  471. " <revision>" . substr($commit,0,10) . "</revision>",
  472. " <author>" . xml_escape($info{"author"}) . "</author>",
  473. " <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
  474. " <files>";
  475. open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
  476. while (<COMMIT>)
  477. {
  478. chomp;
  479. if (/^([AMD])\t(.*)$/)
  480. {
  481. my ($action, $file) = ($1, $2);
  482. my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
  483. next unless defined $actions{$action};
  484. push @cia_text, " <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
  485. }
  486. elsif (/^R\d+\t(.*)\t(.*)$/)
  487. {
  488. my ($old, $new) = ($1, $2);
  489. push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
  490. }
  491. }
  492. close COMMIT or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
  493. push @cia_text,
  494. " </files>",
  495. $gitweb_url ? " <url>" . xml_escape("${gitweb_url}a=commit;h=$commit") . "</url>" : "",
  496. " </commit>",
  497. " </body>",
  498. " <timestamp>" . $info{"author_date"} . "</timestamp>",
  499. "</message>";
  500. mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
  501. }
  502. # send a global commit notice when there are too many commits for individual mails
  503. sub send_global_notice($$$)
  504. {
  505. my ($ref, $old_sha1, $new_sha1) = @_;
  506. my $notice = git_rev_list("--pretty", "^$old_sha1", "$new_sha1", @exclude_list);
  507. foreach my $rev (@$notice)
  508. {
  509. $rev =~ s/^commit /URL: ${gitweb_url}a=commit;h=/ if $gitweb_url;
  510. }
  511. mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @$notice);
  512. }
  513. # send all the notices
  514. sub send_all_notices($$$)
  515. {
  516. my ($old_sha1, $new_sha1, $ref) = @_;
  517. my ($reftype, $refname, $tagtype, $action, @notice);
  518. return if ($ref =~ /^refs\/remotes\//
  519. or (@include_list && !grep {$_ eq $ref} @include_list));
  520. die "The name \"$ref\" doesn't sound like a local branch or tag"
  521. if not (($reftype, $refname) = ($ref =~ /^refs\/(head|tag)s\/(.+)/));
  522. if ($reftype eq "tag")
  523. {
  524. $tagtype = get_object_type($ref) eq "tag" ? "annotated" : "lightweight";
  525. }
  526. if ($new_sha1 eq '0' x 40)
  527. {
  528. $action = "removed";
  529. @notice = ( "Old SHA1: $old_sha1" );
  530. }
  531. elsif ($old_sha1 eq '0' x 40)
  532. {
  533. if ($reftype eq "tag" and $tagtype eq "annotated")
  534. {
  535. send_commit_notice( $refname, $new_sha1 ) if $commitlist_address;
  536. return;
  537. }
  538. $action = "created";
  539. @notice = ( "SHA1: $new_sha1" );
  540. }
  541. elsif ($reftype eq "tag")
  542. {
  543. $action = "updated";
  544. @notice = ( "Old SHA1: $old_sha1", "New SHA1: $new_sha1" );
  545. }
  546. elsif (not grep( $_ eq $old_sha1, @{ git_rev_list( $new_sha1, "--full-history" ) } ))
  547. {
  548. $action = "rewritten";
  549. @notice = ( "Old SHA1: $old_sha1", "New SHA1: $new_sha1" );
  550. }
  551. send_ref_notice( $ref, $action, @notice ) if ($commitlist_address and $action);
  552. unless ($reftype eq "tag" or $new_sha1 eq '0' x 40)
  553. {
  554. my $commits = get_new_commits ( $old_sha1, $new_sha1 );
  555. if (@$commits > $max_individual_notices)
  556. {
  557. send_global_notice( $refname, $old_sha1, $new_sha1 ) if $commitlist_address;
  558. }
  559. elsif (@$commits > 0)
  560. {
  561. foreach my $commit (@$commits)
  562. {
  563. send_commit_notice( $refname, $commit ) if $commitlist_address;
  564. send_cia_notice( $refname, $commit ) if $cia_project_name;
  565. }
  566. }
  567. elsif ($commitlist_address)
  568. {
  569. @notice = ( "Old SHA1: $old_sha1", "New SHA1: $new_sha1" );
  570. send_ref_notice( $ref, "modified", @notice );
  571. }
  572. }
  573. }
  574. parse_options();
  575. umask( $mode_mask );
  576. # append repository path to URL
  577. if ($gitweb_url) {
  578. $gitweb_url .= $sourceforge ? "/$repos_name;" : "/$repos_name.git/?";
  579. }
  580. if (@ARGV)
  581. {
  582. send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
  583. }
  584. else # read them from stdin
  585. {
  586. while (<>)
  587. {
  588. chomp;
  589. if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }
  590. }
  591. }
  592. exit 0;