git-svn: documentation updates
[git/gitweb.git] / git-annotate.perl
bloba6a7a482cdcf34e3a1d545c7d5deab599a7c0d2f
1 #!/usr/bin/perl
2 # Copyright 2006, Ryan Anderson <ryan@michonline.com>
4 # GPL v2 (See COPYING)
6 # This file is licensed under the GPL v2, or a later version
7 # at the discretion of Linus Torvalds.
9 use warnings;
10 use strict;
11 use Getopt::Long;
12 use POSIX qw(strftime gmtime);
13 use File::Basename qw(basename dirname);
15 sub usage() {
16 print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
17 -l, --long
18 Show long rev (Defaults off)
19 -t, --time
20 Show raw timestamp (Defaults off)
21 -r, --rename
22 Follow renames (Defaults on).
23 -S, --rev-file revs-file
24 Use revs from revs-file instead of calling git-rev-list
25 -h, --help
26 This message.
29 exit(1);
32 our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
34 my $rc = GetOptions( "long|l" => \$longrev,
35 "time|t" => \$rawtime,
36 "help|h" => \$help,
37 "rename|r" => \$rename,
38 "rev-file|S=s" => \$rev_file);
39 if (!$rc or $help or !@ARGV) {
40 usage();
43 my $filename = shift @ARGV;
44 if (@ARGV) {
45 $starting_rev = shift @ARGV;
48 my @stack = (
50 'rev' => defined $starting_rev ? $starting_rev : "HEAD",
51 'filename' => $filename,
55 our @filelines = ();
57 if (defined $starting_rev) {
58 @filelines = git_cat_file($starting_rev, $filename);
59 } else {
60 open(F,"<",$filename)
61 or die "Failed to open filename: $!";
63 while(<F>) {
64 chomp;
65 push @filelines, $_;
67 close(F);
71 our %revs;
72 our @revqueue;
73 our $head;
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;
80 $head ||= $rev;
82 if (!defined($rev)) {
83 $rev = "";
85 $revs{$rev}{'filename'} = $bound->{'filename'};
86 if (scalar @parents > 0) {
87 $revs{$rev}{'parents'} = \@parents;
88 next;
91 if (!$rename) {
92 next;
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()),
113 close($diff);
115 handle_rev();
118 my $i = 0;
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);
126 } else {
127 $output = $l;
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);
135 sub init_claim {
136 my ($rev) = @_;
137 for (my $i = 0; $i < @filelines; $i++) {
138 $filelines[$i] = [ $filelines[$i], '', '', '', 1];
139 # line,
140 # rev,
141 # author,
142 # date,
143 # 1 <-- belongs to the original file.
145 $revs{$rev}{'lines'} = \@filelines;
149 sub handle_rev {
150 my $i = 0;
151 my %seen;
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);
160 push @revqueue, $p;
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);
176 sub git_rev_list {
177 my ($rev, $file) = @_;
179 my $revlist;
180 if ($rev_file) {
181 open($revlist, '<' . $rev_file)
182 or die "Failed to open $rev_file : $!";
183 } else {
184 $revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
185 or die "Failed to exec git-rev-list: $!";
188 my @revs;
189 while(my $line = <$revlist>) {
190 chomp $line;
191 my ($rev, @parents) = split /\s+/, $line;
192 push @revs, [ $rev, @parents ];
194 close($revlist);
196 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
197 return @revs;
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: $!";
206 local $/ = "\0";
207 my %bound;
208 my $junk = <$patch>;
209 while (my $change = <$patch>) {
210 chomp $change;
211 my $filename = <$patch>;
212 if (!defined $filename) {
213 next;
215 chomp $filename;
217 if ($change =~ m/^[AMD]$/ ) {
218 next;
219 } elsif ($change =~ m/^R/ ) {
220 my $oldfilename = $filename;
221 $filename = <$patch>;
222 chomp $filename;
223 if ( $file eq $filename ) {
224 my $parent = git_find_parent($rev, $oldfilename);
225 @bound{'rev','filename'} = ($parent, $oldfilename);
226 last;
230 close($patch);
232 return \%bound;
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>;
243 chomp $parentline;
244 my ($revfound,$parent) = split m/\s+/, $parentline;
246 close($revparent);
248 return $parent;
252 # Get a diff between the current revision and a parent.
253 # Record the commit information that results.
254 sub git_diff_parse {
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);
263 close($diff);
266 sub _git_diff_parse {
267 my ($diff, $parent, $rev, %revinfo) = @_;
269 my ($ri, $pi) = (0,0);
270 my $slines = $revs{$rev}{'lines'};
271 my @plines;
273 my $gotheader = 0;
274 my ($remstart);
275 my ($hunk_start, $hunk_index);
276 while(<$diff>) {
277 chomp;
278 if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
279 $remstart = $1;
280 # Adjust for 0-based arrays
281 $remstart--;
282 # Reinit hunk tracking.
283 $hunk_start = $remstart;
284 $hunk_index = 0;
285 $gotheader = 1;
287 for (my $i = $ri; $i < $remstart; $i++) {
288 $plines[$pi++] = $slines->[$i];
289 $ri++;
291 next;
292 } elsif (!$gotheader) {
293 next;
296 if (m/^\+(.*)$/) {
297 my $line = $1;
298 $plines[$pi++] = [ $line, '', '', '', 0 ];
299 next;
301 } elsif (m/^-(.*)$/) {
302 my $line = $1;
303 if (get_line($slines, $ri) eq $line) {
304 # Found a match, claim
305 claim_line($ri, $rev, $slines, %revinfo);
306 } else {
307 die sprintf("Sync error: %d/%d\n|%s\n|%s\n%s => %s\n",
308 $ri, $hunk_start + $hunk_index,
309 $line,
310 get_line($slines, $ri),
311 $rev, $parent);
313 $ri++;
315 } elsif (m/^\\/) {
317 # Skip \No newline at end of file.
318 # But this can be internationalized, so only look
319 # for an initial \
321 } else {
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,
325 substr($_,1),
326 get_line($slines,$ri),
327 $rev, $parent);
329 $plines[$pi++] = $slines->[$ri++];
331 $hunk_index++;
333 for (my $i = $ri; $i < @{$slines} ; $i++) {
334 push @plines, $slines->[$ri++];
337 $revs{$parent}{lines} = \@plines;
338 return;
341 sub get_line {
342 my ($lines, $index) = @_;
344 return ref $lines->[$index] ne '' ? $lines->[$index][0] : $lines->[$index];
347 sub git_cat_file {
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): " . $!;
357 my @lines;
358 while(<$catfile>) {
359 chomp;
360 push @lines, $_;
362 close($catfile);
364 return @lines;
367 sub git_ls_tree {
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);
374 while(<$lstree>) {
375 chomp;
376 ($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
377 last if ($tfilename eq $filename);
379 close($lstree);
381 return $blob if ($tfilename eq $filename);
382 die "git-ls-tree failed to find blob for $filename";
388 sub claim_line {
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 {
398 my ($rev) = @_;
399 my $commit = open_pipe("git-cat-file", "commit", $rev)
400 or die "Failed to call git-cat-file: $!";
402 my %info;
403 while(<$commit>) {
404 chomp;
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;
417 close($commit);
419 return %info;
422 sub format_date {
423 if ($rawtime) {
424 return $_[0];
426 my ($timestamp, $timezone) = split(' ', $_[0]);
427 my $minutes = abs($timezone);
428 $minutes = int($minutes / 100) * 60 + ($minutes % 100);
429 if ($timezone < 0) {
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..
437 sub gitvar {
438 my ($var) = @_;
439 my $fh;
440 my $pid = open($fh, '-|');
441 die "$!" unless defined $pid;
442 if (!$pid) {
443 exec('git-var', $var) or die "$!";
445 my ($val) = <$fh>;
446 close $fh or die "$!";
447 chomp($val);
448 return $val;
451 sub gitvar_name {
452 my ($name) = @_;
453 my $val = gitvar($name);
454 my @field = split(/\s+/, $val);
455 return join(' ', @field[0...(@field-4)]);
458 sub open_pipe {
459 if ($^O eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
460 return open_pipe_activestate(@_);
461 } else {
462 return open_pipe_normal(@_);
466 sub open_pipe_activestate {
467 tie *fh, "Git::ActiveStatePipe", @_;
468 return *fh;
471 sub open_pipe_normal {
472 my (@execlist) = @_;
474 my $pid = open my $kid, "-|";
475 defined $pid or die "Cannot fork: $!";
477 unless ($pid) {
478 exec @execlist;
479 die "Cannot exec @execlist: $!";
482 return $kid;
485 package Git::ActiveStatePipe;
486 use strict;
488 sub TIEHANDLE {
489 my ($class, @params) = @_;
490 my $cmdline = join " ", @params;
491 my @data = qx{$cmdline};
492 bless { i => 0, data => \@data }, $class;
495 sub READLINE {
496 my $self = shift;
497 if ($self->{i} >= scalar @{$self->{data}}) {
498 return undef;
500 return $self->{'data'}->[ $self->{i}++ ];
503 sub CLOSE {
504 my $self = shift;
505 delete $self->{data};
506 delete $self->{i};
509 sub EOF {
510 my $self = shift;
511 return ($self->{i} >= scalar @{$self->{data}});