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