Use the stashed project object for uri generation rather than the attribute in the...
[catagits/Gitalist.git] / lib / Gitalist.pm
index afad99f..edd3519 100644 (file)
@@ -20,18 +20,46 @@ __PACKAGE__->config(
     name => 'Gitalist',
     default_view => 'Default',
     default_model => 'Git', # Yes, we are going to be changing this.
+    # Set to 1 to make your fcgi die the request after you push :)
+    exit_at_end_of_request_if_updated => 0,
 );
 
+{
+    my $version;
+    my $get_version = sub {
+        my $gitdir = shift->path_to('.git');
+        my $version = qx{cat "$gitdir/`cut -d' ' -f2 .git/HEAD`"};
+        chomp $version;
+        return $version;
+    };
+
+    after setup_finalize => sub {
+        my $c = shift;
+        $version = $c->$get_version
+            if $c->config->{exit_at_end_of_request_if_updated};
+    };
+    after handle_request => sub {
+        my $c = shift;
+        if ($version) {
+            my $new = $c->$get_version;
+            exit 0 unless $new eq $version;
+        }
+    };
+}
+
+
 # Start the application
 __PACKAGE__->setup();
 
 around uri_for => sub {
   my ($orig, $c) = (shift, shift);
+  my $project_name = $c->stash->{'Project'} && $c->stash->{'Project'}->name;
   my $hash = ref($_[-1]) eq 'HASH' ? pop @_ : {};
   my $params = Catalyst::Utils::merge_hashes(
-    { p => $hash->{p} || $c->model()->project },
+    { p => $hash->{p} || $project_name },
     $hash,
   );
+  delete $params->{p} unless defined $params->{p} && length $params->{p};
   (my $uri = $c->$orig(@_, $params))
     =~ tr[&][;];
   return $uri;