]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Git.pm
git: cap MAX_INFLIGHT value to POSIX minimum
[public-inbox.git] / lib / PublicInbox / Git.pm
index 9140caea9bfb2d9e28177c92795e06f5cc94a2ca..a1af776be4d62b27186ef4e112b6c46ace89e2c1 100644 (file)
@@ -28,8 +28,10 @@ our $in_cleanup;
 our $RDTIMEO = 60_000; # milliseconds
 our $async_warn; # true in read-only daemons
 
-use constant MAX_INFLIGHT => (POSIX::PIPE_BUF * 3) /
-       65; # SHA-256 hex size + "\n" in preparation for git using non-SHA1
+# 512: POSIX PIPE_BUF minimum (see pipe(7))
+# 3: @$inflight is flattened [ $OID, $cb, $arg ]
+# 65: SHA-256 hex size + "\n" in preparation for git using non-SHA1
+use constant MAX_INFLIGHT => 512 * 3 / 65;
 
 my %GIT_ESC = (
        a => "\a",
@@ -426,6 +428,7 @@ sub cleanup {
                                scalar(@{$self->{inflight} // []}));
        local $in_cleanup = 1;
        delete $self->{async_cat};
+       delete $self->{async_chk};
        async_wait_all($self);
        delete $self->{inflight};
        delete $self->{inflight_c};
@@ -451,7 +454,8 @@ sub DESTROY { cleanup(@_) }
 
 sub local_nick ($) {
        # don't show full FS path, basename should be OK:
-       $_[0]->{git_dir} =~ m!/([^/]+?)(?:/*\.git/*)?\z! ? "$1.git" : '???';
+       $_[0]->{nick} // ($_[0]->{git_dir} =~ m!/([^/]+?)(?:/*\.git/*)?\z! ?
+                       "$1.git" : undef);
 }
 
 sub host_prefix_url ($$) {
@@ -463,12 +467,22 @@ sub host_prefix_url ($$) {
        "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/') . $url;
 }
 
+sub base_url { # for coderepos, PSGI-only
+       my ($self, $env) = @_; # env - PSGI env
+       my $url = host_prefix_url($env, '');
+       # for mount in Plack::Builder
+       $url .= '/' if substr($url, -1, 1) ne '/';
+       $url . $self->{nick} . '/';
+}
+
+sub isrch {} # TODO
+
 sub pub_urls {
        my ($self, $env) = @_;
        if (my $urls = $self->{cgit_url}) {
                return map { host_prefix_url($env, $_) } @$urls;
        }
-       (local_nick($self));
+       (local_nick($self) // '???');
 }
 
 sub cat_async_begin {
@@ -498,6 +512,33 @@ sub modified ($) {
        (split(/ /, <$fh> // time))[0] + 0; # integerize for JSON
 }
 
+sub try_cat {
+       my ($path) = @_;
+       open(my $fh, '<', $path) or return '';
+       local $/;
+       <$fh> // '';
+}
+
+sub cat_desc ($) {
+       my $desc = try_cat($_[0]);
+       chomp $desc;
+       utf8::decode($desc);
+       $desc =~ s/\s+/ /smg;
+       $desc eq '' ? undef : $desc;
+}
+
+sub description {
+       cat_desc("$_[0]->{git_dir}/description") // 'Unnamed repository';
+}
+
+sub cloneurl {
+       my ($self, $env) = @_;
+       $self->{cloneurl} // do {
+               my @urls = split(/\s+/s, try_cat("$self->{git_dir}/cloneurl"));
+               scalar(@urls) ? ($self->{cloneurl} = \@urls) : undef;
+       } // [ substr(base_url($self, $env), 0, -1) ];
+}
+
 # for grokmirror, which doesn't read gitweb.description
 # templates/hooks--update.sample and git-multimail in git.git
 # only match "Unnamed repository", not the full contents of
@@ -520,14 +561,8 @@ sub manifest_entry {
        chomp(my $owner = $self->qx('config', 'gitweb.owner'));
        utf8::decode($owner);
        $ent->{owner} = $owner eq '' ? undef : $owner;
-       my $desc = '';
-       if (open($fh, '<', "$git_dir/description")) {
-               local $/ = "\n";
-               chomp($desc = <$fh>);
-               utf8::decode($desc);
-       }
-       $desc = 'Unnamed repository' if $desc eq '';
-       if (defined $epoch && $desc =~ /\AUnnamed repository/) {
+       my $desc = description($self);
+       if (defined $epoch && index($desc, 'Unnamed repository') == 0) {
                $desc = "$default_desc [epoch $epoch]";
        }
        $ent->{description} = $desc;