gitlog-to-changelog 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. eval '(exit $?0)' && eval 'exec perl -wS "$0" "$@"'
  2. & eval 'exec perl -wS "$0" $argv:q'
  3. if 0;
  4. # Convert git log output to ChangeLog format.
  5. my $VERSION = '2016-05-28 00:02'; # UTC
  6. # The definition above must lie within the first 8 lines in order
  7. # for the Emacs time-stamp write hook (at end) to update it.
  8. # If you change this file with Emacs, please let the write hook
  9. # do its job. Otherwise, update this string manually.
  10. # Copyright (C) 2008-2016 Free Software Foundation, Inc.
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. # Written by Jim Meyering
  22. use strict;
  23. use warnings;
  24. use Getopt::Long;
  25. use POSIX qw(strftime);
  26. (my $ME = $0) =~ s|.*/||;
  27. # use File::Coda; # http://meyering.net/code/Coda/
  28. END {
  29. defined fileno STDOUT or return;
  30. close STDOUT and return;
  31. warn "$ME: failed to close standard output: $!\n";
  32. $? ||= 1;
  33. }
  34. sub usage ($)
  35. {
  36. my ($exit_code) = @_;
  37. my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
  38. if ($exit_code != 0)
  39. {
  40. print $STREAM "Try '$ME --help' for more information.\n";
  41. }
  42. else
  43. {
  44. print $STREAM <<EOF;
  45. Usage: $ME [OPTIONS] [ARGS]
  46. Convert git log output to ChangeLog format. If present, any ARGS
  47. are passed to "git log". To avoid ARGS being parsed as options to
  48. $ME, they may be preceded by '--'.
  49. OPTIONS:
  50. --amend=FILE FILE maps from an SHA1 to perl code (i.e., s/old/new/) that
  51. makes a change to SHA1's commit log text or metadata.
  52. --append-dot append a dot to the first line of each commit message if
  53. there is no other punctuation or blank at the end.
  54. --no-cluster never cluster commit messages under the same date/author
  55. header; the default is to cluster adjacent commit messages
  56. if their headers are the same and neither commit message
  57. contains multiple paragraphs.
  58. --srcdir=DIR the root of the source tree, from which the .git/
  59. directory can be derived.
  60. --since=DATE convert only the logs since DATE;
  61. the default is to convert all log entries.
  62. --until=DATE convert only the logs older than DATE.
  63. --ignore-matching=PAT ignore commit messages whose first lines match PAT.
  64. --ignore-line=PAT ignore lines of commit messages that match PAT.
  65. --format=FMT set format string for commit subject and body;
  66. see 'man git-log' for the list of format metacharacters;
  67. the default is '%s%n%b%n'
  68. --strip-tab remove one additional leading TAB from commit message lines.
  69. --strip-cherry-pick remove data inserted by "git cherry-pick";
  70. this includes the "cherry picked from commit ..." line,
  71. and the possible final "Conflicts:" paragraph.
  72. --help display this help and exit
  73. --version output version information and exit
  74. EXAMPLE:
  75. $ME --since=2008-01-01 > ChangeLog
  76. $ME -- -n 5 foo > last-5-commits-to-branch-foo
  77. SPECIAL SYNTAX:
  78. The following types of strings are interpreted specially when they appear
  79. at the beginning of a log message line. They are not copied to the output.
  80. Copyright-paperwork-exempt: Yes
  81. Append the "(tiny change)" notation to the usual "date name email"
  82. ChangeLog header to mark a change that does not require a copyright
  83. assignment.
  84. Co-authored-by: Joe User <user\@example.com>
  85. List the specified name and email address on a second
  86. ChangeLog header, denoting a co-author.
  87. Signed-off-by: Joe User <user\@example.com>
  88. These lines are simply elided.
  89. In a FILE specified via --amend, comment lines (starting with "#") are ignored.
  90. FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
  91. a line) referring to a commit in the current project, and CODE refers to one
  92. or more consecutive lines of Perl code. Pairs must be separated by one or
  93. more blank line.
  94. Here is sample input for use with --amend=FILE, from coreutils:
  95. 3a169f4c5d9159283548178668d2fae6fced3030
  96. # fix typo in title:
  97. s/all tile types/all file types/
  98. 1379ed974f1fa39b12e2ffab18b3f7a607082202
  99. # Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
  100. # Change the author to be Paul. Note the escaped "@":
  101. s,Jim .*>,Paul Eggert <eggert\\\@cs.ucla.edu>,
  102. EOF
  103. }
  104. exit $exit_code;
  105. }
  106. # If the string $S is a well-behaved file name, simply return it.
  107. # If it contains white space, quotes, etc., quote it, and return the new string.
  108. sub shell_quote($)
  109. {
  110. my ($s) = @_;
  111. if ($s =~ m![^\w+/.,-]!)
  112. {
  113. # Convert each single quote to '\''
  114. $s =~ s/\'/\'\\\'\'/g;
  115. # Then single quote the string.
  116. $s = "'$s'";
  117. }
  118. return $s;
  119. }
  120. sub quoted_cmd(@)
  121. {
  122. return join (' ', map {shell_quote $_} @_);
  123. }
  124. # Parse file F.
  125. # Comment lines (starting with "#") are ignored.
  126. # F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
  127. # (alone on a line) referring to a commit in the current project, and
  128. # CODE refers to one or more consecutive lines of Perl code.
  129. # Pairs must be separated by one or more blank line.
  130. sub parse_amend_file($)
  131. {
  132. my ($f) = @_;
  133. open F, '<', $f
  134. or die "$ME: $f: failed to open for reading: $!\n";
  135. my $fail;
  136. my $h = {};
  137. my $in_code = 0;
  138. my $sha;
  139. while (defined (my $line = <F>))
  140. {
  141. $line =~ /^\#/
  142. and next;
  143. chomp $line;
  144. $line eq ''
  145. and $in_code = 0, next;
  146. if (!$in_code)
  147. {
  148. $line =~ /^([0-9a-fA-F]{40})$/
  149. or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
  150. $fail = 1, next;
  151. $sha = lc $1;
  152. $in_code = 1;
  153. exists $h->{$sha}
  154. and (warn "$ME: $f:$.: duplicate SHA1\n"),
  155. $fail = 1, next;
  156. }
  157. else
  158. {
  159. $h->{$sha} ||= '';
  160. $h->{$sha} .= "$line\n";
  161. }
  162. }
  163. close F;
  164. $fail
  165. and exit 1;
  166. return $h;
  167. }
  168. # git_dir_option $SRCDIR
  169. #
  170. # From $SRCDIR, the --git-dir option to pass to git (none if $SRCDIR
  171. # is undef). Return as a list (0 or 1 element).
  172. sub git_dir_option($)
  173. {
  174. my ($srcdir) = @_;
  175. my @res = ();
  176. if (defined $srcdir)
  177. {
  178. my $qdir = shell_quote $srcdir;
  179. my $cmd = "cd $qdir && git rev-parse --show-toplevel";
  180. my $qcmd = shell_quote $cmd;
  181. my $git_dir = qx($cmd);
  182. defined $git_dir
  183. or die "$ME: cannot run $qcmd: $!\n";
  184. $? == 0
  185. or die "$ME: $qcmd had unexpected exit code or signal ($?)\n";
  186. chomp $git_dir;
  187. push @res, "--git-dir=$git_dir/.git";
  188. }
  189. @res;
  190. }
  191. {
  192. my $since_date;
  193. my $until_date;
  194. my $format_string = '%s%n%b%n';
  195. my $amend_file;
  196. my $append_dot = 0;
  197. my $cluster = 1;
  198. my $ignore_matching;
  199. my $ignore_line;
  200. my $strip_tab = 0;
  201. my $strip_cherry_pick = 0;
  202. my $srcdir;
  203. GetOptions
  204. (
  205. help => sub { usage 0 },
  206. version => sub { print "$ME version $VERSION\n"; exit },
  207. 'since=s' => \$since_date,
  208. 'until=s' => \$until_date,
  209. 'format=s' => \$format_string,
  210. 'amend=s' => \$amend_file,
  211. 'append-dot' => \$append_dot,
  212. 'cluster!' => \$cluster,
  213. 'ignore-matching=s' => \$ignore_matching,
  214. 'ignore-line=s' => \$ignore_line,
  215. 'strip-tab' => \$strip_tab,
  216. 'strip-cherry-pick' => \$strip_cherry_pick,
  217. 'srcdir=s' => \$srcdir,
  218. ) or usage 1;
  219. defined $since_date
  220. and unshift @ARGV, "--since=$since_date";
  221. defined $until_date
  222. and unshift @ARGV, "--until=$until_date";
  223. # This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
  224. # that makes a correction in the log or attribution of that commit.
  225. my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
  226. my @cmd = ('git',
  227. git_dir_option $srcdir,
  228. qw(log --log-size),
  229. '--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
  230. open PIPE, '-|', @cmd
  231. or die ("$ME: failed to run '". quoted_cmd (@cmd) ."': $!\n"
  232. . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
  233. my $prev_multi_paragraph;
  234. my $prev_date_line = '';
  235. my @prev_coauthors = ();
  236. my @skipshas = ();
  237. while (1)
  238. {
  239. defined (my $in = <PIPE>)
  240. or last;
  241. $in =~ /^log size (\d+)$/
  242. or die "$ME:$.: Invalid line (expected log size):\n$in";
  243. my $log_nbytes = $1;
  244. my $log;
  245. my $n_read = read PIPE, $log, $log_nbytes;
  246. $n_read == $log_nbytes
  247. or die "$ME:$.: unexpected EOF\n";
  248. # Extract leading hash.
  249. my ($sha, $rest) = split ':', $log, 2;
  250. defined $sha
  251. or die "$ME:$.: malformed log entry\n";
  252. $sha =~ /^[0-9a-fA-F]{40}$/
  253. or die "$ME:$.: invalid SHA1: $sha\n";
  254. my $skipflag = 0;
  255. if (@skipshas)
  256. {
  257. foreach(@skipshas)
  258. {
  259. if ($sha =~ /^$_/)
  260. {
  261. $skipflag = $_;
  262. last;
  263. }
  264. }
  265. }
  266. # If this commit's log requires any transformation, do it now.
  267. my $code = $amend_code->{$sha};
  268. if (defined $code)
  269. {
  270. eval 'use Safe';
  271. my $s = new Safe;
  272. # Put the unpreprocessed entry into "$_".
  273. $_ = $rest;
  274. # Let $code operate on it, safely.
  275. my $r = $s->reval("$code")
  276. or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
  277. # Note that we've used this entry.
  278. delete $amend_code->{$sha};
  279. # Update $rest upon success.
  280. $rest = $_;
  281. }
  282. # Remove lines inserted by "git cherry-pick".
  283. if ($strip_cherry_pick)
  284. {
  285. $rest =~ s/^\s*Conflicts:\n.*//sm;
  286. $rest =~ s/^\s*\(cherry picked from commit [\da-f]+\)\n//m;
  287. }
  288. my @line = split /[ \t]*\n/, $rest;
  289. my $author_line = shift @line;
  290. defined $author_line
  291. or die "$ME:$.: unexpected EOF\n";
  292. $author_line =~ /^(\d+) (.*>)$/
  293. or die "$ME:$.: Invalid line "
  294. . "(expected date/author/email):\n$author_line\n";
  295. # Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
  296. # `(tiny change)' annotation.
  297. my $tiny = (grep (/^(?:Copyright-paperwork-exempt|Tiny-change):\s+[Yy]es$/, @line)
  298. ? ' (tiny change)' : '');
  299. my $date_line = sprintf "%s %s$tiny\n",
  300. strftime ("%Y-%m-%d", localtime ($1)), $2;
  301. my @coauthors = grep /^Co-authored-by:.*$/, @line;
  302. # Omit meta-data lines we've already interpreted.
  303. @line = grep !/^(?:Signed-off-by:[ ].*>$
  304. |Co-authored-by:[ ]
  305. |Copyright-paperwork-exempt:[ ]
  306. |Tiny-change:[ ]
  307. )/x, @line;
  308. # Remove leading and trailing blank lines.
  309. if (@line)
  310. {
  311. while ($line[0] =~ /^\s*$/) { shift @line; }
  312. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  313. }
  314. # Handle Emacs gitmerge.el "skipped" commits.
  315. # Yes, this should be controlled by an option. So sue me.
  316. if ( grep /^(; )?Merge from /, @line )
  317. {
  318. my $found = 0;
  319. foreach (@line)
  320. {
  321. if (grep /^The following commit.*skipped:$/, $_)
  322. {
  323. $found = 1;
  324. ## Reset at each merge to reduce chance of false matches.
  325. @skipshas = ();
  326. next;
  327. }
  328. if ($found && $_ =~ /^([0-9a-fA-F]{7,}) [^ ]/)
  329. {
  330. push ( @skipshas, $1 );
  331. }
  332. }
  333. }
  334. # Ignore commits that match the --ignore-matching pattern, if specified.
  335. if (defined $ignore_matching && @line && $line[0] =~ /$ignore_matching/)
  336. {
  337. $skipflag = 1;
  338. }
  339. elsif ($skipflag)
  340. {
  341. ## Perhaps only warn if a pattern matches more than once?
  342. warn "$ME: warning: skipping $sha due to $skipflag\n";
  343. }
  344. if (! $skipflag)
  345. {
  346. if (defined $ignore_line && @line)
  347. {
  348. @line = grep ! /$ignore_line/, @line;
  349. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  350. }
  351. # Record whether there are two or more paragraphs.
  352. my $multi_paragraph = grep /^\s*$/, @line;
  353. # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
  354. # standard multi-author ChangeLog format.
  355. for (@coauthors)
  356. {
  357. s/^Co-authored-by:\s*/\t /;
  358. s/\s*</ </;
  359. /<.*?@.*\..*>/
  360. or warn "$ME: warning: missing email address for "
  361. . substr ($_, 5) . "\n";
  362. }
  363. # If clustering of commit messages has been disabled, if this header
  364. # would be different from the previous date/name/etc. header,
  365. # or if this or the previous entry consists of two or more paragraphs,
  366. # then print the header.
  367. if ( ! $cluster
  368. || $date_line ne $prev_date_line
  369. || "@coauthors" ne "@prev_coauthors"
  370. || $multi_paragraph
  371. || $prev_multi_paragraph)
  372. {
  373. $prev_date_line eq ''
  374. or print "\n";
  375. print $date_line;
  376. @coauthors
  377. and print join ("\n", @coauthors), "\n";
  378. }
  379. $prev_date_line = $date_line;
  380. @prev_coauthors = @coauthors;
  381. $prev_multi_paragraph = $multi_paragraph;
  382. # If there were any lines
  383. if (@line == 0)
  384. {
  385. warn "$ME: warning: empty commit message:\n $date_line\n";
  386. }
  387. else
  388. {
  389. if ($append_dot)
  390. {
  391. # If the first line of the message has enough room, then
  392. if (length $line[0] < 72)
  393. {
  394. # append a dot if there is no other punctuation or blank
  395. # at the end.
  396. $line[0] =~ /[[:punct:]\s]$/
  397. or $line[0] .= '.';
  398. }
  399. }
  400. # Remove one additional leading TAB from each line.
  401. $strip_tab
  402. and map { s/^\t// } @line;
  403. # Prefix each non-empty line with a TAB.
  404. @line = map { length $_ ? "\t$_" : '' } @line;
  405. print "\n", join ("\n", @line), "\n";
  406. }
  407. }
  408. defined ($in = <PIPE>)
  409. or last;
  410. $in ne "\n"
  411. and die "$ME:$.: unexpected line:\n$in";
  412. }
  413. close PIPE
  414. or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
  415. # FIXME-someday: include $PROCESS_STATUS in the diagnostic
  416. # Complain about any unused entry in the --amend=F specified file.
  417. my $fail = 0;
  418. foreach my $sha (keys %$amend_code)
  419. {
  420. warn "$ME:$amend_file: unused entry: $sha\n";
  421. $fail = 1;
  422. }
  423. exit $fail;
  424. }
  425. # Local Variables:
  426. # mode: perl
  427. # indent-tabs-mode: nil
  428. # eval: (add-hook 'write-file-hooks 'time-stamp)
  429. # time-stamp-start: "my $VERSION = '"
  430. # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
  431. # time-stamp-time-zone: "UTC0"
  432. # time-stamp-end: "'; # UTC"
  433. # End: