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

filter: Statementstmt 26.6% 28 / 105 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 8 / 24 Podpod 0.0% 0 / 14 totaltotal 20.9% 36 / 172 SCAR 47.0
CC
14
Cov
21%
get_remote_url
30.0
validate_data
12.0
init_repo
12.0
CRAP 109.9
« lib/Serge/Interface/SysCmdRunner.pm ↑ Summary lib/Serge/Engine/Plugin/if.pm »
1 package Serge::Sync::Plugin::VCS::git;
2 1 ▶use parent Serge::Sync::Plugin::Base::VCS;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Util qw(subst_macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 = 'master';
14  
15 sub name {
16 0 ▶ return 'Git 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 clone_params => 'STRING',
30 name => 'STRING',
31 email => 'STRING',
32 commit_params => 'STRING',
33 push_params => 'STRING',
34 fetch_params => 'STRING'
35 });
36 }
37  
38 sub validate_data {
39 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=3 SCAR=24.8
Pod undocumented
40  
41 0 $self->SUPER::validate_data;
42  
43 map {
44 0 ▶ die "'$_' not defined" unless defined $self->{data}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{$_} 0 0 0
45 0 $self->{data}->{$_} = subst_macros($self->{data}->{$_});
46 0 ▶ die "'$_' evaluates to an empty value" if $self->{data}->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{$_} eq "" 0 0 0
47 } qw(name email);
48 }
49  
50 sub split_remote_path_branch {
51 0 ▶ my ($self, $remote_path) = @_;
Subroutine split_remote_path_branch: not called CC=2 SCAR=17.9
Pod undocumented
52  
53 0 ($remote_path, my $branch) = split(/#/, $remote_path, 2);
54 0 ▶ $branch = $DEFAULT_BRANCH unless $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless $branch 0 0 0
55  
56 0 return ($remote_path, $branch);
57 }
58  
59 # get remote repository URL with an optional #branch_name suffix
60 sub get_remote_url {
61 0 ▶ my ($self, $local_path) = @_;
Subroutine get_remote_url: not called CC=5 SCAR=34.0
Pod undocumented
62  
63 # if there's no .git subfolder, return immediately
64 # without running any commands
65 0 ▶ return undef unless -d catfile($local_path, '.git');
Branch: 1 branch
BranchTrueFalseTotal
unless -d catfile($local_path, ".git") 0 0 0
66  
67 0 my ($url, $branch);
68  
69 # if $local_path points not to a git working copy (e.g. blank directory),
70 # git will set errorlevel to 1. So wrap this into eval{} do avoid dying
71 # in run_in() function and just return undef
72  
73 0 $url = $self->run_in($local_path, 'git config --get remote.origin.url', $CAPTURE, $IGNORE_ERRORS);
74 0 chomp($url);
75  
76 0 $branch = $self->run_in($local_path, 'git symbolic-ref -q HEAD', $CAPTURE, $IGNORE_ERRORS);
77 0 chomp($branch);
78 0 ▶ if ($branch =~ m|^refs/heads/(.+)$|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch =~ m[^refs/heads/(.+)$]) 0 0 0
79 0 $branch = $1;
80 0 ▶ $branch = '' if $branch eq $DEFAULT_BRANCH;
Branch: 1 branch
BranchTrueFalseTotal
if $branch eq $DEFAULT_BRANCH 0 0 0
81 }
82  
83 0 ▶ return $branch ? "$url#$branch" : $url;
Branch: 1 branch
BranchTrueFalseTotal
$branch ? : 0 0 0
84 }
85  
86 sub switch_to_branch {
87 0 ▶ my ($self, $local_path, $branch) = @_;
Subroutine switch_to_branch: not called CC=2 SCAR=17.9
Pod undocumented
88  
89 0 my $heads;
90 0 $heads = $self->run_in($local_path, 'git ls-remote --heads --quiet', $CAPTURE);
91 0 ▶ if ($heads !~ m|\srefs/heads/\Q$branch\E$|m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heads =~ m[\srefs/heads/\Q$branch\E$]m) 0 0 0
92 0 die "No remote branch '$branch' found\n";
93 }
94  
95 0 $self->run_in($local_path, qq|git checkout $branch|);
96 # reset to remote state
97 0 $self->run_in($local_path, qq|git reset --hard origin/$branch|);
98 }
99  
100 sub get_last_revision {
101 0 ▶ my ($self, $local_path) = @_;
Subroutine get_last_revision: not called CC=1 SCAR=6.9
Pod undocumented
102  
103 0 my $output = $self->run_in($local_path, 'git rev-parse --verify HEAD', $CAPTURE);
104 0 chomp($output);
105 0 return $output;
106 }
107  
108 sub status {
109 0 ▶ my ($self, $local_path) = @_;
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
110  
111 # just display the summary of this repository (new/changed/deleted files)
112 0 $self->run_in($local_path, 'git status --porcelain');
113 }
114  
115 sub init_repo {
116 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine init_repo: not called CC=3 SCAR=24.8
Pod undocumented
117  
118 0 $self->run_in($local_path, qq|git clone $remote_path --branch $branch $self->{data}->{clone_params} .|);
119  
120 # if email is provided, set it up as an override for this local repository
121 0 ▶ if (exists $self->{data}->{email}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{"email"}) 0 0 0
122 0 $self->run_in($local_path, qq|git config user.email "$self->{data}->{email}"|);
123 }
124  
125 # if user name is provided, set it up as an override for this local repository
126 0 ▶ if (exists $self->{data}->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{"name"}) 0 0 0
127 0 $self->run_in($local_path, qq|git config user.name "$self->{data}->{name}"|);
128 }
129 }
130  
131 sub delete_unversioned {
132 0 ▶ my ($self, $local_path) = @_;
Subroutine delete_unversioned: not called CC=1 SCAR=6.9
Pod undocumented
133  
134 # remove all unversioned files and directories
135 0 $self->run_in($local_path, 'git clean -d --force');
136 }
137  
138 sub add_unversioned {
139 0 ▶ my ($self, $local_path) = @_;
Subroutine add_unversioned: not called CC=1 SCAR=6.9
Pod undocumented
140  
141 # add all unversioned files and directories
142 0 $self->run_in($local_path, 'git add --all .');
143 }
144  
145 sub checkout {
146 0 ▶ my ($self, $local_path, $remote_path, $branch) = @_;
Subroutine checkout: not called CC=3 SCAR=24.8
Pod undocumented
147  
148 # abort unfinished rebase, if any
149 # (see https://github.com/git/git/blob/master/git-rebase.sh
150 # for how Git itself detects an unfinished rebase)
151  
152 0 ▶ if (-d catfile($local_path, ".git/rebase-merge") || -d catfile($local_path, ".git/rebase-apply")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d catfile($local_path, ".git/rebase-merge") or -d catfile($local_path, ".git/rebase-apply")) 0 0 0
Condition0%0/3
-d catfile($local_path, ".git/rebase-merge") or -d catfile($local_path, ".git/rebase-apply")
l!l&&r!l&&!r
000
MC/DC0%0/2
-d catfile($local_path, ".git/rebase-merge") or -d catfile($local_path, ".git/rebase-apply")
-d catfile($local_path, ".git/rebase-merge") -d catfile($local_path, ".git/rebase-apply")
Truth table0%0/3
A or B
A= -d catfile($local_path, ".git/rebase-merge")B= -d catfile($local_path, ".git/rebase-apply")
ABresult
1X1
011
000
153 0 print "Aborting unfinished rebase\n";
154 0 $self->run_in($local_path, qq|git rebase --abort|, undef, $IGNORE_ERRORS);
155 }
156  
157 # on an empty repo where HEAD doesn't exist (there is no single commit)
158 # git will consider this a fatal error and set errorlevel to non-zero value
159 # thus, ignoring all errors
160  
161 # abandon all local changes, if any
162 0 $self->run_in($local_path, qq|git reset --hard origin/$branch|, undef, $IGNORE_ERRORS);
163  
164 # before fetching, remove old conflicting branches, if any
165 0 $self->run_in($local_path, qq|git remote prune origin|);
166  
167 # pull changes from remote server
168 0 $self->run_in($local_path, qq|git fetch $self->{data}->{fetch_params}|);
169 # reset to remote state
170 0 $self->run_in($local_path, qq|git reset --hard origin/$branch|);
171 }
172  
173 sub _update_message {
174 0 ▶ my ($self, $message) = @_;
Subroutine _update_message: not called CC=1 SCAR=6.9
175 # for vanilla Git, do not alter the message;
176 # the Gerrit plugin will append the 'Change-Id: xxxxxx' field to the message
177 0 return $message;
178 }
179  
180 sub commit {
181 0 ▶ my ($self, $local_path, $original_remote_path, $message) = @_;
Subroutine commit: not called CC=2 SCAR=17.9
Pod undocumented
182  
183 0 my ($remote_path, $branch) = $self->split_remote_path_branch($original_remote_path);
184  
185 # update only previously versioned files, do not add unstaged files
186 0 $self->run_in($local_path, 'git add --update');
187  
188 # we need to check if there's something to commit, otherwise git will treat
189 # empty commit as fatal error and set errorlevel to non-zero value
190 0 my $output = $self->run_in($local_path, 'git status --porcelain', $CAPTURE); #1:capture
191 0 chomp($output);
192 0 ▶ if ($output eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output eq "") 0 0 0
193 0 print "Nothing to commit\n";
194 0 return;
195 }
196  
197 # prepare the final message
198 0 $message = $self->_update_message($message);
199 # split the multiline message into a series of -m "..." -m "..."
200 0 my $msg_parameters = join(' ', map { "-m \"$_\"" } split(/\n+/s, $message));
201  
202 # commit locally
203 0 $self->run_in($local_path, qq|git commit $msg_parameters $self->{data}->{commit_params}|);
204  
205 # fetch changes from remote server
206 0 $self->run_in($local_path, qq|git fetch $self->{data}->{fetch_params}|);
207  
208 # rebase (do not merge, since merges are incompatible with Gerrit).
209 0 $self->run_in($local_path, qq|git rebase origin/$branch|);
210  
211 # push changes to the remote server
212 0 $self->_push($local_path, $original_remote_path, $message);
213 }
214  
215 # this method is externalized so that gerrit.pm plugin could override it
216 sub _push {
217 0 ▶ my ($self, $local_path, $remote_path, $message) = @_;
Subroutine _push: not called CC=1 SCAR=6.9
218  
219 0 ($remote_path, my $branch) = $self->split_remote_path_branch($remote_path);
220  
221 0 $self->run_in($local_path, qq|git push origin $branch $self->{data}->{push_params}|);
222 }
223  
224 1 1;
« lib/Serge/Interface/SysCmdRunner.pm ↑ Summary lib/Serge/Engine/Plugin/if.pm »