@@ -5,6 +5,8 @@ use warnings;
5
5
6
6
use MetaCPAN::Server::Diff;
7
7
use Moose;
8
+ use Try::Tiny;
9
+ use namespace::autoclean;
8
10
9
11
BEGIN { extends ' MetaCPAN::Server::Controller' }
10
12
@@ -16,47 +18,29 @@ sub index : Chained('/') : PathPart('diff') : CaptureArgs(0) {
16
18
# Diff two specific releases (/author/release/author/release).
17
19
sub diff_releases : Chained(' index' ) : PathPart(' release' ) : Args(4) {
18
20
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] );
21
21
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] ] );
45
24
}
46
25
47
26
# Only one distribution name specified: Diff latest with previous release.
48
27
sub release : Chained(' index' ) : PathPart(' release' ) : Args(1) {
49
28
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 };
56
37
}
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 ) );
60
44
}
61
45
62
46
# Diff two files (also works with directories).
@@ -67,23 +51,41 @@ sub file : Chained('index') : PathPart('file') : Args(2) {
67
51
= map { [ @$_ {qw( author release path) } ] }
68
52
map {
69
53
my $file = $_ ;
70
- eval { $c -> model(' CPAN::File' )-> inflate(0)-> get($file )-> {_source }; }
54
+ try { $c -> model(' CPAN::File' )-> inflate(0)-> get($file )-> {_source }; }
71
55
or $c -> detach(' /not_found' );
72
56
} ( $source , $target );
73
57
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
+
74
64
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
75
69
relative => $c -> model(' Source' )-> base_dir,
76
- source => $c -> model(' Source' )-> path(@$source_args ),
77
- target => $c -> model(' Source' )-> path(@$target_args ),
78
70
git => $c -> config-> {git }
79
71
);
80
72
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
+
81
83
$c -> stash(
82
84
{
83
- source => join ( q[ /] , @$source_args ),
84
- target => join ( q[ /] , @$target_args ),
85
+ source => join ( q[ /] , @$source ),
86
+ target => join ( q[ /] , @$target ),
85
87
statistics => $diff -> structured,
86
- diff => $diff -> raw,
88
+ $include_raw ? ( diff => $diff -> raw ) : () ,
87
89
}
88
90
);
89
91
}
0 commit comments