]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Git.pm
git: use built-in spawn implementation for vfork
[public-inbox.git] / lib / PublicInbox / Git.pm
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)
3 #
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;
10 use strict;
11 use warnings;
12 use POSIX qw(dup2);
13 require IO::Handle;
14 use PublicInbox::Spawn qw(spawn popen_rd);
15
16 sub new {
17         my ($class, $git_dir) = @_;
18         bless { git_dir => $git_dir }, $class
19 }
20
21 sub _bidi_pipe {
22         my ($self, $batch, $in, $out, $pid) = @_;
23         return if $self->{$pid};
24         my ($in_r, $in_w, $out_r, $out_w);
25
26         pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
27         pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
28
29         my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
30         my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
31         $self->{$pid} = spawn(\@cmd, undef, $redir);
32         close $out_r or fail($self, "close failed: $!");
33         close $in_w or fail($self, "close failed: $!");
34         $out_w->autoflush(1);
35         $self->{$out} = $out_w;
36         $self->{$in} = $in_r;
37 }
38
39 sub cat_file {
40         my ($self, $obj, $ref) = @_;
41
42         $self->_bidi_pipe(qw(--batch in out pid));
43         $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
44
45         my $in = $self->{in};
46         my $head = $in->getline;
47         $head =~ / missing$/ and return undef;
48         $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
49                 fail($self, "Unexpected result from git cat-file: $head");
50
51         my $size = $1;
52         my $ref_type = $ref ? ref($ref) : '';
53
54         my $rv;
55         my $left = $size;
56         $$ref = $size if ($ref_type eq 'SCALAR');
57         my $cb_err;
58
59         if ($ref_type eq 'CODE') {
60                 $rv = eval { $ref->($in, \$left) };
61                 $cb_err = $@;
62                 # drain the rest
63                 my $max = 8192;
64                 while ($left > 0) {
65                         my $r = read($in, my $x, $left > $max ? $max : $left);
66                         defined($r) or fail($self, "read failed: $!");
67                         $r == 0 and fail($self, 'exited unexpectedly');
68                         $left -= $r;
69                 }
70         } else {
71                 my $offset = 0;
72                 my $buf = '';
73                 while ($left > 0) {
74                         my $r = read($in, $buf, $left, $offset);
75                         defined($r) or fail($self, "read failed: $!");
76                         $r == 0 and fail($self, 'exited unexpectedly');
77                         $left -= $r;
78                         $offset += $r;
79                 }
80                 $rv = \$buf;
81         }
82
83         my $r = read($in, my $buf, 1);
84         defined($r) or fail($self, "read failed: $!");
85         fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
86         die $cb_err if $cb_err;
87
88         $rv;
89 }
90
91 sub check {
92         my ($self, $obj) = @_;
93         $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
94         $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
95         chomp(my $line = $self->{in_c}->getline);
96         my ($hex, $type, $size) = split(' ', $line);
97         return if $type eq 'missing';
98         ($hex, $type, $size);
99 }
100
101 sub _destroy {
102         my ($self, $in, $out, $pid) = @_;
103         my $p = $self->{$pid} or return;
104         $self->{$pid} = undef;
105         foreach my $f ($in, $out) {
106                 my $fh = $self->{$f};
107                 defined $fh or next;
108                 close $fh;
109                 $self->{$f} = undef;
110         }
111         waitpid $p, 0;
112 }
113
114 sub fail {
115         my ($self, $msg) = @_;
116         cleanup($self);
117         die $msg;
118 }
119
120 sub popen {
121         my ($self, @cmd) = @_;
122         @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
123         popen_rd(\@cmd);
124 }
125
126 sub cleanup {
127         my ($self) = @_;
128         _destroy($self, qw(in out pid));
129         _destroy($self, qw(in_c out_c pid_c));
130 }
131
132 sub DESTROY { cleanup(@_) }
133
134 1;