Align Vhost code with reality. 0.003007
Dan Brook [Sat, 28 Jan 2012 23:16:01 +0000 (23:16 +0000)]
Now that we potentially have a different COR per hit (depending on config) the
COR model has changed a lot. Now its COMPONENT will return a G::G::COR-based
class which in turn provides an ACCEPT_CONTEXT method that should return a
further G::G::COR object based on the request. In the simple case, as with the
default config, the same G::G::COR that was built for the COMPONENT is also
returned by ACCEPT_CONTEXT. With the Vhost COR a different G::G::COR will be
returned depending on the related configuration.

26 files changed:
lib/Gitalist/Git/CollectionOfRepositories.pm
lib/Gitalist/Git/CollectionOfRepositories/Role/Context.pm [new file with mode: 0644]
lib/Gitalist/Git/CollectionOfRepositories/Vhost.pm
lib/Gitalist/Git/CollectionOfRepositoriesWithRequestState.pm [new file with mode: 0644]
lib/Gitalist/Model/CollectionOfRepos.pm
lib/Gitalist/TraitFor/Script.pm
t/lib/repositories_sets/catgit/Catalyst-Runtime [deleted submodule]
t/lib/repositories_sets/catgit/Catalyst-Runtime/HEAD [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/config [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/description [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/export-ok [copied from t/model_collectionofrepos_vhost.t with 100% similarity]
t/lib/repositories_sets/catgit/Catalyst-Runtime/info/exclude [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/branch1 [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/master [new file with mode: 0644]
t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/tags/0.01 [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose [deleted submodule]
t/lib/repositories_sets/moose/Moose/HEAD [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/config [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/description [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/export-ok [moved from t/model_collectionofrepos_vhost.t with 100% similarity]
t/lib/repositories_sets/moose/Moose/info/exclude [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/refs/heads/branch1 [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/refs/heads/master [new file with mode: 0644]
t/lib/repositories_sets/moose/Moose/refs/tags/0.01 [new file with mode: 0644]
t/model_collectionofrepos.t
t/scripts.t [deleted file]

index 706f489..dc3efe5 100644 (file)
@@ -1,6 +1,8 @@
 use MooseX::Declare;
 
-role Gitalist::Git::CollectionOfRepositories with Gitalist::Git::Serializable {
+role Gitalist::Git::CollectionOfRepositories
+     with Gitalist::Git::Serializable
+     with Gitalist::Git::CollectionOfRepositories::Role::Context {
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use MooseX::Types::Moose qw/ArrayRef/;
     use Moose::Autobox;
diff --git a/lib/Gitalist/Git/CollectionOfRepositories/Role/Context.pm b/lib/Gitalist/Git/CollectionOfRepositories/Role/Context.pm
new file mode 100644 (file)
index 0000000..d5d5174
--- /dev/null
@@ -0,0 +1,16 @@
+use MooseX::Declare;
+
+role Gitalist::Git::CollectionOfRepositories::Role::Context {
+    requires qw/
+        implementation_class
+        ACCEPT_CONTEXT
+    /;
+
+    method implementation_class {
+        $self->meta->name
+    }
+     
+    method ACCEPT_CONTEXT($ctx) {
+        return $self;
+    }
+}
index dd12c12..345f21e 100644 (file)
@@ -1,12 +1,9 @@
 use MooseX::Declare;
 
 class Gitalist::Git::CollectionOfRepositories::Vhost
-    with Gitalist::Git::CollectionOfRepositories {
-    use MooseX::Types::Moose qw/ HashRef Str /;
-    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
-    use MooseX::Types::Path::Class qw/Dir/;
-    use Moose::Util::TypeConstraints;
+     with Gitalist::Git::CollectionOfRepositoriesWithRequestState {
 
+    use MooseX::Types::Moose qw/HashRef/;
     sub BUILDARGS { # FIXME - This is fuck ugly!
         my ($class, @args) = @_;
         my $args = $class->next::method(@args);
@@ -21,6 +18,34 @@ class Gitalist::Git::CollectionOfRepositories::Vhost
         return $ret;
     }
 
+    has collections => (
+        is       => 'ro',
+        isa      => HashRef,
+        required => 1,
+    );
+
+    has vhost_dispatch => (
+        isa => HashRef,
+        traits => ['Hash'],
+        required => 1,
+        handles => {
+            _get_collection_name_for_vhost => 'get',
+        },
+    );
+
+    method implementation_class { 'Gitalist::Git::CollectionOfRepositories::VhostImpl' }
+    method extract_request_state ($ctx) {
+        return (vhost => $ctx->uri->host);
+    }
+}
+
+class Gitalist::Git::CollectionOfRepositories::VhostImpl
+    with Gitalist::Git::CollectionOfRepositories {
+    use MooseX::Types::Moose qw/ HashRef Str /;
+    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Path::Class qw/Dir/;
+    use Moose::Util::TypeConstraints;
+
     has vhost_dispatch => (
         isa => HashRef,
         traits => ['Hash'],
@@ -49,6 +74,7 @@ class Gitalist::Git::CollectionOfRepositories::Vhost
 
     role_type 'Gitalist::Git::CollectionOfRepositories';
     has chosen_collection => (
+        is => 'ro',
         does => 'Gitalist::Git::CollectionOfRepositories',
         handles => [qw/
             _get_repo_from_name
diff --git a/lib/Gitalist/Git/CollectionOfRepositoriesWithRequestState.pm b/lib/Gitalist/Git/CollectionOfRepositoriesWithRequestState.pm
new file mode 100644 (file)
index 0000000..f601d01
--- /dev/null
@@ -0,0 +1,13 @@
+use MooseX::Declare;
+
+role Gitalist::Git::CollectionOfRepositoriesWithRequestState {
+    requires qw/
+        implementation_class
+        extract_request_state
+    /;
+
+    method ACCEPT_CONTEXT($c) {
+        $self->implementation_class->new(%$self, $self->extract_request_state($c))->chosen_collection;
+    }
+}
+
index 08001e2..a45e399 100644 (file)
@@ -9,12 +9,10 @@ use Moose::Util::TypeConstraints;
 use Moose::Autobox;
 use Path::Class qw/ dir /;
 use namespace::autoclean;
+use Carp qw/croak/;
 
 extends 'Catalyst::Model';
 
-with 'Catalyst::Component::ApplicationAttribute';
-with 'Catalyst::Component::InstancePerContext';
-
 has class => (
     isa => LoadableClass,
     is  => 'ro',
@@ -39,7 +37,7 @@ sub _build_class {
         return 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
     }
     else {
-        return "Don't know where to get repositores from. Try a --repo_dir option, or setting up config";
+        croak "Don't know where to get repositores from. Try a --repo_dir option, or setting up config";
     }
 }
 
@@ -88,29 +86,35 @@ sub _build_repo_dir {
     return $ENV{GITALIST_REPO_DIR};
 }
 
-sub build_per_context_instance {
-    my ($self, $ctx) = @_;
+sub BUILD {
+    my($self) = @_;
 
     $self->class();
 
     if ($self->repo_dir) { $self->repo_dir->resolve }
+}
+
+sub COMPONENT {
+    my($class, $ctx, @args) = @_;
+
+    my $self = $class->new($ctx, @args);
 
     my %args = (
         export_ok => $self->export_ok || '',
+        repos      => $self->repos,
+        repo_dir  => $self->repo_dir,
         $self->_has_whitelist ? (whitelist => $self->whitelist) : (),
-        repos => $self->repos,
-        repo_dir => $self->repo_dir,
-        vhost => $ctx->request->uri->host,
         %{ $self->args }
     );
 
-    my $class = $self->class;
+    my $model_class = $self->class;
 
-    $ctx->log->debug("Building $class with " . join(", ", map { $_ . " => " . (defined($args{$_}) ? "'" . $args{$_}  . "'" : 'undef') } keys %args))
+    $ctx->log->debug("Building $model_class with " . join(", ", map { $_ . " => " . (defined($args{$_}) ? "'" . $args{$_}  . "'" : 'undef') } keys %args))
         if $ctx->debug;
-    my $model = $class->new(%args);
 
-    $ctx->log->debug("Using class '$class' " . $model->debug_string) if $ctx->debug;
+    my $model = $model_class->new(\%args);
+
+    $ctx->log->debug("Using class '$model_class' " . $model->debug_string) if $ctx->debug;
 
     return $model;
 }
index 73e31b3..a40fd03 100644 (file)
@@ -16,7 +16,6 @@ has repo_dir => (
 around run => sub {
     my $orig = shift;
     my $self = shift;
-    warn("Script repo dir" . $self->repo_dir);
     local $ENV{GITALIST_REPO_DIR} = $self->repo_dir;
     $self->$orig(@_);
 };
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime b/t/lib/repositories_sets/catgit/Catalyst-Runtime
deleted file mode 160000 (submodule)
index 1516bf4..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 1516bf4a4e8b4ebc4e7b3bd07d42a68d7a6f406a
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/HEAD b/t/lib/repositories_sets/catgit/Catalyst-Runtime/HEAD
new file mode 100644 (file)
index 0000000..cb089cd
--- /dev/null
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/config b/t/lib/repositories_sets/catgit/Catalyst-Runtime/config
new file mode 100644 (file)
index 0000000..07d359d
--- /dev/null
@@ -0,0 +1,4 @@
+[core]
+       repositoryformatversion = 0
+       filemode = true
+       bare = true
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/description b/t/lib/repositories_sets/catgit/Catalyst-Runtime/description
new file mode 100644 (file)
index 0000000..279a9ca
--- /dev/null
@@ -0,0 +1 @@
+A fake Catalyst repo based on repo1 for Vhost testing.
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/info/exclude b/t/lib/repositories_sets/catgit/Catalyst-Runtime/info/exclude
new file mode 100644 (file)
index 0000000..2c87b72
--- /dev/null
@@ -0,0 +1,6 @@
+# git-ls-files --others --exclude-from=.git/info/exclude
+# Lines that start with '#' are comments.
+# For a project mostly in C, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/branch1 b/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/branch1
new file mode 100644 (file)
index 0000000..8e0b31c
--- /dev/null
@@ -0,0 +1 @@
+0710a7c8ee11c73e8098d08f9384c2a839c65e4e
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/master b/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/heads/master
new file mode 100644 (file)
index 0000000..4d5b369
--- /dev/null
@@ -0,0 +1 @@
+d6ddf8b26be63066e01d96a0922c87cd8d6e2270
diff --git a/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/tags/0.01 b/t/lib/repositories_sets/catgit/Catalyst-Runtime/refs/tags/0.01
new file mode 100644 (file)
index 0000000..7c4a265
--- /dev/null
@@ -0,0 +1 @@
+36c6c6708b8360d7023e8a1649c45bcf9b3bd818
diff --git a/t/lib/repositories_sets/moose/Moose b/t/lib/repositories_sets/moose/Moose
deleted file mode 160000 (submodule)
index 5731b02..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 5731b027a3fcf44db136fb06def7c3049e8c7dc6
diff --git a/t/lib/repositories_sets/moose/Moose/HEAD b/t/lib/repositories_sets/moose/Moose/HEAD
new file mode 100644 (file)
index 0000000..cb089cd
--- /dev/null
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/t/lib/repositories_sets/moose/Moose/config b/t/lib/repositories_sets/moose/Moose/config
new file mode 100644 (file)
index 0000000..07d359d
--- /dev/null
@@ -0,0 +1,4 @@
+[core]
+       repositoryformatversion = 0
+       filemode = true
+       bare = true
diff --git a/t/lib/repositories_sets/moose/Moose/description b/t/lib/repositories_sets/moose/Moose/description
new file mode 100644 (file)
index 0000000..ee49016
--- /dev/null
@@ -0,0 +1 @@
+A fake Moose repo based on repo1 for Vhost testing.
diff --git a/t/lib/repositories_sets/moose/Moose/info/exclude b/t/lib/repositories_sets/moose/Moose/info/exclude
new file mode 100644 (file)
index 0000000..2c87b72
--- /dev/null
@@ -0,0 +1,6 @@
+# git-ls-files --others --exclude-from=.git/info/exclude
+# Lines that start with '#' are comments.
+# For a project mostly in C, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
diff --git a/t/lib/repositories_sets/moose/Moose/refs/heads/branch1 b/t/lib/repositories_sets/moose/Moose/refs/heads/branch1
new file mode 100644 (file)
index 0000000..8e0b31c
--- /dev/null
@@ -0,0 +1 @@
+0710a7c8ee11c73e8098d08f9384c2a839c65e4e
diff --git a/t/lib/repositories_sets/moose/Moose/refs/heads/master b/t/lib/repositories_sets/moose/Moose/refs/heads/master
new file mode 100644 (file)
index 0000000..4d5b369
--- /dev/null
@@ -0,0 +1 @@
+d6ddf8b26be63066e01d96a0922c87cd8d6e2270
diff --git a/t/lib/repositories_sets/moose/Moose/refs/tags/0.01 b/t/lib/repositories_sets/moose/Moose/refs/tags/0.01
new file mode 100644 (file)
index 0000000..7c4a265
--- /dev/null
@@ -0,0 +1 @@
+36c6c6708b8360d7023e8a1649c45bcf9b3bd818
index 5b0c601..4736dd5 100644 (file)
@@ -47,7 +47,8 @@ $mock_log->add_method($_ => sub {}) for qw/ warn info debug /;
 my $logger = $mock_log->name->new;
 $mock_ctx_meta->add_method('log' => sub { $logger });
 
-my $host = "example.gitalist.com";
+my $host = "git.shadowcat.co.uk";
+$mock_ctx_meta->add_method('uri' => sub { URI->new("http://$host/") });
 our $ctx_gen = sub {
     my ($cb, %args) = @_;
     my $ctx = $mock_ctx_meta->new_object(
@@ -63,6 +64,7 @@ our $ctx_gen = sub {
 };
 
 local %ENV = %ENV;
+delete $ENV{GITALIST_CONFIG};
 delete $ENV{GITALIST_REPO_DIR};
 
 throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; }
@@ -171,8 +173,8 @@ sub test_vhost_instance {
     });
 }
 
-my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime/.git";
-my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose/.git";
+my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime";
+my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose";
 {
     my $i = test_vhost_instance();
     is scalar($i->repositories->flatten), 2, 'Found 2 repos on test vhost';
@@ -212,9 +214,6 @@ sub test_with_config {
     ok $m, 'Has model';
     my $i = $m->ACCEPT_CONTEXT($ctx);
     ok $i, 'Has model instance from ACCEPT_CONTEXT';
-    isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
-    is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
-    isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
     return $i;
 }
 
diff --git a/t/scripts.t b/t/scripts.t
deleted file mode 100644 (file)
index bee86c3..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-use FindBin qw/$Bin/;
-BEGIN {
-    my $env = "$FindBin::Bin/../script/env";
-    if (-r $env) {
-        do $env or die $@;
-    }
-}
-
-
-use strict;
-use warnings;
-use Test::More;
-
-use_ok('Gitalist::Script::CGI');
-use_ok('Gitalist::Script::Server');
-use_ok('Gitalist::Script::FastCGI');
-
-# FIXME - Test the script role.
-
-done_testing;
-