1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # wrapper for cgit(1) and git-http-backend(1) for browsing and
5 # serving git code repositories. Requires 'publicinbox.cgitrc'
6 # directive to be set in the public-inbox config file.
8 package PublicInbox::Cgit;
10 use parent qw(PublicInbox::WwwCoderepo);
11 use PublicInbox::GitHTTPBackend;
13 # not bothering with Exporter for a one-off
14 *input_prepare = \&PublicInbox::GitHTTPBackend::input_prepare;
15 *serve = \&PublicInbox::GitHTTPBackend::serve;
16 use PublicInbox::Qspawn;
17 use PublicInbox::WwwStatic qw(r);
21 my $cgit_bin = $pi_cfg->{'publicinbox.cgitbin'};
22 my $cgit_data = $pi_cfg->{'publicinbox.cgitdata'};
24 # /var/www/htdocs/cgit is the default install path from cgit.git
25 # /usr/{lib,share}/cgit is where Debian puts cgit
26 # TODO: check other distros for common paths
27 unless (defined $cgit_bin) {
28 foreach (qw(/var/www/htdocs/cgit /usr/lib/cgit)) {
29 my $x = "$_/cgit.cgi";
35 unless (defined $cgit_data) {
36 my @dirs = qw(/var/www/htdocs/cgit /usr/share/cgit);
38 # local installs of cgit from source have
39 # CGIT_SCRIPT_PATH==CGIT_DATA_PATH by default,
40 # so we can usually infer the cgit_data path from cgit_bin
41 if (defined($cgit_bin) && $cgit_bin =~ m!\A(.+?)/[^/]+\z!) {
42 unshift @dirs, $1 if -d $1;
45 next unless -f "$_/cgit.css";
50 ($cgit_bin, $cgit_data);
54 my ($class, $pi_cfg) = @_;
55 my ($cgit_bin, $cgit_data) = locate_cgit($pi_cfg);
56 $cgit_bin // return; # fall back in WWW->cgit
59 cgit_data => $cgit_data,
63 # some cgit repos may not be mapped to inboxes, so ensure those exist:
64 PublicInbox::WwwCoderepo::prepare_coderepos($self);
65 my $s = join('|', map { quotemeta } keys %{$pi_cfg->{-cgit_static}});
66 $self->{static} = qr/\A($s)\z/;
70 # only what cgit cares about:
82 # XXX: cgit filters may care about more variables...
84 my $parse_cgi_headers = \&PublicInbox::GitHTTPBackend::parse_cgi_headers;
87 my ($self, $env, $ctx) = @_; # $ctx is optional, used by WWW
88 my $path_info = $env->{PATH_INFO};
91 # handle requests without spawning cgit iff possible:
92 if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
93 my ($nick, $path) = ($1, $2);
94 if (my PublicInbox::Git $git = $self->{"\0$nick"}) {
95 return serve($env, $git, $path);
97 } elsif ($path_info =~ m!$self->{static}! &&
98 defined($cgit_data = $self->{cgit_data})) {
99 my $f = $cgit_data.$1; # {static} only matches leading slash
100 return PublicInbox::WwwStatic::response($env, [], $f);
103 my $cgi_env = { PATH_INFO => $path_info };
104 foreach (@PASS_ENV) {
105 my $v = $env->{$_} // next;
108 $cgi_env->{'HTTPS'} = 'on' if $env->{'psgi.url_scheme'} eq 'https';
110 my $rdr = input_prepare($env) or return r(500);
111 my $qsp = PublicInbox::Qspawn->new($self->{cmd}, $cgi_env, $rdr);
112 my $limiter = $self->{pi_cfg}->limiter('-cgit');
113 $qsp->psgi_return($env, $limiter, $parse_cgi_headers, $ctx);