2 # Copyright 2006, Ryan Anderson <ryan@michonline.com>
6 # This file is licensed under the GPL v2, or a later version
7 # at the discretion of Linus Torvalds.
12 use POSIX
qw(strftime gmtime);
15 print STDERR
'Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
17 Show long rev (Defaults off)
19 Follow renames (Defaults on).
20 -S, --rev-file revs-file
21 use revs from revs-file instead of calling git-rev-list
29 our ($help, $longrev, $rename, $starting_rev, $rev_file) = (0, 0, 1);
31 my $rc = GetOptions
( "long|l" => \
$longrev,
33 "rename|r" => \
$rename,
34 "rev-file|S" => \
$rev_file);
39 my $filename = shift @ARGV;
41 $starting_rev = shift @ARGV;
46 'rev' => defined $starting_rev ?
$starting_rev : "HEAD",
47 'filename' => $filename,
53 if (defined $starting_rev) {
54 @filelines = git_cat_file
($starting_rev, $filename);
57 or die "Failed to open filename: $!";
71 my $revsprocessed = 0;
72 while (my $bound = pop @stack) {
73 my @revisions = git_rev_list
($bound->{'rev'}, $bound->{'filename'});
74 foreach my $revinst (@revisions) {
75 my ($rev, @parents) = @
$revinst;
81 $revs{$rev}{'filename'} = $bound->{'filename'};
82 if (scalar @parents > 0) {
83 $revs{$rev}{'parents'} = \
@parents;
91 my $newbound = find_parent_renames
($rev, $bound->{'filename'});
92 if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
93 push @stack, $newbound;
94 $revs{$rev}{'parents'} = [$newbound->{'rev'}];
98 push @revqueue, $head;
99 init_claim
( defined $starting_rev ?
$starting_rev : 'dirty');
100 unless (defined $starting_rev) {
101 my $diff = open_pipe
("git","diff","-R", "HEAD", "--",$filename)
102 or die "Failed to call git diff to check for dirty state: $!";
104 _git_diff_parse
($diff, $head, "dirty", (
105 'author' => gitvar_name
("GIT_AUTHOR_IDENT"),
106 'author_date' => sprintf("%s +0000",time()),
115 foreach my $l (@filelines) {
116 my ($output, $rev, $committer, $date);
117 if (ref $l eq 'ARRAY') {
118 ($output, $rev, $committer, $date) = @
$l;
119 if (!$longrev && length($rev) > 8) {
120 $rev = substr($rev,0,8);
124 ($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
127 printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
128 format_date
($date), $i++, $output);
133 for (my $i = 0; $i < @filelines; $i++) {
134 $filelines[$i] = [ $filelines[$i], '', '', '', 1];
139 # 1 <-- belongs to the original file.
141 $revs{$rev}{'lines'} = \
@filelines;
148 while (my $rev = shift @revqueue) {
149 next if $seen{$rev}++;
151 my %revinfo = git_commit_info
($rev);
153 foreach my $p (@
{$revs{$rev}{'parents'}}) {
155 git_diff_parse
($p, $rev, %revinfo);
160 if (scalar @
{$revs{$rev}{parents
}} == 0) {
161 # We must be at the initial rev here, so claim everything that is left.
162 for (my $i = 0; $i < @
{$revs{$rev}{lines
}}; $i++) {
163 if (ref ${$revs{$rev}{lines
}}[$i] eq '' || ${$revs{$rev}{lines
}}[$i][1] eq '') {
164 claim_line
($i, $rev, $revs{$rev}{lines
}, %revinfo);
173 my ($rev, $file) = @_;
177 open($revlist, '<' . $rev_file);
179 $revlist = open_pipe
("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
180 or die "Failed to exec git-rev-list: $!";
184 while(my $line = <$revlist>) {
186 my ($rev, @parents) = split /\s+/, $line;
187 push @revs, [ $rev, @parents ];
191 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
195 sub find_parent_renames
{
196 my ($rev, $file) = @_;
198 my $patch = open_pipe
("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
199 or die "Failed to exec git-diff: $!";
204 while (my $change = <$patch>) {
206 my $filename = <$patch>;
209 if ($change =~ m/^[AMD]$/ ) {
211 } elsif ($change =~ m/^R/ ) {
212 my $oldfilename = $filename;
213 $filename = <$patch>;
215 if ( $file eq $filename ) {
216 my $parent = git_find_parent
($rev, $oldfilename);
217 @bound{'rev','filename'} = ($parent, $oldfilename);
228 sub git_find_parent
{
229 my ($rev, $filename) = @_;
231 my $revparent = open_pipe
("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
232 or die "Failed to open git-rev-list to find a single parent: $!";
234 my $parentline = <$revparent>;
236 my ($revfound,$parent) = split m/\s+/, $parentline;
244 # Get a diff between the current revision and a parent.
245 # Record the commit information that results.
247 my ($parent, $rev, %revinfo) = @_;
249 my $diff = open_pipe
("git-diff-tree","-M","-p",$rev,$parent,"--",
250 $revs{$rev}{'filename'}, $revs{$parent}{'filename'})
251 or die "Failed to call git-diff for annotation: $!";
253 _git_diff_parse
($diff, $parent, $rev, %revinfo);
258 sub _git_diff_parse
{
259 my ($diff, $parent, $rev, %revinfo) = @_;
261 my ($ri, $pi) = (0,0);
262 my $slines = $revs{$rev}{'lines'};
267 my ($hunk_start, $hunk_index);
270 if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
272 # Adjust for 0-based arrays
274 # Reinit hunk tracking.
275 $hunk_start = $remstart;
279 for (my $i = $ri; $i < $remstart; $i++) {
280 $plines[$pi++] = $slines->[$i];
284 } elsif (!$gotheader) {
290 $plines[$pi++] = [ $line, '', '', '', 0 ];
293 } elsif (m/^-(.*)$/) {
295 if (get_line
($slines, $ri) eq $line) {
296 # Found a match, claim
297 claim_line
($ri, $rev, $slines, %revinfo);
299 die sprintf("Sync error: %d/%d\n|%s\n|%s\n%s => %s\n",
300 $ri, $hunk_start + $hunk_index,
302 get_line
($slines, $ri),
308 if (substr($_,1) ne get_line
($slines,$ri) ) {
309 die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
310 $hunk_start + $hunk_index, $ri,
312 get_line
($slines,$ri),
315 $plines[$pi++] = $slines->[$ri++];
319 for (my $i = $ri; $i < @
{$slines} ; $i++) {
320 push @plines, $slines->[$ri++];
323 $revs{$parent}{lines
} = \
@plines;
328 my ($lines, $index) = @_;
330 return ref $lines->[$index] ne '' ?
$lines->[$index][0] : $lines->[$index];
334 my ($rev, $filename) = @_;
335 return () unless defined $rev && defined $filename;
337 my $blob = git_ls_tree
($rev, $filename);
339 my $catfile = open_pipe
("git","cat-file", "blob", $blob)
340 or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
353 my ($rev, $filename) = @_;
355 my $lstree = open_pipe
("git","ls-tree",$rev,$filename)
356 or die "Failed to call git ls-tree: $!";
358 my ($mode, $type, $blob, $tfilename);
360 ($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
361 last if ($tfilename eq $filename);
365 return $blob if $filename eq $filename;
366 die "git-ls-tree failed to find blob for $filename";
373 my ($floffset, $rev, $lines, %revinfo) = @_;
374 my $oline = get_line
($lines, $floffset);
375 @
{$lines->[$floffset]} = ( $oline, $rev,
376 $revinfo{'author'}, $revinfo{'author_date'} );
377 #printf("Claiming line %d with rev %s: '%s'\n",
378 # $floffset, $rev, $oline) if 1;
381 sub git_commit_info
{
383 my $commit = open_pipe
("git-cat-file", "commit", $rev)
384 or die "Failed to call git-cat-file: $!";
389 last if (length $_ == 0);
391 if (m/^author (.*) <(.*)> (.*)$/) {
392 $info{'author'} = $1;
393 $info{'author_email'} = $2;
394 $info{'author_date'} = $3;
395 } elsif (m/^committer (.*) <(.*)> (.*)$/) {
396 $info{'committer'} = $1;
397 $info{'committer_email'} = $2;
398 $info{'committer_date'} = $3;
407 my ($timestamp, $timezone) = split(' ', $_[0]);
409 return strftime
("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($timestamp));
412 # Copied from git-send-email.perl - We need a Git.pm module..
416 my $pid = open($fh, '-|');
417 die "$!" unless defined $pid;
419 exec('git-var', $var) or die "$!";
422 close $fh or die "$!";
429 my $val = gitvar
($name);
430 my @field = split(/\s+/, $val);
431 return join(' ', @field[0...(@field-4)]);
435 if ($^O
eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
436 return open_pipe_activestate
(@_);
438 return open_pipe_normal
(@_);
442 sub open_pipe_activestate
{
443 tie
*fh
, "Git::ActiveStatePipe", @_;
447 sub open_pipe_normal
{
450 my $pid = open my $kid, "-|";
451 defined $pid or die "Cannot fork: $!";
455 die "Cannot exec @execlist: $!";
461 package Git
::ActiveStatePipe
;
465 my ($class, @params) = @_;
466 my $cmdline = join " ", @params;
467 my @data = qx{$cmdline};
468 bless { i
=> 0, data
=> \
@data }, $class;
473 if ($self->{i
} >= scalar @
{$self->{data
}}) {
476 return $self->{'data'}->[ $self->{i
}++ ];
481 delete $self->{data
};
487 return ($self->{i
} >= scalar @
{$self->{data
}});