public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 893df71eb2b3ccb4a6b19c8822ba2c963377e69f 8111 bytes (raw)
$ git show repobrowse:lib/PublicInbox/Git.pm	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
 
# Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
# License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
#
# Used to read files from a git repository without excessive forking.
# Used in our web interfaces as well as our -nntpd server.
# This is based on code in Git.pm which is GPLv2+, but modified to avoid
# dependence on environment variables for compatibility with mod_perl.
# There are also API changes to simplify our usage and data set.
package PublicInbox::Git;
use strict;
use warnings;
use POSIX qw(dup2);
require IO::Handle;
use PublicInbox::Spawn qw(spawn popen_rd);
use Fcntl qw(:seek);
my $have_async = eval {
	require PublicInbox::EvCleanup;
	require PublicInbox::GitAsync;
};

sub new {
	my ($class, $git_dir) = @_;
	bless { git_dir => $git_dir }, $class
}

sub err_begin ($) {
	my $err = $_[0]->{err};
	unless ($err) {
		open($err, '+>', undef);
		$_[0]->{err} = $err;
	}
	sysseek($err, 0, SEEK_SET) or die "sysseek failed: $!";
	truncate($err, 0) or die "truncate failed: $!";
	my $ret = fileno($err);
	defined $ret or die "fileno failed: $!";
	$ret;
}

sub err ($) {
	my $err = $_[0]->{err} or return '';
	sysseek($err, 0, SEEK_SET) or die "sysseek failed: $!";
	defined(sysread($err, my $buf, -s $err)) or die "sysread failed: $!";
	sysseek($err, 0, SEEK_SET) or die "sysseek failed: $!";
	truncate($err, 0) or die "truncate failed: $!";
	$buf;
}

sub cmd {
	my $self = shift;
	[ 'git', "--git-dir=$self->{git_dir}", @_ ];
}

sub _bidi_pipe {
	my ($self, $batch, $in, $out, $pid) = @_;
	return if $self->{$pid};
	my ($in_r, $in_w, $out_r, $out_w);

	pipe($in_r, $in_w) or fail($self, "pipe failed: $!");
	pipe($out_r, $out_w) or fail($self, "pipe failed: $!");

	my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
	my $p = spawn(cmd($self, qw(cat-file), $batch), undef, $redir);
	defined $p or fail($self, "spawn failed: $!");
	$self->{$pid} = $p;
	$out_w->autoflush(1);
	$self->{$out} = $out_w;
	$self->{$in} = $in_r;
}

# legacy synchronous API
sub cat_file_begin {
	my ($self, $obj) = @_;
	$self->_bidi_pipe(qw(--batch in out pid));
	$self->{out}->print($obj, "\n") or fail($self, "write error: $!");

	my $in = $self->{in};
	local $/ = "\n";
	my $head = $in->getline;
	$head =~ / missing$/ and return undef;
	$head =~ /^([0-9a-f]{40}) (\S+) (\d+)$/ or
		fail($self, "Unexpected result from git cat-file: $head");

	($in, $1, $2, $3);
}

# legacy synchronous API
sub cat_file_finish {
	my ($self, $left) = @_;
	my $max = 8192;
	my $in = $self->{in};
	my $buf;
	while ($left > 0) {
		my $r = read($in, $buf, $left > $max ? $max : $left);
		defined($r) or fail($self, "read failed: $!");
		$r == 0 and fail($self, 'exited unexpectedly');
		$left -= $r;
	}

	my $r = read($in, $buf, 1);
	defined($r) or fail($self, "read failed: $!");
	fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
}

# legacy synchronous API
sub cat_file {
	my ($self, $obj, $ref) = @_;

	my ($in, $hex, $type, $size) = $self->cat_file_begin($obj);
	return unless $in;
	my $ref_type = $ref ? ref($ref) : '';

	my $rv;
	my $left = $size;
	$$ref = $size if ($ref_type eq 'SCALAR');
	my $cb_err;

	if ($ref_type eq 'CODE') {
		$rv = eval { $ref->($in, \$left, $type, $hex) };
		$cb_err = $@;
	} else {
		my $offset = 0;
		my $buf = '';
		while ($left > 0) {
			my $r = read($in, $buf, $left, $offset);
			defined($r) or fail($self, "read failed: $!");
			$r == 0 and fail($self, 'exited unexpectedly');
			$left -= $r;
			$offset += $r;
		}
		$rv = \$buf;
	}
	$self->cat_file_finish($left);
	die $cb_err if $cb_err;

	$rv;
}

sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) }

# legacy synchronous API
sub check {
	my ($self, $obj) = @_;
	$self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
	$self->{out_c}->print($obj, "\n") or fail($self, "write error: $!");
	local $/ = "\n";
	chomp(my $line = $self->{in_c}->getline);
	my ($hex, $type, $size) = split(' ', $line);
	return if $type eq 'missing';
	($hex, $type, $size);
}

sub _destroy {
	my ($self, $in, $out, $pid) = @_;
	my $p = delete $self->{$pid} or return;
	foreach my $f ($in, $out) {
		delete $self->{$f};
	}
	waitpid $p, 0;
}

sub fail {
	my ($self, $msg) = @_;
	cleanup($self);
	die $msg;
}

sub popen {
	my ($self, @cmd) = @_;
	my $cmd = cmd($self);
	my ($env, $opt);
	if (ref $cmd[0]) {
		push @$cmd, @{$cmd[0]};
		$env = $cmd[1];
		$opt = $cmd[2];
	} else {
		push @$cmd, @cmd;
	}
	popen_rd($cmd, $env, $opt);
}

sub qx {
	my ($self, @cmd) = @_;
	my $fh = $self->popen(@cmd);
	defined $fh or return;
	local $/ = "\n";
	return <$fh> if wantarray;
	local $/;
	<$fh>
}

sub cleanup {
	my ($self) = @_;
	_destroy($self, qw(in out pid));
	_destroy($self, qw(in_c out_c pid_c));

	if ($have_async) {
		my %h = %$self; # yup, copy ourselves
		%$self = ();
		my $ds_closed;

		# schedule closing with Danga::Socket::close:
		foreach (qw(async async_c)) {
			my $ds = delete $h{$_} or next;
			$ds->close;
			$ds_closed = 1;
		}

		# can't do waitpid in _destroy() until next tick,
		# since D::S defers closing until end of current event loop
		$ds_closed and PublicInbox::EvCleanup::next_tick(sub {
			_destroy(\%h, qw(in_a out_a pid_a));
			_destroy(\%h, qw(in_ac out_ac pid_ac));
		});
	}
}

sub DESTROY { cleanup(@_) }

# modern async API
sub check_async_ds ($$$) {
	my ($self, $obj, $cb) = @_;
	($self->{async_c} ||= do {
		_bidi_pipe($self, qw(--batch-check in_ac out_ac pid_ac));
		PublicInbox::GitAsync->new($self->{in_ac}, $self->{out_ac}, 1);
	})->cat_file_async($obj, $cb);
}

sub cat_async_ds ($$$) {
	my ($self, $obj, $cb) = @_;
	($self->{async} ||= do {
		_bidi_pipe($self, qw(--batch in_a out_a pid_a));
		PublicInbox::GitAsync->new($self->{in_a}, $self->{out_a});
	})->cat_file_async($obj, $cb);
}

sub async_info_compat ($) {
	local $/ = "\n";
	chomp(my $line = $_[0]->getline);
	[ split(/ /, $line) ];
}

sub check_async_compat ($$$) {
	my ($self, $obj, $cb) = @_;
	$self->_bidi_pipe(qw(--batch-check in_c out_c pid_c));
	$self->{out_c}->print($obj."\n") or fail($self, "write error: $!");
	my $info = async_info_compat($self->{in_c});
	$cb->($info);
}

sub cat_async_compat ($$$) {
	my ($self, $obj, $cb) = @_;
	$self->_bidi_pipe(qw(--batch in out pid));
	$self->{out}->print($obj."\n") or fail($self, "write error: $!");
	my $in = $self->{in};
	my $info = async_info_compat($in);
	my (undef, $type, $left) = @$info;
	$cb->($info);
	return if $info->[1] eq 'missing';
	my $max = 8192;
	my ($buf, $r);
	while ($left > 0) {
		$r = read($in, $buf, $left > $max ? $max : $left);
		return $cb->($r) unless $r; # undef or 0
		$left -= $r;
		$cb->(\$buf);
	}
	$r = read($in, $buf, 1);
	defined($r) or fail($self, "read failed: $!");
	fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n");
	$cb->(0);
}

sub check_async {
	my ($self, $env, $obj, $cb) = @_;
	if ($env->{'pi-httpd.async'}) {
		check_async_ds($self, $obj, $cb);
	} else {
		check_async_compat($self, $obj, $cb);
	}
}

sub cat_async {
	my ($self, $env, $obj, $cb) = @_;
	if ($env->{'pi-httpd.async'}) {
		cat_async_ds($self, $obj, $cb);
	} else {
		cat_async_compat($self, $obj, $cb);
	}
}

1;
__END__
=pod

=head1 NAME

PublicInbox::Git - git wrapper

=head1 VERSION

version 1.0

=head1 SYNOPSIS

	use PublicInbox::Git;
	chomp(my $git_dir = `git rev-parse --git-dir`);
	$git_dir or die "GIT_DIR= must be specified\n";
	my $git = PublicInbox::Git->new($git_dir);

=head1 DESCRIPTION

Unstable API outside of the L</new> method.
It requires L<git(1)> to be installed.

=head1 METHODS

=cut

=head2 new

	my $git = PublicInbox::Git->new($git_dir);

Initialize a new PublicInbox::Git object for use with L<PublicInbox::Import>
This is the only public API method we support.  Everything else
in this module is subject to change.

=head1 SEE ALSO

L<Git>, L<PublicInbox::Import>

=head1 CONTACT

All feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>

The mail archives are hosted at L<https://public-inbox.org/meta/>

=head1 COPYRIGHT

Copyright (C) 2016 all contributors L<mailto:meta@public-inbox.org>

License: AGPL-3.0+ L<http://www.gnu.org/licenses/agpl-3.0.txt>

=cut

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git