From: Eric Wong Date: Tue, 22 Dec 2015 01:36:45 +0000 (+0000) Subject: repobrowse: prefix VCS-specific modules X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c2a9bc53532ace22dbb143ef7df65cf063ae6e4f;p=thirdparty%2Fpublic-inbox.git repobrowse: prefix VCS-specific modules Currently we only support git, but this should make it easier to handle other VCS-es in the future. --- diff --git a/lib/PublicInbox/RepoBrowse.pm b/lib/PublicInbox/RepoBrowse.pm index 0b3197ea2..fcecf472d 100644 --- a/lib/PublicInbox/RepoBrowse.pm +++ b/lib/PublicInbox/RepoBrowse.pm @@ -24,6 +24,8 @@ use URI::Escape qw(uri_escape_utf8 uri_unescape); use PublicInbox::RepoConfig; my %CMD = map { lc($_) => $_ } qw(Log Commit Tree); +my %VCS = (git => 'Git'); +my %LOADED; sub new { my ($class, $file) = @_; @@ -61,15 +63,16 @@ sub run { my $cmd = shift @extra; if (defined $cmd && length $cmd) { - my $mod = $CMD{$cmd}; - return r404() unless defined $mod; - if (index($mod, ':') < 0) { - $mod = "PublicInbox::RepoBrowse$mod"; + my $vcs = $VCS{$repo_info->{vcs}} 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"; - $CMD{$cmd} = $mod unless $@; + $LOADED{$mod} = 1; } $req->{relcmd} = '../' x scalar(@extra); - my $rv = eval { $mod->new->call($req) }; + my $rv = eval { $mod->new->call($cmd, $req) }; $rv || r404(); } else { $req->{relcmd} = defined $cmd ? '' : './'; diff --git a/lib/PublicInbox/RepoBrowseBase.pm b/lib/PublicInbox/RepoBrowseBase.pm index cd9e66de6..e0ea2854e 100644 --- a/lib/PublicInbox/RepoBrowseBase.pm +++ b/lib/PublicInbox/RepoBrowseBase.pm @@ -9,11 +9,11 @@ require PublicInbox::Hval; sub new { bless {}, shift } sub call { - my ($self, $req) = @_; + my ($self, $cmd, $req) = @_; my $vcs = $req->{repo_info}->{vcs}; my $rv = eval { no strict 'refs'; - my $sub = 'call_'.$vcs; + my $sub = "call_${vcs}_$cmd"; $self->$sub($req); }; $@ ? [ 500, ['Content-Type'=>'text/plain'], [] ] : $rv; diff --git a/lib/PublicInbox/RepoBrowseCommit.pm b/lib/PublicInbox/RepoBrowseGitCommit.pm similarity index 98% rename from lib/PublicInbox/RepoBrowseCommit.pm rename to lib/PublicInbox/RepoBrowseGitCommit.pm index 91553d1c6..1e2109ecd 100644 --- a/lib/PublicInbox/RepoBrowseCommit.pm +++ b/lib/PublicInbox/RepoBrowseGitCommit.pm @@ -1,7 +1,7 @@ # Copyright (C) 2015 all contributors # License: AGPL-3.0+ -package PublicInbox::RepoBrowseCommit; +package PublicInbox::RepoBrowseGitCommit; use strict; use warnings; use base qw(PublicInbox::RepoBrowseBase); @@ -84,7 +84,7 @@ sub git_commit_stream { $fh->write(''); } -sub call_git { +sub call_git_commit { my ($self, $req) = @_; my $repo_info = $req->{repo_info}; my $path = $repo_info->{path}; diff --git a/lib/PublicInbox/RepoBrowseLog.pm b/lib/PublicInbox/RepoBrowseGitLog.pm similarity index 97% rename from lib/PublicInbox/RepoBrowseLog.pm rename to lib/PublicInbox/RepoBrowseGitLog.pm index 26b5204a2..07d384ac2 100644 --- a/lib/PublicInbox/RepoBrowseLog.pm +++ b/lib/PublicInbox/RepoBrowseGitLog.pm @@ -1,13 +1,13 @@ # Copyright (C) 2015 all contributors # License: AGPL-3.0+ -package PublicInbox::RepoBrowseLog; +package PublicInbox::RepoBrowseGitLog; use strict; use warnings; use base qw(PublicInbox::RepoBrowseBase); use PublicInbox::Git; -sub call_git { +sub call_git_log { my ($self, $req) = @_; my $repo_info = $req->{repo_info}; my $path = $repo_info->{path}; diff --git a/lib/PublicInbox/RepoBrowseTree.pm b/lib/PublicInbox/RepoBrowseGitTree.pm similarity index 98% rename from lib/PublicInbox/RepoBrowseTree.pm rename to lib/PublicInbox/RepoBrowseGitTree.pm index 47ecdd319..b176bb3f7 100644 --- a/lib/PublicInbox/RepoBrowseTree.pm +++ b/lib/PublicInbox/RepoBrowseGitTree.pm @@ -1,6 +1,6 @@ # Copyright (C) 2015 all contributors # License: AGPL-3.0+ -package PublicInbox::RepoBrowseTree; +package PublicInbox::RepoBrowseGitTree; use strict; use warnings; use base qw(PublicInbox::RepoBrowseBase); @@ -54,7 +54,7 @@ sub git_tree_stream { $fh->close; } -sub call_git { +sub call_git_tree { my ($self, $req) = @_; sub { git_tree_stream($self, $req, @_) }; }