Debian Codegraph @

support/git2cl

raw
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007, 2008 Simon Josefsson <[email protected]>
4 # Copyright (C) 2007 Luis Mondesi <[email protected]>
5 # * calls git directly. To use it just:
6 # cd ~/Project/my_git_repo; git2cl > ChangeLog
7 # * implements strptime()
8 # * fixes bugs in $comment parsing
9 # - copy input before we remove leading spaces
10 # - skip "merge branch" statements as they don't
11 # have information about files (i.e. we never
12 # go into $state 2)
13 # - behaves like a pipe/filter if input is given from the CLI
14 # else it calls git log by itself
15 #
16 # The functions mywrap, last_line_len, wrap_log_entry are derived from
17 # the cvs2cl tool, see <http://www.red-bean.com/cvs2cl/>:
18 # Copyright (C) 2001,2002,2003,2004 Martyn J. Pearce <[email protected]>
19 # Copyright (C) 1999 Karl Fogel <[email protected]>
20 #
21 # git2cl is free software; you can redistribute it and/or modify it
22 # under the terms of the GNU General Public License as published by
23 # the Free Software Foundation; either version 2, or (at your option)
24 # any later version.
25 #
26 # git2cl is distributed in the hope that it will be useful, but
27 # WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 # General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with git2cl; see the file COPYING. If not, write to the Free
33 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 # 02110-1301 USA.
35
36 =head1 NAME
37
38 git2cl - tool to convert git logs to GNU ChangeLog
39
40 =head1 SYNOPSIS
41
42 git2cl > ChangeLog
43
44 If you don't want git2cl to invoke git log internally, you can use it
45 as a pipe.
46 It needs a git log generated with --pretty --numstat and --summary.
47 You can use it as follows:
48
49 git log --pretty --numstat --summary | git2cl > ChangeLog
50
51 =head1 DESCRIPTION
52
53 This is a quick'n'dirty tool to convert git logs to GNU ChangeLog
54 format.
55
56 The tool invokes `git log` internally unless you pipe a log to it.
57 Thus, typically you would use it as follows:
58
59 =head1 SEE ALSO
60
61 Output format specification:
62 <http://www.gnu.org/prep/standards/html_node/Change-Logs.html>
63
64 =head1 AUTHORS
65
66 git2cl is developed by Simon Josefsson <[email protected]>
67 and Luis Mondesi <[email protected]>
68
69 =cut
70
71 use strict;
72 use POSIX qw(strftime);
73 use Text::Wrap qw(wrap);
74 use FileHandle;
75
76 use constant EMPTY_LOG_MESSAGE => '*** empty log message ***';
77
78 # this is a helper hash for stptime.
79 # Assumes you are calling 'git log ...' with LC_ALL=C
80 my %month = (
81 'Jan'=>0,
82 'Feb'=>1,
83 'Mar'=>2,
84 'Apr'=>3,
85 'May'=>4,
86 'Jun'=>5,
87 'Jul'=>6,
88 'Aug'=>7,
89 'Sep'=>8,
90 'Oct'=>9,
91 'Nov'=>10,
92 'Dec'=>11,
93 );
94
95 my $fh = new FileHandle;
96
97 sub key_ready
98 {
99 my ($rin, $nfd);
100 vec($rin, fileno(STDIN), 1) = 1;
101 return $nfd = select($rin, undef, undef, 0);
102 }
103
104 sub strptime {
105 my $str = shift;
106 return undef if not defined $str;
107
108 # we are parsing this format
109 # Fri Oct 26 00:42:56 2007 -0400
110 # to these fields
111 # sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1
112 # Luis Mondesi <[email protected]>
113 my @date;
114 if ($str =~ /([[:alpha:]]{3})\s+([[:alpha:]]{3})\s+([[:digit:]]{1,2})\s+([[:digit:]]{1,2}):([[:digit:]]{1,2}):([[:digit:]]{1,2})\s+([[:digit:]]{4})/){
115 push(@date,$6,$5,$4,$3,$month{$2},($7 - 1900),-1,-1,-1);
116 } else {
117 die ("Cannot parse date '$str'\n'");
118 }
119 return @date;
120 }
121
122 sub mywrap {
123 my ($indent1, $indent2, @text) = @_;
124 # If incoming text looks preformatted, don't get clever
125 my $text = Text::Wrap::wrap($indent1, $indent2, @text);
126 if ( grep /^\s+/m, @text ) {
127 return $text;
128 }
129 my @lines = split /\n/, $text;
130 $indent2 =~ s!^((?: {8})+)!"\t" x (length($1)/8)!e;
131 $lines[0] =~ s/^$indent1\s+/$indent1/;
132 s/^$indent2\s+/$indent2/
133 for @lines[1..$#lines];
134 my $newtext = join "\n", @lines;
135 $newtext .= "\n"
136 if substr($text, -1) eq "\n";
137 return $newtext;
138 }
139
140 sub last_line_len {
141 my $files_list = shift;
142 my @lines = split (/\n/, $files_list);
143 my $last_line = pop (@lines);
144 return length ($last_line);
145 }
146
147 # A custom wrap function, sensitive to some common constructs used in
148 # log entries.
149 sub wrap_log_entry {
150 my $text = shift; # The text to wrap.
151 my $left_pad_str = shift; # String to pad with on the left.
152
153 # These do NOT take left_pad_str into account:
154 my $length_remaining = shift; # Amount left on current line.
155 my $max_line_length = shift; # Amount left for a blank line.
156
157 my $wrapped_text = ''; # The accumulating wrapped entry.
158 my $user_indent = ''; # Inherited user_indent from prev line.
159
160 my $first_time = 1; # First iteration of the loop?
161 my $suppress_line_start_match = 0; # Set to disable line start checks.
162
163 my @lines = split (/\n/, $text);
164 while (@lines) # Don't use `foreach' here, it won't work.
165 {
166 my $this_line = shift (@lines);
167 chomp $this_line;
168
169 if ($this_line =~ /^(\s+)/) {
170 $user_indent = $1;
171 }
172 else {
173 $user_indent = '';
174 }
175
176 # If it matches any of the line-start regexps, print a newline now...
177 if ($suppress_line_start_match)
178 {
179 $suppress_line_start_match = 0;
180 }
181 elsif (($this_line =~ /^(\s*)\*\s+[a-zA-Z0-9]/)
182 || ($this_line =~ /^(\s*)\* [a-zA-Z0-9_\.\/\+-]+/)
183 || ($this_line =~ /^(\s*)\([a-zA-Z0-9_\.\/\+-]+(\)|,\s*)/)
184 || ($this_line =~ /^(\s+)(\S+)/)
185 || ($this_line =~ /^(\s*)- +/)
186 || ($this_line =~ /^()\s*$/)
187 || ($this_line =~ /^(\s*)\*\) +/)
188 || ($this_line =~ /^(\s*)[a-zA-Z0-9](\)|\.|\:) +/))
189 {
190 $length_remaining = $max_line_length - (length ($user_indent));
191 }
192
193 # Now that any user_indent has been preserved, strip off leading
194 # whitespace, so up-folding has no ugly side-effects.
195 $this_line =~ s/^\s*//;
196
197 # Accumulate the line, and adjust parameters for next line.
198 my $this_len = length ($this_line);
199 if ($this_len == 0)
200 {
201 # Blank lines should cancel any user_indent level.
202 $user_indent = '';
203 $length_remaining = $max_line_length;
204 }
205 elsif ($this_len >= $length_remaining) # Line too long, try breaking it.
206 {
207 # Walk backwards from the end. At first acceptable spot, break
208 # a new line.
209 my $idx = $length_remaining - 1;
210 if ($idx < 0) { $idx = 0 };
211 while ($idx > 0)
212 {
213 if (substr ($this_line, $idx, 1) =~ /\s/)
214 {
215 my $line_now = substr ($this_line, 0, $idx);
216 my $next_line = substr ($this_line, $idx);
217 $this_line = $line_now;
218
219 # Clean whitespace off the end.
220 chomp $this_line;
221
222 # The current line is ready to be printed.
223 $this_line .= "\n${left_pad_str}";
224
225 # Make sure the next line is allowed full room.
226 $length_remaining = $max_line_length - (length ($user_indent));
227
228 # Strip next_line, but then preserve any user_indent.
229 $next_line =~ s/^\s*//;
230
231 # Sneak a peek at the user_indent of the upcoming line, so
232 # $next_line (which will now precede it) can inherit that
233 # indent level. Otherwise, use whatever user_indent level
234 # we currently have, which might be none.
235 my $next_next_line = shift (@lines);
236 if ((defined ($next_next_line)) && ($next_next_line =~ /^(\s+)/)) {
237 $next_line = $1 . $next_line if (defined ($1));
238 # $length_remaining = $max_line_length - (length ($1));
239 $next_next_line =~ s/^\s*//;
240 }
241 else {
242 $next_line = $user_indent . $next_line;
243 }
244 if (defined ($next_next_line)) {
245 unshift (@lines, $next_next_line);
246 }
247 unshift (@lines, $next_line);
248
249 # Our new next line might, coincidentally, begin with one of
250 # the line-start regexps, so we temporarily turn off
251 # sensitivity to that until we're past the line.
252 $suppress_line_start_match = 1;
253
254 last;
255 }
256 else
257 {
258 $idx--;
259 }
260 }
261
262 if ($idx == 0)
263 {
264 # We bottomed out because the line is longer than the
265 # available space. But that could be because the space is
266 # small, or because the line is longer than even the maximum
267 # possible space. Handle both cases below.
268
269 if ($length_remaining == ($max_line_length - (length ($user_indent))))
270 {
271 # The line is simply too long -- there is no hope of ever
272 # breaking it nicely, so just insert it verbatim, with
273 # appropriate padding.
274 $this_line = "\n${left_pad_str}${this_line}";
275 }
276 else
277 {
278 # Can't break it here, but may be able to on the next round...
279 unshift (@lines, $this_line);
280 $length_remaining = $max_line_length - (length ($user_indent));
281 $this_line = "\n${left_pad_str}";
282 }
283 }
284 }
285 else # $this_len < $length_remaining, so tack on what we can.
286 {
287 # Leave a note for the next iteration.
288 $length_remaining = $length_remaining - $this_len;
289
290 if ($this_line =~ /\.$/)
291 {
292 $this_line .= " ";
293 $length_remaining -= 2;
294 }
295 else # not a sentence end
296 {
297 $this_line .= " ";
298 $length_remaining -= 1;
299 }
300 }
301
302 # Unconditionally indicate that loop has run at least once.
303 $first_time = 0;
304
305 $wrapped_text .= "${user_indent}${this_line}";
306 }
307
308 # One last bit of padding.
309 $wrapped_text .= "\n";
310
311 return $wrapped_text;
312 }
313
314 # main
315
316 my @date;
317 my $author;
318 my @files;
319 my $comment;
320
321 my $state; # 0-header 1-comment 2-files
322 my $done = 0;
323
324 $state = 0;
325
326 # if reading from STDIN, we assume that we are
327 # getting git log as input
328 if (key_ready())
329 {
330
331 #my $dummyfh; # don't care about writing
332 #($fh,$dummyfh) = FileHandle::pipe;
333 $fh->fdopen(*STDIN, 'r');
334 }
335 else
336 {
337 $fh->open("LC_ALL=C git log --pretty --numstat --summary|")
338 or die("Cannot execute git log...$!\n");
339 }
340
341 while (my $_l = <$fh>) {
342 #print STDERR "debug ($state, " . (@date ? (strftime "%Y-%m-%d", @date) : "") . "): `$_'\n";
343 if ($state == 0) {
344 if ($_l =~ m,^Author: (.*),) {
345 $author = $1;
346 }
347 if ($_l =~ m,^Date: (.*),) {
348 @date = strptime($1);
349 }
350 $state = 1 if ($_l =~ m,^$, and $author and (@date+0>0));
351 } elsif ($state == 1) {
352 # * modifying our input text is a bad choice
353 # let's make a copy of it first, then we remove spaces
354 # * if we meet a "merge branch" statement, we need to start
355 # over and find a real entry
356 # Luis Mondesi <[email protected]>
357 my $_s = $_l;
358 $_s =~ s/^ //g;
359 if ($_s =~ m/^Merge branch/)
360 {
361 $state=0;
362 next;
363 }
364 $comment = $comment . $_s;
365 $state = 2 if ($_l =~ m,^$,);
366 } elsif ($state == 2) {
367 if ($_l =~ m,^([0-9]+)\t([0-9]+)\t(.*)$,) {
368 push @files, $3;
369 }
370 $done = 1 if ($_l =~ m,^$,);
371 }
372
373 if ($done) {
374 print (strftime "%Y-%m-%d $author\n\n", @date);
375
376 my $files = join (", ", @files);
377 $files = mywrap ("\t", "\t", "* $files"), ": ";
378
379 if (index($comment, EMPTY_LOG_MESSAGE) > -1 ) {
380 $comment = "[no log message]\n";
381 }
382
383 my $files_last_line_len = 0;
384 $files_last_line_len = last_line_len($files) + 1;
385 my $msg = wrap_log_entry($comment, "\t", 69-$files_last_line_len, 69);
386
387 $msg =~ s/[ \t]+\n/\n/g;
388
389 print "$files: $msg\n";
390
391 @date = ();
392 $author = "";
393 @files = ();
394 $comment = "";
395
396 $state = 0;
397 $done = 0;
398 }
399 }
400
401 if (@date + 0)
402 {
403 print (strftime "%Y-%m-%d $author\n\n", @date);
404 my $msg = wrap_log_entry($comment, "\t", 69, 69);
405 $msg =~ s/[ \t]+\n/\n/g;
406 print "\t* $msg\n";
407 }