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