1 # Copyright (C) 2019 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 package PublicInbox::ViewVCS;
8 use Encode qw(find_encoding);
9 use PublicInbox::SolverGit;
10 use PublicInbox::WwwStream;
11 use PublicInbox::Linkify;
12 use PublicInbox::Hval qw(ascii_html);
13 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
14 my $max_size = 1024 * 1024; # TODO: configurable
15 my $enc_utf8 = find_encoding('UTF-8');
18 my ($ctx, $code, $strref) = @_;
19 $ctx->{-upfx} = '../'; # from "/$INBOX/$OID/s"
20 PublicInbox::WwwStream->response($ctx, $code, sub {
22 $nr == 1 ? $$strref : undef;
27 my ($ctx, $oid_b, $fn) = @_;
28 my $ibx = $ctx->{-inbox};
29 my $inboxes = [ $ibx ];
30 my $solver = PublicInbox::SolverGit->new($ibx->{-repo_objs}, $inboxes);
33 while (my ($from, $to) = each %QP_MAP) {
34 defined(my $v = $qp->{$from}) or next;
38 open my $log, '+>', undef or die "open: $!";
39 my $res = $solver->solve($log, $oid_b, $hints);
41 seek($log, 0, 0) or die "seek: $!";
42 $log = do { local $/; <$log> };
44 my $l = PublicInbox::Linkify->new;
46 $log = '<pre>debug log:</pre><hr /><pre>' .
47 $l->linkify_2(ascii_html($log)) . '</pre>';
49 $res or return html_page($ctx, 404, \$log);
51 my ($git, $oid, $type, $size, $di) = @$res;
52 if ($size > $max_size) {
53 # TODO: stream the raw file if it's gigantic, at least
54 $log = '<pre><b>Too big to show</b></pre>' . $log;
55 return html_page($ctx, 500, \$log);
58 my $blob = $git->cat_file($oid);
60 my $e = "Failed to retrieve generated blob ($oid)";
61 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
62 $log = "<pre><b>$e</b></pre>" . $log;
63 return html_page($ctx, 500, \$log);
66 if (index($$blob, "\0") >= 0) {
67 $log = "<pre>$oid $type $size bytes (binary)</pre>" . $log;
68 return html_page($ctx, 200, \$log);
71 $$blob = $enc_utf8->decode($$blob);
72 my $nl = ($$blob =~ tr/\n/\n/);
73 my $pad = length($nl);
75 # using some of the same CSS class names and ids as cgit
76 $log = "<pre>$oid $type $size bytes</pre><hr /><table\nclass=blob>".
77 "<tr><td\nclass=linenumbers><pre>" . join('', map {
78 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
79 } (1..$nl)) . '</pre></td>' .
80 '<td><pre> </pre></td>'. # pad for non-CSS users
81 "<td\nclass=lines><pre><code>" . ascii_html($$blob) .
82 '</pre></td></tr></table>' . $log;
84 html_page($ctx, 200, \$log);