blib/lib/Doit/Git.pm

filter: Statementstmt 87.3% 269 / 308 Branchbran 74.5% 158 / 212 Conditioncond 75.0% 27 / 36 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 28 / 28 Podpod 83.3% 10 / 12 totaltotal 81.8% 506 / 618 SCAR 53.4
CC
120
Cov
82%
__ANON__
85.8
__ANON__
66.1
__ANON__
20.0
CRAP 208.9
« blib/lib/Doit/Bootstrap.pm ↑ Summary blib/lib/Doit/XTermRPC.pm »
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017,2018,2019,2022 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::Git; # Convention: all commands here should be prefixed with 'git_'
15  
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 our $VERSION = '0.028';
19  
20 2 ▶use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Doit::Util qw(in_directory);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub _pipe_open (@);
24  
25 2 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
26 2 ▶sub functions { qw(git_repo_update git_short_status git_root git_get_commit_hash git_get_commit_files git_get_changed_files git_is_shallow git_current_branch git_config git_get_default_branch) }
Subroutine functions: called CC=1 SCAR=0.0
Pod undocumented
27  
28 sub git_repo_update {
29 42 ▶ my($self, %opts) = @_;
Subroutine git_repo_update: called CC=14 SCAR=26.4
Pod documented
30 42 my $repository = delete $opts{repository};
31 42 ▶ my @repository_aliases = @{ delete $opts{repository_aliases} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"repository_aliases"} or [] 39 3 42
32 42 my $directory = delete $opts{directory};
33 42 ▶ my $origin = delete $opts{origin} || 'origin';
Condition100%2/2
delete $opts{'origin'} || 'origin'
l!l
438
MC/DC100%1/1
delete $opts{'origin'} || 'origin'
delete $opts{'origin'}
Truth table100%2/2
A
A= delete $opts{'origin'}
Aresult
11
00
34 42 my $branch = delete $opts{branch};
35 42 my $allow_remote_url_change = delete $opts{allow_remote_url_change};
36 42 my $clone_opts = delete $opts{clone_opts};
37 42 ▶ my $refresh = delete $opts{refresh} || 'always';
Condition100%2/2
delete $opts{'refresh'} || 'always'
l!l
339
MC/DC100%1/1
delete $opts{'refresh'} || 'always'
delete $opts{'refresh'}
Truth table100%2/2
A
A= delete $opts{'refresh'}
Aresult
11
00
38 42 ▶ if ($refresh !~ m{^(always|never)$}) { error "refresh may be 'always' or 'never'" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($refresh =~ /^(always|never)$/) 1 41 42
39 41 my $quiet = delete $opts{quiet};
40 41 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 40 41
41  
42 40 my $has_changes = 0;
43 40 my $do_clone;
44 40 ▶ if (!-e $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $directory) { } 13 27 40
45 13 $do_clone = 1;
46 } else {
47 27 ▶ if (!-d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory) 1 26 27
48 1 error "'$directory' exists, but is not a directory\n";
49 }
50 26 ▶ if (!-d "$directory/.git") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "$directory/.git") 2 24 26
51 2 ▶ if (_is_dir_empty($directory)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_dir_empty($directory)) { } 1 1 2
52 1 $do_clone = 1;
53 } else {
54 1 error "No .git directory found in non-empty '$directory', refusing to clone...\n";
55 }
56 }
57 }
58 38 ▶ if (!$do_clone) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $do_clone) { } 24 14 38
59 in_directory {
60 24 ▶ my $actual_repository = eval { $self->info_qx({quiet=>1}, qw(git config --get), "remote.$origin.url") };
Subroutine __ANON__: called CC=20 SCAR=30.0
61 24 ▶ if (!defined $actual_repository) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $actual_repository) { } 1 23 24
62 # Remote does not exist yet --- create it.
63 1 $self->system(qw(git remote add), $origin, $repository);
64 } else {
65 23 chomp $actual_repository;
66 23 ▶ if ($actual_repository ne $repository && !grep { $_ eq $actual_repository } @repository_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_repository ne $repository and not grep({$_ eq $actual_repository;} @repository_aliases)) 3 20 23
Condition100%3/3
$actual_repository ne $repository and not grep({$_ eq $actual_repository;} @repository_aliases)
!ll&&!rl&&r
1913
MC/DC100%2/2
$actual_repository ne $repository and not grep({$_ eq $actual_repository;} @repository_aliases)
$actual_repository ne $repository not grep({$_ eq $actual_repository;} @repository_aliases)
Truth table100%3/3
A and not(B)
A= $actual_repository ne $repositoryB= not grep({$_ eq $actual_repository;} @repository_aliases)
ABresult
0X0
100
111
67 3 my @change_cmd = ('git', 'remote', 'set-url', $origin, $repository);
68 3 ▶ if ($allow_remote_url_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_remote_url_change) { } 1 2 3
69 1 info "Need to change remote URL for $origin";
70 1 $self->system(@change_cmd);
71 } else {
72 2 ▶ error
Branch: 1 branch
BranchTrueFalseTotal
@repository_aliases ? : 1 1 2
73 "In $directory: remote $origin does not point to $repository" . (@repository_aliases ? " (or any of the following aliases: @repository_aliases)" : "") . ", but to $actual_repository\n" .
74 "Please run manually\n" .
75 " @change_cmd\n" .
76 "or specify allow_remote_url_change=>1\n";
77 }
78 }
79 }
80  
81 22 my $switch_later;
82 22 ▶ if (defined $branch) { # maybe branch switching necessary?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $branch) 11 11 22
83 11 ▶ if ($branch =~ m{^refs/remotes/(.*)}) { # extract branch with remote
Branch: 1 branch
BranchTrueFalseTotal
if ($branch =~ m[^refs/remotes/(.*)]) 1 10 11
84 1 $branch = $1;
85 }
86 11 my $current_branch = $self->git_current_branch;
87 11 ▶ if (!defined $current_branch || $current_branch ne $branch) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $current_branch or $current_branch ne $branch) 7 4 11
Condition66%2/3
not defined $current_branch or $current_branch ne $branch
l!l&&r!l&&!r
074
MC/DC50%1/2
not defined $current_branch or $current_branch ne $branch
not defined $current_branch $current_branch ne $branch
Truth table66%2/3
not(A) or B
A= not defined $current_branchB= $current_branch ne $branch
ABresult
1X1
011
000
88 7 ▶ if (eval { $self->system({show_cwd=>1,quiet=>$quiet}, qw(git checkout), $branch); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->system({'show_cwd', 1, 'quiet', $quiet}, ('git', 'checkout'), $branch); 1 } }) { } 4 3 7
89 4 $has_changes = 1;
90 } else {
91 # Cannot switch now to the branch. Maybe a
92 # git-fetch has to be done first, as the
93 # branch is not yet in the clone --- try
94 # later.
95 3 $switch_later = 1;
96 }
97 }
98 11 my %info;
99 11 $self->git_current_branch(info_ref => \%info);
100 11 ▶ if ($info{detached}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{"detached"}) 5 6 11
101 5 $switch_later = 1; # because a "git pull" wouldn't update a detached branch
102 }
103 }
104  
105 22 ▶ if ($refresh eq 'always') {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh eq "always") 21 1 22
106 21 $self->system({show_cwd=>1,quiet=>$quiet}, qw(git fetch), $origin);
107 21 my $status = $self->git_short_status(untracked_files => 'no');
108 21 ▶ if ($status =~ m{>$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status =~ />$/) 5 16 21
109 # may actually fail if diverged (status=<>)
110 # or untracked/changed files would get overwritten
111 5 $self->system({show_cwd=>1,quiet=>$quiet}, qw(git pull), $origin); # XXX actually would be more efficient to do a merge or rebase, but need to figure out how git does it exactly...
112 4 $has_changes = 1;
113 } # else: ahead, diverged, or something else
114 }
115  
116 21 ▶ if ($switch_later) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch_later) 8 13 21
117 8 my($commit_before, $branch_before);
118 8 ▶ if (!$has_changes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_changes) 5 3 8
119 5 $commit_before = $self->git_get_commit_hash;
120 5 $branch_before = $self->git_current_branch;
121 }
122 8 ▶ if (!eval { $self->system({show_cwd=>1,quiet=>$quiet}, qw(git checkout), $branch) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->system({"show_cwd", 1, "quiet", $quiet}, ("git", "checkout"), $branch) } }) 1 7 8
123 # Possible reason for the failure: $branch exists
124 # as a remote branch in multiple remotes. Try
125 # again by explicitly specifying the remote.
126 # --track exists since approx git 1.5.1
127 1 $self->system({show_cwd=>1,quiet=>$quiet}, qw(git checkout -b), $branch, qw(--track), "$origin/$branch");
128 }
129 8 ▶ if ($commit_before
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_before and $self->git_get_commit_hash ne $commit_before || $self->git_current_branch ne $branch_before) 4 4 8
Condition100%6/6
$commit_before and $self->git_get_commit_hash ne $commit_before || $self->git_current_branch ne $branch_before
!ll&&!rl&&r
314
$self->git_get_commit_hash ne $commit_before or $self->git_current_branch ne $branch_before
l!l&&r!l&&!r
221
MC/DC100%3/3
$commit_before and $self->git_get_commit_hash ne $commit_before || $self->git_current_branch ne $branch_before
$commit_before $self->git_get_commit_hash ne $commit_before $self->git_current_branch ne $branch_before
Truth table100%4/4
A and B or C
A= $commit_beforeB= $self->git_get_commit_hash ne $commit_beforeC= $self->git_current_branch ne $branch_before
ABCresult
0XX0
1000
11X1
1011
130 && ( $self->git_get_commit_hash ne $commit_before
131 || $self->git_current_branch ne $branch_before
132 )
133 ) {
134 4 $has_changes = 1;
135 }
136 }
137 24 } $directory;
138 } else {
139 14 my @cmd = (qw(git clone --origin), $origin);
140 14 ▶ if (defined $branch) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $branch) 2 12 14
141 2 ▶ if ($branch =~ m{^refs/remotes/[^/]+/(.*)}) { # extract branch without remote
Branch: 1 branch
BranchTrueFalseTotal
if ($branch =~ m[^refs/remotes/[^/]+/(.*)]) 1 1 2
142 1 $branch = $1;
143 }
144 2 push @cmd, "--branch", $branch;
145 }
146 14 ▶ if ($clone_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clone_opts) 1 13 14
147 1 push @cmd, @$clone_opts;
148 }
149 14 push @cmd, $repository, $directory;
150 14 $self->system(@cmd);
151 14 $has_changes = 1;
152 }
153 35 $has_changes;
154 }
155  
156 sub git_short_status {
157 59 ▶ my($self, %opts) = @_;
Subroutine git_short_status: called CC=4 SCAR=16.0
Pod documented
158 59 my $directory = delete $opts{directory};
159 59 my $untracked_files = delete $opts{untracked_files};
160 59 ▶ if (!defined $untracked_files) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $untracked_files) { } 21 38 59
elsif (not $untracked_files =~ /^(normal|no)$/) { } 1 37 38
161 21 $untracked_files = 'normal';
162 } elsif ($untracked_files !~ m{^(normal|no)$}) {
163 1 error "only values 'normal' or 'no' supported for untracked_files";
164 }
165 58 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 57 58
166  
167 in_directory {
168 57 ▶ local $ENV{LC_ALL} = 'C';
Subroutine __ANON__: called CC=26 SCAR=44.5
169  
170 57 my $untracked_marker = '';
171 {
172 57 my @cmd = ("git", "status", "--untracked-files=$untracked_files", "--porcelain");
173 57 ▶ my $fh = _pipe_open(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = &_pipe_open(@cmd) or &error("Can't run '@{cmd}': $!") 0 57 57
174 or error "Can't run '@cmd': $!";
175 57 my $has_untracked;
176 my $has_uncommitted;
177 57 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 13 53 66
178 13 ▶ if (m{^\?\?}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\?\?/) { } 6 7 13
179 6 $has_untracked++;
180 } else {
181 7 $has_uncommitted++;
182 }
183 # Shortcut, exit as early as possible
184 13 ▶ if ($has_uncommitted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_uncommitted) 8 5 13
185 8 ▶ if ($has_untracked) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_untracked) { } 1 7 8
elsif ($untracked_files eq 'no') { } 3 4 7
186 1 return '<<*';
187 } elsif ($untracked_files eq 'no') {
188 3 return '<<';
189 } # else we have to check further, for possible untracked files
190 }
191 }
192 53 ▶ if ($has_uncommitted) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_uncommitted) { } 3 50 53
elsif ($has_untracked) { } 5 45 50
193 3 return '<<';
194 } elsif ($has_untracked) {
195 5 $untracked_marker = '*'; # will be combined later
196 5 last;
197 }
198 45 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &error("Error while running '@{cmd}': $!") 2 43 45
199 or error "Error while running '@cmd': $!";
200 }
201  
202 {
203 57 my @cmd = ("git", "status", "--untracked-files=no");
204 48 ▶ my $fh = _pipe_open(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = &_pipe_open(@cmd) or &error("Can't run '@{cmd}': $!") 0 48 48
205 or error "Can't run '@cmd': $!";
206 48 my $l;
207 48 $l = <$fh>;
208 48 $l = <$fh>;
209 48 ▶ if ($l =~ m{^(# )?Your branch is ahead}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($l =~ /^(# )?Your branch is ahead/) { } 4 44 48
elsif ($l =~ /^(# )?Your branch is behind/) { } 9 35 44
elsif ($l =~ /^(# )?Your branch and .* have diverged/) { } 4 31 35
210 4 return '<'.$untracked_marker;
211 } elsif ($l =~ m{^(# )?Your branch is behind}) {
212 9 return $untracked_marker.'>';
213 } elsif ($l =~ m{^(# )?Your branch and .* have diverged}) {
214 4 return '<'.$untracked_marker.'>';
215 }
216 }
217  
218 31 ▶ if (-f ".git/svn/.metadata") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".git/svn/.metadata") 0 31 31
219 # simple-minded heuristics, works only with svn standard branch
220 # layout
221 0 my $root_dir = $self->git_root;
222 0 ▶ if (open my $fh_remote, "$root_dir/.git/refs/remotes/trunk") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh_remote, "$root_dir/.git/refs/remotes/trunk") 0 0 0
223 0 ▶ if (open my $fh_local, "$root_dir/.git/refs/heads/master") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh_local, "$root_dir/.git/refs/heads/master") 0 0 0
224 0 chomp(my $sha1_remote = <$fh_remote>);
225 0 chomp(my $sha1_local = <$fh_local>);
226 0 ▶ if ($sha1_remote ne $sha1_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha1_remote ne $sha1_local) 0 0 0
227 0 my $remote_is_newer;
228 0 ▶ if (my $log_fh = _pipe_open('git', 'log', '--pretty=format:%H', 'master..remotes/trunk')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_fh = &_pipe_open("git", "log", "--pretty=format:%H", "master..remotes/trunk")) 0 0 0
229 0 ▶ if (scalar <$log_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar readline $log_fh) 0 0 0
230 0 $remote_is_newer = 1;
231 }
232 }
233 0 my $local_is_newer;
234 0 ▶ if (my $log_fh = _pipe_open('git', 'log', '--pretty=format:%H', 'remotes/trunk..master')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_fh = &_pipe_open("git", "log", "--pretty=format:%H", "remotes/trunk..master")) 0 0 0
235 0 ▶ if (scalar <$log_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar readline $log_fh) 0 0 0
236 0 $local_is_newer = 1;
237 }
238 }
239 0 ▶ if ($remote_is_newer && $local_is_newer) {
Branch: 3 branches
BranchTrueFalseTotal
if ($remote_is_newer and $local_is_newer) { } 0 0 0
elsif ($remote_is_newer) { } 0 0 0
elsif ($local_is_newer) { } 0 0 0
Condition0%0/3
$remote_is_newer and $local_is_newer
!ll&&!rl&&r
000
MC/DC0%0/2
$remote_is_newer and $local_is_newer
$remote_is_newer $local_is_newer
Truth table0%0/3
A and B
A= $remote_is_newerB= $local_is_newer
ABresult
0X0
100
111
240 0 return '<'.$untracked_marker.'>';
241 } elsif ($remote_is_newer) {
242 0 return $untracked_marker.'>';
243 } elsif ($local_is_newer) {
244 0 return '<'.$untracked_marker;
245 } else {
246 0 return '?'; # Should never happen
247 }
248 }
249 }
250 }
251 }
252  
253 31 return $untracked_marker;
254  
255 57 } $directory;
256 }
257  
258 sub git_root {
259 62 ▶ my($self, %opts) = @_;
Subroutine git_root: called CC=2 SCAR=6.9
Pod documented
260 62 my $directory = delete $opts{directory};
261 62 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 61 62
262  
263 in_directory {
264 61 ▶ chomp(my $dir = $self->info_qx({quiet=>1}, 'git', 'rev-parse', '--show-toplevel'));
Subroutine __ANON__: called CC=1 SCAR=0.0
265 54 $dir;
266 61 } $directory;
267 }
268  
269 sub git_get_commit_hash {
270 20 ▶ my($self, %opts) = @_;
Subroutine git_get_commit_hash: called CC=2 SCAR=6.9
Pod documented
271 20 my $directory = delete $opts{directory};
272 20 my $commit = delete $opts{commit};
273 20 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 19 20
274  
275 in_directory {
276 19 ▶ chomp(my $commit = $self->info_qx({quiet=>1}, 'git', 'log', '-1', '--format=%H', (defined $commit ? $commit : ())));
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $commit ? : 3 16 19
277 17 $commit;
278 19 } $directory;
279 }
280  
281 sub git_get_commit_files {
282 8 ▶ my($self, %opts) = @_;
Subroutine git_get_commit_files: called CC=3 SCAR=11.0
Pod documented
283 8 my $directory = delete $opts{directory};
284 8 ▶ my $commit = delete $opts{commit}; if (!defined $commit) { $commit = 'HEAD' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $commit) 6 2 8
285 8 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 7 8
286  
287 7 my @files;
288 in_directory {
289 6 ▶ my @cmd = ('git', 'show', $commit, '--pretty=format:', '--name-only');
Subroutine __ANON__: called CC=6 SCAR=18.0
290 6 ▶ my $fh = _pipe_open(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = &_pipe_open(@cmd) or &error("Error running @{cmd}: $!") 0 6 6
291 or error "Error running @cmd: $!";
292 6 my $first = <$fh>;
293 6 ▶ if (defined $first && $first ne "\n") { # first line is empty for older git versions (e.g. 1.7.x)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first and $first ne "\n") 3 3 6
Condition66%2/3
defined $first and $first ne "\n"
!ll&&!rl&&r
303
MC/DC50%1/2
defined $first and $first ne "\n"
defined $first $first ne "\n"
Truth table66%2/3
A and B
A= defined $firstB= $first ne "\n"
ABresult
0X0
100
111
294 3 chomp $first;
295 3 push @files, $first;
296 }
297 6 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 6 7
298 1 chomp;
299 1 push @files, $_;
300 }
301 6 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &error("Error while running @{cmd}: $!") 3 3 6
302 or error "Error while running @cmd: $!";
303 7 } $directory;
304 3 @files;
305 }
306  
307 sub git_get_changed_files {
308 11 ▶ my($self, %opts) = @_;
Subroutine git_get_changed_files: called CC=2 SCAR=6.9
Pod documented
309 11 my $directory = delete $opts{directory};
310 11 my $ignore_untracked = delete $opts{ignore_untracked};
311 11 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 10 11
312  
313 10 my @files;
314 in_directory {
315 10 ▶ my @cmd = qw(git status --porcelain);
Subroutine __ANON__: called CC=6 SCAR=18.0
316 10 ▶ my $fh = _pipe_open(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = &_pipe_open(@cmd) or &error("Error running @{cmd}: $!") 0 10 10
317 or error "Error running @cmd: $!";
318 10 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 7 10 17
319 7 chomp;
320 7 ▶ next if $ignore_untracked && m{^\?\?};
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_untracked and /^\?\?/ 1 6 7
Condition66%2/3
$ignore_untracked and /^\?\?/
!ll&&!rl&&r
601
MC/DC50%1/2
$ignore_untracked and /^\?\?/
$ignore_untracked /^\?\?/
Truth table66%2/3
A and B
A= $ignore_untrackedB= /^\?\?/
ABresult
0X0
100
111
321 6 s{^...}{};
322 6 push @files, $_;
323 }
324 10 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &error("Error while running @{cmd}: $!") 2 8 10
325 or error "Error while running @cmd: $!";
326 10 } $directory;
327 8 @files;
328 }
329  
330 sub git_is_shallow {
331 5 ▶ my($self, %opts) = @_;
Subroutine git_is_shallow: called CC=3 SCAR=11.0
Pod documented
332 5 my $directory = delete $opts{directory};
333 5 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 4 5
334  
335 4 my $git_root = $self->git_root(directory => $directory);
336 2 ▶ -f "$git_root/.git/shallow" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f "$git_root/.git/shallow" ? : 1 1 2
337 }
338  
339 sub git_current_branch {
340 50 ▶ my($self, %opts) = @_;
Subroutine git_current_branch: called CC=2 SCAR=9.7
Pod documented
341 50 my $directory = delete $opts{directory};
342 50 my $info_ref = delete $opts{info_ref};
343 50 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 49 50
344  
345 in_directory {
346 49 ▶ my $git_root = $self->git_root;
Subroutine __ANON__: called CC=15 SCAR=41.9
347 47 my $fh;
348 my $this_head;
349 47 ▶ if (open $fh, "<", "$git_root/.git/HEAD") {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, "<", "$git_root/.git/HEAD") 47 0 47
350 47 chomp($this_head = <$fh>);
351 47 ▶ if ($this_head =~ m{refs/heads/(\S+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_head =~ m[refs/heads/(\S+)]) 29 18 47
352 29 return $1;
353 }
354 }
355  
356 # fallback to git-status
357 18 $ENV{LC_ALL} = 'C';
358 18 ▶ if ($fh = _pipe_open(qw(git status))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh = &_pipe_open(("git", "status"))) 18 0 18
359 18 chomp($_ = <$fh>);
360 18 ▶ if (/^On branch (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^On branch (.*)/) 0 18 18
361 0 ▶ if ($info_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_ref) 0 0 0
362 0 $info_ref->{fallback} = 'git-status';
363 }
364 0 return $1;
365 }
366 18 ▶ if (/^.* detached at (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^.* detached at (.*)/) 18 0 18
367 18 ▶ if ($info_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_ref) 10 8 18
368 10 $info_ref->{detached} = 1;
369 10 $info_ref->{fallback} = 'git-status';
370 }
371 18 return $1;
372 }
373 0 ▶ if (/^\Q# Not currently on any branch./) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\#\ Not\ currently\ on\ any\ branch\./) 0 0 0
374 # Probably old git (~ 1.5 ... 1.7)
375 0 ▶ if (my $fh2 = _pipe_open(qw(git show-ref))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh2 = &_pipe_open(('git', 'show-ref'))) { } 0 0 0
376 0 ▶ while(<$fh2>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh2)) 0 0 0
377 0 chomp;
378 0 ▶ if (my($sha1, $ref) = $_ =~ m{^(\S+)\s+refs/remotes/(.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $_ =~ m[^(\S+)\s+refs/remotes/(.*)$]) 0 0 0
379 0 ▶ if ($sha1 eq $this_head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha1 eq $this_head) 0 0 0
380 0 ▶ if ($info_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_ref) 0 0 0
381 0 $info_ref->{detached} = 1;
382 0 $info_ref->{fallback} = 'git-show-ref';
383 }
384 0 return $ref;
385 }
386 }
387 }
388 0 ▶ close $fh2
Branch: 1 branch
BranchTrueFalseTotal
close $fh2 or &warning("Problem while running 'git show-ref': $!") 0 0 0
389 or warning "Problem while running 'git show-ref': $!";
390 } else {
391 0 warning "Error running 'git show-ref': $!";
392 }
393 }
394 }
395  
396 0 undef;
397 49 } $directory;
398 }
399  
400 sub git_config {
401 23 ▶ my($self, %opts) = @_;
Subroutine git_config: called CC=4 SCAR=13.9
Pod documented
402 23 my $directory = delete $opts{directory};
403 23 my $key = delete $opts{key};
404 23 my $val = delete $opts{val};
405 23 my $unset = delete $opts{unset};
406 23 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 22 23
407 22 ▶ if (defined $val && $unset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $unset) 1 21 22
Condition100%3/3
defined $val and $unset
!ll&&!rl&&r
1471
MC/DC100%2/2
defined $val and $unset
defined $val $unset
Truth table100%3/3
A and B
A= defined $valB= $unset
ABresult
0X0
100
111
408 1 error "Don't specify both 'unset' and 'val'";
409 }
410  
411 in_directory {
412 2 ▶ no warnings 'uninitialized'; # $old_val may be undef
Subroutine BEGIN: called CC=1 SCAR=0.0
413 21 ▶ chomp(my($old_val) = eval { $self->info_qx({quiet=>1}, qw(git config), $key) });
Subroutine __ANON__: called CC=8 SCAR=20.9
414 21 ▶ if ($unset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unset) { } 2 19 21
415 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 1 2
416 1 ▶ if ($@->{exitcode} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->{'exitcode'} == 1) { } 1 0 1
417 # already non-existent (or even invalid)
418 1 0;
419 } else {
420 0 error "git config $key failed with exitcode $@->{exitcode}";
421 }
422 } else {
423 1 ▶ $self->system(qw(git config --unset), $key, (defined $val ? $val : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 1 1
424 1 1;
425 }
426 } else {
427 19 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val) { } 12 7 19
428 12 $old_val;
429 } else {
430 7 ▶ if (!defined $old_val || $old_val ne $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $old_val or $old_val ne $val) { } 6 1 7
Condition100%3/3
not defined $old_val or $old_val ne $val
l!l&&r!l&&!r
421
MC/DC100%2/2
not defined $old_val or $old_val ne $val
not defined $old_val $old_val ne $val
Truth table100%3/3
not(A) or B
A= not defined $old_valB= $old_val ne $val
ABresult
1X1
011
000
431 6 $self->system(qw(git config), $key, $val);
432 4 1;
433 } else {
434 1 0;
435 }
436 }
437 }
438 21 } $directory;
439 }
440  
441 sub git_get_default_branch {
442 5 ▶ my($self, %opts) = @_;
Subroutine git_get_default_branch: called CC=7 SCAR=20.0
Pod documented
443 5 my $directory = delete $opts{directory};
444 5 ▶ my $origin = delete $opts{origin} || 'origin';
Condition50%1/2
delete $opts{'origin'} || 'origin'
l!l
05
MC/DC0%0/1
delete $opts{'origin'} || 'origin'
delete $opts{'origin'}
Truth table50%1/2
A
A= delete $opts{'origin'}
Aresult
11
00
445 5 my $method = delete $opts{method};
446 5 ▶ error "Unhandled options: " . join(' ', %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 5 5
447  
448 5 ▶ my @methods = (
Branch: 2 branches
BranchTrueFalseTotal
ref $method eq 'ARRAY' ? : 1 4 5
defined $method ? : 3 1 4
449 ref $method eq 'ARRAY' ? @$method :
450 defined $method ? $method :
451 ()
452 );
453 5 ▶ if (!@methods) { @methods = 'remote' }
Branch: 1 branch
BranchTrueFalseTotal
unless (@methods) 1 4 5
454  
455 5 my @error_msgs;
456 my $res;
457  
458 in_directory {
459 5 ▶ TRY_METHODS: while (@methods) {
Subroutine __ANON__: called CC=7 SCAR=20.4
Branch: 1 branch
BranchTrueFalseTotal
if (@methods) 5 0 5
460 5 my $method = shift @methods;
461 5 ▶ if ($method eq 'remote') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'remote') { } 2 3 5
elsif ($method eq 'symbolic-ref') { } 2 1 3
462 # from https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch#comment126528129_50056710
463 2 chomp(my $info_res = $self->info_qx({quiet=>1}, qw(env LC_ALL=C git remote show), $origin));
464 2 ▶ if ($info_res =~ /^\s*HEAD branch:\s+(.*)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_res =~ /^\s*HEAD branch:\s+(.*)/m) { } 2 0 2
465 2 $res = $1;
466 2 last TRY_METHODS;
467 } else {
468 0 push @error_msgs, "method $method: Can't get default branch; git-remote output is:\n$res";
469 }
470 } elsif ($method eq 'symbolic-ref') {
471 2 my $parent_ref = 'refs/remotes/' . $origin;
472 2 chomp(my $info_res = eval { $self->info_qx({quiet=>1}, qw(git symbolic-ref), "$parent_ref/HEAD") });
473 2 ▶ if (defined $info_res && $info_res ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info_res and $info_res ne '') { } 2 0 2
Condition33%1/3
defined $info_res and $info_res ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $info_res and $info_res ne ""
defined $info_res $info_res ne ""
Truth table33%1/3
A and B
A= defined $info_resB= $info_res ne ""
ABresult
0X0
100
111
474 2 $res = substr($info_res, length($parent_ref)+1);
475 2 last TRY_METHODS;
476 } else {
477 0 push @error_msgs, "method $method: Can't get default branch ($@)";
478 }
479 } else {
480 1 error "Unhandled git_get_default_branch method '$method'";
481 }
482 }
483 5 } $directory;
484  
485 4 ▶ if (@error_msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@error_msgs) 0 4 4
486 0 error join("\n", @error_msgs);
487 }
488  
489 4 $res;
490 }
491  
492  
493 # From https://stackoverflow.com/a/4495524/2332415
494 sub _is_dir_empty {
495 2 ▶ my ($dir) = @_;
Subroutine _is_dir_empty: called CC=4 SCAR=13.9
496  
497 2 ▶ opendir my $h, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $h, $dir or &error("Cannot open directory: '${dir}': $!") 0 2 2
498 or error "Cannot open directory: '$dir': $!";
499  
500 2 ▶ while (defined (my $entry = readdir $h)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $h)) 5 1 6
501 5 ▶ return unless $entry =~ /^[.][.]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /^[.][.]?\z/ 1 4 5
502 }
503  
504 1 return 1;
505 }
506  
507 sub _pipe_open (@) {
508 139 ▶ my(@cmd) = @_;
Subroutine _pipe_open: called CC=2 SCAR=7.0
509 139 my $fh;
510 139 if (Doit::IS_WIN && $] < 5.022) {
511 open $fh, '-|', Doit::Win32Util::win32_quote_list(@cmd)
512 or return undef;
513 } else {
514 139 ▶ open $fh, '-|', @cmd
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", @cmd or return undef 0 139 139
515 or return undef;
516 }
517 139 return $fh;
518 }
519  
520 2 1;
521  
522 __END__
« blib/lib/Doit/Bootstrap.pm ↑ Summary blib/lib/Doit/XTermRPC.pm »