use warnings;
use strict;
+use Getopt::Long;
+use POSIX qw(strftime gmtime);
+use File::Basename qw(basename dirname);
+use Git;
+
+sub usage() {
+ print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
+ -l, --long
+ Show long rev (Defaults off)
+ -t, --time
+ Show raw timestamp (Defaults off)
+ -r, --rename
+ Follow renames (Defaults on).
+ -S, --rev-file revs-file
+ Use revs from revs-file instead of calling git-rev-list
+ -h, --help
+ This message.
+";
+
+ exit(1);
+}
-my $filename = shift @ARGV;
+our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file, $repo) = (0, 0, 1);
+
+my $rc = GetOptions( "long|l" => \$longrev,
+ "time|t" => \$rawtime,
+ "help|h" => \$help,
+ "rename|r" => \$rename,
+ "rev-file|S=s" => \$rev_file);
+if (!$rc or $help or !@ARGV) {
+ usage();
+}
+my $filename = shift @ARGV;
+if (@ARGV) {
+ $starting_rev = shift @ARGV;
+}
my @stack = (
{
- 'rev' => "HEAD",
+ 'rev' => defined $starting_rev ? $starting_rev : "HEAD",
'filename' => $filename,
},
);
-our (@lineoffsets, @pendinglineoffsets);
+$repo = Git->repository();
+
our @filelines = ();
-open(F,"<",$filename)
- or die "Failed to open filename: $!";
-while(<F>) {
- chomp;
- push @filelines, $_;
+if (defined $starting_rev) {
+ @filelines = git_cat_file($starting_rev, $filename);
+} else {
+ open(F,"<",$filename)
+ or die "Failed to open filename: $!";
+
+ while(<F>) {
+ chomp;
+ push @filelines, $_;
+ }
+ close(F);
+
}
-close(F);
-our $leftover_lines = @filelines;
+
our %revs;
our @revqueue;
our $head;
my ($rev, @parents) = @$revinst;
$head ||= $rev;
+ if (!defined($rev)) {
+ $rev = "";
+ }
$revs{$rev}{'filename'} = $bound->{'filename'};
if (scalar @parents > 0) {
$revs{$rev}{'parents'} = \@parents;
next;
}
+ if (!$rename) {
+ next;
+ }
+
my $newbound = find_parent_renames($rev, $bound->{'filename'});
if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
push @stack, $newbound;
}
}
push @revqueue, $head;
-init_claim($head);
-$revs{$head}{'lineoffsets'} = {};
+init_claim( defined $starting_rev ? $head : 'dirty');
+unless (defined $starting_rev) {
+ my %ident;
+ @ident{'author', 'author_email', 'author_date'} = $repo->ident('author');
+ my $diff = $repo->command_output_pipe('diff', '-R', 'HEAD', '--', $filename);
+ _git_diff_parse($diff, $head, "dirty", %ident);
+ $repo->command_close_pipe($diff);
+}
handle_rev();
my ($output, $rev, $committer, $date);
if (ref $l eq 'ARRAY') {
($output, $rev, $committer, $date) = @$l;
- if (length($rev) > 8) {
+ if (!$longrev && length($rev) > 8) {
$rev = substr($rev,0,8);
}
} else {
($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
}
- printf("(%8s %10s %10s %d)%s\n", $rev, $committer, $date, $i++, $output);
+ printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
+ format_date($date), ++$i, $output);
}
sub init_claim {
my ($rev) = @_;
- my %revinfo = git_commit_info($rev);
for (my $i = 0; $i < @filelines; $i++) {
$filelines[$i] = [ $filelines[$i], '', '', '', 1];
# line,
sub handle_rev {
my $i = 0;
+ my %seen;
while (my $rev = shift @revqueue) {
+ next if $seen{$rev}++;
my %revinfo = git_commit_info($rev);
sub git_rev_list {
my ($rev, $file) = @_;
- open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)
- or die "Failed to exec git-rev-list: $!";
+ my $revlist;
+ if ($rev_file) {
+ open($revlist, '<' . $rev_file)
+ or die "Failed to open $rev_file : $!";
+ } else {
+ $revlist = $repo->command_output_pipe('rev-list', '--parents', '--remove-empty', $rev, '--', $file);
+ }
my @revs;
- while(my $line = <P>) {
+ while(my $line = <$revlist>) {
chomp $line;
my ($rev, @parents) = split /\s+/, $line;
push @revs, [ $rev, @parents ];
}
- close(P);
+ $repo->command_close_pipe($revlist);
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
return @revs;
sub find_parent_renames {
my ($rev, $file) = @_;
- open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
- or die "Failed to exec git-diff: $!";
+ my $patch = $repo->command_output_pipe('diff-tree', '-M50', '-r', '--name-status', '-z', $rev);
local $/ = "\0";
my %bound;
- my $junk = <P>;
- while (my $change = <P>) {
+ my $junk = <$patch>;
+ while (my $change = <$patch>) {
chomp $change;
- my $filename = <P>;
+ my $filename = <$patch>;
+ if (!defined $filename) {
+ next;
+ }
chomp $filename;
if ($change =~ m/^[AMD]$/ ) {
next;
} elsif ($change =~ m/^R/ ) {
my $oldfilename = $filename;
- $filename = <P>;
+ $filename = <$patch>;
chomp $filename;
if ( $file eq $filename ) {
my $parent = git_find_parent($rev, $oldfilename);
}
}
}
- close(P);
+ $repo->command_close_pipe($patch);
return \%bound;
}
sub git_find_parent {
my ($rev, $filename) = @_;
- open(REVPARENT,"-|","git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
- or die "Failed to open git-rev-list to find a single parent: $!";
-
- my $parentline = <REVPARENT>;
- chomp $parentline;
- my ($revfound,$parent) = split m/\s+/, $parentline;
-
- close(REVPARENT);
+ my $parentline = $repo->command_oneline('rev-list', '--remove-empty',
+ '--parents', '--max-count=1', $rev, '--', $filename);
+ my ($revfound, $parent) = split m/\s+/, $parentline;
return $parent;
}
sub git_diff_parse {
my ($parent, $rev, %revinfo) = @_;
- my ($ri, $pi) = (0,0);
- open(DIFF,"-|","git-diff-tree","-M","-p",$rev,$parent,"--",
- $revs{$rev}{'filename'}, $revs{$parent}{'filename'})
- or die "Failed to call git-diff for annotation: $!";
+ my $diff = $repo->command_output_pipe('diff-tree', '-M', '-p',
+ $rev, $parent, '--',
+ $revs{$rev}{'filename'}, $revs{$parent}{'filename'});
+
+ _git_diff_parse($diff, $parent, $rev, %revinfo);
+
+ $repo->command_close_pipe($diff);
+}
+
+sub _git_diff_parse {
+ my ($diff, $parent, $rev, %revinfo) = @_;
+ my ($ri, $pi) = (0,0);
my $slines = $revs{$rev}{'lines'};
my @plines;
my $gotheader = 0;
- my ($remstart, $remlength, $addstart, $addlength);
- my ($hunk_start, $hunk_index, $hunk_adds);
- while(<DIFF>) {
+ my ($remstart);
+ my ($hunk_start, $hunk_index);
+ while(<$diff>) {
chomp;
if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
- ($remstart, $remlength, $addstart, $addlength) = ($1, $2, $3, $4);
+ $remstart = $1;
# Adjust for 0-based arrays
$remstart--;
- $addstart--;
# Reinit hunk tracking.
$hunk_start = $remstart;
$hunk_index = 0;
}
$ri++;
+ } elsif (m/^\\/) {
+ ;
+ # Skip \No newline at end of file.
+ # But this can be internationalized, so only look
+ # for an initial \
+
} else {
if (substr($_,1) ne get_line($slines,$ri) ) {
die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
}
$hunk_index++;
}
- close(DIFF);
for (my $i = $ri; $i < @{$slines} ; $i++) {
push @plines, $slines->[$ri++];
}
}
sub git_cat_file {
- my ($parent, $filename) = @_;
- return () unless defined $parent && defined $filename;
- my $blobline = `git-ls-tree $parent $filename`;
- my ($mode, $type, $blob, $tfilename) = split(/\s+/, $blobline, 4);
+ my ($rev, $filename) = @_;
+ return () unless defined $rev && defined $filename;
- open(C,"-|","git-cat-file", "blob", $blob)
- or die "Failed to git-cat-file blob $blob (rev $parent, file $filename): " . $!;
+ my $blob = git_ls_tree($rev, $filename);
+ die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
- my @lines;
- while(<C>) {
+ my @lines = split(/\n/, $repo->get_object('blob', $blob));
+ pop @lines unless $lines[$#lines]; # Trailing newline
+ return @lines;
+}
+
+sub git_ls_tree {
+ my ($rev, $filename) = @_;
+
+ my $lstree = $repo->command_output_pipe('ls-tree', $rev, $filename);
+ my ($mode, $type, $blob, $tfilename);
+ while(<$lstree>) {
chomp;
- push @lines, $_;
+ ($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
+ last if ($tfilename eq $filename);
}
- close(C);
+ $repo->command_close_pipe($lstree);
- return @lines;
+ return $blob if ($tfilename eq $filename);
+ die "git-ls-tree failed to find blob for $filename";
}
+
sub claim_line {
my ($floffset, $rev, $lines, %revinfo) = @_;
my $oline = get_line($lines, $floffset);
sub git_commit_info {
my ($rev) = @_;
- open(COMMIT, "-|","git-cat-file", "commit", $rev)
- or die "Failed to call git-cat-file: $!";
+ my $commit = $repo->get_object('commit', $rev);
my %info;
- while(<COMMIT>) {
- chomp;
- last if (length $_ == 0);
-
- if (m/^author (.*) <(.*)> (.*)$/) {
- $info{'author'} = $1;
- $info{'author_email'} = $2;
- $info{'author_date'} = $3;
- } elsif (m/^committer (.*) <(.*)> (.*)$/) {
- $info{'committer'} = $1;
- $info{'committer_email'} = $2;
- $info{'committer_date'} = $3;
+ while ($commit =~ /(.*?)\n/g) {
+ my $line = $1;
+ if ($line =~ s/^author //) {
+ @info{'author', 'author_email', 'author_date'} = $repo->ident($line);
+ } elsif ($line =~ s/^committer//) {
+ @info{'committer', 'committer_email', 'committer_date'} = $repo->ident($line);
}
}
- close(COMMIT);
return %info;
}
+
+sub format_date {
+ if ($rawtime) {
+ return $_[0];
+ }
+ my ($timestamp, $timezone) = split(' ', $_[0]);
+ my $minutes = abs($timezone);
+ $minutes = int($minutes / 100) * 60 + ($minutes % 100);
+ if ($timezone < 0) {
+ $minutes = -$minutes;
+ }
+ my $t = $timestamp + $minutes * 60;
+ return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
+}