1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Standalone code repository viewer for users w/o cgit
5 package PublicInbox::WwwCoderepo;
7 use File::Temp 0.19 (); # newdir
8 use PublicInbox::ViewVCS;
9 use PublicInbox::WwwStatic qw(r);
10 use PublicInbox::GitHTTPBackend;
12 use PublicInbox::GitAsyncCat;
13 use PublicInbox::WwwStream;
14 use PublicInbox::Hval qw(ascii_html);
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))."'";
20 # shared with PublicInbox::Cgit
21 sub prepare_coderepos {
23 my $pi_cfg = $self->{pi_cfg};
25 # TODO: support gitweb and other repository viewers?
26 if (defined(my $cgitrc = $pi_cfg->{-cgitrc_unparsed})) {
27 $pi_cfg->parse_cgitrc($cgitrc, 0);
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);
34 while (my ($nick, $repo) = each %$code_repos) {
35 $self->{"\0$nick"} = $repo;
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);
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);
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";
62 s/^ \(([^\)]+)\)// and $d = $1;
64 my ($H, $h, $cs, $s) = split(/ /, $_, 4);
65 print $zfh "<a\nhref=./$H/s/>$h</a> ", ascii_html($s),
67 print $zfh "\t(", ascii_html($d), ")\n" if $d;
69 print $zfh "# no commits, yet\n" if !@r;
70 print $zfh "...\n" if $last;
73 my ($bref, $oid, $ref_path) = @{delete $ctx->{-readme}};
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>';
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};
91 my ($pfx, $oid, $ref, $s, $cd) = split(/\0/);
92 utf8::decode($_) for ($ref, $s);
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";
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};
107 my (undef, $oid, $ref, $s, $cd) = split(/\0/);
108 utf8::decode($_) for ($ref, $s);
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";
114 print $zfh "# no tags yet...\n" if !@r;
115 print $zfh "...\n" if $last;
116 $wcb->($ctx->html_done('</pre>'));
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};
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};
138 my ($self, $ctx) = @_;
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;
157 sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked}
158 $ctx->{env}->{'qspawn.wcb'} = $_[0];
159 $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx);
163 sub srv { # endpoint called by PublicInbox::WWW
164 my ($self, $ctx) = @_;
165 my $path_info = $ctx->{env}->{PATH_INFO};
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);
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);
178 if ($path_info =~ m!\A/(.+?)\z! and ($git = $self->{"\0$1"})) {
179 my $qs = $ctx->{env}->{QUERY_STRING};
180 my $url = $git->base_url($ctx->{env});
181 $url .= "?$qs" if $qs ne '';
182 [ 301, [ Location => $url, 'Content-Type' => 'text/plain' ],
183 [ "Redirecting to $url\n" ] ];