2006-02-20 18:46:09 +08:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Copyright 2006, Ryan Anderson <ryan@michonline.com>
|
|
|
|
#
|
|
|
|
# GPL v2 (See COPYING)
|
|
|
|
#
|
|
|
|
# This file is licensed under the GPL v2, or a later version
|
|
|
|
# at the discretion of Linus Torvalds.
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2006-02-26 09:48:33 +08:00
|
|
|
use Getopt::Long;
|
2006-02-20 23:20:10 +08:00
|
|
|
use POSIX qw(strftime gmtime);
|
2006-04-28 16:41:19 +08:00
|
|
|
use File::Basename qw(basename dirname);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-02-20 23:20:10 +08:00
|
|
|
sub usage() {
|
2006-04-28 16:41:19 +08:00
|
|
|
print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
|
2006-02-26 09:48:33 +08:00
|
|
|
-l, --long
|
|
|
|
Show long rev (Defaults off)
|
2006-03-02 17:50:09 +08:00
|
|
|
-t, --time
|
|
|
|
Show raw timestamp (Defaults off)
|
2006-02-26 09:48:33 +08:00
|
|
|
-r, --rename
|
|
|
|
Follow renames (Defaults on).
|
|
|
|
-S, --rev-file revs-file
|
2006-03-11 15:39:28 +08:00
|
|
|
Use revs from revs-file instead of calling git-rev-list
|
2006-02-26 09:48:33 +08:00
|
|
|
-h, --help
|
|
|
|
This message.
|
2006-04-28 16:41:19 +08:00
|
|
|
";
|
2006-02-20 23:20:10 +08:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-03-02 17:50:09 +08:00
|
|
|
our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
|
2006-02-26 09:48:33 +08:00
|
|
|
|
|
|
|
my $rc = GetOptions( "long|l" => \$longrev,
|
2006-03-02 17:50:09 +08:00
|
|
|
"time|t" => \$rawtime,
|
2006-02-26 09:48:33 +08:00
|
|
|
"help|h" => \$help,
|
|
|
|
"rename|r" => \$rename,
|
2006-03-02 12:24:30 +08:00
|
|
|
"rev-file|S=s" => \$rev_file);
|
2006-04-28 16:41:19 +08:00
|
|
|
if (!$rc or $help or !@ARGV) {
|
2006-02-26 09:48:33 +08:00
|
|
|
usage();
|
|
|
|
}
|
2006-02-20 23:20:10 +08:00
|
|
|
|
|
|
|
my $filename = shift @ARGV;
|
2006-02-26 09:48:33 +08:00
|
|
|
if (@ARGV) {
|
|
|
|
$starting_rev = shift @ARGV;
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
my @stack = (
|
|
|
|
{
|
2006-02-26 09:48:33 +08:00
|
|
|
'rev' => defined $starting_rev ? $starting_rev : "HEAD",
|
2006-02-20 18:46:09 +08:00
|
|
|
'filename' => $filename,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
our @filelines = ();
|
|
|
|
|
2006-02-26 09:48:33 +08:00
|
|
|
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);
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
}
|
2006-02-26 09:48:33 +08:00
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
our %revs;
|
|
|
|
our @revqueue;
|
|
|
|
our $head;
|
|
|
|
|
|
|
|
my $revsprocessed = 0;
|
|
|
|
while (my $bound = pop @stack) {
|
|
|
|
my @revisions = git_rev_list($bound->{'rev'}, $bound->{'filename'});
|
|
|
|
foreach my $revinst (@revisions) {
|
|
|
|
my ($rev, @parents) = @$revinst;
|
|
|
|
$head ||= $rev;
|
|
|
|
|
2006-02-20 23:20:10 +08:00
|
|
|
if (!defined($rev)) {
|
|
|
|
$rev = "";
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
$revs{$rev}{'filename'} = $bound->{'filename'};
|
|
|
|
if (scalar @parents > 0) {
|
|
|
|
$revs{$rev}{'parents'} = \@parents;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2006-02-26 09:48:33 +08:00
|
|
|
if (!$rename) {
|
2006-02-20 23:20:10 +08:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
my $newbound = find_parent_renames($rev, $bound->{'filename'});
|
|
|
|
if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
|
|
|
|
push @stack, $newbound;
|
|
|
|
$revs{$rev}{'parents'} = [$newbound->{'rev'}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
push @revqueue, $head;
|
2006-03-06 10:43:53 +08:00
|
|
|
init_claim( defined $starting_rev ? $head : 'dirty');
|
2006-02-26 09:48:33 +08:00
|
|
|
unless (defined $starting_rev) {
|
2006-07-04 09:30:02 +08:00
|
|
|
my $diff = open_pipe("git","diff","HEAD", "--",$filename)
|
2006-02-26 09:48:33 +08:00
|
|
|
or die "Failed to call git diff to check for dirty state: $!";
|
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
_git_diff_parse($diff, [$head], "dirty", (
|
2006-02-26 09:48:33 +08:00
|
|
|
'author' => gitvar_name("GIT_AUTHOR_IDENT"),
|
|
|
|
'author_date' => sprintf("%s +0000",time()),
|
|
|
|
)
|
|
|
|
);
|
2006-02-26 11:02:05 +08:00
|
|
|
close($diff);
|
2006-02-26 09:48:33 +08:00
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
handle_rev();
|
|
|
|
|
|
|
|
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $l (@filelines) {
|
|
|
|
my ($output, $rev, $committer, $date);
|
|
|
|
if (ref $l eq 'ARRAY') {
|
|
|
|
($output, $rev, $committer, $date) = @$l;
|
2006-02-26 09:48:33 +08:00
|
|
|
if (!$longrev && length($rev) > 8) {
|
2006-02-20 18:46:09 +08:00
|
|
|
$rev = substr($rev,0,8);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$output = $l;
|
|
|
|
($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
|
|
|
|
}
|
|
|
|
|
2006-02-20 23:20:10 +08:00
|
|
|
printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
|
2006-03-03 07:27:31 +08:00
|
|
|
format_date($date), ++$i, $output);
|
2006-02-20 18:46:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub init_claim {
|
|
|
|
my ($rev) = @_;
|
|
|
|
for (my $i = 0; $i < @filelines; $i++) {
|
|
|
|
$filelines[$i] = [ $filelines[$i], '', '', '', 1];
|
|
|
|
# line,
|
|
|
|
# rev,
|
|
|
|
# author,
|
|
|
|
# date,
|
|
|
|
# 1 <-- belongs to the original file.
|
|
|
|
}
|
|
|
|
$revs{$rev}{'lines'} = \@filelines;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub handle_rev {
|
2006-08-07 20:11:24 +08:00
|
|
|
my $revseen = 0;
|
2006-02-26 09:48:33 +08:00
|
|
|
my %seen;
|
2006-02-20 18:46:09 +08:00
|
|
|
while (my $rev = shift @revqueue) {
|
2006-02-26 09:48:33 +08:00
|
|
|
next if $seen{$rev}++;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
my %revinfo = git_commit_info($rev);
|
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
if (exists $revs{$rev}{parents} &&
|
|
|
|
scalar @{$revs{$rev}{parents}} != 0) {
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
git_diff_parse($revs{$rev}{'parents'}, $rev, %revinfo);
|
|
|
|
push @revqueue, @{$revs{$rev}{'parents'}};
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
} else {
|
2006-02-20 18:46:09 +08:00
|
|
|
# We must be at the initial rev here, so claim everything that is left.
|
|
|
|
for (my $i = 0; $i < @{$revs{$rev}{lines}}; $i++) {
|
|
|
|
if (ref ${$revs{$rev}{lines}}[$i] eq '' || ${$revs{$rev}{lines}}[$i][1] eq '') {
|
|
|
|
claim_line($i, $rev, $revs{$rev}{lines}, %revinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub git_rev_list {
|
|
|
|
my ($rev, $file) = @_;
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $revlist;
|
2006-02-26 09:48:33 +08:00
|
|
|
if ($rev_file) {
|
2006-03-02 12:24:30 +08:00
|
|
|
open($revlist, '<' . $rev_file)
|
|
|
|
or die "Failed to open $rev_file : $!";
|
2006-02-20 23:20:10 +08:00
|
|
|
} else {
|
2006-02-26 11:02:05 +08:00
|
|
|
$revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
|
2006-02-20 23:20:10 +08:00
|
|
|
or die "Failed to exec git-rev-list: $!";
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
my @revs;
|
2006-02-26 11:02:05 +08:00
|
|
|
while(my $line = <$revlist>) {
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp $line;
|
|
|
|
my ($rev, @parents) = split /\s+/, $line;
|
|
|
|
push @revs, [ $rev, @parents ];
|
|
|
|
}
|
2006-02-26 11:02:05 +08:00
|
|
|
close($revlist);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
|
|
|
|
return @revs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub find_parent_renames {
|
|
|
|
my ($rev, $file) = @_;
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $patch = open_pipe("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
|
2006-02-20 18:46:09 +08:00
|
|
|
or die "Failed to exec git-diff: $!";
|
|
|
|
|
|
|
|
local $/ = "\0";
|
|
|
|
my %bound;
|
2006-02-26 11:02:05 +08:00
|
|
|
my $junk = <$patch>;
|
|
|
|
while (my $change = <$patch>) {
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp $change;
|
2006-02-26 11:02:05 +08:00
|
|
|
my $filename = <$patch>;
|
2006-04-28 16:42:28 +08:00
|
|
|
if (!defined $filename) {
|
|
|
|
next;
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp $filename;
|
|
|
|
|
|
|
|
if ($change =~ m/^[AMD]$/ ) {
|
|
|
|
next;
|
|
|
|
} elsif ($change =~ m/^R/ ) {
|
|
|
|
my $oldfilename = $filename;
|
2006-02-26 11:02:05 +08:00
|
|
|
$filename = <$patch>;
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp $filename;
|
|
|
|
if ( $file eq $filename ) {
|
|
|
|
my $parent = git_find_parent($rev, $oldfilename);
|
|
|
|
@bound{'rev','filename'} = ($parent, $oldfilename);
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-26 11:02:05 +08:00
|
|
|
close($patch);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
return \%bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub git_find_parent {
|
|
|
|
my ($rev, $filename) = @_;
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
|
2006-02-20 18:46:09 +08:00
|
|
|
or die "Failed to open git-rev-list to find a single parent: $!";
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $parentline = <$revparent>;
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp $parentline;
|
|
|
|
my ($revfound,$parent) = split m/\s+/, $parentline;
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
close($revparent);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:11:24 +08:00
|
|
|
sub git_find_all_parents {
|
|
|
|
my ($rev) = @_;
|
|
|
|
|
|
|
|
my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev")
|
|
|
|
or die "Failed to open git-rev-list to find a single parent: $!";
|
|
|
|
|
|
|
|
my $parentline = <$revparent>;
|
|
|
|
chomp $parentline;
|
|
|
|
my ($origrev, @parents) = split m/\s+/, $parentline;
|
|
|
|
|
|
|
|
close($revparent);
|
|
|
|
|
|
|
|
return @parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub git_merge_base {
|
|
|
|
my ($rev1, $rev2) = @_;
|
|
|
|
|
|
|
|
my $mb = open_pipe("git-merge-base", $rev1, $rev2)
|
|
|
|
or die "Failed to open git-merge-base: $!";
|
|
|
|
|
|
|
|
my $base = <$mb>;
|
|
|
|
chomp $base;
|
|
|
|
|
|
|
|
close($mb);
|
|
|
|
|
|
|
|
return $base;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Construct a set of pseudo parents that are in the same order,
|
|
|
|
# and the same quantity as the real parents,
|
|
|
|
# but whose SHA1s are as similar to the logical parents
|
|
|
|
# as possible.
|
|
|
|
sub get_pseudo_parents {
|
|
|
|
my ($all, $fake) = @_;
|
|
|
|
|
|
|
|
my @all = @$all;
|
|
|
|
my @fake = @$fake;
|
|
|
|
|
|
|
|
my @pseudo;
|
|
|
|
|
|
|
|
my %fake = map {$_ => 1} @fake;
|
|
|
|
my %seenfake;
|
|
|
|
|
|
|
|
my $fakeidx = 0;
|
|
|
|
foreach my $p (@all) {
|
|
|
|
if (exists $fake{$p}) {
|
|
|
|
if ($fake[$fakeidx] ne $p) {
|
|
|
|
die sprintf("parent mismatch: %s != %s\nall:%s\nfake:%s\n",
|
|
|
|
$fake[$fakeidx], $p,
|
|
|
|
join(", ", @all),
|
|
|
|
join(", ", @fake),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @pseudo, $p;
|
|
|
|
$fakeidx++;
|
|
|
|
$seenfake{$p}++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
my $base = git_merge_base($fake[$fakeidx], $p);
|
|
|
|
if ($base ne $fake[$fakeidx]) {
|
|
|
|
die sprintf("Result of merge-base doesn't match fake: %s,%s != %s\n",
|
|
|
|
$fake[$fakeidx], $p, $base);
|
|
|
|
}
|
|
|
|
|
|
|
|
# The details of how we parse the diffs
|
|
|
|
# mean that we cannot have a duplicate
|
|
|
|
# revision in the list, so if we've already
|
|
|
|
# seen the revision we would normally add, just use
|
|
|
|
# the actual revision.
|
|
|
|
if ($seenfake{$base}) {
|
|
|
|
push @pseudo, $p;
|
|
|
|
} else {
|
|
|
|
push @pseudo, $base;
|
|
|
|
$seenfake{$base}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @pseudo;
|
|
|
|
}
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
# Get a diff between the current revision and a parent.
|
|
|
|
# Record the commit information that results.
|
|
|
|
sub git_diff_parse {
|
2006-07-04 09:30:02 +08:00
|
|
|
my ($parents, $rev, %revinfo) = @_;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-08-07 20:11:24 +08:00
|
|
|
my @pseudo_parents;
|
|
|
|
my @command = ("git-diff-tree");
|
|
|
|
my $revision_spec;
|
|
|
|
|
|
|
|
if (scalar @$parents == 1) {
|
|
|
|
|
|
|
|
$revision_spec = join("..", $parents->[0], $rev);
|
|
|
|
@pseudo_parents = @$parents;
|
|
|
|
} else {
|
|
|
|
my @all_parents = git_find_all_parents($rev);
|
|
|
|
|
|
|
|
if (@all_parents != @$parents) {
|
|
|
|
@pseudo_parents = get_pseudo_parents(\@all_parents, $parents);
|
|
|
|
} else {
|
|
|
|
@pseudo_parents = @$parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
$revision_spec = $rev;
|
|
|
|
push @command, "-c";
|
|
|
|
}
|
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
my @filenames = ( $revs{$rev}{'filename'} );
|
2006-08-07 20:11:24 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
push @filenames, $revs{$parent}{'filename'};
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:11:24 +08:00
|
|
|
push @command, "-p", "-M", $revision_spec, "--", @filenames;
|
|
|
|
|
|
|
|
|
|
|
|
my $diff = open_pipe( @command )
|
2006-02-20 18:46:09 +08:00
|
|
|
or die "Failed to call git-diff for annotation: $!";
|
|
|
|
|
2006-08-07 20:11:24 +08:00
|
|
|
_git_diff_parse($diff, \@pseudo_parents, $rev, %revinfo);
|
2006-02-26 09:48:33 +08:00
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
close($diff);
|
2006-02-26 09:48:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _git_diff_parse {
|
2006-07-04 09:30:02 +08:00
|
|
|
my ($diff, $parents, $rev, %revinfo) = @_;
|
|
|
|
|
|
|
|
my $ri = 0;
|
2006-02-26 09:48:33 +08:00
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
my $slines = $revs{$rev}{'lines'};
|
2006-07-04 09:30:02 +08:00
|
|
|
my (%plines, %pi);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
my $gotheader = 0;
|
2006-02-26 09:48:33 +08:00
|
|
|
my ($remstart);
|
2006-07-04 09:30:02 +08:00
|
|
|
my $parent_count = @$parents;
|
|
|
|
|
|
|
|
my $diff_header_regexp = "^@";
|
|
|
|
$diff_header_regexp .= "@" x @$parents;
|
|
|
|
$diff_header_regexp .= ' -\d+,\d+' x @$parents;
|
|
|
|
$diff_header_regexp .= ' \+(\d+),\d+';
|
2006-08-07 20:11:24 +08:00
|
|
|
$diff_header_regexp .= " " . ("@" x @$parents);
|
2006-07-04 09:30:02 +08:00
|
|
|
|
|
|
|
my %claim_regexps;
|
|
|
|
my $allparentplus = '^' . '\\+' x @$parents . '(.*)$';
|
|
|
|
|
|
|
|
{
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
|
|
|
|
$pi{$parent} = 0;
|
|
|
|
my $r = '^' . '.' x @$parents . '(.*)$';
|
|
|
|
my $p = $r;
|
|
|
|
substr($p,$i+1, 1) = '\\+';
|
|
|
|
|
|
|
|
my $m = $r;
|
|
|
|
substr($m,$i+1, 1) = '-';
|
|
|
|
|
|
|
|
$claim_regexps{$parent}{plus} = $p;
|
|
|
|
$claim_regexps{$parent}{minus} = $m;
|
|
|
|
|
|
|
|
$plines{$parent} = [];
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DIFF:
|
2006-02-26 11:02:05 +08:00
|
|
|
while(<$diff>) {
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp;
|
2006-08-07 20:11:24 +08:00
|
|
|
#printf("%d:%s:\n", $gotheader, $_);
|
2006-07-04 09:30:02 +08:00
|
|
|
if (m/$diff_header_regexp/) {
|
|
|
|
$remstart = $1 - 1;
|
|
|
|
# (0-based arrays)
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
$gotheader = 1;
|
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
for (my $i = $ri; $i < $remstart; $i++) {
|
|
|
|
$plines{$parent}[$pi{$parent}++] = $slines->[$i];
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
}
|
2006-07-04 09:30:02 +08:00
|
|
|
$ri = $remstart;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
next DIFF;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
} elsif (!$gotheader) {
|
|
|
|
# Skip over the leadin.
|
|
|
|
next DIFF;
|
|
|
|
}
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-07-04 09:30:02 +08:00
|
|
|
if (m/^\\/) {
|
2006-03-02 13:23:37 +08:00
|
|
|
;
|
|
|
|
# Skip \No newline at end of file.
|
|
|
|
# But this can be internationalized, so only look
|
|
|
|
# for an initial \
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
} else {
|
2006-07-04 09:30:02 +08:00
|
|
|
my %claims = ();
|
|
|
|
my $negclaim = 0;
|
|
|
|
my $allclaimed = 0;
|
|
|
|
my $line;
|
|
|
|
|
|
|
|
if (m/$allparentplus/) {
|
|
|
|
claim_line($ri, $rev, $slines, %revinfo);
|
|
|
|
$allclaimed = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PARENT:
|
|
|
|
foreach my $parent (keys %claim_regexps) {
|
|
|
|
my $m = $claim_regexps{$parent}{minus};
|
|
|
|
my $p = $claim_regexps{$parent}{plus};
|
|
|
|
|
|
|
|
if (m/$m/) {
|
|
|
|
$line = $1;
|
|
|
|
$plines{$parent}[$pi{$parent}++] = [ $line, '', '', '', 0 ];
|
|
|
|
$negclaim++;
|
|
|
|
|
|
|
|
} elsif (m/$p/) {
|
|
|
|
$line = $1;
|
|
|
|
if (get_line($slines, $ri) eq $line) {
|
|
|
|
# Found a match, claim
|
|
|
|
$claims{$parent}++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
die sprintf("Sync error: %d\n|%s\n|%s\n%s => %s\n",
|
|
|
|
$ri, $line,
|
|
|
|
get_line($slines, $ri),
|
|
|
|
$rev, $parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (%claims) {
|
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
next if $claims{$parent} || $allclaimed;
|
|
|
|
$plines{$parent}[$pi{$parent}++] = $slines->[$ri];
|
|
|
|
#[ $line, '', '', '', 0 ];
|
|
|
|
}
|
|
|
|
$ri++;
|
|
|
|
|
|
|
|
} elsif ($negclaim) {
|
|
|
|
next DIFF;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (substr($_,scalar @$parents) ne get_line($slines,$ri) ) {
|
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
printf("parent %s is on line %d\n", $parent, $pi{$parent});
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:11:24 +08:00
|
|
|
my @context;
|
|
|
|
for (my $i = -2; $i < 2; $i++) {
|
|
|
|
push @context, get_line($slines, $ri + $i);
|
|
|
|
}
|
|
|
|
my $context = join("\n", @context);
|
|
|
|
|
|
|
|
my $justline = substr($_, scalar @$parents);
|
2006-07-04 09:30:02 +08:00
|
|
|
die sprintf("Line %d, does not match:\n|%s|\n|%s|\n%s\n",
|
|
|
|
$ri,
|
2006-08-07 20:11:24 +08:00
|
|
|
$justline,
|
|
|
|
$context);
|
2006-07-04 09:30:02 +08:00
|
|
|
}
|
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
$plines{$parent}[$pi{$parent}++] = $slines->[$ri];
|
|
|
|
}
|
|
|
|
$ri++;
|
2006-02-20 18:46:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-04 09:30:02 +08:00
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
for (my $i = $ri; $i < @{$slines} ; $i++) {
|
2006-07-04 09:30:02 +08:00
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
push @{$plines{$parent}}, $slines->[$ri];
|
|
|
|
}
|
|
|
|
$ri++;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $parent (@$parents) {
|
|
|
|
$revs{$parent}{lines} = $plines{$parent};
|
2006-02-20 18:46:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_line {
|
|
|
|
my ($lines, $index) = @_;
|
|
|
|
|
|
|
|
return ref $lines->[$index] ne '' ? $lines->[$index][0] : $lines->[$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub git_cat_file {
|
2006-02-26 09:48:33 +08:00
|
|
|
my ($rev, $filename) = @_;
|
|
|
|
return () unless defined $rev && defined $filename;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
2006-02-26 09:48:33 +08:00
|
|
|
my $blob = git_ls_tree($rev, $filename);
|
2006-03-06 10:43:53 +08:00
|
|
|
die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
|
2006-02-26 09:48:33 +08:00
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $catfile = open_pipe("git","cat-file", "blob", $blob)
|
2006-02-26 09:48:33 +08:00
|
|
|
or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
my @lines;
|
2006-02-26 11:02:05 +08:00
|
|
|
while(<$catfile>) {
|
2006-02-20 18:46:09 +08:00
|
|
|
chomp;
|
|
|
|
push @lines, $_;
|
|
|
|
}
|
2006-02-26 11:02:05 +08:00
|
|
|
close($catfile);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
return @lines;
|
|
|
|
}
|
|
|
|
|
2006-02-26 09:48:33 +08:00
|
|
|
sub git_ls_tree {
|
|
|
|
my ($rev, $filename) = @_;
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
my $lstree = open_pipe("git","ls-tree",$rev,$filename)
|
2006-02-26 09:48:33 +08:00
|
|
|
or die "Failed to call git ls-tree: $!";
|
|
|
|
|
|
|
|
my ($mode, $type, $blob, $tfilename);
|
2006-02-26 11:02:05 +08:00
|
|
|
while(<$lstree>) {
|
2006-03-06 10:43:53 +08:00
|
|
|
chomp;
|
2006-02-26 09:48:33 +08:00
|
|
|
($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
|
|
|
|
last if ($tfilename eq $filename);
|
|
|
|
}
|
2006-02-26 11:02:05 +08:00
|
|
|
close($lstree);
|
2006-02-26 09:48:33 +08:00
|
|
|
|
2006-03-06 10:43:53 +08:00
|
|
|
return $blob if ($tfilename eq $filename);
|
2006-02-26 09:48:33 +08:00
|
|
|
die "git-ls-tree failed to find blob for $filename";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
sub claim_line {
|
|
|
|
my ($floffset, $rev, $lines, %revinfo) = @_;
|
|
|
|
my $oline = get_line($lines, $floffset);
|
|
|
|
@{$lines->[$floffset]} = ( $oline, $rev,
|
|
|
|
$revinfo{'author'}, $revinfo{'author_date'} );
|
|
|
|
#printf("Claiming line %d with rev %s: '%s'\n",
|
|
|
|
# $floffset, $rev, $oline) if 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub git_commit_info {
|
|
|
|
my ($rev) = @_;
|
2006-02-26 11:02:05 +08:00
|
|
|
my $commit = open_pipe("git-cat-file", "commit", $rev)
|
2006-02-20 18:46:09 +08:00
|
|
|
or die "Failed to call git-cat-file: $!";
|
|
|
|
|
|
|
|
my %info;
|
2006-02-26 11:02:05 +08:00
|
|
|
while(<$commit>) {
|
2006-02-20 18:46:09 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-02-26 11:02:05 +08:00
|
|
|
close($commit);
|
2006-02-20 18:46:09 +08:00
|
|
|
|
|
|
|
return %info;
|
|
|
|
}
|
2006-02-20 23:20:10 +08:00
|
|
|
|
|
|
|
sub format_date {
|
2006-03-02 17:50:09 +08:00
|
|
|
if ($rawtime) {
|
|
|
|
return $_[0];
|
|
|
|
}
|
2006-02-20 23:20:10 +08:00
|
|
|
my ($timestamp, $timezone) = split(' ', $_[0]);
|
2006-03-06 06:48:01 +08:00
|
|
|
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));
|
2006-02-20 23:20:10 +08:00
|
|
|
}
|
|
|
|
|
2006-02-26 09:48:33 +08:00
|
|
|
# Copied from git-send-email.perl - We need a Git.pm module..
|
|
|
|
sub gitvar {
|
|
|
|
my ($var) = @_;
|
|
|
|
my $fh;
|
|
|
|
my $pid = open($fh, '-|');
|
|
|
|
die "$!" unless defined $pid;
|
|
|
|
if (!$pid) {
|
|
|
|
exec('git-var', $var) or die "$!";
|
|
|
|
}
|
|
|
|
my ($val) = <$fh>;
|
|
|
|
close $fh or die "$!";
|
|
|
|
chomp($val);
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub gitvar_name {
|
|
|
|
my ($name) = @_;
|
|
|
|
my $val = gitvar($name);
|
|
|
|
my @field = split(/\s+/, $val);
|
|
|
|
return join(' ', @field[0...(@field-4)]);
|
|
|
|
}
|
|
|
|
|
2006-02-26 11:02:05 +08:00
|
|
|
sub open_pipe {
|
2006-02-27 05:09:12 +08:00
|
|
|
if ($^O eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
|
|
|
|
return open_pipe_activestate(@_);
|
|
|
|
} else {
|
|
|
|
return open_pipe_normal(@_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub open_pipe_activestate {
|
|
|
|
tie *fh, "Git::ActiveStatePipe", @_;
|
|
|
|
return *fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub open_pipe_normal {
|
2006-02-26 11:02:05 +08:00
|
|
|
my (@execlist) = @_;
|
|
|
|
|
|
|
|
my $pid = open my $kid, "-|";
|
|
|
|
defined $pid or die "Cannot fork: $!";
|
|
|
|
|
|
|
|
unless ($pid) {
|
|
|
|
exec @execlist;
|
|
|
|
die "Cannot exec @execlist: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $kid;
|
|
|
|
}
|
2006-02-27 05:09:12 +08:00
|
|
|
|
|
|
|
package Git::ActiveStatePipe;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
sub TIEHANDLE {
|
|
|
|
my ($class, @params) = @_;
|
|
|
|
my $cmdline = join " ", @params;
|
|
|
|
my @data = qx{$cmdline};
|
|
|
|
bless { i => 0, data => \@data }, $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub READLINE {
|
|
|
|
my $self = shift;
|
|
|
|
if ($self->{i} >= scalar @{$self->{data}}) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
return $self->{'data'}->[ $self->{i}++ ];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub CLOSE {
|
|
|
|
my $self = shift;
|
|
|
|
delete $self->{data};
|
|
|
|
delete $self->{i};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub EOF {
|
|
|
|
my $self = shift;
|
|
|
|
return ($self->{i} >= scalar @{$self->{data}});
|
|
|
|
}
|