4
0

git-notify 21 KB

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