]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
config: use inbox names to map inboxes <-> coderepos
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # show any VCS object, similar to "git show"
5 #
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).
10 #
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).
14
15 package PublicInbox::ViewVCS;
16 use strict;
17 use v5.10.1;
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 uri_escape_path);
25 use PublicInbox::View;
26 use PublicInbox::Eml;
27 use Text::Wrap qw(wrap);
28 use PublicInbox::Hval qw(ascii_html to_filename prurl);
29 use POSIX qw(strftime);
30 my $hl = eval {
31         require PublicInbox::HlMod;
32         PublicInbox::HlMod->new;
33 };
34
35 my %QP_MAP = ( A => 'oid_a', a => 'path_a', b => 'path_b' );
36 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
37 my $BIN_DETECT = 8000; # same as git
38 my $SHOW_FMT = '--pretty=format:'.join('%n', '%P', '%p', '%H', '%T', '%s', '%f',
39         '%an <%ae>  %ai', '%cn <%ce>  %ci', '%b%x00');
40
41 my %GIT_MODE = (
42         '100644' => ' ', # blob
43         '100755' => 'x', # executable blob
44         '040000' => 'd', # tree
45         '120000' => 'l', # symlink
46         '160000' => 'g', # commit (gitlink)
47 );
48
49 sub html_page ($$;@) {
50         my ($ctx, $code) = @_[0, 1];
51         my $wcb = delete $ctx->{-wcb};
52         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
53         my $res = html_oneshot($ctx, $code, @_[2..$#_]);
54         $wcb ? $wcb->($res) : $res;
55 }
56
57 sub dbg_log ($) {
58         my ($ctx) = @_;
59         my $log = delete $ctx->{lh} // die 'BUG: already captured debug log';
60         if (!seek($log, 0, 0)) {
61                 warn "seek(log): $!";
62                 return '<pre>debug log seek error</pre>';
63         }
64         $log = do { local $/; <$log> } // do {
65                 warn "readline(log): $!";
66                 return '<pre>debug log read error</pre>';
67         };
68         $ctx->{-linkify} //= PublicInbox::Linkify->new;
69         "<hr><pre>debug log:\n\n".
70                 $ctx->{-linkify}->to_html($log).'</pre>';
71 }
72
73 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
74         my ($r, $bref, $ctx) = @_;
75         my ($git, $oid, $type, $size, $di) = @{$ctx->{-res}};
76         my @cl = ('Content-Length', $size);
77         if (!defined $r) { # sysread error
78                 html_page($ctx, 500, dbg_log($ctx));
79         } elsif (index($$bref, "\0") >= 0) {
80                 [200, [qw(Content-Type application/octet-stream), @cl] ];
81         } else {
82                 my $n = length($$bref);
83                 if ($n >= $BIN_DETECT || $n == $size) {
84                         return [200, [ 'Content-Type',
85                                 'text/plain; charset=UTF-8', @cl ] ];
86                 }
87                 if ($r == 0) {
88                         my $log = dbg_log($ctx);
89                         warn "premature EOF on $oid $log";
90                         return html_page($ctx, 500, $log);
91                 }
92                 undef; # bref keeps growing
93         }
94 }
95
96 sub stream_large_blob ($$) {
97         my ($ctx, $res) = @_;
98         $ctx->{-res} = $res;
99         my ($git, $oid, $type, $size, $di) = @$res;
100         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
101         my $qsp = PublicInbox::Qspawn->new($cmd);
102         my $env = $ctx->{env};
103         $env->{'qspawn.wcb'} = $ctx->{-wcb};
104         $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
105 }
106
107 sub show_other_result ($$) { # future-proofing
108         my ($bref, $ctx) = @_;
109         if (my $qsp_err = delete $ctx->{-qsp_err}) {
110                 return html_page($ctx, 500, dbg_log($ctx) .
111                                 "git show error:$qsp_err");
112         }
113         my $l = PublicInbox::Linkify->new;
114         utf8::decode($$bref);
115         html_page($ctx, 200, '<pre>', $l->to_html($$bref), '</pre><hr>',
116                 dbg_log($ctx));
117 }
118
119 sub cmt_title { # git->cat_async callback
120         my ($bref, $oid, $type, $size, $ctx) = @_;
121         utf8::decode($$bref);
122         my $title = $$bref =~ /\r?\n\r?\n([^\r\n]+)\r?\n?/ ? $1 : '';
123         push(@{$ctx->{-cmt_pt}} , ascii_html($title)) == @{$ctx->{-cmt_P}} and
124                 cmt_finalize($ctx);
125 }
126
127 sub show_commit_start { # ->psgi_qx callback
128         my ($bref, $ctx) = @_;
129         if (my $qsp_err = delete $ctx->{-qsp_err}) {
130                 return html_page($ctx, 500, dbg_log($ctx) .
131                                 "git show/patch-id error:$qsp_err");
132         }
133         my $patchid = (split(/ /, $$bref))[0]; # ignore commit
134         $ctx->{-q_value_html} = "patchid:$patchid" if defined $patchid;
135         open my $fh, '<:utf8', "$ctx->{-tmp}/h" or
136                 die "open $ctx->{-tmp}/h: $!";
137         chop(my $buf = do { local $/ = "\0"; <$fh> });
138         chomp $buf;
139         my ($P, $p);
140         ($P, $p, @{$ctx->{cmt_info}}) = split(/\n/, $buf, 9);
141         return cmt_finalize($ctx) if !$P;
142         @{$ctx->{-cmt_P}} = split(/ /, $P);
143         @{$ctx->{-cmt_p}} = split(/ /, $p); # abbreviated
144         if ($ctx->{env}->{'pi-httpd.async'}) {
145                 for (@{$ctx->{-cmt_P}}) {
146                         ibx_async_cat($ctx, $_, \&cmt_title, $ctx);
147                 }
148         } else { # synchronous
149                 for (@{$ctx->{-cmt_P}}) {
150                         $ctx->{git}->cat_async($_, \&cmt_title, $ctx);
151                 }
152                 $ctx->{git}->cat_async_wait;
153         }
154 }
155
156 sub ibx_url_for {
157         my ($ctx) = @_;
158         $ctx->{ibx} and return; # fall back to $upfx
159         $ctx->{git} or return;
160         if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) {
161                 return $ALL->base_url // $ALL->base_url($ctx->{env});
162         } elsif (my $ibx_names = $ctx->{git}->{ibx_names}) {
163                 my $by_name = $ctx->{www}->{pi_cfg}->{-by_name};
164                 for my $name (@$ibx_names) {
165                         my $ibx = $by_name->{$name} // do {
166                                 warn "inbox `$name' no longer exists\n";
167                                 next;
168                         };
169                         $ibx->isrch // next;
170                         return defined($ibx->{url}) ?
171                                 prurl($ctx->{env}, $ibx->{url}) :
172                                 "../../../$name/";
173                 }
174         }
175         undef;
176 }
177
178 sub cmt_finalize {
179         my ($ctx) = @_;
180         $ctx->{-linkify} //= PublicInbox::Linkify->new;
181         my $upfx = $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
182         my ($H, $T, $s, $f, $au, $co, $bdy) = @{delete $ctx->{cmt_info}};
183         # try to keep author and committer dates lined up
184         my $x = length($au) - length($co);
185         if ($x > 0) {
186                 $x = ' ' x $x;
187                 $co =~ s/>/>$x/;
188         } elsif ($x < 0) {
189                 $x = ' ' x (-$x);
190                 $au =~ s/>/>$x/;
191         }
192         $_ = ascii_html($_) for ($au, $co);
193         $au =~ s!(&gt; +)([0-9]{4,}-\S+ \S+)!
194                 my ($gt, $t) = ($1, $2);
195                 $t =~ tr/ :-//d;
196                 qq($gt<a
197 href="$upfx?t=$t"
198 title="list contemporary emails">$2</a>)
199                 !e;
200         $ctx->{-title_html} = $s = $ctx->{-linkify}->to_html($s);
201         my ($P, $p, $pt) = delete @$ctx{qw(-cmt_P -cmt_p -cmt_pt)};
202         $_ = qq(<a href="$upfx$_/s/">).shift(@$p).'</a> '.shift(@$pt) for @$P;
203         if (@$P == 1) {
204                 $x = qq{ (<a
205 href="$f.patch">patch</a>)\n   <a href=#parent>parent</a> $P->[0]};
206         } elsif (@$P > 1) {
207                 $x = qq(\n  <a href=#parents>parents</a> $P->[0]\n);
208                 shift @$P;
209                 $x .= qq(          $_\n) for @$P;
210                 chop $x;
211         } else {
212                 $x = ' (<a href=#root_commit>root commit</a>)';
213         }
214         PublicInbox::WwwStream::html_init($ctx);
215         my $zfh = $ctx->zfh;
216         print $zfh <<EOM;
217 <pre>   <a href=#commit>commit</a> $H$x
218      <a href=#tree>tree</a> <a href="$upfx$T/s/?b=">$T</a>
219    author $au
220 committer $co
221
222 <b>$s</b>
223 EOM
224         print $zfh "\n", $ctx->{-linkify}->to_html($bdy) if length($bdy);
225         $bdy = '';
226         open my $fh, '<:utf8', "$ctx->{-tmp}/p" or
227                 die "open $ctx->{-tmp}/p: $!";
228         if (-s $fh > $MAX_SIZE) {
229                 print $zfh "---\n patch is too large to show\n";
230         } else { # prepare flush_diff:
231                 read($fh, $x, -s _);
232                 $ctx->{-apfx} = $ctx->{-spfx} = $upfx;
233                 $x =~ s/\r?\n/\n/gs;
234                 $ctx->{-anchors} = {} if $x =~ /^diff --git /sm;
235                 flush_diff($ctx, \$x); # undefs $x
236                 # TODO: should there be another textarea which attempts to
237                 # search for the exact email which was applied to make this
238                 # commit?
239                 if (my $qry = delete $ctx->{-qry}) {
240                         my $q = '';
241                         for (@{$qry->{dfpost}}, @{$qry->{dfpre}}) {
242                                 # keep blobs as short as reasonable, emails
243                                 # are going to be older than what's in git
244                                 substr($_, 7, 64, '');
245                                 $q .= "dfblob:$_ ";
246                         }
247                         chop $q; # no trailing SP
248                         local $Text::Wrap::columns = PublicInbox::View::COLS;
249                         local $Text::Wrap::huge = 'overflow';
250                         $q = wrap('', '', $q);
251                         my $rows = ($q =~ tr/\n/\n/) + 1;
252                         $q = ascii_html($q);
253                         my $ibx_url = ibx_url_for($ctx);
254                         my $alt;
255                         if (defined $ibx_url) {
256                                 $ibx_url = ascii_html($ibx_url);
257                                 $alt = ' '.$ibx_url;
258                         } else {
259                                 $ibx_url = $upfx;
260                                 $alt = '';
261                         }
262                         print $zfh <<EOM;
263 <hr><form action="$ibx_url"
264 id=related><pre>find related emails, including ancestors/descendants/conflicts
265 <textarea name=q cols=${\PublicInbox::View::COLS} rows=$rows>$q</textarea>
266 <input type=submit value="search$alt"
267 />\t(<a href="${ibx_url}_/text/help/">help</a>)</pre></form>
268 EOM
269                 }
270         }
271         chop($x = <<EOM);
272 <hr><pre>glossary
273 --------
274 <dfn
275 id=commit>Commit</dfn> objects reference one tree, and zero or more parents.
276
277 Single <dfn
278 id=parent>parent</dfn> commits can typically generate a patch in
279 unified diff format via `git format-patch'.
280
281 Multiple <dfn id=parents>parents</dfn> means the commit is a merge.
282
283 <dfn id=root_commit>Root commits</dfn> have no ancestor.  Note that it is
284 possible to have multiple root commits when merging independent histories.
285
286 Every commit references one top-level <dfn id=tree>tree</dfn> object.</pre>
287 EOM
288         delete($ctx->{-wcb})->($ctx->html_done($x));
289 }
290
291 sub stream_patch_parse_hdr { # {parse_hdr} for Qspawn
292         my ($r, $bref, $ctx) = @_;
293         if (!defined $r) { # sysread error
294                 html_page($ctx, 500, dbg_log($ctx));
295         } elsif (index($$bref, "\n\n") >= 0) {
296                 my $eml = bless { hdr => $bref }, 'PublicInbox::Eml';
297                 my $fn = to_filename($eml->header('Subject') // '');
298                 $fn = substr($fn // 'PATCH-no-subject', 6); # drop "PATCH-"
299                 return [ 200, [ 'Content-Type', 'text/plain; charset=UTF-8',
300                                 'Content-Disposition',
301                                 qq(inline; filename=$fn.patch) ] ];
302         } elsif ($r == 0) {
303                 my $log = dbg_log($ctx);
304                 warn "premature EOF on $ctx->{patch_oid} $log";
305                 return html_page($ctx, 500, $log);
306         } else {
307                 undef; # bref keeps growing until "\n\n"
308         }
309 }
310
311 sub show_patch ($$) {
312         my ($ctx, $res) = @_;
313         my ($git, $oid) = @$res;
314         my @cmd = ('git', "--git-dir=$git->{git_dir}",
315                 qw(format-patch -1 --stdout -C),
316                 "--signature=git format-patch -1 --stdout -C $oid", $oid);
317         my $qsp = PublicInbox::Qspawn->new(\@cmd);
318         $ctx->{env}->{'qspawn.wcb'} = $ctx->{-wcb};
319         $ctx->{patch_oid} = $oid;
320         $qsp->psgi_return($ctx->{env}, undef, \&stream_patch_parse_hdr, $ctx);
321 }
322
323 sub show_commit ($$) {
324         my ($ctx, $res) = @_;
325         return show_patch($ctx, $res) if ($ctx->{fn} // '') =~ /\.patch\z/;
326         my ($git, $oid) = @$res;
327         # patch-id needs two passes, and we use the initial show to ensure
328         # a patch embedded inside the commit message body doesn't get fed
329         # to patch-id:
330         my $cmd = [ '/bin/sh', '-c',
331                 "git show --encoding=UTF-8 '$SHOW_FMT'".
332                 " -z --no-notes --no-patch $oid >h && ".
333                 'git show --encoding=UTF-8 --pretty=format:%n -M'.
334                 " --stat -p $oid >p && ".
335                 "git patch-id --stable <p" ];
336         my $e = { GIT_DIR => $git->{git_dir} };
337         my $qsp = PublicInbox::Qspawn->new($cmd, $e, { -C => "$ctx->{-tmp}" });
338         $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
339         $ctx->{env}->{'qspawn.wcb'} = $ctx->{-wcb};
340         $ctx->{git} = $git;
341         $qsp->psgi_qx($ctx->{env}, undef, \&show_commit_start, $ctx);
342 }
343
344 sub show_other ($$) { # just in case...
345         my ($ctx, $res) = @_;
346         my ($git, $oid, $type, $size) = @$res;
347         $size > $MAX_SIZE and return html_page($ctx, 200,
348                 ascii_html($type)." $oid is too big to show\n". dbg_log($ctx));
349         my $cmd = ['git', "--git-dir=$git->{git_dir}",
350                 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
351         my $qsp = PublicInbox::Qspawn->new($cmd);
352         $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
353         $qsp->psgi_qx($ctx->{env}, undef, \&show_other_result, $ctx);
354 }
355
356 sub show_tree_result ($$) {
357         my ($bref, $ctx) = @_;
358         if (my $qsp_err = delete $ctx->{-qsp_err}) {
359                 return html_page($ctx, 500, dbg_log($ctx) .
360                                 "git ls-tree -z error:$qsp_err");
361         }
362         my @ent = split(/\0/, $$bref);
363         my $qp = delete $ctx->{qp};
364         my $l = $ctx->{-linkify} //= PublicInbox::Linkify->new;
365         my $pfx = $qp->{b};
366         $$bref = "<pre><a href=#tree>tree</a> $ctx->{tree_oid}";
367         if (defined $pfx) {
368                 if ($pfx eq '') {
369                         $$bref .= "  (root)\n";
370                 } else {
371                         my $x = ascii_html($pfx);
372                         $pfx .= '/';
373                         $$bref .= qq(  <a href=#path>path</a>: $x</a>\n);
374                 }
375         } else {
376                 $pfx = '';
377                 $$bref .= qq[  (<a href=#path>path</a> unknown)\n];
378         }
379         my ($x, $m, $t, $oid, $sz, $f, $n);
380         $$bref .= "\n   size    name";
381         for (@ent) {
382                 ($x, $f) = split(/\t/, $_, 2);
383                 undef $_;
384                 ($m, $t, $oid, $sz) = split(/ +/, $x, 4);
385                 $m = $GIT_MODE{$m} // '?';
386                 utf8::decode($f);
387                 $n = ascii_html($f);
388                 if ($m eq 'g') { # gitlink submodule commit
389                         $$bref .= "\ng\t\t$n @ <a\nhref=#g>commit</a>$oid";
390                         next;
391                 }
392                 my $q = 'b='.ascii_html(uri_escape_path($pfx.$f));
393                 if ($m eq 'd') { $n .= '/' }
394                 elsif ($m eq 'x') { $n = "<b>$n</b>" }
395                 elsif ($m eq 'l') { $n = "<i>$n</i>" }
396                 $$bref .= qq(\n$m\t$sz\t<a\nhref="../../$oid/s/?$q">$n</a>);
397         }
398         $$bref .= dbg_log($ctx);
399         $$bref .= <<EOM;
400 <pre>glossary
401 --------
402 <dfn
403 id=tree>Tree</dfn> objects belong to commits or other tree objects.  Trees may
404 reference blobs, sub-trees, or commits of submodules.
405
406 <dfn
407 id=path>Path</dfn> names are stored in tree objects, but trees do not know
408 their own path name.  A tree's path name comes from their parent tree,
409 or it is the root tree referenced by a commit object.  Thus, this web UI
410 relies on the `b=' URI parameter as a hint to display the path name.
411
412 <dfn title="submodule commit"
413 id=g>Commit</dfn> objects may be stored in trees to reference submodules.</pre>
414 EOM
415         chop $$bref;
416         html_page($ctx, 200, $$bref);
417 }
418
419 sub show_tree ($$) {
420         my ($ctx, $res) = @_;
421         my ($git, $oid, undef, $size) = @$res;
422         $size > $MAX_SIZE and return html_page($ctx, 200,
423                         "tree $oid is too big to show\n". dbg_log($ctx));
424         my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
425                 qw(ls-tree -z -l --no-abbrev), $oid ];
426         my $qsp = PublicInbox::Qspawn->new($cmd);
427         $ctx->{tree_oid} = $oid;
428         $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
429         $qsp->psgi_qx($ctx->{env}, undef, \&show_tree_result, $ctx);
430 }
431
432 # returns seconds offset from git TZ offset
433 sub tz_adj ($) {
434         my ($tz) = @_; # e.g "-0700"
435         $tz = int($tz);
436         my $mm = $tz < 0 ? -$tz : $tz;
437         $mm = int($mm / 100) * 60 + ($mm % 100);
438         $mm = $tz < 0 ? -$mm : $mm;
439         ($mm * 60);
440 }
441
442 sub show_tag_result { # git->cat_async callback
443         my ($bref, $oid, $type, $size, $ctx) = @_;
444         utf8::decode($$bref);
445         my $l = PublicInbox::Linkify->new;
446         $$bref = $l->to_html($$bref);
447         $$bref =~ s!^object ([a-f0-9]+)!object <a
448 href=../../$1/s/>$1</a>!;
449
450         $$bref =~ s/^(tagger .*&gt; )([0-9]+) ([\-+]?[0-9]+)/$1.strftime(
451                 '%Y-%m-%d %H:%M:%S', gmtime($2 + tz_adj($3)))." $3"/sme;
452         # TODO: download link
453         html_page($ctx, 200, '<pre>', $$bref, '</pre>', dbg_log($ctx));
454 }
455
456 sub show_tag ($$) {
457         my ($ctx, $res) = @_;
458         my ($git, $oid) = @$res;
459         $ctx->{git} = $git;
460         if ($ctx->{env}->{'pi-httpd.async'}) {
461                 ibx_async_cat($ctx, $oid, \&show_tag_result, $ctx);
462         } else { # synchronous (generic PSGI)
463                 $git->cat_async($oid, \&show_tag_result, $ctx);
464                 $git->cat_async_wait;
465         }
466 }
467
468 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
469 sub solve_result {
470         my ($res, $ctx) = @_;
471         my $hints = delete $ctx->{hints};
472         $res or return html_page($ctx, 404, dbg_log($ctx));
473         ref($res) eq 'ARRAY' or return html_page($ctx, 500, dbg_log($ctx));
474
475         my ($git, $oid, $type, $size, $di) = @$res;
476         return show_commit($ctx, $res) if $type eq 'commit';
477         return show_tree($ctx, $res) if $type eq 'tree';
478         return show_tag($ctx, $res) if $type eq 'tag';
479         return show_other($ctx, $res) if $type ne 'blob';
480         my $path = to_filename($di->{path_b} // $hints->{path_b} // 'blob');
481         my $raw_link = "(<a\nhref=$path>raw</a>)";
482         if ($size > $MAX_SIZE) {
483                 return stream_large_blob($ctx, $res) if defined $ctx->{fn};
484                 return html_page($ctx, 200, <<EOM . dbg_log($ctx));
485 <pre><b>Too big to show, download available</b>
486 blob $oid $size bytes $raw_link</pre>
487 EOM
488         }
489         @{$ctx->{-paths}} = ($path, $raw_link);
490         bless $ctx, 'PublicInbox::WwwStream'; # for DESTROY
491         $ctx->{git} = $git;
492         if ($ctx->{env}->{'pi-httpd.async'}) {
493                 ibx_async_cat($ctx, $oid, \&show_blob, $ctx);
494         } else { # synchronous
495                 $git->cat_async($oid, \&show_blob, $ctx);
496                 $git->cat_async_wait;
497         }
498 }
499
500 sub show_blob { # git->cat_async callback
501         my ($blob, $oid, $type, $size, $ctx) = @_;
502         if (!$blob) {
503                 my $e = "Failed to retrieve generated blob ($oid)";
504                 warn "$e ($ctx->{git}->{git_dir}) type=$type";
505                 return html_page($ctx, 500, "<pre><b>$e</b></pre>".dbg_log($ctx))
506         }
507
508         my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
509         if (defined $ctx->{fn}) {
510                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
511                 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
512                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
513         }
514
515         my ($path, $raw_link) = @{delete $ctx->{-paths}};
516         $bin and return html_page($ctx, 200,
517                                 "<pre>blob $oid $size bytes (binary)" .
518                                 " $raw_link</pre>".dbg_log($ctx));
519
520         # TODO: detect + convert to ensure validity
521         utf8::decode($$blob);
522         my $nl = ($$blob =~ s/\r?\n/\n/sg);
523         my $pad = length($nl);
524
525         ($ctx->{-linkify} //= PublicInbox::Linkify->new)->linkify_1($$blob);
526         my $ok = $hl->do_hl($blob, $path) if $hl;
527         if ($ok) {
528                 $blob = $ok;
529         } else {
530                 $$blob = ascii_html($$blob);
531         }
532
533         # using some of the same CSS class names and ids as cgit
534         my $x = "<pre>blob $oid $size bytes $raw_link</pre>" .
535                 "<hr /><table\nclass=blob>".
536                 "<tr><td\nclass=linenumbers><pre>";
537         # scratchpad in this loop is faster here than `printf $zfh':
538         $x .= sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_) for (1..$nl);
539         $x .= '</pre></td><td><pre> </pre></td>'. # pad for non-CSS users
540                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>";
541         html_page($ctx, 200, $x, $ctx->{-linkify}->linkify_2($$blob),
542                 '</code></pre></td></tr></table>'.dbg_log($ctx));
543 }
544
545 # GET /$INBOX/$GIT_OBJECT_ID/s/
546 # GET /$INBOX/$GIT_OBJECT_ID/s/$FILENAME
547 sub show ($$;$) {
548         my ($ctx, $oid_b, $fn) = @_;
549         my $qp = $ctx->{qp};
550         my $hints = $ctx->{hints} = {};
551         while (my ($from, $to) = each %QP_MAP) {
552                 defined(my $v = $qp->{$from}) or next;
553                 $hints->{$to} = $v if $v ne '';
554         }
555         $ctx->{fn} = $fn;
556         $ctx->{-tmp} = File::Temp->newdir("solver.$oid_b-XXXX", TMPDIR => 1);
557         open $ctx->{lh}, '+>>', "$ctx->{-tmp}/solve.log" or die "open: $!";
558         my $solver = PublicInbox::SolverGit->new($ctx->{ibx},
559                                                 \&solve_result, $ctx);
560         $solver->{gits} //= [ $ctx->{git} ];
561         $solver->{tmp} = $ctx->{-tmp}; # share tmpdir
562         # PSGI server will call this immediately and give us a callback (-wcb)
563         sub {
564                 $ctx->{-wcb} = $_[0]; # HTTP write callback
565                 $solver->solve($ctx->{env}, $ctx->{lh}, $oid_b, $hints);
566         };
567 }
568
569 1;