gitlog-to-changelog 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
  2. & eval 'exec perl -wS "$0" $argv:q'
  3. if 0;
  4. # Convert git log output to ChangeLog format.
  5. my $VERSION = '2015-07-21 22:45'; # 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-2015 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 = 1;
  262. ## Perhaps only warn if a pattern matches more than once?
  263. warn "$ME: warning: skipping $sha due to $_\n";
  264. last;
  265. }
  266. }
  267. }
  268. # If this commit's log requires any transformation, do it now.
  269. my $code = $amend_code->{$sha};
  270. if (defined $code)
  271. {
  272. eval 'use Safe';
  273. my $s = new Safe;
  274. # Put the unpreprocessed entry into "$_".
  275. $_ = $rest;
  276. # Let $code operate on it, safely.
  277. my $r = $s->reval("$code")
  278. or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
  279. # Note that we've used this entry.
  280. delete $amend_code->{$sha};
  281. # Update $rest upon success.
  282. $rest = $_;
  283. }
  284. # Remove lines inserted by "git cherry-pick".
  285. if ($strip_cherry_pick)
  286. {
  287. $rest =~ s/^\s*Conflicts:\n.*//sm;
  288. $rest =~ s/^\s*\(cherry picked from commit [\da-f]+\)\n//m;
  289. }
  290. my @line = split /\s*\n/, $rest;
  291. my $author_line = shift @line;
  292. defined $author_line
  293. or die "$ME:$.: unexpected EOF\n";
  294. $author_line =~ /^(\d+) (.*>)$/
  295. or die "$ME:$.: Invalid line "
  296. . "(expected date/author/email):\n$author_line\n";
  297. # Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
  298. # `(tiny change)' annotation.
  299. my $tiny = (grep (/^(?:Copyright-paperwork-exempt|Tiny-change):\s+[Yy]es$/, @line)
  300. ? ' (tiny change)' : '');
  301. my $date_line = sprintf "%s %s$tiny\n",
  302. strftime ("%Y-%m-%d", localtime ($1)), $2;
  303. my @coauthors = grep /^Co-authored-by:.*$/, @line;
  304. # Omit meta-data lines we've already interpreted.
  305. @line = grep !/^(?:Signed-off-by:[ ].*>$
  306. |Co-authored-by:[ ]
  307. |Copyright-paperwork-exempt:[ ]
  308. |Tiny-change:[ ]
  309. )/x, @line;
  310. # Remove leading and trailing blank lines.
  311. if (@line)
  312. {
  313. while ($line[0] =~ /^\s*$/) { shift @line; }
  314. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  315. }
  316. # Handle Emacs gitmerge.el "skipped" commits.
  317. # Yes, this should be controlled by an option. So sue me.
  318. if ( grep /^(; )?Merge from /, @line )
  319. {
  320. my $found = 0;
  321. foreach (@line)
  322. {
  323. if (grep /^The following commit.*skipped:$/, $_)
  324. {
  325. $found = 1;
  326. ## Reset at each merge to reduce chance of false matches.
  327. @skipshas = ();
  328. next;
  329. }
  330. if ($found && $_ =~ /^([0-9a-fA-F]{7,}) [^ ]/)
  331. {
  332. push ( @skipshas, $1 );
  333. }
  334. }
  335. }
  336. # Ignore commits that match the --ignore-matching pattern, if specified.
  337. if (! ($skipflag || (defined $ignore_matching
  338. && @line && $line[0] =~ /$ignore_matching/)))
  339. {
  340. if (defined $ignore_line && @line)
  341. {
  342. @line = grep ! /$ignore_line/, @line;
  343. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  344. }
  345. # Record whether there are two or more paragraphs.
  346. my $multi_paragraph = grep /^\s*$/, @line;
  347. # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
  348. # standard multi-author ChangeLog format.
  349. for (@coauthors)
  350. {
  351. s/^Co-authored-by:\s*/\t /;
  352. s/\s*</ </;
  353. /<.*?@.*\..*>/
  354. or warn "$ME: warning: missing email address for "
  355. . substr ($_, 5) . "\n";
  356. }
  357. # If clustering of commit messages has been disabled, if this header
  358. # would be different from the previous date/name/etc. header,
  359. # or if this or the previous entry consists of two or more paragraphs,
  360. # then print the header.
  361. if ( ! $cluster
  362. || $date_line ne $prev_date_line
  363. || "@coauthors" ne "@prev_coauthors"
  364. || $multi_paragraph
  365. || $prev_multi_paragraph)
  366. {
  367. $prev_date_line eq ''
  368. or print "\n";
  369. print $date_line;
  370. @coauthors
  371. and print join ("\n", @coauthors), "\n";
  372. }
  373. $prev_date_line = $date_line;
  374. @prev_coauthors = @coauthors;
  375. $prev_multi_paragraph = $multi_paragraph;
  376. # If there were any lines
  377. if (@line == 0)
  378. {
  379. warn "$ME: warning: empty commit message:\n $date_line\n";
  380. }
  381. else
  382. {
  383. if ($append_dot)
  384. {
  385. # If the first line of the message has enough room, then
  386. if (length $line[0] < 72)
  387. {
  388. # append a dot if there is no other punctuation or blank
  389. # at the end.
  390. $line[0] =~ /[[:punct:]\s]$/
  391. or $line[0] .= '.';
  392. }
  393. }
  394. # Remove one additional leading TAB from each line.
  395. $strip_tab
  396. and map { s/^\t// } @line;
  397. # Prefix each non-empty line with a TAB.
  398. @line = map { length $_ ? "\t$_" : '' } @line;
  399. print "\n", join ("\n", @line), "\n";
  400. }
  401. }
  402. defined ($in = <PIPE>)
  403. or last;
  404. $in ne "\n"
  405. and die "$ME:$.: unexpected line:\n$in";
  406. }
  407. close PIPE
  408. or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
  409. # FIXME-someday: include $PROCESS_STATUS in the diagnostic
  410. # Complain about any unused entry in the --amend=F specified file.
  411. my $fail = 0;
  412. foreach my $sha (keys %$amend_code)
  413. {
  414. warn "$ME:$amend_file: unused entry: $sha\n";
  415. $fail = 1;
  416. }
  417. exit $fail;
  418. }
  419. # Local Variables:
  420. # mode: perl
  421. # indent-tabs-mode: nil
  422. # eval: (add-hook 'write-file-hooks 'time-stamp)
  423. # time-stamp-start: "my $VERSION = '"
  424. # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
  425. # time-stamp-time-zone: "UTC"
  426. # time-stamp-end: "'; # UTC"
  427. # End: