File Coverage

blib/lib/Pinto/Action/Revert.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 33 34 97.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Revert stack to a prior revision
2              
3             package Pinto::Action::Revert;
4              
5 1     1   577 use Moose;
  1         3  
  1         17  
6 1     1   6949 use MooseX::StrictConstructor;
  1         2  
  1         12  
7 1     1   3132 use MooseX::Types::Moose qw(Bool);
  1         3  
  1         20  
8 1     1   4560 use MooseX::MarkAsMethods ( autoclean => 1 );
  1         2  
  1         9  
9              
10 1     1   3451 use Pinto::Util qw(throw);
  1         2  
  1         72  
11 1     1   6 use Pinto::Types qw(RevisionID RevisionHead);
  1         1  
  1         9  
12              
13             #------------------------------------------------------------------------------
14              
15             our $VERSION = '0.14'; # VERSION
16              
17             #------------------------------------------------------------------------------
18              
19             extends qw( Pinto::Action );
20              
21             #------------------------------------------------------------------------------
22              
23             with qw( Pinto::Role::Committable );
24              
25             #------------------------------------------------------------------------------
26              
27             has revision => (
28             is => 'ro',
29             isa => RevisionID | RevisionHead,
30             default => undef,
31             coerce => 1,
32             );
33              
34             has force => (
35             is => 'ro',
36             isa => Bool,
37             default => 0,
38             );
39              
40             #------------------------------------------------------------------------------
41              
42             sub execute {
43             my ($self) = @_;
44              
45             # Remember that the Committable role has already moved the head
46             # forward to a new revision which is a duplicate of the last head.
47              
48             my $stack = $self->stack;
49             my $new_head = $stack->head;
50             my $old_head = ($new_head->parents)[0];
51              
52             my $rev = $self->revision
53             ? $self->repo->get_revision($self->revision)
54             : ($old_head->parents)[0];
55              
56             throw "Cannot revert past the root commit"
57             if not $rev;
58              
59             throw "Revision $rev is the head of stack $stack"
60             if $rev eq $old_head;
61              
62             throw "Revision $rev is not an ancestor of stack $stack"
63             if !$rev->is_ancestor_of($old_head) && !$self->force;
64              
65             $new_head->registrations_rs->delete;
66             $stack->duplicate_registrations(to => $new_head, from => $rev);
67              
68             # We must explicitly mark the stack as changed, snce we injected the
69             # registrations directly. But it is possible that the new state is
70             # exactly the same as the old state. So we use the diff to be sure.
71              
72             $stack->diff->is_different
73             ? $stack->mark_as_changed
74             : throw "Revision $rev is identical to the head of stack $stack";
75              
76             return 1;
77             }
78              
79             #------------------------------------------------------------------------------
80              
81             sub generate_message_title {
82 4     4 0 11 my ($self) = @_;
83              
84             # TODO: fix duplication...
85 4         82 my $stack = $self->stack;
86 4         67 my $new_head = $stack->head;
87 4         149 my $old_head = ($new_head->parents)[0];
88              
89 4 100       26329 my $rev = $self->revision
90             ? $self->repo->get_revision($self->revision)
91             : ($old_head->parents)[0];
92              
93 4         8154 return sprintf "Revert to %s: %s", $rev->uuid_prefix, $rev->message_title;
94             }
95              
96             #------------------------------------------------------------------------------
97              
98             __PACKAGE__->meta->make_immutable;
99              
100             #------------------------------------------------------------------------------
101              
102             1;
103              
104             __END__
105              
106             =pod
107              
108             =encoding UTF-8
109              
110             =for :stopwords Jeffrey Ryan Thalhammer
111              
112             =head1 NAME
113              
114             Pinto::Action::Revert - Revert stack to a prior revision
115              
116             =head1 VERSION
117              
118             version 0.14
119              
120             =head1 AUTHOR
121              
122             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut