Blame autobuild/git2cl

168141
#!/usr/bin/perl
168141
168141
# Copyright (C) 2007, 2008 Simon Josefsson <simon@josefsson.org></simon@josefsson.org>
168141
# Copyright (C) 2007 Luis Mondesi <lemsx1@gmail.com></lemsx1@gmail.com>
168141
# * calls git directly. To use it just: 
168141
#   cd ~/Project/my_git_repo; git2cl > ChangeLog
168141
# * implements strptime()
168141
# * fixes bugs in $comment parsing
168141
#   - copy input before we remove leading spaces
168141
#   - skip "merge branch" statements as they don't
168141
#     have information about files (i.e. we never
168141
#     go into $state 2)
168141
#   - behaves like a pipe/filter if input is given from the CLI
168141
#     else it calls git log by itself
168141
#
168141
# The functions mywrap, last_line_len, wrap_log_entry are derived from
168141
# the cvs2cl tool, see <http: cvs2cl="" www.red-bean.com="">:</http:>
168141
# Copyright (C) 2001,2002,2003,2004 Martyn J. Pearce <fluffy@cpan.org></fluffy@cpan.org>
168141
# Copyright (C) 1999 Karl Fogel <kfogel@red-bean.com></kfogel@red-bean.com>
168141
#
168141
# git2cl is free software; you can redistribute it and/or modify it
168141
# under the terms of the GNU General Public License as published by
168141
# the Free Software Foundation; either version 2, or (at your option)
168141
# any later version.
168141
#
168141
# git2cl is distributed in the hope that it will be useful, but
168141
# WITHOUT ANY WARRANTY; without even the implied warranty of
168141
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168141
# General Public License for more details.
168141
#
168141
# You should have received a copy of the GNU General Public License
168141
# along with git2cl; see the file COPYING.  If not, write to the Free
168141
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
168141
# 02111-1307, USA.
168141
168141
use strict;
168141
use POSIX qw(strftime);
168141
use Text::Wrap qw(wrap);
168141
use FileHandle;
168141
168141
use constant EMPTY_LOG_MESSAGE => '*** empty log message ***';
168141
168141
# this is a helper hash for stptime.
168141
# Assumes you are calling 'git log ...' with LC_ALL=C
168141
my %month = (
168141
    'Jan'=>0,
168141
    'Feb'=>1,
168141
    'Mar'=>2,
168141
    'Apr'=>3,
168141
    'May'=>4,
168141
    'Jun'=>5,
168141
    'Jul'=>6,
168141
    'Aug'=>7,
168141
    'Sep'=>8,
168141
    'Oct'=>9,
168141
    'Nov'=>10,
168141
    'Dec'=>11,
168141
);
168141
168141
my $fh = new FileHandle;
168141
168141
sub key_ready
168141
{
168141
    my ($rin, $nfd);
168141
    vec($rin, fileno(STDIN), 1) = 1;
168141
    return $nfd = select($rin, undef, undef, 0);
168141
}
168141
168141
sub strptime {
168141
    my $str = shift;
168141
    return undef if not defined $str;
168141
168141
    # we are parsing this format
168141
    # Fri Oct 26 00:42:56 2007 -0400
168141
    # to these fields
168141
    # sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1
168141
    # Luis Mondesi <lemsx1@gmail.com></lemsx1@gmail.com>
168141
    my @date;
168141
    if ($str =~ /([[:alpha:]]{3})\s+([[:alpha:]]{3})\s+([[:digit:]]{1,2})\s+([[:digit:]]{1,2}):([[:digit:]]{1,2}):([[:digit:]]{1,2})\s+([[:digit:]]{4})/){
168141
        push(@date,$6,$5,$4,$3,$month{$2},($7 - 1900),-1,-1,-1);
168141
    } else {
168141
        die ("Cannot parse date '$str'\n'");
168141
    }
168141
    return @date;
168141
}
168141
168141
sub mywrap {
168141
    my ($indent1, $indent2, @text) = @_;
168141
    # If incoming text looks preformatted, don't get clever
168141
    my $text = Text::Wrap::wrap($indent1, $indent2, @text);
168141
    if ( grep /^\s+/m, @text ) {
168141
	return $text;
168141
    }
168141
    my @lines = split /\n/, $text;
168141
    $indent2 =~ s!^((?: {8})+)!"\t" x (length($1)/8)!e;
168141
    $lines[0] =~ s/^$indent1\s+/$indent1/;
168141
    s/^$indent2\s+/$indent2/
168141
	for @lines[1..$#lines];
168141
    my $newtext = join "\n", @lines;
168141
    $newtext .= "\n"
168141
	if substr($text, -1) eq "\n";
168141
    return $newtext;
168141
}
168141
168141
sub last_line_len {
168141
    my $files_list = shift;
168141
    my @lines = split (/\n/, $files_list);
168141
    my $last_line = pop (@lines);
168141
    return length ($last_line);
168141
}
168141
168141
# A custom wrap function, sensitive to some common constructs used in
168141
# log entries.
168141
sub wrap_log_entry {
168141
    my $text = shift;                  # The text to wrap.
168141
    my $left_pad_str = shift;          # String to pad with on the left.
168141
168141
    # These do NOT take left_pad_str into account:
168141
    my $length_remaining = shift;      # Amount left on current line.
168141
    my $max_line_length  = shift;      # Amount left for a blank line.
168141
168141
    my $wrapped_text = '';             # The accumulating wrapped entry.
168141
    my $user_indent = '';              # Inherited user_indent from prev line.
168141
168141
    my $first_time = 1;                # First iteration of the loop?
168141
    my $suppress_line_start_match = 0; # Set to disable line start checks.
168141
168141
    my @lines = split (/\n/, $text);
168141
    while (@lines)   # Don't use `foreach' here, it won't work.
168141
    {
168141
	my $this_line = shift (@lines);
168141
	chomp $this_line;
168141
168141
	if ($this_line =~ /^(\s+)/) {
168141
	    $user_indent = $1;
168141
	}
168141
	else {
168141
	    $user_indent = '';
168141
	}
168141
168141
	# If it matches any of the line-start regexps, print a newline now...
168141
	if ($suppress_line_start_match)
168141
	{
168141
	    $suppress_line_start_match = 0;
168141
	}
168141
	elsif (($this_line =~ /^(\s*)\*\s+[a-zA-Z0-9]/)
168141
	       || ($this_line =~ /^(\s*)\* [a-zA-Z0-9_\.\/\+-]+/)
168141
	       || ($this_line =~ /^(\s*)\([a-zA-Z0-9_\.\/\+-]+(\)|,\s*)/)
168141
	       || ($this_line =~ /^(\s+)(\S+)/)
168141
	       || ($this_line =~ /^(\s*)- +/)
168141
	       || ($this_line =~ /^()\s*$/)
168141
	       || ($this_line =~ /^(\s*)\*\) +/)
168141
	       || ($this_line =~ /^(\s*)[a-zA-Z0-9](\)|\.|\:) +/))
168141
	{
168141
	    $length_remaining = $max_line_length - (length ($user_indent));
168141
	}
168141
168141
	# Now that any user_indent has been preserved, strip off leading
168141
	# whitespace, so up-folding has no ugly side-effects.
168141
	$this_line =~ s/^\s*//;
168141
168141
	# Accumulate the line, and adjust parameters for next line.
168141
	my $this_len = length ($this_line);
168141
	if ($this_len == 0)
168141
	{
168141
	    # Blank lines should cancel any user_indent level.
168141
	    $user_indent = '';
168141
	    $length_remaining = $max_line_length;
168141
	}
168141
	elsif ($this_len >= $length_remaining) # Line too long, try breaking it.
168141
	{
168141
	    # Walk backwards from the end.  At first acceptable spot, break
168141
	    # a new line.
168141
	    my $idx = $length_remaining - 1;
168141
	    if ($idx < 0) { $idx = 0 };
168141
	    while ($idx > 0)
168141
	    {
168141
		if (substr ($this_line, $idx, 1) =~ /\s/)
168141
		{
168141
		    my $line_now = substr ($this_line, 0, $idx);
168141
		    my $next_line = substr ($this_line, $idx);
168141
		    $this_line = $line_now;
168141
168141
		    # Clean whitespace off the end.
168141
		    chomp $this_line;
168141
168141
		    # The current line is ready to be printed.
168141
		    $this_line .= "\n${left_pad_str}";
168141
168141
		    # Make sure the next line is allowed full room.
168141
		    $length_remaining = $max_line_length - (length ($user_indent));
168141
168141
		    # Strip next_line, but then preserve any user_indent.
168141
		    $next_line =~ s/^\s*//;
168141
168141
		    # Sneak a peek at the user_indent of the upcoming line, so
168141
		    # $next_line (which will now precede it) can inherit that
168141
		    # indent level.  Otherwise, use whatever user_indent level
168141
		    # we currently have, which might be none.
168141
		    my $next_next_line = shift (@lines);
168141
		    if ((defined ($next_next_line)) && ($next_next_line =~ /^(\s+)/)) {
168141
			$next_line = $1 . $next_line if (defined ($1));
168141
			# $length_remaining = $max_line_length - (length ($1));
168141
			$next_next_line =~ s/^\s*//;
168141
		    }
168141
		    else {
168141
			$next_line = $user_indent . $next_line;
168141
		    }
168141
		    if (defined ($next_next_line)) {
168141
			unshift (@lines, $next_next_line);
168141
		    }
168141
		    unshift (@lines, $next_line);
168141
168141
		    # Our new next line might, coincidentally, begin with one of
168141
		    # the line-start regexps, so we temporarily turn off
168141
		    # sensitivity to that until we're past the line.
168141
		    $suppress_line_start_match = 1;
168141
168141
		    last;
168141
		}
168141
		else
168141
		{
168141
		    $idx--;
168141
		}
168141
	    }
168141
168141
	    if ($idx == 0)
168141
	    {
168141
		# We bottomed out because the line is longer than the
168141
		# available space.  But that could be because the space is
168141
		# small, or because the line is longer than even the maximum
168141
		# possible space.  Handle both cases below.
168141
168141
		if ($length_remaining == ($max_line_length - (length ($user_indent))))
168141
		{
168141
		    # The line is simply too long -- there is no hope of ever
168141
		    # breaking it nicely, so just insert it verbatim, with
168141
		    # appropriate padding.
168141
		    $this_line = "\n${left_pad_str}${this_line}";
168141
		}
168141
		else
168141
		{
168141
		    # Can't break it here, but may be able to on the next round...
168141
		    unshift (@lines, $this_line);
168141
		    $length_remaining = $max_line_length - (length ($user_indent));
168141
		    $this_line = "\n${left_pad_str}";
168141
		}
168141
	    }
168141
	}
168141
	else  # $this_len < $length_remaining, so tack on what we can.
168141
	{
168141
	    # Leave a note for the next iteration.
168141
	    $length_remaining = $length_remaining - $this_len;
168141
168141
	    if ($this_line =~ /\.$/)
168141
	    {
168141
		$this_line .= "  ";
168141
		$length_remaining -= 2;
168141
	    }
168141
	    else  # not a sentence end
168141
	    {
168141
		$this_line .= " ";
168141
		$length_remaining -= 1;
168141
	    }
168141
	}
168141
168141
	# Unconditionally indicate that loop has run at least once.
168141
	$first_time = 0;
168141
168141
	$wrapped_text .= "${user_indent}${this_line}";
168141
    }
168141
168141
    # One last bit of padding.
168141
    $wrapped_text .= "\n";
168141
168141
    return $wrapped_text;
168141
}
168141
168141
# main
168141
168141
my @date;
168141
my $author;
168141
my @files;
168141
my $comment;
168141
168141
my $state; # 0-header 1-comment 2-files
168141
my $done = 0;
168141
168141
$state = 0;
168141
168141
# if reading from STDIN, we assume that we are
168141
# getting git log as input
168141
if (key_ready())
168141
{
168141
168141
    #my $dummyfh; # don't care about writing
168141
    #($fh,$dummyfh) = FileHandle::pipe;
168141
    $fh->fdopen(*STDIN, 'r');
168141
}
168141
else
168141
{
168141
    $fh->open("LC_ALL=C git log --pretty --numstat --summary . |")
168141
	or die("Cannot execute git log...$!\n");
168141
}
168141
168141
while (my $_l = <$fh>) {
168141
    #print STDERR "debug ($state, " . (@date ? (strftime "%Y-%m-%d", @date) : "") . "): `$_'\n";
168141
    if ($state == 0) {
168141
	if ($_l =~ m,^Author: (.*),) {
168141
	    $author = $1;
168141
	}
168141
	if ($_l =~ m,^Date: (.*),) {
168141
	    @date = strptime($1);
168141
	}
168141
	$state = 1 if ($_l =~ m,^$, and $author and (@date+0>0));
168141
    } elsif ($state == 1) {
168141
        # * modifying our input text is a bad choice
168141
        #   let's make a copy of it first, then we remove spaces 
168141
        # * if we meet a "merge branch" statement, we need to start
168141
        #   over and find a real entry
168141
        # Luis Mondesi <lemsx1@gmail.com></lemsx1@gmail.com>
168141
        my $_s = $_l;
168141
	$_s =~ s/^    //g;
168141
        if ($_s =~ m/^Merge branch/)
168141
        {
168141
            $state=0;
168141
            next;
168141
        }
168141
	$comment = $comment . $_s;
168141
	$state = 2 if ($_l =~ m,^$,);
168141
    } elsif ($state == 2) {
168141
	if ($_l =~ m,^([0-9]+)\t([0-9]+)\t(.*)$,) {
168141
	    push @files, $3;
168141
	}
168141
	$done = 1 if ($_l =~ m,^$,);
168141
    }
168141
168141
    if ($done) {
168141
	print (strftime "%Y-%m-%d  $author\n\n", @date);
168141
168141
	my $files = join (", ", @files);
168141
	$files = mywrap ("\t", "\t", "* $files"), ": ";
168141
168141
	if (index($comment, EMPTY_LOG_MESSAGE) > -1 ) {
168141
	    $comment = "[no log message]\n";
168141
	}
168141
168141
	my $files_last_line_len = 0;
168141
	$files_last_line_len = last_line_len($files) + 1;
168141
	my $msg = wrap_log_entry($comment, "\t", 69-$files_last_line_len, 69);
168141
168141
	$msg =~ s/[ \t]+\n/\n/g;
168141
168141
	print "$files: $msg\n";
168141
168141
	@date = ();
168141
	$author = "";
168141
	@files = ();
168141
	$comment = "";
168141
168141
	$state = 0;
168141
	$done = 0;
168141
    }
168141
}
168141
168141
if (@date + 0)
168141
{
168141
    print (strftime "%Y-%m-%d  $author\n\n", @date);
168141
    my $msg = wrap_log_entry($comment, "\t", 69, 69);
168141
    $msg =~ s/[ \t]+\n/\n/g;
168141
    print "\t* $msg\n";
168141
}