2536dc5fb1dab487025138f3040d25bbe852fc8b
[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     $c->stash->{data} = $c->stash->{Commit};
26 }
27
28 sub diff : Chained('find') CaptureArgs(0) {}
29
30 sub _set_diff_args {
31     my($self, $c, @rest) = @_;
32
33     # FIXME - This ain't pretty
34     $c->stash(parent   => shift @rest)
35         if @rest == 2
36         # Check that the single arg is unlikely to be a path.
37         or @rest && to_SHA1($rest[0]) && $c->stash->{Repository}->get_object_or_head($rest[0]);
38     $c->stash(filename => $rest[-1])
39       if @rest;
40 }
41
42 sub diff_fancy : Chained('diff') PathPart('') Args() {
43     my($self, $c, @rest) = @_;
44
45     $self->_set_diff_args($c, @rest);
46  }
47
48 sub diff_plain : Chained('diff') PathPart('plain') Args() {
49     my($self, $c, $comparison, @rest) = @_;
50
51     $self->_set_diff_args($c, @rest);
52
53     $c->stash(no_wrapper => 1);
54     $c->response->content_type('text/plain; charset=utf-8');
55 }
56
57 sub commit : Chained('find') PathPart('commit') Args(0) {}
58
59 sub file_commit_info : Chained('find') Does('FilenameArgs') Args() {}
60
61 sub tree : Chained('find') Does('FilenameArgs') Args() {}
62
63 sub find_blob : Action {
64     my ($self, $c) = @_;
65     my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
66     # FIXME - Eugh!
67     my $h  = $object->isa('Gitalist::Git::Object::Commit')
68            ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
69            : $object->isa('Gitalist::Git::Object::Blob')
70              ? $object->sha1
71              : die "Unknown object type for '${\$object->sha1}'";
72     die "No file or sha1 provided."
73         unless $h;
74     $c->stash(blob => $repo->get_object($h)->content);
75 }
76
77 sub blob : Chained('find') Does('FilenameArgs') Args() {
78     my ($self, $c) = @_;
79     $c->forward('find_blob');
80 }
81
82 sub blame : Chained('find') Does('FilenameArgs') Args() {}
83
84 sub history : Chained('find') Does('FilenameArgs') Args() {}
85
86 1;