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