\$ git log --pretty=format:'%h %s (%cs)%d'$tip_html
EOM
for (@r) {
my $d; # decorations
s/^ \(([^\)]+)\)// and $d = $1;
substr($_, 0, 1, '');
my ($H, $h, $cs, $s) = split(/ /, $_, 4);
print $zfh "$h ", ascii_html($s),
" (", $cs, ")\n";
print $zfh "\t(", ascii_html($d), ")\n" if $d;
}
print $zfh '# no commits in `', ($tip//'HEAD'),"', yet\n\n" if !@r;
print $zfh "...\n" if $last;
# README
my ($bref, $oid, $ref_path) = @{delete $ctx->{qx_res}->{readme}};
if ($bref) {
my $l = PublicInbox::Linkify->new;
$$bref =~ s/\s*\z//sm;
my (undef, $path) = split(/:/, $ref_path, 2); # HEAD:README
print $zfh "\n\$ " .
qq(git cat-file blob ).
ascii_html($ref_path), "\n",
$l->to_html($$bref), '
';
}
# refs/heads
print $zfh '', $HEADS_CMD , '';
@r = split(/^/sm, delete($ctx->{qx_res}->{heads}) // '');
$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, delete($ctx->{qx_res}->{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) }
print $zfh $NO_TAGS if !@r;
print $zfh qq(...\n) if $last;
emit_joined_inboxes $ctx;
$wcb->($ctx->html_done('
'));
}
sub capture { # psgi_qx callback to capture git-for-each-ref
my ($bref, $ctx, $key) = @_; # $_[3] = OnDestroy(summary_END)
$ctx->{qx_res}->{$key} = $$bref;
# summary_END may be called via OnDestroy $arg->[2]
}
sub set_readme { # git->cat_async callback
my ($bref, $oid, $type, $size, $ctx) = @_;
my $ref_path = shift @{$ctx->{-readme_tries}}; # e.g. HEAD:README
if ($type eq 'blob' && !$ctx->{qx_res}->{readme}) {
$ctx->{qx_res}->{readme} = [ $bref, $oid, $ref_path ];
} elsif (scalar @{$ctx->{-readme_tries}} == 0) {
$ctx->{qx_res}->{readme} //= []; # nothing left to try
} # or try another README...
# summary_END may be called via OnDestroy ($ctx->{-END})
}
sub summary ($$) {
my ($ctx, $wcb) = @_;
$ctx->{-wcb} = $wcb; # PublicInbox::HTTP::{Identity,Chunked}
my $tip = $ctx->{qp}->{h}; # same as cgit
if (defined $tip && $tip eq '') {
delete $ctx->{qp}->{h};
undef($tip);
}
my ($nb, $nt, $nl) = map { $_ + 1 } @{$ctx->{wcr}}{qw(
summary_branches summary_tags summary_log)};
$ctx->{qx_res} = {};
my $qsp_err = \($ctx->{-qsp_err} = '');
my %opt = (quiet => 1, 2 => $ctx->{wcr}->{log_fh});
my %env = (GIT_DIR => $ctx->{git}->{git_dir});
my @log = (qw(git log), "-$nl", '--pretty=format:%d %H %h %cs %s');
push(@log, $tip) if defined $tip;
# limit scope for MockHTTP test (t/solver_git.t)
my $END = on_destroy \&summary_END, $ctx;
for (['log', \@log],
[ 'heads', [@EACH_REF, "--count=$nb", 'refs/heads'] ],
[ 'tags', [@EACH_REF, "--count=$nt", 'refs/tags'] ]) {
my ($k, $cmd) = @$_;
my $qsp = PublicInbox::Qspawn->new($cmd, \%env, \%opt);
$qsp->{qsp_err} = $qsp_err;
$qsp->psgi_qx($ctx->{env}, undef, \&capture, $ctx, $k, $END);
}
$tip //= 'HEAD';
my @try = ("$tip:README", "$tip:README.md"); # TODO: configurable
my %ctx = (%$ctx, -END => $END, -readme_tries => [ @try ]);
PublicInbox::ViewVCS::do_cat_async(\%ctx, \&set_readme, @try);
}
# called by GzipFilter->close after translate
sub zflush { $_[0]->SUPER::zflush('', $_[0]->_html_end) }
# called by GzipFilter->write or GetlineResponse->getline
sub translate {
my $ctx = shift;
$_[0] // return zflush($ctx); # getline caller
my @out;
my $fbuf = delete($ctx->{fbuf}) // shift;
$fbuf .= shift while @_;
if ($ctx->{-heads}) {
while ($fbuf =~ s/\A([^\n]+)\n//s) {
utf8_maybe(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_maybe(my $x = $1);
push @out, _refs_tags_link($x, '../../',
$snap_pfx, @snap_fmt);
}
}
$ctx->{fbuf} = $fbuf; # may be incomplete
@out ? $ctx->SUPER::translate(@out) : ''; # not EOF, yet
}
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_yield($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 $pi_cfg = $self->{pi_cfg};
if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x and
($git = $pi_cfg->get_coderepo($1))) {
PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
} elsif ($path_info =~ m!\A/(.+?)/\z! and
($ctx->{git} = $pi_cfg->get_coderepo($1))) {
$ctx->{wcr} = $self;
sub { summary($ctx, $_[0]) }; # $_[0] = wcb
} elsif ($path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/([^/]+)?\z! and
($ctx->{git} = $pi_cfg->get_coderepo($1))) {
$ctx->{lh} = $self->{log_fh};
PublicInbox::ViewVCS::show($ctx, $2, $3);
} elsif ($path_info =~ m!\A/(.+?)/tree/(.*)\z! and
($ctx->{git} = $pi_cfg->get_coderepo($1))) {
$ctx->{lh} = $self->{log_fh};
PublicInbox::RepoTree::srv_tree($ctx, $2) // r(404);
} elsif ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
($ctx->{git} = $pi_cfg->get_coderepo($1))) {
$ctx->{wcr} = $self;
PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
} elsif ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
($ctx->{git} = $pi_cfg->get_coderepo($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} = $pi_cfg->get_coderepo($1))) {
PublicInbox::RepoAtom::srv_tags_atom($ctx);
} elsif ($path_info =~ m!\A/(.+?)/(refs/(?:heads|tags))/\z! and
($ctx->{git} = $pi_cfg->get_coderepo($1))) {
refs_foo($self, $ctx, $2);
} elsif ($path_info =~ m!\A/(.*?\*.*?)/*\z!) {
my $re = PublicInbox::Config::glob2re($1);
PublicInbox::RepoList::html($self, $ctx, qr!$re\z!) // r(404);
} elsif ($path_info =~ m!\A/(.+?)/\z!) {
my $re = qr!\A\Q$1\E/!;
PublicInbox::RepoList::html($self, $ctx, $re) // r(404);
} elsif ($path_info =~ m!\A/(.+?)\z! and
($git = $pi_cfg->get_coderepo($1))) {
my $qs = $ctx->{env}->{QUERY_STRING};
my $url = $git->base_url($ctx->{env});
$url .= "?$qs" if $qs ne '';
[ 301, [ Location => $url, 'Content-Type' => 'text/plain' ],
[ "Redirecting to $url\n" ] ];
} else {
r(404);
}
}
1;