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