Added commit object serialization.
[catagits/Gitalist.git] / lib / Gitalist / URIStructure / Ref.pm
CommitLineData
2980657b 1package Gitalist::URIStructure::Ref;
0da7966e 2use MooseX::MethodAttributes::Role;
b6ec181b 3use Moose::Autobox;
0da7966e 4use namespace::autoclean;
5
cbcd8397 6use Gitalist::Git::Types qw/SHA1/;
7
0da7966e 8requires 'base';
9
b9423b8e 10with qw/
11 Gitalist::URIStructure::WithLog
12/;
13
0da7966e 14after 'base' => sub {
15 my ($self, $c) = @_;
16 confess("No repository in the stash")
17 unless $c->stash->{Repository};
18};
19
20sub find : Chained('base') PathPart('') CaptureArgs(1) {
21 my ($self, $c, $sha1part) = @_;
22 # FIXME - Should not be here!
dcb1b927 23 $c->stash->{Commit} = $c->stash->{Repository}->get_object_or_head($sha1part)
0da7966e 24 or $c->detach('/error404', "Couldn't find a object for '$sha1part' in XXXX!");
53bb75da 25 $c->stash->{data} = $c->stash->{Commit};
0da7966e 26}
27
d361d955 28sub diff : Chained('find') CaptureArgs(0) {}
29
cbcd8397 30sub _set_diff_args {
31 my($self, $c, @rest) = @_;
32
7998de12 33 # FIXME - This ain't pretty
cbcd8397 34 $c->stash(parent => shift @rest)
c4ed9eb3 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]);
cbcd8397 38 $c->stash(filename => $rest[-1])
7998de12 39 if @rest;
40}
d361d955 41
cbcd8397 42sub diff_fancy : Chained('diff') PathPart('') Args() {
43 my($self, $c, @rest) = @_;
44
45 $self->_set_diff_args($c, @rest);
46 }
47
65f0ead5 48sub diff_plain : Chained('diff') PathPart('plain') Args() {
49 my($self, $c, $comparison, @rest) = @_;
cbcd8397 50
51 $self->_set_diff_args($c, @rest);
52
65f0ead5 53 $c->stash(no_wrapper => 1);
54 $c->response->content_type('text/plain; charset=utf-8');
8254b171 55}
0da7966e 56
bec3aecb 57sub commit : Chained('find') PathPart('commit') Args(0) {}
0da7966e 58
9912b062 59sub file_commit_info : Chained('find') Does('FilenameArgs') Args() {}
60
b6ec181b 61sub tree : Chained('find') Does('FilenameArgs') Args() {}
0da7966e 62
ca06a177 63sub find_blob : Action {
b132dce6 64 my ($self, $c) = @_;
592fa490 65 my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
b132dce6 66 # FIXME - Eugh!
592fa490 67 my $h = $object->isa('Gitalist::Git::Object::Commit')
24450117 68 ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
69 : $object->isa('Gitalist::Git::Object::Blob')
592fa490 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);
b132dce6 75}
1f9a47c2 76
ca06a177 77sub blob : Chained('find') Does('FilenameArgs') Args() {
78 my ($self, $c) = @_;
79 $c->forward('find_blob');
80}
1f9a47c2 81
b6ec181b 82sub blame : Chained('find') Does('FilenameArgs') Args() {}
1f9a47c2 83
b6ec181b 84sub history : Chained('find') Does('FilenameArgs') Args() {}
85
0da7966e 861;