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 my $filename = shift @ARGV;
18 'filename' => $filename,
22 our (@lineoffsets, @pendinglineoffsets);
25 or die "Failed to open filename: $!";
32 our $leftover_lines = @filelines;
37 my $revsprocessed = 0;
38 while (my $bound = pop @stack) {
39 my @revisions = git_rev_list($bound->{'rev'}, $bound->{'filename'});
40 foreach my $revinst (@revisions) {
41 my ($rev, @parents) = @$revinst;
44 $revs{$rev}{'filename'} = $bound->{'filename'};
45 if (scalar @parents > 0) {
46 $revs{$rev}{'parents'} = \@parents;
50 my $newbound = find_parent_renames($rev, $bound->{'filename'});
51 if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
52 push @stack, $newbound;
53 $revs{$rev}{'parents'} = [$newbound->{'rev'}];
57 push @revqueue, $head;
59 $revs{$head}{'lineoffsets'} = {};
64 foreach my $l (@filelines) {
65 my ($output, $rev, $committer, $date);
66 if (ref $l eq 'ARRAY') {
67 ($output, $rev, $committer, $date) = @$l;
68 if (length($rev) > 8) {
69 $rev = substr($rev,0,8);
73 ($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
76 printf("(%8s %10s %10s %d)%s\n", $rev, $committer, $date, $i++, $output);
81 my %revinfo = git_commit_info($rev);
82 for (my $i = 0; $i < @filelines; $i++) {
83 $filelines[$i] = [ $filelines[$i], '', '', '', 1];
88 # 1 <-- belongs to the original file.
90 $revs{$rev}{'lines'} = \@filelines;
96 while (my $rev = shift @revqueue) {
98 my %revinfo = git_commit_info($rev);
100 foreach my $p (@{$revs{$rev}{'parents'}}) {
102 git_diff_parse($p, $rev, %revinfo);
107 if (scalar @{$revs{$rev}{parents}} == 0) {
108 # We must be at the initial rev here, so claim everything that is left.
109 for (my $i = 0; $i < @{$revs{$rev}{lines}}; $i++) {
110 if (ref ${$revs{$rev}{lines}}[$i] eq '' || ${$revs{$rev}{lines}}[$i][1] eq '') {
111 claim_line($i, $rev, $revs{$rev}{lines}, %revinfo);
120 my ($rev, $file) = @_;
122 open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)
123 or die "Failed to exec git-rev-list: $!";
126 while(my $line = <P>) {
128 my ($rev, @parents) = split /\s+/, $line;
129 push @revs, [ $rev, @parents ];
133 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
137 sub find_parent_renames {
138 my ($rev, $file) = @_;
140 open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
141 or die "Failed to exec git-diff: $!";
146 while (my $change = <P>) {
151 if ($change =~ m/^[AMD]$/ ) {
153 } elsif ($change =~ m/^R/ ) {
154 my $oldfilename = $filename;
157 if ( $file eq $filename ) {
158 my $parent = git_find_parent($rev, $oldfilename);
159 @bound{'rev','filename'} = ($parent, $oldfilename);
170 sub git_find_parent {
171 my ($rev, $filename) = @_;
173 open(REVPARENT,"-|","git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
174 or die "Failed to open git-rev-list to find a single parent: $!";
176 my $parentline = <REVPARENT>;
178 my ($revfound,$parent) = split m/\s+/, $parentline;
186 # Get a diff between the current revision and a parent.
187 # Record the commit information that results.
189 my ($parent, $rev, %revinfo) = @_;
191 my ($ri, $pi) = (0,0);
192 open(DIFF,"-|","git-diff-tree","-M","-p",$rev,$parent,"--",
193 $revs{$rev}{'filename'}, $revs{$parent}{'filename'})
194 or die "Failed to call git-diff for annotation: $!";
196 my $slines = $revs{$rev}{'lines'};
200 my ($remstart, $remlength, $addstart, $addlength);
201 my ($hunk_start, $hunk_index, $hunk_adds);
204 if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
205 ($remstart, $remlength, $addstart, $addlength) = ($1, $2, $3, $4);
206 # Adjust for 0-based arrays
209 # Reinit hunk tracking.
210 $hunk_start = $remstart;
214 for (my $i = $ri; $i < $remstart; $i++) {
215 $plines[$pi++] = $slines->[$i];
219 } elsif (!$gotheader) {
225 $plines[$pi++] = [ $line, '', '', '', 0 ];
228 } elsif (m/^-(.*)$/) {
230 if (get_line($slines, $ri) eq $line) {
231 # Found a match, claim
232 claim_line($ri, $rev, $slines, %revinfo);
234 die sprintf("Sync error: %d/%d\n|%s\n|%s\n%s => %s\n",
235 $ri, $hunk_start + $hunk_index,
237 get_line($slines, $ri),
243 if (substr($_,1) ne get_line($slines,$ri) ) {
244 die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
245 $hunk_start + $hunk_index, $ri,
247 get_line($slines,$ri),
250 $plines[$pi++] = $slines->[$ri++];
255 for (my $i = $ri; $i < @{$slines} ; $i++) {
256 push @plines, $slines->[$ri++];
259 $revs{$parent}{lines} = \@plines;
264 my ($lines, $index) = @_;
266 return ref $lines->[$index] ne '' ? $lines->[$index][0] : $lines->[$index];
270 my ($parent, $filename) = @_;
271 return () unless defined $parent && defined $filename;
272 my $blobline = `git-ls-tree $parent $filename`;
273 my ($mode, $type, $blob, $tfilename) = split(/\s+/, $blobline, 4);
275 open(C,"-|","git-cat-file", "blob", $blob)
276 or die "Failed to git-cat-file blob $blob (rev $parent, file $filename): " . $!;
290 my ($floffset, $rev, $lines, %revinfo) = @_;
291 my $oline = get_line($lines, $floffset);
292 @{$lines->[$floffset]} = ( $oline, $rev,
293 $revinfo{'author'}, $revinfo{'author_date'} );
294 #printf("Claiming line %d with rev %s: '%s'\n",
295 # $floffset, $rev, $oline) if 1;
298 sub git_commit_info {
300 open(COMMIT, "-|","git-cat-file", "commit", $rev)
301 or die "Failed to call git-cat-file: $!";
306 last if (length $_ == 0);
308 if (m/^author (.*) <(.*)> (.*)$/) {
309 $info{'author'} = $1;
310 $info{'author_email'} = $2;
311 $info{'author_date'} = $3;
312 } elsif (m/^committer (.*) <(.*)> (.*)$/) {
313 $info{'committer'} = $1;
314 $info{'committer_email'} = $2;
315 $info{'committer_date'} = $3;