X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGit.pm;h=8426cc7d6a705488e8dded3bd0691fd412e0a372;hb=2e168e869df3f1ca88f2eb22a8d1a1dda869b6ef;hp=af3a5712cf79dec86c90e4f92eb9a2df87a3683e;hpb=65e3cc8f6cc73e45db827cbeee4ccecbf1502496;p=public-inbox.git diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index af3a5712..8426cc7d 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2019 all contributors +# Copyright (C) 2014-2020 all contributors # License: GPLv2 or later # # Used to read files from a git repository without excessive forking. @@ -9,9 +9,10 @@ package PublicInbox::Git; use strict; use warnings; -use POSIX qw(dup2); -require IO::Handle; -use PublicInbox::Spawn qw(spawn popen_rd); +use POSIX (); +use IO::Handle; # ->autoflush +use File::Glob qw(bsd_glob GLOB_NOSORT); +use PublicInbox::Spawn qw(popen_rd); use PublicInbox::Tmpfile; use base qw(Exporter); our @EXPORT_OK = qw(git_unquote git_quote); @@ -54,10 +55,8 @@ sub git_quote ($) { sub new { my ($class, $git_dir) = @_; - my @st; - $st[7] = $st[10] = 0; # may contain {-tmp} field for File::Temp::Dir - bless { git_dir => $git_dir, st => \@st, -git_path => {} }, $class + bless { git_dir => $git_dir, alt_st => '', -git_path => {} }, $class } sub git_path ($$) { @@ -78,10 +77,11 @@ sub alternates_changed { my ($self) = @_; my $alt = git_path($self, 'objects/info/alternates'); my @st = stat($alt) or return 0; - my $old_st = $self->{st}; - # 10 - ctime, 7 - size - return 0 if ($st[10] == $old_st->[10] && $st[7] == $old_st->[7]); - $self->{st} = \@st; + + # can't rely on 'q' on some 32-bit builds, but `d' works + my $st = pack('dd', $st[10], $st[7]); # 10: ctime, 7: size + return 0 if $self->{alt_st} eq $st; + $self->{alt_st} = $st; # always a true value } sub last_check_err { @@ -103,85 +103,71 @@ sub _bidi_pipe { } return; } - my ($in_r, $in_w, $out_r, $out_w); - - pipe($in_r, $in_w) or fail($self, "pipe failed: $!"); + my ($out_r, $out_w); pipe($out_r, $out_w) or fail($self, "pipe failed: $!"); - if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ - fcntl($out_w, 1031, 4096); - fcntl($in_w, 1031, 4096) if $batch eq '--batch-check'; - } - my @cmd = (qw(git), "--git-dir=$self->{git_dir}", qw(-c core.abbrev=40 cat-file), $batch); - my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) }; + my $redir = { 0 => $out_r }; if ($err) { my $id = "git.$self->{git_dir}$batch.err"; my $fh = tmpfile($id) or fail($self, "tmpfile($id): $!"); $self->{$err} = $fh; - $redir->{2} = fileno($fh); + $redir->{2} = $fh; } - my $p = spawn(\@cmd, undef, $redir); - defined $p or fail($self, "spawn failed: $!"); + my ($in_r, $p) = popen_rd(\@cmd, undef, $redir); $self->{$pid} = $p; $out_w->autoflush(1); + if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ + fcntl($out_w, 1031, 4096); + fcntl($in_r, 1031, 4096) if $batch eq '--batch-check'; + } $self->{$out} = $out_w; $self->{$in} = $in_r; } -sub read_cat_in_full ($$$) { - my ($self, $in, $left) = @_; - 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; - } - my $r = read($in, my $lf, 1); - defined($r) or fail($self, "read failed: $!"); - fail($self, 'newline missing after blob') if ($r != 1 || $lf ne "\n"); +sub read_cat_in_full ($$) { + my ($self, $len) = @_; + ++$len; # for final "\n" added by git + read($self->{in}, my $buf, $len) == $len or fail($self, 'short read'); + chop($buf) eq "\n" or fail($self, 'newline missing after blob'); \$buf; } -sub _cat_async_step ($$$) { - my ($self, $inflight, $in) = @_; +sub _cat_async_step ($$) { + my ($self, $inflight) = @_; my $pair = shift @$inflight or die 'BUG: inflight empty'; my ($cb, $arg) = @$pair; local $/ = "\n"; - my $head = $in->getline; + my $head = readline($self->{in}); $head =~ / missing$/ and return eval { $cb->(undef, undef, undef, undef, $arg) }; $head =~ /^([0-9a-f]{40}) (\S+) ([0-9]+)$/ or fail($self, "Unexpected result from async git cat-file: $head"); my ($oid_hex, $type, $size) = ($1, $2, $3 + 0); - my $bref = read_cat_in_full($self, $in, $size); + my $bref = read_cat_in_full($self, $size); eval { $cb->($bref, $oid_hex, $type, $size, $arg) }; + warn "E: $oid_hex $@\n" if $@; } sub cat_async_wait ($) { my ($self) = @_; my $inflight = delete $self->{inflight} or return; - my $in = $self->{in}; while (scalar(@$inflight)) { - _cat_async_step($self, $inflight, $in); + _cat_async_step($self, $inflight); } } sub cat_file { my ($self, $obj, $ref) = @_; - my ($retried, $in, $head); + my ($retried, $head); cat_async_wait($self); again: batch_prepare($self); - $self->{out}->print($obj, "\n") or fail($self, "write error: $!"); + print { $self->{out} } $obj, "\n" or fail($self, "write error: $!"); - $in = $self->{in}; local $/ = "\n"; - $head = $in->getline; + $head = readline($self->{in}); if ($head =~ / missing$/) { if (!$retried && alternates_changed($self)) { $retried = 1; @@ -195,7 +181,7 @@ again: my $size = $1; $$ref = $size if $ref; - read_cat_in_full($self, $in, $size); + read_cat_in_full($self, $size); } sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) } @@ -203,9 +189,9 @@ sub batch_prepare ($) { _bidi_pipe($_[0], qw(--batch in out pid)) } sub check { my ($self, $obj) = @_; _bidi_pipe($self, qw(--batch-check in_c out_c pid_c err_c)); - $self->{out_c}->print($obj, "\n") or fail($self, "write error: $!"); + print { $self->{out_c} } $obj, "\n" or fail($self, "write error: $!"); local $/ = "\n"; - chomp(my $line = $self->{in_c}->getline); + chomp(my $line = readline($self->{in_c})); my ($hex, $type, $size) = split(' ', $line); # Future versions of git.git may show 'ambiguous', but for now, @@ -256,7 +242,6 @@ sub popen { sub qx { my ($self, @cmd) = @_; my $fh = $self->popen(@cmd); - defined $fh or return; local $/ = "\n"; return <$fh> if wantarray; local $/; @@ -278,7 +263,7 @@ sub packed_bytes { my ($self) = @_; my $n = 0; my $pack_dir = git_path($self, 'objects/pack'); - foreach my $p (glob("$pack_dir/*.pack")) { + foreach my $p (bsd_glob("$pack_dir/*.pack", GLOB_NOSORT)) { $n += -s $p; } $n @@ -300,7 +285,7 @@ sub host_prefix_url ($$) { my ($env, $url) = @_; return $url if index($url, '//') >= 0; my $scheme = $env->{'psgi.url_scheme'}; - my $host_port = $env->{HTTP_HOST} || + my $host_port = $env->{HTTP_HOST} // "$env->{SERVER_NAME}:$env->{SERVER_PORT}"; "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/') . $url; } @@ -325,18 +310,20 @@ sub cat_async ($$$;$) { my ($self, $oid, $cb, $arg) = @_; my $inflight = $self->{inflight} or die 'BUG: not in async'; if (scalar(@$inflight) >= MAX_INFLIGHT) { - _cat_async_step($self, $inflight, $self->{in}); + _cat_async_step($self, $inflight); } - $self->{out}->print($oid, "\n") or fail($self, "write error: $!"); + print { $self->{out} } $oid, "\n" or fail($self, "write error: $!"); push(@$inflight, [ $cb, $arg ]); } -sub commit_title ($$) { - my ($self, $oid) = @_; # PublicInbox::Git, $sha1hex - my $buf = cat_file($self, $oid) or return; - utf8::decode($$buf); - ($$buf =~ /\r?\n\r?\n([^\r\n]+)\r?\n?/)[0] +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 @@ -345,15 +332,13 @@ sub modified ($) { my ($self) = @_; my $modified = 0; my $fh = popen($self, qw(rev-parse --branches)); - defined $fh or return $modified; + cat_async_begin($self); local $/ = "\n"; - foreach my $oid (<$fh>) { + while (my $oid = <$fh>) { chomp $oid; - my $buf = cat_file($self, $oid) or next; - $$buf =~ /^committer .*?> ([0-9]+) [\+\-]?[0-9]+/sm or next; - my $cmt_time = $1 + 0; - $modified = $cmt_time if $cmt_time > $modified; + cat_async($self, $oid, \&extract_cmt_time, \$modified); } + cat_async_wait($self); $modified || time; }