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