Merge and fix some bugs
[catagits/Gitalist.git] / lib / Gitalist.pm
index 5213ef7..085456c 100644 (file)
@@ -6,13 +6,11 @@ use Catalyst::Runtime 5.80;
 
 extends 'Catalyst';
 
-use Catalyst qw/-Debug
+use Catalyst qw/
                 ConfigLoader
                 Static::Simple
                 StackTrace/;
 
-use Class::C3::Adopt::NEXT -no_warn;
-
 our $VERSION = '0.01';
 
 # Bring in the libified gitweb.cgi.
@@ -21,22 +19,50 @@ use gitweb;
 __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();
 
-sub uri_for {
-    my $p = ref $_[-1] eq 'HASH'
-          ? $_[-1]
-          : push(@_, {}) && $_[-1];
-    $p->{p} = $_[0]->model('Git')->project;
-
-    (my $uri = $_[0]->NEXT::uri_for(@_[1 .. $#_]))
-      # Ampersand! What is this, the 90s?
-      =~ s/&/;/g;
-    return $uri;
-}
+around uri_for => sub {
+  my ($orig, $c) = (shift, shift);
+  local $c->stash->{current_model}; # FIXME - for zts..
+  my $hash = ref($_[-1]) eq 'HASH' ? pop @_ : {};
+  my $params = Catalyst::Utils::merge_hashes(
+    { p => $hash->{p} || $c->model()->project },
+    $hash,
+  );
+  (my $uri = $c->$orig(@_, $params))
+    =~ tr[&][;];
+  return $uri;
+};
 
 =head1 NAME