use parent qw(Exporter);
use POSIX ();
use IO::Handle; # ->autoflush
-use Errno qw(EINTR EAGAIN);
+use Errno qw(EINTR EAGAIN ENOENT);
use File::Glob qw(bsd_glob GLOB_NOSORT);
use File::Spec ();
use Time::HiRes qw(stat);
-use PublicInbox::Spawn qw(popen_rd);
+use PublicInbox::Spawn qw(popen_rd spawn);
use PublicInbox::Tmpfile;
use IO::Poll qw(POLLIN);
use Carp qw(croak);
our $in_cleanup;
our $RDTIMEO = 60_000; # milliseconds
-use constant MAX_INFLIGHT =>
- (($^O eq 'linux' ? 4096 : POSIX::_POSIX_PIPE_BUF()) * 3)
- /
+use constant MAX_INFLIGHT => (POSIX::PIPE_BUF * 3) /
65; # SHA-256 hex size + "\n" in preparation for git using non-SHA1
my %GIT_ESC = (
sub git_unquote ($) {
return $_[0] unless ($_[0] =~ /\A"(.*)"\z/);
$_[0] = $1;
- $_[0] =~ s/\\([\\"abfnrtv])/$GIT_ESC{$1}/g;
- $_[0] =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
+ $_[0] =~ s!\\([\\"abfnrtv]|[0-3][0-7]{2})!$GIT_ESC{$1}//chr(oct($1))!ge;
$_[0];
}
sub git_quote ($) {
if ($_[0] =~ s/([\\"\a\b\f\n\r\t\013]|[^[:print:]])/
- '\\'.($ESC_GIT{$1}||sprintf("%0o",ord($1)))/egs) {
+ '\\'.($ESC_GIT{$1}||sprintf("%03o",ord($1)))/egs) {
return qq{"$_[0]"};
}
$_[0];
sub new {
my ($class, $git_dir) = @_;
+ $git_dir =~ tr!/!/!s;
+ $git_dir =~ s!/*\z!!s;
# may contain {-tmp} field for File::Temp::Dir
bless { git_dir => $git_dir, alt_st => '', -git_path => {} }, $class
}
}
return;
}
- my ($out_r, $out_w);
- pipe($out_r, $out_w) or $self->fail("pipe failed: $!");
- my @cmd = (qw(git), "--git-dir=$self->{git_dir}",
+ pipe(my ($out_r, $out_w)) or $self->fail("pipe failed: $!");
+ my $rdr = { 0 => $out_r };
+ my $gd = $self->{git_dir};
+ if ($gd =~ s!/([^/]+/[^/]+)\z!/!) {
+ $rdr->{-C} = $gd;
+ $gd = $1;
+ }
+ my @cmd = (qw(git), "--git-dir=$gd",
qw(-c core.abbrev=40 cat-file), $batch);
- my $redir = { 0 => $out_r };
if ($err) {
my $id = "git.$self->{git_dir}$batch.err";
my $fh = tmpfile($id) or $self->fail("tmpfile($id): $!");
$self->{$err} = $fh;
- $redir->{2} = $fh;
+ $rdr->{2} = $fh;
}
- my ($in_r, $p) = popen_rd(\@cmd, undef, $redir);
+ my ($in_r, $p) = popen_rd(\@cmd, undef, $rdr);
$self->{$pid} = $p;
$self->{"$pid.owner"} = $$;
$out_w->autoflush(1);
my ($self, $inflight) = @_;
die 'BUG: inflight empty or odd' if scalar(@$inflight) < 3;
my ($req, $cb, $arg) = splice(@$inflight, 0, 3);
- my $rbuf = delete($self->{cat_rbuf}) // \(my $new = '');
+ my $rbuf = delete($self->{rbuf}) // \(my $new = '');
my ($bref, $oid, $type, $size);
my $head = my_readline($self->{in}, $rbuf);
# ->fail may be called via Gcf2Client.pm
my $err = $! ? " ($!)" : '';
$self->fail("bad result from async cat-file: $head$err");
}
- $self->{cat_rbuf} = $rbuf if $$rbuf ne '';
+ $self->{rbuf} = $rbuf if $$rbuf ne '';
eval { $cb->($bref, $oid, $type, $size, $arg) };
warn "E: $oid: $@\n" if $@;
}
my ($self, $inflight_c) = @_;
die 'BUG: inflight empty or odd' if scalar(@$inflight_c) < 3;
my ($req, $cb, $arg) = splice(@$inflight_c, 0, 3);
- my $rbuf = delete($self->{chk_rbuf}) // \(my $new = '');
+ my $rbuf = delete($self->{rbuf_c}) // \(my $new = '');
chomp(my $line = my_readline($self->{in_c}, $rbuf));
my ($hex, $type, $size) = split(/ /, $line);
my $ret = my_read($self->{in_c}, $rbuf, $type + 1);
$self->fail(defined($ret) ? 'read EOF' : "read: $!") if !$ret;
}
- $self->{chk_rbuf} = $rbuf if $$rbuf ne '';
+ $self->{rbuf_c} = $rbuf if $$rbuf ne '';
eval { $cb->($hex, $type, $size, $arg, $self) };
warn "E: check($req) $@\n" if $@;
}
dwaitpid($p) if $$ == $self->{"$pid.owner"};
}
-sub cat_async_abort ($) {
+sub async_abort ($) {
my ($self) = @_;
- if (my $inflight = $self->{inflight}) {
- while (@$inflight) {
- my ($req, $cb, $arg) = splice(@$inflight, 0, 3);
- $req =~ s/ .*//; # drop git_dir for Gcf2Client
- eval { $cb->(undef, $req, undef, undef, $arg) };
- warn "E: $req: $@ (in abort)\n" if $@;
+ while (scalar(@{$self->{inflight_c} // []}) ||
+ scalar(@{$self->{inflight} // []})) {
+ for my $c ('', '_c') {
+ my $q = $self->{"inflight$c"};
+ while (@$q) {
+ my ($req, $cb, $arg) = splice(@$q, 0, 3);
+ $req =~ s/ .*//; # drop git_dir for Gcf2Client
+ eval { $cb->(undef, $req, undef, undef, $arg) };
+ warn "E: $req: $@ (in abort)\n" if $@;
+ }
+ delete $self->{"inflight$c"};
+ delete $self->{"rbuf$c"};
}
- delete $self->{cat_rbuf};
- delete $self->{inflight};
}
cleanup($self);
}
sub fail { # may be augmented in subclasses
my ($self, $msg) = @_;
- cat_async_abort($self);
+ async_abort($self);
croak(ref($self) . ' ' . ($self->{git_dir} // '') . ": $msg");
}
my ($self) = @_;
while (scalar(@{$self->{inflight_c} // []}) ||
scalar(@{$self->{inflight} // []})) {
- $self->check_async_wait;
- $self->cat_async_wait;
+ check_async_wait($self);
+ cat_async_wait($self);
}
}
# returns true if there are pending "git cat-file" processes
sub cleanup {
- my ($self) = @_;
+ my ($self, $lazy) = @_;
+ return 1 if $lazy && (scalar(@{$self->{inflight_c} // []}) ||
+ scalar(@{$self->{inflight} // []}));
local $in_cleanup = 1;
delete $self->{async_cat};
async_wait_all($self);
delete $self->{inflight};
delete $self->{inflight_c};
- _destroy($self, qw(cat_rbuf in out pid));
- _destroy($self, qw(chk_rbuf in_c out_c pid_c err_c));
- !!($self->{pid} || $self->{pid_c});
+ _destroy($self, qw(rbuf in out pid));
+ _destroy($self, qw(rbuf_c in_c out_c pid_c err_c));
+ undef;
}
-
# assuming a well-maintained repo, this should be a somewhat
# accurate estimation of its size
# TODO: show this in the WWW UI as a hint to potential cloners
my ($self) = @_;
my $ret = '???';
# don't show full FS path, basename should be OK:
- if ($self->{git_dir} =~ m!/([^/]+)(?:/\.git)?\z!) {
+ if ($self->{git_dir} =~ m!/([^/]+)(?:/*\.git/*)?\z!) {
$ret = "$1.git";
}
wantarray ? ($ret) : $ret;
push(@$inflight, $oid, $cb, $arg);
}
-sub extract_cmt_time {
- my ($bref, undef, undef, undef, $modified) = @_;
-
- if ($$bref =~ /^committer .*?> ([0-9]+) [\+\-]?[0-9]+/sm) {
- my $cmt_time = $1 + 0;
- $$modified = $cmt_time if $cmt_time > $$modified;
- }
-}
-
# returns the modified time of a git repo, same as the "modified" field
# of a grokmirror manifest
sub modified ($) {
- my ($self) = @_;
- my $modified = 0;
- my $fh = popen($self, qw(rev-parse --branches));
- local $/ = "\n";
- while (my $oid = <$fh>) {
- chomp $oid;
- cat_async($self, $oid, \&extract_cmt_time, \$modified);
- }
- cat_async_wait($self);
- $modified || time;
+ # committerdate:unix is git 2.9.4+ (2017-05-05), so using raw instead
+ my $fh = popen($_[0], qw[for-each-ref --sort=-committerdate
+ --format=%(committerdate:raw) --count=1]);
+ (split(/ /, <$fh> // time))[0] + 0; # integerize for JSON
}
# for grokmirror, which doesn't read gitweb.description
$ent;
}
+# returns true if there are pending cat-file processes
+sub cleanup_if_unlinked {
+ my ($self) = @_;
+ return cleanup($self, 1) if $^O ne 'linux';
+ # Linux-specific /proc/$PID/maps access
+ # TODO: support this inside git.git
+ my $ret = 0;
+ for my $fld (qw(pid pid_c)) {
+ my $pid = $self->{$fld} // next;
+ open my $fh, '<', "/proc/$pid/maps" or return cleanup($self, 1);
+ while (<$fh>) {
+ # n.b. we do not restart for unlinked multi-pack-index
+ # since it's not too huge, and the startup cost may
+ # be higher.
+ /\.(?:idx|pack) \(deleted\)$/ and
+ return cleanup($self, 1);
+ }
+ ++$ret;
+ }
+ $ret;
+}
+
1;
__END__
=pod