]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/RepoTree.pm
www_coderepo: /tree/ 404s search git history
[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::GitAsyncCat;
9 use PublicInbox::WwwStatic qw(r);
10 use PublicInbox::Qspawn;
11 use PublicInbox::WwwStream qw(html_oneshot);
12 use PublicInbox::Hval qw(ascii_html);
13
14 sub rd_404_log {
15         my ($bref, $ctx) = @_;
16         my $path = $ctx->{-q_value_html} = ascii_html($ctx->{-path});
17         my $tip = 'HEAD';
18         $tip = ascii_html($ctx->{qp}->{h}) if defined($ctx->{qp}->{h});
19         PublicInbox::WwwStream::html_init($ctx);
20         my $zfh = $ctx->{zfh};
21         print $zfh "<pre>\$ git log -1 $tip -- $path\n";
22         if ($$bref eq '') {
23                 say $zfh "found no record of `$path' in git history";
24                 $ctx->{-has_srch} and
25                         say $zfh 'perhaps try searching mail (above)';
26         } else {
27                 my ($H, $h, $s_as) = split(/ /, $$bref, 3);
28                 utf8::decode($s_as);
29                 my $x = uri_escape_path($ctx->{-path});
30                 $s_as = ascii_html($s_as);
31                 print $zfh <<EOM;
32 found last record of `$path' in the following commit:
33
34 <a href="$ctx->{-upfx}$H/s/?b=$x">$h</a> $s_as
35 EOM
36         }
37         delete($ctx->{-wcb})->($ctx->html_done);
38 }
39
40 sub find_missing {
41         my ($ctx) = @_;
42         my $cmd = ['git', "--git-dir=$ctx->{git}->{git_dir}",
43                 qw(log --no-color -1), '--pretty=%H %h %s (%as)' ];
44         push @$cmd, $ctx->{qp}->{h} if defined($ctx->{qp}->{h});
45         push @$cmd, '--';
46         push @$cmd, $ctx->{-path} if $ctx->{-path} ne '';
47         my $qsp = PublicInbox::Qspawn->new($cmd);
48         $qsp->psgi_qx($ctx->{env}, undef, \&rd_404_log, $ctx);
49 }
50
51 sub tree_30x { # git check_async callback
52         my ($oid, $type, $size, $ctx) = @_;
53         return find_missing($ctx) if $type eq 'missing';
54         my $wcb = delete $ctx->{-wcb};
55         my $u = $ctx->{git}->base_url($ctx->{env});
56         my $path = uri_escape_path(delete $ctx->{-path});
57         $u .= "$oid/s/?b=$path";
58         $wcb->([ 302, [ Location => $u, 'Content-Type' => 'text/plain' ],
59                 [ "Redirecting to $u\n" ] ])
60 }
61
62 sub srv_tree {
63         my ($ctx, $path) = @_;
64         return if index($path, '//') >= 0 || index($path, '/') == 0;
65         my $tip = $ctx->{qp}->{h} // 'HEAD';
66         $ctx->{-upfx} = '../' x (($path =~ tr!/!/!) + 1);
67         $path =~ s!/\z!!;
68         my $obj = $ctx->{-obj} = "$tip:$path";
69         $ctx->{-path} = $path;
70
71         # "\n" breaks with `git cat-file --batch-check', and there's no
72         # legitimate use of "\n" in filenames anyways.
73         return if index($obj, "\n") >= 0;
74         sub {
75                 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
76                 if ($ctx->{env}->{'pi-httpd.async'}) {
77                         async_check($ctx, $obj, \&tree_30x, $ctx);
78                 } else {
79                         $ctx->{git}->check_async($obj, \&tree_30x, $ctx);
80                         $ctx->{git}->async_wait_all;
81                 }
82         };
83 }
84
85 1;