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::WwwStream qw(html_oneshot);
21 use PublicInbox::Linkify;
22 use PublicInbox::Tmpfile;
23 use PublicInbox::ViewDiff qw(flush_diff);
24 use PublicInbox::View;
25 use Text::Wrap qw(wrap);
26 use PublicInbox::Hval qw(ascii_html to_filename);
28 require PublicInbox::HlMod;
29 PublicInbox::HlMod->new;
32 my %QP_MAP = ( A => 'oid_a', a => 'path_a', b => 'path_b' );
33 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
34 my $BIN_DETECT = 8000; # same as git
35 my $SHOW_FMT = '--pretty=format:'.join('%n', '%H', '%T', '%P', '%s',
36 '%an <%ae>%x09%ai', '%cn <%ce>%x09%ci', '%b%x00');
39 my ($ctx, $code, $strref) = @_;
40 my $wcb = delete $ctx->{-wcb};
41 $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
42 my $res = html_oneshot($ctx, $code, $strref);
43 $wcb ? $wcb->($res) : $res;
46 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
47 my ($r, $bref, $ctx) = @_;
48 my ($res, $logref) = delete @$ctx{qw(-res -logref)};
49 my ($git, $oid, $type, $size, $di) = @$res;
50 my @cl = ('Content-Length', $size);
51 if (!defined $r) { # error
52 html_page($ctx, 500, $logref);
53 } elsif (index($$bref, "\0") >= 0) {
54 [200, [qw(Content-Type application/octet-stream), @cl] ];
56 my $n = length($$bref);
57 if ($n >= $BIN_DETECT || $n == $size) {
58 return [200, [ 'Content-Type',
59 'text/plain; charset=UTF-8', @cl ] ];
62 warn "premature EOF on $oid $$logref";
63 return html_page($ctx, 500, $logref);
65 @$ctx{qw(-res -logref)} = ($res, $logref);
66 undef; # bref keeps growing
70 sub stream_large_blob ($$$$) {
71 my ($ctx, $res, $logref, $fn) = @_;
72 $ctx->{-logref} = $logref;
74 my ($git, $oid, $type, $size, $di) = @$res;
75 my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
76 my $qsp = PublicInbox::Qspawn->new($cmd);
77 my $env = $ctx->{env};
78 $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
79 $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
82 sub show_other_result ($$) {
83 my ($bref, $ctx) = @_;
84 my ($qsp_err, $logref) = delete @$ctx{qw(-qsp_err -logref)};
86 $$logref .= "git show error:$qsp_err";
87 return html_page($ctx, 500, $logref);
89 my $l = PublicInbox::Linkify->new;
91 $$bref = '<pre>'. $l->to_html($$bref);
92 $$bref .= '</pre><hr>' . $$logref;
93 html_page($ctx, 200, $bref);
96 sub show_commit_result ($$) {
97 my ($bref, $ctx) = @_;
98 my ($qsp_err, $logref, $tmp) = @$ctx{qw(-qsp_err -logref -tmp)};
100 $$logref .= "git show/patch-id error:$qsp_err";
101 return html_page($ctx, 500, $logref);
103 my $upfx = $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
104 my $patchid = (split(/ /, $$bref))[0]; # ignore commit
105 $ctx->{-q_value_html} = "patchid:$patchid" if defined $patchid;
106 my $l = $ctx->{-linkify} = PublicInbox::Linkify->new;
107 open my $fh, '<:utf8', "$tmp/h" or die "open $tmp/h: $!";
108 chop(my $buf = do { local $/ = "\0"; <$fh> });
109 my ($H, $T, $P, $s, $au, $co, $bdy) = split(/\n/, $buf, 7);
111 # try to keep author and committer dates lined up
112 my $x = length($au) - length($co);
120 $_ = ascii_html($_) for ($au, $co);
121 $_ = $l->to_html($_) for ($s, $bdy);
122 $ctx->{-title_html} = $s;
123 my @p = split(/ /, $P);
125 $P = qq(\n parent <a href="$upfx$P/s/">$P</a>);
127 $P = qq(\n parents <a href="$upfx$p[0]/s/">$p[0]</a>\n);
129 $P .= qq( <a href="$upfx$_/s/">$_</a>\n) for @p;
131 } else { # root commit
132 $P = ' (root commit)';
134 PublicInbox::WwwStream::html_init($ctx);
137 tree <a href="$upfx$T/s/">$T</a>
144 open $fh, '<:utf8', "$tmp/p" or die "open $tmp/p: $!";
145 if (-s $fh > $MAX_SIZE) {
146 $ctx->zmore("---\n patch is too large to show\n");
147 } else { # prepare flush_diff:
149 $ctx->{obuf} = \$buf;
150 $ctx->{-apfx} = $ctx->{-spfx} = $upfx;
152 read($fh, $bdy, -s _);
153 $bdy =~ s/\r?\n/\n/gs;
154 $ctx->{-anchors} = {} if $bdy =~ /^diff --git /sm;
155 flush_diff($ctx, \$bdy);
158 # TODO: should there be another textarea which attempts to
159 # search for the exact email which was applied to make this
161 if (my $qry = delete $ctx->{-qry}) {
163 for (@{$qry->{dfpost}}, @{$qry->{dfpre}}) {
164 # keep blobs as short as reasonable, emails
165 # are going to be older than what's in git
166 substr($_, 7, 64, '');
169 chop $q; # no trailing SP
170 local $Text::Wrap::columns = PublicInbox::View::COLS;
171 local $Text::Wrap::huge = 'overflow';
172 $q = wrap('', '', $q);
173 my $rows = ($q =~ tr/\n/\n/) + 1;
176 <hr><form action=$upfx
177 id=related><pre>find related emails, including ancestors/descendants/conflicts
178 <textarea name=q cols=${\PublicInbox::View::COLS} rows=$rows>$q</textarea>
179 <input type=submit value=search
180 />\t(<a href=${upfx}_/text/help/>help</a>)</pre></form>
184 $x = $ctx->zflush($ctx->_html_end);
185 my $res_hdr = delete $ctx->{-res_hdr};
186 push @$res_hdr, 'Content-Length', length($x);
187 delete($ctx->{env}->{'qspawn.wcb'})->([200, $res_hdr, [$x]]);
190 sub show_commit ($$$$) {
191 my ($ctx, $res, $logref, $fn) = @_;
192 my ($git, $oid) = @$res;
193 # patch-id needs two passes, and we use the initial show to ensure
194 # a patch embedded inside the commit message body doesn't get fed
196 my $cmd = [ '/bin/sh', '-c',
197 "git show --encoding=UTF-8 '$SHOW_FMT'".
198 " -z --no-notes --no-patch $oid >h && ".
199 'git show --encoding=UTF-8 --pretty=format:%n -M'.
200 " --stat -p $oid >p && ".
201 "git patch-id --stable <p" ];
202 my $xenv = { GIT_DIR => $git->{git_dir} };
203 my $tmp = File::Temp->newdir("show-$oid-XXXX", TMPDIR => 1);
204 my $qsp = PublicInbox::Qspawn->new($cmd, $xenv, { -C => "$tmp" });
205 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
206 $ctx->{-logref} = $logref;
208 $ctx->{env}->{'qspawn.wcb'} = delete $ctx->{-wcb};
209 $qsp->psgi_qx($ctx->{env}, undef, \&show_commit_result, $ctx);
212 sub show_other ($$$$) {
213 my ($ctx, $res, $logref, $fn) = @_;
214 my ($git, $oid, $type, $size) = @$res;
215 if ($size > $MAX_SIZE) {
216 $$logref = "$oid is too big to show\n" . $$logref;
217 return html_page($ctx, 200, $logref);
219 my $cmd = ['git', "--git-dir=$git->{git_dir}",
220 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
221 my $qsp = PublicInbox::Qspawn->new($cmd);
222 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
223 $ctx->{-logref} = $logref;
224 $qsp->psgi_qx($ctx->{env}, undef, \&show_other_result, $ctx);
227 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
229 my ($res, $ctx) = @_;
230 my ($log, $hints, $fn) = delete @$ctx{qw(log hints fn)};
232 unless (seek($log, 0, 0)) {
233 warn "seek(log): $!";
234 return html_page($ctx, 500, \'seek error');
236 $log = do { local $/; <$log> };
238 my $l = PublicInbox::Linkify->new;
239 $log = '<pre>debug log:</pre><hr /><pre>' .
240 $l->to_html($log) . '</pre>';
242 $res or return html_page($ctx, 404, \$log);
243 ref($res) eq 'ARRAY' or return html_page($ctx, 500, \$log);
245 my ($git, $oid, $type, $size, $di) = @$res;
246 return show_commit($ctx, $res, \$log, $fn) if $type eq 'commit';
247 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
248 my $path = to_filename($di->{path_b} // $hints->{path_b} // 'blob');
249 my $raw_link = "(<a\nhref=$path>raw</a>)";
250 if ($size > $MAX_SIZE) {
251 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
252 $log = "<pre><b>Too big to show, download available</b>\n" .
253 "$oid $type $size bytes $raw_link</pre>" . $log;
254 return html_page($ctx, 200, \$log);
257 my $blob = $git->cat_file($oid);
259 my $e = "Failed to retrieve generated blob ($oid)";
260 warn "$e ($git->{git_dir})";
261 $log = "<pre><b>$e</b></pre>" . $log;
262 return html_page($ctx, 500, \$log);
265 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
267 my $h = [ 'Content-Length', $size, 'Content-Type' ];
268 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
269 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
273 $log = "<pre>$oid $type $size bytes (binary)" .
274 " $raw_link</pre>" . $log;
275 return html_page($ctx, 200, \$log);
278 # TODO: detect + convert to ensure validity
279 utf8::decode($$blob);
280 my $nl = ($$blob =~ s/\r?\n/\n/sg);
281 my $pad = length($nl);
283 $l->linkify_1($$blob);
284 my $ok = $hl->do_hl($blob, $path) if $hl;
288 $$blob = ascii_html($$blob);
291 # using some of the same CSS class names and ids as cgit
292 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
293 "<hr /><table\nclass=blob>".
294 "<tr><td\nclass=linenumbers><pre>" . join('', map {
295 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
296 } (1..$nl)) . '</pre></td>' .
297 '<td><pre> </pre></td>'. # pad for non-CSS users
298 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
299 $l->linkify_2($$blob) .
300 '</code></pre></td></tr></table>' . $log;
302 html_page($ctx, 200, \$log);
305 # GET /$INBOX/$GIT_OBJECT_ID/s/
306 # GET /$INBOX/$GIT_OBJECT_ID/s/$FILENAME
308 my ($ctx, $oid_b, $fn) = @_;
310 my $hints = $ctx->{hints} = {};
311 while (my ($from, $to) = each %QP_MAP) {
312 defined(my $v = $qp->{$from}) or next;
313 $hints->{$to} = $v if $v ne '';
316 $ctx->{'log'} = tmpfile("solve.$oid_b") // die "tmpfile: $!";
318 my $solver = PublicInbox::SolverGit->new($ctx->{ibx},
319 \&solve_result, $ctx);
320 # PSGI server will call this immediately and give us a callback (-wcb)
322 $ctx->{-wcb} = $_[0]; # HTTP write callback
323 $solver->solve($ctx->{env}, $ctx->{log}, $oid_b, $hints);