Use Gnulib's `version-etc-fsf' for `--version' and `--help' output.
[bpt/guile.git] / build-aux / gitlog-to-changelog
CommitLineData
0f3eacf7
LC
1#!/bin/sh
2# Accommodate systems where /usr/bin/perl is not the right path.
3exec perl -x "$0" "$@"
4#!perl
5b55e293
AW
5# Convert git log output to ChangeLog format.
6
7my $VERSION = '2009-06-04 08:53'; # UTC
8# The definition above must lie within the first 8 lines in order
9# for the Emacs time-stamp write hook (at end) to update it.
10# If you change this file with Emacs, please let the write hook
11# do its job. Otherwise, update this string manually.
12
13# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
14
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28# Written by Jim Meyering
29
30use strict;
31use warnings;
32use Getopt::Long;
33use POSIX qw(strftime);
34
35(my $ME = $0) =~ s|.*/||;
36
37# use File::Coda; # http://meyering.net/code/Coda/
38END {
39 defined fileno STDOUT or return;
40 close STDOUT and return;
41 warn "$ME: failed to close standard output: $!\n";
42 $? ||= 1;
43}
44
45sub usage ($)
46{
47 my ($exit_code) = @_;
48 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
49 if ($exit_code != 0)
50 {
51 print $STREAM "Try `$ME --help' for more information.\n";
52 }
53 else
54 {
55 print $STREAM <<EOF;
56Usage: $ME [OPTIONS] [ARGS]
57
58Convert git log output to ChangeLog format. If present, any ARGS
59are passed to "git log". To avoid ARGS being parsed as options to
60$ME, they may be preceded by '--'.
61
62OPTIONS:
63
64 --since=DATE convert only the logs since DATE;
65 the default is to convert all log entries.
66
67 --help display this help and exit
68 --version output version information and exit
69
70EXAMPLE:
71
72 $ME --since=2008-01-01 > ChangeLog
73 $ME -- -n 5 foo > last-5-commits-to-branch-foo
74
75EOF
76 }
77 exit $exit_code;
78}
79
80# If the string $S is a well-behaved file name, simply return it.
81# If it contains white space, quotes, etc., quote it, and return the new string.
82sub shell_quote($)
83{
84 my ($s) = @_;
85 if ($s =~ m![^\w+/.,-]!)
86 {
87 # Convert each single quote to '\''
88 $s =~ s/\'/\'\\\'\'/g;
89 # Then single quote the string.
90 $s = "'$s'";
91 }
92 return $s;
93}
94
95sub quoted_cmd(@)
96{
97 return join (' ', map {shell_quote $_} @_);
98}
99
100{
101 my $since_date = '1970-01-01 UTC';
102 GetOptions
103 (
104 help => sub { usage 0 },
105 version => sub { print "$ME version $VERSION\n"; exit },
106 'since=s' => \$since_date,
107 ) or usage 1;
108
109 my @cmd = (qw (git log --log-size), "--since=$since_date",
110 '--pretty=format:%ct %an <%ae>%n%n%s%n%b%n', @ARGV);
111 open PIPE, '-|', @cmd
112 or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n"
113 . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
114
115 my $prev_date_line = '';
116 while (1)
117 {
118 defined (my $in = <PIPE>)
119 or last;
120 $in =~ /^log size (\d+)$/
121 or die "$ME:$.: Invalid line (expected log size):\n$in";
122 my $log_nbytes = $1;
123
124 my $log;
125 my $n_read = read PIPE, $log, $log_nbytes;
126 $n_read == $log_nbytes
127 or die "$ME:$.: unexpected EOF\n";
128
129 my @line = split "\n", $log;
130 my $author_line = shift @line;
131 defined $author_line
132 or die "$ME:$.: unexpected EOF\n";
133 $author_line =~ /^(\d+) (.*>)$/
134 or die "$ME:$.: Invalid line "
135 . "(expected date/author/email):\n$author_line\n";
136
137 my $date_line = sprintf "%s $2\n", strftime ("%F", localtime ($1));
138 # If this line would be the same as the previous date/name/email
139 # line, then arrange not to print it.
140 if ($date_line ne $prev_date_line)
141 {
142 $prev_date_line eq ''
143 or print "\n";
144 print $date_line;
145 }
146 $prev_date_line = $date_line;
147
148 # Omit "Signed-off-by..." lines.
149 @line = grep !/^Signed-off-by: .*>$/, @line;
150
151 # If there were any lines
152 if (@line == 0)
153 {
154 warn "$ME: warning: empty commit message:\n $date_line\n";
155 }
156 else
157 {
158 # Remove leading and trailing blank lines.
159 while ($line[0] =~ /^\s*$/) { shift @line; }
160 while ($line[$#line] =~ /^\s*$/) { pop @line; }
161
162 # Prefix each non-empty line with a TAB.
163 @line = map { length $_ ? "\t$_" : '' } @line;
164
165 print "\n", join ("\n", @line), "\n";
166 }
167
168 defined ($in = <PIPE>)
169 or last;
170 $in ne "\n"
171 and die "$ME:$.: unexpected line:\n$in";
172 }
173
174 close PIPE
175 or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
176 # FIXME-someday: include $PROCESS_STATUS in the diagnostic
177}
178
179# Local Variables:
180# indent-tabs-mode: nil
181# eval: (add-hook 'write-file-hooks 'time-stamp)
182# time-stamp-start: "my $VERSION = '"
183# time-stamp-format: "%:y-%02m-%02d %02H:%02M"
184# time-stamp-time-zone: "UTC"
185# time-stamp-end: "'; # UTC"
186# End: