提交 7182530d 编写于 作者: A Adam Roben 提交者: Junio C Hamano

Git.pm: Add hash_and_insert_object and cat_blob

These functions are more efficient ways of executing `git hash-object -w` and
`git cat-file blob` when you are dealing with many files/objects.
Signed-off-by: NAdam Roben <aroben@apple.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 d1a29af9
...@@ -39,6 +39,10 @@ =head1 SYNOPSIS ...@@ -39,6 +39,10 @@ =head1 SYNOPSIS
my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ], my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ],
STDERR => 0 ); STDERR => 0 );
my $sha1 = $repo->hash_and_insert_object('file.txt');
my $tempfile = tempfile();
my $size = $repo->cat_blob($sha1, $tempfile);
=cut =cut
...@@ -218,7 +222,6 @@ sub repository { ...@@ -218,7 +222,6 @@ sub repository {
bless $self, $class; bless $self, $class;
} }
=back =back
=head1 METHODS =head1 METHODS
...@@ -734,6 +737,147 @@ sub hash_object { ...@@ -734,6 +737,147 @@ sub hash_object {
} }
=item hash_and_insert_object ( FILENAME )
Compute the SHA1 object id of the given C<FILENAME> and add the object to the
object database.
The function returns the SHA1 hash.
=cut
# TODO: Support for passing FILEHANDLE instead of FILENAME
sub hash_and_insert_object {
my ($self, $filename) = @_;
carp "Bad filename \"$filename\"" if $filename =~ /[\r\n]/;
$self->_open_hash_and_insert_object_if_needed();
my ($in, $out) = ($self->{hash_object_in}, $self->{hash_object_out});
unless (print $out $filename, "\n") {
$self->_close_hash_and_insert_object();
throw Error::Simple("out pipe went bad");
}
chomp(my $hash = <$in>);
unless (defined($hash)) {
$self->_close_hash_and_insert_object();
throw Error::Simple("in pipe went bad");
}
return $hash;
}
sub _open_hash_and_insert_object_if_needed {
my ($self) = @_;
return if defined($self->{hash_object_pid});
($self->{hash_object_pid}, $self->{hash_object_in},
$self->{hash_object_out}, $self->{hash_object_ctx}) =
command_bidi_pipe(qw(hash-object -w --stdin-paths));
}
sub _close_hash_and_insert_object {
my ($self) = @_;
return unless defined($self->{hash_object_pid});
my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
command_close_bidi_pipe($self->{@vars});
delete $self->{@vars};
}
=item cat_blob ( SHA1, FILEHANDLE )
Prints the contents of the blob identified by C<SHA1> to C<FILEHANDLE> and
returns the number of bytes printed.
=cut
sub cat_blob {
my ($self, $sha1, $fh) = @_;
$self->_open_cat_blob_if_needed();
my ($in, $out) = ($self->{cat_blob_in}, $self->{cat_blob_out});
unless (print $out $sha1, "\n") {
$self->_close_cat_blob();
throw Error::Simple("out pipe went bad");
}
my $description = <$in>;
if ($description =~ / missing$/) {
carp "$sha1 doesn't exist in the repository";
return 0;
}
if ($description !~ /^[0-9a-fA-F]{40} \S+ (\d+)$/) {
carp "Unexpected result returned from git cat-file";
return 0;
}
my $size = $1;
my $blob;
my $bytesRead = 0;
while (1) {
my $bytesLeft = $size - $bytesRead;
last unless $bytesLeft;
my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;
my $read = read($in, $blob, $bytesToRead, $bytesRead);
unless (defined($read)) {
$self->_close_cat_blob();
throw Error::Simple("in pipe went bad");
}
$bytesRead += $read;
}
# Skip past the trailing newline.
my $newline;
my $read = read($in, $newline, 1);
unless (defined($read)) {
$self->_close_cat_blob();
throw Error::Simple("in pipe went bad");
}
unless ($read == 1 && $newline eq "\n") {
$self->_close_cat_blob();
throw Error::Simple("didn't find newline after blob");
}
unless (print $fh $blob) {
$self->_close_cat_blob();
throw Error::Simple("couldn't write to passed in filehandle");
}
return $size;
}
sub _open_cat_blob_if_needed {
my ($self) = @_;
return if defined($self->{cat_blob_pid});
($self->{cat_blob_pid}, $self->{cat_blob_in},
$self->{cat_blob_out}, $self->{cat_blob_ctx}) =
command_bidi_pipe(qw(cat-file --batch));
}
sub _close_cat_blob {
my ($self) = @_;
return unless defined($self->{cat_blob_pid});
my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx);
command_close_bidi_pipe($self->{@vars});
delete $self->{@vars};
}
=back =back
...@@ -951,7 +1095,11 @@ sub _cmd_close { ...@@ -951,7 +1095,11 @@ sub _cmd_close {
} }
sub DESTROY { } sub DESTROY {
my ($self) = @_;
$self->_close_hash_and_insert_object();
$self->_close_cat_blob();
}
# Pipe implementation for ActiveState Perl. # Pipe implementation for ActiveState Perl.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册