gitlog-to-changelog 13 KB

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