X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=blobdiff_plain;f=lib%2FPublicInbox%2FGit.pm;h=f5c7a95c34d7aedad7860a1189f8e121167eb1fb;hp=a756684a70355b6435b26b753f1dc6593e5481fa;hb=3c30532aed6256a984c535530c6667552c2e6a84;hpb=2d4403cf9972f8ae78aa52fe6ce7a01d9b6757c1 diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index a756684a..f5c7a95c 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -59,6 +59,11 @@ sub git_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; }; } @@ -136,45 +141,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; } @@ -206,19 +194,10 @@ sub check { } sub _destroy { - my ($self, $in, $out, $pid, $expire) = @_; - my $rfh = $self->{$in} or return; - if (defined $expire) { - # at least FreeBSD 11.2 and Linux 4.20 update mtime of the - # read end of a pipe when the pipe is written to; dunno - # about other OSes. - my $mtime = (stat($rfh))[9]; - return if $mtime > $expire; - } + my ($self, $in, $out, $pid, $err) = @_; my $p = delete $self->{$pid} or return; - foreach my $f ($in, $out) { - delete $self->{$f}; - } + delete @$self{($in, $out)}; + delete $self->{$err} if $err; # `err_c' waitpid $p, 0; } @@ -246,9 +225,9 @@ sub qx { # returns true if there are pending "git cat-file" processes sub cleanup { - my ($self, $expire) = @_; - _destroy($self, qw(in out pid), $expire); - _destroy($self, qw(in_c out_c pid_c), $expire); + my ($self) = @_; + _destroy($self, qw(in out pid)); + _destroy($self, qw(in_c out_c pid_c err_c)); !!($self->{pid} || $self->{pid_c}); } @@ -288,14 +267,48 @@ sub src_blob_url { 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