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 = '2015-06-11 01:03'; # 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-2015 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 --no-cluster never cluster commit messages under the same date/author
68 header; the default is to cluster adjacent commit messages
69 if their headers are the same and neither commit message
70 contains multiple paragraphs.
71 --srcdir=DIR the root of the source tree, from which the .git/
72 directory can be derived.
73 --since=DATE convert only the logs since DATE;
74 the default is to convert all log entries.
75 --until=DATE convert only the logs older than DATE.
76 --ignore-matching=PAT ignore commit messages whose first lines match PAT.
77 --ignore-line=PAT ignore lines of commit messages that match PAT.
78 --format=FMT set format string for commit subject and body;
79 see 'man git-log' for the list of format metacharacters;
80 the default is '%s%n%b%n'
81 --strip-tab remove one additional leading TAB from commit message lines.
82 --strip-cherry-pick remove data inserted by "git cherry-pick";
83 this includes the "cherry picked from commit ..." line,
84 and the possible final "Conflicts:" paragraph.
85 --help display this help and exit
86 --version output version information and exit
90 $ME --since=2008-01-01 > ChangeLog
91 $ME -- -n 5 foo > last-5-commits-to-branch-foo
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.
98 Copyright-paperwork-exempt: Yes
99 Append the "(tiny change)" notation to the usual "date name email"
100 ChangeLog header to mark a change that does not require a copyright
102 Co-authored-by: Joe User <user\@example.com>
103 List the specified name and email address on a second
104 ChangeLog header, denoting a co-author.
105 Signed-off-by: Joe User <user\@example.com>
106 These lines are simply elided.
108 In a FILE specified via --amend, comment lines (starting with "#") are ignored.
109 FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
110 a line) referring to a commit in the current project, and CODE refers to one
111 or more consecutive lines of Perl code. Pairs must be separated by one or
114 Here is sample input for use with --amend=FILE, from coreutils:
116 3a169f4c5d9159283548178668d2fae6fced3030
118 s/all tile types/all file types/
120 1379ed974f1fa39b12e2ffab18b3f7a607082202
121 # Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
122 # Change the author to be Paul. Note the escaped "@":
123 s,Jim .*>,Paul Eggert <eggert\\\@cs.ucla.edu>,
130 # If the string $S is a well-behaved file name, simply return it.
131 # If it contains white space, quotes, etc., quote it, and return the new string.
135 if ($s =~ m![^\w+/.,-]!)
137 # Convert each single quote to '\''
138 $s =~ s/\'/\'\\\'\'/g;
139 # Then single quote the string.
147 return join (' ', map {shell_quote $_} @_);
151 # Comment lines (starting with "#") are ignored.
152 # F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
153 # (alone on a line) referring to a commit in the current project, and
154 # CODE refers to one or more consecutive lines of Perl code.
155 # Pairs must be separated by one or more blank line.
156 sub parse_amend_file($)
161 or die "$ME: $f: failed to open for reading: $!\n";
167 while (defined (my $line = <F>))
173 and $in_code = 0, next;
177 $line =~ /^([0-9a-fA-F]{40})$/
178 or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
183 and (warn "$ME: $f:$.: duplicate SHA1\n"),
189 $h->{$sha} .= "$line\n";
200 # git_dir_option $SRCDIR
202 # From $SRCDIR, the --git-dir option to pass to git (none if $SRCDIR
203 # is undef). Return as a list (0 or 1 element).
204 sub git_dir_option($)
210 my $qdir = shell_quote $srcdir;
211 my $cmd = "cd $qdir && git rev-parse --show-toplevel";
212 my $qcmd = shell_quote $cmd;
213 my $git_dir = qx($cmd);
215 or die "$ME: cannot run $qcmd: $!\n";
217 or die "$ME: $qcmd had unexpected exit code or signal ($?)\n";
219 push @res, "--git-dir=$git_dir/.git";
227 my $format_string = '%s%n%b%n';
234 my $strip_cherry_pick = 0;
238 help => sub { usage 0 },
239 version => sub { print "$ME version $VERSION\n"; exit },
240 'since=s' => \$since_date,
241 'until=s' => \$until_date,
242 'format=s' => \$format_string,
243 'amend=s' => \$amend_file,
244 'append-dot' => \$append_dot,
245 'cluster!' => \$cluster,
246 'ignore-matching=s' => \$ignore_matching,
247 'ignore-line=s' => \$ignore_line,
248 'strip-tab' => \$strip_tab,
249 'strip-cherry-pick' => \$strip_cherry_pick,
250 'srcdir=s' => \$srcdir,
254 and unshift @ARGV, "--since=$since_date";
256 and unshift @ARGV, "--until=$until_date";
258 # This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
259 # that makes a correction in the log or attribution of that commit.
260 my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
263 git_dir_option $srcdir,
265 '--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
266 open PIPE, '-|', @cmd
267 or die ("$ME: failed to run '". quoted_cmd (@cmd) ."': $!\n"
268 . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
270 my $prev_multi_paragraph;
271 my $prev_date_line = '';
272 my @prev_coauthors = ();
276 defined (my $in = <PIPE>)
278 $in =~ /^log size (\d+)$/
279 or die "$ME:$.: Invalid line (expected log size):\n$in";
283 my $n_read = read PIPE, $log, $log_nbytes;
284 $n_read == $log_nbytes
285 or die "$ME:$.: unexpected EOF\n";
287 # Extract leading hash.
288 my ($sha, $rest) = split ':', $log, 2;
290 or die "$ME:$.: malformed log entry\n";
291 $sha =~ /^[0-9a-fA-F]{40}$/
292 or die "$ME:$.: invalid SHA1: $sha\n";
302 ## Perhaps only warn if a pattern matches more than once?
303 warn "$ME: warning: skipping $sha due to $_\n";
309 # If this commit's log requires any transformation, do it now.
310 my $code = $amend_code->{$sha};
315 # Put the unpreprocessed entry into "$_".
318 # Let $code operate on it, safely.
319 my $r = $s->reval("$code")
320 or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
322 # Note that we've used this entry.
323 delete $amend_code->{$sha};
325 # Update $rest upon success.
329 # Remove lines inserted by "git cherry-pick".
330 if ($strip_cherry_pick)
332 $rest =~ s/^\s*Conflicts:\n.*//sm;
333 $rest =~ s/^\s*\(cherry picked from commit [\da-f]+\)\n//m;
336 my @line = split /[ \t]*\n/, $rest;
337 my $author_line = shift @line;
339 or die "$ME:$.: unexpected EOF\n";
340 $author_line =~ /^(\d+) (.*>)$/
341 or die "$ME:$.: Invalid line "
342 . "(expected date/author/email):\n$author_line\n";
344 # Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
345 # `(tiny change)' annotation.
346 my $tiny = (grep (/^(?:Copyright-paperwork-exempt|Tiny-change):\s+[Yy]es$/, @line)
347 ? ' (tiny change)' : '');
349 my $date_line = sprintf "%s %s$tiny\n",
350 strftime ("%Y-%m-%d", localtime ($1)), $2;
352 my @coauthors = grep /^Co-authored-by:.*$/, @line;
353 # Omit meta-data lines we've already interpreted.
354 @line = grep !/^(?:Signed-off-by:[ ].*>$
356 |Copyright-paperwork-exempt:[ ]
360 # Remove leading and trailing blank lines.
363 while ($line[0] =~ /^\s*$/) { shift @line; }
364 while ($line[$#line] =~ /^\s*$/) { pop @line; }
367 # Handle Emacs gitmerge.el "skipped" commits.
368 # Yes, this should be controlled by an option. So sue me.
369 if ( grep /^(; )?Merge from /, @line )
374 if (grep /^The following commit.*skipped:$/, $_)
377 ## Reset at each merge to reduce chance of false matches.
381 if ($found && $_ =~ /^([0-9a-fA-F]{7,}) [^ ]/)
383 push ( @skipshas, $1 );
388 # Ignore commits that match the --ignore-matching pattern, if specified.
389 if (! ($skipflag || (defined $ignore_matching
390 && @line && $line[0] =~ /$ignore_matching/)))
392 if (defined $ignore_line && @line)
394 @line = grep ! /$ignore_line/, @line;
395 while ($line[$#line] =~ /^\s*$/) { pop @line; }
398 # Record whether there are two or more paragraphs.
399 my $multi_paragraph = grep /^\s*$/, @line;
401 # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
402 # standard multi-author ChangeLog format.
405 s/^Co-authored-by:\s*/\t /;
409 or warn "$ME: warning: missing email address for "
410 . substr ($_, 5) . "\n";
413 # If clustering of commit messages has been disabled, if this header
414 # would be different from the previous date/name/etc. header,
415 # or if this or the previous entry consists of two or more paragraphs,
416 # then print the header.
418 || $date_line ne $prev_date_line
419 || "@coauthors" ne "@prev_coauthors"
421 || $prev_multi_paragraph)
423 $prev_date_line eq ''
427 and print join ("\n", @coauthors), "\n";
429 $prev_date_line = $date_line;
430 @prev_coauthors = @coauthors;
431 $prev_multi_paragraph = $multi_paragraph;
433 # If there were any lines
436 warn "$ME: warning: empty commit message:\n $date_line\n";
442 # If the first line of the message has enough room, then
443 if (length $line[0] < 72)
445 # append a dot if there is no other punctuation or blank
447 $line[0] =~ /[[:punct:]\s]$/
452 # Remove one additional leading TAB from each line.
454 and map { s/^\t// } @line;
456 # Prefix each non-empty line with a TAB.
457 @line = map { length $_ ? "\t$_" : '' } @line;
459 print "\n", join ("\n", @line), "\n";
463 defined ($in = <PIPE>)
466 and die "$ME:$.: unexpected line:\n$in";
470 or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
471 # FIXME-someday: include $PROCESS_STATUS in the diagnostic
473 # Complain about any unused entry in the --amend=F specified file.
475 foreach my $sha (keys %$amend_code)
477 warn "$ME:$amend_file: unused entry: $sha\n";
486 # indent-tabs-mode: nil
487 # eval: (add-hook 'write-file-hooks 'time-stamp)
488 # time-stamp-start: "my $VERSION = '"
489 # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
490 # time-stamp-time-zone: "UTC"
491 # time-stamp-end: "'; # UTC"