user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] www_coderepo: /tree/ redirects to /$OID/s/
Date: Thu, 12 Jan 2023 14:14:34 +0000	[thread overview]
Message-ID: <20230112141435.1924376-3-e@80x24.org> (raw)
In-Reply-To: <20230112141435.1924376-1-e@80x24.org>

This is for compatibility with cgit to ease migration.
---
 MANIFEST                       |  1 +
 lib/PublicInbox/GitAsyncCat.pm |  2 +-
 lib/PublicInbox/RepoTree.pm    | 44 ++++++++++++++++++++++++++++++++++
 lib/PublicInbox/WwwCoderepo.pm |  6 +++++
 t/solver_git.t                 |  7 ++++++
 5 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 lib/PublicInbox/RepoTree.pm

diff --git a/MANIFEST b/MANIFEST
index 3626e4d2..c494d6f7 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -311,6 +311,7 @@ lib/PublicInbox/Qspawn.pm
 lib/PublicInbox/Reply.pm
 lib/PublicInbox/RepoAtom.pm
 lib/PublicInbox/RepoSnapshot.pm
+lib/PublicInbox/RepoTree.pm
 lib/PublicInbox/SaPlugin/ListMirror.pm
 lib/PublicInbox/SaPlugin/ListMirror.pod
 lib/PublicInbox/Search.pm
diff --git a/lib/PublicInbox/GitAsyncCat.pm b/lib/PublicInbox/GitAsyncCat.pm
index 2e0725a6..6dda7340 100644
--- a/lib/PublicInbox/GitAsyncCat.pm
+++ b/lib/PublicInbox/GitAsyncCat.pm
@@ -75,7 +75,7 @@ sub ibx_async_cat ($$$$) {
 }
 
 sub async_check ($$$$) {
-	my ($ibx, $oidish, $cb, $arg) = @_;
+	my ($ibx, $oidish, $cb, $arg) = @_; # $ibx may be $ctx
 	my $git = $ibx->{git} // $ibx->git;
 	$git->check_async($oidish, $cb, $arg);
 	$git->{async_chk} //= do {
diff --git a/lib/PublicInbox/RepoTree.pm b/lib/PublicInbox/RepoTree.pm
new file mode 100644
index 00000000..7f2ff206
--- /dev/null
+++ b/lib/PublicInbox/RepoTree.pm
@@ -0,0 +1,44 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# cgit-compatible $REPO/tree/[PATH]?h=$tip redirector
+package PublicInbox::RepoTree;
+use v5.12;
+use PublicInbox::ViewDiff qw(uri_escape_path);
+use PublicInbox::GitAsyncCat;
+use PublicInbox::WwwStatic qw(r);
+
+sub tree_30x { # git check_async callback
+	my ($oid, $type, $size, $ctx) = @_;
+	my $wcb = delete $ctx->{-wcb};
+	return $wcb->(r(404)) if $type eq 'missing';
+	my $u = $ctx->{git}->base_url($ctx->{env});
+	my $path = uri_escape_path(delete $ctx->{-path});
+	$u .= "$oid/s/?b=$path";
+	$wcb->([ 302, [ Location => $u, 'Content-Type' => 'text/plain' ],
+		[ "Redirecting to $u\n" ] ])
+}
+
+sub srv_tree {
+	my ($ctx, $path) = @_;
+	return if index($path, '//') >= 0 || index($path, '/') == 0;
+	my $tip = $ctx->{qp}->{h} // 'HEAD';
+	$path =~ s!/\z!!;
+	my $obj = $ctx->{-obj} = "$tip:$path";
+	$ctx->{-path} = $path;
+
+	# "\n" breaks with `git cat-file --batch-check', and there's no
+	# legitimate use of "\n" in filenames anyways.
+	return if index($obj, "\n") >= 0;
+	sub {
+		$ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
+		if ($ctx->{env}->{'pi-httpd.async'}) {
+			async_check($ctx, $obj, \&tree_30x, $ctx);
+		} else {
+			$ctx->{git}->check_async($obj, \&tree_30x, $ctx);
+			$ctx->{git}->async_wait_all;
+		}
+	};
+}
+
+1;
diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index 2fba0cd0..668b6398 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -18,6 +18,7 @@ use PublicInbox::Hval qw(ascii_html);
 use PublicInbox::ViewDiff qw(uri_escape_path);
 use PublicInbox::RepoSnapshot;
 use PublicInbox::RepoAtom;
+use PublicInbox::RepoTree;
 
 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
 	join('%00', map { "%($_)" }
@@ -226,6 +227,11 @@ sub srv { # endpoint called by PublicInbox::WWW
 			($ctx->{git} = $cr->{$1}) and
 		return PublicInbox::ViewVCS::show($ctx, $2);
 
+	if ($path_info =~ m!\A/(.+?)/tree/(.*)\z! and
+			($ctx->{git} = $cr->{$1})) {
+		return PublicInbox::RepoTree::srv_tree($ctx, $2) // r(404);
+	}
+
 	# snapshots:
 	if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
 			($ctx->{git} = $cr->{$1})) {
diff --git a/t/solver_git.t b/t/solver_git.t
index 89ed0362..5519fa18 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -381,6 +381,13 @@ EOF
 			$res = $cb->(GET('/public-inbox/atom/README.md'));
 			is($res->code, 404, '404 on non-existent file Atom feed');
 		}
+
+		$res = $cb->(GET('/public-inbox/tree/'));
+		is($res->code, 302, 'got redirect');
+		$res = $cb->(GET('/public-inbox/tree/README'));
+		is($res->code, 302, 'got redirect for regular file');
+		$res = $cb->(GET('/public-inbox/tree/Documentation'));
+		is($res->code, 302, 'got redirect for directory');
 	};
 	test_psgi(sub { $www->call(@_) }, $client);
 	my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };

  parent reply	other threads:[~2023-01-12 14:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-12 14:14 [PATCH 0/3] coderepo: cgit-compatible /tree/ redirect Eric Wong
2023-01-12 14:14 ` [PATCH 1/3] www_stream: coderepo-specific top bar Eric Wong
2023-01-12 14:14 ` Eric Wong [this message]
2023-01-12 14:14 ` [PATCH 3/3] www_coderepo: /tree/ 404s search git history Eric Wong
2023-01-12 14:19   ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230112141435.1924376-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).