gitlog-to-changelog 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 = '2012-01-06 07:14'; # 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-2012 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. --since=DATE convert only the logs since DATE;
  55. the default is to convert all log entries.
  56. --format=FMT set format string for commit subject and body;
  57. see 'man git-log' for the list of format metacharacters;
  58. the default is '%s%n%b%n'
  59. --help display this help and exit
  60. --version output version information and exit
  61. EXAMPLE:
  62. $ME --since=2008-01-01 > ChangeLog
  63. $ME -- -n 5 foo > last-5-commits-to-branch-foo
  64. SPECIAL SYNTAX:
  65. The following types of strings are interpreted specially when they appear
  66. at the beginning of a log message line. They are not copied to the output.
  67. Copyright-paperwork-exempt: Yes
  68. Append the "(tiny change)" notation to the usual "date name email"
  69. ChangeLog header to mark a change that does not require a copyright
  70. assignment.
  71. Co-authored-by: Joe User <user\@example.com>
  72. List the specified name and email address on a second
  73. ChangeLog header, denoting a co-author.
  74. Signed-off-by: Joe User <user\@example.com>
  75. These lines are simply elided.
  76. In a FILE specified via --amend, comment lines (starting with "#") are ignored.
  77. FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
  78. a line) referring to a commit in the current project, and CODE refers to one
  79. or more consecutive lines of Perl code. Pairs must be separated by one or
  80. more blank line.
  81. Here is sample input for use with --amend=FILE, from coreutils:
  82. 3a169f4c5d9159283548178668d2fae6fced3030
  83. # fix typo in title:
  84. s/all tile types/all file types/
  85. 1379ed974f1fa39b12e2ffab18b3f7a607082202
  86. # Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
  87. # Change the author to be Paul. Note the escaped "@":
  88. s,Jim .*>,Paul Eggert <eggert\\\@cs.ucla.edu>,
  89. EOF
  90. }
  91. exit $exit_code;
  92. }
  93. # If the string $S is a well-behaved file name, simply return it.
  94. # If it contains white space, quotes, etc., quote it, and return the new string.
  95. sub shell_quote($)
  96. {
  97. my ($s) = @_;
  98. if ($s =~ m![^\w+/.,-]!)
  99. {
  100. # Convert each single quote to '\''
  101. $s =~ s/\'/\'\\\'\'/g;
  102. # Then single quote the string.
  103. $s = "'$s'";
  104. }
  105. return $s;
  106. }
  107. sub quoted_cmd(@)
  108. {
  109. return join (' ', map {shell_quote $_} @_);
  110. }
  111. # Parse file F.
  112. # Comment lines (starting with "#") are ignored.
  113. # F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
  114. # (alone on a line) referring to a commit in the current project, and
  115. # CODE refers to one or more consecutive lines of Perl code.
  116. # Pairs must be separated by one or more blank line.
  117. sub parse_amend_file($)
  118. {
  119. my ($f) = @_;
  120. open F, '<', $f
  121. or die "$ME: $f: failed to open for reading: $!\n";
  122. my $fail;
  123. my $h = {};
  124. my $in_code = 0;
  125. my $sha;
  126. while (defined (my $line = <F>))
  127. {
  128. $line =~ /^\#/
  129. and next;
  130. chomp $line;
  131. $line eq ''
  132. and $in_code = 0, next;
  133. if (!$in_code)
  134. {
  135. $line =~ /^([0-9a-fA-F]{40})$/
  136. or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
  137. $fail = 1, next;
  138. $sha = lc $1;
  139. $in_code = 1;
  140. exists $h->{$sha}
  141. and (warn "$ME: $f:$.: duplicate SHA1\n"),
  142. $fail = 1, next;
  143. }
  144. else
  145. {
  146. $h->{$sha} ||= '';
  147. $h->{$sha} .= "$line\n";
  148. }
  149. }
  150. close F;
  151. $fail
  152. and exit 1;
  153. return $h;
  154. }
  155. {
  156. my $since_date;
  157. my $format_string = '%s%n%b%n';
  158. my $amend_file;
  159. my $append_dot = 0;
  160. GetOptions
  161. (
  162. help => sub { usage 0 },
  163. version => sub { print "$ME version $VERSION\n"; exit },
  164. 'since=s' => \$since_date,
  165. 'format=s' => \$format_string,
  166. 'amend=s' => \$amend_file,
  167. 'append-dot' => \$append_dot,
  168. ) or usage 1;
  169. defined $since_date
  170. and unshift @ARGV, "--since=$since_date";
  171. # This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
  172. # that makes a correction in the log or attribution of that commit.
  173. my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
  174. my @cmd = (qw (git log --log-size),
  175. '--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
  176. open PIPE, '-|', @cmd
  177. or die ("$ME: failed to run '". quoted_cmd (@cmd) ."': $!\n"
  178. . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
  179. my $prev_multi_paragraph;
  180. my $prev_date_line = '';
  181. my @prev_coauthors = ();
  182. while (1)
  183. {
  184. defined (my $in = <PIPE>)
  185. or last;
  186. $in =~ /^log size (\d+)$/
  187. or die "$ME:$.: Invalid line (expected log size):\n$in";
  188. my $log_nbytes = $1;
  189. my $log;
  190. my $n_read = read PIPE, $log, $log_nbytes;
  191. $n_read == $log_nbytes
  192. or die "$ME:$.: unexpected EOF\n";
  193. # Extract leading hash.
  194. my ($sha, $rest) = split ':', $log, 2;
  195. defined $sha
  196. or die "$ME:$.: malformed log entry\n";
  197. $sha =~ /^[0-9a-fA-F]{40}$/
  198. or die "$ME:$.: invalid SHA1: $sha\n";
  199. # If this commit's log requires any transformation, do it now.
  200. my $code = $amend_code->{$sha};
  201. if (defined $code)
  202. {
  203. eval 'use Safe';
  204. my $s = new Safe;
  205. # Put the unpreprocessed entry into "$_".
  206. $_ = $rest;
  207. # Let $code operate on it, safely.
  208. my $r = $s->reval("$code")
  209. or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
  210. # Note that we've used this entry.
  211. delete $amend_code->{$sha};
  212. # Update $rest upon success.
  213. $rest = $_;
  214. }
  215. my @line = split "\n", $rest;
  216. my $author_line = shift @line;
  217. defined $author_line
  218. or die "$ME:$.: unexpected EOF\n";
  219. $author_line =~ /^(\d+) (.*>)$/
  220. or die "$ME:$.: Invalid line "
  221. . "(expected date/author/email):\n$author_line\n";
  222. # Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
  223. # `(tiny change)' annotation.
  224. my $tiny = (grep (/^Copyright-paperwork-exempt:\s+[Yy]es$/, @line)
  225. ? ' (tiny change)' : '');
  226. my $date_line = sprintf "%s %s$tiny\n",
  227. strftime ("%F", localtime ($1)), $2;
  228. my @coauthors = grep /^Co-authored-by:.*$/, @line;
  229. # Omit meta-data lines we've already interpreted.
  230. @line = grep !/^(?:Signed-off-by:[ ].*>$
  231. |Co-authored-by:[ ]
  232. |Copyright-paperwork-exempt:[ ]
  233. )/x, @line;
  234. # Remove leading and trailing blank lines.
  235. if (@line)
  236. {
  237. while ($line[0] =~ /^\s*$/) { shift @line; }
  238. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  239. }
  240. # Record whether there are two or more paragraphs.
  241. my $multi_paragraph = grep /^\s*$/, @line;
  242. # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
  243. # standard multi-author ChangeLog format.
  244. for (@coauthors)
  245. {
  246. s/^Co-authored-by:\s*/\t /;
  247. s/\s*</ </;
  248. /<.*?@.*\..*>/
  249. or warn "$ME: warning: missing email address for "
  250. . substr ($_, 5) . "\n";
  251. }
  252. # If this header would be different from the previous date/name/email/
  253. # coauthors header, or if this or the previous entry consists of two
  254. # or more paragraphs, then print the header.
  255. if ($date_line ne $prev_date_line
  256. or "@coauthors" ne "@prev_coauthors"
  257. or $multi_paragraph
  258. or $prev_multi_paragraph)
  259. {
  260. $prev_date_line eq ''
  261. or print "\n";
  262. print $date_line;
  263. @coauthors
  264. and print join ("\n", @coauthors), "\n";
  265. }
  266. $prev_date_line = $date_line;
  267. @prev_coauthors = @coauthors;
  268. $prev_multi_paragraph = $multi_paragraph;
  269. # If there were any lines
  270. if (@line == 0)
  271. {
  272. warn "$ME: warning: empty commit message:\n $date_line\n";
  273. }
  274. else
  275. {
  276. if ($append_dot)
  277. {
  278. # If the first line of the message has enough room, then
  279. if (length $line[0] < 72)
  280. {
  281. # append a dot if there is no other punctuation or blank
  282. # at the end.
  283. $line[0] =~ /[[:punct:]\s]$/
  284. or $line[0] .= '.';
  285. }
  286. }
  287. # Prefix each non-empty line with a TAB.
  288. @line = map { length $_ ? "\t$_" : '' } @line;
  289. print "\n", join ("\n", @line), "\n";
  290. }
  291. defined ($in = <PIPE>)
  292. or last;
  293. $in ne "\n"
  294. and die "$ME:$.: unexpected line:\n$in";
  295. }
  296. close PIPE
  297. or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
  298. # FIXME-someday: include $PROCESS_STATUS in the diagnostic
  299. # Complain about any unused entry in the --amend=F specified file.
  300. my $fail = 0;
  301. foreach my $sha (keys %$amend_code)
  302. {
  303. warn "$ME:$amend_file: unused entry: $sha\n";
  304. $fail = 1;
  305. }
  306. exit $fail;
  307. }
  308. # Local Variables:
  309. # mode: perl
  310. # indent-tabs-mode: nil
  311. # eval: (add-hook 'write-file-hooks 'time-stamp)
  312. # time-stamp-start: "my $VERSION = '"
  313. # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
  314. # time-stamp-time-zone: "UTC"
  315. # time-stamp-end: "'; # UTC"
  316. # End: