]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwCoderepo.pm
www_coderepo: do not copy {-code_repos} from config
[public-inbox.git] / lib / PublicInbox / WwwCoderepo.pm
index e0fc90459d209b1a7299a550f09235723880d360..7f8b34597b62a5d4069a6fecdf483da35483a43e 100644 (file)
@@ -1,7 +1,10 @@
 # Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
-# Standalone code repository viewer for users w/o cgit
+# Standalone code repository viewer for users w/o cgit.
+# This isn't intended to replicate all of cgit, but merely to be a
+# "good enough" viewer with search support and some UI hints to encourage
+# cloning + command-line usage.
 package PublicInbox::WwwCoderepo;
 use v5.12;
 use File::Temp 0.19 (); # newdir
@@ -12,6 +15,8 @@ use PublicInbox::Git;
 use PublicInbox::GitAsyncCat;
 use PublicInbox::WwwStream;
 use PublicInbox::Hval qw(ascii_html);
+use PublicInbox::RepoSnapshot;
+use PublicInbox::RepoAtom;
 
 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
        join('%00', map { "%($_)" }
@@ -23,16 +28,26 @@ sub prepare_coderepos {
        my $pi_cfg = $self->{pi_cfg};
 
        # TODO: support gitweb and other repository viewers?
-       if (defined(my $cgitrc = $pi_cfg->{-cgitrc_unparsed})) {
-               $pi_cfg->parse_cgitrc($cgitrc, 0);
-       }
+       $pi_cfg->parse_cgitrc(undef, 0);
+
        my $code_repos = $pi_cfg->{-code_repos};
        for my $k (grep(/\Acoderepo\.(?:.+)\.dir\z/, keys %$pi_cfg)) {
                $k = substr($k, length('coderepo.'), -length('.dir'));
                $code_repos->{$k} //= $pi_cfg->fill_code_repo($k);
        }
-       while (my ($nick, $repo) = each %$code_repos) {
-               $self->{"\0$nick"} = $repo;
+
+       # associate inboxes and extindices with coderepos for search:
+       for my $k (grep(/\Apublicinbox\.(?:.+)\.coderepo\z/, keys %$pi_cfg)) {
+               $k = substr($k, length('publicinbox.'), -length('.coderepo'));
+               my $ibx = $pi_cfg->lookup_name($k) // next;
+               $pi_cfg->repo_objs($ibx);
+               push @{$self->{-strong}}, $ibx; # strengthen {-ibxs} weakref
+       }
+       for my $k (grep(/\Aextindex\.(?:.+)\.coderepo\z/, keys %$pi_cfg)) {
+               $k = substr($k, length('extindex.'), -length('.coderepo'));
+               my $eidx = $pi_cfg->lookup_ei($k) // next;
+               $pi_cfg->repo_objs($eidx);
+               push @{$self->{-strong}}, $eidx; # strengthen {-ibxs} weakref
        }
 }
 
@@ -40,6 +55,11 @@ sub new {
        my ($cls, $pi_cfg) = @_;
        my $self = bless { pi_cfg => $pi_cfg }, $cls;
        prepare_coderepos($self);
+       $self->{snapshots} = do {
+               my $s = $pi_cfg->{'coderepo.snapshots'} // '';
+               $s eq 'all' ? \%PublicInbox::RepoSnapshot::FMT_TYPES :
+                       +{ map { $_ => 1 } split(/\s+/, $s) };
+       };
        $self->{$_} = 10 for qw(summary_branches summary_tags);
        $self->{$_} = 10 for qw(summary_log);
        $self;
@@ -55,8 +75,16 @@ sub summary_finish {
        # git log
        my @r = split(/\n/s, pop(@x) // '');
        my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
-       print $zfh '<pre><a id=log>$</a> '.
-               "git log --pretty=format:'%h %s (%cs)%d'\n";
+       print $zfh <<EOM;
+<pre>
+<a
+href='#readme'>about</a> <a
+href='#heads'>heads</a> <a
+href='#tags'>tags</a>
+
+<a
+id=log>\$</a> git log --pretty=format:'%h %s (%cs)%d'
+EOM
        for (@r) {
                my $d; # decorations
                s/^ \(([^\)]+)\)// and $d = $1;
@@ -103,13 +131,28 @@ sub summary_finish {
                "%(refname:short) %(subject) (%(creatordate:short))'\n";
        @r = split(/^/sm, shift(@x) // '');
        $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_tags};
+       my @s = sort keys %{$ctx->{wcr}->{snapshots}};
+       my $n;
+       if (@s) {
+               $n = $ctx->{git}->local_nick // die "BUG: $ctx->{git_dir} nick";
+               $n =~ s/\.git\z/-/;
+               ($n) = ($n =~ m!([^/]+)\z!);
+               $n = ascii_html($n);
+       }
        for (@r) {
                my (undef, $oid, $ref, $s, $cd) = split(/\0/);
                utf8::decode($_) for ($ref, $s);
                chomp $cd;
                my $align = length($ref) < 12 ? ' ' x (12 - length($ref)) : '';
                print $zfh "<a\nhref=./$oid/s/>", ascii_html($ref),
-                       "</a>$align ", ascii_html($s), " ($cd)\n";
+                       "</a>$align ", ascii_html($s), " ($cd)";
+               if (@s) {
+                       my $v = $ref;
+                       $v =~ s/\A[vV]//;
+                       print $zfh "\t",  join(' ', map {
+                               qq{<a href="snapshot/$n$v.$_">$_</a>} } @s);
+               }
+               print $zfh "\n";
        }
        print $zfh "# no tags yet...\n" if !@r;
        print $zfh "...\n" if $last;
@@ -165,17 +208,31 @@ sub srv { # endpoint called by PublicInbox::WWW
        my $path_info = $ctx->{env}->{PATH_INFO};
        my $git;
        # handle clone requests
+       my $cr = $self->{pi_cfg}->{-code_repos};
        if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x) {
-               $git = $self->{"\0$1"} and return
+               $git = $cr->{$1} and return
                        PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
        }
        $path_info =~ m!\A/(.+?)/\z! and
-               ($ctx->{git} = $self->{"\0$1"}) and return summary($self, $ctx);
+               ($ctx->{git} = $cr->{$1}) and return summary($self, $ctx);
        $path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/\z! and
-                       ($ctx->{git} = $self->{"\0$1"}) and
+                       ($ctx->{git} = $cr->{$1}) and
                return PublicInbox::ViewVCS::show($ctx, $2);
 
-       if ($path_info =~ m!\A/(.+?)\z! and ($git = $self->{"\0$1"})) {
+       # snapshots:
+       if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               $ctx->{wcr} = $self;
+               return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
+       }
+
+       if ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               return PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
+       }
+
+       # enforce trailing slash:
+       if ($path_info =~ m!\A/(.+?)\z! and ($git = $cr->{$1})) {
                my $qs = $ctx->{env}->{QUERY_STRING};
                my $url = $git->base_url($ctx->{env});
                $url .= "?$qs" if $qs ne '';