From 8d83eab9d95348e996a2d1f92d970ddc0bf4f85b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 17 Feb 2017 03:31:16 +0000 Subject: repobrowse: rename "plain" endpoint to "raw" This name is shorter and matches terminology in gitweb and other popular git web viewers. --- MANIFEST | 4 +- lib/PublicInbox/RepoGitPlain.pm | 98 ----------------------------------------- lib/PublicInbox/RepoGitRaw.pm | 98 +++++++++++++++++++++++++++++++++++++++++ lib/PublicInbox/RepoGitTree.pm | 10 ++--- lib/PublicInbox/Repobrowse.pm | 4 +- t/repobrowse_git_plain.t | 30 ------------- t/repobrowse_git_raw.t | 30 +++++++++++++ 7 files changed, 137 insertions(+), 137 deletions(-) delete mode 100644 lib/PublicInbox/RepoGitPlain.pm create mode 100644 lib/PublicInbox/RepoGitRaw.pm delete mode 100644 t/repobrowse_git_plain.t create mode 100644 t/repobrowse_git_raw.t diff --git a/MANIFEST b/MANIFEST index bced2804..495a63e6 100644 --- a/MANIFEST +++ b/MANIFEST @@ -92,8 +92,8 @@ lib/PublicInbox/RepoGitDiffCommon.pm lib/PublicInbox/RepoGitFallback.pm lib/PublicInbox/RepoGitLog.pm lib/PublicInbox/RepoGitPatch.pm -lib/PublicInbox/RepoGitPlain.pm lib/PublicInbox/RepoGitQuery.pm +lib/PublicInbox/RepoGitRaw.pm lib/PublicInbox/RepoGitSearch.pm lib/PublicInbox/RepoGitSearchIdx.pm lib/PublicInbox/RepoGitSnapshot.pm @@ -195,7 +195,7 @@ t/repobrowse_git_atom.t t/repobrowse_git_commit.t t/repobrowse_git_httpd.t t/repobrowse_git_log.t -t/repobrowse_git_plain.t +t/repobrowse_git_raw.t t/repobrowse_git_snapshot.t t/repobrowse_git_tree.t t/search.t diff --git a/lib/PublicInbox/RepoGitPlain.pm b/lib/PublicInbox/RepoGitPlain.pm deleted file mode 100644 index 8aff2177..00000000 --- a/lib/PublicInbox/RepoGitPlain.pm +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright (C) 2015-2016 all contributors -# License: AGPL-3.0+ -package PublicInbox::RepoGitPlain; -use strict; -use warnings; -use base qw(PublicInbox::RepoBase); -use PublicInbox::RepoGitBlob; -use PublicInbox::Hval qw(utf8_html); -use PublicInbox::Qspawn; - -sub call_git_plain { - my ($self, $req) = @_; - my $repo = $req->{-repo}; - my $git = $repo->{git}; - my $id = $repo->tip . ':' . $req->{expath}; - my ($cat, $hex, $type, $size) = $git->cat_file_begin($id); - return unless defined $cat; - - my ($r, $buf); - my $left = $size; - if ($type eq 'blob') { - $type = git_blob_mime_type($self, $req, $cat, \$buf, \$left); - } elsif ($type eq 'commit' || $type eq 'tag') { - $type = 'text/plain'; - } elsif ($type eq 'tree') { - $git->cat_file_finish($left); - return git_tree_plain($req, $git, $hex); - } else { - $type = 'application/octet-stream'; - } - git_blob_stream_response($git, $cat, $size, $type, $buf, $left); -} - -sub git_tree_sed ($) { - my ($req) = @_; - my $buf = ''; - my $end; - my $pfx = $req->{tpfx}; - sub { # $_[0] = buffer or undef - my $dst = delete $req->{tstart} || ''; - my @files; - if (defined $_[0]) { - @files = split(/\0/, $buf .= $_[0]); - $buf = pop @files if scalar @files; - } else { - @files = split(/\0/, $buf); - $end = ''; - } - foreach my $n (@files) { - $n = PublicInbox::Hval->utf8($n); - my $ref = $n->as_path; - $dst .= qq(
  • ); - $dst .= $n->as_html; - $dst .= '
  • '; - } - $end ? $dst .= $end : $dst; - } -} - -# This should follow the cgit DOM structure in case anybody depends on it, -# not using
     here as we don't expect people to actually view it much
    -sub git_tree_plain {
    -	my ($req, $git, $hex) = @_;
    -
    -	my @ex = @{$req->{extra}};
    -	my $rel = $req->{relcmd};
    -	my $title = utf8_html(join('/', '', @ex, ''));
    -	my $tslash = $req->{tslash};
    -	my $pfx = $tslash ? './' : 'plain/';
    -	my $t = "

    $title

      "; - if (@ex) { - if ($tslash) { - $t .= qq(
    • ../
    • ); - } else { - $t .= qq(
    • ../
    • ); - my $last = PublicInbox::Hval->utf8($ex[-1])->as_href; - $pfx = "$last/"; - } - } - - $req->{tpfx} = $pfx; - $req->{tstart} = "$title".$t; - my $cmd = $git->cmd(qw(ls-tree --name-only -z), $git->abbrev, $hex); - my $rdr = { 2 => $git->err_begin }; - my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr); - my $env = $req->{env}; - $qsp->psgi_return($env, undef, sub { - my ($r) = @_; - if (!defined $r) { - [ 500, [ 'Content-Type', 'text/plain' ], [ $git->err ]]; - } else { - $env->{'qspawn.filter'} = git_tree_sed($req); - [ 200, [ 'Content-Type', 'text/html' ] ]; - } - }); -} - -1; diff --git a/lib/PublicInbox/RepoGitRaw.pm b/lib/PublicInbox/RepoGitRaw.pm new file mode 100644 index 00000000..ce6d7e73 --- /dev/null +++ b/lib/PublicInbox/RepoGitRaw.pm @@ -0,0 +1,98 @@ +# Copyright (C) 2015-2016 all contributors +# License: AGPL-3.0+ +package PublicInbox::RepoGitRaw; +use strict; +use warnings; +use base qw(PublicInbox::RepoBase); +use PublicInbox::RepoGitBlob; +use PublicInbox::Hval qw(utf8_html); +use PublicInbox::Qspawn; + +sub call_git_raw { + my ($self, $req) = @_; + my $repo = $req->{-repo}; + my $git = $repo->{git}; + my $id = $repo->tip . ':' . $req->{expath}; + my ($cat, $hex, $type, $size) = $git->cat_file_begin($id); + return unless defined $cat; + + my ($r, $buf); + my $left = $size; + if ($type eq 'blob') { + $type = git_blob_mime_type($self, $req, $cat, \$buf, \$left); + } elsif ($type eq 'commit' || $type eq 'tag') { + $type = 'text/plain'; + } elsif ($type eq 'tree') { + $git->cat_file_finish($left); + return git_tree_raw($req, $git, $hex); + } else { + $type = 'application/octet-stream'; + } + git_blob_stream_response($git, $cat, $size, $type, $buf, $left); +} + +sub git_tree_sed ($) { + my ($req) = @_; + my $buf = ''; + my $end; + my $pfx = $req->{tpfx}; + sub { # $_[0] = buffer or undef + my $dst = delete $req->{tstart} || ''; + my @files; + if (defined $_[0]) { + @files = split(/\0/, $buf .= $_[0]); + $buf = pop @files if scalar @files; + } else { + @files = split(/\0/, $buf); + $end = '
    '; + } + foreach my $n (@files) { + $n = PublicInbox::Hval->utf8($n); + my $ref = $n->as_path; + $dst .= qq(
  • ); + $dst .= $n->as_html; + $dst .= '
  • '; + } + $end ? $dst .= $end : $dst; + } +} + +# This should follow the cgit DOM structure in case anybody depends on it, +# not using
     here as we don't expect people to actually view it much
    +sub git_tree_raw {
    +	my ($req, $git, $hex) = @_;
    +
    +	my @ex = @{$req->{extra}};
    +	my $rel = $req->{relcmd};
    +	my $title = utf8_html(join('/', '', @ex, ''));
    +	my $tslash = $req->{tslash};
    +	my $pfx = $tslash ? './' : 'raw/';
    +	my $t = "

    $title

      "; + if (@ex) { + if ($tslash) { + $t .= qq(
    • ../
    • ); + } else { + $t .= qq(
    • ../
    • ); + my $last = PublicInbox::Hval->utf8($ex[-1])->as_href; + $pfx = "$last/"; + } + } + + $req->{tpfx} = $pfx; + $req->{tstart} = "$title".$t; + my $cmd = $git->cmd(qw(ls-tree --name-only -z), $git->abbrev, $hex); + my $rdr = { 2 => $git->err_begin }; + my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr); + my $env = $req->{env}; + $qsp->psgi_return($env, undef, sub { + my ($r) = @_; + if (!defined $r) { + [ 500, [ 'Content-Type', 'text/plain' ], [ $git->err ]]; + } else { + $env->{'qspawn.filter'} = git_tree_sed($req); + [ 200, [ 'Content-Type', 'text/html' ] ]; + } + }); +} + +1; diff --git a/lib/PublicInbox/RepoGitTree.pm b/lib/PublicInbox/RepoGitTree.pm index a4add619..5b428da0 100644 --- a/lib/PublicInbox/RepoGitTree.pm +++ b/lib/PublicInbox/RepoGitTree.pm @@ -83,16 +83,16 @@ sub git_blob_show { my $n = 0; my $rel = $req->{relcmd}; - my $plain = join('/', - "${rel}plain", $req->{-repo}->tip, @{$req->{extra}}); - $plain = PublicInbox::Hval->utf8($plain)->as_path; + my $raw = join('/', + "${rel}raw", $req->{-repo}->tip, @{$req->{extra}}); + $raw = PublicInbox::Hval->utf8($raw)->as_path; my $t = cur_path($req); my $s = qq{\npath: $t\n\nblob $hex}; my $end = ''; $git->cat_file($hex, sub { my ($cat, $left) = @_; # $$left == $size - $s .= qq{\t$$left bytes (raw)}; + $s .= qq{\t$$left bytes (raw)}; $to_read = $$left if $to_read > $$left; my $r = read($cat, my $buf, $to_read); return unless defined($r) && $r > 0; @@ -172,7 +172,7 @@ sub git_tree_sed ($) { elsif ($m eq 'l') { $path = "$path" } $s =~ s/\s+//g; - # 'plain' and 'log' links intentionally omitted + # 'raw' and 'log' links intentionally omitted # for brevity and speed $dst .= qq($m\t). qq($s\t$path\n); diff --git a/lib/PublicInbox/Repobrowse.pm b/lib/PublicInbox/Repobrowse.pm index 34ffd542..de5bd364 100644 --- a/lib/PublicInbox/Repobrowse.pm +++ b/lib/PublicInbox/Repobrowse.pm @@ -23,7 +23,7 @@ use warnings; use URI::Escape qw(uri_escape_utf8); use PublicInbox::RepoConfig; -my %CMD = map { lc($_) => $_ } qw(Log Commit Tree Patch Blob Plain Tag Atom +my %CMD = map { lc($_) => $_ } qw(Log Commit Tree Patch Blob Raw Tag Atom Diff Snapshot); my %VCS = (git => 'Git'); my %LOADED; @@ -57,7 +57,7 @@ sub base_url ($) { # Remove trailing slash in URLs which regular humans are likely to read # in an attempt to improve cache hit ratios. Do not redirect -# plain|patch|blob|fallback endpoints since those could be using +# raw|patch|blob|fallback endpoints since those could be using # automated tools which may not follow redirects automatically # (e.g. curl does not follow 301 unless given "-L") my %NO_TSLASH = map { $_ => 1 } qw(Log Commit Tree Summary Tag); diff --git a/t/repobrowse_git_plain.t b/t/repobrowse_git_plain.t deleted file mode 100644 index a93fa10e..00000000 --- a/t/repobrowse_git_plain.t +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (C) 2016 all contributors -# License: AGPL-3.0+ -use strict; -use warnings; -my $test = require './t/repobrowse_common_git.perl'; - -test_psgi($test->{app}, sub { - my ($cb) = @_; - - my $req = 'http://example.com/test.git/plain/master/dir'; - my $res = $cb->(GET($req)); - is(200, $res->code, 'got 200 response from dir'); - my $noslash_body = dechunk($res); - like($noslash_body, qr{href="dir/dur">dur}, - 'path ok w/o slash'); - - my $slash = $req . '/'; - my $r2 = $cb->(GET($slash)); - is(200, $r2->code, 'got 200 response from dir'); - my $slash_body = dechunk($r2); - like($slash_body, qr{href="\./dur\">dur}, 'path ok w/ slash'); - - $req = 'http://example.com/test.git/plain/master/foo.txt'; - my $blob = $cb->(GET($req)); - like($blob->header('Content-Type'), qr!\Atext/plain\b!, - 'got text/plain blob'); - is($blob->content, "-----\nhello\nworld\n", 'raw blob passed'); -}); - -done_testing(); diff --git a/t/repobrowse_git_raw.t b/t/repobrowse_git_raw.t new file mode 100644 index 00000000..2048d82a --- /dev/null +++ b/t/repobrowse_git_raw.t @@ -0,0 +1,30 @@ +# Copyright (C) 2016 all contributors +# License: AGPL-3.0+ +use strict; +use warnings; +my $test = require './t/repobrowse_common_git.perl'; + +test_psgi($test->{app}, sub { + my ($cb) = @_; + + my $req = 'http://example.com/test.git/raw/master/dir'; + my $res = $cb->(GET($req)); + is(200, $res->code, 'got 200 response from dir'); + my $noslash_body = dechunk($res); + like($noslash_body, qr{href="dir/dur">dur}, + 'path ok w/o slash'); + + my $slash = $req . '/'; + my $r2 = $cb->(GET($slash)); + is(200, $r2->code, 'got 200 response from dir'); + my $slash_body = dechunk($r2); + like($slash_body, qr{href="\./dur\">dur}, 'path ok w/ slash'); + + $req = 'http://example.com/test.git/raw/master/foo.txt'; + my $blob = $cb->(GET($req)); + like($blob->header('Content-Type'), qr!\Atext/plain\b!, + 'got text/plain blob'); + is($blob->content, "-----\nhello\nworld\n", 'raw blob passed'); +}); + +done_testing(); -- cgit v1.2.3-24-ge0c7