1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
4 # Used to read files from a git repository without excessive forking.
5 # Used in our web interfaces as well as our -nntpd server.
6 # This is based on code in Git.pm which is GPLv2, but modified to avoid
7 # dependence on environment variables for compatibility with mod_perl.
8 # There are also API changes to simplify our usage and data set.
9 package PublicInbox::Git;
16 my ($class, $git_dir) = @_;
17 bless { git_dir => $git_dir }, $class
21 my ($self, $batch, $in, $out, $pid) = @_;
22 return if $self->{$pid};
23 my ($in_r, $in_w, $out_r, $out_w);
25 pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
26 pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
28 my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
30 defined $self->{$pid} or fail($self, "fork failed: $!");
31 if ($self->{$pid} == 0) {
32 dup2(fileno($out_r), 0) or die "redirect stdin failed: $!\n";
33 dup2(fileno($in_w), 1) or die "redirect stdout failed: $!\n";
34 exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
36 close $out_r or fail($self, "close failed: $!");
37 close $in_w or fail($self, "close failed: $!");
39 $self->{$out} = $out_w;
44 my ($self, $obj, $ref) = @_;
46 $self->_bidi_pipe(qw(--batch in out pid));
47 $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
50 my $head = $in->getline;
51 $head =~ / missing$/ and return undef;
52 $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
53 fail($self, "Unexpected result from git cat-file: $head");
56 my $ref_type = $ref ? ref($ref) : '';
60 $$ref = $size if ($ref_type eq 'SCALAR');
63 if ($ref_type eq 'CODE') {
64 $rv = eval { $ref->($in, \$left) };
69 my $r = read($in, my $x, $left > $max ? $max : $left);
70 defined($r) or fail($self, "read failed: $!");
71 $r == 0 and fail($self, 'exited unexpectedly');
78 my $r = read($in, $buf, $left, $offset);
79 defined($r) or fail($self, "read failed: $!");
80 $r == 0 and fail($self, 'exited unexpectedly');
87 my $r = read($in, my $buf, 1);
88 defined($r) or fail($self, "read failed: $!");
89 fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
90 die $cb_err if $cb_err;
96 my ($self, $obj) = @_;
97 $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
98 $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
99 chomp(my $line = $self->{in_c}->getline);
100 my ($hex, $type, $size) = split(' ', $line);
101 return if $type eq 'missing';
102 ($hex, $type, $size);
106 my ($self, $in, $out, $pid) = @_;
107 my $p = $self->{$pid} or return;
108 $self->{$pid} = undef;
109 foreach my $f ($in, $out) {
110 my $fh = $self->{$f};
119 my ($self, $msg) = @_;
125 my ($self, @cmd) = @_;
127 $mode = shift @cmd if ($cmd[0] eq '|-');
128 @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
129 my $pid = open my $fh, $mode, @cmd or
130 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
136 _destroy($self, qw(in out pid));
137 _destroy($self, qw(in_c out_c pid_c));
140 sub DESTROY { cleanup(@_) }