]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewVCS.pm
config: support "inboxdir" in addition to "mainrepo"
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
index f6a769427dfae230a1bd9a3261063768852af999..369afe93bd2a528a9155a2f7ce0a2d43097dd5fb 100644 (file)
 package PublicInbox::ViewVCS;
 use strict;
 use warnings;
+use bytes (); # only for bytes::length
 use PublicInbox::SolverGit;
 use PublicInbox::WwwStream;
 use PublicInbox::Linkify;
+use PublicInbox::Tmpfile;
 use PublicInbox::Hval qw(ascii_html to_filename);
 my $hl = eval {
        require PublicInbox::HlMod;
@@ -47,6 +49,7 @@ sub stream_large_blob ($$$$) {
        my $qsp = PublicInbox::Qspawn->new($cmd);
        my @cl = ('Content-Length', $size);
        my $env = $ctx->{env};
+       $env->{'public-inbox.tmpgit'} = $git; # for {-tmp}/File::Temp::Dir
        $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
        $qsp->psgi_return($env, undef, sub {
                my ($r, $bref) = @_;
@@ -61,11 +64,42 @@ sub stream_large_blob ($$$$) {
                                my $ct = 'text/plain; charset=UTF-8';
                                return [200, ['Content-Type', $ct, @cl] ];
                        }
+                       if ($r == 0) {
+                               warn "premature EOF on $oid $$logref\n";
+                               return html_page($ctx, 500, $logref);
+                       }
                        undef; # bref keeps growing
                }
        });
 }
 
+sub show_other ($$$$) {
+       my ($ctx, $res, $logref, $fn) = @_;
+       my ($git, $oid, $type, $size) = @$res;
+       if ($size > $max_size) {
+               $$logref = "$oid is too big to show\n" . $$logref;
+               return html_page($ctx, 200, $logref);
+       }
+       my $cmd = ['git', "--git-dir=$git->{git_dir}",
+               qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
+       my $qsp = PublicInbox::Qspawn->new($cmd);
+       my $env = $ctx->{env};
+       $qsp->psgi_qx($env, undef, sub {
+               my ($bref) = @_;
+               if (my $err = $qsp->{err}) {
+                       utf8::decode($$err);
+                       $$logref .= "git show error: $err";
+                       return html_page($ctx, 500, $logref);
+               }
+               my $l = PublicInbox::Linkify->new;
+               utf8::decode($$bref);
+               $l->linkify_1($$bref);
+               $$bref = '<pre>'. $l->linkify_2(ascii_html($$bref));
+               $$bref .= '</pre><hr>' . $$logref;
+               html_page($ctx, 200, $bref);
+       });
+}
+
 sub solve_result {
        my ($ctx, $res, $log, $hints, $fn) = @_;
 
@@ -85,6 +119,7 @@ sub solve_result {
        $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
 
        my ($git, $oid, $type, $size, $di) = @$res;
+       return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
        my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
        my $raw_link = "(<a\nhref=$path>raw</a>)";
        if ($size > $max_size) {
@@ -102,14 +137,14 @@ sub solve_result {
                return html_page($ctx, 500, \$log);
        }
 
-       my $binary = index($$blob, "\0") >= 0;
+       my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
        if (defined $fn) {
                my $h = [ 'Content-Length', $size, 'Content-Type' ];
-               push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
+               push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
                return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
        }
 
-       if ($binary) {
+       if ($bin) {
                $log = "<pre>$oid $type $size bytes (binary)" .
                        " $raw_link</pre>" . $log;
                return html_page($ctx, 200, \$log);
@@ -151,7 +186,7 @@ sub show ($$;$) {
                $hints->{$to} = $v;
        }
 
-       open my $log, '+>', undef or die "open: $!";
+       my $log = tmpfile("solve.$oid_b");
        my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
                solve_result($ctx, $_[0], $log, $hints, $fn);
        });