unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH 0/7] www_coderepo updates
@ 2023-01-28 11:02 Eric Wong
  2023-01-28 11:02 ` [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs Eric Wong
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

Not aiming to achieve feature parity with cgit, but
trying to minimize 404s and provide hints/education for
CLI git usage...

And having an Atom feed for just tags (not all commits) can be
nice...

Eric Wong (7):
  www_coderepo: tree: quiet and 404 on non-existent refs
  www_coderepo: support /$REPO/tags.atom endpoint
  www_coderepo: fix snapshot link generation
  www_coderepo: reduce utf8::decode calls
  repo_atom: translate: account for multiple args
  www_coderepo: support $REPO/refs/{heads,tags}/ endpoints
  www_coderepo: summary: fix mis-linkification of `...'

 lib/PublicInbox/RepoAtom.pm    |  64 +++++++++---
 lib/PublicInbox/RepoTree.pm    |  22 ++++-
 lib/PublicInbox/WwwCoderepo.pm | 173 +++++++++++++++++++++++----------
 t/solver_git.t                 |  15 +++
 4 files changed, 209 insertions(+), 65 deletions(-)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 2/7] www_coderepo: support /$REPO/tags.atom endpoint Eric Wong
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

Clients should see 404s when attempting to hit files for deleted
branches or tags.
---
 lib/PublicInbox/RepoTree.pm | 22 ++++++++++++++++++----
 t/solver_git.t              |  7 +++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/RepoTree.pm b/lib/PublicInbox/RepoTree.pm
index 3a848c6f..25dc5b10 100644
--- a/lib/PublicInbox/RepoTree.pm
+++ b/lib/PublicInbox/RepoTree.pm
@@ -18,10 +18,12 @@ sub rd_404_log {
 	PublicInbox::WwwStream::html_init($ctx);
 	my $zfh = $ctx->{zfh};
 	print $zfh "<pre>\$ git log -1 $tip -- $path\n";
+	my $code = 200;
 	if ($$bref eq '') {
-		say $zfh "found no record of `$path' in git history";
+		say $zfh "found no record of `$path' in git history in `$tip'";
 		$ctx->{-has_srch} and
 			say $zfh 'perhaps try searching mail (above)';
+		$code = 404;
 	} else {
 		my ($H, $h, $s_as) = split(/ /, $$bref, 3);
 		utf8::decode($s_as);
@@ -33,17 +35,29 @@ found last record of `$path' in the following commit:
 <a href="$ctx->{-upfx}$H/s/?b=$x">$h</a> $s_as
 EOM
 	}
-	delete($ctx->{-wcb})->($ctx->html_done);
+	my $res = $ctx->html_done;
+	$res->[0] = $code;
+	delete($ctx->{-wcb})->($res);
 }
 
 sub find_missing {
 	my ($ctx) = @_;
+	if ($ctx->{-path} eq '') {
+		my $tip = 'HEAD';
+		$tip = ascii_html($ctx->{qp}->{h}) if defined($ctx->{qp}->{h});
+		PublicInbox::WwwStream::html_init($ctx);
+		print { $ctx->{zfh} } "<pre>`$tip' ref not found</pre>";
+		my $res = $ctx->html_done;
+		$res->[0] = 404;
+		return delete($ctx->{-wcb})->($res);
+	}
 	my $cmd = ['git', "--git-dir=$ctx->{git}->{git_dir}",
 		qw(log --no-color -1), '--pretty=%H %h %s (%as)' ];
 	push @$cmd, $ctx->{qp}->{h} if defined($ctx->{qp}->{h});
 	push @$cmd, '--';
-	push @$cmd, $ctx->{-path} if $ctx->{-path} ne '';
-	my $qsp = PublicInbox::Qspawn->new($cmd);
+	push @$cmd, $ctx->{-path};
+	my $qsp = PublicInbox::Qspawn->new($cmd, undef,
+					{ quiet => 1, 2 => $ctx->{lh} });
 	$qsp->psgi_qx($ctx->{env}, undef, \&rd_404_log, $ctx);
 }
 
diff --git a/t/solver_git.t b/t/solver_git.t
index 122cf888..7743913b 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -396,6 +396,13 @@ EOF
 		is($res->code, 200, 'got 200 for a directory');
 		$got = $res->content;
 		like($got, qr/\bgit ls-tree\b/, 'ls-tree help shown');
+
+		$res = $cb->(GET('/public-inbox/tree/?h=no-branch'));
+		is($res->code, 404, 'got 404 for non-existent ref root');
+		$res = $cb->(GET('/public-inbox/tree/README?h=no-file'));
+		is($res->code, 404, 'got 404 for non-existent ref README');
+		$res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
+		is($res->code, 404, 'got 404 for non-existent ref directory');
 	};
 	test_psgi(sub { $www->call(@_) }, $client);
 	my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/7] www_coderepo: support /$REPO/tags.atom endpoint
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
  2023-01-28 11:02 ` [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 3/7] www_coderepo: fix snapshot link generation Eric Wong
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

Providing an Atom feed for tags can be a nice way for users
to subscribe to new releases without excessive noise.
---
 lib/PublicInbox/RepoAtom.pm    | 62 ++++++++++++++++++++++++++++------
 lib/PublicInbox/WwwCoderepo.pm |  3 ++
 t/solver_git.t                 |  8 +++++
 3 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/lib/PublicInbox/RepoAtom.pm b/lib/PublicInbox/RepoAtom.pm
index 66f12157..4a013147 100644
--- a/lib/PublicInbox/RepoAtom.pm
+++ b/lib/PublicInbox/RepoAtom.pm
@@ -10,10 +10,15 @@ use URI::Escape qw(uri_escape);
 use Scalar::Util ();
 use PublicInbox::Hval qw(ascii_html);
 
+# git for-each-ref and log use different format fields :<
 my $ATOM_FMT = '--pretty=tformat:'.join('%n',
 				map { "%$_" } qw(H ct an ae at s b)).'%x00';
 
-sub log2atom_ok { # parse_hdr for qspawn
+my $EACH_REF_FMT = '--format='.join(';', map { "\$r{'$_'}=%($_)" } qw(
+	objectname refname:short creator contents:subject contents:body
+	*subject *body)).'%00';
+
+sub atom_ok { # parse_hdr for qspawn
 	my ($r, $bref, $ctx) = @_;
 	return [ 404, [], [ "Not Found\n"] ] if $r == 0;
 	bless $ctx, __PACKAGE__;
@@ -42,28 +47,62 @@ sub translate {
 	my @out;
 	my $lbuf = delete($self->{lbuf}) // shift;
 	$lbuf .= shift if @_;
+	my $is_tag = $self->{-is_tag};
+	my ($H, $ct, $an, $ae, $at, $s, $bdy);
 	while ($lbuf =~ s/\A([^\0]+)\0\n//s) {
-		my $ent = $1;
-		utf8::decode($ent);
-		$ent = ascii_html($ent);
-		my ($H, $ct, $an, $ae, $at, $s, $bdy) = split(/\n/, $ent, 7);
-		undef $ent;
+		utf8::decode($bdy = $1);
+		if ($is_tag) {
+			my %r;
+			eval "$bdy";
+			for (qw(contents:subject contents:body)) {
+				$r{$_} =~ /\S/ or delete($r{$_})
+			}
+			$H = $r{objectname};
+			$s = $r{'contents:subject'} // $r{'*subject'};
+			$bdy = $r{'contents:body'} // $r{'*body'};
+			$s .= " ($r{'refname:short'})";
+			$_ = ascii_html($_) for ($s, $bdy, $r{creator});
+			($an, $ae, $at) = split(/\s*&[gl]t;\s*/, $r{creator});
+			$at =~ s/ .*\z//; # no TZ
+			$ct = $at = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($at));
+		} else {
+			$bdy = ascii_html($bdy);
+			($H, $ct, $an, $ae, $at, $s, $bdy) =
+							split(/\n/, $bdy, 7);
+			$at = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($at));
+			$ct = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($ct));
+		}
 		$bdy //= '';
-		$_ = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($_)) for ($ct, $at);
-
-		push @out, <<"", $bdy, '</pre></div></content></entry>'
+		push @out, <<"";
 <entry><title>$s</title><updated>$ct</updated><author><name>$an</name>
 <email>$ae</email></author><published>$at</published><link
 rel="alternate" type="text/html" href="$self->{-base_url}$H/s/"
-/><id>$H</id><content type="xhtml"><div
+/><id>$H</id>
+
+		push @out, <<'', $bdy, '</pre></div></content>' if $bdy ne '';
+<content type="xhtml"><div
 xmlns="http://www.w3.org/1999/xhtml"><pre style="white-space:pre-wrap">
 
+		push @out, '</entry>';
 	}
 	$self->{lbuf} = $lbuf;
 	chomp @out;
 	$self->SUPER::translate(@out);
 }
 
+# $REPO/tags.atom endpoint
+sub srv_tags_atom {
+	my ($ctx) = @_;
+	my $max = 50; # TODO configurable
+	my @cmd = ('git', "--git-dir=$ctx->{git}->{git_dir}",
+			qw(for-each-ref --sort=-creatordate), "--count=$max",
+			'--perl', $EACH_REF_FMT, 'refs/tags');
+	$ctx->{-feed_title} = "$ctx->{git}->{nick} tags";
+	my $qsp = PublicInbox::Qspawn->new(\@cmd);
+	$ctx->{-is_tag} = 1;
+	$qsp->psgi_return($ctx->{env}, undef, \&atom_ok, $ctx);
+}
+
 sub srv_atom {
 	my ($ctx, $path) = @_;
 	return if index($path, '//') >= 0 || index($path, '/') == 0;
@@ -73,6 +112,7 @@ sub srv_atom {
 			$ATOM_FMT, "-$max");
 	my $tip = $ctx->{qp}->{h}; # same as cgit
 	$ctx->{-feed_title} = $ctx->{git}->{nick};
+	$ctx->{-feed_title} .= " $path" if $path ne '';
 	if (defined($tip)) {
 		push @cmd, $tip;
 		$ctx->{-feed_title} .= ", $tip";
@@ -81,7 +121,7 @@ sub srv_atom {
 	push @cmd, '--';
 	push @cmd, $path if $path ne '';
 	my $qsp = PublicInbox::Qspawn->new(\@cmd);
-	$qsp->psgi_return($ctx->{env}, undef, \&log2atom_ok, $ctx);
+	$qsp->psgi_return($ctx->{env}, undef, \&atom_ok, $ctx);
 }
 
 1;
diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index 8dcd9772..e3d45c56 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -240,6 +240,9 @@ sub srv { # endpoint called by PublicInbox::WWW
 	} elsif ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
 			($ctx->{git} = $cr->{$1})) {
 		PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
+	} elsif ($path_info =~ m!\A/(.+?)/tags\.atom\z! and
+			($ctx->{git} = $cr->{$1})) {
+		PublicInbox::RepoAtom::srv_tags_atom($ctx);
 	} elsif ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
 		my $qs = $ctx->{env}->{QUERY_STRING};
 		my $url = $git->base_url($ctx->{env});
diff --git a/t/solver_git.t b/t/solver_git.t
index 7743913b..0090bc06 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -403,6 +403,14 @@ EOF
 		is($res->code, 404, 'got 404 for non-existent ref README');
 		$res = $cb->(GET('/public-inbox/tree/Documentation?h=no-dir'));
 		is($res->code, 404, 'got 404 for non-existent ref directory');
+
+		$res = $cb->(GET('/public-inbox/tags.atom'));
+		is($res->code, 200, 'Atom feed');
+		SKIP: {
+			require_mods('XML::TreePP', 1);
+			my $t = XML::TreePP->new->parse($res->content);
+			ok(scalar @{$t->{feed}->{entry}}, 'got tag entries');
+		}
 	};
 	test_psgi(sub { $www->call(@_) }, $client);
 	my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/7] www_coderepo: fix snapshot link generation
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
  2023-01-28 11:02 ` [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs Eric Wong
  2023-01-28 11:02 ` [PATCH 2/7] www_coderepo: support /$REPO/tags.atom endpoint Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 4/7] www_coderepo: reduce utf8::decode calls Eric Wong
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

Do not assume ".git" exists as a suffix in the repo nickname,
and filter out all trailing slashes in case it didn't get
filtered from Config.
---
 lib/PublicInbox/WwwCoderepo.pm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index e3d45c56..4d8713b4 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -142,9 +142,9 @@ EOM
 	my $n;
 	if (@s) {
 		$n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
-		$n =~ s/\.git\z/-/;
-		($n) = ($n =~ m!([^/]+)\z!);
-		$n = ascii_html($n);
+		$n =~ s!\.git/*\z!!;
+		($n) = ($n =~ m!([^/]+)/*\z!);
+		$n = ascii_html($n).'-';
 	}
 	for (@r) {
 		my (undef, $oid, $ref, $s, $cd) = split(/\0/);

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/7] www_coderepo: reduce utf8::decode calls
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
                   ` (2 preceding siblings ...)
  2023-01-28 11:02 ` [PATCH 3/7] www_coderepo: fix snapshot link generation Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 5/7] repo_atom: translate: account for multiple args Eric Wong
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

It's safe to call utf8::decode on data where "\0" exists.
---
 lib/PublicInbox/WwwCoderepo.pm | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index 4d8713b4..8df6116a 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -124,7 +124,6 @@ EOM
 	$last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_branches};
 	for (@r) {
 		my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
-		utf8::decode($_) for ($ref, $s);
 		chomp $cd;
 		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
 		print $zfh "$pfx <a\nhref=./$oid/s/>", ascii_html($ref),
@@ -148,7 +147,6 @@ EOM
 	}
 	for (@r) {
 		my (undef, $oid, $ref, $s, $cd) = split(/\0/);
-		utf8::decode($_) for ($ref, $s);
 		chomp $cd;
 		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
 		print $zfh "<a\nhref=./$oid/s/>", ascii_html($ref),
@@ -169,6 +167,7 @@ EOM
 sub capture_refs ($$) { # psgi_qx callback to capture git-for-each-ref + git-log
 	my ($bref, $ctx) = @_;
 	my $qsp_err = delete $ctx->{-qsp_err};
+	utf8::decode($$bref);
 	$ctx->{-each_refs} = $$bref;
 	summary_finish($ctx) if $ctx->{-readme};
 }

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 5/7] repo_atom: translate: account for multiple args
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
                   ` (3 preceding siblings ...)
  2023-01-28 11:02 ` [PATCH 4/7] www_coderepo: reduce utf8::decode calls Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 6/7] www_coderepo: support $REPO/refs/{heads,tags}/ endpoints Eric Wong
  2023-01-28 11:02 ` [PATCH 7/7] www_coderepo: summary: fix mis-linkification of `...' Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

->translate should handle unlimited args, even if we don't
currently use it that way...
---
 lib/PublicInbox/RepoAtom.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/RepoAtom.pm b/lib/PublicInbox/RepoAtom.pm
index 4a013147..44883ab4 100644
--- a/lib/PublicInbox/RepoAtom.pm
+++ b/lib/PublicInbox/RepoAtom.pm
@@ -46,7 +46,7 @@ sub translate {
 	my $rec = $_[0] // return $self->zflush; # getline
 	my @out;
 	my $lbuf = delete($self->{lbuf}) // shift;
-	$lbuf .= shift if @_;
+	$lbuf .= shift while @_;
 	my $is_tag = $self->{-is_tag};
 	my ($H, $ct, $an, $ae, $at, $s, $bdy);
 	while ($lbuf =~ s/\A([^\0]+)\0\n//s) {

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 6/7] www_coderepo: support $REPO/refs/{heads,tags}/ endpoints
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
                   ` (4 preceding siblings ...)
  2023-01-28 11:02 ` [PATCH 5/7] repo_atom: translate: account for multiple args Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  2023-01-28 11:02 ` [PATCH 7/7] www_coderepo: summary: fix mis-linkification of `...' Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

These are also in cgit, but we'll include CLI hints to show
viewers how our data is generated.  We don't have "$REPO/refs/"
without (heads|tags) yet, though...
---
 lib/PublicInbox/WwwCoderepo.pm | 161 ++++++++++++++++++++++++---------
 1 file changed, 117 insertions(+), 44 deletions(-)

diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index 8df6116a..d28b1a03 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -7,6 +7,7 @@
 # cloning + command-line usage.
 package PublicInbox::WwwCoderepo;
 use v5.12;
+use parent qw(PublicInbox::WwwStream);
 use File::Temp 0.19 (); # newdir
 use POSIX qw(O_RDWR F_GETFL);
 use PublicInbox::ViewVCS;
@@ -19,9 +20,22 @@ use PublicInbox::RepoSnapshot;
 use PublicInbox::RepoAtom;
 use PublicInbox::RepoTree;
 
-my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
-	join('%00', map { "%($_)" }
-	qw(objectname refname:short subject creatordate:short))."'";
+my @EACH_REF = (qw(git for-each-ref --sort=-creatordate),
+		"--format=%(HEAD)%00".join('%00', map { "%($_)" }
+		qw(objectname refname:short subject creatordate:short)));
+my $EACH_REF = "@EACH_REF[0..2] '$EACH_REF[3]'";
+my $HEADS_CMD = <<'';
+# heads (aka `branches'):
+$ git for-each-ref --sort=-creatordate refs/heads \
+	--format='%(HEAD) %(refname:short) %(subject) (%(creatordate:short))'
+
+my $TAGS_CMD = <<'';
+# tags:
+$ git for-each-ref --sort=-creatordate refs/tags \
+	--format='%(refname:short) %(subject) (%(creatordate:short))'
+
+my $NO_HEADS = "# no heads (branches), yet...\n";
+my $NO_TAGS = "# no tags, yet...\n";
 
 # shared with PublicInbox::Cgit
 sub prepare_coderepos {
@@ -73,6 +87,39 @@ sub new {
 	$self;
 }
 
+sub _snapshot_link_prep {
+	my ($ctx) = @_;
+	my @s = sort keys %{$ctx->{wcr}->{snapshots}} or return ();
+	my $n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
+	$n =~ s!\.git/*\z!!;
+	($n) = ($n =~ m!([^/]+)/*\z!);
+	(ascii_html($n).'-', @s);
+}
+
+sub _refs_heads_link {
+	my ($line, $upfx) = @_;
+	my ($pfx, $oid, $ref, $s, $cd) = split(/\0/, $line);
+	my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
+	("$pfx <a\nhref=$upfx$oid/s/>", ascii_html($ref),
+		"</a>$align ", ascii_html($s), " ($cd)\n")
+}
+
+sub _refs_tags_link {
+	my ($line, $upfx, $snap_pfx, @snap_fmt) = @_;
+	my (undef, $oid, $ref, $s, $cd) = split(/\0/, $line);
+	my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
+	if (@snap_fmt) {
+		my $v = $ref;
+		$v =~ s/\A[vV]//;
+		@snap_fmt = map {
+			qq{ <a href="${upfx}snapshot/$snap_pfx$v.$_">$_</a>}
+		} @snap_fmt;
+		substr($snap_fmt[0], 0, 1) = "\t";
+	}
+	("<a\nhref=$upfx$oid/s/>", ascii_html($ref),
+		"</a>$align ", ascii_html($s), " ($cd)", @snap_fmt, "\n");
+}
+
 sub summary_finish {
 	my ($ctx) = @_;
 	my $wcb = delete($ctx->{env}->{'qspawn.wcb'}) or return; # already done
@@ -116,51 +163,21 @@ EOM
 	}
 
 	# refs/heads
-	print $zfh "<a id=heads># heads (aka `branches'):</a>\n\$ " .
-		"git for-each-ref --sort=-creatordate refs/heads" .
-		" \\\n\t--format='%(HEAD) ". # no space for %(align:) hint
-		"%(refname:short) %(subject) (%(creatordate:short))'\n";
+	print $zfh '<a id=heads>', $HEADS_CMD , '</a>';
 	@r = split(/^/sm, shift(@x) // '');
 	$last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_branches};
-	for (@r) {
-		my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
-		chomp $cd;
-		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
-		print $zfh "$pfx <a\nhref=./$oid/s/>", ascii_html($ref),
-			"</a>$align ", ascii_html($s), " ($cd)\n";
-	}
-	print $zfh "# no heads (branches) yet...\n" if !@r;
-	print $zfh "...\n" if $last;
-	print $zfh "\n<a id=tags># tags:</a>\n\$ " .
-		"git for-each-ref --sort=-creatordate refs/tags" .
-		" \\\n\t--format='". # no space for %(align:) hint
-		"%(refname:short) %(subject) (%(creatordate:short))'\n";
+	chomp(@r);
+	for (@r) { print $zfh _refs_heads_link($_, './') }
+	print $zfh $NO_HEADS if !@r;
+	print $zfh qq(<a href="refs/heads/">...</a>\n) if $last;
+	print $zfh "\n<a id=tags>", $TAGS_CMD, '</a>';
 	@r = split(/^/sm, shift(@x) // '');
 	$last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_tags};
-	my @s = sort keys %{$ctx->{wcr}->{snapshots}};
-	my $n;
-	if (@s) {
-		$n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
-		$n =~ s!\.git/*\z!!;
-		($n) = ($n =~ m!([^/]+)/*\z!);
-		$n = ascii_html($n).'-';
-	}
-	for (@r) {
-		my (undef, $oid, $ref, $s, $cd) = split(/\0/);
-		chomp $cd;
-		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
-		print $zfh "<a\nhref=./$oid/s/>", ascii_html($ref),
-			"</a>$align ", ascii_html($s), " ($cd)";
-		if (@s) {
-			my $v = $ref;
-			$v =~ s/\A[vV]//;
-			print $zfh "\t",  join(' ', map {
-				qq{<a href="snapshot/$n$v.$_">$_</a>} } @s);
-		}
-		print $zfh "\n";
-	}
-	print $zfh "# no tags yet...\n" if !@r;
-	print $zfh "...\n" if $last;
+	my ($snap_pfx, @snap_fmt) = _snapshot_link_prep($ctx);
+	chomp @r;
+	for (@r) { print $zfh _refs_tags_link($_, './', $snap_pfx, @snap_fmt) }
+	print $zfh $NO_TAGS if !@r;
+	print $zfh qq(<a href="refs/tags/">...</a>\n) if $last;
 	$wcb->($ctx->html_done('</pre>'));
 }
 
@@ -213,6 +230,59 @@ sub summary {
 	}
 }
 
+# called by GzipFilter->close after translate
+sub zflush { $_[0]->SUPER::zflush('</pre>', $_[0]->_html_end) }
+
+# called by GzipFilter->write or GetlineBody->getline
+sub translate {
+	my $ctx = shift;
+	my $rec = $_[0] // return zflush($ctx); # getline
+	my @out;
+	my $fbuf = delete($ctx->{fbuf}) // shift;
+	$fbuf .= shift while @_;
+	if ($ctx->{-heads}) {
+		while ($fbuf =~ s/\A([^\n]+)\n//s) {
+			utf8::decode(my $x = $1);
+			push @out, _refs_heads_link($x, '../../');
+		}
+	} else {
+		my ($snap_pfx, @snap_fmt) = _snapshot_link_prep($ctx);
+		while ($fbuf =~ s/\A([^\n]+)\n//s) {
+			utf8::decode(my $x = $1);
+			push @out, _refs_tags_link($x, '../../',
+						$snap_pfx, @snap_fmt);
+		}
+	}
+	$ctx->{fbuf} = $fbuf;
+	$ctx->SUPER::translate(@out);
+}
+
+sub _refs_parse_hdr { # {parse_hdr} for Qspawn
+	my ($r, $bref, $ctx) = @_;
+	my ($code, $top);
+	if ($r == 0) {
+		$code = 404;
+		$top = $ctx->{-heads} ? $NO_HEADS : $NO_TAGS;
+	} else {
+		$code = 200;
+		$top = $ctx->{-heads} ? $HEADS_CMD : $TAGS_CMD;
+	}
+	PublicInbox::WwwStream::html_init($ctx);
+	bless $ctx, __PACKAGE__; # re-bless for ->translate
+	print { $ctx->{zfh} } '<pre>', $top;
+	[ $code, delete($ctx->{-res_hdr}), $ctx ]; # [2] is qspawn.filter
+}
+
+sub refs_foo { # /$REPO/refs/{heads,tags} endpoints
+	my ($self, $ctx, $pfx) = @_;
+	$ctx->{wcr} = $self;
+	$ctx->{-upfx} = '../../';
+	$ctx->{-heads} = 1 if $pfx eq 'refs/heads';
+	my $qsp = PublicInbox::Qspawn->new([@EACH_REF, $pfx ],
+					{ GIT_DIR => $ctx->{git}->{git_dir} });
+	$qsp->psgi_return($ctx->{env}, undef, \&_refs_parse_hdr, $ctx);
+}
+
 sub srv { # endpoint called by PublicInbox::WWW
 	my ($self, $ctx) = @_;
 	my $path_info = $ctx->{env}->{PATH_INFO};
@@ -242,6 +312,9 @@ sub srv { # endpoint called by PublicInbox::WWW
 	} elsif ($path_info =~ m!\A/(.+?)/tags\.atom\z! and
 			($ctx->{git} = $cr->{$1})) {
 		PublicInbox::RepoAtom::srv_tags_atom($ctx);
+	} elsif ($path_info =~ m!\A/(.+?)/(refs/(?:heads|tags))/\z! and
+			($ctx->{git} = $cr->{$1})) {
+		refs_foo($self, $ctx, $2);
 	} elsif ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
 		my $qs = $ctx->{env}->{QUERY_STRING};
 		my $url = $git->base_url($ctx->{env});

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 7/7] www_coderepo: summary: fix mis-linkification of `...'
  2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
                   ` (5 preceding siblings ...)
  2023-01-28 11:02 ` [PATCH 6/7] www_coderepo: support $REPO/refs/{heads,tags}/ endpoints Eric Wong
@ 2023-01-28 11:02 ` Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2023-01-28 11:02 UTC (permalink / raw)
  To: meta

We need to use the ternary operator in assignments to clobber
previous values of `$last'.
---
 lib/PublicInbox/WwwCoderepo.pm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm
index d28b1a03..7a2cb80b 100644
--- a/lib/PublicInbox/WwwCoderepo.pm
+++ b/lib/PublicInbox/WwwCoderepo.pm
@@ -129,7 +129,7 @@ sub summary_finish {
 
 	# git log
 	my @r = split(/\n/s, pop(@x) // '');
-	my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
+	my $last = scalar(@r) > $ctx->{wcr}->{summary_log} ? pop(@r) : undef;
 	my $tip_html = '';
 	if (defined(my $tip = $ctx->{qp}->{h})) {
 		$tip_html .= ' '.ascii_html($tip).' --';
@@ -165,14 +165,14 @@ EOM
 	# refs/heads
 	print $zfh '<a id=heads>', $HEADS_CMD , '</a>';
 	@r = split(/^/sm, shift(@x) // '');
-	$last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_branches};
+	$last = scalar(@r) > $ctx->{wcr}->{summary_branches} ? pop(@r) : undef;
 	chomp(@r);
 	for (@r) { print $zfh _refs_heads_link($_, './') }
 	print $zfh $NO_HEADS if !@r;
 	print $zfh qq(<a href="refs/heads/">...</a>\n) if $last;
 	print $zfh "\n<a id=tags>", $TAGS_CMD, '</a>';
 	@r = split(/^/sm, shift(@x) // '');
-	$last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_tags};
+	$last = scalar(@r) > $ctx->{wcr}->{summary_tags} ? pop(@r) : undef;
 	my ($snap_pfx, @snap_fmt) = _snapshot_link_prep($ctx);
 	chomp @r;
 	for (@r) { print $zfh _refs_tags_link($_, './', $snap_pfx, @snap_fmt) }

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-01-28 11:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-28 11:02 [PATCH 0/7] www_coderepo updates Eric Wong
2023-01-28 11:02 ` [PATCH 1/7] www_coderepo: tree: quiet and 404 on non-existent refs Eric Wong
2023-01-28 11:02 ` [PATCH 2/7] www_coderepo: support /$REPO/tags.atom endpoint Eric Wong
2023-01-28 11:02 ` [PATCH 3/7] www_coderepo: fix snapshot link generation Eric Wong
2023-01-28 11:02 ` [PATCH 4/7] www_coderepo: reduce utf8::decode calls Eric Wong
2023-01-28 11:02 ` [PATCH 5/7] repo_atom: translate: account for multiple args Eric Wong
2023-01-28 11:02 ` [PATCH 6/7] www_coderepo: support $REPO/refs/{heads,tags}/ endpoints Eric Wong
2023-01-28 11:02 ` [PATCH 7/7] www_coderepo: summary: fix mis-linkification of `...' Eric Wong

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).