blib/lib/Giovanni/Plugins/Git.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 83 CC 9 SCAR 31.5
CC
9
Cov
0%
tag
42.0
get_last_tag
12.0
_get_cache_dir
6.0
CRAP 23.4
1 package Giovanni::Plugins::Git;
2  
3 use Mouse::Role;
4 use Git::Repository;
5  
6 has 'git' => (
7 is => 'rw',
8 isa => 'Git::Repository',
9 lazy => 1,
10 default => sub { Git::Repository->new(work_tree => $_[0]->repo) },
11 );
12  
13 sub tag {
14 my $self = shift;
15  
16 my $tag = 'v' . time;
17 my $log =
18 $self->git->run(tag => '-a', $tag, '-m', "tagging to $tag for rollout");
19 $self->log('git', "Tag: [$tag] " . $log) if $self->is_debug;
20 $log = $self->git->run('pull') unless $self->is_debug;
21 $self->log('git', "Pull: " . $log) if $self->is_debug;
22 $log = $self->git->run(push => 'origin', '--tags') unless $self->is_debug;
23 $self->log('git', "Push: " . $log) if $self->is_debug;
24 $self->version($tag);
25 $self->log('git', $log);
26  
27 return $tag;
28 }
29  
30 sub get_last_tag {
31 my ($self, $n) = @_;
32  
33 $n = 1 unless $n;
34 my @tags = $self->git->run('tag');
35 $self->log('git', "Tags: " . join(', ', @tags) . $/) if $self->is_debug;
36  
37 return splice(@tags, $n - 1, $n);
38 }
39  
40 around 'update_cache' => sub {
41 my ($orig, $self, $ssh, $conf) = @_;
42  
43 my $log;
44 my $cache_dir = $self->_get_cache_dir($conf);
45  
46 if ($cache_dir) {
47 if ($ssh->test("[ -d " . $cache_dir . "/.git ]")) {
48 $log .= $ssh->capture("cd $cache_dir && git pull");
49 $self->log("running git pull ...");
50 }
51 else {
52 $log = $ssh->capture("mkdir -p " . $cache_dir)
53 unless $ssh->test("[ -d " . $cache_dir . " ]");
54 $log .= $ssh->capture("cd ". $cache_dir
55 . "&& git clone " . $self->config->{repo} . " .");
56 $self->log("running git clone ...");
57 }
58 }
59 $self->log($ssh, $log);
60  
61 return;
62 };
63  
64 around 'checkout' => sub {
65 my ($orig, $self, $ssh, $conf) = @_;
66  
67 my $log;
68 my $cache_dir = $self->_get_cache_dir($conf);
69 if ($self->config->{deploy_dir}) {
70 $log .=
71 $ssh->capture("cd "
72 . $self->config->{deploy_dir}
73 . " && git clone --depth 1 --no-hardlinks file://"
74 . $cache_dir
75 . " .");
76 }
77 $self->log($ssh, $log);
78 };
79  
80 sub _get_cache_dir {
81 my ($self, $conf) = @_;
82 if($self->config->{cache}){
83 my @parts = split(/\//, $self->config->{repo});
84 my $git_dir = pop(@parts);
85 return join('/', $self->config->{cache}, $git_dir);
86 } else {
87 return $self->config->{root};
88 }
89 }
90  
91 1;
92  
93 __END__