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);
13 use File
::Basename
qw(basename dirname);
16 print STDERR
"Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
18 Show long rev (Defaults off)
20 Show raw timestamp (Defaults off)
22 Follow renames (Defaults on).
23 -S, --rev-file revs-file
24 Use revs from revs-file instead of calling git-rev-list
32 our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
34 my $rc = GetOptions
( "long|l" => \
$longrev,
35 "time|t" => \
$rawtime,
37 "rename|r" => \
$rename,
38 "rev-file|S=s" => \
$rev_file);
39 if (!$rc or $help or !@ARGV) {
43 my $filename = shift @ARGV;
45 $starting_rev = shift @ARGV;
50 'rev' => defined $starting_rev ?
$starting_rev : "HEAD",
51 'filename' => $filename,
57 if (defined $starting_rev) {
58 @filelines = git_cat_file
($starting_rev, $filename);
61 or die "Failed to open filename: $!";
75 my $revsprocessed = 0;
76 while (my $bound = pop @stack) {
77 my @revisions = git_rev_list
($bound->{'rev'}, $bound->{'filename'});
78 foreach my $revinst (@revisions) {
79 my ($rev, @parents) = @
$revinst;
85 $revs{$rev}{'filename'} = $bound->{'filename'};
86 if (scalar @parents > 0) {
87 $revs{$rev}{'parents'} = \
@parents;
95 my $newbound = find_parent_renames
($rev, $bound->{'filename'});
96 if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
97 push @stack, $newbound;
98 $revs{$rev}{'parents'} = [$newbound->{'rev'}];
102 push @revqueue, $head;
103 init_claim
( defined $starting_rev ?
$head : 'dirty');
104 unless (defined $starting_rev) {
105 my $diff = open_pipe
("git","diff","-R", "HEAD", "--",$filename)
106 or die "Failed to call git diff to check for dirty state: $!";
108 _git_diff_parse
($diff, $head, "dirty", (
109 'author' => gitvar_name
("GIT_AUTHOR_IDENT"),
110 'author_date' => sprintf("%s +0000",time()),
119 foreach my $l (@filelines) {
120 my ($output, $rev, $committer, $date);
121 if (ref $l eq 'ARRAY') {
122 ($output, $rev, $committer, $date) = @
$l;
123 if (!$longrev && length($rev) > 8) {
124 $rev = substr($rev,0,8);
128 ($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
131 printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
132 format_date
($date), ++$i, $output);
137 for (my $i = 0; $i < @filelines; $i++) {
138 $filelines[$i] = [ $filelines[$i], '', '', '', 1];
143 # 1 <-- belongs to the original file.
145 $revs{$rev}{'lines'} = \
@filelines;
152 while (my $rev = shift @revqueue) {
153 next if $seen{$rev}++;
155 my %revinfo = git_commit_info
($rev);
157 foreach my $p (@
{$revs{$rev}{'parents'}}) {
159 git_diff_parse
($p, $rev, %revinfo);
164 if (scalar @
{$revs{$rev}{parents
}} == 0) {
165 # We must be at the initial rev here, so claim everything that is left.
166 for (my $i = 0; $i < @
{$revs{$rev}{lines
}}; $i++) {
167 if (ref ${$revs{$rev}{lines
}}[$i] eq '' || ${$revs{$rev}{lines
}}[$i][1] eq '') {
168 claim_line
($i, $rev, $revs{$rev}{lines
}, %revinfo);
177 my ($rev, $file) = @_;
181 open($revlist, '<' . $rev_file)
182 or die "Failed to open $rev_file : $!";
184 $revlist = open_pipe
("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
185 or die "Failed to exec git-rev-list: $!";
189 while(my $line = <$revlist>) {
191 my ($rev, @parents) = split /\s+/, $line;
192 push @revs, [ $rev, @parents ];
196 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
200 sub find_parent_renames
{
201 my ($rev, $file) = @_;
203 my $patch = open_pipe
("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
204 or die "Failed to exec git-diff: $!";
209 while (my $change = <$patch>) {
211 my $filename = <$patch>;
212 if (!defined $filename) {
217 if ($change =~ m/^[AMD]$/ ) {
219 } elsif ($change =~ m/^R/ ) {
220 my $oldfilename = $filename;
221 $filename = <$patch>;
223 if ( $file eq $filename ) {
224 my $parent = git_find_parent
($rev, $oldfilename);
225 @bound{'rev','filename'} = ($parent, $oldfilename);
236 sub git_find_parent
{
237 my ($rev, $filename) = @_;
239 my $revparent = open_pipe
("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
240 or die "Failed to open git-rev-list to find a single parent: $!";
242 my $parentline = <$revparent>;
244 my ($revfound,$parent) = split m/\s+/, $parentline;
252 # Get a diff between the current revision and a parent.
253 # Record the commit information that results.
255 my ($parent, $rev, %revinfo) = @_;
257 my $diff = open_pipe
("git-diff-tree","-M","-p",$rev,$parent,"--",
258 $revs{$rev}{'filename'}, $revs{$parent}{'filename'})
259 or die "Failed to call git-diff for annotation: $!";
261 _git_diff_parse
($diff, $parent, $rev, %revinfo);
266 sub _git_diff_parse
{
267 my ($diff, $parent, $rev, %revinfo) = @_;
269 my ($ri, $pi) = (0,0);
270 my $slines = $revs{$rev}{'lines'};
275 my ($hunk_start, $hunk_index);
278 if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
280 # Adjust for 0-based arrays
282 # Reinit hunk tracking.
283 $hunk_start = $remstart;
287 for (my $i = $ri; $i < $remstart; $i++) {
288 $plines[$pi++] = $slines->[$i];
292 } elsif (!$gotheader) {
298 $plines[$pi++] = [ $line, '', '', '', 0 ];
301 } elsif (m/^-(.*)$/) {
303 if (get_line
($slines, $ri) eq $line) {
304 # Found a match, claim
305 claim_line
($ri, $rev, $slines, %revinfo);
307 die sprintf("Sync error: %d/%d\n|%s\n|%s\n%s => %s\n",
308 $ri, $hunk_start + $hunk_index,
310 get_line
($slines, $ri),
317 # Skip \No newline at end of file.
318 # But this can be internationalized, so only look
322 if (substr($_,1) ne get_line
($slines,$ri) ) {
323 die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
324 $hunk_start + $hunk_index, $ri,
326 get_line
($slines,$ri),
329 $plines[$pi++] = $slines->[$ri++];
333 for (my $i = $ri; $i < @
{$slines} ; $i++) {
334 push @plines, $slines->[$ri++];
337 $revs{$parent}{lines
} = \
@plines;
342 my ($lines, $index) = @_;
344 return ref $lines->[$index] ne '' ?
$lines->[$index][0] : $lines->[$index];
348 my ($rev, $filename) = @_;
349 return () unless defined $rev && defined $filename;
351 my $blob = git_ls_tree
($rev, $filename);
352 die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
354 my $catfile = open_pipe
("git","cat-file", "blob", $blob)
355 or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
368 my ($rev, $filename) = @_;
370 my $lstree = open_pipe
("git","ls-tree",$rev,$filename)
371 or die "Failed to call git ls-tree: $!";
373 my ($mode, $type, $blob, $tfilename);
376 ($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
377 last if ($tfilename eq $filename);
381 return $blob if ($tfilename eq $filename);
382 die "git-ls-tree failed to find blob for $filename";
389 my ($floffset, $rev, $lines, %revinfo) = @_;
390 my $oline = get_line
($lines, $floffset);
391 @
{$lines->[$floffset]} = ( $oline, $rev,
392 $revinfo{'author'}, $revinfo{'author_date'} );
393 #printf("Claiming line %d with rev %s: '%s'\n",
394 # $floffset, $rev, $oline) if 1;
397 sub git_commit_info
{
399 my $commit = open_pipe
("git-cat-file", "commit", $rev)
400 or die "Failed to call git-cat-file: $!";
405 last if (length $_ == 0);
407 if (m/^author (.*) <(.*)> (.*)$/) {
408 $info{'author'} = $1;
409 $info{'author_email'} = $2;
410 $info{'author_date'} = $3;
411 } elsif (m/^committer (.*) <(.*)> (.*)$/) {
412 $info{'committer'} = $1;
413 $info{'committer_email'} = $2;
414 $info{'committer_date'} = $3;
426 my ($timestamp, $timezone) = split(' ', $_[0]);
427 my $minutes = abs($timezone);
428 $minutes = int($minutes / 100) * 60 + ($minutes % 100);
430 $minutes = -$minutes;
432 my $t = $timestamp + $minutes * 60;
433 return strftime
("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
436 # Copied from git-send-email.perl - We need a Git.pm module..
440 my $pid = open($fh, '-|');
441 die "$!" unless defined $pid;
443 exec('git-var', $var) or die "$!";
446 close $fh or die "$!";
453 my $val = gitvar
($name);
454 my @field = split(/\s+/, $val);
455 return join(' ', @field[0...(@field-4)]);
459 if ($^O
eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
460 return open_pipe_activestate
(@_);
462 return open_pipe_normal
(@_);
466 sub open_pipe_activestate
{
467 tie
*fh
, "Git::ActiveStatePipe", @_;
471 sub open_pipe_normal
{
474 my $pid = open my $kid, "-|";
475 defined $pid or die "Cannot fork: $!";
479 die "Cannot exec @execlist: $!";
485 package Git
::ActiveStatePipe
;
489 my ($class, @params) = @_;
490 my $cmdline = join " ", @params;
491 my @data = qx{$cmdline};
492 bless { i
=> 0, data
=> \
@data }, $class;
497 if ($self->{i
} >= scalar @
{$self->{data
}}) {
500 return $self->{'data'}->[ $self->{i
}++ ];
505 delete $self->{data
};
511 return ($self->{i
} >= scalar @
{$self->{data
}});