]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: consolidate PublicInbox::Git initalization
authorEric Wong <e@80x24.org>
Thu, 24 Dec 2015 09:32:29 +0000 (09:32 +0000)
committerEric Wong <e@80x24.org>
Tue, 5 Apr 2016 18:58:27 +0000 (18:58 +0000)
We should've done this earlier, but second time's a charm
and this forces us to think about other VCSes early.

lib/PublicInbox/RepoBrowse.pm
lib/PublicInbox/RepoBrowseGitCommit.pm
lib/PublicInbox/RepoBrowseGitLog.pm
lib/PublicInbox/RepoBrowseGitPatch.pm
lib/PublicInbox/RepoBrowseGitTree.pm

index 3aa8b98a86e7d293863199a032445a90041d09e4..e6063f1162c58307fab38a5d999c7fb85fb4459d 100644 (file)
@@ -63,14 +63,14 @@ sub run {
 
        my $cmd = shift @extra;
        if (defined $cmd && length $cmd) {
-               my $vcs = $VCS{$repo_info->{vcs}} or return r404();
+               my $vcs_lc = $repo_info->{vcs};
+               my $vcs = $VCS{$vcs_lc} or return r404();
                my $mod = $CMD{$cmd} or return r404();
                return r404() unless defined $mod && defined $vcs;
-               $mod = "PublicInbox::RepoBrowse$vcs$mod";
-               unless ($LOADED{$mod}) {
-                       eval "require $mod";
-                       $LOADED{$mod} = 1 unless $@;
-               }
+               $mod = load_once("PublicInbox::RepoBrowse$vcs$mod");
+               $vcs = load_once("PublicInbox::$vcs");
+               $repo_info->{$vcs_lc} ||= $vcs->new($repo_info->{path});
+
                $req->{relcmd} = '../' x scalar(@extra);
                my $rv = eval { $mod->new->call($cmd, $req) };
                $rv || r404();
@@ -86,4 +86,13 @@ sub summary {
 
 sub r404 { r(404, 'Not Found') }
 
+sub load_once {
+       my ($mod) = @_;
+
+       return $mod if $LOADED{$mod};
+       eval "require $mod";
+       $LOADED{$mod} = 1 unless $@;
+       $mod;
+}
+
 1;
index 8b831a151e1db7900a3a2d174583237897492203..16c14928f19e437a36d8ae7a36a50299dfae02b2 100644 (file)
@@ -7,7 +7,6 @@ use strict;
 use warnings;
 use base qw(PublicInbox::RepoBrowseBase);
 use PublicInbox::Hval qw(utf8_html);
-use PublicInbox::Git;
 use PublicInbox::RepoBrowseGit qw(git_unquote git_commit_title);
 
 use constant GIT_FMT => '--pretty=format:'.join('%n',
@@ -114,14 +113,11 @@ sub git_commit_stream {
 
 sub call_git_commit {
        my ($self, $req) = @_;
-       my $repo_info = $req->{repo_info};
-       my $dir = $repo_info->{path};
 
        my $q = PublicInbox::RepoBrowseQuery->new($req->{cgi});
        my $id = $q->{id};
        $id eq '' and $id = 'HEAD';
-       my $git = $repo_info->{git} ||= PublicInbox::Git->new($dir);
-
+       my $git = $req->{repo_info}->{git};
        my @cmd = qw(show -z --numstat -p --cc --encoding=UTF-8
                        --no-notes --no-color --abbrev=10);
        my @path;
index 07d384ac28cece53ab11d0e05760d3af2e8d4655..8ce16eac4e45b8fce335649c6f5a8f5419b7b198 100644 (file)
@@ -5,12 +5,10 @@ package PublicInbox::RepoBrowseGitLog;
 use strict;
 use warnings;
 use base qw(PublicInbox::RepoBrowseBase);
-use PublicInbox::Git;
 
 sub call_git_log {
        my ($self, $req) = @_;
        my $repo_info = $req->{repo_info};
-       my $path = $repo_info->{path};
        my $max = $repo_info->{max_commit_count} || 50;
        $max = int($max);
        $max = 50 if $max == 0;
@@ -26,7 +24,7 @@ sub call_git_log {
        my $ofs = $q->{ofs};
        $h .= "~$ofs" if $ofs =~ /\A\d+\z/;
 
-       my $git = $repo_info->{git} ||= PublicInbox::Git->new($path);
+       my $git = $repo_info->{git};
        my $log = $git->popen(qw(log --no-notes --no-color
                                --abbrev-commit --abbrev=16),
                                "--format=$fmt", "-$max", $h);
index b998a860b35e88de5aaf9bfc9d7d0cc55dccd43e..c5f1c01b6dc83fc0c8666955ea7c7652167893e3 100644 (file)
@@ -6,14 +6,10 @@ package PublicInbox::RepoBrowseGitPatch;
 use strict;
 use warnings;
 use base qw(PublicInbox::RepoBrowseBase);
-use PublicInbox::Git;
 
 sub call_git_patch {
        my ($self, $req) = @_;
-       my $repo_info = $req->{repo_info};
-       my $dir = $repo_info->{path};
-       my $git = $repo_info->{git} ||= PublicInbox::Git->new($dir);
-
+       my $git = $req->{repo_info}->{git};
        my $q = PublicInbox::RepoBrowseQuery->new($req->{cgi});
        my $id = $q->{id};
        $id eq '' and $id = 'HEAD';
index 973c07467ff34c17afb34fc8bea8f4a6e806a195..a3592a302bc243e17d283df84ea2ad9e8e71aa87 100644 (file)
@@ -4,7 +4,6 @@ package PublicInbox::RepoBrowseGitTree;
 use strict;
 use warnings;
 use base qw(PublicInbox::RepoBrowseBase);
-use PublicInbox::Git;
 use PublicInbox::Hval qw(utf8_html);
 
 my %GIT_MODE = (
@@ -17,8 +16,6 @@ my %GIT_MODE = (
 
 sub git_tree_stream {
        my ($self, $req, $res) = @_; # res: Plack callback
-       my $repo_info = $req->{repo_info};
-       my $dir = $repo_info->{path};
        my @extra = @{$req->{extra}};
        my $tslash;
        if (@extra && $extra[-1] eq '') { # no trailing slash
@@ -31,7 +28,7 @@ sub git_tree_stream {
        $id eq '' and $id = 'HEAD';
 
        my $obj = "$id:$tree_path";
-       my $git = $repo_info->{git} ||= PublicInbox::Git->new($dir);
+       my $git = $req->{repo_info}->{git};
        my ($hex, $type, $size) = $git->check($obj);
 
        if (!defined($type) || ($type ne 'blob' && $type ne 'tree')) {