File Coverage

blib/lib/Pinto/Action/Diff.pm
Criterion Covered Total %
statement 43 43 100.0
branch 5 8 62.5
condition 3 6 50.0
subroutine 11 11 100.0
pod 0 1 0.0
total 62 69 89.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Show the difference between stacks or revisions
2              
3             package Pinto::Action::Diff;
4              
5 1     1   626 use Moose;
  1         3  
  1         9  
6 1     1   6458 use MooseX::Aliases;
  1         3  
  1         17  
7 1     1   20486 use MooseX::StrictConstructor;
  1         3  
  1         8  
8 1     1   9011 use MooseX::MarkAsMethods ( autoclean => 1 );
  1         2  
  1         10  
9 1     1   8600 use MooseX::Types::Moose qw(Bool Str);
  1         3  
  1         13  
10              
11 1     1   4491 use Pinto::Difference;
  1         2  
  1         19  
12 1     1   29 use Pinto::Constants qw(:color :diff);
  1         2  
  1         108  
13 1     1   172 use Pinto::Types qw(StackName StackDefault StackObject RevisionID DiffStyle);
  1         2  
  1         7  
14 1     1   6289 use Pinto::Util qw(throw default_diff_style);
  1         3  
  1         442  
15              
16             #------------------------------------------------------------------------------
17              
18             our $VERSION = '0.13'; # VERSION
19              
20             #------------------------------------------------------------------------------
21              
22             extends qw( Pinto::Action );
23              
24             #------------------------------------------------------------------------------
25              
26             has left => (
27             is => 'ro',
28             isa => StackName | StackObject | StackDefault | RevisionID,
29             default => undef,
30             );
31              
32             has right => (
33             is => 'ro',
34             isa => StackName | StackObject | RevisionID,
35             required => 1,
36             );
37              
38             has style => (
39             is => 'ro',
40             isa => DiffStyle,
41             alias => 'diff_style',
42             default => \&default_diff_style,
43             lazy => 1,
44             );
45              
46             has format => (
47             is => 'ro',
48             isa => Str,
49             builder => '_build_format',
50             );
51              
52             sub _build_format {
53 6     6   16 my ($self) = @_;
54              
55 6 50       162 return $self->style eq $PINTO_DIFF_STYLE_DETAILED
56             ? '%o[%F] %-40p %12v %a/%f'
57             : '%o[%F] %a/%f';
58             }
59              
60             #------------------------------------------------------------------------------
61              
62             sub execute {
63 7     7 0 20 my ($self) = @_;
64              
65 7         20 my $error_message = qq{"%s" does not match any stack or revision};
66              
67 7   33     181 my $left =
68             $self->repo->get_stack_maybe( $self->left )
69             || $self->repo->get_revision_maybe( $self->left )
70             || throw sprintf $error_message, $self->left;
71              
72 6   66     298 my $right =
73             $self->repo->get_stack_maybe( $self->right )
74             || $self->repo->get_revision_maybe( $self->right )
75             || throw sprintf $error_message, $self->right;
76              
77 4         198 my $diff = Pinto::Difference->new( left => $left,
78             right => $right,
79             style => $self->style );
80              
81             # TODO: Extract the colorizing & formatting code into a separate
82             # class that can be reused. Maybe subclassed for HTML and text.
83              
84 4 50       102 if ( $diff->is_different ) {
85 4         31 $self->show( "--- $left", { color => $PINTO_PALETTE_COLOR_1 } );
86 4         26 $self->show( "+++ $right", { color => $PINTO_PALETTE_COLOR_1 } );
87             }
88              
89 4         117 for my $entry ( $diff->entries ) {
90 8 100       30 my $color = $entry->is_addition ? $PINTO_PALETTE_COLOR_0 : $PINTO_PALETTE_COLOR_2;
91 8         238 my $string = $entry->to_string( $self->format );
92 8         604 $self->show( $string, { color => $color } );
93             }
94              
95 4 50       95 $self->notice('No difference') if not $diff->is_different;
96              
97 4         102 return $self->result;
98             }
99              
100             #-------------------------------------------------------------------------------
101              
102             __PACKAGE__->meta->make_immutable;
103              
104             #------------------------------------------------------------------------------
105              
106             1;
107              
108             __END__
109              
110             =pod
111              
112             =encoding UTF-8
113              
114             =for :stopwords Jeffrey Ryan Thalhammer
115              
116             =head1 NAME
117              
118             Pinto::Action::Diff - Show the difference between stacks or revisions
119              
120             =head1 VERSION
121              
122             version 0.13
123              
124             =head1 AUTHOR
125              
126             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut