]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
5de37ee6afba06b28c94499db2a69d8354e1d166
[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         my $wcb = delete $ctx->{-wcb};
31         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
32         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
33                 my ($nr, undef) =  @_;
34                 $nr == 1 ? $$strref : undef;
35         });
36         $wcb->($res);
37 }
38
39 sub solve_result {
40         my ($ctx, $res, $log, $hints, $fn) = @_;
41
42         unless (seek($log, 0, 0)) {
43                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
44                 return html_page($ctx, 500, \'seek error');
45         }
46         $log = do { local $/; <$log> };
47
48         my $ref = ref($res);
49         my $l = PublicInbox::Linkify->new;
50         $l->linkify_1($log);
51         $log = '<pre>debug log:</pre><hr /><pre>' .
52                 $l->linkify_2(ascii_html($log)) . '</pre>';
53
54         $res or return html_page($ctx, 404, \$log);
55         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
56
57         my ($git, $oid, $type, $size, $di) = @$res;
58         if ($size > $max_size) {
59                 # TODO: stream the raw file if it's gigantic, at least
60                 $log = '<pre><b>Too big to show</b></pre>' . $log;
61                 return html_page($ctx, 500, \$log);
62         }
63
64         my $blob = $git->cat_file($oid);
65         if (!$blob) { # WTF?
66                 my $e = "Failed to retrieve generated blob ($oid)";
67                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
68                 $log = "<pre><b>$e</b></pre>" . $log;
69                 return html_page($ctx, 500, \$log);
70         }
71
72         my $binary = index($$blob, "\0") >= 0;
73         if ($fn) {
74                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
75                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
76                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
77         }
78
79         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
80         my $raw_link = "(<a\nhref=$path>raw</a>)";
81         if ($binary) {
82                 $log = "<pre>$oid $type $size bytes (binary)" .
83                         " $raw_link</pre>" . $log;
84                 return html_page($ctx, 200, \$log);
85         }
86
87         $$blob = $enc_utf8->decode($$blob);
88         my $nl = ($$blob =~ tr/\n/\n/);
89         my $pad = length($nl);
90
91         # using some of the same CSS class names and ids as cgit
92         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
93                 "<hr /><table\nclass=blob>".
94                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
95                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
96                 } (1..$nl)) . '</pre></td>' .
97                 '<td><pre> </pre></td>'. # pad for non-CSS users
98                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
99                 ascii_html($$blob) .
100                 '</code></pre></td></tr></table>' . $log;
101
102         html_page($ctx, 200, \$log);
103 }
104
105 sub show ($$;$) {
106         my ($ctx, $oid_b, $fn) = @_;
107         my $qp = $ctx->{qp};
108         my $hints = {};
109         while (my ($from, $to) = each %QP_MAP) {
110                 defined(my $v = $qp->{$from}) or next;
111                 $hints->{$to} = $v;
112         }
113
114         open my $log, '+>', undef or die "open: $!";
115         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
116                 solve_result($ctx, $_[0], $log, $hints, $fn);
117         });
118
119         # PSGI server will call this and give us a callback
120         sub {
121                 $ctx->{-wcb} = $_[0]; # HTTP write callback
122                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
123         };
124 }
125
126 1;