]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: memoize git symbolic-ref resolution
authorEric Wong <e@80x24.org>
Thu, 16 Feb 2017 23:26:01 +0000 (23:26 +0000)
committerEric Wong <e@80x24.org>
Thu, 16 Feb 2017 23:26:01 +0000 (23:26 +0000)
The "HEAD" symbolic ref is rarely changed, so
memoize it for now and avoid exposing it in URLs.

12 files changed:
lib/PublicInbox/Repo.pm
lib/PublicInbox/RepoGitAtom.pm
lib/PublicInbox/RepoGitBlob.pm
lib/PublicInbox/RepoGitCommit.pm
lib/PublicInbox/RepoGitDiffCommon.pm
lib/PublicInbox/RepoGitLog.pm
lib/PublicInbox/RepoGitPatch.pm
lib/PublicInbox/RepoGitPlain.pm
lib/PublicInbox/RepoGitSnapshot.pm
lib/PublicInbox/RepoGitSummary.pm
lib/PublicInbox/RepoGitTree.pm
lib/PublicInbox/Repobrowse.pm

index ceebe5c42f8257238a6e04f5fd8536fd9f098d27..f0cb4b3d2c4a5da73ccda8e964b8c50b25655412 100644 (file)
@@ -46,4 +46,15 @@ sub cloneurl {
        $self->{cloneurl} = $url;
 }
 
+sub tip {
+       my ($self) = @_;
+       $self->{-tip} ||= do {
+               if ($self->{vcs} eq 'git') {
+                       my $t = $self->{git}->qx(qw(symbolic-ref --short HEAD));
+                       chomp $t;
+                       $t;
+               }
+       };
+}
+
 1;
index 531d8b9dd233bd12625dea044f704f0ce54b9a15..615c8927ccdeb6a1ea9270e7ba1219c11480674e 100644 (file)
@@ -66,8 +66,9 @@ sub git_atom_sed ($$) {
        my $state = 0;
        my $rel = $req->{relcmd};
        my $repo = $req->{-repo};
+       my $tip = $repo->tip;
        my $title = join('/', $repo->{repo}, @{$req->{extra}});
-       $title = utf8_html("$title, $req->{-tip}");
+       $title = utf8_html("$title, $tip");
        my $url = repo_root_url($self, $req);
        my $hdr = {};
        my $subtitle = $repo->desc_html;
@@ -140,7 +141,7 @@ sub call_git_atom {
 
        my $git = $repo->{git};
        my $env = $req->{env};
-       my $tip = $req->{-tip};
+       my $tip = $req->{h} || $repo->tip;
        my $read_log = sub {
                my $cmd = $git->cmd(qw(log --no-notes --no-color
                                        --abbrev-commit), $git->abbrev,
@@ -154,15 +155,7 @@ sub call_git_atom {
 
        sub {
                $env->{'qspawn.response'} = $_[0];
-               return $read_log->() if $tip ne '';
-
-               my $cmd = $git->cmd(qw(symbolic-ref --short HEAD));
-               my $rdr = { 2 => $git->err_begin };
-               my $qsp = PublicInbox::Qspawn->new($cmd, undef, undef, $rdr);
-               $qsp->psgi_qx($env, undef, sub {
-                       chomp($tip = ${$_[0]});
-                       $read_log->();
-               })
+               $read_log->();
        }
 }
 
index ca8a3c2f5bd5cbd9c1499565d8f0b6795e1dba15..b535b0ad79aade3cb38c4d6b923eef4f70dd7504 100644 (file)
@@ -11,8 +11,9 @@ our @EXPORT = qw(git_blob_mime_type git_blob_stream_response);
 
 sub call_git_blob {
        my ($self, $req) = @_;
-       my $git = $req->{-repo}->{git};
-       my $id = $req->{-tip} . ':' . $req->{expath};
+       my $repo = $req->{-repo};
+       my $git = $repo->{git};
+       my $id = $repo->tip . ':' . $req->{expath};
 
        my ($cat, $hex, $type, $size) = $git->cat_file_begin($id);
        return unless defined $cat;
index 21f450ea7beca779758e9dfa83df50d42a73fcb7..1a10b13cd2177bf02d625ecb0c78f50ff95e92ba 100644 (file)
@@ -117,7 +117,6 @@ sub git_commit_sed ($$) {
 sub call_git_commit { # RepoBase calls this
        my ($self, $req) = @_;
        my $env = $req->{env};
-       my $tip = $req->{-tip};
 
        my $expath = $req->{expath};
        if ($expath ne '') {
@@ -128,7 +127,7 @@ sub call_git_commit { # RepoBase calls this
        my $git = $req->{-repo}->{git};
        my $cmd = $git->cmd(qw(show -z --numstat -p --encoding=UTF-8
                        --no-notes --no-color -c),
-                       $git->abbrev, GIT_FMT, $tip, '--');
+                       $git->abbrev, GIT_FMT, $req->{-repo}->tip, '--');
        my $rdr = { 2 => $git->err_begin };
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        $env->{'qspawn.quiet'} = 1;
index 3e3ea4ee04f40b3569392397cf0bce8569b4a33b..67adca682d0c357d0c7f420081b18837fe8808e4 100644 (file)
@@ -162,6 +162,7 @@ sub DSTATE_LINES () { 2 }
 sub git_diff_sed_init ($) {
        my ($req) = @_;
        $req->{dbuf} = '';
+       $req->{-tip} = $req->{-repo}->tip;
        $req->{ndiff} = $req->{nchg} = $req->{nadd} = $req->{ndel} = 0;
        $req->{dstate} = DSTATE_INIT;
 }
index b759a5c080aad2a506df5e2602a2f618bb9b4a55..4e0997e036abc99ed12d64fed3c8bcc8fc8b50a6 100644 (file)
@@ -128,16 +128,17 @@ sub call_git_log {
        $max = 50 if $max == 0;
        my $env = $req->{env};
        my $git = $repo->{git};
+       my $tip = $req->{-repo}->tip;
        my $cmd = $git->cmd(qw(log --no-notes --no-color --abbrev-commit),
                                $git->abbrev, $LOG_FMT, "-$max",
-                               $req->{-tip}, '--');
+                               $tip, '--');
        my $rdr = { 2 => $git->err_begin };
        my $title = "log: $repo->{repo}";
        if (defined $h) {
                $title .= ' ('. utf8_html($h). ')';
                $req->{lpfx} = $req->{relcmd};
        } else {
-               $req->{lpfx} = $req->{relcmd}.$req->{-tip};
+               $req->{lpfx} = $req->{relcmd} . $tip;
        }
        $req->{lhtml} = $self->html_start($req, $title) . "\n\n";
        my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
index e1431f39a5f7412f348851e63551145883d1cbb9..3e4eabe9a60ea2462403856f90879a83f6f99c05 100644 (file)
@@ -15,9 +15,10 @@ my $sig = '--signature=git '.join(' ', @CMD);
 
 sub call_git_patch {
        my ($self, $req) = @_;
-       my $git = $req->{-repo}->{git};
+       my $repo = $req->{-repo};
+       my $git = $repo->{git};
        my $env = $req->{env};
-       my $tip = $req->{-tip};
+       my $tip = $repo->tip;
        $tip =~ /\A[\w-]+([~\^][~\^\d])*\z/;
 
        # limit scope, don't take extra args to avoid wasting server
index 79a15be6422faa151c6328bfcc52d52c4fa1b7f7..8aff2177c4209094a46bb42a5db9d9a7a41fdc62 100644 (file)
@@ -10,8 +10,9 @@ use PublicInbox::Qspawn;
 
 sub call_git_plain {
        my ($self, $req) = @_;
-       my $git = $req->{-repo}->{git};
-       my $id = $req->{-tip} . ':' . $req->{expath};
+       my $repo = $req->{-repo};
+       my $git = $repo->{git};
+       my $id = $repo->tip . ':' . $req->{expath};
        my ($cat, $hex, $type, $size) = $git->cat_file_begin($id);
        return unless defined $cat;
 
index 44a6bd596906b64d894a95d2cbbae31175b4bc66..3d53fa6d9a2ae3823a3f6372c6cc9cbe235f81bd 100644 (file)
@@ -36,7 +36,7 @@ our %FMT_TYPES = (
 sub call_git_snapshot ($$) { # invoked by PublicInbox::RepoBase::call
        my ($self, $req) = @_;
 
-       my $ref = $req->{-tip};
+       my $ref = $req->{h} || $req->{-repo}->tip;
        my $orig_fn = $ref;
 
        # just in case git changes refname rules, don't allow wonky filenames
index 76d9c2b9fe9f19a57f962c2a465551ddf374d638..38fce1f5151e57776199627c1653204cfaa5fdb7 100644 (file)
@@ -13,18 +13,9 @@ sub call_git_summary {
        my ($self, $req) = @_;
        my $git = $req->{-repo}->{git};
        my $env = $req->{env};
-
-       # n.b. we would use %(HEAD) in for-each-ref --format if we could
-       # rely on git 1.9.0+, but it's too soon for that in early 2017...
-       my $cmd = $git->cmd(qw(symbolic-ref HEAD));
-       my $rdr = { 2 => $git->err_begin };
-       my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
        sub {
                my ($res) = @_; # Plack streaming callback
-               $qsp->psgi_qx($env, undef, sub {
-                       chomp(my $head_ref = ${$_[0]});
-                       for_each_ref($self, $req, $res, $head_ref);
-               });
+               for_each_ref($self, $req, $res, $req->{-repo}->tip);
        }
 }
 
@@ -92,15 +83,15 @@ sub for_each_ref {
 sub readme_path_links {
        my ($req, $rel, $readme) = @_;
        my @path = split(m!/+!, $readme);
-
-       my $s = "tree <a\nhref=\"${rel}tree/$req->{-tip}\">root</a>/";
+       my $tip = $req->{-repo}->tip;
+       my $s = "tree <a\nhref=\"${rel}tree/$tip\">root</a>/";
        my @t;
        $s .= join('/', (map {
                push @t, $_;
                my $e = PublicInbox::Hval->utf8($_, join('/', @t));
                my $ep = $e->as_path;
                my $eh = $e->as_html;
-               $e = "<a\nhref=\"${rel}tree/$req->{-tip}/$ep\">$eh</a>";
+               $e = "<a\nhref=\"${rel}tree/$tip/$ep\">$eh</a>";
                # bold the last one
                scalar(@t) == scalar(@path) ? "<b>$e</b>" : $e;
        } @path));
index 716dfe029c94734d95f46c95d89ee64da28f11a9..a4add6198d32acb59e3191a8b9e814abf2c9d0b1 100644 (file)
@@ -20,8 +20,10 @@ my $BINARY_MSG = "Binary file, save using the 'raw' link above";
 sub call_git_tree {
        my ($self, $req) = @_;
        my @extra = @{$req->{extra}};
-       my $git = $req->{-repo}->{git};
-       my $obj = "$req->{-tip}:$req->{expath}";
+       my $repo = $req->{-repo};
+       my $git = $repo->{git};
+       my $tip = $repo->tip;
+       my $obj = "$tip:$req->{expath}";
        my ($hex, $type, $size) = $git->check($obj);
 
        unless (defined($type)) {
@@ -57,7 +59,7 @@ sub cur_path {
        my @ex = @{$req->{extra}} or return '<b>root</b>';
        my $s;
 
-       my $tip = $req->{-tip};
+       my $tip = $req->{-repo}->tip;
        my $rel = $req->{relcmd};
        # avoid relative paths, here, we don't want to propagate
        # trailing-slash URLs although we tolerate them
@@ -80,9 +82,9 @@ sub git_blob_show {
        my $text_p;
        my $n = 0;
 
-       my $tip = $req->{-tip};
        my $rel = $req->{relcmd};
-       my $plain = join('/', "${rel}plain/$tip", @{$req->{extra}});
+       my $plain = join('/',
+                       "${rel}plain", $req->{-repo}->tip, @{$req->{extra}});
        $plain = PublicInbox::Hval->utf8($plain)->as_path;
        my $t = cur_path($req);
        my $s = qq{\npath: $t\n\nblob $hex};
@@ -194,9 +196,9 @@ sub git_tree_show {
        } elsif (defined(my $last = $req->{extra}->[-1])) {
                $pfx = PublicInbox::Hval->utf8($last)->as_path;
        } elsif (defined $req->{h}) {
-               $pfx = $req->{-tip};
+               $pfx = $req->{-repo}->tip;
        } else {
-               $pfx = 'tree/' . $req->{-tip};
+               $pfx = 'tree/' . $req->{-repo}->tip;
        }
        $req->{tpfx} = $pfx;
        my $env = $req->{env};
index 7f50a87708f2e3da9e3d07db67466093b7e7bf1a..34ffd5426c41d37675c12fedfa1ac4b4ca5f2cba 100644 (file)
@@ -141,7 +141,6 @@ sub call {
                ++$tslash;
        }
        $req->{h} = $h;
-       $req->{-tip} = defined $h ? $h : 'HEAD';
        return no_tslash($env) if ($tslash && $NO_TSLASH{$mod});
 
        $req->{tslash} = $tslash;