File: git2cl

package info (click to toggle)
gerbv 2.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,332 kB
  • sloc: ansic: 25,151; sh: 3,585; lisp: 896; makefile: 436; cpp: 248; xml: 49; perl: 42
file content (523 lines) | stat: -rwxr-xr-x 14,459 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/bin/sh
exec perl -w -x $0 ${1+"$@"} # -*- mode: perl; perl-indent-level: 2; -*-
#!perl -w

# Copyright (C) 2007, 2008 Simon Josefsson <simon@josefsson.org>
# Copyright (C) 2007 Luis Mondesi <lemsx1@gmail.com>
# * calls git directly. To use it just:
#   cd ~/Project/my_git_repo; git2cl > ChangeLog
# * implements strptime()
# * fixes bugs in $comment parsing
#   - copy input before we remove leading spaces
#   - skip "merge branch" statements as they don't
#     have information about files (i.e. we never
#     go into $state 2)
#   - behaves like a pipe/filter if input is given from the CLI
#     else it calls git log by itself
#
# The functions mywrap, last_line_len, wrap_log_entry are derived from
# the cvs2cl tool, see <http://www.red-bean.com/cvs2cl/>:
# Copyright (C) 2001,2002,2003,2004 Martyn J. Pearce <fluffy@cpan.org>
# Copyright (C) 1999 Karl Fogel <kfogel@red-bean.com>
#
# git2cl is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# git2cl is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with git2cl; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

use strict;
use POSIX qw(strftime);
use Getopt::Long   qw( GetOptions );
use Text::Wrap qw(wrap);
use FileHandle;

use constant EMPTY_LOG_MESSAGE => '*** empty log message ***';

# Print debugging messages?
my $Debug = 0;

# Expand usernames to email addresses based on a map file?
my $User_Map_File = '';
my $User_Passwd_File;
my $Mail_Domain;
my $Obfuscate_Emails = 0;

# Just print usage message and exit?
my $Print_Usage = 0;

GetOptions('help|usage|h'   => \$Print_Usage,
	   'debug'          => \$Debug,         # unadvertised option, heh
	   'obfuscate|o'      => \$Obfuscate_Emails,
	   'usermap|U=s'    => \$User_Map_File, # not implemented yet
	   )
    or die "options parsing failed\n";

if ($Print_Usage) {
  &usage ();
  exit (0);
}

# -------------------------------------

sub usage {

  eval "use Pod::Usage qw( pod2usage )";

   if ( $@ ) {
    print <<'END';

* Pod::Usage was not found.  The formatting may be suboptimal.  Consider
  upgrading your Perl --- Pod::Usage is standard from 5.6 onwards, and
  versions of perl prior to 5.6 are getting rather rusty, now.  Alternatively,
  install Pod::Usage direct from CPAN.
END

    local $/ = undef;
    my $message = <DATA>;
    $message =~ s/^=(head1|item) //gm;
    $message =~ s/^=(over|back).*\n//gm;
    $message =~ s/\n{3,}/\n\n/g;
    print $message;
  } else {
    print "\n";
    pod2usage( -exitval => 'NOEXIT',
               -verbose => 1,
               -output  => \*STDOUT,
             );
  }

  return;
}

# this is a helper hash for stptime.
# Assumes you are calling 'git log ...' with LC_ALL=C
my %month = (
    'Jan'=>0,
    'Feb'=>1,
    'Mar'=>2,
    'Apr'=>3,
    'May'=>4,
    'Jun'=>5,
    'Jul'=>6,
    'Aug'=>7,
    'Sep'=>8,
    'Oct'=>9,
    'Nov'=>10,
    'Dec'=>11,
);

my $fh = new FileHandle;

sub key_ready
{
    my ($rin, $nfd);
    $rin = '';
    vec($rin, fileno(STDIN), 1) = 1;
    return $nfd = select($rin, undef, undef, 0);
}

sub strptime {
    my $str = shift;
    return undef if not defined $str;

    # we are parsing this format
    # Fri Oct 26 00:42:56 2007 -0400
    # to these fields
    # sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1
    # Luis Mondesi <lemsx1@gmail.com>
    my @date;
    if ($str =~ /([[:alpha:]]{3})\s+([[:alpha:]]{3})\s+([[:digit:]]{1,2})\s+([[:digit:]]{1,2}):([[:digit:]]{1,2}):([[:digit:]]{1,2})\s+([[:digit:]]{4})/){
        push(@date,$6,$5,$4,$3,$month{$2},($7 - 1900),-1,-1,-1);
    } else {
        die ("Cannot parse date '$str'\n'");
    }
    return @date;
}

sub mywrap {
    my ($indent1, $indent2, @text) = @_;
    # If incoming text looks preformatted, don't get clever
    my $text = Text::Wrap::wrap($indent1, $indent2, @text);
    if ( grep /^\s+/m, @text ) {
	return $text;
    }
    my @lines = split /\n/, $text;
    $indent2 =~ s!^((?: {8})+)!"\t" x (length($1)/8)!e;
    $lines[0] =~ s/^$indent1\s+/$indent1/;
    s/^$indent2\s+/$indent2/
	for @lines[1..$#lines];
    my $newtext = join "\n", @lines;
    $newtext .= "\n"
	if substr($text, -1) eq "\n";
    return $newtext;
}

sub last_line_len {
    my $files_list = shift;
    my @lines = split (/\n/, $files_list);
    my $last_line = pop (@lines);
    return length ($last_line);
}

# Obfuscate email addresses in the author information at the
# beginning of each log.
sub obfuscate_email {
    my $text = shift;                  # The text to wrap.
    my $text_out = '';
    my @words = split (/ /, $text);
    while (@words)   # Don't use `foreach' here, it won't work.
    {
	my $this_word = shift (@words);
	if ($this_word =~ /[a-zA-Z_\.0-9]@[a-zA-Z_\.0-9]/) {
	    $this_word =~ s/^[<\[]/* /;
	    $this_word =~ s/\./ dot /g;
	    $this_word =~ s/@/ AT /g;
	    $this_word =~ s/[>\]]/ */;
	}
	$text_out = $text_out . " " . $this_word;
      }

    return $text_out;
  }

# A custom wrap function, sensitive to some common constructs used in
# log entries.
sub wrap_log_entry {
    my $text = shift;                  # The text to wrap.
    my $left_pad_str = shift;          # String to pad with on the left.

    # These do NOT take left_pad_str into account:
    my $length_remaining = shift;      # Amount left on current line.
    my $max_line_length  = shift;      # Amount left for a blank line.

    my $wrapped_text = '';             # The accumulating wrapped entry.
    my $user_indent = '';              # Inherited user_indent from prev line.

    my $first_time = 1;                # First iteration of the loop?
    my $suppress_line_start_match = 0; # Set to disable line start checks.

    my @lines = split (/\n/, $text);
    while (@lines)   # Don't use `foreach' here, it won't work.
    {
	my $this_line = shift (@lines);
	chomp $this_line;

	if ($this_line =~ /^(\s+)/) {
	    $user_indent = $1;
	}
	else {
	    $user_indent = '';
	}

	# If it matches any of the line-start regexps, print a newline now...
	if ($suppress_line_start_match)
	{
	    $suppress_line_start_match = 0;
	}
	elsif (($this_line =~ /^(\s*)\*\s+[a-zA-Z0-9]/)
	       || ($this_line =~ /^(\s*)\* [a-zA-Z0-9_\.\/\+-]+/)
	       || ($this_line =~ /^(\s*)\([a-zA-Z0-9_\.\/\+-]+(\)|,\s*)/)
	       || ($this_line =~ /^(\s+)(\S+)/)
	       || ($this_line =~ /^(\s*)- +/)
	       || ($this_line =~ /^()\s*$/)
	       || ($this_line =~ /^(\s*)\*\) +/)
	       || ($this_line =~ /^(\s*)[a-zA-Z0-9](\)|\.|\:) +/))
	{
	    $length_remaining = $max_line_length - (length ($user_indent));
	}

	# Now that any user_indent has been preserved, strip off leading
	# whitespace, so up-folding has no ugly side-effects.
	$this_line =~ s/^\s*//;

        # obfuscate any emails found inside the log
	if ($Obfuscate_Emails && $this_line =~ /@/ ) {
	    $this_line = &obfuscate_email ($this_line);
	}

	# Accumulate the line, and adjust parameters for next line.
	my $this_len = length ($this_line);
	if ($this_len == 0)
	{
	    # Blank lines should cancel any user_indent level.
	    $user_indent = '';
	    $length_remaining = $max_line_length;
	}
	elsif ($this_len >= $length_remaining) # Line too long, try breaking it.
	{
	    # Walk backwards from the end.  At first acceptable spot, break
	    # a new line.
	    my $idx = $length_remaining - 1;
	    if ($idx < 0) { $idx = 0 };
	    while ($idx > 0)
	    {
		if (substr ($this_line, $idx, 1) =~ /\s/)
		{
		    my $line_now = substr ($this_line, 0, $idx);
		    my $next_line = substr ($this_line, $idx);
		    $this_line = $line_now;

		    # Clean whitespace off the end.
		    chomp $this_line;

		    # The current line is ready to be printed.
		    $this_line .= "\n${left_pad_str}";

		    # Make sure the next line is allowed full room.
		    $length_remaining = $max_line_length - (length ($user_indent));

		    # Strip next_line, but then preserve any user_indent.
		    $next_line =~ s/^\s*//;

		    # Sneak a peek at the user_indent of the upcoming line, so
		    # $next_line (which will now precede it) can inherit that
		    # indent level.  Otherwise, use whatever user_indent level
		    # we currently have, which might be none.
		    my $next_next_line = shift (@lines);
		    if ((defined ($next_next_line)) && ($next_next_line =~ /^(\s+)/)) {
			$next_line = $1 . $next_line if (defined ($1));
			# $length_remaining = $max_line_length - (length ($1));
			$next_next_line =~ s/^\s*//;
		    }
		    else {
			$next_line = $user_indent . $next_line;
		    }
		    if (defined ($next_next_line)) {
			unshift (@lines, $next_next_line);
		    }
		    unshift (@lines, $next_line);

		    # Our new next line might, coincidentally, begin with one of
		    # the line-start regexps, so we temporarily turn off
		    # sensitivity to that until we're past the line.
		    $suppress_line_start_match = 1;

		    last;
		}
		else
		{
		    $idx--;
		}
	    }

	    if ($idx == 0)
	    {
		# We bottomed out because the line is longer than the
		# available space.  But that could be because the space is
		# small, or because the line is longer than even the maximum
		# possible space.  Handle both cases below.

		if ($length_remaining == ($max_line_length - (length ($user_indent))))
		{
		    # The line is simply too long -- there is no hope of ever
		    # breaking it nicely, so just insert it verbatim, with
		    # appropriate padding.
		    $this_line = "\n${left_pad_str}${this_line}";
		}
		else
		{
		    # Can't break it here, but may be able to on the next round...
		    unshift (@lines, $this_line);
		    $length_remaining = $max_line_length - (length ($user_indent));
		    $this_line = "\n${left_pad_str}";
		}
	    }
	}
	else  # $this_len < $length_remaining, so tack on what we can.
	{
	    # Leave a note for the next iteration.
	    $length_remaining = $length_remaining - $this_len;

	    if ($this_line =~ /\.$/)
	    {
		$this_line .= "  ";
		$length_remaining -= 2;
	    }
	    else  # not a sentence end
	    {
		$this_line .= " ";
		$length_remaining -= 1;
	    }
	}

	# Unconditionally indicate that loop has run at least once.
	$first_time = 0;

	$wrapped_text .= "${user_indent}${this_line}";
    }

    # One last bit of padding.
    $wrapped_text .= "\n";

    return $wrapped_text;
}

# main


my @date;
my $author;
my @files;
my $comment;

my $state; # 0-header 1-comment 2-files
my $done = 0;

$state = 0;

# if reading from STDIN, we assume that we are
# getting git log as input
if (key_ready())
{

    #my $dummyfh; # don't care about writing
    #($fh,$dummyfh) = FileHandle::pipe;
    $fh->fdopen(*STDIN, 'r');
}
else
{
    $fh->open("LC_ALL=C git log --pretty --numstat --summary|")
	or die("Cannot execute git log...$!\n");
}

while (my $_l = <$fh>) {
    #print STDERR "debug ($state, " . (@date ? (strftime "%Y-%m-%d", @date) : "") . "): `$_'\n";
    if ($state == 0) {
	if ($_l =~ m,^Author: (.*),) {
	    $author = $1;
	    if ($Obfuscate_Emails) {
	      $author = &obfuscate_email ($author);
	    }
	}
	if ($_l =~ m,^Date: (.*),) {
	    @date = strptime($1);
	}
	$state = 1 if ($_l =~ m,^$, and $author and (@date+0>0));
    } elsif ($state == 1) {
        # * modifying our input text is a bad choice
        #   let's make a copy of it first, then we remove spaces
        # * if we meet a "merge branch" statement, we need to start
        #   over and find a real entry
        # Luis Mondesi <lemsx1@gmail.com>
        my $_s = $_l;
	$_s =~ s/^    //g;
        if ($_s =~ m/^Merge branch/)
        {
            $state=0;
            next;
        }
	$comment = $comment . $_s;
	$state = 2 if ($_l =~ m,^$,);
    } elsif ($state == 2) {
	if ($_l =~ m,^([0-9]+)\t([0-9]+)\t(.*)$,) {
	    push @files, $3;
	}
	$done = 1 if ($_l =~ m,^$,);
    }

    if ($done) {
	my $now = (strftime "%Y-%m-%d  $author\n\n", @date);
	$now =~ s/[ \t]+\n/\n/g;
	print "$now";

	my $files = join (", ", @files);
	$files = mywrap ("\t", "\t", "* $files");

	if (index($comment, EMPTY_LOG_MESSAGE) > -1 ) {
	    $comment = "[no log message]\n";
	}

	my $files_last_line_len = 0;
	$files_last_line_len = last_line_len($files) + 1;
	my $msg = wrap_log_entry($comment, "\t", 69-$files_last_line_len, 69);
	$msg = "$files: $msg\n";

	# clean up any whitespace at the end of lines
	$msg =~ s/[ \t]+\n/\n/g;

	print "$msg";

	@date = ();
	$author = "";
	@files = ();
	$comment = "";

	$state = 0;
	$done = 0;
    }
}

if (@date + 0)
{
    my $when = (strftime "%Y-%m-%d  $author\n\n", @date);
    $when =~ s/[ \t]+\n/\n/g;
    print "$when";

    my $msg = wrap_log_entry($comment, "\t", 69, 69);
    $msg = "\t* $msg\n";

    # clean up any whitespace at the end
    $msg =~ s/[ \t]+\n/\n/g;
    print "$msg";
}

__DATA__

=head1 NAME

cvs2cl.pl - convert cvs log messages to changelogs

=head1 SYNOPSIS

B<cvs2cl> [I<options>] [I<FILE1> [I<FILE2> ...]]

=head1 DESCRIPTION

cvs2cl produces a GNU-style ChangeLog for CVS-controlled sources by
running "cvs log" and parsing the output. Duplicate log messages get
unified in the Right Way.

The default output of cvs2cl is designed to be compact, formally unambiguous,
but still easy for humans to read.  It should be largely self-explanatory; the
one abbreviation that might not be obvious is "utags".  That stands for
"universal tags" -- a universal tag is one held by all the files in a given
change entry.

If you need output that's easy for a program to parse, use the B<--xml> option.
Note that with XML output, just about all available information is included
with each change entry, whether you asked for it or not, on the theory that
your parser can ignore anything it's not looking for.

If filenames are given as arguments cvs2cl only shows log information for the
named files.

=head1 OPTIONS

=over 4

=item B<-h>, B<-help>, B<--help>, B<-?>

Show a short help and exit.

=item B<--obfuscate>, B<-O>

Obfuscate email addresses in the logs.

=item -B<U> I<UFILE>, B<--usermap> I<UFILE>

Expand usernames to email addresses from I<UFILE>.
NOT IMPLEMENTED YET

=head1 SEE ALSO
git(1)
cvs2cl(1)