@matheusbsbs / matheus-bagliano / commits / 7fb39d5f58
Fork 0

Convert git-annotate to use Git.pm

Together with the other converted scripts, this is probably still pu material; it appears to work fine for me, though. The speed gain from get_object() is about 10% (I expected more...). Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Jul 3, 2006 at 22:48 UTC 7fb39d5f58efd05e982fe148630edc97ded753b6
1 file changed +31 -136
git-annotate.perl
+31 -136
@@ -11,6 +11,7 @@ use strict;
11 use Getopt::Long;
12 use POSIX qw(strftime gmtime);
13 use File::Basename qw(basename dirname);
14 +use Git;
15
16 sub usage() {
17 print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
@@ -29,7 +30,7 @@ sub usage() {
30 exit(1);
31 }
32
32 -our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
33 +our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file, $repo) = (0, 0, 1);
34
35 my $rc = GetOptions( "long|l" => \$longrev,
36 "time|t" => \$rawtime,
@@ -52,6 +53,8 @@ my @stack = (
53 },
54 );
55
56 +$repo = Git->repository();
57 +
58 our @filelines = ();
59
60 if (defined $starting_rev) {
@@ -102,15 +105,11 @@ while (my $bound = pop @stack) {
105 push @revqueue, $head;
106 init_claim( defined $starting_rev ? $head : 'dirty');
107 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: $!";
107 -
108 - _git_diff_parse($diff, $head, "dirty", (
109 - 'author' => gitvar_name("GIT_AUTHOR_IDENT"),
110 - 'author_date' => sprintf("%s +0000",time()),
111 - )
112 - );
113 - close($diff);
108 + my %ident;
109 + @ident{'author', 'author_email', 'author_date'} = $repo->ident('author');
110 + my $diff = $repo->command_output_pipe('diff', '-R', 'HEAD', '--', $filename);
111 + _git_diff_parse($diff, $head, "dirty", %ident);
112 + $repo->command_close_pipe($diff);
113 }
114 handle_rev();
115
@@ -181,8 +180,7 @@ sub git_rev_list {
180 open($revlist, '<' . $rev_file)
181 or die "Failed to open $rev_file : $!";
182 } else {
184 - $revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
185 - or die "Failed to exec git-rev-list: $!";
183 + $revlist = $repo->command_output_pipe('rev-list', '--parents', '--remove-empty', $rev, '--', $file);
184 }
185
186 my @revs;
@@ -191,7 +189,7 @@ sub git_rev_list {
189 my ($rev, @parents) = split /\s+/, $line;
190 push @revs, [ $rev, @parents ];
191 }
194 - close($revlist);
192 + $repo->command_close_pipe($revlist);
193
194 printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
195 return @revs;
@@ -200,8 +198,7 @@ sub git_rev_list {
198 sub find_parent_renames {
199 my ($rev, $file) = @_;
200
203 - my $patch = open_pipe("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
204 - or die "Failed to exec git-diff: $!";
201 + my $patch = $repo->command_output_pipe('diff-tree', '-M50', '-r', '--name-status', '-z', $rev);
202
203 local $/ = "\0";
204 my %bound;
@@ -227,7 +224,7 @@ sub find_parent_renames {
224 }
225 }
226 }
230 - close($patch);
227 + $repo->command_close_pipe($patch);
228
229 return \%bound;
230 }
@@ -236,14 +233,9 @@ sub find_parent_renames {
233 sub git_find_parent {
234 my ($rev, $filename) = @_;
235
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: $!";
241 -
242 - my $parentline = <$revparent>;
243 - chomp $parentline;
244 - my ($revfound,$parent) = split m/\s+/, $parentline;
245 -
246 - close($revparent);
236 + my $parentline = $repo->command_oneline('rev-list', '--remove-empty',
237 + '--parents', '--max-count=1', $rev, '--', $filename);
238 + my ($revfound, $parent) = split m/\s+/, $parentline;
239
240 return $parent;
241 }
@@ -254,13 +246,13 @@ sub git_find_parent {
246 sub git_diff_parse {
247 my ($parent, $rev, %revinfo) = @_;
248
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: $!";
249 + my $diff = $repo->command_output_pipe('diff-tree', '-M', '-p',
250 + $rev, $parent, '--',
251 + $revs{$rev}{'filename'}, $revs{$parent}{'filename'});
252
253 _git_diff_parse($diff, $parent, $rev, %revinfo);
254
263 - close($diff);
255 + $repo->command_close_pipe($diff);
256 }
257
258 sub _git_diff_parse {
@@ -351,36 +343,25 @@ sub git_cat_file {
343 my $blob = git_ls_tree($rev, $filename);
344 die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
345
354 - my $catfile = open_pipe("git","cat-file", "blob", $blob)
355 - or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
356 -
357 - my @lines;
358 - while(<$catfile>) {
359 - chomp;
360 - push @lines, $_;
361 - }
362 - close($catfile);
363 -
346 + my @lines = split(/\n/, $repo->get_object('blob', $blob));
347 + pop @lines unless $lines[$#lines]; # Trailing newline
348 return @lines;
349 }
350
351 sub git_ls_tree {
352 my ($rev, $filename) = @_;
353
370 - my $lstree = open_pipe("git","ls-tree",$rev,$filename)
371 - or die "Failed to call git ls-tree: $!";
372 -
354 + my $lstree = $repo->command_output_pipe('ls-tree', $rev, $filename);
355 my ($mode, $type, $blob, $tfilename);
356 while(<$lstree>) {
357 chomp;
358 ($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
359 last if ($tfilename eq $filename);
360 }
379 - close($lstree);
361 + $repo->command_close_pipe($lstree);
362
363 return $blob if ($tfilename eq $filename);
364 die "git-ls-tree failed to find blob for $filename";
383 -
365 }
366
367
@@ -396,25 +377,17 @@ sub claim_line {
377
378 sub git_commit_info {
379 my ($rev) = @_;
399 - my $commit = open_pipe("git-cat-file", "commit", $rev)
400 - or die "Failed to call git-cat-file: $!";
380 + my $commit = $repo->get_object('commit', $rev);
381
382 my %info;
403 - while(<$commit>) {
404 - chomp;
405 - last if (length $_ == 0);
406 -
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;
383 + while ($commit =~ /(.*?)\n/g) {
384 + my $line = $1;
385 + if ($line =~ s/^author //) {
386 + @info{'author', 'author_email', 'author_date'} = $repo->ident($line);
387 + } elsif ($line =~ s/^committer//) {
388 + @info{'committer', 'committer_email', 'committer_date'} = $repo->ident($line);
389 }
390 }
417 - close($commit);
391
392 return %info;
393 }
@@ -432,81 +405,3 @@ sub format_date {
405 my $t = $timestamp + $minutes * 60;
406 return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
407 }
435 -
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 "$!";
444 - }
445 - my ($val) = <$fh>;
446 - close $fh or die "$!";
447 - chomp($val);
448 - return $val;
449 -}
450 -
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)]);
456 -}
457 -
458 -sub open_pipe {
459 - if ($^O eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
460 - return open_pipe_activestate(@_);
461 - } else {
462 - return open_pipe_normal(@_);
463 - }
464 -}
465 -
466 -sub open_pipe_activestate {
467 - tie *fh, "Git::ActiveStatePipe", @_;
468 - return *fh;
469 -}
470 -
471 -sub open_pipe_normal {
472 - my (@execlist) = @_;
473 -
474 - my $pid = open my $kid, "-|";
475 - defined $pid or die "Cannot fork: $!";
476 -
477 - unless ($pid) {
478 - exec @execlist;
479 - die "Cannot exec @execlist: $!";
480 - }
481 -
482 - return $kid;
483 -}
484 -
485 -package Git::ActiveStatePipe;
486 -use strict;
487 -
488 -sub TIEHANDLE {
489 - my ($class, @params) = @_;
490 - my $cmdline = join " ", @params;
491 - my @data = qx{$cmdline};
492 - bless { i => 0, data => \@data }, $class;
493 -}
494 -
495 -sub READLINE {
496 - my $self = shift;
497 - if ($self->{i} >= scalar @{$self->{data}}) {
498 - return undef;
499 - }
500 - return $self->{'data'}->[ $self->{i}++ ];
501 -}
502 -
503 -sub CLOSE {
504 - my $self = shift;
505 - delete $self->{data};
506 - delete $self->{i};
507 -}
508 -
509 -sub EOF {
510 - my $self = shift;
511 - return ($self->{i} >= scalar @{$self->{data}});
512 -}