diff --git a/git-annotate.perl b/git-annotate.perl
index 91da6d5b7f8b35592597dbedf2fd7f7c6fc5609e..a6a7a482cdcf34e3a1d545c7d5deab599a7c0d2f 100755 (executable)
--- a/git-annotate.perl
+++ b/git-annotate.perl
use strict;
use Getopt::Long;
use POSIX qw(strftime gmtime);
use strict;
use Getopt::Long;
use POSIX qw(strftime gmtime);
+use File::Basename qw(basename dirname);
sub usage() {
sub usage() {
- print STDERR 'Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
+ print STDERR "Usage: ${\basename $0} [-s] [-S revs-file] file [ revision ]
-l, --long
Show long rev (Defaults off)
-l, --long
Show long rev (Defaults off)
+ -t, --time
+ Show raw timestamp (Defaults off)
-r, --rename
Follow renames (Defaults on).
-S, --rev-file revs-file
-r, --rename
Follow renames (Defaults on).
-S, --rev-file revs-file
- use revs from revs-file instead of calling git-rev-list
+ Use revs from revs-file instead of calling git-rev-list
-h, --help
This message.
-h, --help
This message.
-';
+";
exit(1);
}
exit(1);
}
-our ($help, $longrev, $rename, $starting_rev, $rev_file) = (0, 0, 1);
+our ($help, $longrev, $rename, $rawtime, $starting_rev, $rev_file) = (0, 0, 1);
my $rc = GetOptions( "long|l" => \$longrev,
my $rc = GetOptions( "long|l" => \$longrev,
+ "time|t" => \$rawtime,
"help|h" => \$help,
"rename|r" => \$rename,
"help|h" => \$help,
"rename|r" => \$rename,
- "rev-file|S" => \$rev_file);
-if (!$rc or $help) {
+ "rev-file|S=s" => \$rev_file);
+if (!$rc or $help or !@ARGV) {
usage();
}
usage();
}
}
}
push @revqueue, $head;
}
}
push @revqueue, $head;
-init_claim( defined $starting_rev ? $starting_rev : 'dirty');
+init_claim( defined $starting_rev ? $head : 'dirty');
unless (defined $starting_rev) {
unless (defined $starting_rev) {
- open(DIFF,"-|","git","diff","-R", "HEAD", "--",$filename)
+ my $diff = open_pipe("git","diff","-R", "HEAD", "--",$filename)
or die "Failed to call git diff to check for dirty state: $!";
or die "Failed to call git diff to check for dirty state: $!";
- _git_diff_parse(*DIFF, $head, "dirty", (
+ _git_diff_parse($diff, $head, "dirty", (
'author' => gitvar_name("GIT_AUTHOR_IDENT"),
'author_date' => sprintf("%s +0000",time()),
)
);
'author' => gitvar_name("GIT_AUTHOR_IDENT"),
'author_date' => sprintf("%s +0000",time()),
)
);
- close(DIFF);
+ close($diff);
}
handle_rev();
}
handle_rev();
}
printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
}
printf("%s\t(%10s\t%10s\t%d)%s\n", $rev, $committer,
- format_date($date), $i++, $output);
+ format_date($date), ++$i, $output);
}
sub init_claim {
}
sub init_claim {
sub git_rev_list {
my ($rev, $file) = @_;
sub git_rev_list {
my ($rev, $file) = @_;
+ my $revlist;
if ($rev_file) {
if ($rev_file) {
- open(P, '<' . $rev_file);
+ open($revlist, '<' . $rev_file)
+ or die "Failed to open $rev_file : $!";
} else {
} else {
- open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)
+ $revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
or die "Failed to exec git-rev-list: $!";
}
my @revs;
or die "Failed to exec git-rev-list: $!";
}
my @revs;
- while(my $line = <P>) {
+ while(my $line = <$revlist>) {
chomp $line;
my ($rev, @parents) = split /\s+/, $line;
push @revs, [ $rev, @parents ];
}
chomp $line;
my ($rev, @parents) = split /\s+/, $line;
push @revs, [ $rev, @parents ];
}
- close(P);
+ close($revlist);
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
return @revs;
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
return @revs;
sub find_parent_renames {
my ($rev, $file) = @_;
sub find_parent_renames {
my ($rev, $file) = @_;
- open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
+ my $patch = open_pipe("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
or die "Failed to exec git-diff: $!";
local $/ = "\0";
my %bound;
or die "Failed to exec git-diff: $!";
local $/ = "\0";
my %bound;
- my $junk = <P>;
- while (my $change = <P>) {
+ my $junk = <$patch>;
+ while (my $change = <$patch>) {
chomp $change;
chomp $change;
- my $filename = <P>;
+ my $filename = <$patch>;
+ if (!defined $filename) {
+ next;
+ }
chomp $filename;
if ($change =~ m/^[AMD]$/ ) {
next;
} elsif ($change =~ m/^R/ ) {
my $oldfilename = $filename;
chomp $filename;
if ($change =~ m/^[AMD]$/ ) {
next;
} elsif ($change =~ m/^R/ ) {
my $oldfilename = $filename;
- $filename = <P>;
+ $filename = <$patch>;
chomp $filename;
if ( $file eq $filename ) {
my $parent = git_find_parent($rev, $oldfilename);
chomp $filename;
if ( $file eq $filename ) {
my $parent = git_find_parent($rev, $oldfilename);
}
}
}
}
}
}
- close(P);
+ close($patch);
return \%bound;
}
return \%bound;
}
sub git_find_parent {
my ($rev, $filename) = @_;
sub git_find_parent {
my ($rev, $filename) = @_;
- open(REVPARENT,"-|","git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
+ my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
or die "Failed to open git-rev-list to find a single parent: $!";
or die "Failed to open git-rev-list to find a single parent: $!";
- my $parentline = <REVPARENT>;
+ my $parentline = <$revparent>;
chomp $parentline;
my ($revfound,$parent) = split m/\s+/, $parentline;
chomp $parentline;
my ($revfound,$parent) = split m/\s+/, $parentline;
- close(REVPARENT);
+ close($revparent);
return $parent;
}
return $parent;
}
sub git_diff_parse {
my ($parent, $rev, %revinfo) = @_;
sub git_diff_parse {
my ($parent, $rev, %revinfo) = @_;
- open(DIFF,"-|","git-diff-tree","-M","-p",$rev,$parent,"--",
+ my $diff = open_pipe("git-diff-tree","-M","-p",$rev,$parent,"--",
$revs{$rev}{'filename'}, $revs{$parent}{'filename'})
or die "Failed to call git-diff for annotation: $!";
$revs{$rev}{'filename'}, $revs{$parent}{'filename'})
or die "Failed to call git-diff for annotation: $!";
- _git_diff_parse(*DIFF, $parent, $rev, %revinfo);
+ _git_diff_parse($diff, $parent, $rev, %revinfo);
- close(DIFF);
+ close($diff);
}
sub _git_diff_parse {
}
sub _git_diff_parse {
my $gotheader = 0;
my ($remstart);
my ($hunk_start, $hunk_index);
my $gotheader = 0;
my ($remstart);
my ($hunk_start, $hunk_index);
- while(<DIFF>) {
+ while(<$diff>) {
chomp;
if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
$remstart = $1;
chomp;
if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
$remstart = $1;
}
$ri++;
}
$ri++;
+ } elsif (m/^\\/) {
+ ;
+ # Skip \No newline at end of file.
+ # But this can be internationalized, so only look
+ # for an initial \
+
} else {
if (substr($_,1) ne get_line($slines,$ri) ) {
die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
} else {
if (substr($_,1) ne get_line($slines,$ri) ) {
die sprintf("Line %d (%d) does not match:\n|%s\n|%s\n%s => %s\n",
return () unless defined $rev && defined $filename;
my $blob = git_ls_tree($rev, $filename);
return () unless defined $rev && defined $filename;
my $blob = git_ls_tree($rev, $filename);
+ die "Failed to find a blob for $filename in rev $rev\n" if !defined $blob;
- open(C,"-|","git","cat-file", "blob", $blob)
+ my $catfile = open_pipe("git","cat-file", "blob", $blob)
or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
my @lines;
or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
my @lines;
- while(<C>) {
+ while(<$catfile>) {
chomp;
push @lines, $_;
}
chomp;
push @lines, $_;
}
- close(C);
+ close($catfile);
return @lines;
}
return @lines;
}
sub git_ls_tree {
my ($rev, $filename) = @_;
sub git_ls_tree {
my ($rev, $filename) = @_;
- open(T,"-|","git","ls-tree",$rev,$filename)
+ my $lstree = open_pipe("git","ls-tree",$rev,$filename)
or die "Failed to call git ls-tree: $!";
my ($mode, $type, $blob, $tfilename);
or die "Failed to call git ls-tree: $!";
my ($mode, $type, $blob, $tfilename);
- while(<T>) {
+ while(<$lstree>) {
+ chomp;
($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
last if ($tfilename eq $filename);
}
($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
last if ($tfilename eq $filename);
}
- close(T);
+ close($lstree);
- return $blob if $filename eq $filename;
+ return $blob if ($tfilename eq $filename);
die "git-ls-tree failed to find blob for $filename";
}
die "git-ls-tree failed to find blob for $filename";
}
sub git_commit_info {
my ($rev) = @_;
sub git_commit_info {
my ($rev) = @_;
- open(COMMIT, "-|","git-cat-file", "commit", $rev)
+ my $commit = open_pipe("git-cat-file", "commit", $rev)
or die "Failed to call git-cat-file: $!";
my %info;
or die "Failed to call git-cat-file: $!";
my %info;
- while(<COMMIT>) {
+ while(<$commit>) {
chomp;
last if (length $_ == 0);
chomp;
last if (length $_ == 0);
$info{'committer_date'} = $3;
}
}
$info{'committer_date'} = $3;
}
}
- close(COMMIT);
+ close($commit);
return %info;
}
sub format_date {
return %info;
}
sub format_date {
+ if ($rawtime) {
+ return $_[0];
+ }
my ($timestamp, $timezone) = split(' ', $_[0]);
my ($timestamp, $timezone) = split(' ', $_[0]);
-
- return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($timestamp));
+ my $minutes = abs($timezone);
+ $minutes = int($minutes / 100) * 60 + ($minutes % 100);
+ if ($timezone < 0) {
+ $minutes = -$minutes;
+ }
+ my $t = $timestamp + $minutes * 60;
+ return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
}
# Copied from git-send-email.perl - We need a Git.pm module..
}
# Copied from git-send-email.perl - We need a Git.pm module..
return join(' ', @field[0...(@field-4)]);
}
return join(' ', @field[0...(@field-4)]);
}
+sub open_pipe {
+ if ($^O eq '##INSERT_ACTIVESTATE_STRING_HERE##') {
+ return open_pipe_activestate(@_);
+ } else {
+ return open_pipe_normal(@_);
+ }
+}
+
+sub open_pipe_activestate {
+ tie *fh, "Git::ActiveStatePipe", @_;
+ return *fh;
+}
+
+sub open_pipe_normal {
+ my (@execlist) = @_;
+
+ my $pid = open my $kid, "-|";
+ defined $pid or die "Cannot fork: $!";
+
+ unless ($pid) {
+ exec @execlist;
+ die "Cannot exec @execlist: $!";
+ }
+
+ return $kid;
+}
+
+package Git::ActiveStatePipe;
+use strict;
+
+sub TIEHANDLE {
+ my ($class, @params) = @_;
+ my $cmdline = join " ", @params;
+ my @data = qx{$cmdline};
+ bless { i => 0, data => \@data }, $class;
+}
+
+sub READLINE {
+ my $self = shift;
+ if ($self->{i} >= scalar @{$self->{data}}) {
+ return undef;
+ }
+ return $self->{'data'}->[ $self->{i}++ ];
+}
+
+sub CLOSE {
+ my $self = shift;
+ delete $self->{data};
+ delete $self->{i};
+}
+
+sub EOF {
+ my $self = shift;
+ return ($self->{i} >= scalar @{$self->{data}});
+}