OCaml stable dependencies.
[virt-top.git] / gitlog-to-changelog
1 #!/usr/bin/perl
2 # Convert git log output to ChangeLog format.
3
4 my $VERSION = '2009-10-05 15:04'; # 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, 2009 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] [ARGS]
54
55 Convert git log output to ChangeLog format.  If present, any ARGS
56 are passed to "git log".  To avoid ARGS being parsed as options to
57 $ME, they may be preceded by '--'.
58
59 OPTIONS:
60
61    --since=DATE convert only the logs since DATE;
62                   the default is to convert all log entries.
63    --format=FMT set format string for commit subject and body;
64                   see 'man git-log' for the list of format metacharacters;
65                   the default is '%s%n%b%n'
66
67    --help       display this help and exit
68    --version    output version information and exit
69
70 EXAMPLE:
71
72   $ME --since=2008-01-01 > ChangeLog
73   $ME -- -n 5 foo > last-5-commits-to-branch-foo
74
75 EOF
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.
82 sub 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
95 sub quoted_cmd(@)
96 {
97   return join (' ', map {shell_quote $_} @_);
98 }
99
100 {
101   my $since_date = '1970-01-01 UTC';
102   my $format_string = '%s%n%b%n';
103   GetOptions
104     (
105      help => sub { usage 0 },
106      version => sub { print "$ME version $VERSION\n"; exit },
107      'since=s' => \$since_date,
108      'format=s' => \$format_string,
109     ) or usage 1;
110
111   my @cmd = (qw (git log --log-size), "--since=$since_date",
112              '--pretty=format:%ct  %an  <%ae>%n%n'.$format_string, @ARGV);
113   open PIPE, '-|', @cmd
114     or die ("$ME: failed to run `". quoted_cmd (@cmd) ."': $!\n"
115             . "(Is your Git too old?  Version 1.5.1 or later is required.)\n");
116
117   my $prev_date_line = '';
118   while (1)
119     {
120       defined (my $in = <PIPE>)
121         or last;
122       $in =~ /^log size (\d+)$/
123         or die "$ME:$.: Invalid line (expected log size):\n$in";
124       my $log_nbytes = $1;
125
126       my $log;
127       my $n_read = read PIPE, $log, $log_nbytes;
128       $n_read == $log_nbytes
129         or die "$ME:$.: unexpected EOF\n";
130
131       my @line = split "\n", $log;
132       my $author_line = shift @line;
133       defined $author_line
134         or die "$ME:$.: unexpected EOF\n";
135       if ($author_line =~ /^(\d+)  (.*>)$/) {
136           my $date_line = sprintf "%s  $2\n", strftime ("%F", localtime ($1));
137           # If this line would be the same as the previous date/name/email
138           # line, then arrange not to print it.
139           if ($date_line ne $prev_date_line)
140           {
141               $prev_date_line eq ''
142                   or print "\n";
143               print $date_line;
144           }
145           $prev_date_line = $date_line;
146       }
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  $prev_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: