gitlog-to-changelog 12 KB

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