diff --git a/perl/Git.pm b/perl/Git.pm index c279bfb244..0b9f8dd5b5 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -928,22 +928,26 @@ sub cat_blob { } my $size = $1; - - my $blob; my $bytesRead = 0; while (1) { + my $blob; my $bytesLeft = $size - $bytesRead; last unless $bytesLeft; my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024; - my $read = read($in, $blob, $bytesToRead, $bytesRead); + my $read = read($in, $blob, $bytesToRead); unless (defined($read)) { $self->_close_cat_blob(); throw Error::Simple("in pipe went bad"); } $bytesRead += $read; + + unless (print $fh $blob) { + $self->_close_cat_blob(); + throw Error::Simple("couldn't write to passed in filehandle"); + } } # Skip past the trailing newline. @@ -958,11 +962,6 @@ sub 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; }