blib/lib/Pinto/Difference.pm

filter: Statementstmt 90.9% 80 / 88 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 2 totaltotal 86.8% 99 / 114 CC 7 SCAR 6.9
CC
7
Cov
89%
foreach
6.0
__ANON__
3.0
to_string
2.0
CRAP 2.0
« blib/lib/Pinto/Chrome.pm ↑ Summary blib/lib/Pinto/Initializer.pm »
1 # ABSTRACT: Compute difference between two revisions
2  
3 package Pinto::Difference;
4  
5 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use MooseX::Types::Moose qw(ArrayRef Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use MooseX::MarkAsMethods ( autoclean => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Pinto::DifferenceEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Pinto::Constants qw(:diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Pinto::Types qw(DiffStyle);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Pinto::Util qw(itis default_diff_style);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use overload ( q{""} => 'to_string' );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #------------------------------------------------------------------------------
18  
19 7 our $VERSION = '0.14'; # VERSION
20  
21 #------------------------------------------------------------------------------
22  
23 7 has left => (
24 is => 'ro',
25 isa => 'Pinto::Schema::Result::Revision',
26 required => 1,
27 );
28  
29 7 has right => (
30 is => 'ro',
31 isa => 'Pinto::Schema::Result::Revision',
32 required => 1,
33 );
34  
35 7 has entries => (
36 traits => [qw(Array)],
37 handles => { entries => 'elements' },
38 isa => ArrayRef ['Pinto::DifferenceEntry'],
39 builder => '_build_diffs',
40 init_arg => undef,
41 lazy => 1,
42 );
43  
44 has additions => (
45 traits => [qw(Array)],
46 handles => { additions => 'elements' },
47 isa => ArrayRef ['Pinto::DifferenceEntry'],
48 0 ▶ default => sub { [ grep { $_->op eq '+' } shift->entries ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 7 init_arg => undef,
50 lazy => 1,
51 );
52  
53 has deletions => (
54 traits => [qw(Array)],
55 handles => { deletions => 'elements' },
56 isa => ArrayRef ['Pinto::DifferenceEntry'],
57 0 ▶ default => sub { [ grep { $_->op eq '-' } shift->entries ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 7 init_arg => undef,
59 lazy => 1,
60 );
61  
62 has is_different => (
63 is => 'ro',
64 isa => Bool,
65 init_arg => undef,
66 5 ▶ default => sub { shift->entries > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 7 lazy => 1,
68 );
69  
70 7 has style => (
71 is => 'ro',
72 isa => DiffStyle,
73 default => \&default_diff_style,
74 );
75  
76 #------------------------------------------------------------------------------
77  
78 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
79 6 my $orig = shift;
80 6 my $class = shift;
81 6 my $args = $class->$orig(@_);
82  
83 # The left and right attributes can also be Stack objects.
84 # In those cases, we just use the head revision of the Stack
85  
86 6 for my $side (qw(left right)) {
87 12 ▶ if ( $args->{$side}->isa('Pinto::Schema::Result::Stack') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$side}->isa("Pinto::Schema::Result::Stack")) 3 9 12
88 3 $args->{$side} = $args->{$side}->head;
89 }
90 }
91  
92 6 return $args;
93 7 };
94  
95 #------------------------------------------------------------------------------
96  
97 6 ▶sub _build_diffs {
Subroutine _build_diffs: called CC=2 SCAR=7.0
98 6 my ($self) = @_;
99  
100 # We want to find the registrations that are "different" in either
101 # side. Two registrations are the same if they have the same values in
102 # the package, distribution, and is_pinned columns. So we use these
103 # columns to construct the keys of a hash. The value is the id of
104 # the registration. For a concise diff, we just use the distribution
105 # and is_pinned columns, which effectively groups the records so there
106 # is only one diff entry per distribution. In that case, the package
107 # referenced by the registration won't be meaningful.
108  
109 6 ▶ my @fields = $self->style eq $PINTO_DIFF_STYLE_DETAILED
Branch: 1 branch
BranchTrueFalseTotal
$self->style eq $Pinto::Difference::PINTO_DIFF_STYLE_DETAILED ? : 0 6 6
110 ? qw(distribution package is_pinned)
111 : qw(distribution is_pinned);
112  
113 2 ▶ my $cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 2 my $value = $_[0]->id;
115 2 my $key = join '|', map { $_[0]->get_column($_) } @fields;
116 2 return ( $key => $value );
117 6 };
118  
119 6 my $attrs = { select => [ 'id', @fields ] };
120 6 my %left = $self->left->registrations( {}, $attrs )->as_hash($cb);
121 6 my %right = $self->right->registrations( {}, $attrs )->as_hash($cb);
122  
123 # Now that we have hashes representing the left and right, we use
124 # the keys as "sets" and compute the difference between them. Keys
125 # present on the right but not on the left have been added. And
126 # those present on left but not on the right have been deleted.
127  
128 6 my @add_ids = @right{ grep { not exists $left{$_} } keys %right };
129 0 my @del_ids = @left{ grep { not exists $right{$_} } keys %left };
130  
131 # Now we have the ids of all the registrations that were added or
132 # deleted between the left and right revisions. We use those ids to
133 # requery the database and construct full objects for each of them.
134  
135 6 my @adds = $self->_create_entries( '+', $self->right, \@add_ids );
136 6 my @dels = $self->_create_entries( '-', $self->left, \@del_ids );
137  
138 # Strictly speaking, the registrations are an unordered list. But
139 # the diff is more readable if we group registrations together by
140 # distribution name.
141  
142 6 my @diffs = sort @dels, @adds;
143  
144 6 return \@diffs;
145 }
146  
147 #------------------------------------------------------------------------------
148  
149 12 ▶sub _create_entries {
Subroutine _create_entries: called CC=2 SCAR=6.9
150 12 my ( $self, $type, $side, $ids ) = @_;
151  
152 # The number of ids is potentially pretty big (1000's) and we
153 # can't use that many values in an IN clause. So we insert all
154 # those ids into a temporary table.
155  
156 12 my $tmp_tbl = "__diff_${$}__";
157 12 my $dbh = $self->right->result_source->schema->storage->dbh;
158 12 $dbh->do("CREATE TEMP TABLE $tmp_tbl (reg INTEGER NOT NULL)");
159  
160 12 my $sth = $dbh->prepare("INSERT INTO $tmp_tbl VALUES( ? )");
161 12 $sth->execute($_) for @{$ids};
162  
163 # Now fetch the actual Registration objects (with all their
164 # related objects) for each id in the temp table. Finally,
165 # map all the Registrations into DifferenceEntry objects.
166  
167 12 my $where = { 'me.id' => { in => \"SELECT reg from $tmp_tbl" } };
168 12 my $reg_rs = $side->registrations($where)->with_distribution->with_package;
169  
170 12 my @entries = map { Pinto::DifferenceEntry->new( op => $type,
171 registration => $_ ) } $reg_rs->all;
172  
173 12 $dbh->do("DROP TABLE $tmp_tbl");
174  
175 12 return @entries;
176 }
177  
178 #------------------------------------------------------------------------------
179  
180 0 ▶sub foreach {
Subroutine foreach: not called CC=2 SCAR=17.9
Pod undocumented
181 0 my ( $self, $cb ) = @_;
182  
183 0 $cb->($_) for $self->entries;
184  
185 0 return $self;
186 }
187  
188 #------------------------------------------------------------------------------
189  
190 2 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=7.0
Pod undocumented
191 2 my ($self) = @_;
192  
193 2 ▶ my $format = $self->style eq $PINTO_DIFF_STYLE_CONCISE
Branch: 1 branch
BranchTrueFalseTotal
$self->style eq $Pinto::Difference::PINTO_DIFF_STYLE_CONCISE ? : 2 0 2
194 ? '%o[%F] %a/%f'
195 : '';
196  
197 2 return join("\n", map {$_->to_string($format) } $self->entries) . "\n";
198 }
199  
200 #------------------------------------------------------------------------------
201  
202 7 __PACKAGE__->meta->make_immutable;
203  
204 #------------------------------------------------------------------------------
205 7 1;
206  
207 __END__
« blib/lib/Pinto/Chrome.pm ↑ Summary blib/lib/Pinto/Initializer.pm »