]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Git.pm
5135862e6ad51bc904e880f94175239d59519707
[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
15 sub new {
16         my ($class, $git_dir) = @_;
17         bless { git_dir => $git_dir }, $class
18 }
19
20 sub _bidi_pipe {
21         my ($self, $batch, $in, $out, $pid) = @_;
22         return if $self->{$pid};
23         my ($in_r, $in_w, $out_r, $out_w);
24
25         pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
26         pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
27
28         my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
29         $self->{$pid} = fork;
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";
35         }
36         close $out_r or fail($self, "close failed: $!");
37         close $in_w or fail($self, "close failed: $!");
38         $out_w->autoflush(1);
39         $self->{$out} = $out_w;
40         $self->{$in} = $in_r;
41 }
42
43 sub cat_file {
44         my ($self, $obj, $ref) = @_;
45
46         $self->_bidi_pipe(qw(--batch in out pid));
47         $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
48
49         my $in = $self->{in};
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");
54
55         my $size = $1;
56         my $ref_type = $ref ? ref($ref) : '';
57
58         my $rv;
59         my $left = $size;
60         $$ref = $size if ($ref_type eq 'SCALAR');
61         my $cb_err;
62
63         if ($ref_type eq 'CODE') {
64                 $rv = eval { $ref->($in, \$left) };
65                 $cb_err = $@;
66                 # drain the rest
67                 my $max = 8192;
68                 while ($left > 0) {
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');
72                         $left -= $r;
73                 }
74         } else {
75                 my $offset = 0;
76                 my $buf = '';
77                 while ($left > 0) {
78                         my $r = read($in, $buf, $left, $offset);
79                         defined($r) or fail($self, "read failed: $!");
80                         $r == 0 and fail($self, 'exited unexpectedly');
81                         $left -= $r;
82                         $offset += $r;
83                 }
84                 $rv = \$buf;
85         }
86
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;
91
92         $rv;
93 }
94
95 sub check {
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);
103 }
104
105 sub _destroy {
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};
111                 defined $fh or next;
112                 close $fh;
113                 $self->{$f} = undef;
114         }
115         waitpid $p, 0;
116 }
117
118 sub fail {
119         my ($self, $msg) = @_;
120         cleanup($self);
121         die $msg;
122 }
123
124 sub popen {
125         my ($self, @cmd) = @_;
126         my $mode = '-|';
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");
131         $fh;
132 }
133
134 sub cleanup {
135         my ($self) = @_;
136         _destroy($self, qw(in out pid));
137         _destroy($self, qw(in_c out_c pid_c));
138 }
139
140 sub DESTROY { cleanup(@_) }
141
142 1;