gitlog-to-changelog 16 KB

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