1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Standalone code repository viewer for users w/o cgit
5 package PublicInbox::WwwCoderepo;
7 use File::Temp 0.19 (); # newdir
8 use PublicInbox::ViewVCS;
9 use PublicInbox::WwwStatic qw(r);
10 use PublicInbox::GitHTTPBackend;
12 use PublicInbox::GitAsyncCat;
13 use PublicInbox::WwwStream;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::RepoSnapshot;
17 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
18 join('%00', map { "%($_)" }
19 qw(objectname refname:short subject creatordate:short))."'";
21 # shared with PublicInbox::Cgit
22 sub prepare_coderepos {
24 my $pi_cfg = $self->{pi_cfg};
26 # TODO: support gitweb and other repository viewers?
27 defined($pi_cfg->{'publicinbox.cgitrc'}) and
28 $pi_cfg->parse_cgitrc(undef, 0);
30 my $code_repos = $pi_cfg->{-code_repos};
31 for my $k (grep(/\Acoderepo\.(?:.+)\.dir\z/, keys %$pi_cfg)) {
32 $k = substr($k, length('coderepo.'), -length('.dir'));
33 $code_repos->{$k} //= $pi_cfg->fill_code_repo($k);
35 while (my ($nick, $repo) = each %$code_repos) {
36 $self->{"\0$nick"} = $repo;
41 my ($cls, $pi_cfg) = @_;
42 my $self = bless { pi_cfg => $pi_cfg }, $cls;
43 prepare_coderepos($self);
44 $self->{snapshots} = do {
45 my $s = $pi_cfg->{'coderepo.snapshots'} // '';
46 $s eq 'all' ? \%PublicInbox::RepoSnapshot::FMT_TYPES :
47 +{ map { $_ => 1 } split(/\s+/, $s) };
49 $self->{$_} = 10 for qw(summary_branches summary_tags);
50 $self->{$_} = 10 for qw(summary_log);
56 my $wcb = delete($ctx->{env}->{'qspawn.wcb'}) or return; # already done
57 my @x = split(/\n\n/sm, delete($ctx->{-each_refs}));
58 PublicInbox::WwwStream::html_init($ctx);
62 my @r = split(/\n/s, pop(@x) // '');
63 my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
67 href='#readme'>about</a> <a
68 href='#heads'>heads</a> <a
72 id=log>\$</a> git log --pretty=format:'%h %s (%cs)%d'
76 s/^ \(([^\)]+)\)// and $d = $1;
78 my ($H, $h, $cs, $s) = split(/ /, $_, 4);
79 print $zfh "<a\nhref=./$H/s/>$h</a> ", ascii_html($s),
81 print $zfh "\t(", ascii_html($d), ")\n" if $d;
83 print $zfh "# no commits, yet\n" if !@r;
84 print $zfh "...\n" if $last;
87 my ($bref, $oid, $ref_path) = @{delete $ctx->{-readme}};
89 my $l = PublicInbox::Linkify->new;
90 $$bref =~ s/\s*\z//sm;
91 print $zfh "\n<a id=readme>\$</a> " .
92 "git cat-file blob <a href=./$oid/s/>",
93 ascii_html($ref_path), "</a>\n",
94 $l->to_html($$bref), '</pre><hr><pre>';
98 print $zfh "<a id=heads># heads (aka `branches'):</a>\n\$ " .
99 "git for-each-ref --sort=-creatordate refs/heads" .
100 " \\\n\t--format='%(HEAD) ". # no space for %(align:) hint
101 "%(refname:short) %(subject) (%(creatordate:short))'\n";
102 @r = split(/^/sm, shift(@x) // '');
103 $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_branches};
105 my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
106 utf8::decode($_) for ($ref, $s);
108 my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
109 print $zfh "$pfx <a\nhref=./$oid/s/>", ascii_html($ref),
110 "</a>$align ", ascii_html($s), " ($cd)\n";
112 print $zfh "# no heads (branches) yet...\n" if !@r;
113 print $zfh "...\n" if $last;
114 print $zfh "\n<a id=tags># tags:</a>\n\$ " .
115 "git for-each-ref --sort=-creatordate refs/tags" .
116 " \\\n\t--format='". # no space for %(align:) hint
117 "%(refname:short) %(subject) (%(creatordate:short))'\n";
118 @r = split(/^/sm, shift(@x) // '');
119 $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_tags};
120 my @s = sort keys %{$ctx->{wcr}->{snapshots}};
123 $n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
125 ($n) = ($n =~ m!([^/]+)\z!);
129 my (undef, $oid, $ref, $s, $cd) = split(/\0/);
130 utf8::decode($_) for ($ref, $s);
132 my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
133 print $zfh "<a\nhref=./$oid/s/>", ascii_html($ref),
134 "</a>$align ", ascii_html($s), " ($cd)";
138 print $zfh "\t", join(' ', map {
139 qq{<a href="snapshot/$n$v.$_">$_</a>} } @s);
143 print $zfh "# no tags yet...\n" if !@r;
144 print $zfh "...\n" if $last;
145 $wcb->($ctx->html_done('</pre>'));
148 sub capture_refs ($$) { # psgi_qx callback to capture git-for-each-ref + git-log
149 my ($bref, $ctx) = @_;
150 my $qsp_err = delete $ctx->{-qsp_err};
151 $ctx->{-each_refs} = $$bref;
152 summary_finish($ctx) if $ctx->{-readme};
155 sub set_readme { # git->cat_async callback
156 my ($bref, $oid, $type, $size, $ctx) = @_;
157 my $ref_path = shift @{$ctx->{-nr_readme_tries}}; # e.g. HEAD:README
158 if ($type eq 'blob' && !$ctx->{-readme}) {
159 $ctx->{-readme} = [ $bref, $oid, $ref_path ];
160 } elsif (scalar @{$ctx->{-nr_readme_tries}} == 0) {
161 $ctx->{-readme} //= []; # nothing left to try
162 } # or try another README...
163 summary_finish($ctx) if $ctx->{-each_refs} && $ctx->{-readme};
167 my ($self, $ctx) = @_;
169 my $nb = $self->{summary_branches} + 1;
170 my $nt = $self->{summary_tags} + 1;
171 my $nl = $self->{summary_log} + 1;
172 my $qsp = PublicInbox::Qspawn->new([qw(/bin/sh -c),
173 "$EACH_REF --count=$nb refs/heads; echo && " .
174 "$EACH_REF --count=$nt refs/tags; echo && " .
175 "git log -$nl --pretty=format:'%d %H %h %cs %s' --" ],
176 { GIT_DIR => $ctx->{git}->{git_dir} });
177 $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
178 my @try = qw(HEAD:README HEAD:README.md); # TODO: configurable
179 $ctx->{-nr_readme_tries} = [ @try ];
180 $ctx->{git}->cat_async($_, \&set_readme, $ctx) for @try;
181 if ($ctx->{env}->{'pi-httpd.async'}) {
182 PublicInbox::GitAsyncCat::watch_cat($ctx->{git});
183 } else { # synchronous
184 $ctx->{git}->cat_async_wait;
186 sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked}
187 $ctx->{env}->{'qspawn.wcb'} = $_[0];
188 $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx);
192 sub srv { # endpoint called by PublicInbox::WWW
193 my ($self, $ctx) = @_;
194 my $path_info = $ctx->{env}->{PATH_INFO};
196 # handle clone requests
197 if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x) {
198 $git = $self->{"\0$1"} and return
199 PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
201 $path_info =~ m!\A/(.+?)/\z! and
202 ($ctx->{git} = $self->{"\0$1"}) and return summary($self, $ctx);
203 $path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/\z! and
204 ($ctx->{git} = $self->{"\0$1"}) and
205 return PublicInbox::ViewVCS::show($ctx, $2);
208 if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
209 ($ctx->{git} = $self->{"\0$1"})) {
211 return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
214 # enforce trailing slash:
215 if ($path_info =~ m!\A/(.+?)\z! and ($git = $self->{"\0$1"})) {
216 my $qs = $ctx->{env}->{QUERY_STRING};
217 my $url = $git->base_url($ctx->{env});
218 $url .= "?$qs" if $qs ne '';
219 [ 301, [ Location => $url, 'Content-Type' => 'text/plain' ],
220 [ "Redirecting to $url\n" ] ];