lib/Serge/Sync/Plugin/VCS/mercurial.pm

filter: Statementstmt 18.3% 16 / 87 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 21.0% 4 / 19 Podpod 0.0% 0 / 14 totaltotal 13.8% 20 / 144 SCAR 49.2
CC
14
Cov
14%
get_remote_url
20.0
validate_data
12.0
delete_unversioned
12.0
CRAP 136.9
« lib/Serge/Engine/Plugin/parse_js.pm ↑ Summary lib/Serge/Interface/SysCmdRunner.pm »
1 package Serge::Sync::Plugin::VCS::mercurial;
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 File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Util qw(subst_macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # named boolean values for clarity
10 1 my $CAPTURE = 1;
11 1 my $IGNORE_ERRORS = 1;
12  
13 1 my $DEFAULT_BRANCH = 'default';
14  
15 sub name {
16 0 ▶ return 'Mercurial sync plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
17 }
18  
19 sub support_branch_switching {
20 0 ▶ return 1;
Subroutine support_branch_switching: not called CC=1 SCAR=6.9
Pod undocumented
21 }
22  
23 sub init {
24 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 0 $self->SUPER::init(@_);
27  
28 0 $self->merge_schema({
29 email => 'STRING',
30 name => 'STRING'
31 });
32 }
33  
34 sub validate_data {
35 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=3 SCAR=24.8
Pod undocumented
36  
37 0 $self->SUPER::validate_data;
38  
39 map {
40 0 ▶ die "'$_' not defined" unless defined $self->{data}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{$_} 0 0 0
41 0 $self->{data}->{$_} = subst_macros($self->{data}->{$_});
42 0 ▶ die "'$_' evaluates to an empty value" if $self->{data}->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{$_} eq "" 0 0 0
43 } qw(name email);
44 }
45  
46 sub split_remote_path_branch {
47 0 ▶ my ($self, $remote_path) = @_;
Subroutine split_remote_path_branch: not called CC=2 SCAR=17.9
Pod undocumented
48  
49 0 my ($remote_path, $branch) = split(/#/, $remote_path, 2);
50 0 ▶ $branch = $DEFAULT_BRANCH unless $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless $branch 0 0 0
51  
52 0 return ($remote_path, $branch);
53 }
54  
55 # get remote repository URL with an optional #branch_name suffix
56 sub get_remote_url {
57 0 ▶ my ($self, $local_path) = @_;
Subroutine get_remote_url: not called CC=4 SCAR=30.0
Pod undocumented
58  
59 # if there's no .hg subfolder, return immediately
60 # without running any commands
61 0 ▶ return undef unless -d catfile($local_path, 'hg');
Branch: 1 branch
BranchTrueFalseTotal
unless -d catfile($local_path, "hg") 0 0 0
62  
63 0 my ($url, $branch);
64  
65 # if $local_path points not to a git working copy (e.g. blank directory),
66 # git will set errorlevel to 1. So wrap this into eval{} do avoid dying
67 # in run_in() function and just return undef
68  
69 0 $url = $self->run_in($local_path, 'hg paths default', $CAPTURE, $IGNORE_ERRORS);
70 0 chomp($url);
71  
72 0 $branch = $self->run_in($local_path, 'hg branch', $CAPTURE, $IGNORE_ERRORS);
73 0 chomp($branch);
74 0 ▶ $branch = '' if $branch eq $DEFAULT_BRANCH;
Branch: 1 branch
BranchTrueFalseTotal
if $branch eq $DEFAULT_BRANCH 0 0 0
75  
76 0 ▶ return $branch ? "$url#$branch" : $url;
Branch: 1 branch
BranchTrueFalseTotal
$branch ? : 0 0 0
77 }
78  
79 sub switch_to_branch {
80 0 ▶ my ($self, $local_path, $branch) = @_;
Subroutine switch_to_branch: not called CC=2 SCAR=17.9
Pod undocumented
81  
82 0 my $heads;
83 0 $heads = $self->run_in($local_path, 'hg branches', $CAPTURE);
84 0 ▶ if ($heads !~ m|^\Q$branch\E|m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heads =~ /^\Q$branch\E/m) 0 0 0
85 0 die "No branch '$branch' found\n";
86 }
87  
88 0 $self->run_in($local_path, qq|hg update -C $branch|);
89 }
90  
91 sub get_last_revision {
92 0 ▶ my ($self, $local_path) = @_;
Subroutine get_last_revision: not called CC=2 SCAR=17.9
Pod undocumented
93  
94 0 my $id = $self->run_in($local_path, 'hg identify --id', $CAPTURE);
95 0 chomp($id);
96 0 ▶ die "Failed to get revision ID\n" unless $id ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $id ne "" 0 0 0
97 0 return $id;
98 }
99  
100 sub status {
101 0 ▶ my ($self, $local_path) = @_;
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
102  
103 # just display the summary of this repository (new/changed/deleted files)
104 0 $self->run_in($local_path, 'hg status');
105 }
106  
107 sub init_repo {
108 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine init_repo: not called CC=1 SCAR=6.9
Pod undocumented
109  
110 # cloning just a specific branch will prevent quick branch switching
111 #$self->run_in($local_path, qq|hg clone $remote_path --branch $branch .|);
112  
113 # instead, clone everything, then select the desired branch
114 0 $self->run_in($local_path, qq|hg clone $remote_path .|);
115 0 $self->switch_to_branch($local_path, $branch);
116 }
117  
118 sub delete_unversioned {
119 0 ▶ my ($self, $local_path) = @_;
Subroutine delete_unversioned: not called CC=3 SCAR=24.8
Pod undocumented
120  
121 # remove all untracked files
122 # (empty untracked directories are left in the file system, which
123 # shouldn't be a problem, though)
124 0 my $output = $self->run_in($local_path, 'hg status -nu', $CAPTURE);
125 0 chomp($output);
126 0 foreach my $rel_path (split(/[\r\n]+/, $output)) {
127 0 my $path = catfile($local_path, $rel_path);
128 0 print "Removing $path\n";
129 0 ▶ unlink($path) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or die $! 0 0 0
130 }
131 }
132  
133 sub add_unversioned {
134 0 ▶ my ($self, $local_path) = @_;
Subroutine add_unversioned: not called CC=1 SCAR=6.9
Pod undocumented
135  
136 # add all untracked files
137 0 $self->run_in($local_path, 'hg add');
138 }
139  
140 sub _username {
141 0 ▶ my ($self) = @_;
Subroutine _username: not called CC=3 SCAR=24.8
142  
143 0 ▶ if ($self->{data}->{name} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"name"} ne "") 0 0 0
144 0 ▶ if ($self->{data}->{email} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"email"} ne "") 0 0 0
145 0 return qq|--user "$self->{data}->{name} <$self->{data}->{email}>"|;
146 }
147 0 return qq|--user "$self->{data}->{name}"|;
148 }
149 0 return '';
150 }
151  
152 sub checkout {
153 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine checkout: not called CC=1 SCAR=6.9
Pod undocumented
154  
155 # pull changes from remote server, abandoning all local changes, if any
156 0 $self->run_in($local_path, qq|hg pull|);
157 0 $self->run_in($local_path, qq|hg update -r $branch -C|);
158 }
159  
160 sub commit {
161 0 ▶ my ($self, $local_path, $original_remote_path, $message) = @_;
Subroutine commit: not called CC=2 SCAR=17.9
Pod undocumented
162  
163 0 my ($remote_path, $branch) = $self->split_remote_path_branch($original_remote_path);
164  
165 # at this point, we don't have any untracked files; they have been added or deleted,
166 # depending on the high-level logic
167  
168 # we need to check if there's something to commit
169 0 my $output = $self->run_in($local_path, 'hg status', $CAPTURE); #1:capture
170 0 chomp($output);
171 0 ▶ if ($output eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output eq "") 0 0 0
172 0 print "Nothing to commit\n";
173 0 return;
174 }
175  
176 # multiline messages are not supported
177 0 $message =~ s/\n/ /sg;
178 0 my $msg_parameters = "-m \"$message\"";
179  
180 # commit locally
181 0 $self->run_in($local_path, qq|hg commit $msg_parameters|.$self->_username());
182  
183 # fetch changes from remote server
184 0 $self->run_in($local_path, qq|hg pull|);
185  
186 # merge changes; resolve conflict using 'theirs' strategy
187 0 $self->run_in($local_path, qq|hg merge -t internal:other|);
188  
189 # push changes to the remote server
190 0 $self->run_in($local_path, qq|hg push -b $branch|);
191 }
192  
193 1 1;
« lib/Serge/Engine/Plugin/parse_js.pm ↑ Summary lib/Serge/Interface/SysCmdRunner.pm »