lib/Serge/Sync/Plugin/Base/VCS.pm

filter: Statementstmt 16.6% 31 / 186 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 35.7% 10 / 28 Podpod 0.0% 0 / 13 totaltotal 12.3% 41 / 333 SCAR 74.6
CC
49
Cov
11%
checkout_all
210.0
_update_from_vcs
210.0
validate_data
110.0
CRAP 1735.3
« lib/Serge/Engine/Plugin/transform.pm ↑ Summary lib/Serge/Command/sync.pm »
1 package Serge::Sync::Plugin::Base::VCS;
2 1 ▶use parent Serge::Plugin::Base, Serge::Interface::SysCmdRunner;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Sync::Util qw(get_directory_contents);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge::Util qw(normalize_path subst_macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub init {
11 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
12  
13 0 $self->SUPER::init(@_);
14  
15 0 $self->{initialize} = undef; # set to 1 externally to allow data directory initialization
16  
17 0 $self->merge_schema({
18 add_unversioned => 'BOOLEAN',
19 name => 'STRING',
20 email => 'STRING',
21 local_path => 'STRING',
22 commit_message => 'STRING',
23 remote_path => {
24 '' => 'STRING_OR_HASH',
25 '*' => 'STRING',
26 }
27 });
28 }
29  
30 sub validate_data {
31 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=10 SCAR=47.0
Pod undocumented
32  
33 0 $self->SUPER::validate_data;
34  
35 # Check job config validity
36  
37 map {
38 0 ▶ die "'$_' not defined" unless defined $self->{data}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{$_} 0 0 0
39 0 $self->{data}->{$_} = subst_macros($self->{data}->{$_});
40 0 ▶ die "'$_' evaluates to an empty value" if $self->{data}->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{$_} eq "" 0 0 0
41 } qw(local_path remote_path);
42  
43 0 $self->{data}->{local_path} = normalize_path($self->{parent}->abspath($self->{data}->{local_path}));
44  
45 0 ▶ $self->{data}->{commit_message} = 'Automatic commit of updated project files' unless defined $self->{data}->{commit_message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{"commit_message"} 0 0 0
46  
47 # 'path' can be either a scalar (string) or a hash { subdir => value },
48 # so normalizing scalar to a hash with just one key/value pair
49 0 my $paths = $self->{data}->{remote_path};
50 0 ▶ if (ref(\$paths) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$paths eq "SCALAR") 0 0 0
51 0 $paths = $self->{data}->{remote_path} = {'' => $paths};
52 }
53  
54 # do not allow deeply nested subdirectory structure, since this would complicate directory structure cleanup
55 # so make sure each key contains only latin characters, digits, underscore, hyphen or dot
56 # (sans directory names like '.' or '..')
57 0 foreach (keys %$paths) {
58 0 ▶ die "Not a valid subdirectory name: '$_'" if ($_ ne '') && ($_ !~ m/^[\w\.\-]+$/ || $_ =~ m/^\.+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "" and !($_ =~ /^[\w\.\-]+$/) || $_ =~ /^\.+$/ 0 0 0
Condition0%0/6
$_ ne "" and !($_ =~ /^[\w\.\-]+$/) || $_ =~ /^\.+$/
!ll&&!rl&&r
000
not $_ =~ /^[\w\.\-]+$/ or $_ =~ /^\.+$/
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ ne "" and !($_ =~ /^[\w\.\-]+$/) || $_ =~ /^\.+$/
$_ ne "" not $_ =~ /^[\w\.\-]+$/ $_ =~ /^\.+$/
Truth table0%0/4
A and not(B) or C
A= $_ ne ""B= not $_ =~ /^[\w\.\-]+$/C= $_ =~ /^\.+$/
ABCresult
0XX0
1000
11X1
1011
59 }
60  
61 # Expand macros (environment variables) in paths
62 0 foreach (keys %$paths) {
63 0 $self->{data}->{remote_path}->{$_} = subst_macros($self->{data}->{remote_path}->{$_});
64 }
65 }
66  
67 sub checkout_all {
68 0 ▶ my $self = shift;
Subroutine checkout_all: not called CC=14 SCAR=53.5
Pod undocumented
69  
70 0 my $paths = $self->{data}->{remote_path};
71  
72 0 my $data_path = $self->{data}->{local_path};
73  
74 0 $self->_make_directory_on_initialize($data_path);
75  
76 0 my $init_errors_found = undef;
77  
78 # if the root project data directory is not under VCS control,
79 # see if it contains some random (old) data, and if yes, clean it up
80 0 ▶ if (!exists $paths->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $paths->{""}) 0 0 0
81 0 my $local_path = $data_path;
82 0 my @a = get_directory_contents($local_path);
83  
84 # delete files and directories that shouldn't be there
85 0 foreach my $path (@a) {
86 0 my $fullpath = $data_path.'/'.$path;
87 0 ▶ if (-f $fullpath) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $fullpath) { } 0 0 0
elsif (-d $fullpath and not exists $paths->{$path}) { } 0 0 0
88 # allow Apple-specific file to reside in the directory
89 0 ▶ next if $path eq '.DS_Store';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq ".DS_Store" 0 0 0
90  
91 0 ▶ if ($self->{initialize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'initialize'}) { } 0 0 0
92 0 print "Deleting file '$fullpath'\n";
93 0 unlink($fullpath);
94 } else {
95 0 print "ERROR: file '$fullpath' should not be present in the local project folder\n";
96 0 $init_errors_found = 1;
97 }
98 ▶ } elsif (-d $fullpath && !exists $paths->{$path}) {
Condition0%0/3
-d $fullpath and not exists $paths->{$path}
!ll&&!rl&&r
000
MC/DC0%0/2
-d $fullpath and not exists $paths->{$path}
-d $fullpath not exists $paths->{$path}
Truth table0%0/3
A and not(B)
A= -d $fullpathB= not exists $paths->{$path}
ABresult
0X0
100
111
99 0 ▶ if ($self->{initialize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'initialize'}) { } 0 0 0
100 0 $self->_delete_directory($fullpath);
101 } else {
102 0 print "ERROR: directory '$fullpath' should not be present in the local project folder\n";
103 0 $init_errors_found = 1;
104 }
105 }
106 }
107 }
108  
109 # then go through each subfolder
110  
111 0 my $n = 0;
112 0 my $total = scalar keys %$paths;
113 0 foreach my $key (sort keys %$paths) {
114 0 ▶ last if $init_errors_found;
Branch: 1 branch
BranchTrueFalseTotal
if $init_errors_found 0 0 0
115 0 ▶ print "[", ++$n, " of $total]: /$key\n" if $total > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $total > 1 0 0 0
116 0 my $local_path = $data_path.'/'.$key;
117 0 $self->_make_directory_on_initialize($local_path);
118 0 my $remote_path = $paths->{$key};
119 0 ▶ $init_errors_found ||= !$self->_update_from_vcs($local_path, $remote_path);
Condition0%0/3
$init_errors_found ||= !$self->_update_from_vcs($local_path, $remote_path)
l!l&&r!l&&!r
000
MC/DC0%0/2
$init_errors_found ||= !$self->_update_from_vcs($local_path, $remote_path)
$init_errors_found !$self->_update_from_vcs($local_path, $remote_path)
Truth table0%0/3
A ||= not(B)
A= $init_errors_foundB= !$self->_update_from_vcs($local_path, $remote_path)
ABresult
1X1
011
000
120 }
121  
122 0 ▶ if ($init_errors_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init_errors_found) 0 0 0
123 0 print "Use --initialize option to force update the directory structure\n";
124 0 print "\n";
125 0 die "Must initialize data before continuing\n";
126 }
127 }
128  
129 # get remote repository URL with an optional #branch_name suffix
130 sub get_remote_url {
131 0 ▶ my ($self, $local_path) = @_;
Subroutine get_remote_url: not called CC=1 SCAR=6.9
Pod undocumented
132 0 die "Please define your get_remote_url method";
133 }
134  
135 # return true value if plugin can switch branches without re-initializing the whole checkout
136 sub support_branch_switching {
137 0 ▶ die "Please define your support_branch_switching method";
Subroutine support_branch_switching: not called CC=1 SCAR=6.9
Pod undocumented
138 #return 1; # example
139 }
140  
141 # split the remothe path URL into path and branch
142 # (this needs to be implemented only if support_branch_switching returns true)
143 sub split_remote_path_branch {
144 0 ▶ my ($self, $remote_path) = @_;
Subroutine split_remote_path_branch: not called CC=1 SCAR=6.9
Pod undocumented
145  
146 0 die "Please define your split_remote_path_branch method";
147 #return ($remote_path, $branch); # example
148 }
149  
150 # split the remothe path URL into path and branch
151 # (this needs to be implemented only if support_branch_switching returns true)
152 sub switch_to_branch {
153 0 ▶ my ($self, $local_path, $branch) = @_;
Subroutine switch_to_branch: not called CC=1 SCAR=6.9
Pod undocumented
154 0 die "Please define your switch_to_branch method";
155 }
156  
157 # init local repository (checkout)
158 sub init_repo {
159 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine init_repo: not called CC=1 SCAR=6.9
Pod undocumented
160 0 die "Please define your init_repo method";
161 }
162  
163 # add all unversioned files and directories in local repository
164 sub add_unversioned {
165 0 ▶ my ($self, $local_path) = @_;
Subroutine add_unversioned: not called CC=1 SCAR=6.9
Pod undocumented
166 0 die "Please define your add_unversioned method";
167 }
168  
169 # remove all unversioned files and directories in local repository
170 sub delete_unversioned {
171 0 ▶ my ($self, $local_path) = @_;
Subroutine delete_unversioned: not called CC=1 SCAR=6.9
Pod undocumented
172 0 die "Please define your delete_unversioned method";
173 }
174  
175 # checkout (pull) changes from remote repository;
176 # this code should deal with fixing the state of the repository,
177 # abandoning local changes, resolving conflicts
178 sub checkout {
179 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine checkout: not called CC=1 SCAR=6.9
Pod undocumented
180 0 die "Please define your checkout method";
181 }
182  
183 # commit (push) changes to remote repository;
184 # this code should try to merge the remote changes and avoid conflicts
185 sub commit {
186 0 ▶ my ($self, $local_path, $original_remote_path, $message) = @_;
Subroutine commit: not called CC=1 SCAR=6.9
Pod undocumented
187 0 die "Please define your commit method";
188 }
189  
190 sub _update_from_vcs {
191 0 ▶ my ($self, $local_path, $expected_url) = @_;
Subroutine _update_from_vcs: not called CC=14 SCAR=53.5
192  
193 0 ▶ die "expected URL must not be blank" unless $expected_url ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $expected_url ne "" 0 0 0
194  
195 0 my $current_url = $self->get_remote_url($local_path);
196  
197 0 my ($expected_branch, $current_branch);
198  
199 0 ▶ if ($self->support_branch_switching) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->support_branch_switching) 0 0 0
200 0 ($expected_url, $expected_branch) = $self->split_remote_path_branch($expected_url);
201 0 ($current_url, $current_branch) = $self->split_remote_path_branch($current_url);
202 }
203  
204 0 ▶ if ($expected_url ne $current_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_url ne $current_url) 0 0 0
205 0 $self->_show_url_differs_error($local_path, $expected_url, $current_url, $self->{initialize});
206 0 ▶ return undef unless $self->{initialize};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"initialize"} 0 0 0
207  
208 0 $self->_delete_directory($local_path);
209 0 $self->_make_directory($local_path);
210  
211 0 ▶ if ($expected_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_url) 0 0 0
212 0 $self->init_repo($local_path, $expected_url, $expected_branch);
213 }
214  
215 0 $current_url = $self->get_remote_url($local_path);
216 0 ▶ if ($self->support_branch_switching) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->support_branch_switching) 0 0 0
217 0 ($current_url, $current_branch) = $self->split_remote_path_branch($current_url);
218 }
219  
220 0 ▶ if ($expected_url ne $current_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_url ne $current_url) 0 0 0
221 0 die "Failed to init repository. Currently reported URL differs: '$current_url'\n";
222 }
223  
224 0 ▶ if ($self->support_branch_switching && ($expected_branch ne $current_branch)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->support_branch_switching and $expected_branch ne $current_branch) 0 0 0
Condition0%0/3
$self->support_branch_switching and $expected_branch ne $current_branch
!ll&&!rl&&r
000
MC/DC0%0/2
$self->support_branch_switching and $expected_branch ne $current_branch
$self->support_branch_switching $expected_branch ne $current_branch
Truth table0%0/3
A and B
A= $self->support_branch_switchingB= $expected_branch ne $current_branch
ABresult
0X0
100
111
225 0 die "Failed to init repository. Currently reported branch differs: '$current_branch'\n";
226 }
227  
228 0 return 1; # ok
229 }
230  
231 # do cleanup and checkout before switching branches to make sure
232 # we can get the up-to-date list of remote branches
233  
234 0 $self->delete_unversioned($local_path);
235  
236 # note that here we checkout the currently reported url/branch first;
237 # VCS plugins that don't support branching will not get to this point if their URL differs,
238 # since they will do a full re-initialization of the local copy
239  
240 0 $self->checkout($local_path, $current_url, $current_branch);
241  
242 0 ▶ if ($self->support_branch_switching && ($expected_branch ne $current_branch)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->support_branch_switching and $expected_branch ne $current_branch) 0 0 0
Condition0%0/3
$self->support_branch_switching and $expected_branch ne $current_branch
!ll&&!rl&&r
000
MC/DC0%0/2
$self->support_branch_switching and $expected_branch ne $current_branch
$self->support_branch_switching $expected_branch ne $current_branch
Truth table0%0/3
A and B
A= $self->support_branch_switchingB= $expected_branch ne $current_branch
ABresult
0X0
100
111
243 0 print "Switching to branch '$expected_branch'\n";
244 0 $self->switch_to_branch($local_path, $expected_branch);
245 0 ($current_url, $current_branch) = $self->split_remote_path_branch($self->get_remote_url($local_path));
246 0 ▶ if ($expected_url ne $current_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_url ne $current_url) 0 0 0
247 0 die "Failed to switch to branch '$expected_branch'. Currently reported URL differs: '$current_url'\n";
248 }
249 0 ▶ if ($expected_branch ne $current_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_branch ne $current_branch) 0 0 0
250 0 die "Failed to switch to branch '$expected_branch'. Currently reported branch differs: '$current_branch'\n";
251 }
252 }
253  
254 0 return 1; # ok
255 }
256  
257 sub _show_url_differs_error {
258 0 ▶ my ($self, $local_path, $expected_url, $current_url, $initialize) = @_;
Subroutine _show_url_differs_error: not called CC=2 SCAR=17.9
259  
260 0 ▶ my $status_prefix = $initialize ? 'WARNING' : 'ERROR';
Branch: 1 branch
BranchTrueFalseTotal
$initialize ? : 0 0 0
261  
262 0 print "\n";
263 0 print "***************************************************************\n";
264 0 print "$status_prefix: expected remote repository URL does not match the reported one.\n";
265 0 print "Directory : $local_path\n";
266 0 print "Reported URL : $current_url\n";
267 0 print "Expected URL : $expected_url\n";
268 0 print "***************************************************************\n";
269 0 print "\n";
270 }
271  
272 sub commit_all {
273 0 ▶ my ($self, $message) = @_;
Subroutine commit_all: not called CC=6 SCAR=37.4
Pod undocumented
274  
275 0 my $paths = $self->{data}->{remote_path};
276  
277 0 my $n = 0;
278 0 my $total = scalar keys %$paths;
279 0 foreach my $key (sort keys %$paths) {
280 0 ▶ print "[", ++$n, " of $total]: /$key\n" if $total > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $total > 1 0 0 0
281 0 my $local_path = normalize_path($self->{data}->{local_path}.'/'.$key);
282 0 my $remote_path = $paths->{$key};
283  
284 0 my $current_url = $self->get_remote_url($local_path);
285  
286 0 ▶ if ($remote_path ne $current_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_path ne $current_url) 0 0 0
287 0 $self->_show_url_differs_error($local_path, $remote_path, $current_url);
288 0 die "Can't commit to an unknown repository; use 'serge pull' first to make sure the local repository checkout is properly initialized\n";
289 }
290  
291 0 ▶ if ($self->{data}->{add_unversioned}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'data'}{'add_unversioned'}) { } 0 0 0
292 0 $self->add_unversioned($local_path);
293 } else {
294 0 $self->delete_unversioned($local_path);
295 }
296 0 ▶ $self->commit($local_path, $remote_path, $message || $self->{data}->{commit_message});
Condition0%0/3
$message || $self->{'data'}{'commit_message'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$message || $self->{'data'}{'commit_message'}
$message $self->{'data'}{'commit_message'}
Truth table0%0/3
A || B
A= $messageB= $self->{'data'}{'commit_message'}
ABresult
1X1
011
000
297 }
298 }
299  
300 sub _make_directory_on_initialize {
301 0 ▶ my ($self, $path) = @_;
Subroutine _make_directory_on_initialize: not called CC=3 SCAR=24.8
302  
303 0 ▶ if (!-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $path) 0 0 0
304 0 ▶ if ($self->{initialize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'initialize'}) { } 0 0 0
305 0 $self->_make_directory($path);
306 } else {
307 0 print "\n";
308 0 print "***************************************************************\n";
309 0 print "ERROR: target data directory does not exist\n";
310 0 print "Directory: $path\n";
311 0 print "***************************************************************\n";
312 0 print "\n";
313 0 print "Use 'serge pull --initialize <config_file>' command to create the target data directory and do the initial checkout\n";
314 0 print "\n";
315 0 die "Must initialize the data before continuing\n";
316 }
317 }
318 }
319  
320 sub _make_directory {
321 0 ▶ my ($self, $path) = @_;
Subroutine _make_directory: not called CC=4 SCAR=30.0
322  
323 0 ▶ return if (-d $path); # directory already exists
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 0 0
324 0 ▶ die "'$path' points to a file, not a folder" if (-f $path);
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
325  
326 0 print "Creating directory '$path'\n";
327  
328 0 eval { mkpath($path) };
329 0 ▶ ($@) && die "Couldn't create $path: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Couldn't create ${path}: $@" 0 0 0
330 }
331  
332 sub _delete_directory {
333 0 ▶ my ($self, $path) = @_;
Subroutine _delete_directory: not called CC=3 SCAR=24.8
334  
335 0 ▶ die "'$path' points to a file, not a folder" if (-f $path);
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
336  
337 0 print "Deleting directory '$path'\n";
338  
339 0 eval { rmtree($path) };
340 0 ▶ ($@) && die "Couldn't delete $path: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Couldn't delete ${path}: $@" 0 0 0
341 }
342  
343 1 1;
« lib/Serge/Engine/Plugin/transform.pm ↑ Summary lib/Serge/Command/sync.pm »