]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewVCS.pm
www_stream: use DESTROY to cleanup temporary gits
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
index d3ac1a7d5d111e40a8c32babcfe81eac312370b9..b0f58455018b0ea03d97ddd42a377cccd927fd82 100644 (file)
@@ -189,7 +189,8 @@ href="$f.patch">patch</a>)\n   <a href=#parent>parent</a> $P->[0]};
                $x = ' (<a href=#root_commit>root commit</a>)';
        }
        PublicInbox::WwwStream::html_init($ctx);
-       $ctx->zmore(<<EOM);
+       my $zfh = $ctx->zfh;
+       print $zfh <<EOM;
 <pre>   <a href=#commit>commit</a> $H$x
      <a href=#tree>tree</a> <a href="$upfx$T/s/">$T</a>
    author $au
@@ -197,15 +198,14 @@ committer $co
 
 <b>$s</b>
 EOM
-       $ctx->zmore("\n", $ctx->{-linkify}->to_html($bdy)) if length($bdy);
+       print $zfh "\n", $ctx->{-linkify}->to_html($bdy) if length($bdy);
        $bdy = '';
        open my $fh, '<:utf8', "$ctx->{-tmp}/p" or
                die "open $ctx->{-tmp}/p: $!";
        if (-s $fh > $MAX_SIZE) {
-               $ctx->zmore("---\n patch is too large to show\n");
+               print $zfh "---\n patch is too large to show\n";
        } else { # prepare flush_diff:
                read($fh, $x, -s _);
-               $ctx->{obuf} = \$bdy;
                $ctx->{-apfx} = $ctx->{-spfx} = $upfx;
                $x =~ s/\r?\n/\n/gs;
                $ctx->{-anchors} = {} if $x =~ /^diff --git /sm;
@@ -227,7 +227,7 @@ EOM
                        $q = wrap('', '', $q);
                        my $rows = ($q =~ tr/\n/\n/) + 1;
                        $q = ascii_html($q);
-                       $ctx->zmore(<<EOM);
+                       print $zfh <<EOM;
 <hr><form action=$upfx
 id=related><pre>find related emails, including ancestors/descendants/conflicts
 <textarea name=q cols=${\PublicInbox::View::COLS} rows=$rows>$q</textarea>
@@ -413,11 +413,22 @@ sub solve_result {
 blob $oid $size bytes $raw_link</pre>
 EOM
        }
+       @{$ctx->{-paths}} = ($path, $raw_link);
+       bless $ctx, 'PublicInbox::WwwStream'; # for DESTROY
+       $ctx->{git} = $git;
+       if ($ctx->{env}->{'pi-httpd.async'}) {
+               ibx_async_cat($ctx, $oid, \&show_blob, $ctx);
+       } else { # synchronous
+               $git->cat_async($oid, \&show_blob, $ctx);
+               $git->cat_async_wait;
+       }
+}
 
-       my $blob = $git->cat_file($oid);
-       if (!$blob) { # WTF?
+sub show_blob { # git->cat_async callback
+       my ($blob, $oid, $type, $size, $ctx) = @_;
+       if (!$blob) {
                my $e = "Failed to retrieve generated blob ($oid)";
-               warn "$e ($git->{git_dir})";
+               warn "$e ($ctx->{git}->{git_dir}) type=$type";
                return html_page($ctx, 500, "<pre><b>$e</b></pre>".dbg_log($ctx))
        }
 
@@ -428,6 +439,7 @@ EOM
                return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
        }
 
+       my ($path, $raw_link) = @{delete $ctx->{-paths}};
        $bin and return html_page($ctx, 200,
                                "<pre>blob $oid $size bytes (binary)" .
                                " $raw_link</pre>".dbg_log($ctx));
@@ -445,14 +457,14 @@ EOM
                $$blob = ascii_html($$blob);
        }
 
+       # using some of the same CSS class names and ids as cgit
        my $x = "<pre>blob $oid $size bytes $raw_link</pre>" .
                "<hr /><table\nclass=blob>".
                "<tr><td\nclass=linenumbers><pre>";
+       # scratchpad in this loop is faster here than `printf $zfh':
        $x .= sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_) for (1..$nl);
        $x .= '</pre></td><td><pre> </pre></td>'. # pad for non-CSS users
                "<td\nclass=lines><pre\nstyle='white-space:pre'><code>";
-
-       # using some of the same CSS class names and ids as cgit
        html_page($ctx, 200, $x, $ctx->{-linkify}->linkify_2($$blob),
                '</code></pre></td></tr></table>'.dbg_log($ctx));
 }