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