]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwCoderepo.pm
www_coderepo: wire up snapshots from summary
[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 package PublicInbox::WwwCoderepo;
6 use v5.12;
7 use File::Temp 0.19 (); # newdir
8 use PublicInbox::ViewVCS;
9 use PublicInbox::WwwStatic qw(r);
10 use PublicInbox::GitHTTPBackend;
11 use PublicInbox::Git;
12 use PublicInbox::GitAsyncCat;
13 use PublicInbox::WwwStream;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::RepoSnapshot;
16
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))."'";
20
21 # shared with PublicInbox::Cgit
22 sub prepare_coderepos {
23         my ($self) = @_;
24         my $pi_cfg = $self->{pi_cfg};
25
26         # TODO: support gitweb and other repository viewers?
27         defined($pi_cfg->{'publicinbox.cgitrc'}) and
28                 $pi_cfg->parse_cgitrc(undef, 0);
29
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);
34         }
35         while (my ($nick, $repo) = each %$code_repos) {
36                 $self->{"\0$nick"} = $repo;
37         }
38 }
39
40 sub new {
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) };
48         };
49         $self->{$_} = 10 for qw(summary_branches summary_tags);
50         $self->{$_} = 10 for qw(summary_log);
51         $self;
52 }
53
54 sub summary_finish {
55         my ($ctx) = @_;
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);
59         my $zfh = $ctx->zfh;
60
61         # git log
62         my @r = split(/\n/s, pop(@x) // '');
63         my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
64         print $zfh <<EOM;
65 <pre>
66 <a
67 href='#readme'>about</a> <a
68 href='#heads'>heads</a> <a
69 href='#tags'>tags</a>
70
71 <a
72 id=log>\$</a> git log --pretty=format:'%h %s (%cs)%d'
73 EOM
74         for (@r) {
75                 my $d; # decorations
76                 s/^ \(([^\)]+)\)// and $d = $1;
77                 substr($_, 0, 1, '');
78                 my ($H, $h, $cs, $s) = split(/ /, $_, 4);
79                 print $zfh "<a\nhref=./$H/s/>$h</a> ", ascii_html($s),
80                         " (", $cs, ")\n";
81                 print $zfh "\t(", ascii_html($d), ")\n" if $d;
82         }
83         print $zfh "# no commits, yet\n" if !@r;
84         print $zfh "...\n" if $last;
85
86         # README
87         my ($bref, $oid, $ref_path) = @{delete $ctx->{-readme}};
88         if ($bref) {
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>';
95         }
96
97         # refs/heads
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};
104         for (@r) {
105                 my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
106                 utf8::decode($_) for ($ref, $s);
107                 chomp $cd;
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";
111         }
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}};
121         my $n;
122         if (@s) {
123                 $n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
124                 $n =~ s/\.git\z/-/;
125                 ($n) = ($n =~ m!([^/]+)\z!);
126                 $n = ascii_html($n);
127         }
128         for (@r) {
129                 my (undef, $oid, $ref, $s, $cd) = split(/\0/);
130                 utf8::decode($_) for ($ref, $s);
131                 chomp $cd;
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)";
135                 if (@s) {
136                         my $v = $ref;
137                         $v =~ s/\A[vV]//;
138                         print $zfh "\t",  join(' ', map {
139                                 qq{<a href="snapshot/$n$v.$_">$_</a>} } @s);
140                 }
141                 print $zfh "\n";
142         }
143         print $zfh "# no tags yet...\n" if !@r;
144         print $zfh "...\n" if $last;
145         $wcb->($ctx->html_done('</pre>'));
146 }
147
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};
153 }
154
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};
164 }
165
166 sub summary {
167         my ($self, $ctx) = @_;
168         $ctx->{wcr} = $self;
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;
185         }
186         sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked}
187                 $ctx->{env}->{'qspawn.wcb'} = $_[0];
188                 $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx);
189         }
190 }
191
192 sub srv { # endpoint called by PublicInbox::WWW
193         my ($self, $ctx) = @_;
194         my $path_info = $ctx->{env}->{PATH_INFO};
195         my $git;
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);
200         }
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);
206
207         # snapshots:
208         if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
209                         ($ctx->{git} = $self->{"\0$1"})) {
210                 $ctx->{wcr} = $self;
211                 return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
212         }
213
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" ] ];
221         } else {
222                 r(404);
223         }
224 }
225
226 1;