]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Git.pm
localize $/ in more places to avoid potential problems
[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         $out_w->autoflush(1);
33         $self->{$out} = $out_w;
34         $self->{$in} = $in_r;
35 }
36
37 sub cat_file {
38         my ($self, $obj, $ref) = @_;
39
40         $self->_bidi_pipe(qw(--batch in out pid));
41         $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
42
43         my $in = $self->{in};
44         local $/ = "\n";
45         my $head = $in->getline;
46         $head =~ / missing$/ and return undef;
47         $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
48                 fail($self, "Unexpected result from git cat-file: $head");
49
50         my $size = $1;
51         my $ref_type = $ref ? ref($ref) : '';
52
53         my $rv;
54         my $left = $size;
55         $$ref = $size if ($ref_type eq 'SCALAR');
56         my $cb_err;
57
58         if ($ref_type eq 'CODE') {
59                 $rv = eval { $ref->($in, \$left) };
60                 $cb_err = $@;
61                 # drain the rest
62                 my $max = 8192;
63                 while ($left > 0) {
64                         my $r = read($in, my $x, $left > $max ? $max : $left);
65                         defined($r) or fail($self, "read failed: $!");
66                         $r == 0 and fail($self, 'exited unexpectedly');
67                         $left -= $r;
68                 }
69         } else {
70                 my $offset = 0;
71                 my $buf = '';
72                 while ($left > 0) {
73                         my $r = read($in, $buf, $left, $offset);
74                         defined($r) or fail($self, "read failed: $!");
75                         $r == 0 and fail($self, 'exited unexpectedly');
76                         $left -= $r;
77                         $offset += $r;
78                 }
79                 $rv = \$buf;
80         }
81
82         my $r = read($in, my $buf, 1);
83         defined($r) or fail($self, "read failed: $!");
84         fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
85         die $cb_err if $cb_err;
86
87         $rv;
88 }
89
90 sub check {
91         my ($self, $obj) = @_;
92         $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
93         $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
94         local $/ = "\n";
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 = delete $self->{$pid} or return;
104         foreach my $f ($in, $out) {
105                 delete $self->{$f};
106         }
107         waitpid $p, 0;
108 }
109
110 sub fail {
111         my ($self, $msg) = @_;
112         cleanup($self);
113         die $msg;
114 }
115
116 sub popen {
117         my ($self, @cmd) = @_;
118         @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
119         popen_rd(\@cmd);
120 }
121
122 sub qx {
123         my ($self, @cmd) = @_;
124         my $fh = $self->popen(@cmd);
125         local $/ = "\n";
126         return <$fh> if wantarray;
127         local $/;
128         <$fh>
129 }
130
131 sub cleanup {
132         my ($self) = @_;
133         _destroy($self, qw(in out pid));
134         _destroy($self, qw(in_c out_c pid_c));
135 }
136
137 sub DESTROY { cleanup(@_) }
138
139 1;
140 __END__
141 =pod
142
143 =head1 NAME
144
145 PublicInbox::Git - git wrapper
146
147 =head1 VERSION
148
149 version 1.0
150
151 =head1 SYNOPSIS
152
153         use PublicInbox::Git;
154         chomp(my $git_dir = `git rev-parse --git-dir`);
155         $git_dir or die "GIT_DIR= must be specified\n";
156         my $git = PublicInbox::Git->new($git_dir);
157
158 =head1 DESCRIPTION
159
160 Unstable API outside of the L</new> method.
161 It requires L<git(1)> to be installed.
162
163 =head1 METHODS
164
165 =cut
166
167 =head2 new
168
169         my $git = PublicInbox::Git->new($git_dir);
170
171 Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
172 This is the only public API method we support.  Everything else
173 in this module is subject to change.
174
175 =head1 SEE ALSO
176
177 L<Git>, L<PublicInbox::Import>
178
179 =head1 CONTACT
180
181 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
182
183 The mail archives are hosted at L<https://public-inbox.org/meta/>
184
185 =head1 COPYRIGHT
186
187 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
188
189 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>
190
191 =cut