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
19 -S commit use revs from revs-file instead of calling git-rev-list
25 our ($opt_h, $opt_l, $opt_r, $opt_S);
26 getopts("hlrS:") or usage();
29 my $filename = shift @ARGV;
34 'filename' => $filename,
38 our (@lineoffsets, @pendinglineoffsets);
41 or die "Failed to open filename: $!";
48 our $leftover_lines = @filelines;
53 my $revsprocessed = 0;
54 while (my $bound = pop @stack) {
55 my @revisions = git_rev_list($bound->{'rev'}, $bound->{'filename'});
56 foreach my $revinst (@revisions) {
57 my ($rev, @parents) = @$revinst;
63 $revs{$rev}{'filename'} = $bound->{'filename'};
64 if (scalar @parents > 0) {
65 $revs{$rev}{'parents'} = \@parents;
73 my $newbound = find_parent_renames($rev, $bound->{'filename'});
74 if ( exists $newbound->{'filename'} && $newbound->{'filename'} ne $bound->{'filename'}) {
75 push @stack, $newbound;
76 $revs{$rev}{'parents'} = [$newbound->{'rev'}];
80 push @revqueue, $head;
82 $revs{$head}{'lineoffsets'} = {};
87 foreach my $l (@filelines) {
88 my ($output, $rev, $committer, $date);
89 if (ref $l eq 'ARRAY') {
90 ($output, $rev, $committer, $date) = @$l;
91 if (!$opt_l && length($rev) > 8) {
92 $rev = substr($rev,0,8);
96 ($rev, $committer, $date) = ('unknown', 'unknown', 'unknown');
99 printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
100 format_date($date), $i++, $output);
105 my %revinfo = git_commit_info($rev);
106 for (my $i = 0; $i < @filelines; $i++) {
107 $filelines[$i] = [ $filelines[$i], '', '', '', 1];
112 # 1 <-- belongs to the original file.
114 $revs{$rev}{'lines'} = \@filelines;
120 while (my $rev = shift @revqueue) {
122 my %revinfo = git_commit_info($rev);
124 foreach my $p (@{$revs{$rev}{'parents'}}) {
126 git_diff_parse($p, $rev, %revinfo);
131 if (scalar @{$revs{$rev}{parents}} == 0) {
132 # We must be at the initial rev here, so claim everything that is left.
133 for (my $i = 0; $i < @{$revs{$rev}{lines}}; $i++) {
134 if (ref ${$revs{$rev}{lines}}[$i] eq '' || ${$revs{$rev}{lines}}[$i][1] eq '') {
135 claim_line($i, $rev, $revs{$rev}{lines}, %revinfo);
144 my ($rev, $file) = @_;
147 open(P, '<' . $opt_S);
149 open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)
150 or die "Failed to exec git-rev-list: $!";
154 while(my $line = <P>) {
156 my ($rev, @parents) = split /\s+/, $line;
157 push @revs, [ $rev, @parents ];
161 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
165 sub find_parent_renames {
166 my ($rev, $file) = @_;
168 open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
169 or die "Failed to exec git-diff: $!";
174 while (my $change = <P>) {
179 if ($change =~ m/^[AMD]$/ ) {
181 } elsif ($change =~ m/^R/ ) {
182 my $oldfilename = $filename;
185 if ( $file eq $filename ) {
186 my $parent = git_find_parent($rev, $oldfilename);
187 @bound{'rev','filename'} = ($parent, $oldfilename);
198 sub git_find_parent {
199 my ($rev, $filename) = @_;
201 open(REVPARENT,"-|","git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
202 or die "Failed to open git-rev-list to find a single parent: $!";
204 my $parentline = <REVPARENT>;
206 my ($revfound,$parent) = split m/\s+/, $parentline;
214 # Get a diff between the current revision and a parent.
215 # Record the commit information that results.
217 my ($parent, $rev, %revinfo) = @_;
219 my ($ri, $pi) = (0,0);
220 open(DIFF,"-|","git-diff-tree","-M","-p",$rev,$parent,"--",
221 $revs{$rev}{'filename'}, $revs{$parent}{'filename'})
222 or die "Failed to call git-diff for annotation: $!";
224 my $slines = $revs{$rev}{'lines'};
228 my ($remstart, $remlength, $addstart, $addlength);
229 my ($hunk_start, $hunk_index, $hunk_adds);
232 if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
233 ($remstart, $remlength, $addstart, $addlength) = ($1, $2, $3, $4);
234 # Adjust for 0-based arrays
237 # Reinit hunk tracking.
238 $hunk_start = $remstart;
242 for (my $i = $ri; $i < $remstart; $i++) {
243 $plines[$pi++] = $slines->[$i];
247 } elsif (!$gotheader) {
253 $plines[$pi++] = [ $line, '', '', '', 0 ];
256 } elsif (m/^-(.*)$/) {
258 if (get_line($slines, $ri) eq $line) {
259 # Found a match, claim
260 claim_line($ri, $rev, $slines, %revinfo);
262 die sprintf("Sync error: %d/%d\n|%s\n|%s\n%s => %s\n",
263 $ri, $hunk_start + $hunk_index,
265 get_line($slines, $ri),
271 if (substr($_,1) ne get_line($slines,$ri) ) {
272 die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
273 $hunk_start + $hunk_index, $ri,
275 get_line($slines,$ri),
278 $plines[$pi++] = $slines->[$ri++];
283 for (my $i = $ri; $i < @{$slines} ; $i++) {
284 push @plines, $slines->[$ri++];
287 $revs{$parent}{lines} = \@plines;
292 my ($lines, $index) = @_;
294 return ref $lines->[$index] ne '' ? $lines->[$index][0] : $lines->[$index];
298 my ($parent, $filename) = @_;
299 return () unless defined $parent && defined $filename;
300 my $blobline = `git-ls-tree $parent $filename`;
301 my ($mode, $type, $blob, $tfilename) = split(/\s+/, $blobline, 4);
303 open(C,"-|","git-cat-file", "blob", $blob)
304 or die "Failed to git-cat-file blob $blob (rev $parent, file $filename): " . $!;
318 my ($floffset, $rev, $lines, %revinfo) = @_;
319 my $oline = get_line($lines, $floffset);
320 @{$lines->[$floffset]} = ( $oline, $rev,
321 $revinfo{'author'}, $revinfo{'author_date'} );
322 #printf("Claiming line %d with rev %s: '%s'\n",
323 # $floffset, $rev, $oline) if 1;
326 sub git_commit_info {
328 open(COMMIT, "-|","git-cat-file", "commit", $rev)
329 or die "Failed to call git-cat-file: $!";
334 last if (length $_ == 0);
336 if (m/^author (.*) <(.*)> (.*)$/) {
337 $info{'author'} = $1;
338 $info{'author_email'} = $2;
339 $info{'author_date'} = $3;
340 } elsif (m/^committer (.*) <(.*)> (.*)$/) {
341 $info{'committer'} = $1;
342 $info{'committer_email'} = $2;
343 $info{'committer_date'} = $3;
352 my ($timestamp, $timezone) = split(' ', $_[0]);
354 return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($timestamp));