File Coverage

blib/lib/Test/Smoke/Syncer/Git.pm
Criterion Covered Total %
statement 24 73 32.8
branch 0 20 0.0
condition 0 6 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 34 111 30.6


line stmt bran cond sub pod time code
1             package Test::Smoke::Syncer::Git;
2 11     11   80 use warnings;
  11         23  
  11         507  
3 11     11   78 use strict;
  11         21  
  11         517  
4              
5             our $VERSION = '0.029';
6              
7 11     11   80 use base 'Test::Smoke::Syncer::Base';
  11         27  
  11         1532  
8              
9             =head1 Test::Smoke::Syncer::Git
10              
11             This handles syncing with git repositories.
12              
13             =cut
14              
15              
16 11     11   81 use Cwd;
  11         22  
  11         778  
17 11     11   81 use File::Spec::Functions;
  11         59  
  11         1171  
18 11     11   84 use Test::Smoke::LogMixin;
  11         26  
  11         666  
19 11     11   74 use Test::Smoke::Util::Execute;
  11         25  
  11         841  
20              
21             =head2 Test::Smoke::Syncer::Git->new( %args )
22              
23             Keys for C<%args>:
24              
25             * gitorigin
26             * gitdir
27             * gitbin
28             * gitbranchfile
29             * gitdfbranch
30              
31             =cut
32              
33             =head2 $syncer->sync()
34              
35             Do the actual syncing.
36              
37             There are 2 repositories, they both need to be updated:
38              
39             The first (proxy) repository has the github.com/Perl repository as its
40             (origin) remote. The second repository is used to run the smoker from.
41              
42             For the proxy-repository we do:
43              
44             git fetch --all
45             git remote prune origin
46             git reset --hard origin/$gitbranch
47              
48             For the working-repository we do:
49              
50             git clean -dfx
51             git fetch --all
52             git reset --hard origin/$gitbranch
53              
54             =cut
55              
56             sub sync {
57 0     0 1   my $self = shift;
58              
59             my $gitbin = Test::Smoke::Util::Execute->new(
60             command => $self->{gitbin},
61 0           verbose => $self->verbose,
62             );
63 11     11   71 use Carp;
  11         81  
  11         9782  
64 0           my $cwd = cwd();
65             # Handle the proxy-clone
66 0 0 0       if ( ! -d $self->{gitdir} || ! -d catdir($self->{gitdir}, '.git') ) {
67             my $cloneout = $gitbin->run(
68             clone => $self->{gitorigin},
69             $self->{gitdir},
70 0           '2>&1'
71             );
72 0 0         if ( my $gitexit = $gitbin->exitcode ) {
73 0           croak("Cannot make initial clone: $self->{gitbin} exit $gitexit");
74             }
75 0           $self->log_debug("[git clone from $self->{gitorigin}]: $cloneout");
76             }
77              
78 0           my $gitbranch = $self->get_git_branch;
79 0 0         chdir $self->{gitdir} or croak("Cannot chdir($self->{gitdir}): $!");
80 0           $self->log_debug("chdir($self->{gitdir})");
81              
82 0           my $gitout = $gitbin->run(remote => 'update', '--prune', '2>&1');
83 0           $self->log_debug("gitorigin(update --prune): $gitout");
84              
85 0           $gitout = $gitbin->run(checkout => $gitbranch, '2>&1');
86 0           $self->log_debug("gitorigin(checkout): $gitout");
87              
88 0           $gitout = $gitbin->run(reset => '--hard', "origin/$gitbranch", '2>&1');
89 0           $self->log_debug("gitorigin(reset --hard): $gitout");
90              
91             # Now handle the working-clone
92 0 0         chdir $cwd or croak("Cannot chdir($cwd): $!");
93 0           $self->log_debug("chdir($cwd)");
94             # make the working-clone if it doesn't exist yet
95 0 0 0       if ( ! -d $self->{ddir} || ! -d catdir($self->{ddir}, '.git') ) {
96             # It needs to be empty ...
97             my $cloneout = $gitbin->run(
98             clone => $self->{gitdir},
99             $self->{ddir},
100 0           '2>&1'
101             );
102 0 0         if ( my $gitexit = $gitbin->exitcode ) {
103 0           croak("Cannot make smoke clone: $self->{gitbin} exit $gitexit");
104             }
105 0           $self->log_debug("[git clone $self->{gitdir}]: $cloneout");
106             }
107              
108 0 0         chdir $self->{ddir} or croak("Cannot chdir($self->{ddir}): $!");
109 0           $self->log_debug("chdir($self->{ddir})");
110              
111             # reset the working-dir to HEAD of the last branch smoked
112 0           $gitout = $gitbin->run(reset => '--hard', 'HEAD', '2>&1');
113 0           $self->log_debug("working-dir(reset --hard): $gitout");
114              
115             # remove all untracked files and dirs
116 0           $gitout = $gitbin->run(clean => '-dfx', '2>&1');
117 0           $self->log_debug("working-dir(clean -dfx): $gitout");
118              
119             # update from origin
120 0           $gitout = $gitbin->run(fetch => 'origin', '2>&1');
121 0           $self->log_debug("working-dir(fetch origin): $gitout");
122              
123             # now checkout the branch we want smoked
124 0           $gitout = $gitbin->run(checkout => $gitbranch, '2>&1');
125 0           $self->log_debug("working-dir(checkout $gitbranch): $gitout");
126              
127             # Make sure HEAD is exactly what the branch is
128 0           $gitout = $gitbin->run(reset => '--hard', "origin/$gitbranch", '2>&1');
129 0           $self->log_debug("working-dir(reset --hard): $gitout");
130              
131 0           $self->make_dot_patch();
132              
133 0           chdir $cwd;
134              
135 0           return $self->check_dot_patch;
136             }
137              
138             =head2 $git->get_git_branch()
139              
140             Reads the first line of the file set in B and returns its
141             value.
142              
143             =cut
144              
145             sub get_git_branch {
146 0     0 1   my $self = shift;
147              
148 0 0         return $self->{gitdfbranch} if !$self->{gitbranchfile};
149 0 0         return $self->{gitdfbranch} if ! -f $self->{gitbranchfile};
150              
151 0 0         if (open my $fh, '<', $self->{gitbranchfile}) {
152 0           $self->log_debug("Reading branch to smoke from: '$self->{gitbranchfile}'");
153              
154 0           chomp( my $branch = <$fh> );
155 0           close $fh;
156 0           return $branch;
157             }
158 0           $self->log_warn("Error opening '$self->{gitbranchfile}': $!");
159 0           return $self->{gitdfbranch};
160             }
161              
162             1;
163              
164             =head1 COPYRIGHT
165              
166             (c) 2002-2013, All rights reserved.
167              
168             * Abe Timmerman
169              
170             This library is free software; you can redistribute it and/or modify
171             it under the same terms as Perl itself.
172              
173             See:
174              
175             * ,
176             *
177              
178             This program is distributed in the hope that it will be useful,
179             but WITHOUT ANY WARRANTY; without even the implied warranty of
180             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
181              
182             =cut