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