X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGit.pm;h=218846f3d1d25d42699a4a4be55351394c6a5341;hb=720885ccd944444eb6410ad90117fc1dc7b2a1e1;hp=a0b934a312d508cba5175eb439eaa1841358f936;hpb=4cefd2b05e5262f31505c7226ce761cbd4b1f308;p=public-inbox.git diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index a0b934a3..218846f3 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2018 all contributors +# Copyright (C) 2014-2019 all contributors # License: GPLv2 or later # # Used to read files from a git repository without excessive forking. @@ -12,6 +12,7 @@ use warnings; use POSIX qw(dup2); require IO::Handle; use PublicInbox::Spawn qw(spawn popen_rd); +use PublicInbox::Tmpfile; use base qw(Exporter); our @EXPORT_OK = qw(git_unquote git_quote); @@ -50,13 +51,27 @@ sub new { my ($class, $git_dir) = @_; my @st; $st[7] = $st[10] = 0; - # may contain {-wt} field (working-tree (File::Temp::Dir)) - bless { git_dir => $git_dir, st => \@st }, $class + # may contain {-tmp} field for File::Temp::Dir + bless { git_dir => $git_dir, st => \@st, -git_path => {} }, $class +} + +sub git_path ($$) { + my ($self, $path) = @_; + $self->{-git_path}->{$path} ||= do { + local $/ = "\n"; + chomp(my $str = $self->qx(qw(rev-parse --git-path), $path)); + + # git prior to 2.5.0 did not understand --git-path + if ($str eq "--git-path\n$path") { + $str = "$self->{git_dir}/$path"; + } + $str; + }; } sub alternates_changed { my ($self) = @_; - my $alt = "$self->{git_dir}/objects/info/alternates"; + my $alt = git_path($self, 'objects/info/alternates'); my @st = stat($alt) or return 0; my $old_st = $self->{st}; # 10 - ctime, 7 - size @@ -96,7 +111,8 @@ sub _bidi_pipe { qw(-c core.abbrev=40 cat-file), $batch); my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) }; if ($err) { - open(my $fh, '+>', undef) or fail($self, "open.err failed: $!"); + my $id = "git.$self->{git_dir}$batch.err"; + my $fh = tmpfile($id) or fail($self, "tmpfile($id): $!"); $self->{$err} = $fh; $redir->{2} = fileno($fh); } @@ -127,45 +143,28 @@ again: } return; } - $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or + $head =~ /^[0-9a-f]{40} \S+ ([0-9]+)$/ or fail($self, "Unexpected result from git cat-file: $head"); my $size = $1; - 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) }; - $cb_err = $@; - # drain the rest - my $max = 8192; - while ($left > 0) { - my $r = read($in, my $x, $left > $max ? $max : $left); - defined($r) or fail($self, "read failed: $!"); - $r == 0 and fail($self, 'exited unexpectedly'); - $left -= $r; - } - } 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; + $$ref = $size if $ref; + + 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; - my $r = read($in, my $buf, 1); + my $r = read($in, my $lf, 1); defined($r) or fail($self, "read failed: $!"); - fail($self, 'newline missing after blob') if ($r != 1 || $buf ne "\n"); - die $cb_err if $cb_err; + fail($self, 'newline missing after blob') if ($r != 1 || $lf ne "\n"); $rv; } @@ -197,12 +196,14 @@ sub check { } sub _destroy { - my ($self, $in, $out, $pid) = @_; + my ($self, $in, $out, $pid, $err) = @_; my $p = delete $self->{$pid} or return; - foreach my $f ($in, $out) { - delete $self->{$f}; - } - waitpid $p, 0; + delete @$self{($in, $out)}; + delete $self->{$err} if $err; # `err_c' + + # PublicInbox::DS may not be loaded + eval { PublicInbox::DS::dwaitpid($p, undef, undef) }; + waitpid($p, 0) if $@; # wait synchronously if not in event loop } sub fail { @@ -227,10 +228,12 @@ sub qx { <$fh> } +# returns true if there are pending "git cat-file" processes sub cleanup { my ($self) = @_; _destroy($self, qw(in out pid)); - _destroy($self, qw(in_c out_c pid_c)); + _destroy($self, qw(in_c out_c pid_c err_c)); + !!($self->{pid} || $self->{pid_c}); } # assuming a well-maintained repo, this should be a somewhat @@ -239,7 +242,8 @@ sub cleanup { sub packed_bytes { my ($self) = @_; my $n = 0; - foreach my $p (glob("$self->{git_dir}/objects/pack/*.pack")) { + my $pack_dir = git_path($self, 'objects/pack'); + foreach my $p (glob("$pack_dir/*.pack")) { $n += -s $p; } $n @@ -257,25 +261,48 @@ sub local_nick ($) { wantarray ? ($ret) : $ret; } -# show the blob URL for cgit/gitweb/whatever -sub src_blob_url { - my ($self, $oid) = @_; - # blob_url_format = "https://example.com/foo.git/blob/%s" - if (my $bfu = $self->{blob_url_format}) { - return map { sprintf($_, $oid) } @$bfu if wantarray; - return sprintf($bfu->[0], $oid); - } - local_nick($self); +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} || + "$env->{SERVER_NAME}:$env->{SERVER_PORT}"; + "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/') . $url; } sub pub_urls { - my ($self) = @_; + my ($self, $env) = @_; if (my $urls = $self->{cgit_url}) { - return @$urls; + return map { host_prefix_url($env, $_) } @$urls; } local_nick($self); } +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] +} + +# 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)); + defined $fh or return $modified; + local $/ = "\n"; + foreach 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; + } + $modified || time; +} + 1; __END__ =pod