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>
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);
15 use base qw(Exporter);
16 our @EXPORT_OK = qw(git_unquote);
28 # unquote pathnames used by git, see quote.c::unquote_c_style.c in git.git
30 return $_[0] unless ($_[0] =~ /\A"(.*)"\z/);
32 $_[0] =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
33 $_[0] =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
38 my ($class, $git_dir) = @_;
41 bless { git_dir => $git_dir, st => \@st }, $class
44 sub alternates_changed {
46 my $alt = "$self->{git_dir}/objects/info/alternates";
47 my @st = stat($alt) or return 0;
48 my $old_st = $self->{st};
49 # 10 - ctime, 7 - size
50 return 0 if ($st[10] == $old_st->[10] && $st[7] == $old_st->[7]);
55 my ($self, $batch, $in, $out, $pid) = @_;
56 return if $self->{$pid};
57 my ($in_r, $in_w, $out_r, $out_w);
59 pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
60 pipe($out_r, $out_w) or fail($self, "pipe failed: $!");
61 if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ
62 fcntl($out_w, 1031, 4096);
63 fcntl($in_w, 1031, 4096) if $batch eq '--batch-check';
66 my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
67 my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
68 my $p = spawn(\@cmd, undef, $redir);
69 defined $p or fail($self, "spawn failed: $!");
72 $self->{$out} = $out_w;
77 my ($self, $obj, $ref) = @_;
78 my ($retried, $in, $head);
82 $self->{out}->print($obj, "\n") or fail($self, "write error: $!");
87 if ($head =~ / missing$/) {
88 if (!$retried && alternates_changed($self)) {
95 $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
96 fail($self, "Unexpected result from git cat-file: $head");
99 my $ref_type = $ref ? ref($ref) : '';
103 $$ref = $size if ($ref_type eq 'SCALAR');
106 if ($ref_type eq 'CODE') {
107 $rv = eval { $ref->($in, \$left) };
112 my $r = read($in, my $x, $left > $max ? $max : $left);
113 defined($r) or fail($self, "read failed: $!");
114 $r == 0 and fail($self, 'exited unexpectedly');
121 my $r = read($in, $buf, $left, $offset);
122 defined($r) or fail($self, "read failed: $!");
123 $r == 0 and fail($self, 'exited unexpectedly');
130 my $r = read($in, my $buf, 1);
131 defined($r) or fail($self, "read failed: $!");
132 fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
133 die $cb_err if $cb_err;
138 sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) }
141 my ($self, $obj) = @_;
142 $self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
143 $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
145 chomp(my $line = $self->{in_c}->getline);
146 my ($hex, $type, $size) = split(' ', $line);
147 return if $type eq 'missing';
148 ($hex, $type, $size);
152 my ($self, $in, $out, $pid) = @_;
153 my $p = delete $self->{$pid} or return;
154 foreach my $f ($in, $out) {
161 my ($self, $msg) = @_;
167 my ($self, @cmd) = @_;
168 @cmd = ('git', "--git-dir=$self->{git_dir}", @cmd);
173 my ($self, @cmd) = @_;
174 my $fh = $self->popen(@cmd);
175 defined $fh or return;
177 return <$fh> if wantarray;
184 _destroy($self, qw(in out pid));
185 _destroy($self, qw(in_c out_c pid_c));
188 # assuming a well-maintained repo, this should be a somewhat
189 # accurate estimation of its size
190 # TODO: show this in the WWW UI as a hint to potential cloners
194 foreach my $p (glob("$self->{git_dir}/objects/pack/*.pack")) {
200 sub DESTROY { cleanup(@_) }
208 PublicInbox::Git - git wrapper
216 use PublicInbox::Git;
217 chomp(my $git_dir = `git rev-parse --git-dir`);
218 $git_dir or die "GIT_DIR= must be specified\n";
219 my $git = PublicInbox::Git->new($git_dir);
223 Unstable API outside of the L</new> method.
224 It requires L<git(1)> to be installed.
232 my $git = PublicInbox::Git->new($git_dir);
234 Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
235 This is the only public API method we support. Everything else
236 in this module is subject to change.
240 L<Git>, L<PublicInbox::Import>
244 All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
246 The mail archives are hosted at L<https://public-inbox.org/meta/>
250 Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>
252 License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>