| 1 | package Serge::Sync::Plugin::VCS::svn; | ||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Sync::Plugin::Base::VCS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use Config::Neat::Schema; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use File::Path; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use File::Spec::Functions qw(catfile rel2abs); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use Serge::Sync::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Serge::Util qw(subst_macros); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | sub name { | ||||||||||||||
| 14 | 0 | ▶ | return 'Subversion sync plugin'; | ||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | sub support_branch_switching { | ||||||||||||||
| 18 | 0 | ▶ | return undef; | ||||||||||||
|
Subroutine
support_branch_switching: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | sub init { | ||||||||||||||
| 22 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 0 | $self->SUPER::init(@_); | |||||||||||||
| 25 | |||||||||||||||
| 26 | 0 | $self->merge_schema({ | |||||||||||||
| 27 | user => 'STRING', | ||||||||||||||
| 28 | password => 'STRING' | ||||||||||||||
| 29 | }); | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | sub validate_data { | ||||||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
validate_data: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | $self->SUPER::validate_data; | |||||||||||||
| 36 | |||||||||||||||
| 37 | map { | ||||||||||||||
| 38 | 0 | ▶ | die "'$_' not defined" unless defined $self->{data}->{$_}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | $self->{data}->{$_} = subst_macros($self->{data}->{$_}); | |||||||||||||
| 40 | 0 | ▶ | die "'$_' evaluates to an empty value" if $self->{data}->{$_} eq ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | } qw(user password); | ||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub get_remote_url { | ||||||||||||||
| 45 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
get_remote_url: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | # if there's no .svn subfolder, return immediately | ||||||||||||||
| 48 | # without running any commands | ||||||||||||||
| 49 | 0 | ▶ | return undef unless -d catfile($local_path, '.svn'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | my $output; | |||||||||||||
| 52 | |||||||||||||||
| 53 | # if $local_path points not to an svn working copy (e.g. blank directory), | ||||||||||||||
| 54 | # svn will set errorlevel to 1. So wrap this into eval{} do avoid dying | ||||||||||||||
| 55 | # in run_in() function and just return undef | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | eval { | |||||||||||||
| 58 | 0 | $output = $self->run_in($local_path, 'svn info --non-interactive', 1); #1:capture | |||||||||||||
| 59 | }; | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | if ($output =~ m/\nURL:(.*?)\n/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 0 | my $url = $1; | |||||||||||||
| 63 | 0 | $url =~ s/^\s+//; | |||||||||||||
| 64 | 0 | $url =~ s/\s+$//; | |||||||||||||
| 65 | 0 | return $url; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | return undef; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | sub get_last_revision { | ||||||||||||||
| 72 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
get_last_revision: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | # run recursively (i.e. for all files) | ||||||||||||||
| 75 | # never echo - this output can be huge (several megabytes) | ||||||||||||||
| 76 | 0 | my $output = $self->run_in($local_path, 'svn info -R --xml --non-interactive"', 1, 1); #1:capture, 1:suppress_echo | |||||||||||||
| 77 | |||||||||||||||
| 78 | # because of the "mixed revision working copies", the folders' committed revision | ||||||||||||||
| 79 | # may be lower than the committed revision of a just committed file. | ||||||||||||||
| 80 | # and doing `svn up` to update the folder's revision can't guarantee the there were | ||||||||||||||
| 81 | # no updates to these files in between. | ||||||||||||||
| 82 | # so scanning all files and finding the biggest revision number seems to be | ||||||||||||||
| 83 | # the only solution | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | my $rev; | |||||||||||||
| 86 | 0 | ▶ | while ($output =~ m/<commit\s+revision=\"(\d+)\">/g) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | ▶ | $rev = $1 if ($1 > $rev); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | } | ||||||||||||||
| 89 | 0 | return $rev; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub status { | ||||||||||||||
| 93 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 94 | 0 | $self->run_in($local_path, 'svn status --non-interactive'); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | sub _resolve { | ||||||||||||||
| 98 | 0 | ▶ | my ($self, $local_path, $mine) = @_; | ||||||||||||
|
Subroutine
_resolve: not called CC=2 SCAR=17.9
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | ▶ | my $strategy = $mine ? 'mine-full' : 'theirs-full'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | $self->run_in($local_path, | |||||||||||||
| 103 | qq|svn resolve . --accept $strategy --recursive --non-interactive| | ||||||||||||||
| 104 | ); | ||||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | sub init_repo { | ||||||||||||||
| 108 | 0 | ▶ | my ($self, $local_path, $remote_path) = @_; | ||||||||||||
|
Subroutine
init_repo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 109 | 0 | return $self->checkout($local_path, $remote_path); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | sub _get_unversioned_paths { | ||||||||||||||
| 113 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
_get_unversioned_paths: not called CC=4 SCAR=30.0
| |||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | my @paths; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | my $output = $self->run_in($local_path, 'svn status --non-interactive', 1); #1:capture | |||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | my @lines = split(/[\r\n]+/, $output); | |||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | foreach my $line (@lines) { | |||||||||||||
| 122 | 0 | my ($status, $path) = split(/\s+/, $line, 2); | |||||||||||||
| 123 | 0 | ▶ | next unless ($status eq '?'); # skip all statuses except '?' (not under version control) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | |||||||||||||||
| 125 | 0 | $path = rel2abs($path, $local_path); | |||||||||||||
| 126 | |||||||||||||||
| 127 | 0 | ▶ | next if ($path eq $local_path); # skip the root path itself | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 0 | push(@paths, $path); | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | return @paths; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | sub delete_unversioned { | ||||||||||||||
| 135 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
delete_unversioned: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | my @paths = $self->_get_unversioned_paths($local_path); | |||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | foreach my $path (@paths) { | |||||||||||||
| 140 | 0 | ▶ | if (-d $path) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 141 | # directory | ||||||||||||||
| 142 | 0 | print "Deleting the directory $path\n"; | |||||||||||||
| 143 | 0 | rmtree($path); | |||||||||||||
| 144 | } elsif (-f $path) { | ||||||||||||||
| 145 | # file | ||||||||||||||
| 146 | 0 | print "Deleting the file $path\n"; | |||||||||||||
| 147 | 0 | ▶ | unlink $path || die $!; | ||||||||||||
|
Condition0%0/2
$path || die($!)
MC/DC0%0/1
$path || die($!)
$path
Truth table0%0/2
A
A= $path
| |||||||||||||||
| 148 | } else { | ||||||||||||||
| 149 | 0 | die "$path is not a file and not a directory?\n"; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | } | ||||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | sub _add { | ||||||||||||||
| 155 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
_add: not called CC=1 SCAR=6.9
| |||||||||||||||
| 156 | |||||||||||||||
| 157 | 0 | $local_path = escape_path($local_path); | |||||||||||||
| 158 | |||||||||||||||
| 159 | # add just this specific item (file or directory, without its contents) to svn | ||||||||||||||
| 160 | 0 | $self->run_cmd(qq|svn add "$local_path" --depth empty --non-interactive|); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | sub add_unversioned { | ||||||||||||||
| 164 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
add_unversioned: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 165 | |||||||||||||||
| 166 | 0 | my @paths = $self->_get_unversioned_paths($local_path); | |||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | foreach my $path (@paths) { | |||||||||||||
| 169 | 0 | $self->_add($path); | |||||||||||||
| 170 | # after adding a direcotry, add it's immediate children | ||||||||||||||
| 171 | 0 | ▶ | if (-d $path) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 0 | $self->add_unversioned($path); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | } | ||||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | sub _credentials { | ||||||||||||||
| 178 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_credentials: not called CC=1 SCAR=6.9
| |||||||||||||||
| 179 | 0 | return qq|--username "$self->{data}->{user}" --password "$self->{data}->{password}"|; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | sub checkout { | ||||||||||||||
| 183 | 0 | ▶ | my ($self, $local_path, $remote_path) = @_; | ||||||||||||
|
Subroutine
checkout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 184 | |||||||||||||||
| 185 | 0 | $self->run_in($local_path, | |||||||||||||
| 186 | qq|svn co "$remote_path" . --force --non-interactive |.$self->_credentials() | ||||||||||||||
| 187 | ); | ||||||||||||||
| 188 | |||||||||||||||
| 189 | 0 | $self->_resolve($local_path); # resolve conflicts using their copy | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | sub _get_externals { | ||||||||||||||
| 193 | 0 | ▶ | my ($self, $local_path) = @_; | ||||||||||||
|
Subroutine
_get_externals: not called CC=4 SCAR=30.0
| |||||||||||||||
| 194 | |||||||||||||||
| 195 | 0 | my $output = $self->run_in($local_path, 'svn status --non-interactive', 1); #1:capture | |||||||||||||
| 196 | |||||||||||||||
| 197 | 0 | my @externals; | |||||||||||||
| 198 | 0 | my @lines = split(/[\r\n]+/, $output); | |||||||||||||
| 199 | |||||||||||||||
| 200 | 0 | foreach my $line (@lines) { | |||||||||||||
| 201 | 0 | my ($status, $path) = split(/\s+/, $line, 2); | |||||||||||||
| 202 | 0 | ▶ | next unless ($status eq 'X'); # skip all statuses except 'X' (external) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 203 | |||||||||||||||
| 204 | 0 | $path = rel2abs($path, $local_path); | |||||||||||||
| 205 | |||||||||||||||
| 206 | 0 | ▶ | if (-d $path) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | # directory | ||||||||||||||
| 208 | 0 | push(@externals, $path); | |||||||||||||
| 209 | } else { | ||||||||||||||
| 210 | # file | ||||||||||||||
| 211 | 0 | print "WARNING: reported external '$path' is not a directory?\n"; | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | } | ||||||||||||||
| 214 | 0 | return @externals; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | sub _commit { | ||||||||||||||
| 218 | 0 | ▶ | my ($self, $local_path, $remote_path, $message) = @_; | ||||||||||||
|
Subroutine
_commit: not called CC=1 SCAR=6.9
| |||||||||||||||
| 219 | |||||||||||||||
| 220 | 0 | $self->run_in($local_path, | |||||||||||||
| 221 | qq|svn ci --message "$message" --non-interactive |.$self->_credentials() | ||||||||||||||
| 222 | ); | ||||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | sub commit { | ||||||||||||||
| 226 | 0 | ▶ | my ($self, $local_path, $message) = @_; | ||||||||||||
|
Subroutine
commit: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 227 | |||||||||||||||
| 228 | # update the project including externals | ||||||||||||||
| 229 | |||||||||||||||
| 230 | 0 | $self->run_in($local_path, | |||||||||||||
| 231 | qq|svn up --force --non-interactive |.$self->_credentials() | ||||||||||||||
| 232 | ); | ||||||||||||||
| 233 | |||||||||||||||
| 234 | 0 | $self->_resolve($local_path, 1); # resolve conflicts using local copy | |||||||||||||
| 235 | |||||||||||||||
| 236 | # commit base dir (externals will not be committed) | ||||||||||||||
| 237 | |||||||||||||||
| 238 | 0 | $self->_commit($local_path, $message); | |||||||||||||
| 239 | |||||||||||||||
| 240 | # commit externals | ||||||||||||||
| 241 | |||||||||||||||
| 242 | 0 | my @externals = $self->_get_externals($local_path); | |||||||||||||
| 243 | |||||||||||||||
| 244 | 0 | foreach my $path (@externals) { | |||||||||||||
| 245 | 0 | $self->_commit($path, $message); | |||||||||||||
| 246 | } | ||||||||||||||
| 247 | } | ||||||||||||||
| 248 | |||||||||||||||
| 249 | 1 | 1; | |||||||||||||