blib/lib/Shipwright/Backend/SVK.pm

filter: Statementstmt 19.7% 26 / 132 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 42.1% 8 / 19 Podpod 100% 4 / 4 totaltotal 16.1% 38 / 236 CC 38 SCAR 53.3
CC
38
Cov
13%
_cmd
506.0
check_repository
42.0
_svnroot
20.0
CRAP 207.1
« blib/lib/Shipwright/Backend/Base.pm ↑ Summary blib/lib/Shipwright/Script/Delete.pm »
1 package Shipwright::Backend::SVK;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Spec::Functions qw/catfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Shipwright::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Copy::Recursive qw/rcopy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Path qw/remove_tree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our %REQUIRE_OPTIONS = ( import => [qw/source/] );
11  
12 3 ▶use base qw/Shipwright::Backend::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Shipwright::Backend::SVK - SVK repository backend
17  
18 =head1 SYNOPSIS
19  
20 shipwright create -r svk:/depot/shipwright/my_proj
21  
22 =head1 DESCRIPTION
23  
24 This module implements an L<SVK> based backend
25 for Shipwright L<repository|Shipwright::Manual::Glossary/repository>.
26  
27 =head1 ENVIRONMENT VARIABLES
28  
29 =over 4
30  
31 =item SHIPWRIGHT_SVK - path of F<svk> command, default value is F<svk>.
32  
33 =back
34  
35 L<Shipwright::Manual::ENV/SHIPWRIGHT_SVN> can be used as well.
36  
37 =head1 METHODS
38  
39 =over 4
40  
41 =item build
42  
43 =cut
44  
45 4 ▶sub build {
Subroutine build: called CC=1 SCAR=0.0
Pod documented
46 4 my $self = shift;
47 4 $self->strip_repository;
48 4 $self->SUPER::build(@_);
49 }
50  
51 =item initialize
52  
53 initialize a project.
54  
55 =cut
56  
57 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod documented
58 0 my $self = shift;
59 0 my $dir = $self->SUPER::initialize(@_);
60  
61 0 $self->delete; # clean repository in case it exists
62 0 $self->import(
63 source => $dir,
64 _initialize => 1,
65 comment => 'created project',
66 );
67 0 $self->_initialize_local_dir;
68 }
69  
70 0 ▶sub _svnroot {
Subroutine _svnroot: not called CC=4 SCAR=30.0
71 0 my $self = shift;
72 0 ▶ return $self->{svnroot} if $self->{svnroot};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"svnroot"} 0 0 0
73 0 my $depotmap = run_cmd( [ $ENV{'SHIPWRIGHT_SVK'} => depotmap => '--list' ] );
74 0 $depotmap =~ s{\A.*?^(?=/)}{}sm;
75 0 ▶ while ($depotmap =~ /^(\S*)\s+(.*?)$/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depotmap =~ /^(\S*)\s+(.*?)$/gm) 0 0 0
76 0 my ($depot, $svnroot) = ($1, $2);
77 0 ▶ if ($self->repository =~ /^$depot(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->repository =~ /^$depot(.*)/) 0 0 0
78 0 return $self->{svnroot} = "file://$svnroot/$1";
79 }
80 }
81 0 confess_or_die "Can't find determine underlying SVN repository for ". $self->repository;
82 }
83  
84 # a cmd generating factory
85 0 ▶sub _cmd {
Subroutine _cmd: not called CC=22 SCAR=62.3
86 0 my $self = shift;
87 0 my $type = shift;
88 0 my %args = @_;
89 0 ▶ $args{path} ||= '';
Condition0%0/2
$args{"path"} ||= ""
l!l
00
MC/DC0%0/1
$args{"path"} ||= ""
$args{"path"}
Truth table0%0/2
A
A= $args{"path"}
Aresult
11
00
90 0 ▶ $args{comment} ||= '';
Condition0%0/2
$args{"comment"} ||= ""
l!l
00
MC/DC0%0/1
$args{"comment"} ||= ""
$args{"comment"}
Truth table0%0/2
A
A= $args{"comment"}
Aresult
11
00
91  
92 0 for ( @{ $REQUIRE_OPTIONS{$type} } ) {
93 0 ▶ confess_or_die "$type need option $_" unless $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$_} 0 0 0
94 }
95  
96 0 my @cmd;
97  
98 0 ▶ if ( $type eq 'checkout' ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type eq 'checkout') { } 0 0 0
elsif ($type eq 'export') { } 0 0 0
elsif ($type eq 'list') { } 0 0 0
elsif ($type eq 'import') { } 0 0 0
elsif ($type eq 'commit') { } 0 0 0
elsif ($type eq 'delete') { } 0 0 0
elsif ($type eq 'move') { } 0 0 0
elsif ($type eq 'info') { } 0 0 0
elsif ($type eq 'cat') { } 0 0 0
99 0 ▶ if ( $args{detach} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detach'}) { } 0 0 0
100 0 @cmd = [ $ENV{'SHIPWRIGHT_SVK'}, 'checkout', '-d', $args{target} ];
101 }
102 else {
103 @cmd = [
104 $ENV{'SHIPWRIGHT_SVK'}, 'checkout',
105 $self->repository . $args{path}, $args{target}
106 0 ];
107 }
108 }
109 elsif ( $type eq 'export' ) {
110 @cmd = (
111 [
112 $ENV{'SHIPWRIGHT_SVN'}, 'export',
113 $self->_svnroot . $args{path}, $args{target}
114 0 ],
115 );
116 }
117 elsif ( $type eq 'list' ) {
118 0 @cmd = [ $ENV{'SHIPWRIGHT_SVN'}, 'list', $self->_svnroot . $args{path} ];
119 }
120 elsif ( $type eq 'import' ) {
121 0 ▶ if ( $args{_initialize} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'_initialize'}) { } 0 0 0
elsif ($args{'_extra_tests'}) { } 0 0 0
122 @cmd = [
123 $ENV{'SHIPWRIGHT_SVK'}, 'import', $args{source},
124 $self->repository . ( $args{path} || '' ),
125 '-m', $args{comment},
126 0 ▶ ];
Condition0%0/2
$args{'path'} || ''
l!l
00
MC/DC0%0/1
$args{'path'} || ''
$args{'path'}
Truth table0%0/2
A
A= $args{'path'}
Aresult
11
00
127 }
128 elsif ( $args{_extra_tests} ) {
129 @cmd = [
130 $ENV{'SHIPWRIGHT_SVK'}, 'import',
131 $args{source}, $self->repository . '/t/extra',
132 '-m', $args{comment},
133 0 ];
134 }
135 else {
136 0 my ( $path, $source );
137 0 ▶ if ( $args{build_script} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'build_script'}) { } 0 0 0
138 0 $path = "/scripts/$args{name}";
139 0 $source = $args{build_script};
140 }
141 else {
142 0 ▶ $path =
Branch: 1 branch
BranchTrueFalseTotal
$self->has_branch_support ? : 0 0 0
143 $self->has_branch_support
144 ? "/sources/$args{name}/$args{as}"
145 : "/dists/$args{name}";
146 0 $source = $args{source};
147 }
148  
149 0 ▶ if ( $self->info( path => $path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->info('path', $path)) { } 0 0 0
150 @cmd = (
151 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 $self->_sync_local_dir( $path );
153 0 remove_tree( $self->local_dir . $path );
154 0 rcopy( $source, $self->local_dir . $path, );
155 },
156 [
157 $ENV{'SHIPWRIGHT_SVK'}, 'commit',
158 '--import', $self->local_dir . $path,
159 '-m', $args{comment}
160 0 ],
161 );
162 }
163 else {
164 @cmd = [
165 $ENV{'SHIPWRIGHT_SVK'}, 'import',
166 $source, $self->repository . $path,
167 '-m', $args{comment},
168 0 ];
169 }
170 }
171  
172 }
173 elsif ( $type eq 'commit' ) {
174 @cmd = [
175 $ENV{'SHIPWRIGHT_SVK'},
176 'commit',
177 (
178 $args{import}
179 ? '--import'
180 : ()
181 ),
182 '-m',
183 $args{comment},
184 $args{path}
185 0 ▶ ];
Branch: 1 branch
BranchTrueFalseTotal
$args{'import'} ? : 0 0 0
186 }
187 elsif ( $type eq 'delete' ) {
188 @cmd = [
189 $ENV{'SHIPWRIGHT_SVK'}, 'delete',
190 '-m', 'delete repository',
191 $self->repository . $args{path},
192 0 ];
193 }
194 elsif ( $type eq 'move' ) {
195 @cmd = [
196 $ENV{'SHIPWRIGHT_SVK'},
197 'move',
198 '-m',
199 "move $args{path} to $args{new_path}",
200 $self->repository . $args{path},
201 $self->repository . $args{new_path}
202 0 ];
203 }
204 elsif ( $type eq 'info' ) {
205 0 @cmd = [ $ENV{'SHIPWRIGHT_SVK'}, 'info', $self->repository . $args{path} ];
206 }
207 elsif ( $type eq 'cat' ) {
208 0 @cmd = [ $ENV{'SHIPWRIGHT_SVN'}, 'cat', $self->_svnroot . $args{path} ];
209 }
210 else {
211 0 confess_or_die "invalid command: $type";
212 }
213  
214 0 return @cmd;
215 }
216  
217 0 ▶sub _yml {
Subroutine _yml: not called CC=3 SCAR=24.8
218 0 my $self = shift;
219 0 my $path = shift;
220 0 my $yml = shift;
221  
222 0 my $file = catfile( $self->local_dir . $path );
223  
224 0 ▶ if ($yml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yml) { } 0 0 0
225 0 ▶ if ( $path =~ /scripts/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /scripts/) { } 0 0 0
226 0 $self->_sync_local_dir('/scripts');
227 }
228 else {
229 0 $self->_sync_local_dir($path);
230 }
231 0 dump_yaml_file( $file, $yml );
232 0 $self->commit( path => $file, comment => "updated $path" );
233 }
234 else {
235 my ($out) = run_cmd(
236 0 [ $ENV{'SHIPWRIGHT_SVN'}, 'cat', $self->_svnroot . $path ] );
237 0 return load_yaml($out);
238 }
239 }
240  
241 =item info
242  
243 a wrapper around svk's info command.
244  
245 =cut
246  
247 0 ▶sub info {
Subroutine info: not called CC=3 SCAR=24.8
Pod documented
248 0 my $self = shift;
249 0 my ( $info, $err ) = $self->SUPER::info(@_);
250  
251 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
252 0 return $info, $err;
253 }
254 else {
255 0 ▶ return if $info =~ /not exist|not a checkout path/;
Branch: 1 branch
BranchTrueFalseTotal
if $info =~ /not exist|not a checkout path/ 0 0 0
256 0 return $info;
257 }
258 }
259  
260 =item check_repository
261  
262 check if the given repository is valid.
263  
264 =cut
265  
266 0 ▶sub check_repository {
Subroutine check_repository: not called CC=6 SCAR=37.4
Pod documented
267 0 my $self = shift;
268 0 my %args = @_;
269  
270 0 ▶ if ( $args{action} eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'action'} eq 'create') { } 0 0 0
271  
272 0 my $repo = $self->repository;
273 0 my ( $info, $err ) = $self->info;
274 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
275 0 $err =~ s{\s+$}{ };
276 0 $self->log->fatal( $err, "maybe root of $repo does not exist?" );
277 0 return;
278 }
279  
280 return 1
281 0 ▶ if $args{force} || $info =~ /not exist/ || $info =~ /Revision: 0/;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"force"} or $info =~ /not exist/ or $info =~ /Revision: 0/ 0 0 0
Condition0%0/6
$args{"force"} or $info =~ /not exist/ or $info =~ /Revision: 0/
l!l&&r!l&&!r
000
$args{"force"} or $info =~ /not exist/
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"force"} or $info =~ /not exist/ or $info =~ /Revision: 0/
$args{"force"} $info =~ /not exist/ $info =~ /Revision: 0/
Truth table0%0/4
A or B or C
A= $args{"force"}B= $info =~ /not exist/C= $info =~ /Revision: 0/
ABCresult
1XX1
01X1
0011
0000
282  
283 0 $self->log->fatal("$repo has commits already");
284 0 return;
285 }
286 else {
287 0 return $self->SUPER::check_repository(@_);
288 }
289 0 return;
290 }
291  
292 0 ▶sub _update_file {
Subroutine _update_file: not called CC=2 SCAR=17.9
293 0 my $self = shift;
294 0 my $path = shift;
295 0 my $latest = shift;
296  
297 0 my $file = $self->local_dir . $path;
298 0 $self->_sync_local_dir( $path );
299  
300 0 ▶ rcopy( $latest, $file ) or confess_or_die "can't copy $latest to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rcopy($latest, $file) or confess_or_die("can't copy $latest to ${file}: $!") 0 0 0
301 0 $self->commit(
302 path => $file,
303 comment => "updated $path",
304 );
305 }
306  
307 0 ▶sub _update_dir {
Subroutine _update_dir: not called CC=2 SCAR=17.9
308 0 my $self = shift;
309 0 my $path = shift;
310 0 my $latest = shift;
311  
312 0 $self->_sync_local_dir( $path );
313 0 my $dir = $self->local_dir . $path;
314 0 remove_tree( $dir );
315 0 ▶ rcopy( $latest, $dir ) or confess_or_die "can't copy $latest to $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
rcopy($latest, $dir) or confess_or_die("can't copy $latest to ${dir}: $!") 0 0 0
316 0 $self->commit(
317 path => $dir,
318 comment => "updated $path",
319 import => 1,
320 );
321 }
322  
323 0 ▶sub _initialize_local_dir {
Subroutine _initialize_local_dir: not called CC=2 SCAR=17.9
324 0 my $self = shift;
325 # the 0 is very important, or it may results in recursion
326 0 my $target = $self->local_dir( 0 );
327 0 ▶ remove_tree( $target ) if -e $target;
Branch: 1 branch
BranchTrueFalseTotal
if -e $target 0 0 0
328  
329 run_cmd(
330 0 [ $ENV{'SHIPWRIGHT_SVK'}, 'checkout', $self->repository, $target ] );
331 0 return $target;
332 }
333  
334 0 ▶sub _sync_local_dir {
Subroutine _sync_local_dir: not called CC=2 SCAR=17.9
335 0 my $self = shift;
336 0 ▶ my $path = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
337  
338 run_cmd(
339 0 [ $ENV{'SHIPWRIGHT_SVK'}, 'update', $self->local_dir . $path ] );
340 }
341  
342 =back
343  
344 =cut
345  
346 3 1;
347  
348 __END__
« blib/lib/Shipwright/Backend/Base.pm ↑ Summary blib/lib/Shipwright/Script/Delete.pm »