1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
3 # This is based on code in Git.pm which is GPLv2, but modified to avoid
4 # dependence on environment variables for compatibility with mod_perl.
5 # There are also API changes to simplify our usage and data set.
6 package PublicInbox::GitCatFile;
9 use IPC::Open2 qw(open2);
12 my ($class, $git_dir) = @_;
13 bless { git_dir => $git_dir }, $class;
18 return if $self->{pid};
20 my $pid = open2($in, $out, 'git', "--git-dir=$self->{git_dir}",
21 qw(cat-file --batch));
29 my ($self, $object) = @_;
31 $self->_cat_file_begin;
32 print { $self->{out} } $object, "\n" or die "write error: $!\n";
36 $head =~ / missing$/ and return undef;
37 $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
38 die "Unexpected result from git cat-file: $head\n";
41 my $bytes_left = $size;
46 my $read = read($in, $buf, $bytes_left);
47 defined($read) or die "read pipe failed: $!\n";
52 my $read = read($in, $buf, 1);
53 defined($read) or die "read pipe failed: $!\n";
54 if ($read != 1 || $buf ne "\n") {
55 die "newline missing after blob\n";
62 my $pid = $self->{pid} or return;
64 foreach my $f (qw(in out)) {