Skip to content

Commit db26b58

Browse files
committed
Make raw diff available from all diff endpoints
Move common logic a single method and just get the source/target in each endpoint. fixes #250.
1 parent 58ac5f7 commit db26b58

File tree

1 file changed

+42
-40
lines changed
  • lib/MetaCPAN/Server/Controller

1 file changed

+42
-40
lines changed

lib/MetaCPAN/Server/Controller/Diff.pm

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use warnings;
55

66
use MetaCPAN::Server::Diff;
77
use Moose;
8+
use Try::Tiny;
9+
use namespace::autoclean;
810

911
BEGIN { extends 'MetaCPAN::Server::Controller' }
1012

@@ -16,47 +18,29 @@ sub index : Chained('/') : PathPart('diff') : CaptureArgs(0) {
1618
# Diff two specific releases (/author/release/author/release).
1719
sub diff_releases : Chained('index') : PathPart('release') : Args(4) {
1820
my ( $self, $c, @path ) = @_;
19-
my $path1 = $c->model('Source')->path( $path[0], $path[1] );
20-
my $path2 = $c->model('Source')->path( $path[2], $path[3] );
2121

22-
my $diff = MetaCPAN::Server::Diff->new(
23-
source => $path1,
24-
target => $path2,
25-
git => $c->config->{git},
26-
27-
# use same dir prefix as source and target
28-
relative => $c->model('Source')->base_dir,
29-
);
30-
31-
my $ct = eval { $c->req->preferred_content_type };
32-
if ( $ct && $ct eq 'text/plain' ) {
33-
$c->res->content_type('text/plain');
34-
$c->res->body( $diff->raw );
35-
$c->detach;
36-
}
37-
38-
$c->stash(
39-
{
40-
source => join( '/', $path[0], $path[1] ),
41-
target => join( '/', $path[2], $path[3] ),
42-
statistics => $diff->structured,
43-
}
44-
);
22+
# Use author/release as top dirs for diff.
23+
$self->_do_diff( $c, [ $path[0], $path[1] ], [ $path[2], $path[3] ] );
4524
}
4625

4726
# Only one distribution name specified: Diff latest with previous release.
4827
sub release : Chained('index') : PathPart('release') : Args(1) {
4928
my ( $self, $c, $name ) = @_;
50-
my $release = eval {
51-
$c->model('CPAN::Release')->inflate(0)->find($name)->{_source};
52-
}
53-
or $c->detach('/not_found');
54-
my $with = eval {
55-
$c->model('CPAN::Release')->inflate(0)->predecessor($name)->{_source};
29+
30+
my ( $latest, $previous );
31+
try {
32+
$latest
33+
= $c->model('CPAN::Release')->inflate(0)->find($name)->{_source};
34+
$previous
35+
= $c->model('CPAN::Release')->inflate(0)->predecessor($name)
36+
->{_source};
5637
}
57-
or $c->detach('/not_found');
58-
$c->forward( 'diff_releases',
59-
[ @$with{qw(author name)}, @$release{qw(author name)} ] );
38+
catch {
39+
$c->detach('/not_found');
40+
};
41+
42+
$self->_do_diff( $c,
43+
( map { [ @$_{qw(author name)} ] } $previous, $latest ) );
6044
}
6145

6246
# Diff two files (also works with directories).
@@ -67,23 +51,41 @@ sub file : Chained('index') : PathPart('file') : Args(2) {
6751
= map { [ @$_{qw(author release path)} ] }
6852
map {
6953
my $file = $_;
70-
eval { $c->model('CPAN::File')->inflate(0)->get($file)->{_source}; }
54+
try { $c->model('CPAN::File')->inflate(0)->get($file)->{_source}; }
7155
or $c->detach('/not_found');
7256
} ( $source, $target );
7357

58+
$self->_do_diff( $c, $source_args, $target_args, 1 );
59+
}
60+
61+
sub _do_diff {
62+
my ( $self, $c, $source, $target, $include_raw ) = @_;
63+
7464
my $diff = MetaCPAN::Server::Diff->new(
65+
source => $c->model('Source')->path(@$source),
66+
target => $c->model('Source')->path(@$target),
67+
68+
# use same dir prefix as source and target
7569
relative => $c->model('Source')->base_dir,
76-
source => $c->model('Source')->path(@$source_args),
77-
target => $c->model('Source')->path(@$target_args),
7870
git => $c->config->{git}
7971
);
8072

73+
# As of Catalyst::TraitFor::Request::REST 1.17 this method will error
74+
# if request contains no content-type hints (undef not a Str).
75+
my $ct = try { $c->req->preferred_content_type };
76+
77+
if ( $ct && $ct eq 'text/plain' ) {
78+
$c->res->content_type('text/plain');
79+
$c->res->body( $diff->raw );
80+
$c->detach;
81+
}
82+
8183
$c->stash(
8284
{
83-
source => join( q[/], @$source_args ),
84-
target => join( q[/], @$target_args ),
85+
source => join( q[/], @$source ),
86+
target => join( q[/], @$target ),
8587
statistics => $diff->structured,
86-
diff => $diff->raw,
88+
$include_raw ? ( diff => $diff->raw ) : (),
8789
}
8890
);
8991
}

0 commit comments

Comments
 (0)