]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/RepoTree.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / RepoTree.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 # cgit-compatible $REPO/tree/[PATH]?h=$tip redirector
5 package PublicInbox::RepoTree;
6 use v5.12;
7 use PublicInbox::ViewDiff qw(uri_escape_path);
8 use PublicInbox::WwwStatic qw(r);
9 use PublicInbox::Qspawn;
10 use PublicInbox::WwwStream qw(html_oneshot);
11 use PublicInbox::Hval qw(ascii_html);
12
13 sub rd_404_log {
14         my ($bref, $ctx) = @_;
15         my $path = $ctx->{-q_value_html} = ascii_html($ctx->{-path});
16         my $tip = 'HEAD';
17         $tip = ascii_html($ctx->{qp}->{h}) if defined($ctx->{qp}->{h});
18         PublicInbox::WwwStream::html_init($ctx);
19         my $zfh = $ctx->{zfh};
20         print $zfh "<pre>\$ git log -1 $tip -- $path\n";
21         my $code = 200;
22         if ($$bref eq '') {
23                 say $zfh "found no record of `$path' in git history in `$tip'";
24                 $ctx->{-has_srch} and
25                         say $zfh 'perhaps try searching mail (above)';
26                 $code = 404;
27         } else {
28                 my ($H, $h, $s_as) = split(/ /, $$bref, 3);
29                 utf8::decode($s_as);
30                 my $x = uri_escape_path($ctx->{-path});
31                 $s_as = ascii_html($s_as);
32                 print $zfh <<EOM;
33 found last record of `$path' in the following commit:
34
35 <a href="$ctx->{-upfx}$H/s/?b=$x">$h</a> $s_as
36 EOM
37         }
38         my $res = $ctx->html_done;
39         $res->[0] = $code;
40         delete($ctx->{-wcb})->($res);
41 }
42
43 sub find_missing {
44         my ($ctx) = @_;
45         if ($ctx->{-path} eq '') {
46                 my $tip = 'HEAD';
47                 $tip = ascii_html($ctx->{qp}->{h}) if defined($ctx->{qp}->{h});
48                 PublicInbox::WwwStream::html_init($ctx);
49                 print { $ctx->{zfh} } "<pre>`$tip' ref not found</pre>";
50                 my $res = $ctx->html_done;
51                 $res->[0] = 404;
52                 return delete($ctx->{-wcb})->($res);
53         }
54         my $cmd = ['git', "--git-dir=$ctx->{git}->{git_dir}",
55                 qw(log --no-color -1), '--pretty=%H %h %s (%as)' ];
56         push @$cmd, $ctx->{qp}->{h} if defined($ctx->{qp}->{h});
57         push @$cmd, '--';
58         push @$cmd, $ctx->{-path};
59         my $qsp = PublicInbox::Qspawn->new($cmd, undef,
60                                         { quiet => 1, 2 => $ctx->{lh} });
61         $qsp->psgi_qx($ctx->{env}, undef, \&rd_404_log, $ctx);
62 }
63
64 sub tree_show { # git check_async callback
65         my ($oid, $type, $size, $ctx) = @_;
66         return find_missing($ctx) if $type eq 'missing';
67
68         my $res = [ $ctx->{git}, $oid, $type, $size ];
69         my ($bn) = ($ctx->{-path} =~ m!/?([^/]+)\z!);
70         if ($type eq 'blob') {
71                 my $obj = ascii_html($ctx->{-obj});
72                 $ctx->{-q_value_html} = 'dfn:'.ascii_html($ctx->{-path}) .
73                         ' dfpost:'.substr($oid, 0, 7);
74                 $ctx->{-paths} = [ $bn, qq[(<a
75 href="$ctx->{-upfx}$oid/s/$bn">raw</a>)
76 \$ git show $obj\t# shows this blob on the CLI] ];
77         }
78         PublicInbox::ViewVCS::solve_result($res, $ctx);
79 }
80
81 sub srv_tree {
82         my ($ctx, $path) = @_;
83         return if index($path, '//') >= 0 || index($path, '/') == 0;
84         my $tip = $ctx->{qp}->{h} // 'HEAD';
85         $ctx->{-upfx} = '../' x (($path =~ tr!/!/!) + 1);
86         $path =~ s!/\z!!;
87         my $obj = $ctx->{-obj} = "$tip:$path";
88         $ctx->{-path} = $path;
89
90         # "\n" breaks with `git cat-file --batch-check', and there's no
91         # legitimate use of "\n" in filenames anyways.
92         return if index($obj, "\n") >= 0;
93         sub {
94                 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
95                 PublicInbox::ViewVCS::do_check_async($ctx, \&tree_show, $obj);
96         };
97 }
98
99 1;