]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Git.pm
shrink low-bandwidth pipes under Linux
[public-inbox.git] / lib / PublicInbox / Git.pm
1 # Copyright (C) 2014-2018 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         my @st;
19         $st[7] = $st[10] = 0;
20         bless { git_dir => $git_dir, st => \@st }, $class
21 }
22
23 sub alternates_changed {
24         my ($self) = @_;
25         my $alt = "$self->{git_dir}/objects/info/alternates";
26         my @st = stat($alt) or return 0;
27         my $old_st = $self->{st};
28         # 10 - ctime, 7 - size
29         return 0 if ($st[10] == $old_st->[10] && $st[7] == $old_st->[7]);
30         $self->{st} = \@st;
31 }
32
33 sub _bidi_pipe {
34         my ($self, $batch, $in, $out, $pid) = @_;
35         return if $self->{$pid};
36         my ($in_r, $in_w, $out_r, $out_w);
37
38         pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
39         pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
40         if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ
41                 fcntl($out_w, 1031, 4096);
42                 fcntl($in_w, 1031, 4096) if $batch eq '--batch-check';
43         }
44
45         my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
46         my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
47         my $p = spawn(\@cmd, undef, $redir);
48         defined $p or fail($self, "spawn failed: $!");
49         $self->{$pid} = $p;
50         $out_w->autoflush(1);
51         $self->{$out} = $out_w;
52         $self->{$in} = $in_r;
53 }
54
55 sub cat_file {
56         my ($self, $obj, $ref) = @_;
57         my ($retried, $in, $head);
58
59 again:
60         batch_prepare($self);
61         $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
62
63         $in = $self->{in};
64         local $/ = "\n";
65         $head = $in->getline;
66         if ($head =~ / missing$/) {
67                 if (!$retried && alternates_changed($self)) {
68                         $retried = 1;
69                         cleanup($self);
70                         goto again;
71                 }
72                 return;
73         }
74         $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
75                 fail($self, "Unexpected result from git cat-file: $head");
76
77         my $size = $1;
78         my $ref_type = $ref ? ref($ref) : '';
79
80         my $rv;
81         my $left = $size;
82         $$ref = $size if ($ref_type eq 'SCALAR');
83         my $cb_err;
84
85         if ($ref_type eq 'CODE') {
86                 $rv = eval { $ref->($in, \$left) };
87                 $cb_err = $@;
88                 # drain the rest
89                 my $max = 8192;
90                 while ($left > 0) {
91                         my $r = read($in, my $x, $left > $max ? $max : $left);
92                         defined($r) or fail($self, "read failed: $!");
93                         $r == 0 and fail($self, 'exited unexpectedly');
94                         $left -= $r;
95                 }
96         } else {
97                 my $offset = 0;
98                 my $buf = '';
99                 while ($left > 0) {
100                         my $r = read($in, $buf, $left, $offset);
101                         defined($r) or fail($self, "read failed: $!");
102                         $r == 0 and fail($self, 'exited unexpectedly');
103                         $left -= $r;
104                         $offset += $r;
105                 }
106                 $rv = \$buf;
107         }
108
109         my $r = read($in, my $buf, 1);
110         defined($r) or fail($self, "read failed: $!");
111         fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
112         die $cb_err if $cb_err;
113
114         $rv;
115 }
116
117 sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) }
118
119 sub check {
120         my ($self, $obj) = @_;
121         $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
122         $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
123         local $/ = "\n";
124         chomp(my $line = $self->{in_c}->getline);
125         my ($hex, $type, $size) = split(' ', $line);
126         return if $type eq 'missing';
127         ($hex, $type, $size);
128 }
129
130 sub _destroy {
131         my ($self, $in, $out, $pid) = @_;
132         my $p = delete $self->{$pid} or return;
133         foreach my $f ($in, $out) {
134                 delete $self->{$f};
135         }
136         waitpid $p, 0;
137 }
138
139 sub fail {
140         my ($self, $msg) = @_;
141         cleanup($self);
142         die $msg;
143 }
144
145 sub popen {
146         my ($self, @cmd) = @_;
147         @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
148         popen_rd(\@cmd);
149 }
150
151 sub qx {
152         my ($self, @cmd) = @_;
153         my $fh = $self->popen(@cmd);
154         defined $fh or return;
155         local $/ = "\n";
156         return <$fh> if wantarray;
157         local $/;
158         <$fh>
159 }
160
161 sub cleanup {
162         my ($self) = @_;
163         _destroy($self, qw(in out pid));
164         _destroy($self, qw(in_c out_c pid_c));
165 }
166
167 # assuming a well-maintained repo, this should be a somewhat
168 # accurate estimation of its size
169 # TODO: show this in the WWW UI as a hint to potential cloners
170 sub packed_bytes {
171         my ($self) = @_;
172         my $n = 0;
173         foreach my $p (glob("$self->{git_dir}/objects/pack/*.pack")) {
174                 $n += -s $p;
175         }
176         $n
177 }
178
179 sub DESTROY { cleanup(@_) }
180
181 1;
182 __END__
183 =pod
184
185 =head1 NAME
186
187 PublicInbox::Git - git wrapper
188
189 =head1 VERSION
190
191 version 1.0
192
193 =head1 SYNOPSIS
194
195         use PublicInbox::Git;
196         chomp(my $git_dir = `git rev-parse --git-dir`);
197         $git_dir or die "GIT_DIR= must be specified\n";
198         my $git = PublicInbox::Git->new($git_dir);
199
200 =head1 DESCRIPTION
201
202 Unstable API outside of the L</new> method.
203 It requires L<git(1)> to be installed.
204
205 =head1 METHODS
206
207 =cut
208
209 =head2 new
210
211         my $git = PublicInbox::Git->new($git_dir);
212
213 Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
214 This is the only public API method we support.  Everything else
215 in this module is subject to change.
216
217 =head1 SEE ALSO
218
219 L<Git>, L<PublicInbox::Import>
220
221 =head1 CONTACT
222
223 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
224
225 The mail archives are hosted at L<https://public-inbox.org/meta/>
226
227 =head1 COPYRIGHT
228
229 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
230
231 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>
232
233 =cut