Provided support for blobdiff_plain legacy URIs.
[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 requires 'base';
7
8 with qw/
9     Gitalist::URIStructure::WithLog
10 /;
11
12 after 'base' => sub {
13     my ($self, $c) = @_;
14     confess("No repository in the stash")
15         unless $c->stash->{Repository};
16 };
17
18 sub find : Chained('base') PathPart('') CaptureArgs(1) {
19     my ($self, $c, $sha1part) = @_;
20     # FIXME - Should not be here!
21     $c->stash->{Commit} = $c->stash->{Repository}->get_object_or_head($sha1part)
22         or $c->detach('/error404', "Couldn't find a object for '$sha1part' in XXXX!");
23 }
24
25 sub diff : Chained('find') CaptureArgs(0) {}
26
27 sub diff_fancy : Chained('diff') PathPart('') Args() {
28     my($self, $c, $comparison, @rest) = @_;
29     # FIXME - This ain't pretty
30     $c->stash(parent   => $comparison)
31       if $comparison;
32     $c->stash(filename => $rest[0])
33       if @rest;
34 }
35
36 sub diff_plain : Chained('diff') PathPart('plain') Args() {
37     my($self, $c, $comparison, @rest) = @_;
38     # FIXME - This ain't pretty
39     $c->stash(parent   => $comparison)
40       if $comparison;
41     $c->stash(filename => $rest[0])
42       if @rest;
43     $c->stash(no_wrapper => 1);
44     $c->response->content_type('text/plain; charset=utf-8');
45 }
46
47 sub commit : Chained('find') PathPart('commit') Args(0) {}
48
49 sub tree : Chained('find') Does('FilenameArgs') Args() {}
50
51 sub find_blob : Action {
52     my ($self, $c) = @_;
53     my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
54     # FIXME - Eugh!
55     my $h  = $object->isa('Gitalist::Git::Object::Commit')
56            ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
57            : $object->isa('Gitalist::Git::Object::Blob')
58              ? $object->sha1
59              : die "Unknown object type for '${\$object->sha1}'";
60     die "No file or sha1 provided."
61         unless $h;
62     $c->stash(blob => $repo->get_object($h)->content);
63 }
64
65 sub blob : Chained('find') Does('FilenameArgs') Args() {
66     my ($self, $c) = @_;
67     $c->forward('find_blob');
68 }
69
70 sub blame : Chained('find') Does('FilenameArgs') Args() {}
71
72 sub history : Chained('find') Does('FilenameArgs') Args() {}
73
74 1;