1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # show any VCS object, similar to "git show"
5 # FIXME: we only show blobs for now
7 # This can use a "solver" to reconstruct blobs based on git
8 # patches (with abbreviated OIDs in the header). However, the
9 # abbreviated OIDs must match exactly what's in the original
10 # email (unless a normal code repo already has the blob).
12 # In other words, we can only reliably reconstruct blobs based
13 # on links generated by ViewDiff (and only if the emailed
14 # patches apply 100% cleanly to published blobs).
16 package PublicInbox::ViewVCS;
19 use PublicInbox::SolverGit;
20 use PublicInbox::WwwStream qw(html_oneshot);
21 use PublicInbox::Linkify;
22 use PublicInbox::Tmpfile;
23 use PublicInbox::Hval qw(ascii_html to_filename);
25 require PublicInbox::HlMod;
26 PublicInbox::HlMod->new;
29 my %QP_MAP = ( A => 'oid_a', a => 'path_a', b => 'path_b' );
30 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
31 my $BIN_DETECT = 8000; # same as git
34 my ($ctx, $code, $strref) = @_;
35 my $wcb = delete $ctx->{-wcb};
36 $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
37 my $res = html_oneshot($ctx, $code, $strref);
38 $wcb ? $wcb->($res) : $res;
41 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
42 my ($r, $bref, $ctx) = @_;
43 my ($res, $logref) = delete @$ctx{qw(-res -logref)};
44 my ($git, $oid, $type, $size, $di) = @$res;
45 my @cl = ('Content-Length', $size);
46 if (!defined $r) { # error
47 html_page($ctx, 500, $logref);
48 } elsif (index($$bref, "\0") >= 0) {
49 [200, [qw(Content-Type application/octet-stream), @cl] ];
51 my $n = length($$bref);
52 if ($n >= $BIN_DETECT || $n == $size) {
53 return [200, [ 'Content-Type',
54 'text/plain; charset=UTF-8', @cl ] ];
57 warn "premature EOF on $oid $$logref";
58 return html_page($ctx, 500, $logref);
60 @$ctx{qw(-res -logref)} = ($res, $logref);
61 undef; # bref keeps growing
65 sub stream_large_blob ($$$$) {
66 my ($ctx, $res, $logref, $fn) = @_;
67 $ctx->{-logref} = $logref;
69 my ($git, $oid, $type, $size, $di) = @$res;
70 my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
71 my $qsp = PublicInbox::Qspawn->new($cmd);
72 my $env = $ctx->{env};
73 $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
74 $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
77 sub show_other_result ($$) {
78 my ($bref, $ctx) = @_;
79 my ($qsp_err, $logref) = delete @$ctx{qw(-qsp_err -logref)};
81 $$logref .= "git show error:$qsp_err";
82 return html_page($ctx, 500, $logref);
84 my $l = PublicInbox::Linkify->new;
86 $$bref = '<pre>'. $l->to_html($$bref);
87 $$bref .= '</pre><hr>' . $$logref;
88 html_page($ctx, 200, $bref);
91 sub show_other ($$$$) {
92 my ($ctx, $res, $logref, $fn) = @_;
93 my ($git, $oid, $type, $size) = @$res;
94 if ($size > $MAX_SIZE) {
95 $$logref = "$oid is too big to show\n" . $$logref;
96 return html_page($ctx, 200, $logref);
98 my $cmd = ['git', "--git-dir=$git->{git_dir}",
99 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
100 my $qsp = PublicInbox::Qspawn->new($cmd);
101 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
102 $ctx->{-logref} = $logref;
103 $qsp->psgi_qx($ctx->{env}, undef, \&show_other_result, $ctx);
106 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
108 my ($res, $ctx) = @_;
109 my ($log, $hints, $fn) = delete @$ctx{qw(log hints fn)};
111 unless (seek($log, 0, 0)) {
112 warn "seek(log): $!";
113 return html_page($ctx, 500, \'seek error');
115 $log = do { local $/; <$log> };
118 my $l = PublicInbox::Linkify->new;
119 $log = '<pre>debug log:</pre><hr /><pre>' .
120 $l->to_html($log) . '</pre>';
122 $res or return html_page($ctx, 404, \$log);
123 $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
125 my ($git, $oid, $type, $size, $di) = @$res;
126 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
127 my $path = to_filename($di->{path_b} // $hints->{path_b} // 'blob');
128 my $raw_link = "(<a\nhref=$path>raw</a>)";
129 if ($size > $MAX_SIZE) {
130 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
131 $log = "<pre><b>Too big to show, download available</b>\n" .
132 "$oid $type $size bytes $raw_link</pre>" . $log;
133 return html_page($ctx, 200, \$log);
136 my $blob = $git->cat_file($oid);
138 my $e = "Failed to retrieve generated blob ($oid)";
139 warn "$e ($git->{git_dir})";
140 $log = "<pre><b>$e</b></pre>" . $log;
141 return html_page($ctx, 500, \$log);
144 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
146 my $h = [ 'Content-Length', $size, 'Content-Type' ];
147 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
148 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
152 $log = "<pre>$oid $type $size bytes (binary)" .
153 " $raw_link</pre>" . $log;
154 return html_page($ctx, 200, \$log);
157 # TODO: detect + convert to ensure validity
158 utf8::decode($$blob);
159 my $nl = ($$blob =~ s/\r?\n/\n/sg);
160 my $pad = length($nl);
162 $l->linkify_1($$blob);
163 my $ok = $hl->do_hl($blob, $path) if $hl;
167 $$blob = ascii_html($$blob);
170 # using some of the same CSS class names and ids as cgit
171 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
172 "<hr /><table\nclass=blob>".
173 "<tr><td\nclass=linenumbers><pre>" . join('', map {
174 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
175 } (1..$nl)) . '</pre></td>' .
176 '<td><pre> </pre></td>'. # pad for non-CSS users
177 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
178 $l->linkify_2($$blob) .
179 '</code></pre></td></tr></table>' . $log;
181 html_page($ctx, 200, \$log);
184 # GET /$INBOX/$GIT_OBJECT_ID/s/
185 # GET /$INBOX/$GIT_OBJECT_ID/s/$FILENAME
187 my ($ctx, $oid_b, $fn) = @_;
189 my $hints = $ctx->{hints} = {};
190 while (my ($from, $to) = each %QP_MAP) {
191 defined(my $v = $qp->{$from}) or next;
192 $hints->{$to} = $v if $v ne '';
195 $ctx->{'log'} = tmpfile("solve.$oid_b") // die "tmpfile: $!";
197 my $solver = PublicInbox::SolverGit->new($ctx->{ibx},
198 \&solve_result, $ctx);
199 # PSGI server will call this immediately and give us a callback (-wcb)
201 $ctx->{-wcb} = $_[0]; # HTTP write callback
202 $solver->solve($ctx->{env}, $ctx->{log}, $oid_b, $hints);