]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwCoderepo.pm
www_coderepo: remove some needless return statements
[public-inbox.git] / lib / PublicInbox / WwwCoderepo.pm
index 99df39ef9cb9d8ac971d31733c2ede0e90808ed8..8dcd97728293f8f3bd8b5ffc4ff7fd653ead6467 100644 (file)
@@ -8,14 +8,16 @@
 package PublicInbox::WwwCoderepo;
 use v5.12;
 use File::Temp 0.19 (); # newdir
+use POSIX qw(O_RDWR F_GETFL);
 use PublicInbox::ViewVCS;
 use PublicInbox::WwwStatic qw(r);
 use PublicInbox::GitHTTPBackend;
-use PublicInbox::Git;
-use PublicInbox::GitAsyncCat;
 use PublicInbox::WwwStream;
 use PublicInbox::Hval qw(ascii_html);
+use PublicInbox::ViewDiff qw(uri_escape_path);
 use PublicInbox::RepoSnapshot;
+use PublicInbox::RepoAtom;
+use PublicInbox::RepoTree;
 
 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
        join('%00', map { "%($_)" }
@@ -27,8 +29,7 @@ sub prepare_coderepos {
        my $pi_cfg = $self->{pi_cfg};
 
        # TODO: support gitweb and other repository viewers?
-       defined($pi_cfg->{'publicinbox.cgitrc'}) and
-               $pi_cfg->parse_cgitrc(undef, 0);
+       $pi_cfg->parse_cgitrc(undef, 0);
 
        my $code_repos = $pi_cfg->{-code_repos};
        for my $k (grep(/\Acoderepo\.(?:.+)\.dir\z/, keys %$pi_cfg)) {
@@ -41,16 +42,11 @@ sub prepare_coderepos {
                $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
-       }
-       while (my ($nick, $repo) = each %$code_repos) {
-               $self->{"\0$nick"} = $repo;
        }
 }
 
@@ -65,6 +61,15 @@ sub new {
        };
        $self->{$_} = 10 for qw(summary_branches summary_tags);
        $self->{$_} = 10 for qw(summary_log);
+
+       # try reuse STDIN if it's already /dev/null
+       open $self->{log_fh}, '+>', '/dev/null' or die "open: $!";
+       my @l = stat($self->{log_fh}) or die "stat: $!";
+       my @s = stat(STDIN) or die "stat(STDIN): $!";
+       if ("@l[0, 1]" eq "@s[0, 1]") {
+               my $f = fcntl(STDIN, F_GETFL, 0) // die "F_GETFL: $!";
+               $self->{log_fh} = *STDIN{IO} if $f & O_RDWR;
+       }
        $self;
 }
 
@@ -78,15 +83,12 @@ sub summary_finish {
        # git log
        my @r = split(/\n/s, pop(@x) // '');
        my $last = pop(@r) if scalar(@r) > $ctx->{wcr}->{summary_log};
+       my $tip_html = '';
+       if (defined(my $tip = $ctx->{qp}->{h})) {
+               $tip_html .= ' '.ascii_html($tip).' --';
+       }
        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'
+<pre><a id=log>\$</a> git log --pretty=format:'%h %s (%cs)%d'$tip_html
 EOM
        for (@r) {
                my $d; # decorations
@@ -105,8 +107,10 @@ EOM
        if ($bref) {
                my $l = PublicInbox::Linkify->new;
                $$bref =~ s/\s*\z//sm;
+               my (undef, $path) = split(/:/, $ref_path, 2); # HEAD:README
                print $zfh "\n<a id=readme>\$</a> " .
-                       "git cat-file blob <a href=./$oid/s/>",
+                       qq(git cat-file blob <a href="./$oid/s/?b=) .
+                       ascii_html(uri_escape_path($path)) . q(">).
                        ascii_html($ref_path), "</a>\n",
                        $l->to_html($$bref), '</pre><hr><pre>';
        }
@@ -183,23 +187,27 @@ sub set_readme { # git->cat_async callback
 sub summary {
        my ($self, $ctx) = @_;
        $ctx->{wcr} = $self;
+       my $tip = $ctx->{qp}->{h}; # same as cgit
+       if (defined $tip && $tip eq '') {
+               delete $ctx->{qp}->{h};
+               undef($tip);
+       }
        my $nb = $self->{summary_branches} + 1;
        my $nt = $self->{summary_tags} + 1;
        my $nl = $self->{summary_log} + 1;
-       my $qsp = PublicInbox::Qspawn->new([qw(/bin/sh -c),
+
+       my @cmd = (qw(/bin/sh -c),
                "$EACH_REF --count=$nb refs/heads; echo && " .
                "$EACH_REF --count=$nt refs/tags; echo && " .
-               "git log -$nl --pretty=format:'%d %H %h %cs %s' --" ],
+               qq(git log -$nl --pretty=format:'%d %H %h %cs %s' "\$@" --));
+       push @cmd, '--', $tip if defined($tip);
+       my $qsp = PublicInbox::Qspawn->new(\@cmd,
                { GIT_DIR => $ctx->{git}->{git_dir} });
        $qsp->{qsp_err} = \($ctx->{-qsp_err} = '');
-       my @try = qw(HEAD:README HEAD:README.md); # TODO: configurable
+       $tip //= 'HEAD';
+       my @try = ("$tip:README", "$tip:README.md"); # TODO: configurable
        $ctx->{-nr_readme_tries} = [ @try ];
-       $ctx->{git}->cat_async($_, \&set_readme, $ctx) for @try;
-       if ($ctx->{env}->{'pi-httpd.async'}) {
-               PublicInbox::GitAsyncCat::watch_cat($ctx->{git});
-       } else { # synchronous
-               $ctx->{git}->cat_async_wait;
-       }
+       PublicInbox::ViewVCS::do_cat_async($ctx, \&set_readme, @try);
        sub { # $_[0] => PublicInbox::HTTP::{Identity,Chunked}
                $ctx->{env}->{'qspawn.wcb'} = $_[0];
                $qsp->psgi_qx($ctx->{env}, undef, \&capture_refs, $ctx);
@@ -211,25 +219,28 @@ sub srv { # endpoint called by PublicInbox::WWW
        my $path_info = $ctx->{env}->{PATH_INFO};
        my $git;
        # handle clone requests
-       if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x) {
-               $git = $self->{"\0$1"} and return
+       my $cr = $self->{pi_cfg}->{-code_repos};
+       if ($path_info =~ m!\A/(.+?)/($PublicInbox::GitHTTPBackend::ANY)\z!x and
+               ($git = $cr->{$1})) {
                        PublicInbox::GitHTTPBackend::serve($ctx->{env},$git,$2);
-       }
-       $path_info =~ m!\A/(.+?)/\z! and
-               ($ctx->{git} = $self->{"\0$1"}) and return summary($self, $ctx);
-       $path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/\z! and
-                       ($ctx->{git} = $self->{"\0$1"}) and
-               return PublicInbox::ViewVCS::show($ctx, $2);
-
-       # snapshots:
-       if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
-                       ($ctx->{git} = $self->{"\0$1"})) {
+       } elsif ($path_info =~ m!\A/(.+?)/\z! and ($ctx->{git} = $cr->{$1})) {
+               summary($self, $ctx)
+       } elsif ($path_info =~ m!\A/(.+?)/([a-f0-9]+)/s/([^/]+)?\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               $ctx->{lh} = $self->{log_fh};
+               PublicInbox::ViewVCS::show($ctx, $2, $3);
+       } elsif ($path_info =~ m!\A/(.+?)/tree/(.*)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               $ctx->{lh} = $self->{log_fh};
+               PublicInbox::RepoTree::srv_tree($ctx, $2) // r(404);
+       } elsif ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
                $ctx->{wcr} = $self;
-               return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
-       }
-
-       # enforce trailing slash:
-       if ($path_info =~ m!\A/(.+?)\z! and ($git = $self->{"\0$1"})) {
+               PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
+       } elsif ($path_info =~ m!\A/(.+?)/atom/(.*)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               PublicInbox::RepoAtom::srv_atom($ctx, $2) // r(404);
+       } elsif ($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 '';