]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwCoderepo.pm
config: use inbox names to map inboxes <-> coderepos
[public-inbox.git] / lib / PublicInbox / WwwCoderepo.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Standalone code repository viewer for users w/o cgit.
5 # This isn't intended to replicate all of cgit, but merely to be a
6 # "good enough" viewer with search support and some UI hints to encourage
7 # cloning + command-line usage.
8 package PublicInbox::WwwCoderepo;
9 use v5.12;
10 use File::Temp 0.19 (); # newdir
11 use PublicInbox::ViewVCS;
12 use PublicInbox::WwwStatic qw(r);
13 use PublicInbox::GitHTTPBackend;
14 use PublicInbox::Git;
15 use PublicInbox::GitAsyncCat;
16 use PublicInbox::WwwStream;
17 use PublicInbox::Hval qw(ascii_html);
18 use PublicInbox::ViewDiff qw(uri_escape_path);
19 use PublicInbox::RepoSnapshot;
20 use PublicInbox::RepoAtom;
21
22 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
23         join('%00', map { "%($_)" }
24         qw(objectname refname:short subject creatordate:short))."'";
25
26 # shared with PublicInbox::Cgit
27 sub prepare_coderepos {
28         my ($self) = @_;
29         my $pi_cfg = $self->{pi_cfg};
30
31         # TODO: support gitweb and other repository viewers?
32         $pi_cfg->parse_cgitrc(undef, 0);
33
34         my $code_repos = $pi_cfg->{-code_repos};
35         for my $k (grep(/\Acoderepo\.(?:.+)\.dir\z/, keys %$pi_cfg)) {
36                 $k = substr($k, length('coderepo.'), -length('.dir'));
37                 $code_repos->{$k} //= $pi_cfg->fill_code_repo($k);
38         }
39
40         # associate inboxes and extindices with coderepos for search:
41         for my $k (grep(/\Apublicinbox\.(?:.+)\.coderepo\z/, keys %$pi_cfg)) {
42                 $k = substr($k, length('publicinbox.'), -length('.coderepo'));
43                 my $ibx = $pi_cfg->lookup_name($k) // next;
44                 $pi_cfg->repo_objs($ibx);
45         }
46         for my $k (grep(/\Aextindex\.(?:.+)\.coderepo\z/, keys %$pi_cfg)) {
47                 $k = substr($k, length('extindex.'), -length('.coderepo'));
48                 my $eidx = $pi_cfg->lookup_ei($k) // next;
49                 $pi_cfg->repo_objs($eidx);
50         }
51 }
52
53 sub new {
54         my ($cls, $pi_cfg) = @_;
55         my $self = bless { pi_cfg => $pi_cfg }, $cls;
56         prepare_coderepos($self);
57         $self->{snapshots} = do {
58                 my $s = $pi_cfg->{'coderepo.snapshots'} // '';
59                 $s eq 'all' ? \%PublicInbox::RepoSnapshot::FMT_TYPES :
60                         +{ map { $_ => 1 } split(/\s+/, $s) };
61         };
62         $self->{$_} = 10 for qw(summary_branches summary_tags);
63         $self->{$_} = 10 for qw(summary_log);
64         $self;
65 }
66
67 sub summary_finish {
68         my ($ctx) = @_;
69         my $wcb = delete($ctx->{env}->{'qspawn.wcb'}) or return; # already done
70         my @x = split(/\n\n/sm, delete($ctx->{-each_refs}));
71         PublicInbox::WwwStream::html_init($ctx);
72         my $zfh = $ctx->zfh;
73
74         # git log
75         my @r = split(/\n/s, pop(@x) // '');
76         my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
77         my $tip_html = '';
78         if (defined(my $tip = $ctx->{qp}->{h})) {
79                 $tip_html .= ' '.ascii_html($tip).' --';
80         }
81         print $zfh <<EOM;
82 <pre>
83 <a
84 href='#readme'>about</a> <a
85 href='#heads'>heads</a> <a
86 href='#tags'>tags</a>
87
88 <a
89 id=log>\$</a> git log --pretty=format:'%h %s (%cs)%d'$tip_html
90 EOM
91         for (@r) {
92                 my $d; # decorations
93                 s/^ \(([^\)]+)\)// and $d = $1;
94                 substr($_, 0, 1, '');
95                 my ($H, $h, $cs, $s) = split(/ /, $_, 4);
96                 print $zfh "<a\nhref=./$H/s/>$h</a> ", ascii_html($s),
97                         " (", $cs, ")\n";
98                 print $zfh "\t(", ascii_html($d), ")\n" if $d;
99         }
100         print $zfh "# no commits, yet\n" if !@r;
101         print $zfh "...\n" if $last;
102
103         # README
104         my ($bref, $oid, $ref_path) = @{delete $ctx->{-readme}};
105         if ($bref) {
106                 my $l = PublicInbox::Linkify->new;
107                 $$bref =~ s/\s*\z//sm;
108                 my (undef, $path) = split(/:/, $ref_path, 2); # HEAD:README
109                 print $zfh "\n<a id=readme>\$</a> " .
110                         qq(git cat-file blob <a href="./$oid/s/?b=) .
111                         ascii_html(uri_escape_path($path)) . q(">).
112                         ascii_html($ref_path), "</a>\n",
113                         $l->to_html($$bref), '</pre><hr><pre>';
114         }
115
116         # refs/heads
117         print $zfh "<a id=heads># heads (aka `branches'):</a>\n\$ " .
118                 "git for-each-ref --sort=-creatordate refs/heads" .
119                 " \\\n\t--format='%(HEAD) ". # no space for %(align:) hint
120                 "%(refname:short) %(subject) (%(creatordate:short))'\n";
121         @r = split(/^/sm, shift(@x) // '');
122         $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_branches};
123         for (@r) {
124                 my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
125                 utf8::decode($_) for ($ref, $s);
126                 chomp $cd;
127                 my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
128                 print $zfh "$pfx <a\nhref=./$oid/s/>", ascii_html($ref),
129                         "</a>$align ", ascii_html($s), " ($cd)\n";
130         }
131         print $zfh "# no heads (branches) yet...\n" if !@r;
132         print $zfh "...\n" if $last;
133         print $zfh "\n<a id=tags># tags:</a>\n\$ " .
134                 "git for-each-ref --sort=-creatordate refs/tags" .
135                 " \\\n\t--format='". # no space for %(align:) hint
136                 "%(refname:short) %(subject) (%(creatordate:short))'\n";
137         @r = split(/^/sm, shift(@x) // '');
138         $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_tags};
139         my @s = sort keys %{$ctx->{wcr}->{snapshots}};
140         my $n;
141         if (@s) {
142                 $n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
143                 $n =~ s/\.git\z/-/;
144                 ($n) = ($n =~ m!([^/]+)\z!);
145                 $n = ascii_html($n);
146         }
147         for (@r) {
148                 my (undef, $oid, $ref, $s, $cd) = split(/\0/);
149                 utf8::decode($_) for ($ref, $s);
150                 chomp $cd;
151                 my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
152                 print $zfh "<a\nhref=./$oid/s/>", ascii_html($ref),
153                         "</a>$align ", ascii_html($s), " ($cd)";
154                 if (@s) {
155                         my $v = $ref;
156                         $v =~ s/\A[vV]//;
157                         print $zfh "\t",  join(' ', map {
158                                 qq{<a href="snapshot/$n$v.$_">$_</a>} } @s);
159                 }
160                 print $zfh "\n";
161         }
162         print $zfh "# no tags yet...\n" if !@r;
163         print $zfh "...\n" if $last;
164         $wcb->($ctx->html_done('</pre>'));
165 }
166
167 sub capture_refs ($$) { # psgi_qx callback to capture git-for-each-ref + git-log
168         my ($bref, $ctx) = @_;
169         my $qsp_err = delete $ctx->{-qsp_err};
170         $ctx->{-each_refs} = $$bref;
171         summary_finish($ctx) if $ctx->{-readme};
172 }
173
174 sub set_readme { # git->cat_async callback
175         my ($bref, $oid, $type, $size, $ctx) = @_;
176         my $ref_path = shift @{$ctx->{-nr_readme_tries}}; # e.g. HEAD:README
177         if ($type eq 'blob' && !$ctx->{-readme}) {
178                 $ctx->{-readme} = [ $bref, $oid, $ref_path ];
179         } elsif (scalar @{$ctx->{-nr_readme_tries}} == 0) {
180                 $ctx->{-readme} //= []; # nothing left to try
181         } # or try another README...
182         summary_finish($ctx) if $ctx->{-each_refs} && $ctx->{-readme};
183 }
184
185 sub summary {
186         my ($self, $ctx) = @_;
187         $ctx->{wcr} = $self;
188         my $tip = $ctx->{qp}->{h}; # same as cgit
189         if (defined $tip && $tip eq '') {
190                 delete $ctx->{qp}->{h};
191                 undef($tip);
192         }
193         my $nb = $self->{summary_branches} + 1;
194         my $nt = $self->{summary_tags} + 1;
195         my $nl = $self->{summary_log} + 1;
196
197         my @cmd = (qw(/bin/sh -c),
198                 "$EACH_REF --count=$nb refs/heads; echo && " .
199                 "$EACH_REF --count=$nt refs/tags; echo && " .
200                 qq(git log -$nl --pretty=format:'%d %H %h %cs %s' "\$@" --));
201         push @cmd, '--', $tip if defined($tip);
202         my $qsp = PublicInbox::Qspawn->new(\@cmd,
203                 { GIT_DIR => $ctx->{git}->{git_dir} });
204         $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
205         $tip //= 'HEAD';
206         my @try = ("$tip:README", "$tip:README.md"); # TODO: configurable
207         $ctx->{-nr_readme_tries} = [ @try ];
208         $ctx->{git}->cat_async($_, \&set_readme, $ctx) for @try;
209         if ($ctx->{env}->{'pi-httpd.async'}) {
210                 PublicInbox::GitAsyncCat::watch_cat($ctx->{git});
211         } else { # synchronous
212                 $ctx->{git}->cat_async_wait;
213         }
214         sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked}
215                 $ctx->{env}->{'qspawn.wcb'} = $_[0];
216                 $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx);
217         }
218 }
219
220 sub srv { # endpoint called by PublicInbox::WWW
221         my ($self, $ctx) = @_;
222         my $path_info = $ctx->{env}->{PATH_INFO};
223         my $git;
224         # handle clone requests
225         my $cr = $self->{pi_cfg}->{-code_repos};
226         if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x) {
227                 $git = $cr->{$1} and return
228                         PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
229         }
230         $path_info =~ m!\A/(.+?)/\z! and
231                 ($ctx->{git} = $cr->{$1}) and return summary($self, $ctx);
232         $path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/\z! and
233                         ($ctx->{git} = $cr->{$1}) and
234                 return PublicInbox::ViewVCS::show($ctx, $2);
235
236         # snapshots:
237         if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
238                         ($ctx->{git} = $cr->{$1})) {
239                 $ctx->{wcr} = $self;
240                 return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
241         }
242
243         if ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
244                         ($ctx->{git} = $cr->{$1})) {
245                 return PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
246         }
247
248         # enforce trailing slash:
249         if ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
250                 my $qs = $ctx->{env}->{QUERY_STRING};
251                 my $url = $git->base_url($ctx->{env});
252                 $url .= "?$qs" if $qs ne '';
253                 [ 301, [ Location => $url, 'Content-Type' => 'text/plain' ],
254                         [ "Redirecting to $url\n" ] ];
255         } else {
256                 r(404);
257         }
258 }
259
260 1;