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)
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;
14 use PublicInbox::Spawn qw(spawn popen_rd);
17 my ($class, $git_dir) = @_;
18 bless { git_dir => $git_dir }, $class
22 my ($self, $batch, $in, $out, $pid) = @_;
23 return if $self->{$pid};
24 my ($in_r, $in_w, $out_r, $out_w);
26 pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
27 pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
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 my $p = spawn(\@cmd, undef, $redir);
32 defined $p or fail($self, "spawn failed: $!");
35 $self->{$out} = $out_w;
40 my ($self, $obj, $ref) = @_;
43 $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
47 my $head = $in->getline;
48 $head =~ / missing$/ and return undef;
49 $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
50 fail($self, "Unexpected result from git cat-file: $head");
53 my $ref_type = $ref ? ref($ref) : '';
57 $$ref = $size if ($ref_type eq 'SCALAR');
60 if ($ref_type eq 'CODE') {
61 $rv = eval { $ref->($in, \$left) };
66 my $r = read($in, my $x, $left > $max ? $max : $left);
67 defined($r) or fail($self, "read failed: $!");
68 $r == 0 and fail($self, 'exited unexpectedly');
75 my $r = read($in, $buf, $left, $offset);
76 defined($r) or fail($self, "read failed: $!");
77 $r == 0 and fail($self, 'exited unexpectedly');
84 my $r = read($in, my $buf, 1);
85 defined($r) or fail($self, "read failed: $!");
86 fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
87 die $cb_err if $cb_err;
92 sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) }
95 my ($self, $obj) = @_;
96 $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
97 $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);
106 my ($self, $in, $out, $pid) = @_;
107 my $p = delete $self->{$pid} or return;
108 foreach my $f ($in, $out) {
115 my ($self, $msg) = @_;
121 my ($self, @cmd) = @_;
122 @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
127 my ($self, @cmd) = @_;
128 my $fh = $self->popen(@cmd);
129 defined $fh or return;
131 return <$fh> if wantarray;
138 _destroy($self, qw(in out pid));
139 _destroy($self, qw(in_c out_c pid_c));
142 sub DESTROY { cleanup(@_) }
150 PublicInbox::Git - git wrapper
158 use PublicInbox::Git;
159 chomp(my $git_dir = `git rev-parse --git-dir`);
160 $git_dir or die "GIT_DIR= must be specified\n";
161 my $git = PublicInbox::Git->new($git_dir);
165 Unstable API outside of the L</new> method.
166 It requires L<git(1)> to be installed.
174 my $git = PublicInbox::Git->new($git_dir);
176 Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
177 This is the only public API method we support. Everything else
178 in this module is subject to change.
182 L<Git>, L<PublicInbox::Import>
186 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
188 The mail archives are hosted at L<https://public-inbox.org/meta/>
192 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
194 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>