blib/lib/Shipwright/Backend/Git.pm

filter: Statementstmt 21.7% 34 / 156 Branchbran 2.9% 1 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 39.2% 11 / 28 Podpod 100% 8 / 8 totaltotal 22.0% 54 / 245 CC 22 SCAR 23.8
CC
22
Cov
17%
build
30.0
check_repository
30.0
_init_new_git_repos
20.0
CRAP 10.8
« blib/lib/Shipwright/Source/SVN.pm ↑ Summary blib/lib/Shipwright/Script.pm »
1 package Shipwright::Backend::Git;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec::Functions qw/catfile catdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Shipwright::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Copy::Recursive qw/rcopy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Shipwright::Backend::FS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw/remove_tree make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our %REQUIRE_OPTIONS = ( import => [qw/source/] );
14  
15 1 ▶use base qw/Shipwright::Backend::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Shipwright::Backend::Git - Git repository backend
20  
21 =head1 SYNOPSIS
22  
23 shipwright create -r git:file:///home/me/shipwright/my_proj.git
24  
25 =head1 DESCRIPTION
26  
27 This module implements a Git based backend
28 for Shipwright L<repository|Shipwright::Manual::Glossary/repository>.
29  
30 =head1 ENVIRONMENT VARIABLES
31  
32 =over 4
33  
34 =item SHIPWRIGHT_GIT - path of F<git> command, default value is F<git>.
35  
36 =back
37  
38 =head1 METHODS
39  
40 =over 4
41  
42 =item build
43  
44 =cut
45  
46 0 ▶sub build {
Subroutine build: not called CC=5 SCAR=34.0
Pod documented
47 0 my $self = shift;
48 0 my %args = @_;
49 0 $self->SUPER::build(%args);
50 0 ▶ $self->strip_repository
Branch: 1 branch
BranchTrueFalseTotal
if $self->repository =~ m[^git:[a-z]+(?:\+[a-z]+)?://] 0 0 0
51 if $self->repository =~ m{^git:[a-z]+(?:\+[a-z]+)?://};
52 0 my $repo_path = $self->repository;
53 0 $repo_path =~ s!.*?://!!;
54 0 ▶ $self->_sync_local_dir if -e $repo_path && -e $self->local_dir && !$args{no_sync_local_dir};
Branch: 1 branch
BranchTrueFalseTotal
if -e $repo_path and -e $self->local_dir and not $args{"no_sync_local_dir"} 0 0 0
Condition0%0/6
-e $repo_path and -e $self->local_dir and not $args{"no_sync_local_dir"}
!ll&&!rl&&r
000
-e $repo_path and -e $self->local_dir
!ll&&!rl&&r
000
MC/DC0%0/3
-e $repo_path and -e $self->local_dir and not $args{"no_sync_local_dir"}
-e $repo_path -e $self->local_dir not $args{"no_sync_local_dir"}
Truth table0%0/4
A and B and not(C)
A= -e $repo_pathB= -e $self->local_dirC= not $args{"no_sync_local_dir"}
ABCresult
0XX0
10X0
1100
1111
55 0 return $self;
56 }
57  
58 =item initialize
59  
60 initialize a project.
61  
62 =cut
63  
64 0 ▶sub initialize {
Subroutine initialize: not called CC=2 SCAR=17.9
Pod documented
65 0 my $self = shift;
66  
67 0 my $dir = $self->SUPER::initialize(@_);
68  
69 0 my $path = $self->repository;
70 0 $path =~ s!^file://!!;
71  
72 0 ▶ run_cmd( sub { remove_tree( $path ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ run_cmd( sub { make_path( $path ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
74  
75 0 $self->_init_new_git_repos( $path );
76 0 $self->_initialize_local_dir();
77  
78 0 ▶ rcopy( $dir, $self->local_dir )
Branch: 1 branch
BranchTrueFalseTotal
rcopy($dir, $self->local_dir) or confess_or_die("can't copy $dir to ${path}: $!") 0 0 0
79 or confess_or_die "can't copy $dir to $path: $!";
80 0 $self->commit( comment => 'create project' );
81 }
82  
83 0 ▶sub _init_new_git_repos {
Subroutine _init_new_git_repos: not called CC=4 SCAR=30.0
84 0 my $self = shift;
85 0 my $new_repos_dir = shift;
86  
87 0 my $cwd = getcwd;
88  
89 # make a new bare repos at the target path
90 0 chdir $new_repos_dir;
91 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, '--bare', 'init' ] );
92  
93 my ($output) =
94 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, '--version' ] );
95 0 my ($version) = $output =~ /(\d+\.\d+\.\d+)/;
96 0 ▶ if ( $version && $version lt '1.6.2' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version and $version lt "1.6.2") 0 0 0
Condition0%0/3
$version and $version lt "1.6.2"
!ll&&!rl&&r
000
MC/DC0%0/2
$version and $version lt "1.6.2"
$version $version lt "1.6.2"
Truth table0%0/3
A and B
A= $versionB= $version lt "1.6.2"
ABresult
0X0
100
111
97  
98 ### git doesn't allow to clone an empty repo before 1.6.2
99 ### make a temporary non-bare repos to initialize the new bare
100 ### repos with, pushing from the regular repos to the bare one
101 0 my $dir =
102 tempdir( 'shipwright_backend_git_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
103  
104 0 chdir $dir;
105 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, 'init' ] );
106  
107 # touch a file in the non-bare repos
108 0 my $initial_file = '.shipwright_git_initial';
109 {
110 0 ▶ open my $f,
Branch: 1 branch
BranchTrueFalseTotal
open my $f, ">", $initial_file or confess_or_die("$! writing $dir/$initial_file") 0 0 0
111 '>', $initial_file
112 or confess_or_die "$! writing $dir/$initial_file"
113 }
114  
115 run_cmd(
116 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'add', $initial_file ] );
117 run_cmd(
118 [
119 0 $ENV{'SHIPWRIGHT_GIT'},
120 'commit',
121 -m => 'initial commit, shipwright creating new git repository'
122 ]
123 );
124 run_cmd(
125 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'push', $new_repos_dir, 'master' ] );
126  
127 }
128  
129 0 chdir $cwd;
130 0 return $new_repos_dir;
131 }
132  
133 0 ▶sub _initialize_local_dir {
Subroutine _initialize_local_dir: not called CC=2 SCAR=17.9
134 0 my $self = shift;
135 # the 0 is very important, or it may results in recursion
136 0 my $target = $self->local_dir( 0 );
137 0 ▶ remove_tree( $target ) if -e $target;
Branch: 1 branch
BranchTrueFalseTotal
if -e $target 0 0 0
138  
139 run_cmd(
140 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'clone', $self->repository, $target ] );
141 0 my $cwd = getcwd;
142 0 chdir $target;
143 # git 1.6.3.3 will warn if we don't specify push.default
144 run_cmd(
145 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'config', 'push.default', 'matching' ] );
146 0 chdir $cwd;
147 0 return $target;
148 }
149  
150 0 ▶sub _sync_local_dir {
Subroutine _sync_local_dir: not called CC=2 SCAR=17.9
151 0 my $self = shift;
152 0 my $target = $self->local_dir;
153 0 my $cwd = getcwd;
154 0 ▶ chdir $target or return;
Branch: 1 branch
BranchTrueFalseTotal
chdir $target or return 0 0 0
155  
156 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, 'pull' ] );
157 0 chdir $cwd;
158 }
159  
160  
161 =item check_repository
162  
163 check if the given repository is valid.
164  
165 =cut
166  
167 0 ▶sub check_repository {
Subroutine check_repository: not called CC=5 SCAR=34.0
Pod documented
168 0 my $self = shift;
169 0 my %args = @_;
170  
171 0 ▶ if ( $args{action} eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'action'} eq 'create') { } 0 0 0
172 0 my $repo = $self->repository;
173 0 ▶ if ( $repo =~ m{^file://(.*)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repo =~ m[^file://(.*)]) { } 0 0 0
174 0 ▶ if ( $args{force} || !-e $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"force"} or not -e $1) 0 0 0
Condition0%0/3
$args{"force"} or not -e $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"force"} or not -e $1
$args{"force"} not -e $1
Truth table0%0/3
A or not(B)
A= $args{"force"}B= not -e $1
ABresult
1X1
011
000
175 0 return 1;
176 }
177 0 $self->log->fatal("$repo exists already");
178 0 return;
179 }
180 else {
181 0 $self->log->fatal(
182 "git backend doesn't support remote repository");
183 0 return;
184 }
185 }
186 else {
187  
188 0 return $self->SUPER::check_repository(@_);
189 }
190 0 return;
191 }
192  
193 =item fs_backend
194  
195 git's local clone is nearly the same as a fs backend, this returns
196 a Shipwright::Backend::FS object which reflects the local_dir repository.
197  
198 =cut
199  
200 0 ▶sub fs_backend {
Subroutine fs_backend: not called CC=2 SCAR=17.9
Pod documented
201 0 my $self = shift;
202 0 ▶ return $self->{fs_backend} if $self->{fs_backend};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fs_backend"} 0 0 0
203 # XXX TODO not a great place to clone, need refactor
204 0 my $local_dir = $self->local_dir;
205 0 $self->{fs_backend} = Shipwright::Backend::FS->new(
206 repository => $self->local_dir,
207 );
208 0 return $self->{fs_backend};
209 }
210  
211 0 ▶sub _cmd {
Subroutine _cmd: not called CC=1 SCAR=6.9
212 0 my $self = shift;
213 0 return $self->fs_backend->_cmd(@_);
214 }
215  
216 0 ▶sub _yml {
Subroutine _yml: not called CC=2 SCAR=17.9
217 0 my $self = shift;
218 0 my $return = $self->fs_backend->_yml(@_);
219 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
220 0 $self->commit( comment => 'update ' . $_[0] );
221 }
222 0 return $return;
223 }
224  
225 =item info
226  
227 =cut
228  
229 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
230 0 my $self = shift;
231 0 return $self->fs_backend->info(@_);
232 }
233  
234 0 ▶sub _update_dir {
Subroutine _update_dir: not called CC=1 SCAR=6.9
235 0 my $self = shift;
236 0 $self->fs_backend->_update_dir(@_);
237 0 $self->commit( comment => 'update ' . $_[0] );
238 }
239  
240 0 ▶sub _update_file {
Subroutine _update_file: not called CC=1 SCAR=6.9
241 0 my $self = shift;
242 0 $self->fs_backend->_update_file(@_);
243 0 $self->commit( comment => 'update ' . $_[0] );
244 }
245  
246 =item import
247  
248 =cut
249  
250 1 ▶sub import {
Subroutine import: called CC=2 SCAR=11.6
251 1 my $self = shift;
252 1 ▶ return unless ref $self; # get rid of class->import
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 0 1
253 0 $self->fs_backend->import(@_);
254 0 my %args = @_;
255 0 my $name = $args{source};
256 0 $name =~ s!.*/!!;
257 0 $self->commit( comment => 'import ' . $name );
258 }
259  
260 =item commit
261  
262 =cut
263  
264 0 ▶sub commit {
Subroutine commit: not called CC=3 SCAR=24.8
Pod documented
265 0 my $self = shift;
266 0 my %args =
267 ( comment => 'comment', @_ ); # git doesn't allow comment to be blank
268  
269 0 ▶ if ( $self->local_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_dir) 0 0 0
270 0 my $cwd = getcwd;
271 0 ▶ chdir $self->local_dir or return;
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->local_dir or return 0 0 0
272  
273 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, 'add', '-f', '.' ] );
274  
275 # TODO comment need to be something special
276 run_cmd(
277 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'commit', '-m', $args{comment} ], 1 );
278 run_cmd(
279 0 [ $ENV{'SHIPWRIGHT_GIT'}, 'push', 'origin', 'master' ] );
280 0 chdir $cwd;
281 }
282 0 return;
283 }
284  
285 =item delete
286  
287 =cut
288  
289 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 my %args = @_;
292 0 my $path = $args{path};
293 0 $path =~ s!^/!!;
294 0 my $cwd = getcwd;
295 0 ▶ chdir $self->local_dir or return;
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->local_dir or return 0 0 0
296 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, 'rm', '-rf', $path ] );
297 0 chdir $cwd;
298 0 $self->commit( comment => 'delete ' . $path );
299 }
300  
301 =item move
302  
303 =cut
304  
305 0 ▶sub move {
Subroutine move: not called CC=2 SCAR=17.9
Pod documented
306 0 my $self = shift;
307 0 my %args = @_;
308 0 my $path = $args{path};
309 0 $path =~ s!^/!!;
310 0 my $new_path = $args{new_path};
311 0 $new_path =~ s!^/!!;
312 0 my $cwd = getcwd;
313  
314 0 ▶ chdir $self->local_dir or return;
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->local_dir or return 0 0 0
315 0 run_cmd( [ $ENV{'SHIPWRIGHT_GIT'}, 'mv', $path, $new_path ] );
316 0 chdir $cwd;
317 0 $self->commit( comment => "move $path to $new_path" );
318 }
319  
320 =back
321  
322 =cut
323  
324 1 1;
325  
326 __END__
« blib/lib/Shipwright/Source/SVN.pm ↑ Summary blib/lib/Shipwright/Script.pm »