1 eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
2 & eval 'exec perl -wS "$0" $argv:q'
4 # Convert git log output to ChangeLog format.
6 my $VERSION = '2011-12-24 18:51'; # UTC
7 # The definition above must lie within the first 8 lines in order
8 # for the Emacs time-stamp write hook (at end) to update it.
9 # If you change this file with Emacs, please let the write hook
10 # do its job. Otherwise, update this string manually.
12 # Copyright (C) 2008-2011 Free Software Foundation, Inc.
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 3 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 # Written by Jim Meyering
32 use POSIX qw(strftime);
34 (my $ME = $0) =~ s|.*/||;
36 # use File::Coda; # http://meyering.net/code/Coda/
38 defined fileno STDOUT or return;
39 close STDOUT and return;
40 warn "$ME: failed to close standard output: $!\n";
47 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
50 print $STREAM "Try `$ME --help' for more information.\n";
55 Usage: $ME [OPTIONS] [ARGS]
57 Convert git log output to ChangeLog format. If present, any ARGS
58 are passed to "git log". To avoid ARGS being parsed as options to
59 $ME, they may be preceded by '--'.
63 --amend=FILE FILE maps from an SHA1 to perl code (i.e., s/old/new/) that
64 makes a change to SHA1's commit log text or metadata.
65 --append-dot append a dot to the first line of each commit message if
66 there is no other punctuation or blank at the end.
67 --since=DATE convert only the logs since DATE;
68 the default is to convert all log entries.
69 --format=FMT set format string for commit subject and body;
70 see 'man git-log' for the list of format metacharacters;
71 the default is '%s%n%b%n'
73 --help display this help and exit
74 --version output version information and exit
78 $ME --since=2008-01-01 > ChangeLog
79 $ME -- -n 5 foo > last-5-commits-to-branch-foo
81 In a FILE specified via --amend, comment lines (starting with "#") are ignored.
82 FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
83 a line) referring to a commit in the current project, and CODE refers to one
84 or more consecutive lines of Perl code. Pairs must be separated by one or
87 Here is sample input for use with --amend=FILE, from coreutils:
89 3a169f4c5d9159283548178668d2fae6fced3030
91 s/all tile types/all file types/
93 1379ed974f1fa39b12e2ffab18b3f7a607082202
94 # Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
95 # Change the author to be Paul. Note the escaped "@":
96 s,Jim .*>,Paul Eggert <eggert\@cs.ucla.edu>,
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.
108 if ($s =~ m![^\w+/.,-]!)
110 # Convert each single quote to '\''
111 $s =~ s/\'/\'\\\'\'/g;
112 # Then single quote the string.
120 return join (' ', map {shell_quote $_} @_);
124 # Comment lines (starting with "#") are ignored.
125 # F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
126 # (alone on a line) referring to a commit in the current project, and
127 # CODE refers to one or more consecutive lines of Perl code.
128 # Pairs must be separated by one or more blank line.
129 sub parse_amend_file($)
134 or die "$ME: $f: failed to open for reading: $!\n";
140 while (defined (my $line = <F>))
146 and $in_code = 0, next;
150 $line =~ /^([0-9a-fA-F]{40})$/
151 or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
156 and (warn "$ME: $f:$.: duplicate SHA1\n"),
162 $h->{$sha} .= "$line\n";
175 my $format_string = '%s%n%b%n';
180 help => sub { usage 0 },
181 version => sub { print "$ME version $VERSION\n"; exit },
182 'since=s' => \$since_date,
183 'format=s' => \$format_string,
184 'amend=s' => \$amend_file,
185 'append-dot' => \$append_dot,
190 and unshift @ARGV, "--since=$since_date";
192 # This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
193 # that makes a correction in the log or attribution of that commit.
194 my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
196 my @cmd = (qw (git log --log-size),
197 '--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
198 open PIPE, '-|', @cmd
199 or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n"
200 . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
202 my $prev_multi_paragraph;
203 my $prev_date_line = '';
204 my @prev_coauthors = ();
207 defined (my $in = <PIPE>)
209 $in =~ /^log size (\d+)$/
210 or die "$ME:$.: Invalid line (expected log size):\n$in";
214 my $n_read = read PIPE, $log, $log_nbytes;
215 $n_read == $log_nbytes
216 or die "$ME:$.: unexpected EOF\n";
218 # Extract leading hash.
219 my ($sha, $rest) = split ':', $log, 2;
221 or die "$ME:$.: malformed log entry\n";
222 $sha =~ /^[0-9a-fA-F]{40}$/
223 or die "$ME:$.: invalid SHA1: $sha\n";
225 # If this commit's log requires any transformation, do it now.
226 my $code = $amend_code->{$sha};
231 # Put the unpreprocessed entry into "$_".
234 # Let $code operate on it, safely.
235 my $r = $s->reval("$code")
236 or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
238 # Note that we've used this entry.
239 delete $amend_code->{$sha};
241 # Update $rest upon success.
245 my @line = split "\n", $rest;
246 my $author_line = shift @line;
248 or die "$ME:$.: unexpected EOF\n";
249 $author_line =~ /^(\d+) (.*>)$/
250 or die "$ME:$.: Invalid line "
251 . "(expected date/author/email):\n$author_line\n";
252 my $date_line = sprintf "%s $2\n", strftime ("%F", localtime ($1));
254 my @coauthors = grep /^Co-authored-by:.*$/, @line;
255 # Omit "Co-authored-by..." and "Signed-off-by..." lines.
256 @line = grep !/^Signed-off-by: .*>$/, @line;
257 @line = grep !/^Co-authored-by: /, @line;
259 # Remove leading and trailing blank lines.
262 while ($line[0] =~ /^\s*$/) { shift @line; }
263 while ($line[$#line] =~ /^\s*$/) { pop @line; }
266 # Record whether there are two or more paragraphs.
267 my $multi_paragraph = grep /^\s*$/, @line;
269 # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
270 # standard multi-author ChangeLog format.
273 s/^Co-authored-by:\s*/\t /;
277 or warn "$ME: warning: missing email address for "
278 . substr ($_, 5) . "\n";
281 # If this header would be different from the previous date/name/email/
282 # coauthors header, or if this or the previous entry consists of two
283 # or more paragraphs, then print the header.
284 if ($date_line ne $prev_date_line
285 or "@coauthors" ne "@prev_coauthors"
287 or $prev_multi_paragraph)
289 $prev_date_line eq ''
293 and print join ("\n", @coauthors), "\n";
295 $prev_date_line = $date_line;
296 @prev_coauthors = @coauthors;
297 $prev_multi_paragraph = $multi_paragraph;
299 # If there were any lines
302 warn "$ME: warning: empty commit message:\n $date_line\n";
308 # If the first line of the message has enough room, then
309 if (length $line[0] < 72)
311 # append a dot if there is no other punctuation or blank
313 $line[0] =~ /[[:punct:]\s]$/
318 # Prefix each non-empty line with a TAB.
319 @line = map { length $_ ? "\t$_" : '' } @line;
321 print "\n", join ("\n", @line), "\n";
324 defined ($in = <PIPE>)
327 and die "$ME:$.: unexpected line:\n$in";
331 or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
332 # FIXME-someday: include $PROCESS_STATUS in the diagnostic
334 # Complain about any unused entry in the --amend=F specified file.
336 foreach my $sha (keys %$amend_code)
338 warn "$ME:$amend_file: unused entry: $sha\n";
347 # indent-tabs-mode: nil
348 # eval: (add-hook 'write-file-hooks 'time-stamp)
349 # time-stamp-start: "my $VERSION = '"
350 # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
351 # time-stamp-time-zone: "UTC"
352 # time-stamp-end: "'; # UTC"