blib/lib/App/FileSync/Merge.pm

filter: Statementstmt 100% 133 / 133 Branchbran 93.7% 60 / 64 Conditioncond 79.0% 49 / 62 MC/DCmcdc 65.7% 23 / 35 Subroutinesub 100% 24 / 24 Podpod 100% 2 / 2 totaltotal 90.9% 291 / 320 CC 59 SCAR 23.6
CC
59
Cov
93%
plan
22.1
check_delete_limit
12.0
_conflict
9.5
CRAP 10.6
« blib/lib/App/FileSync/Snapshot.pm ↑ Summary blib/lib/App/FileSync/Transport/SSH.pm »
1 package App::FileSync::Merge;
2  
3 # The three-way merge: given what the client had at the last sync (the base),
4 # what it has now, and what the master has now, decide what moves where.
5 # Pure functions of their arguments; nothing here touches the disk.
6  
7 416 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 416 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 416 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 416 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 416 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 416 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 416 ▶use App::FileSync qw(conflict_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 416 ▶use App::FileSync::Args qw(named);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 416 ▶use App::FileSync::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 $ROLE_CLIENT $ROLE_REPLICA @ROLES $TYPE_FILE $TYPE_DIR
20 $MAX_DELETES $MAX_DELETE_PERCENT $MAX_PERCENT $MTIME_TOLERANCE
21 416 );
22 416 ▶use App::FileSync::I18N qw(message);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 416 ▶use version; our $VERSION = version->declare('0.002.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # max_delete_percent is a percentage of the repo's size.
27 416 Readonly::Scalar my $PERCENT => 100;
28  
29 # The comparisons below run several times per path in the tree, so they are
30 # lexical code references rather than wrapped private subs; see
31 # App::FileSync/LIMITATIONS.
32  
33 # Entry type, treating a missing "t" as a file.
34 3027 ▶my $type = sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
35 3027 my ($entry) = @_;
36 3027 ▶ return ($entry && defined $entry->{t} && length $entry->{t}) ? $entry->{t} : $TYPE_FILE;
Branch: 1 branch
BranchTrueFalseTotal
$entry && defined $entry->{'t'} && length $entry->{'t'} ? : 3027 0 3027
Condition33%2/6
$entry and defined $entry->{"t"} and length $entry->{"t"}
!ll&&!rl&&r
003027
$entry and defined $entry->{"t"}
!ll&&!rl&&r
003027
MC/DC0%0/3
$entry and defined $entry->{"t"} and length $entry->{"t"}
$entry defined $entry->{"t"} length $entry->{"t"}
Truth table25%1/4
A and B and C
A= $entryB= defined $entry->{"t"}C= length $entry->{"t"}
ABCresult
0XX0
10X0
1100
1111
37 416 };
38  
39 # Two entries describe the same content: both directories, or both files with
40 # the same hash. Both must be present: every caller checks that first, so an
41 # absent entry is never compared here.
42 903 ▶my $same = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
43 903 my ($left, $right) = @_;
44  
45 903 my $left_type = $type->($left);
46 903 ▶ return 0 unless $left_type eq $type->($right);
Branch: 1 branch
BranchTrueFalseTotal
unless $left_type eq &$type($right) 5 898 903
47 898 ▶ return 1 if $left_type eq $TYPE_DIR;
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq $App::FileSync::Merge::TYPE_DIR 23 875 898
48  
49 875 ▶ return (defined $left->{h} && defined $right->{h} && $left->{h} eq $right->{h}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $left->{'h'} && defined $right->{'h'} && $left->{'h'} eq $right->{'h'} ? : 595 280 875
Condition50%3/6
defined $left->{"h"} and defined $right->{"h"} and $left->{"h"} eq $right->{"h"}
!ll&&!rl&&r
0280595
defined $left->{"h"} and defined $right->{"h"}
!ll&&!rl&&r
00875
MC/DC33%1/3
defined $left->{"h"} and defined $right->{"h"} and $left->{"h"} eq $right->{"h"}
defined $left->{"h"} defined $right->{"h"} $left->{"h"} eq $right->{"h"}
Truth table50%2/4
A and B and C
A= defined $left->{"h"}B= defined $right->{"h"}C= $left->{"h"} eq $right->{"h"}
ABCresult
0XX0
10X0
1100
1111
50 416 };
51  
52 # A pull or push action for one path, carrying the hash the receiver must
53 # verify against.
54 471 ▶my $transfer = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 471 my ($verb, $path, $entry) = @_;
56 return {
57 a => $verb,
58 p => $path,
59 t => $type->($entry),
60 471 ▶ (defined $entry->{h} ? (h => $entry->{h}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->{'h'} ? : 457 14 471
61 };
62 416 };
63  
64 =encoding utf8
65  
66 =head1 NAME
67  
68 App::FileSync::Merge - three-way merge of base, client and master manifests
69  
70 =head1 SYNOPSIS
71  
72 my $plan = App::FileSync::Merge::plan(
73 base => $base, client => $client, master => $master,
74 client_id => 'laptop-1', role => 'client', now => time,
75 );
76 my $refusal = App::FileSync::Merge::check_delete_limit(
77 deletions => $plan->{deletions}, base_size => scalar(keys %$base),
78 );
79  
80 =head1 FUNCTIONS
81  
82 =head2 plan
83  
84 Purpose: decide, path by path, what a session must do.
85  
86 Args: C<base>, C<client>, C<master> (manifests: path =E<gt> entry, see
87 L<App::FileSync::Scan>), C<client_id>, C<role> (C<client> or C<replica>),
88 C<now> (the epoch used in conflict-copy names).
89  
90 Returns: a hashref C<{ actions, warnings, deletions, conflicts }>. Actions are
91 hashes with C<a> (pull, push, delete_local, delete_remote or conflict) and
92 C<p> (the path); deletions come first, so a type change never writes over a
93 directory. Warnings are translated sentences.
94  
95 Side Effects: none.
96  
97 Usage:
98  
99 my $plan = App::FileSync::Merge::plan(%manifests, client_id => $id);
100  
101 =head3 EXAMPLE
102  
103 my $plan = App::FileSync::Merge::plan(
104 base => {},
105 client => { 'a.txt' => { t => 'f', h => 'aa' } },
106 master => {},
107 );
108 print $plan->{actions}[0]{a}, "\n"; # push
109  
110 =head3 API SPECIFICATION
111  
112 =head4 INPUT
113  
114 {
115 base => { type => 'hashref', optional => 1 },
116 client => { type => 'hashref', optional => 1 },
117 master => { type => 'hashref', optional => 1 },
118 client_id => { type => 'string', optional => 1 },
119 role => { type => 'string', memberof => [ 'client', 'replica' ], optional => 1 },
120 now => { type => 'number', optional => 1 },
121 }
122  
123 =head4 OUTPUT
124  
125 {
126 type => 'hashref',
127 schema => {
128 actions => { type => 'arrayref' },
129 warnings => { type => 'arrayref' },
130 deletions => { type => 'integer', min => 0 },
131 conflicts => { type => 'integer', min => 0 },
132 },
133 }
134  
135 =head3 MESSAGES
136  
137 Warnings (returned, not raised):
138  
139 Key Meaning
140 merge.deleted_changed deleted on the client, changed on the master; master copy restored
141 merge.changed_deleted changed on the client, deleted on the master; client copy restored
142 merge.conflict_copy changed on both; loser kept as a conflict copy
143 merge.conflict_dir changed on both; an empty directory lost
144 merge.replica_overwritten a replica's local edit was overwritten
145 merge.replica_removed a replica's extra file was removed
146  
147 =head3 FORMAL SPECIFICATION
148  
149 B, A, M : PATH ⇸ ENTRY (base, client, master)
150 ∀ p ∈ dom B ∪ dom A ∪ dom M •
151 p ∉ dom A ∧ p ∉ dom M ⇒ no action
152 p ∉ dom A ∧ same(M p, B p) ⇒ delete_remote p
153 p ∉ dom A ∧ ¬same(M p, B p) ⇒ pull p
154 p ∉ dom M ∧ same(A p, B p) ⇒ delete_local p
155 p ∉ dom M ∧ ¬same(A p, B p) ⇒ push p
156 same(A p, M p) ⇒ no action
157 same(A p, B p) ⇒ pull p
158 same(M p, B p) ⇒ push p
159 otherwise ⇒ conflict p
160 role = replica ⇒ actions ⊆ {pull, delete_local}
161  
162 =head3 PSEUDOCODE
163  
164 if replica: mirror the master and return
165 for each path in base, client or master, sorted:
166 apply the table above, adding a warning where one side's change
167 is undone or a conflict copy is made
168 put deletions before transfers
169 count deletions and conflicts
170  
171 =cut
172  
173 283 ▶sub plan {
Subroutine plan: called CC=22 SCAR=31.0
Pod documented
174 283 my $args = named(\@_, {
175 base => { type => 'hashref', optional => 1 },
176 client => { type => 'hashref', optional => 1 },
177 master => { type => 'hashref', optional => 1 },
178 client_id => { type => 'string', optional => 1 },
179 role => { type => 'string', memberof => [@ROLES], optional => 1 },
180 now => { type => 'number', optional => 1 },
181 }, 'Merge::plan');
182  
183 283 ▶ my $base = $args->{base} || {};
Condition50%1/2
$args->{'base'} || {}
l!l
2830
MC/DC0%0/1
$args->{'base'} || {}
$args->{'base'}
Truth table50%1/2
A
A= $args->{'base'}
Aresult
11
00
184 283 ▶ my $client = $args->{client} || {};
Condition50%1/2
$args->{'client'} || {}
l!l
2830
MC/DC0%0/1
$args->{'client'} || {}
$args->{'client'}
Truth table50%1/2
A
A= $args->{'client'}
Aresult
11
00
185 283 ▶ my $master = $args->{master} || {};
Condition50%1/2
$args->{'master'} || {}
l!l
2830
MC/DC0%0/1
$args->{'master'} || {}
$args->{'master'}
Truth table50%1/2
A
A= $args->{'master'}
Aresult
11
00
186 283 ▶ my $client_id = defined $args->{client_id} ? $args->{client_id} : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'client_id'} ? : 283 0 283
187 283 ▶ my $now = defined $args->{now} ? $args->{now} : time;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'now'} ? : 282 1 283
188  
189 return _replica_plan($base, $client, $master)
190 283 ▶ if ($args->{role} || $ROLE_CLIENT) eq $ROLE_REPLICA;
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"role"} || $App::FileSync::Merge::ROLE_CLIENT) eq $App::FileSync::Merge::ROLE_REPLICA 29 254 283
Condition66%2/3
$args->{'role'} || $App::FileSync::Merge::ROLE_CLIENT
l!l&&r!l&&!r
259240
MC/DC0%0/2
$args->{'role'} || $App::FileSync::Merge::ROLE_CLIENT
$args->{'role'} $App::FileSync::Merge::ROLE_CLIENT
Truth table66%2/3
A || B
A= $args->{'role'}B= $App::FileSync::Merge::ROLE_CLIENT
ABresult
1X1
011
000
191  
192 254 my (@actions, @warnings);
193 254 my %paths = (%{$base}, %{$client}, %{$master});
194  
195 254 for my $path (sort keys %paths) {
196 971 my ($b, $a, $m) = ($base->{$path}, $client->{$path}, $master->{$path});
197 971 my %who = (path => $path, client => $client_id);
198  
199 # Gone on both sides: nothing to do, the base entry is dropped at commit.
200 971 ▶ next unless $a || $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $a or $m 1 970 971
Condition100%3/3
$a or $m
l!l&&r!l&&!r
7891811
MC/DC100%2/2
$a or $m
$a $m
Truth table100%3/3
A or B
A= $aB= $m
ABresult
1X1
011
000
201  
202 970 ▶ if (!$a || !$m) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $a && $m) { } 534 436 970
elsif (&$same($a, $m)) { } 330 106 436
elsif ($b and &$same($a, $b)) { } 6 100 106
elsif ($b and &$same($m, $b)) { } 11 89 100
Condition100%3/3
$a && $m
!ll&&!rl&&r
181353436
MC/DC100%2/2
$a && $m
$a $m
Truth table100%3/3
A && B
A= $aB= $m
ABresult
0X0
100
111
203 # One side lacks it: an unchanged survivor follows the deletion;
204 # a changed one wins, and the deletion is undone with a warning.
205 534 ▶ my ($have, $verb, $delete, $undo) = $a
Branch: 1 branch
BranchTrueFalseTotal
$a ? : 353 181 534
206 ? ($a, 'push', { a => 'delete_local', p => $path, t => $type->($a) }, 'merge.changed_deleted')
207 : ($m, 'pull', { a => 'delete_remote', p => $path }, 'merge.deleted_changed');
208  
209 534 ▶ if ($b && $same->($have, $b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b and &$same($have, $b)) { } 155 379 534
Condition100%3/3
$b and &$same($have, $b)
!ll&&!rl&&r
3745155
MC/DC100%2/2
$b and &$same($have, $b)
$b &$same($have, $b)
Truth table100%3/3
A and B
A= $bB= &$same($have, $b)
ABresult
0X0
100
111
210 155 push @actions, $delete;
211 } else {
212 379 push @actions, $transfer->($verb, $path, $have);
213 379 ▶ push @warnings, message($undo, \%who) if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 5 374 379
214 }
215 } elsif ($same->($a, $m)) {
216 330 next; # already converged
217 ▶ } elsif ($b && $same->($a, $b)) {
Condition100%3/3
$b and &$same($a, $b)
!ll&&!rl&&r
11896
MC/DC100%2/2
$b and &$same($a, $b)
$b &$same($a, $b)
Truth table100%3/3
A and B
A= $bB= &$same($a, $b)
ABresult
0X0
100
111
218 6 push @actions, $transfer->('pull', $path, $m);
219 ▶ } elsif ($b && $same->($m, $b)) {
Condition100%3/3
$b and &$same($m, $b)
!ll&&!rl&&r
117811
MC/DC100%2/2
$b and &$same($m, $b)
$b &$same($m, $b)
Truth table100%3/3
A and B
A= $bB= &$same($m, $b)
ABresult
0X0
100
111
220 11 push @actions, $transfer->('push', $path, $a);
221 } else {
222 89 my $resolution = _conflict($path, $a, $m, $client_id, $now);
223 89 push @actions, $resolution;
224 89 push @warnings, _conflict_warning($resolution, \%who);
225 }
226 }
227  
228 254 return _ordered(\@actions, \@warnings);
229 }
230  
231 =head2 check_delete_limit
232  
233 Purpose: guard against a misconfigured client (or an empty mount) wiping the
234 master.
235  
236 Args: C<deletions> (planned), C<base_size> (files known at the last sync),
237 C<max_deletes> (default 200; 0 disables), C<max_delete_percent> (default 10,
238 at most 100; 0 disables), C<force>.
239  
240 Returns: C<undef> when the plan is acceptable, or a translated sentence
241 explaining the refusal. Both limits are inclusive: with C<max_deletes> 5, a
242 plan may delete exactly 5 files and is refused at 6. The percentage limit
243 allows C<max_delete_percent> percent of C<base_size>, rounded down (10% of 25
244 known files allows 2 deletions), but never fewer than one, so a small repo
245 stays usable. A plan that deletes nothing is always acceptable.
246  
247 Side Effects: none.
248  
249 Usage:
250  
251 my $refusal = App::FileSync::Merge::check_delete_limit(%counts, force => $force);
252  
253 =head3 EXAMPLE
254  
255 my $why = App::FileSync::Merge::check_delete_limit(deletions => 300, base_size => 1000);
256 print "$why\n" if defined $why; # refusing to delete 300 files (limit is 200); ...
257  
258 =head3 API SPECIFICATION
259  
260 =head4 INPUT
261  
262 {
263 deletions => { type => 'integer', min => 0, optional => 1 },
264 base_size => { type => 'integer', min => 0, optional => 1 },
265 max_deletes => { type => 'integer', min => 0, optional => 1 },
266 max_delete_percent => { type => 'number', min => 0, max => 100, optional => 1 },
267 force => { type => 'boolean', optional => 1 },
268 }
269  
270 =head4 OUTPUT
271  
272 { type => 'string', optional => 1 }
273  
274 =head3 MESSAGES
275  
276 Key Meaning Resolution
277 merge.too_many over the absolute limit --force if intended
278 merge.too_many_percent over the percentage of known files --force if intended
279  
280 =head3 FORMAL SPECIFICATION
281  
282 allowed(n, k) ⇔ n = 0 ∨ force ∨ ((max = 0 ∨ n ≤ max) ∧ (pct = 0 ∨ k = 0 ∨ n ≤ max(1, ⌊k × pct / 100⌋)))
283 check(n, k) = ⊥ if allowed(n, k), reason otherwise
284  
285 =cut
286  
287 278 ▶sub check_delete_limit {
Subroutine check_delete_limit: called CC=12 SCAR=24.8
Pod documented
288 278 my $args = named(\@_, {
289 deletions => { type => 'integer', min => 0, optional => 1 },
290 base_size => { type => 'integer', min => 0, optional => 1 },
291 max_deletes => { type => 'integer', min => 0, optional => 1 },
292 max_delete_percent => { type => 'number', min => 0, max => $MAX_PERCENT, optional => 1 },
293 force => { type => 'boolean', optional => 1 },
294 }, 'Merge::check_delete_limit');
295  
296 277 ▶ my $deletions = $args->{deletions} || 0;
Condition100%2/2
$args->{'deletions'} || 0
l!l
91186
MC/DC100%1/1
$args->{'deletions'} || 0
$args->{'deletions'}
Truth table100%2/2
A
A= $args->{'deletions'}
Aresult
11
00
297 277 ▶ my $known = $args->{base_size} || 0;
Condition100%2/2
$args->{'base_size'} || 0
l!l
168109
MC/DC100%1/1
$args->{'base_size'} || 0
$args->{'base_size'}
Truth table100%2/2
A
A= $args->{'base_size'}
Aresult
11
00
298 277 ▶ my $max = defined $args->{max_deletes} ? $args->{max_deletes} : $MAX_DELETES;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'max_deletes'} ? : 272 5 277
299 277 ▶ my $percent = defined $args->{max_delete_percent} ? $args->{max_delete_percent} : $MAX_DELETE_PERCENT;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'max_delete_percent'} ? : 271 6 277
300  
301 # Never block a single deletion just because the repo is small: 10% of
302 # five files is zero, which would make the tool unusable.
303 277 my $allowed = int($known * $percent / $PERCENT);
304 277 ▶ $allowed = 1 if $allowed < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $allowed < 1 245 32 277
305  
306 277 my %args = (count => $deletions, known => $known, max => $max, percent => $percent);
307 # No limit refuses zero deletions: $allowed is at least one.
308 my $refusal =
309 $args->{force} ? undef
310 277 ▶ : ($max && $deletions > $max) ? message('merge.too_many', \%args)
Branch: 3 branches
BranchTrueFalseTotal
$args->{'force'} ? : 9 268 277
$max && $deletions > $max ? : 7 261 268
$percent && $known > 0 && $deletions > $allowed ? : 10 251 261
Condition100%9/9
$max and $deletions > $max
!ll&&!rl&&r
82537
$percent and $known > 0 and $deletions > $allowed
!ll&&!rl&&r
14210910
$percent and $known > 0
!ll&&!rl&&r
39103119
MC/DC100%2/2
$max and $deletions > $max
$max $deletions > $max
MC/DC100%3/3
$percent and $known > 0 and $deletions > $allowed
$percent $known > 0 $deletions > $allowed
Truth table100%3/3
A and B
A= $maxB= $deletions > $max
ABresult
0X0
100
111
Truth table100%4/4
A and B and C
A= $percentB= $known > 0C= $deletions > $allowed
ABCresult
0XX0
10X0
1100
1111
311 : ($percent && $known > 0 && $deletions > $allowed) ? message('merge.too_many_percent', \%args)
312 : undef;
313  
314 277 return $refusal;
315 }
316  
317 # _replica_plan
318 # Purpose: plan for a replica: mirror the master, never push.
319 # Entry: manifests for base, replica and master.
320 # Exit: returns the same shape as plan(); local edits are reported and
321 # then overwritten, local extras removed.
322 # Side effects: none.
323 29 ▶sub _replica_plan :Private {
Subroutine _replica_plan: called CC=8 SCAR=20.8
324 29 my ($base, $client, $master) = @_;
325  
326 29 my (@actions, @warnings);
327  
328 29 for my $path (sort keys %{$master}) {
329 189 my $have = $client->{$path};
330 189 ▶ next if $have && $same->($have, $master->{$path});
Branch: 1 branch
BranchTrueFalseTotal
if $have and &$same($have, $master->{$path}) 114 75 189
Condition100%3/3
$have and &$same($have, $master->{$path})
!ll&&!rl&&r
705114
MC/DC100%2/2
$have and &$same($have, $master->{$path})
$have &$same($have, $master->{$path})
Truth table100%3/3
A and B
A= $haveB= &$same($have, $master->{$path})
ABresult
0X0
100
111
331  
332 75 push @actions, $transfer->('pull', $path, $master->{$path});
333 push @warnings, message('merge.replica_overwritten', { path => $path })
334 75 ▶ if $have && $base->{$path} && !$same->($have, $base->{$path});
Branch: 1 branch
BranchTrueFalseTotal
if $have and $base->{$path} and not &$same($have, $base->{$path}) 2 73 75
Condition100%6/6
$have and $base->{$path} and not &$same($have, $base->{$path})
!ll&&!rl&&r
7122
$have and $base->{$path}
!ll&&!rl&&r
7014
MC/DC100%3/3
$have and $base->{$path} and not &$same($have, $base->{$path})
$have $base->{$path} not &$same($have, $base->{$path})
Truth table100%4/4
A and B and not(C)
A= $haveB= $base->{$path}C= not &$same($have, $base->{$path})
ABCresult
0XX0
10X0
1100
1111
335 }
336  
337 29 for my $path (sort grep { !$master->{$_} } keys %{$client}) {
338 41 push @actions, { a => 'delete_local', p => $path, t => $type->($client->{$path}) };
339 41 push @warnings, message('merge.replica_removed', { path => $path });
340 }
341  
342 29 return _ordered(\@actions, \@warnings);
343 416 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
344  
345 # _ordered
346 # Purpose: the plan result: deletions first, with counts.
347 # Entry: $actions and $warnings are arrayrefs.
348 # Exit: returns { actions, warnings, deletions, conflicts }.
349 # Side effects: none.
350 283 ▶sub _ordered :Private {
Subroutine _ordered: called CC=1 SCAR=0.0
351 283 my ($actions, $warnings) = @_;
352  
353 # Deletions run first so a type change never tries to write over a directory.
354 283 my @deletions = grep { $_->{a} =~ /\Adelete_/ } @{$actions};
355 283 my @transfers = grep { $_->{a} !~ /\Adelete_/ } @{$actions};
356  
357 return {
358 actions => [ @deletions, @transfers ],
359 warnings => $warnings,
360 deletions => scalar(@deletions),
361 283 conflicts => scalar(grep { $_->{a} eq 'conflict' } @transfers),
362 };
363 416 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
364  
365 # _conflict
366 # Purpose: resolve a path both sides changed.
367 # Entry: both entries exist and differ from each other and the base.
368 # Exit: returns a conflict action naming the winner and, for a losing
369 # file, the name to keep it under.
370 # Side effects: none.
371 89 ▶sub _conflict :Private {
Subroutine _conflict: called CC=9 SCAR=22.5
372 89 my ($path, $client_entry, $master_entry, $client_id, $now) = @_;
373  
374 89 my ($client_type, $master_type) = ($type->($client_entry), $type->($master_entry));
375  
376 # A file beats an empty directory; between two files the newer wins, and
377 # within the clock tolerance the master wins deterministically.
378 my $winner = $client_type ne $TYPE_FILE ? 'remote'
379 : $master_type ne $TYPE_FILE ? 'client'
380 89 ▶ : ($client_entry->{m} || 0) > ($master_entry->{m} || 0) + $MTIME_TOLERANCE ? 'client'
Branch: 3 branches
BranchTrueFalseTotal
$client_type ne $App::FileSync::Merge::TYPE_FILE ? : 0 89 89
$master_type ne $App::FileSync::Merge::TYPE_FILE ? : 1 88 89
($client_entry->{'m'} || 0) > ($master_entry->{'m'} || 0) + $App::FileSync::Merge::MTIME_TOLERANCE ? : 46 42 88
Condition50%2/4
$client_entry->{'m'} || 0
l!l
880
$master_entry->{'m'} || 0
l!l
880
MC/DC0%0/1
$client_entry->{'m'} || 0
$client_entry->{'m'}
MC/DC0%0/1
$master_entry->{'m'} || 0
$master_entry->{'m'}
Truth table50%1/2
A
A= $client_entry->{'m'}
Aresult
11
00
Truth table50%1/2
A
A= $master_entry->{'m'}
Aresult
11
00
381 : 'remote';
382  
383 89 ▶ my ($winner_entry, $loser) = $winner eq 'client'
Branch: 1 branch
BranchTrueFalseTotal
$winner eq 'client' ? : 47 42 89
384 ? ($client_entry, $master_entry)
385 : ($master_entry, $client_entry);
386 89 my $loser_type = $type->($loser);
387  
388 return {
389 a => 'conflict',
390 p => $path,
391 winner => $winner,
392 winner_type => $type->($winner_entry),
393 89 ▶ (defined $winner_entry->{h} ? (h => $winner_entry->{h}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $winner_entry->{'h'} ? : 89 0 89
$loser_type eq $App::FileSync::Merge::TYPE_FILE ? : 88 1 89
394 ($loser_type eq $TYPE_FILE ? (loser_name => conflict_name($path, $client_id, $now)) : ()),
395 loser_type => $loser_type,
396 };
397 416 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
398  
399 # _conflict_warning
400 # Purpose: the sentence that reports a conflict.
401 # Entry: $resolution is a conflict action; $who holds path and client.
402 # Exit: returns the translated warning.
403 # Side effects: none.
404 89 ▶sub _conflict_warning :Private {
Subroutine _conflict_warning: called CC=3 SCAR=11.0
405 89 my ($resolution, $who) = @_;
406  
407 89 my $master = message('merge.master');
408 my ($kept, $other) = $resolution->{winner} eq 'client'
409 ? ($who->{client}, $master)
410 89 ▶ : ($master, $who->{client});
Branch: 1 branch
BranchTrueFalseTotal
$resolution->{'winner'} eq 'client' ? : 47 42 89
411  
412 return message($resolution->{loser_name} ? 'merge.conflict_copy' : 'merge.conflict_dir', {
413 89 %{$who},
414 kept => $kept,
415 other => $other,
416 copy => $resolution->{loser_name},
417 89 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$resolution->{'loser_name'} ? : 88 1 89
418 416 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
419  
420 416 1;
421  
422 __END__
« blib/lib/App/FileSync/Snapshot.pm ↑ Summary blib/lib/App/FileSync/Transport/SSH.pm »