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"
6 # This can use a "solver" to reconstruct blobs based on git
7 # patches (with abbreviated OIDs in the header). However, the
8 # abbreviated OIDs must match exactly what's in the original
9 # email (unless a normal code repo already has the blob).
11 # In other words, we can only reliably reconstruct blobs based
12 # on links generated by ViewDiff (and only if the emailed
13 # patches apply 100% cleanly to published blobs).
15 package PublicInbox::ViewVCS;
18 use File::Temp 0.19 (); # newdir
19 use PublicInbox::SolverGit;
20 use PublicInbox::GitAsyncCat;
21 use PublicInbox::WwwStream qw(html_oneshot);
22 use PublicInbox::Linkify;
23 use PublicInbox::Tmpfile;
24 use PublicInbox::ViewDiff qw(flush_diff);
25 use PublicInbox::View;
26 use Text::Wrap qw(wrap);
27 use PublicInbox::Hval qw(ascii_html to_filename);
29 require PublicInbox::HlMod;
30 PublicInbox::HlMod->new;
33 my %QP_MAP = ( A => 'oid_a', a => 'path_a', b => 'path_b' );
34 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
35 my $BIN_DETECT = 8000; # same as git
36 my $SHOW_FMT = '--pretty=format:'.join('%n', '%P', '%p', '%H', '%T', '%s',
37 '%an <%ae> %ai', '%cn <%ce> %ci', '%b%x00');
40 my ($ctx, $code, $strref) = @_;
41 my $wcb = delete $ctx->{-wcb};
42 $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
43 my $res = html_oneshot($ctx, $code, $strref);
44 $wcb ? $wcb->($res) : $res;
47 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
48 my ($r, $bref, $ctx) = @_;
49 my ($res, $logref) = delete @$ctx{qw(-res -logref)};
50 my ($git, $oid, $type, $size, $di) = @$res;
51 my @cl = ('Content-Length', $size);
52 if (!defined $r) { # error
53 html_page($ctx, 500, $logref);
54 } elsif (index($$bref, "\0") >= 0) {
55 [200, [qw(Content-Type application/octet-stream), @cl] ];
57 my $n = length($$bref);
58 if ($n >= $BIN_DETECT || $n == $size) {
59 return [200, [ 'Content-Type',
60 'text/plain; charset=UTF-8', @cl ] ];
63 warn "premature EOF on $oid $$logref";
64 return html_page($ctx, 500, $logref);
66 @$ctx{qw(-res -logref)} = ($res, $logref);
67 undef; # bref keeps growing
71 sub stream_large_blob ($$$$) {
72 my ($ctx, $res, $logref, $fn) = @_;
73 $ctx->{-logref} = $logref;
75 my ($git, $oid, $type, $size, $di) = @$res;
76 my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
77 my $qsp = PublicInbox::Qspawn->new($cmd);
78 my $env = $ctx->{env};
79 $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
80 $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
83 sub show_other_result ($$) {
84 my ($bref, $ctx) = @_;
85 my ($qsp_err, $logref) = delete @$ctx{qw(-qsp_err -logref)};
87 $$logref .= "git show error:$qsp_err";
88 return html_page($ctx, 500, $logref);
90 my $l = PublicInbox::Linkify->new;
92 $$bref = '<pre>'. $l->to_html($$bref);
93 $$bref .= '</pre><hr>' . $$logref;
94 html_page($ctx, 200, $bref);
97 sub cmt_title { # git->cat_async callback
98 my ($bref, $oid, $type, $size, $ctx) = @_;
100 my $title = $$bref =~ /\r?\n\r?\n([^\r\n]+)\r?\n?/ ? $1 : '';
101 push(@{$ctx->{-cmt_pt}} , ascii_html($title)) == @{$ctx->{-cmt_P}} and
105 sub show_commit_start { # ->psgi_qx callback
106 my ($bref, $ctx) = @_;
107 my ($qsp_err, $logref) = delete @$ctx{qw(-qsp_err -logref)};
109 $$logref .= "git show/patch-id error:$qsp_err";
110 return html_page($ctx, 500, $logref);
112 my $patchid = (split(/ /, $$bref))[0]; # ignore commit
113 $ctx->{-q_value_html} = "patchid:$patchid" if defined $patchid;
114 open my $fh, '<:utf8', "$ctx->{-tmp}/h" or
115 die "open $ctx->{-tmp}/h: $!";
116 chop(my $buf = do { local $/ = "\0"; <$fh> });
119 ($P, $p, @$ctx{qw(cmt_H cmt_T cmt_s cmt_au cmt_co cmt_b)})
120 = split(/\n/, $buf, 8);
121 return cmt_finalize($ctx) if !$P;
122 @{$ctx->{-cmt_P}} = split(/ /, $P);
123 @{$ctx->{-cmt_p}} = split(/ /, $p); # abbreviated
124 if ($ctx->{env}->{'pi-httpd.async'}) {
125 for (@{$ctx->{-cmt_P}}) {
126 ibx_async_cat($ctx, $_, \&cmt_title, $ctx);
128 } else { # synchronous
129 for (@{$ctx->{-cmt_P}}) {
130 $ctx->{git}->cat_async($_, \&cmt_title, $ctx);
132 $ctx->{git}->cat_async_wait;
138 $ctx->{-linkify} = PublicInbox::Linkify->new;
139 # try to keep author and committer dates lined up
140 my ($au, $co) = delete @$ctx{qw(cmt_au cmt_co)};
141 my $x = length($au) - length($co);
149 $_ = ascii_html($_) for ($au, $co);
150 my $s = $ctx->{-linkify}->to_html(delete $ctx->{cmt_s});
151 $ctx->{-title_html} = $s;
152 my $upfx = $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
153 my ($P, $p, $pt) = delete @$ctx{qw(-cmt_P -cmt_p -cmt_pt)};
154 $_ = qq(<a href="$upfx$_/s/">).shift(@$p).'</a> '.shift(@$pt) for @$P;
156 $x = qq(\n parent $P->[0]);
158 $x = qq(\n parents $P->[0]\n);
160 $x .= qq( $_\n) for @$P;
163 $x = ' (root commit)';
165 PublicInbox::WwwStream::html_init($ctx);
167 <pre> commit $ctx->{cmt_H}$x
168 tree <a href="$upfx$ctx->{cmt_T}/s/">$ctx->{cmt_T}</a>
174 $x = delete $ctx->{cmt_b};
175 $ctx->zmore("\n", $ctx->{-linkify}->to_html($x)) if length($x);
177 open my $fh, '<:utf8', "$ctx->{-tmp}/p" or
178 die "open $ctx->{-tmp}/p: $!";
179 if (-s $fh > $MAX_SIZE) {
180 $ctx->zmore("---\n patch is too large to show\n");
181 } else { # prepare flush_diff:
183 $ctx->{-apfx} = $ctx->{-spfx} = $upfx;
184 read($fh, my $bdy, -s _);
185 $bdy =~ s/\r?\n/\n/gs;
186 $ctx->{-anchors} = {} if $bdy =~ /^diff --git /sm;
187 flush_diff($ctx, \$bdy); # undefs $bdy
190 # TODO: should there be another textarea which attempts to
191 # search for the exact email which was applied to make this
193 if (my $qry = delete $ctx->{-qry}) {
195 for (@{$qry->{dfpost}}, @{$qry->{dfpre}}) {
196 # keep blobs as short as reasonable, emails
197 # are going to be older than what's in git
198 substr($_, 7, 64, '');
201 chop $q; # no trailing SP
202 local $Text::Wrap::columns = PublicInbox::View::COLS;
203 local $Text::Wrap::huge = 'overflow';
204 $q = wrap('', '', $q);
205 my $rows = ($q =~ tr/\n/\n/) + 1;
208 <hr><form action=$upfx
209 id=related><pre>find related emails, including ancestors/descendants/conflicts
210 <textarea name=q cols=${\PublicInbox::View::COLS} rows=$rows>$q</textarea>
211 <input type=submit value=search
212 />\t(<a href=${upfx}_/text/help/>help</a>)</pre></form>
216 $x = $ctx->zflush($ctx->_html_end);
217 my $res_hdr = delete $ctx->{-res_hdr};
218 push @$res_hdr, 'Content-Length', length($x);
219 delete($ctx->{env}->{'qspawn.wcb'})->([200, $res_hdr, [$x]]);
222 sub show_commit ($$$$) {
223 my ($ctx, $res, $logref, $fn) = @_;
224 my ($git, $oid) = @$res;
225 # patch-id needs two passes, and we use the initial show to ensure
226 # a patch embedded inside the commit message body doesn't get fed
228 my $cmd = [ '/bin/sh', '-c',
229 "git show --encoding=UTF-8 '$SHOW_FMT'".
230 " -z --no-notes --no-patch $oid >h && ".
231 'git show --encoding=UTF-8 --pretty=format:%n -M'.
232 " --stat -p $oid >p && ".
233 "git patch-id --stable <p" ];
234 my $e = { GIT_DIR => $git->{git_dir} };
235 my $qsp = PublicInbox::Qspawn->new($cmd, $e, { -C => "$ctx->{-tmp}" });
236 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
237 $ctx->{-logref} = $logref;
238 $ctx->{env}->{'qspawn.wcb'} = delete $ctx->{-wcb};
240 $qsp->psgi_qx($ctx->{env}, undef, \&show_commit_start, $ctx);
243 sub show_other ($$$$) {
244 my ($ctx, $res, $logref, $fn) = @_;
245 my ($git, $oid, $type, $size) = @$res;
246 if ($size > $MAX_SIZE) {
247 $$logref = "$oid is too big to show\n" . $$logref;
248 return html_page($ctx, 200, $logref);
250 my $cmd = ['git', "--git-dir=$git->{git_dir}",
251 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
252 my $qsp = PublicInbox::Qspawn->new($cmd);
253 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
254 $ctx->{-logref} = $logref;
255 $qsp->psgi_qx($ctx->{env}, undef, \&show_other_result, $ctx);
258 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
260 my ($res, $ctx) = @_;
261 my ($log, $hints, $fn) = delete @$ctx{qw(lh hints fn)};
263 unless (seek($log, 0, 0)) {
264 warn "seek(log): $!";
265 return html_page($ctx, 500, \'seek error');
267 $log = do { local $/; <$log> };
269 my $l = PublicInbox::Linkify->new;
270 $log = '<pre>debug log:</pre><hr /><pre>' .
271 $l->to_html($log) . '</pre>';
273 $res or return html_page($ctx, 404, \$log);
274 ref($res) eq 'ARRAY' or return html_page($ctx, 500, \$log);
276 my ($git, $oid, $type, $size, $di) = @$res;
277 return show_commit($ctx, $res, \$log, $fn) if $type eq 'commit';
278 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
279 my $path = to_filename($di->{path_b} // $hints->{path_b} // 'blob');
280 my $raw_link = "(<a\nhref=$path>raw</a>)";
281 if ($size > $MAX_SIZE) {
282 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
283 $log = "<pre><b>Too big to show, download available</b>\n" .
284 "$oid $type $size bytes $raw_link</pre>" . $log;
285 return html_page($ctx, 200, \$log);
288 my $blob = $git->cat_file($oid);
290 my $e = "Failed to retrieve generated blob ($oid)";
291 warn "$e ($git->{git_dir})";
292 $log = "<pre><b>$e</b></pre>" . $log;
293 return html_page($ctx, 500, \$log);
296 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
298 my $h = [ 'Content-Length', $size, 'Content-Type' ];
299 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
300 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
304 $log = "<pre>$oid $type $size bytes (binary)" .
305 " $raw_link</pre>" . $log;
306 return html_page($ctx, 200, \$log);
309 # TODO: detect + convert to ensure validity
310 utf8::decode($$blob);
311 my $nl = ($$blob =~ s/\r?\n/\n/sg);
312 my $pad = length($nl);
314 $l->linkify_1($$blob);
315 my $ok = $hl->do_hl($blob, $path) if $hl;
319 $$blob = ascii_html($$blob);
322 # using some of the same CSS class names and ids as cgit
323 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
324 "<hr /><table\nclass=blob>".
325 "<tr><td\nclass=linenumbers><pre>" . join('', map {
326 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
327 } (1..$nl)) . '</pre></td>' .
328 '<td><pre> </pre></td>'. # pad for non-CSS users
329 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
330 $l->linkify_2($$blob) .
331 '</code></pre></td></tr></table>' . $log;
333 html_page($ctx, 200, \$log);
336 # GET /$INBOX/$GIT_OBJECT_ID/s/
337 # GET /$INBOX/$GIT_OBJECT_ID/s/$FILENAME
339 my ($ctx, $oid_b, $fn) = @_;
341 my $hints = $ctx->{hints} = {};
342 while (my ($from, $to) = each %QP_MAP) {
343 defined(my $v = $qp->{$from}) or next;
344 $hints->{$to} = $v if $v ne '';
347 $ctx->{-tmp} = File::Temp->newdir("solver.$oid_b-XXXX", TMPDIR => 1);
348 open $ctx->{lh}, '+>>', "$ctx->{-tmp}/solve.log" or die "open: $!";
349 my $solver = PublicInbox::SolverGit->new($ctx->{ibx},
350 \&solve_result, $ctx);
351 $solver->{tmp} = $ctx->{-tmp}; # share tmpdir
352 # PSGI server will call this immediately and give us a callback (-wcb)
354 $ctx->{-wcb} = $_[0]; # HTTP write callback
355 $solver->solve($ctx->{env}, $ctx->{lh}, $oid_b, $hints);