X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWwwCoderepo.pm;h=52ab6e4800b3930af46991f6e5059ecfcb6bc4b9;hb=HEAD;hp=7f8b34597b62a5d4069a6fecdf483da35483a43e;hpb=1802dc29bda25a5478787d3d7a8a3c2de6cc6797;p=public-inbox.git diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm index 7f8b3459..52ab6e48 100644 --- a/lib/PublicInbox/WwwCoderepo.pm +++ b/lib/PublicInbox/WwwCoderepo.pm @@ -7,20 +7,35 @@ # 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; use PublicInbox::WwwStatic qw(r); use PublicInbox::GitHTTPBackend; -use PublicInbox::Git; -use PublicInbox::GitAsyncCat; use PublicInbox::WwwStream; 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 { "%($_)" } - 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 { @@ -41,13 +56,11 @@ sub prepare_coderepos { $k = substr($k, length('publicinbox.'), -length('.coderepo')); my $ibx = $pi_cfg->lookup_name($k) // next; $pi_cfg->repo_objs($ibx); - push @{$self->{-strong}}, $ibx; # strengthen {-ibxs} weakref } for my $k (grep(/\Aextindex\.(?:.+)\.coderepo\z/, keys %$pi_cfg)) { $k = substr($k, length('extindex.'), -length('.coderepo')); my $eidx = $pi_cfg->lookup_ei($k) // next; $pi_cfg->repo_objs($eidx); - push @{$self->{-strong}}, $eidx; # strengthen {-ibxs} weakref } } @@ -62,28 +75,66 @@ sub new { }; $self->{$_} = 10 for qw(summary_branches summary_tags); $self->{$_} = 10 for qw(summary_log); + + # try reuse STDIN if it's already /dev/null + open $self->{log_fh}, '+>', '/dev/null' or die "open: $!"; + my @l = stat($self->{log_fh}) or die "stat: $!"; + my @s = stat(STDIN) or die "stat(STDIN): $!"; + if ("@l[0, 1]" eq "@s[0, 1]") { + my $f = fcntl(STDIN, F_GETFL, 0) // die "F_GETFL: $!"; + $self->{log_fh} = *STDIN{IO} if $f & O_RDWR; + } $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 ", ascii_html($ref), + "$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{ $_} + } @snap_fmt; + substr($snap_fmt[0], 0, 1) = "\t"; + } + ("", ascii_html($ref), + "$align ", ascii_html($s), " ($cd)", @snap_fmt, "\n"); +} + sub summary_finish { my ($ctx) = @_; my $wcb = delete($ctx->{env}->{'qspawn.wcb'}) or return; # already done - my @x = split(/\n\n/sm, delete($ctx->{-each_refs})); + my @x = split(/\n\n/sm, delete($ctx->{-each_refs}), 3); PublicInbox::WwwStream::html_init($ctx); my $zfh = $ctx->zfh; # git log - my @r = split(/\n/s, pop(@x) // ''); - my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log}; + my @r = split(/\n/s, pop(@x)); + my $last = scalar(@r) > $ctx->{wcr}->{summary_log} ? pop(@r) : undef; + my $tip_html = ''; + my $tip = $ctx->{qp}->{h}; + $tip_html .= ' '.ascii_html($tip).' --' if defined $tip; print $zfh < -about heads tags - -\$ git log --pretty=format:'%h %s (%cs)%d' +
\$ git log --pretty=format:'%h %s (%cs)%d'$tip_html
 EOM
 	for (@r) {
 		my $d; # decorations
@@ -94,7 +145,7 @@ EOM
 			" (", $cs, ")\n";
 		print $zfh "\t(", ascii_html($d), ")\n" if $d;
 	}
-	print $zfh "# no commits, yet\n" if !@r;
+	print $zfh '# no commits in `', ($tip//'HEAD'),"', yet\n\n" if !@r;
 	print $zfh "...\n" if $last;
 
 	# README
@@ -102,66 +153,37 @@ EOM
 	if ($bref) {
 		my $l = PublicInbox::Linkify->new;
 		$$bref =~ s/\s*\z//sm;
+		my (undef, $path) = split(/:/, $ref_path, 2); # HEAD:README
 		print $zfh "\n\$ " .
-			"git cat-file blob ",
+			qq(git cat-file blob ).
 			ascii_html($ref_path), "\n",
 			$l->to_html($$bref), '

';
 	}
 
 	# refs/heads
-	print $zfh "# heads (aka `branches'):\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 '', $HEADS_CMD , '';
 	@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/);
-		utf8::decode($_) for ($ref, $s);
-		chomp $cd;
-		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
-		print $zfh "$pfx ", ascii_html($ref),
-			"$align ", ascii_html($s), " ($cd)\n";
-	}
-	print $zfh "# no heads (branches) yet...\n" if !@r;
-	print $zfh "...\n" if $last;
-	print $zfh "\n# tags:\n\$ " .
-		"git for-each-ref --sort=-creatordate refs/tags" .
-		" \\\n\t--format='". # no space for %(align:) hint
-		"%(refname:short) %(subject) (%(creatordate:short))'\n";
+	$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(...\n) if $last;
+	print $zfh "\n", $TAGS_CMD, '';
 	@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/);
-		utf8::decode($_) for ($ref, $s);
-		chomp $cd;
-		my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
-		print $zfh "", ascii_html($ref),
-			"$align ", ascii_html($s), " ($cd)";
-		if (@s) {
-			my $v = $ref;
-			$v =~ s/\A[vV]//;
-			print $zfh "\t",  join(' ', map {
-				qq{$_} } @s);
-		}
-		print $zfh "\n";
-	}
-	print $zfh "# no tags yet...\n" if !@r;
-	print $zfh "...\n" if $last;
+	$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) }
+	print $zfh $NO_TAGS if !@r;
+	print $zfh qq(...\n) if $last;
 	$wcb->($ctx->html_done('
')); } 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}; } @@ -180,59 +202,121 @@ sub set_readme { # git->cat_async callback sub summary { my ($self, $ctx) = @_; $ctx->{wcr} = $self; + my $tip = $ctx->{qp}->{h}; # same as cgit + if (defined $tip && $tip eq '') { + delete $ctx->{qp}->{h}; + undef($tip); + } my $nb = $self->{summary_branches} + 1; my $nt = $self->{summary_tags} + 1; my $nl = $self->{summary_log} + 1; - my $qsp = PublicInbox::Qspawn->new([qw(/bin/sh -c), + + my @cmd = (qw(/bin/sh -c), "$EACH_REF --count=$nb refs/heads; echo && " . "$EACH_REF --count=$nt refs/tags; echo && " . - "git log -$nl --pretty=format:'%d %H %h %cs %s' --" ], - { GIT_DIR => $ctx->{git}->{git_dir} }); + qq(git log -$nl --pretty=format:'%d %H %h %cs %s' "\$@" --)); + push @cmd, 'git', $tip if defined($tip); + my $qsp = PublicInbox::Qspawn->new(\@cmd, + { GIT_DIR => $ctx->{git}->{git_dir} }, + { quiet => 1, 2 => $self->{log_fh} }); $qsp->{qsp_err} = \($ctx->{-qsp_err} = ''); - my @try = qw(HEAD:README HEAD:README.md); # TODO: configurable + $tip //= 'HEAD'; + my @try = ("$tip:README", "$tip:README.md"); # TODO: configurable $ctx->{-nr_readme_tries} = [ @try ]; - $ctx->{git}->cat_async($_, \&set_readme, $ctx) for @try; - if ($ctx->{env}->{'pi-httpd.async'}) { - PublicInbox::GitAsyncCat::watch_cat($ctx->{git}); - } else { # synchronous - $ctx->{git}->cat_async_wait; - } + PublicInbox::ViewVCS::do_cat_async($ctx, \&set_readme, @try); sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked} $ctx->{env}->{'qspawn.wcb'} = $_[0]; $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx); } } +# called by GzipFilter->close after translate +sub zflush { $_[0]->SUPER::zflush('', $_[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} } '
', $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};
 	my $git;
 	# handle clone requests
 	my $cr = $self->{pi_cfg}->{-code_repos};
-	if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x) {
-		$git = $cr->{$1} and return
+	if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x and
+		($git = $cr->{$1})) {
 			PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
-	}
-	$path_info =~ m!\A/(.+?)/\z! and
-		($ctx->{git} = $cr->{$1}) and return summary($self, $ctx);
-	$path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/\z! and
-			($ctx->{git} = $cr->{$1}) and
-		return PublicInbox::ViewVCS::show($ctx, $2);
-
-	# snapshots:
-	if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
+	} elsif ($path_info =~ m!\A/(.+?)/\z! and ($ctx->{git} = $cr->{$1})) {
+		summary($self, $ctx)
+	} elsif ($path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/([^/]+)?\z! and
+			($ctx->{git} = $cr->{$1})) {
+		$ctx->{lh} = $self->{log_fh};
+		PublicInbox::ViewVCS::show($ctx, $2, $3);
+	} elsif ($path_info =~ m!\A/(.+?)/tree/(.*)\z! and
+			($ctx->{git} = $cr->{$1})) {
+		$ctx->{lh} = $self->{log_fh};
+		PublicInbox::RepoTree::srv_tree($ctx, $2) // r(404);
+	} elsif ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
 			($ctx->{git} = $cr->{$1})) {
 		$ctx->{wcr} = $self;
-		return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
-	}
-
-	if ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
+		PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
+	} elsif ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
 			($ctx->{git} = $cr->{$1})) {
-		return PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
-	}
-
-	# enforce trailing slash:
-	if ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
+		$ctx->{lh} = $self->{log_fh};
+		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/(.+?)/(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});
 		$url .= "?$qs" if $qs ne '';