#!/usr/local/bin/perl -w
###########################################
# log2csv - Convert git logs to CSV format
# Mike Schilli, 2010 (m@perlmeister.com)
###########################################
use strict;
use local::lib;
use Text::CSV;

my $logfile = "perl-git-log.txt.bz2";
my $csvfile = "perl-git-log.csv";

my $csv = Text::CSV_XS->new ( { binary => 1, eol => $/ } ) or
    die "Cannot use CSV: ", Text::CSV->error_diag();

open my $logfh, "bzip2 -dc $logfile |" or die "$logfile: $!";
open my $csvfh, ">$csvfile" or die "$csvfile: $!";

my($dummy, $author, $time, $committer);

$csv->print( $csvfh, ["time", "file", "author", "committer"] );

while( <$logfh> ) {
    if( /^commit/ ) {
        chomp;
        ($dummy, $author, $time, $committer) = split /,/, $_;
    } elsif( /^(\w)\s+(.*)/ ) {
        my $file = $2;
        $csv->print( $csvfh, [$time, $file, $author, $committer] ) or
          die "print failed: ", Text::CSV->error_diag();
    }
}

close $logfh or die "$logfile: $!";
close $csvfh or die "$csvfile: $!";
