]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
view: enable naming hints for raw blob downloads
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
1 # Copyright (C) 2019 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 # FIXME: we only show blobs for now
6 #
7 # This can use a "solver" to reconstruct blobs based on git
8 # patches (with abbreviated OIDs in the header).  However, the
9 # abbreviated OIDs must match exactly what's in the original
10 # email (unless a normal code repo already has the blob).
11 #
12 # In other words, we can only reliably reconstruct blobs based
13 # on links generated by ViewDiff (and only if the emailed
14 # patches apply 100% cleanly to published blobs).
15
16 package PublicInbox::ViewVCS;
17 use strict;
18 use warnings;
19 use Encode qw(find_encoding);
20 use PublicInbox::SolverGit;
21 use PublicInbox::WwwStream;
22 use PublicInbox::Linkify;
23 use PublicInbox::Hval qw(ascii_html to_filename);
24 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
25 my $max_size = 1024 * 1024; # TODO: configurable
26 my $enc_utf8 = find_encoding('UTF-8');
27
28 sub html_page ($$$) {
29         my ($ctx, $code, $strref) = @_;
30         $ctx->{-upfx} = '../'; # from "/$INBOX/$OID/s"
31         PublicInbox::WwwStream->response($ctx, $code, sub {
32                 my ($nr, undef) =  @_;
33                 $nr == 1 ? $$strref : undef;
34         });
35 }
36
37 sub show ($$;$) {
38         my ($ctx, $oid_b, $fn) = @_;
39         my $ibx = $ctx->{-inbox};
40         my $inboxes = [ $ibx ];
41         my $solver = PublicInbox::SolverGit->new($ibx->{-repo_objs}, $inboxes);
42         my $qp = $ctx->{qp};
43         my $hints = {};
44         while (my ($from, $to) = each %QP_MAP) {
45                 defined(my $v = $qp->{$from}) or next;
46                 $hints->{$to} = $v;
47         }
48
49         open my $log, '+>', undef or die "open: $!";
50         my $res = $solver->solve($log, $oid_b, $hints);
51
52         seek($log, 0, 0) or die "seek: $!";
53         $log = do { local $/; <$log> };
54
55         my $l = PublicInbox::Linkify->new;
56         $l->linkify_1($log);
57         $log = '<pre>debug log:</pre><hr /><pre>' .
58                 $l->linkify_2(ascii_html($log)) . '</pre>';
59
60         $res or return html_page($ctx, 404, \$log);
61
62         my ($git, $oid, $type, $size, $di) = @$res;
63         if ($size > $max_size) {
64                 # TODO: stream the raw file if it's gigantic, at least
65                 $log = '<pre><b>Too big to show</b></pre>' . $log;
66                 return html_page($ctx, 500, \$log);
67         }
68
69         my $blob = $git->cat_file($oid);
70         if (!$blob) { # WTF?
71                 my $e = "Failed to retrieve generated blob ($oid)";
72                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
73                 $log = "<pre><b>$e</b></pre>" . $log;
74                 return html_page($ctx, 500, \$log);
75         }
76
77         my $binary = index($$blob, "\0") >= 0;
78         if ($fn) {
79                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
80                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
81                 return [ 200, $h, [ $$blob ]];
82         }
83
84         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
85         my $raw_link = "(<a\nhref=_$path>raw</a>)";
86         if ($binary) {
87                 $log = "<pre>$oid $type $size bytes (binary)" .
88                         " $raw_link</pre>" . $log;
89                 return html_page($ctx, 200, \$log);
90         }
91
92         $$blob = $enc_utf8->decode($$blob);
93         my $nl = ($$blob =~ tr/\n/\n/);
94         my $pad = length($nl);
95
96         # using some of the same CSS class names and ids as cgit
97         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
98                 "<hr /><table\nclass=blob>".
99                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
100                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
101                 } (1..$nl)) . '</pre></td>' .
102                 '<td><pre> </pre></td>'. # pad for non-CSS users
103                 "<td\nclass=lines><pre><code>" .  ascii_html($$blob) .
104                 '</code></pre></td></tr></table>' . $log;
105
106         html_page($ctx, 200, \$log);
107 }
108
109 1;