Use the stashed project object for uri generation rather than the attribute in the...
[catagits/Gitalist.git] / lib / Gitalist.pm
1 package Gitalist;
2 use Moose;
3 use namespace::autoclean;
4
5 use Catalyst::Runtime 5.80;
6
7 extends 'Catalyst';
8
9 use Catalyst qw/
10                 ConfigLoader
11                 Static::Simple
12                 StackTrace/;
13
14 our $VERSION = '0.01';
15
16 # Bring in the libified gitweb.cgi.
17 use gitweb;
18
19 __PACKAGE__->config(
20     name => 'Gitalist',
21     default_view => 'Default',
22     default_model => 'Git', # Yes, we are going to be changing this.
23     # Set to 1 to make your fcgi die the request after you push :)
24     exit_at_end_of_request_if_updated => 0,
25 );
26
27 {
28     my $version;
29     my $get_version = sub {
30         my $gitdir = shift->path_to('.git');
31         my $version = qx{cat "$gitdir/`cut -d' ' -f2 .git/HEAD`"};
32         chomp $version;
33         return $version;
34     };
35
36     after setup_finalize => sub {
37         my $c = shift;
38         $version = $c->$get_version
39             if $c->config->{exit_at_end_of_request_if_updated};
40     };
41     after handle_request => sub {
42         my $c = shift;
43         if ($version) {
44             my $new = $c->$get_version;
45             exit 0 unless $new eq $version;
46         }
47     };
48 }
49
50
51 # Start the application
52 __PACKAGE__->setup();
53
54 around uri_for => sub {
55   my ($orig, $c) = (shift, shift);
56   my $project_name = $c->stash->{'Project'} && $c->stash->{'Project'}->name;
57   my $hash = ref($_[-1]) eq 'HASH' ? pop @_ : {};
58   my $params = Catalyst::Utils::merge_hashes(
59     { p => $hash->{p} || $project_name },
60     $hash,
61   );
62   delete $params->{p} unless defined $params->{p} && length $params->{p};
63   (my $uri = $c->$orig(@_, $params))
64     =~ tr[&][;];
65   return $uri;
66 };
67
68 =head1 NAME
69
70 Gitalist - Catalyst based application
71
72 =head1 SYNOPSIS
73
74     script/gitalist_server.pl
75
76 =head1 DESCRIPTION
77
78 [enter your description here]
79
80 =head1 SEE ALSO
81
82 L<Gitalist::Controller::Root>, L<Catalyst>
83
84 =head1 AUTHORS AND COPYRIGHT
85
86   Catalyst application:
87     (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
88
89   Original gitweb.cgi from which this was derived:
90     (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
91     (C) 2005, Christian Gierke
92
93 =head1 LICENSE
94
95 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
96
97 This library is free software. You can redistribute it and/or modify
98 it under the same terms as Perl itself.
99
100 =cut
101
102 1;