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