]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/RepoTree.pm
coderepo: consolidate git --batch-check users
[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         if ($$bref eq '') {
22                 say $zfh "found no record of `$path' in git history";
23                 $ctx->{-has_srch} and
24                         say $zfh 'perhaps try searching mail (above)';
25         } else {
26                 my ($H, $h, $s_as) = split(/ /, $$bref, 3);
27                 utf8::decode($s_as);
28                 my $x = uri_escape_path($ctx->{-path});
29                 $s_as = ascii_html($s_as);
30                 print $zfh <<EOM;
31 found last record of `$path' in the following commit:
32
33 <a href="$ctx->{-upfx}$H/s/?b=$x">$h</a> $s_as
34 EOM
35         }
36         delete($ctx->{-wcb})->($ctx->html_done);
37 }
38
39 sub find_missing {
40         my ($ctx) = @_;
41         my $cmd = ['git', "--git-dir=$ctx->{git}->{git_dir}",
42                 qw(log --no-color -1), '--pretty=%H %h %s (%as)' ];
43         push @$cmd, $ctx->{qp}->{h} if defined($ctx->{qp}->{h});
44         push @$cmd, '--';
45         push @$cmd, $ctx->{-path} if $ctx->{-path} ne '';
46         my $qsp = PublicInbox::Qspawn->new($cmd);
47         $qsp->psgi_qx($ctx->{env}, undef, \&rd_404_log, $ctx);
48 }
49
50 sub tree_show { # git check_async callback
51         my ($oid, $type, $size, $ctx) = @_;
52         return find_missing($ctx) if $type eq 'missing';
53
54         open $ctx->{lh}, '<', \(my $dbg_log = '') or die "open(scalar): $!";
55         my $res = [ $ctx->{git}, $oid, $type, $size ];
56         my ($bn) = ($ctx->{-path} =~ m!/?([^/]+)\z!);
57         if ($type eq 'blob') {
58                 my $obj = ascii_html($ctx->{-obj});
59                 $ctx->{-paths} = [ $bn, qq[(<a
60 href="$ctx->{-upfx}$oid/s/$bn">raw</a>)
61 \$ git show $obj\t# shows this blob on the CLI] ];
62         }
63         PublicInbox::ViewVCS::solve_result($res, $ctx);
64 }
65
66 sub srv_tree {
67         my ($ctx, $path) = @_;
68         return if index($path, '//') >= 0 || index($path, '/') == 0;
69         my $tip = $ctx->{qp}->{h} // 'HEAD';
70         $ctx->{-upfx} = '../' x (($path =~ tr!/!/!) + 1);
71         $path =~ s!/\z!!;
72         my $obj = $ctx->{-obj} = "$tip:$path";
73         $ctx->{-path} = $path;
74
75         # "\n" breaks with `git cat-file --batch-check', and there's no
76         # legitimate use of "\n" in filenames anyways.
77         return if index($obj, "\n") >= 0;
78         sub {
79                 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
80                 PublicInbox::ViewVCS::do_check_async($ctx, \&tree_show, $obj);
81         };
82 }
83
84 1;