From: Eric Wong Date: Thu, 24 Dec 2015 20:07:30 +0000 (+0000) Subject: repobrowse: consolidate trailing slash handling X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5ef6c4370a597a1217865944b78080ab3dd6a544;p=thirdparty%2Fpublic-inbox.git repobrowse: consolidate trailing slash handling And apply this to the git tree viewing code and allow navigating upwards from the tree listing view. --- diff --git a/lib/PublicInbox/RepoBrowse.pm b/lib/PublicInbox/RepoBrowse.pm index e6063f116..8fa1d035c 100644 --- a/lib/PublicInbox/RepoBrowse.pm +++ b/lib/PublicInbox/RepoBrowse.pm @@ -59,6 +59,7 @@ sub run { extra => \@extra, # path cgi => $cgi, rconfig => $rconfig, + tslash => 0, }; my $cmd = shift @extra; @@ -70,8 +71,12 @@ sub run { $mod = load_once("PublicInbox::RepoBrowse$vcs$mod"); $vcs = load_once("PublicInbox::$vcs"); $repo_info->{$vcs_lc} ||= $vcs->new($repo_info->{path}); - $req->{relcmd} = '../' x scalar(@extra); + while (@extra && $extra[-1] eq '') { + pop @extra; + ++$req->{tslash}; + } + $req->{expath} = join('/', @extra); my $rv = eval { $mod->new->call($cmd, $req) }; $rv || r404(); } else { diff --git a/lib/PublicInbox/RepoBrowseGitTree.pm b/lib/PublicInbox/RepoBrowseGitTree.pm index a3592a302..f0b15627c 100644 --- a/lib/PublicInbox/RepoBrowseGitTree.pm +++ b/lib/PublicInbox/RepoBrowseGitTree.pm @@ -17,17 +17,11 @@ my %GIT_MODE = ( sub git_tree_stream { my ($self, $req, $res) = @_; # res: Plack callback my @extra = @{$req->{extra}}; - my $tslash; - if (@extra && $extra[-1] eq '') { # no trailing slash - pop @extra; - $tslash = 1; - } - my $tree_path = join('/', @extra); my $q = PublicInbox::RepoBrowseQuery->new($req->{cgi}); my $id = $q->{id}; $id eq '' and $id = 'HEAD'; - my $obj = "$id:$tree_path"; + my $obj = "$id:$req->{expath}"; my $git = $req->{repo_info}->{git}; my ($hex, $type, $size) = $git->check($obj); @@ -41,7 +35,7 @@ sub git_tree_stream { PublicInbox::Hval::PRE); if ($type eq 'tree') { - git_tree_show($fh, $git, $hex, $q, \@extra, $tslash); + git_tree_show($req, $fh, $git, $hex, $q); } elsif ($type eq 'blob') { git_blob_show($fh, $git, $hex); } @@ -96,16 +90,40 @@ sub git_blob_show { } sub git_tree_show { - my ($fh, $git, $hex, $q, $extra, $tslash) = @_; + my ($req, $fh, $git, $hex, $q) = @_; my $ls = $git->popen(qw(ls-tree --abbrev=16 -l -z), $hex); local $/ = "\0"; - my $pfx = $tslash ? '.' : - (@$extra ? PublicInbox::Hval->new_bin($extra->[-1])->as_path : - 'tree'); - $pfx .= '/'; - + my $pfx; my $qs = $q->qs; + my @ex = @{$req->{extra}}; + my $rel = $req->{relcmd}; + my $t; + if (@ex) { + $t = "root/"; + my $cur = pop @ex; + my @t; + $t .= join('/', (map { + push @t, $_; + my $e = PublicInbox::Hval->new_bin($_, join('/', @t)); + my $ep = $e->as_path; + my $eh = $e->as_html; + "$eh"; + } @ex), ''.utf8_html($cur).''); + push @ex, $cur; + } else { + $t = 'root'; + } + $fh->write("$t\n"); + + if ($req->{tslash}) { + $pfx = './'; + } elsif (@ex) { + $pfx = $ex[-1] . '/'; + } else { + $pfx = 'tree/'; + } + while (defined(my $l = <$ls>)) { chomp $l; my ($m, $t, $x, $s, $path) =