]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewVCS.pm
rename reference to git epochs as "partitions"
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
index a8aa0b61e2934b96b3fd4eff8fdba151017a074d..60a62e577ab731050dfbcabcdae6c17cda093bb0 100644 (file)
@@ -16,7 +16,7 @@
 package PublicInbox::ViewVCS;
 use strict;
 use warnings;
-use Encode qw(find_encoding);
+use bytes (); # only for bytes::length
 use PublicInbox::SolverGit;
 use PublicInbox::WwwStream;
 use PublicInbox::Linkify;
@@ -28,7 +28,7 @@ my $hl = eval {
 
 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
 my $max_size = 1024 * 1024; # TODO: configurable
-my $enc_utf8 = find_encoding('UTF-8');
+my $BIN_DETECT = 8000; # same as git
 
 sub html_page ($$$) {
        my ($ctx, $code, $strref) = @_;
@@ -38,7 +38,65 @@ sub html_page ($$$) {
                my ($nr, undef) =  @_;
                $nr == 1 ? $$strref : undef;
        });
-       $wcb->($res);
+       $wcb ? $wcb->($res) : $res;
+}
+
+sub stream_large_blob ($$$$) {
+       my ($ctx, $res, $logref, $fn) = @_;
+       my ($git, $oid, $type, $size, $di) = @$res;
+       my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
+       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) = @_;
+               if (!defined $r) { # error
+                       html_page($ctx, 500, $logref);
+               } elsif (index($$bref, "\0") >= 0) {
+                       my $ct = 'application/octet-stream';
+                       [200, ['Content-Type', $ct, @cl ] ];
+               } else {
+                       my $n = bytes::length($$bref);
+                       if ($n >= $BIN_DETECT || $n == $size) {
+                               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 {
@@ -60,9 +118,13 @@ 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) {
-               # TODO: stream the raw file if it's gigantic, at least
-               $log = '<pre><b>Too big to show</b></pre>' . $log;
+               return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
+               $log = "<pre><b>Too big to show, download available</b>\n" .
+                       "$oid $type $size bytes $raw_link</pre>" . $log;
                return html_page($ctx, 500, \$log);
        }
 
@@ -74,22 +136,21 @@ sub solve_result {
                return html_page($ctx, 500, \$log);
        }
 
-       my $binary = index($$blob, "\0") >= 0;
-       if ($fn) {
+       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 ]]);
        }
 
-       my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
-       my $raw_link = "(<a\nhref=$path>raw</a>)";
-       if ($binary) {
+       if ($bin) {
                $log = "<pre>$oid $type $size bytes (binary)" .
                        " $raw_link</pre>" . $log;
                return html_page($ctx, 200, \$log);
        }
 
-       $$blob = $enc_utf8->decode($$blob);
+       # TODO: detect + convert to ensure validity
+       utf8::decode($$blob);
        my $nl = ($$blob =~ tr/\n/\n/);
        my $pad = length($nl);