4
4
# Convert git log output to ChangeLog format.
6
my $VERSION = '2009-10-30 13:46'; # UTC
6
my $VERSION = '2012-07-29 06:11'; # UTC
7
7
# The definition above must lie within the first 8 lines in order
8
8
# for the Emacs time-stamp write hook (at end) to update it.
9
9
# If you change this file with Emacs, please let the write hook
10
10
# do its job. Otherwise, update this string manually.
12
# Copyright (C) 2008-2011 Free Software Foundation, Inc.
12
# Copyright (C) 2008-2012 Free Software Foundation, Inc.
14
14
# This program is free software: you can redistribute it and/or modify
15
15
# it under the terms of the GNU General Public License as published 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.
63
73
--since=DATE convert only the logs since DATE;
64
74
the default is to convert all log entries.
65
75
--format=FMT set format string for commit subject and body;
66
76
see 'man git-log' for the list of format metacharacters;
67
77
the default is '%s%n%b%n'
78
--strip-tab remove one additional leading TAB from commit message lines.
79
--strip-cherry-pick remove data inserted by "git cherry-pick";
80
this includes the "cherry picked from commit ..." line,
81
and the possible final "Conflicts:" paragraph.
69
82
--help display this help and exit
70
83
--version output version information and exit
74
87
$ME --since=2008-01-01 > ChangeLog
75
88
$ME -- -n 5 foo > last-5-commits-to-branch-foo
92
The following types of strings are interpreted specially when they appear
93
at the beginning of a log message line. They are not copied to the output.
95
Copyright-paperwork-exempt: Yes
96
Append the "(tiny change)" notation to the usual "date name email"
97
ChangeLog header to mark a change that does not require a copyright
99
Co-authored-by: Joe User <user\@example.com>
100
List the specified name and email address on a second
101
ChangeLog header, denoting a co-author.
102
Signed-off-by: Joe User <user\@example.com>
103
These lines are simply elided.
105
In a FILE specified via --amend, comment lines (starting with "#") are ignored.
106
FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
107
a line) referring to a commit in the current project, and CODE refers to one
108
or more consecutive lines of Perl code. Pairs must be separated by one or
111
Here is sample input for use with --amend=FILE, from coreutils:
113
3a169f4c5d9159283548178668d2fae6fced3030
115
s/all tile types/all file types/
117
1379ed974f1fa39b12e2ffab18b3f7a607082202
118
# Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
119
# Change the author to be Paul. Note the escaped "@":
120
s,Jim .*>,Paul Eggert <eggert\\\@cs.ucla.edu>,
99
144
return join (' ', map {shell_quote $_} @_);
103
my $since_date = '1970-01-01 UTC';
148
# Comment lines (starting with "#") are ignored.
149
# F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
150
# (alone on a line) referring to a commit in the current project, and
151
# CODE refers to one or more consecutive lines of Perl code.
152
# Pairs must be separated by one or more blank line.
153
sub parse_amend_file($)
158
or die "$ME: $f: failed to open for reading: $!\n";
164
while (defined (my $line = <F>))
170
and $in_code = 0, next;
174
$line =~ /^([0-9a-fA-F]{40})$/
175
or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
180
and (warn "$ME: $f:$.: duplicate SHA1\n"),
186
$h->{$sha} .= "$line\n";
197
# git_dir_option $SRCDIR
199
# From $SRCDIR, the --git-dir option to pass to git (none if $SRCDIR
200
# is undef). Return as a list (0 or 1 element).
201
sub git_dir_option($)
207
my $qdir = shell_quote $srcdir;
208
my $cmd = "cd $qdir && git rev-parse --show-toplevel";
209
my $qcmd = shell_quote $cmd;
210
my $git_dir = qx($cmd);
212
or die "$ME: cannot run $qcmd: $!\n";
214
or die "$ME: $qcmd had unexpected exit code or signal ($?)\n";
216
push @res, "--git-dir=$git_dir/.git";
104
223
my $format_string = '%s%n%b%n';
228
my $strip_cherry_pick = 0;
107
232
help => sub { usage 0 },
108
233
version => sub { print "$ME version $VERSION\n"; exit },
109
234
'since=s' => \$since_date,
110
235
'format=s' => \$format_string,
236
'amend=s' => \$amend_file,
237
'append-dot' => \$append_dot,
238
'cluster!' => \$cluster,
239
'strip-tab' => \$strip_tab,
240
'strip-cherry-pick' => \$strip_cherry_pick,
241
'srcdir=s' => \$srcdir,
113
my @cmd = (qw (git log --log-size), "--since=$since_date",
114
'--pretty=format:%ct %an <%ae>%n%n'.$format_string, @ARGV);
245
and unshift @ARGV, "--since=$since_date";
247
# This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
248
# that makes a correction in the log or attribution of that commit.
249
my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
252
git_dir_option $srcdir,
254
'--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
115
255
open PIPE, '-|', @cmd
116
or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n"
256
or die ("$ME: failed to run '". quoted_cmd (@cmd) ."': $!\n"
117
257
. "(Is your Git too old? Version 1.5.1 or later is required.)\n");
259
my $prev_multi_paragraph;
119
260
my $prev_date_line = '';
261
my @prev_coauthors = ();
122
264
defined (my $in = <PIPE>)
130
272
$n_read == $log_nbytes
131
273
or die "$ME:$.: unexpected EOF\n";
133
my @line = split "\n", $log;
275
# Extract leading hash.
276
my ($sha, $rest) = split ':', $log, 2;
278
or die "$ME:$.: malformed log entry\n";
279
$sha =~ /^[0-9a-fA-F]{40}$/
280
or die "$ME:$.: invalid SHA1: $sha\n";
282
# If this commit's log requires any transformation, do it now.
283
my $code = $amend_code->{$sha};
288
# Put the unpreprocessed entry into "$_".
291
# Let $code operate on it, safely.
292
my $r = $s->reval("$code")
293
or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
295
# Note that we've used this entry.
296
delete $amend_code->{$sha};
298
# Update $rest upon success.
302
# Remove lines inserted by "git cherry-pick".
303
if ($strip_cherry_pick)
305
$rest =~ s/^\s*Conflicts:\n.*//sm;
306
$rest =~ s/^\s*\(cherry picked from commit [\da-f]+\)\n//m;
309
my @line = split "\n", $rest;
134
310
my $author_line = shift @line;
135
311
defined $author_line
136
312
or die "$ME:$.: unexpected EOF\n";
138
314
or die "$ME:$.: Invalid line "
139
315
. "(expected date/author/email):\n$author_line\n";
141
my $date_line = sprintf "%s $2\n", strftime ("%F", localtime ($1));
142
# If this line would be the same as the previous date/name/email
143
# line, then arrange not to print it.
144
if ($date_line ne $prev_date_line)
317
# Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
318
# `(tiny change)' annotation.
319
my $tiny = (grep (/^Copyright-paperwork-exempt:\s+[Yy]es$/, @line)
320
? ' (tiny change)' : '');
322
my $date_line = sprintf "%s %s$tiny\n",
323
strftime ("%F", localtime ($1)), $2;
325
my @coauthors = grep /^Co-authored-by:.*$/, @line;
326
# Omit meta-data lines we've already interpreted.
327
@line = grep !/^(?:Signed-off-by:[ ].*>$
329
|Copyright-paperwork-exempt:[ ]
332
# Remove leading and trailing blank lines.
335
while ($line[0] =~ /^\s*$/) { shift @line; }
336
while ($line[$#line] =~ /^\s*$/) { pop @line; }
339
# Record whether there are two or more paragraphs.
340
my $multi_paragraph = grep /^\s*$/, @line;
342
# Format 'Co-authored-by: A U Thor <email@example.com>' lines in
343
# standard multi-author ChangeLog format.
346
s/^Co-authored-by:\s*/\t /;
350
or warn "$ME: warning: missing email address for "
351
. substr ($_, 5) . "\n";
354
# If clustering of commit messages has been disabled, if this header
355
# would be different from the previous date/name/email/coauthors header,
356
# or if this or the previous entry consists of two or more paragraphs,
357
# then print the header.
359
|| $date_line ne $prev_date_line
360
|| "@coauthors" ne "@prev_coauthors"
362
|| $prev_multi_paragraph)
146
364
$prev_date_line eq ''
148
366
print $date_line;
368
and print join ("\n", @coauthors), "\n";
150
370
$prev_date_line = $date_line;
152
# Omit "Signed-off-by..." lines.
153
@line = grep !/^Signed-off-by: .*>$/, @line;
371
@prev_coauthors = @coauthors;
372
$prev_multi_paragraph = $multi_paragraph;
155
374
# If there were any lines
162
# Remove leading and trailing blank lines.
163
while ($line[0] =~ /^\s*$/) { shift @line; }
164
while ($line[$#line] =~ /^\s*$/) { pop @line; }
383
# If the first line of the message has enough room, then
384
if (length $line[0] < 72)
386
# append a dot if there is no other punctuation or blank
388
$line[0] =~ /[[:punct:]\s]$/
393
# Remove one additional leading TAB from each line.
395
and map { s/^\t// } @line;
166
397
# Prefix each non-empty line with a TAB.
167
398
@line = map { length $_ ? "\t$_" : '' } @line;