Bumped version number and fixed tabs.
[catagits/Gitalist.git] / lib / Gitalist / URIStructure / Ref.pm
1 package Gitalist::URIStructure::Ref;
2 use MooseX::MethodAttributes::Role;
3 use Moose::Autobox;
4 use namespace::autoclean;
5
6 use Gitalist::Git::Types qw/SHA1/;
7
8 requires 'base';
9
10 with qw/
11     Gitalist::URIStructure::WithLog
12 /;
13
14 after 'base' => sub {
15     my ($self, $c) = @_;
16     confess("No repository in the stash")
17         unless $c->stash->{Repository};
18 };
19
20 sub find : Chained('base') PathPart('') CaptureArgs(1) {
21     my ($self, $c, $sha1part) = @_;
22     # FIXME - Should not be here!
23     $c->stash->{Commit} = $c->stash->{Repository}->get_object_or_head($sha1part)
24         or $c->detach('/error404', "Couldn't find a object for '$sha1part' in XXXX!");
25 }
26
27 sub diff : Chained('find') CaptureArgs(0) {}
28
29 sub _set_diff_args {
30     my($self, $c, @rest) = @_;
31
32     # FIXME - This ain't pretty
33     $c->stash(parent   => shift @rest)
34         if @rest == 2
35         # Check that the single arg is unlikely to be a path.
36         or @rest && to_SHA1($rest[0]) && $c->stash->{Repository}->get_object_or_head($rest[0]);
37     $c->stash(filename => $rest[-1])
38       if @rest;
39 }
40
41 sub diff_fancy : Chained('diff') PathPart('') Args() {
42     my($self, $c, @rest) = @_;
43
44     $self->_set_diff_args($c, @rest);
45  }
46
47 sub diff_plain : Chained('diff') PathPart('plain') Args() {
48     my($self, $c, $comparison, @rest) = @_;
49
50     $self->_set_diff_args($c, @rest);
51
52     $c->stash(no_wrapper => 1);
53     $c->response->content_type('text/plain; charset=utf-8');
54 }
55
56 sub commit : Chained('find') PathPart('commit') Args(0) {}
57
58 sub tree : Chained('find') Does('FilenameArgs') Args() {}
59
60 sub find_blob : Action {
61     my ($self, $c) = @_;
62     my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
63     # FIXME - Eugh!
64     my $h  = $object->isa('Gitalist::Git::Object::Commit')
65            ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
66            : $object->isa('Gitalist::Git::Object::Blob')
67              ? $object->sha1
68              : die "Unknown object type for '${\$object->sha1}'";
69     die "No file or sha1 provided."
70         unless $h;
71     $c->stash(blob => $repo->get_object($h)->content);
72 }
73
74 sub blob : Chained('find') Does('FilenameArgs') Args() {
75     my ($self, $c) = @_;
76     $c->forward('find_blob');
77 }
78
79 sub blame : Chained('find') Does('FilenameArgs') Args() {}
80
81 sub history : Chained('find') Does('FilenameArgs') Args() {}
82
83 1;