blib/lib/Git/Bunch.pm

filter: Statementstmt 12.0% 63 / 524 Branchbran 0.0% 0 / 262 Conditioncond 0.0% 0 / 119 MC/DCmcdc 0.0% 0 / 70 Subroutinesub 40.5% 15 / 37 Podpod 100% 6 / 6 totaltotal 8.2% 84 / 1018 CC 192 SCAR 67.2
CC
192
Cov
7%
sync_bunch
2352.0
check_bunch
930.0
_sync_repo
930.0
CRAP 828.4
↑ Summary
1 package Git::Bunch;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::ger::Format 'MultilevelLog';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IPC::System::Options 'system', 'readpipe', -log=>1, -lang=>'C';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
19 2 our $DATE = '2026-01-03'; # DATE
20 2 our $DIST = 'Git-Bunch'; # DIST
21 2 our $VERSION = '0.632'; # VERSION
22  
23 2 our @EXPORT_OK = qw(check_bunch sync_bunch exec_bunch);
24  
25 2 our %SPEC;
26  
27 2 $SPEC{":package"} = {
28 v => 1.1,
29 summary => 'Manage gitbunch directory (directory which contain git repos)',
30 description => <<'MARKDOWN',
31  
32 A _gitbunch_ or _bunch_ directory is just a term I coined to refer to a
33 directory which contains, well, a bunch of git repositories. It can also contain
34 other stuffs like files and non-git repositories (but they must be dot-dirs).
35 Example:
36  
37 repos/ -> a gitbunch dir
38 proj1/ -> a git repo
39 proj2/ -> ditto
40 perl-Git-Bunch/ -> ditto
41 ...
42 .videos/ -> a non-git dir
43 README.txt -> file
44  
45 If you organize your data as a bunch, you can easily check the status of your
46 repositories and synchronize your data between two locations, e.g. your
47 computer's harddisk and an external/USB harddisk.
48  
49 A little bit of history: after _git_ got popular, in 2008 I started using it for
50 software projects, replacing Subversion and Bazaar. Soon, I moved everything*)
51 to git repositories: notes & writings, Emacs .org agenda files, configuration,
52 even temporary downloads/browser-saved HTML files. I put the repositories inside
53 _$HOME/repos_ and add symlinks to various places for conveniences. Thus, the
54 _$HOME/repos_ became the first bunch directory.
55  
56 *) everything except large media files (e.g. recorded videos) which I put in
57 dot-dirs inside the bunch.
58  
59 See also <prog:rsybak>, which I wrote to backup everything else.
60  
61 MARKDOWN
62 links => [
63 {
64 url => 'prog:rsybak',
65 },
66 {
67 url => 'http://joeyh.name/code/mr/',
68 description => <<'MARKDOWN',
69  
70 You probably want to use this instead. _mr_ supports other control version
71 software aside from git, doesn't restrict you to put all your repos in one
72 directory, supports more operations, and has been developed since 2007. Had I
73 known about _mr_, I probably wouldn't have started gitbunch. On the other hand,
74 gitbunch is simpler (I think), doesn't require any config file, and can
75 copy/sync files/directories not under source control. I mainly use gitbunch to
76 quickly: 1) check whether there are any of my repositories which have
77 uncommitted changes; 2) synchronize (pull/push) to other locations. I put all my
78 data in one big gitbunch directory; I find it simpler. gitbunch works for me and
79 I use it daily.
80  
81 MARKDOWN
82 },
83 ],
84 };
85  
86 our %common_args = (
87 source => {
88 summary => 'Directory to check',
89 schema => ['str*'],
90 req => 1,
91 pos => 0,
92 },
93 include_repos => {
94 summary => 'Specific git repos to sync, if not specified '.
95 'all repos in the bunch will be processed',
96 schema => ['array' => {
97 of => 'str*',
98 }],
99 tags => ['filter'],
100 },
101 repo => {
102 summary => 'Only process a single repo',
103 schema => 'str*',
104 tags => ['filter'],
105 },
106 # XXX option to only process a single non-git dir?
107 # XXX option to only process a single file?
108 include_repos_pat=> {
109 summary => 'Specify regex pattern of repos to include',
110 schema => ['str'],
111 tags => ['filter'],
112 },
113 exclude_repos => {
114 summary => 'Exclude some repos from processing',
115 schema => ['array*' => {of => 'str*'}],
116 tags => ['filter'],
117 },
118 exclude_non_git_dirs => {
119 summary => 'Exclude non-git dirs from processing',
120 schema => ['bool'],
121 description => <<'MARKDOWN',
122  
123 This only applies to and `sync_bunch` operations. Operations like `check_bunch`
124 and `exec_bunch` already ignore these and only operate on git repos.
125  
126 MARKDOWN
127 cmdline_aliases => {
128 include_non_git_dirs => {
129 summary => 'Alias for --no-exclude-non-git-dirs',
130 schema => ['bool*', is=>1],
131 0 ▶ code => sub { $_[0]{exclude_non_git_dirs} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 },
133 },
134 tags => ['filter'],
135 },
136 exclude_files => {
137 summary => 'Exclude files from processing',
138 schema => ['bool'],
139 description => <<'MARKDOWN',
140  
141 This only applies to `sync_bunch` operations. Operations like `check_bunch` and
142 `exec_bunch` already ignore these and only operate on git repos.
143  
144 MARKDOWN
145 cmdline_aliases => {
146 include_files => {
147 summary => 'Alias for --no-exclude-files',
148 schema => ['bool*', is=>1],
149 0 ▶ code => sub { $_[0]{exclude_non_git_dirs} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 },
151 },
152 tags => ['filter'],
153 },
154 exclude_repos_pat=> {
155 summary => 'Specify regex pattern of repos to exclude',
156 schema => ['str'],
157 tags => ['filter'],
158 },
159 min_repo_access_time => {
160 summary => 'Limit to repos that are accessed (mtime, committed, status-ed, pushed) recently',
161 description => <<'MARKDOWN',
162  
163 This can significantly reduce the time to process the bunch if you are only
164 interested in recent repos (which is most of the time unless you are doing a
165 full check/sync).
166  
167 MARKDOWN
168 schema => ['date*', 'x.perl.coerce_rules' => ['!From_float::epoch', 'From_float::epoch_always', 'From_str::natural']],
169 cmdline_aliases => {
170 0 ▶ recent => {is_flag=>1, summary=>'Shortcut for --min-repo-access-time="2 weeks ago"', code=>sub { $_[0]{min_repo_access_time} = time() - 14*86400} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 ▶ r => {is_flag=>1, summary=>'Shortcut for --min-repo-access-time="2 weeks ago"', code=>sub { $_[0]{min_repo_access_time} = time() - 14*86400} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 },
173 2 tags => ['filter'],
174 },
175 );
176  
177 2 our %sort_args = (
178 sort => {
179 summary => 'Order entries',
180 schema => ['str' => {
181 in => [qw/name -name
182 mtime -mtime
183 commit_time -commit_time
184 status_time -status_time
185 pull_time -pull_time
186 /],
187 }],
188 },
189 );
190  
191 2 our %target_args = (
192 target => {
193 summary => 'Destination bunch',
194 schema => ['str*'],
195 req => 1,
196 pos => 1,
197 },
198 );
199  
200 2 our %remote_ssh_args = (
201 ssh_user => {
202 summary => 'Remote SSH user',
203 schema => ['str*', match=>qr/\A[\w-]+\z/],
204 default => 22,
205 },
206 ssh_host => {
207 summary => 'Remote SSH host',
208 schema => ['net::hostname*'],
209 req => 1,
210 },
211 ssh_port => {
212 summary => 'Remote SSH port',
213 schema => ['net::port*'],
214 default => 22,
215 },
216 ssh_path => {
217 summary => 'Remote host path to the bunch directory',
218 schema => ['pathname*'],
219 default => 22,
220 },
221 );
222  
223 2 our %command_opts_args = (
224 command_opts => {
225 summary => "Options to pass to IPC::System::Options's system()",
226 schema => ['hash*'],
227 },
228 );
229  
230 0 ▶sub _check_common_args {
Subroutine _check_common_args: not called CC=16 SCAR=56.1
231 0 my ($args, $requires_target) = @_;
232 0 my $res;
233  
234 0 ▶ $args->{source} or return [400, "Please specify source"];
Branch: 1 branch
BranchTrueFalseTotal
$args->{"source"} or return [400, "Please specify source"] 0 0 0
235 0 $args->{source} =~ s!/+$!!;
236 0 $res = _check_bunch_sanity(\$args->{source}, 'Source');
237 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
238  
239 0 my $ir = $args->{include_repos};
240 0 ▶ return [400, "include_repos must be an array"]
Branch: 1 branch
BranchTrueFalseTotal
if defined $ir and ref $ir ne "ARRAY" 0 0 0
Condition0%0/3
defined $ir and ref $ir ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ir and ref $ir ne "ARRAY"
defined $ir ref $ir ne "ARRAY"
Truth table0%0/3
A and B
A= defined $irB= ref $ir ne "ARRAY"
ABresult
0X0
100
111
241 if defined($ir) && ref($ir) ne 'ARRAY';
242 0 my $irp = $args->{include_repos_pat};
243 0 ▶ if (defined $irp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irp) 0 0 0
244 0 ▶ return [400, "Invalid include_repos_pat: must be a string"]
Branch: 1 branch
BranchTrueFalseTotal
if ref $irp 0 0 0
245 if ref($irp);
246 0 ▶ return [400, "Invalid include_repos_pat: $@"]
Branch: 1 branch
BranchTrueFalseTotal
unless eval "qr/\$irp/" 0 0 0
247 if !(eval q{qr/$irp/}); ## no critic: TestingAndDebugging::ProhibitNoStrict
248 }
249 0 my $er = $args->{exclude_repos};
250 0 ▶ return [400, "exclude_repos must be an array"]
Branch: 1 branch
BranchTrueFalseTotal
if defined $er and ref $er ne "ARRAY" 0 0 0
Condition0%0/3
defined $er and ref $er ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $er and ref $er ne "ARRAY"
defined $er ref $er ne "ARRAY"
Truth table0%0/3
A and B
A= defined $erB= ref $er ne "ARRAY"
ABresult
0X0
100
111
251 if defined($er) && ref($er) ne 'ARRAY';
252 0 my $erp = $args->{exclude_repos_pat};
253 0 ▶ if (defined $erp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $erp) 0 0 0
254 0 ▶ return [400, "Invalid exclude_repos_pat: must be a string"]
Branch: 1 branch
BranchTrueFalseTotal
if ref $erp 0 0 0
255 if ref($erp);
256 0 ▶ return [400, "Invalid exclude_repos_pat: must be a string"]
Branch: 1 branch
BranchTrueFalseTotal
unless eval "qr/\$erp/" 0 0 0
257 if !(eval q{qr/$erp/}); ## no critic: TestingAndDebugging::ProhibitNoStrict
258 }
259  
260 0 ▶ if ($requires_target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires_target) 0 0 0
261 0 ▶ $args->{target} or return [400, "Please specify target"];
Branch: 1 branch
BranchTrueFalseTotal
$args->{"target"} or return [400, "Please specify target"] 0 0 0
262 0 $res = _check_bunch_sanity(\$args->{target}, 'Target', 0);
263 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
264 }
265  
266 0 [200];
267 }
268  
269 # return 1 if normal git repo, 2 if bare git repo, 0 if not repo
270 0 ▶sub _is_repo {
Subroutine _is_repo: not called CC=5 SCAR=34.0
271 0 my $dir = shift;
272  
273 0 ▶ return 0 unless (-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
274 0 ▶ return 1 if (-d "$dir/.git");
Branch: 1 branch
BranchTrueFalseTotal
if -d "$dir/.git" 0 0 0
275 0 ▶ return 2 if (-d "$dir/branches") && (-f "$dir/HEAD");
Branch: 1 branch
BranchTrueFalseTotal
if -d "$dir/branches" and -f "$dir/HEAD" 0 0 0
Condition0%0/3
-d "$dir/branches" and -f "$dir/HEAD"
!ll&&!rl&&r
000
MC/DC0%0/2
-d "$dir/branches" and -f "$dir/HEAD"
-d "$dir/branches" -f "$dir/HEAD"
Truth table0%0/3
A and B
A= -d "$dir/branches"B= -f "$dir/HEAD"
ABresult
0X0
100
111
276 0 0;
277 }
278  
279 # return true if entry should be skipped
280 0 ▶sub _skip_process_entry {
Subroutine _skip_process_entry: not called CC=22 SCAR=62.3
281 0 my ($e, $args, $dir, $skip_non_repo) = @_;
282  
283 # skip special files
284 0 ▶ if ($e->{name} =~ /\A(repos\.db|\.gitbunch-sync-timestamp)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"name"} =~ /\A(repos\.db|\.gitbunch-sync-timestamp)\z/) 0 0 0
285 0 log_debug("Skipped $e->{name} (special files)");
286 0 return 1;
287 }
288  
289 0 my $is_repo = $e->{type} eq 'r';
290  
291 0 ▶ if (defined $args->{repo}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"repo"}) 0 0 0
292 # avoid logging all the skipped messages if user just wants to process a
293 # single repo
294 0 ▶ return 1 unless $is_repo;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_repo 0 0 0
295 0 ▶ return 1 unless $args->{repo} eq $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"repo"} eq $e 0 0 0
296 0 return 0;
297 }
298  
299 0 ▶ if ($skip_non_repo && !$is_repo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_non_repo and not $is_repo) 0 0 0
Condition0%0/3
$skip_non_repo and not $is_repo
!ll&&!rl&&r
000
MC/DC0%0/2
$skip_non_repo and not $is_repo
$skip_non_repo not $is_repo
Truth table0%0/3
A and not(B)
A= $skip_non_repoB= not $is_repo
ABresult
0X0
100
111
300 0 log_debug("Skipped $e->{name} (not a git repo), ".
301 "please remove it or rename to .$e->{name}");
302 0 return 1;
303 }
304 0 ▶ if ($is_repo) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_repo) { } 0 0 0
elsif (-f $dir and $args->{'exclude_files'}) { } 0 0 0
elsif (-d $dir and $args->{'exclude_non_git_dirs'}) { } 0 0 0
305 0 my $ir = $args->{include_repos};
306 0 ▶ if ($ir && !(grep { $_ eq $e->{name} } @$ir)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir and not grep({$_ eq $e->{"name"};} @$ir)) 0 0 0
Condition0%0/3
$ir and not grep({$_ eq $e->{"name"};} @$ir)
!ll&&!rl&&r
000
MC/DC0%0/2
$ir and not grep({$_ eq $e->{"name"};} @$ir)
$ir not grep({$_ eq $e->{"name"};} @$ir)
Truth table0%0/3
A and not(B)
A= $irB= not grep({$_ eq $e->{"name"};} @$ir)
ABresult
0X0
100
111
307 0 log_debug("Skipped $e->{name} (not in include_repos)");
308 0 return 1;
309 }
310 0 my $irp = $args->{include_repos_pat};
311 0 ▶ if (defined($irp) && $e->{name} !~ qr/$irp/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irp and not $e->{"name"} =~ qr/$irp/) 0 0 0
Condition0%0/3
defined $irp and not $e->{"name"} =~ qr/$irp/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $irp and not $e->{"name"} =~ qr/$irp/
defined $irp not $e->{"name"} =~ qr/$irp/
Truth table0%0/3
A and not(B)
A= defined $irpB= not $e->{"name"} =~ qr/$irp/
ABresult
0X0
100
111
312 0 log_debug("Skipped $e->{name} (not matched include_repos_pat)");
313 0 return 1;
314 }
315 0 my $er = $args->{exclude_repos};
316 0 ▶ if ($er && grep { $_ eq $e->{name} } @$er) {
Branch: 1 branch
BranchTrueFalseTotal
if ($er and grep {$_ eq $e->{"name"};} @$er) 0 0 0
Condition0%0/3
$er and grep {$_ eq $e->{"name"};} @$er
!ll&&!rl&&r
000
MC/DC0%0/2
$er and grep {$_ eq $e->{"name"};} @$er
$er grep {$_ eq $e->{"name"};} @$er
Truth table0%0/3
A and B
A= $erB= grep {$_ eq $e->{"name"};} @$er
ABresult
0X0
100
111
317 0 log_debug("Skipped $e->{name} (in exclude_repos)");
318 0 return 1;
319 }
320 0 my $erp = $args->{exclude_repos_pat};
321 0 ▶ if (defined($erp) && $e->{name} =~ qr/$erp/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $erp and $e->{"name"} =~ qr/$erp/) 0 0 0
Condition0%0/3
defined $erp and $e->{"name"} =~ qr/$erp/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $erp and $e->{"name"} =~ qr/$erp/
defined $erp $e->{"name"} =~ qr/$erp/
Truth table0%0/3
A and B
A= defined $erpB= $e->{"name"} =~ qr/$erp/
ABresult
0X0
100
111
322 0 log_debug("Skipped $e->{name} (not matched exclude_repos_pat)");
323 0 return 1;
324 }
325 0 my $min_rat = $args->{min_repo_access_time};
326 0 ▶ if ($min_rat && max(grep {defined} $e->{mtime}, $e->{commit_time}, $e->{status_time}, $e->{pull_time}) < $min_rat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_rat and &max(grep({defined $_;} $e->{"mtime"}, $e->{"commit_time"}, $e->{"status_time"}, $e->{"pull_time"})) < $min_rat) 0 0 0
Condition0%0/3
$min_rat and &max(grep({defined $_;} $e->{"mtime"}, $e->{"commit_time"}, $e->{"status_time"}, $e->{"pull_time"})) < $min_rat
!ll&&!rl&&r
000
MC/DC0%0/2
$min_rat and &max(grep({defined $_;} $e->{"mtime"}, $e->{"commit_time"}, $e->{"status_time"}, $e->{"pull_time"})) < $min_rat
$min_rat &max(grep({defined $_;} $e->{"mtime"}, $e->{"commit_time"}, $e->{"status_time"}, $e->{"pull_time"})) < $min_rat
Truth table0%0/3
A and B
A= $min_ratB= &max(grep({defined $_;} $e->{"mtime"}, $e->{"commit_time"}, $e->{"status_time"}, $e->{"pull_time"})) < $min_rat
ABresult
0X0
100
111
327 0 log_debug("Skipped $e->{name} (doesn't pass min_repo_access_time)");
328 0 return 1;
329 }
330 ▶ } elsif ((-f $dir) && $args->{exclude_files}) {
Condition0%0/3
-f $dir and $args->{"exclude_files"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $dir and $args->{"exclude_files"}
-f $dir $args->{"exclude_files"}
Truth table0%0/3
A and B
A= -f $dirB= $args->{"exclude_files"}
ABresult
0X0
100
111
331 0 log_debug("Skipped $e->{name} (exclude_files)");
332 0 return 1;
333 ▶ } elsif ((-d $dir) && $args->{exclude_non_git_dirs}) {
Condition0%0/3
-d $dir and $args->{"exclude_non_git_dirs"}
!ll&&!rl&&r
000
MC/DC0%0/2
-d $dir and $args->{"exclude_non_git_dirs"}
-d $dir $args->{"exclude_non_git_dirs"}
Truth table0%0/3
A and B
A= -d $dirB= $args->{"exclude_non_git_dirs"}
ABresult
0X0
100
111
334 0 log_debug("Skipped $e->{name} (exclude_non_git_dirs)");
335 0 return 1;
336 }
337 0 return 0;
338 }
339  
340 0 ▶sub _skip_process_repo {
Subroutine _skip_process_repo: not called CC=1 SCAR=6.9
341 0 my ($repo, $args, $dir) = @_;
342 0 _skip_process_entry($repo, $args, $dir, 1);
343 }
344  
345 0 ▶sub _check_bunch_sanity {
Subroutine _check_bunch_sanity: not called CC=6 SCAR=37.4
346 0 my ($path_ref, $title, $must_exist) = @_;
347 0 ▶ $title //= "Directory";
Condition0%0/2
$title //= "Directory"
l!l
00
MC/DC0%0/1
$title //= "Directory"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
348 0 $$path_ref =~ s!/+$!!;
349 0 ▶ if ($must_exist // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($must_exist // 1) 0 0 0
Condition0%0/2
$must_exist // 1
l!l
00
MC/DC0%0/1
$must_exist // 1
$must_exist
Truth table0%0/2
A
A= $must_exist
Aresult
11
00
350 0 ▶ (-d $$path_ref) or return [404, "$title doesn't exist"];
Branch: 1 branch
BranchTrueFalseTotal
-d $$path_ref or return [404, "$title doesn't exist"] 0 0 0
351 }
352 0 ▶ _is_repo($$path_ref) and
Branch: 1 branch
BranchTrueFalseTotal
_is_repo($$path_ref) and return [400, "$title is probably a git repo, " . 'you should specify a dir *containing* ' . "git repos instead"] 0 0 0
353 return [400, "$title is probably a git repo, ".
354 "you should specify a dir *containing* ".
355 "git repos instead"];
356 0 [200, "OK"];
357 }
358  
359 0 ▶sub _list {
Subroutine _list: not called CC=11 SCAR=48.8
360 0 my $args = shift;
361  
362 0 my @entries;
363 0 @entries = do {
364 0 ▶ opendir my ($dh), "." or die "Can't read dir '$args->{source}': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "." or die qq[Can't read dir '$args->{"source"}': $!] 0 0 0
365 0 ▶ map { +{name => $_} } grep { $_ ne '.' && $_ ne '..' } readdir($dh);
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
366 };
367 0 for my $e (@entries) {
368 0 my @st = stat $e->{name};
369 0 $e->{mtime} = $st[9];
370 0 ▶ if (-d _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) { } 0 0 0
371 0 ▶ if ($e->{name} =~ /\A\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{'name'} =~ /\A\./) { } 0 0 0
372 0 $e->{type} = 'd';
373  
374 # to save stat() call, we assume any dir that does not start
375 # with dot to be a repo
376  
377 #} elsif (-d "$e->{name}/.git") {
378 # $e->{type} = 'r';
379  
380 } else {
381 0 $e->{type} = 'r';
382 }
383 } else {
384 0 $e->{type} = 'f';
385 }
386 }
387 {
388 #last unless $sort =~ /\A-?(commit_time|status_time|pull_time)/;
389 0 ▶ last unless -f "repos.db";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "repos.db" 0 0 0
390 0 require DBI;
391 0 my $dbh = DBI->connect("dbi:SQLite:dbname=repos.db", "", "",
392 {RaiseError=>1});
393 0 my $sth = $dbh->prepare("SELECT * FROM repos");
394 0 $sth->execute;
395 0 my %rows;
396 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
397 0 $rows{$row->{name}} = $row;
398 }
399 0 for my $e (@entries) {
400 0 ▶ next unless my $row = $rows{$e->{name}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $row = $rows{$e->{"name"}} 0 0 0
401 0 for (qw/commit_time status_time pull_time/) {
402 0 $e->{$_} = $row->{$_};
403 }
404 }
405 }
406 0 @entries;
407 }
408  
409 0 ▶sub _sort_entries_by_recent {
Subroutine _sort_entries_by_recent: not called CC=1 SCAR=1.7
410 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
411 sort {
412 0 my $sort_a = max($a->{commit_time}, $a->{pull_time}, $a->{status_time}, $a->{mtime});
413 0 my $sort_b = max($b->{commit_time}, $b->{pull_time}, $b->{status_time}, $b->{mtime});
414 0 $sort_b <=> $sort_a;
415 } @_;
416 }
417  
418 $SPEC{check_bunch} = {
419 2 v => 1.1,
420 summary =>
421 'Check status of git repositories inside gitbunch directory',
422 description => <<'MARKDOWN',
423  
424 Will perform a 'git status' for each git repositories inside the bunch and
425 report which repositories are clean/unclean.
426  
427 Will die if can't chdir into bunch or git repository.
428  
429 MARKDOWN
430 args => {
431 %common_args,
432 %sort_args,
433 },
434 deps => {
435 all => [
436 {prog => 'git'},
437 ],
438 },
439 features => {
440 progress => 1,
441 dry_run => 1,
442 },
443 };
444 0 ▶sub check_bunch {
Subroutine check_bunch: not called CC=30 SCAR=68.4
Pod documented
445 0 my %args = @_;
446 0 my $res;
447  
448 0 my $progress = $args{-progress};
449  
450 # XXX schema
451 0 $res = _check_common_args(\%args);
452 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
453 0 my $source = $args{source};
454  
455 #log_info("Checking bunch $source ...");
456 0 ▶ log($args{_loglevel} // 'info', "Checking bunch $source ...");
Condition0%0/2
$args{"_loglevel"} // "info"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "info"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
457  
458 0 my $has_unclean;
459 my %res;
460 0 local $CWD = $source;
461  
462 0 my @entries = _list(\%args);
463  
464 0 my $i = 0;
465 0 ▶ $progress->pos(0) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
466 0 ▶ $progress->target(scalar @entries) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
467 REPO:
468 0 for my $e (@entries) {
469 0 my $repo = $e->{name};
470 0 ▶ next REPO if _skip_process_repo($e, \%args, ".");
Branch: 1 branch
BranchTrueFalseTotal
if _skip_process_repo($e, \%args, ".") 0 0 0
471 0 ▶ $CWD = $i++ ? "../$repo" : $repo;
Branch: 1 branch
BranchTrueFalseTotal
$i++ ? : 0 0 0
472  
473 0 ▶ $progress->update(pos => $i,
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
474 message =>
475 "Checking repo $repo ...")
476 if $progress;
477  
478 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
479 #log_info("[DRY-RUN] checking status of repo %s", $repo);
480 0 ▶ log($args{_loglevel} // 'info', "[DRY-RUN] checking status of repo %s", $repo);
Condition0%0/2
$args{"_loglevel"} // "info"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "info"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
481 0 next REPO;
482 }
483  
484 0 my $output = readpipe("git status 2>&1");
485 0 my $exit = $? >> 8;
486 0 ▶ if ($exit == 0 && $output =~ /nothing to commit/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 0 and $output =~ /nothing to commit/) 0 0 0
Condition0%0/3
$exit == 0 and $output =~ /nothing to commit/
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /nothing to commit/
$exit == 0 $output =~ /nothing to commit/
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /nothing to commit/
ABresult
0X0
100
111
487 #log_info("$repo is clean");
488 0 ▶ log($args{_loglevel} // 'info', "$repo is clean");
Condition0%0/2
$args{"_loglevel"} // "info"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "info"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
489 0 $res{$repo} = [200, "Clean"];
490 0 next;
491 }
492  
493 0 $has_unclean++;
494 0 ▶ if ($exit == 0 && $output =~ /^\s*Unmerged paths:/m) {
Branch: 4 branches
BranchTrueFalseTotal
if ($exit == 0 and $output =~ /^\s*Unmerged paths:/m) { } 0 0 0
elsif ($exit == 0 and $output =~ /( Untracked \s files )/x) { } 0 0 0
elsif ($exit == 0 and $output =~ /( Changes \s to \s be \s committed | Changes \s not \s staged \s for \s commit | Changed \s but )/mx) { } 0 0 0
elsif ($exit == 128 and $output =~ /Not a git repository/) { } 0 0 0
Condition0%0/3
$exit == 0 and $output =~ /^\s*Unmerged paths:/m
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /^\s*Unmerged paths:/m
$exit == 0 $output =~ /^\s*Unmerged paths:/m
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /^\s*Unmerged paths:/m
ABresult
0X0
100
111
495 #log_warn("$repo needs merging");
496 0 ▶ log($args{_loglevel} // 'warn', "$repo needs merging");
Condition0%0/2
$args{"_loglevel"} // "warn"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "warn"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
497 0 $res{$repo} = [500, "Needs merging"];
498 ▶ } elsif ($exit == 0 &&
Condition0%0/3
$exit == 0 and $output =~ /( Untracked \s files )/x
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /( Untracked \s files )/x
$exit == 0 $output =~ /( Untracked \s files )/x
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /( Untracked \s files )/x
ABresult
0X0
100
111
499 $output =~ /(
500 Untracked \s files
501 )/x) {
502 #log_warn("$repo has untracked files");
503 0 ▶ log($args{_loglevel} // 'warn', "$repo has untracked files");
Condition0%0/2
$args{"_loglevel"} // "warn"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "warn"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
504 0 $res{$repo} = [500, "Has untracked files"];
505 ▶ } elsif ($exit == 0 &&
Condition0%0/3
$exit == 0 and $output =~ /( Changes \s to \s be \s committed | Changes \s not \s staged \s for \s commit | Changed \s but )/mx
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /( Changes \s to \s be \s committed | Changes \s not \s staged \s for \s commit | Changed \s but )/mx
$exit == 0 $output =~ /( Changes \s to \s be \s committed | Changes \s not \s staged \s for \s commit | Changed \s but )/mx
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /( Changes \s to \s be \s committed | Changes \s not \s staged \s for \s commit | Changed \s but )/mx
ABresult
0X0
100
111
506 $output =~ /(
507 Changes \s to \s be \s committed |
508 Changes \s not \s staged \s for \s commit |
509 Changed \s but
510 )/mx) {
511 #log_warn("$repo needs commit");
512 0 ▶ log($args{_loglevel} // 'warn', "$repo needs commit");
Condition0%0/2
$args{"_loglevel"} // "warn"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "warn"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
513 0 $res{$repo} = [500, "Needs commit"];
514 ▶ } elsif ($exit == 128 && $output =~ /Not a git repository/) {
Condition0%0/3
$exit == 128 and $output =~ /Not a git repository/
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 128 and $output =~ /Not a git repository/
$exit == 128 $output =~ /Not a git repository/
Truth table0%0/3
A and B
A= $exit == 128B= $output =~ /Not a git repository/
ABresult
0X0
100
111
515 #log_warn("$repo is not a git repo (2)");
516 0 ▶ log($args{_loglevel} // 'warn', "$repo is not a git repo (2)");
Condition0%0/2
$args{"_loglevel"} // "warn"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "warn"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
517 0 $res{$repo} = [500, "Not a git repo (2)"];
518 } else {
519 #log_warn(
520 # "Can't figure out result of 'git status' ".
521 # "for repo $repo: exit=$exit, output=$output");
522 0 ▶ log($args{_loglevel} // 'error',
Condition0%0/2
$args{"_loglevel"} // "error"
l!l
00
MC/DC0%0/1
$args{"_loglevel"} // "error"
$args{"_loglevel"}
Truth table0%0/2
A
A= $args{"_loglevel"}
Aresult
11
00
523 "Can't figure out result of 'git status' ".
524 "for repo $repo: exit=$exit, output=$output");
525 0 $res{$repo} = [500, "Unknown (exit=$exit, output=$output)"];
526 }
527 }
528 0 ▶ $progress->finish if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
529 0 ▶ [200,
Branch: 2 branches
BranchTrueFalseTotal
$has_unclean ? : 0 0 0
$has_unclean ? : 0 0 0
530 $has_unclean ? "Some repos unclean" : "All repos clean",
531 \%res,
532 {
533 'cmdline.result' => '', 'func.res'=>\%res,
534 'cmdline.exit_code' => $has_unclean ? 1:0,
535 }];
536 }
537  
538 $SPEC{list_bunch_contents} = {
539 2 v => 1.1,
540 summary =>
541 'List contents inside gitbunch directory',
542 description => <<'MARKDOWN',
543  
544 Will list each repo or non-repo dir/file.
545  
546 MARKDOWN
547 args => {
548 %common_args,
549 %sort_args,
550 detail => {
551 summary =>
552 'Show detailed record for each entry instead of just its name',
553 schema => 'bool',
554 cmdline_aliases => {l => {}},
555 },
556 },
557 features => {
558 },
559 };
560 0 ▶sub list_bunch_contents {
Subroutine list_bunch_contents: not called CC=9 SCAR=43.3
Pod documented
561 0 my %args = @_;
562  
563 # XXX schema
564 0 my $res = _check_common_args(\%args);
565 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
566 0 my $source = $args{source};
567 0 ▶ my $sort = $args{sort} // '';
Condition0%0/2
$args{"sort"} // ""
l!l
00
MC/DC0%0/1
$args{"sort"} // ""
$args{"sort"}
Truth table0%0/2
A
A= $args{"sort"}
Aresult
11
00
568  
569 0 local $CWD = $source;
570  
571 0 my @entries = _list(\%args);
572  
573 0 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 0 0 0
574 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
575 0 my $sortsub;
576 0 my ($rev, $field);
577 0 ▶ if (($rev, $field) = $sort =~ /\A(-)?(mtime|commit_time|status_time|pull_time)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $sort =~ /\A(-)?(mtime|commit_time|status_time|pull_time)/) { } 0 0 0
elsif (() = $sort =~ /\A(-)?(name)/) { } 0 0 0
578 0 ▶ $sortsub = sub { ($rev ? -1:1) * ($a->{$field} <=> $b->{$field}) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 0 0
579 } elsif (($rev, $field) = $sort =~ /\A(-)?(name)/) {
580 0 ▶ $sortsub = sub { ($rev ? -1:1) * ($a->{$field} cmp $b->{$field}) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 0 0
581 }
582 0 @entries = sort $sortsub @entries;
583 }
584 #log_trace("entries: %s", \@entries);
585  
586 0 my @res;
587 ENTRY:
588 0 for my $e (@entries) {
589 0 ▶ next ENTRY if _skip_process_entry($e, \%args, ".");
Branch: 1 branch
BranchTrueFalseTotal
if _skip_process_entry($e, \%args, ".") 0 0 0
590 0 push @res, $e;
591 }
592  
593 0 my %resmeta;
594 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 0 0 0
595 0 $resmeta{'table.fields'} =
596 [qw/name type mtime commit_time status_time pull_time/];
597 0 $resmeta{'table.field_formats'} =
598 [undef, undef, 'iso8601_datetime', 'iso8601_datetime', 'iso8601_datetime', 'iso8601_datetime'];
599 } else {
600 0 @res = map { $_->{name} } @res;
601 }
602 0 [200, "OK", \@res, \%resmeta];
603 }
604  
605 0 ▶sub _sync_repo {
Subroutine _sync_repo: not called CC=30 SCAR=68.4
606 0 my ($src, $dest, $repo, $opts) = @_;
607 0 my $exit;
608  
609 my @src_branches;
610 0 my @dest_branches;
611 0 my %src_heads; # last revisions for each branch
612 0 my %dest_heads; # last revisions for each branch
613  
614 0 local $CWD = "$src/$repo";
615 0 @src_branches = map {(/^[* ] (.+)/, $1)[-1]} readpipe("git branch");
616 0 $exit = $? >> 8;
617 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
618 0 log_error("Can't list branches on src repo $src/$repo: $exit");
619 0 return [500, "Can't list source branches"];
620 }
621 0 log_debug("Source branches: %s", \@src_branches);
622  
623 0 for my $branch (@src_branches) {
624 0 my $output = readpipe("git log -1 '$branch'");
625 0 $exit = $? >> 8;
626 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
627 0 log_error("Can't find out head for branch $branch on src repo ".
628 "$src/$repo: $exit");
629 0 return [500, "Can't find out head for source branch $branch"];
630 }
631 0 ▶ $output =~ /commit (\S+)/ or do {
Branch: 1 branch
BranchTrueFalseTotal
$output =~ /commit (\S+)/ or do { log_error(q[Can't recognize git log output ] . "(searching for commit XXX): $output"); return [500, "Can't recognize git log output on src: $output"] } 0 0 0
632 0 log_error("Can't recognize git log output ".
633 "(searching for commit XXX): $output");
634 0 return [500, "Can't recognize git log output on src: $output"];
635 };
636 0 $src_heads{$branch} = $1;
637 }
638 0 log_debug("Source branch heads: %s", \%src_heads);
639  
640 0 $CWD = "$dest/$repo";
641 0 my $is_bare = _is_repo(".") == 2;
642 0 @dest_branches = map {(/^[* ] (.+)/, $1)[-1]} readpipe("git branch");
643 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
644 0 log_error("Can't list branches on dest repo $repo: $exit");
645 0 return [500, "Can't list branches on dest: $exit"];
646 }
647 0 log_debug("Dest branches: %s", \@dest_branches);
648 0 for my $branch (@dest_branches) {
649 0 my $output = readpipe("git log -1 '$branch'");
650 0 $exit = $? >> 8;
651 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
652 0 log_error("Can't find out head for branch $branch on dest repo ".
653 "$dest/$repo: $exit");
654 0 return [500, "Can't find out head for dest branch $branch"];
655 }
656 0 ▶ $output =~ /commit (\S+)/ or do {
Branch: 1 branch
BranchTrueFalseTotal
$output =~ /commit (\S+)/ or do { log_error(q[Can't recognize git log output ] . "(searching for commit XXX): $output"); return [500, "Can't recognize git log output on src: $output"] } 0 0 0
657 0 log_error("Can't recognize git log output ".
658 "(searching for commit XXX): $output");
659 0 return [500, "Can't recognize git log output on src: $output"];
660 };
661 0 $dest_heads{$branch} = $1;
662 }
663 0 log_debug("Dest branch heads: %s", \%dest_heads);
664  
665 0 my $output;
666 my $lock_deleted;
667 0 my $changed_branch;
668 BRANCH:
669 0 for my $branch (@src_branches) {
670 # XXX we should allow fetching tags only even if head is the same, but
671 # right now tags are not that important
672 0 ▶ if ($src_heads{$branch} && $dest_heads{$branch} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($src_heads{$branch} and $dest_heads{$branch} and $src_heads{$branch} eq $dest_heads{$branch}) 0 0 0
Condition0%0/6
$src_heads{$branch} and $dest_heads{$branch} and $src_heads{$branch} eq $dest_heads{$branch}
!ll&&!rl&&r
000
$src_heads{$branch} and $dest_heads{$branch}
!ll&&!rl&&r
000
MC/DC0%0/3
$src_heads{$branch} and $dest_heads{$branch} and $src_heads{$branch} eq $dest_heads{$branch}
$src_heads{$branch} $dest_heads{$branch} $src_heads{$branch} eq $dest_heads{$branch}
Truth table0%0/4
A and B and C
A= $src_heads{$branch}B= $dest_heads{$branch}C= $src_heads{$branch} eq $dest_heads{$branch}
ABCresult
0XX0
10X0
1100
1111
673 $src_heads{$branch} eq $dest_heads{$branch}) {
674 0 log_debug("Skipping branch $branch because heads are the same");
675 0 next BRANCH;
676 }
677 0 $changed_branch++;
678 0 if (0 && !$lock_deleted++) {
679 log_debug("Deleting locks first ...");
680 unlink "$src/$repo" .($is_bare ? "" : "/.git")."/index.lock";
681 unlink "$dest/$repo".($is_bare ? "" : "/.git")."/index.lock";
682 }
683 0 ▶ log_info("Updating branch $branch of repo $repo ...")
Branch: 1 branch
BranchTrueFalseTotal
if @src_branches > 1 0 0 0
684 if @src_branches > 1;
685 0 ▶ if ($is_bare) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_bare) { } 0 0 0
686 0 $output = readpipe(
687 join("",
688 "cd '$src/$repo'; ",
689 "git push '$dest/$repo' '$branch' 2>&1",
690 ));
691 } else {
692 $output = readpipe(
693 join("",
694 "cd '$dest/$repo'; ",
695 0 ▶ ((grep { $_ eq $branch } @dest_branches) ? "":"git branch '$branch'; "),
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $branch;} @dest_branches) ? : 0 0 0
696 "git checkout '$branch' 2>/dev/null; ",
697 "git pull '$src/$repo' '$branch' 2>&1"
698 ));
699 }
700 0 $exit = $? >> 8;
701 0 ▶ if ($exit == 0 && $output =~ /Already up-to-date/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($exit == 0 and $output =~ /Already up-to-date/) { } 0 0 0
elsif ($output =~ /^error: (.+)/m) { } 0 0 0
elsif ($exit == 0 and $output =~ /^Updating \s| ^Merge \s made \s by \s recursive| ^Merge \s made \s by \s the \s 'recursive'| /mx) { } 0 0 0
Condition0%0/3
$exit == 0 and $output =~ /Already up-to-date/
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /Already up-to-date/
$exit == 0 $output =~ /Already up-to-date/
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /Already up-to-date/
ABresult
0X0
100
111
702 0 log_debug("Branch $branch of repo $repo is up to date");
703 0 next BRANCH;
704 } elsif ($output =~ /^error: (.+)/m) {
705 0 log_error("Can't successfully git pull/push branch $branch of ".
706 "repo $repo: $1");
707 0 return [500, "git pull/push branch $branch failed: $1"];
708 ▶ } elsif ($exit == 0 &&
Condition0%0/3
$exit == 0 and $output =~ /^Updating \s| ^Merge \s made \s by \s recursive| ^Merge \s made \s by \s the \s 'recursive'| /mx
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and $output =~ /^Updating \s| ^Merge \s made \s by \s recursive| ^Merge \s made \s by \s the \s 'recursive'| /mx
$exit == 0 $output =~ /^Updating \s| ^Merge \s made \s by \s recursive| ^Merge \s made \s by \s the \s 'recursive'| /mx
Truth table0%0/3
A and B
A= $exit == 0B= $output =~ /^Updating \s| ^Merge \s made \s by \s recursive| ^Merge \s made \s by \s the \s 'recursive'| /mx
ABresult
0X0
100
111
709 $output =~ /^Updating \s|
710 ^Merge \s made \s by \s recursive|
711 ^Merge \s made \s by \s the \s 'recursive'|
712 /mx) {
713 0 ▶ log_warn("Branch $branch of repo $repo updated")
Branch: 1 branch
BranchTrueFalseTotal
if @src_branches > 1 0 0 0
714 if @src_branches > 1;
715 0 ▶ log_warn("Repo $repo updated")
Branch: 1 branch
BranchTrueFalseTotal
if @src_branches == 1 0 0 0
716 if @src_branches == 1;
717 } else {
718 0 log_error(
719 "Can't recognize 'git pull/push' output for branch ".
720 "$branch of repo $repo: exit=$exit, output=$output");
721 0 return [500, "Can't recognize git pull/push output: $output"];
722 }
723 0 log_debug("Result of 'git pull/push' for branch $branch of repo ".
724 "$repo: exit=$exit, output=$output");
725  
726 0 $output = readpipe("cd '$dest/$repo'; ".
727 "git fetch --tags '$src/$repo' 2>&1");
728 0 $exit = $? >> 8;
729 0 ▶ if ($exit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit != 0) 0 0 0
730 0 log_debug("Failed fetching tags: ".
731 "$output (exit=$exit)");
732 0 return [500, "git fetch --tags failed: $1"];
733 }
734 }
735  
736 0 ▶ if ($opts->{delete_branch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"delete_branch"}) 0 0 0
737 0 for my $branch (@dest_branches) {
738 0 ▶ next if grep { $_ eq $branch } @src_branches;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $branch;} @src_branches 0 0 0
739 0 ▶ next if $branch eq 'master'; # can't delete master branch
Branch: 1 branch
BranchTrueFalseTotal
if $branch eq "master" 0 0 0
740 0 $changed_branch++;
741 0 log_info("Deleting branch $branch of repo $repo because ".
742 "it no longer exists in src ...");
743 0 system("cd '$dest/$repo' && git checkout master 2>/dev/null && ".
744 "git branch -D '$branch' 2>/dev/null");
745 0 $exit = $? >> 8;
746 0 ▶ log_error("Failed deleting branch $branch of repo $repo: $exit")
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 0 0
747 if $exit;
748 }
749 }
750  
751 0 ▶ if ($changed_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed_branch) { } 0 0 0
752 0 return [200, "OK"];
753 } else {
754 0 return [304, "Not modified"];
755 }
756 }
757  
758 $SPEC{sync_bunch} = {
759 2 v => 1.1,
760 summary =>
761 'Synchronize bunch to another bunch',
762 description => <<'MARKDOWN',
763  
764 For each git repository in the bunch, will perform a 'git pull/push' for each
765 branch. If repository in destination doesn't exist, it will be rsync-ed first
766 from source. When 'git pull' fails, will exit to let you fix the problem
767 manually.
768  
769 For all other non-repo file/directory, will simply synchronize by one-way rsync.
770 But, for added safety, will first check the newest mtime (mtime of the newest
771 file or subdirectory) between source and target is checked first. If target
772 contains the newer newest mtime, rsync-ing for that non-repo file/dir will be
773 aborted. Note: you can use `--skip-mtime-check` option to skip this check.
774  
775 MARKDOWN
776 args => {
777 %common_args,
778 %target_args,
779 delete_branch => {
780 summary => 'Whether to delete branches in dest repos '.
781 'not existing in source repos',
782 schema => ['bool' => default => 0],
783 },
784 rsync_opt_maintain_ownership => {
785 summary => 'Whether or not, when rsync-ing from source, '.
786 'we use -a (= -rlptgoD) or -rlptD (-a minus -go)',
787 schema => ['bool' => default => 0],
788 description => <<'MARKDOWN',
789  
790 Sometimes using -a results in failure to preserve permission modes on
791 sshfs-mounted filesystem, while -rlptD succeeds, so by default we don't maintain
792 ownership. If you need to maintain ownership (e.g. you run as root and the repos
793 are not owned by root), turn this option on.
794  
795 MARKDOWN
796 },
797 rsync_del => {
798 summary => 'Whether to use --del rsync option',
799 schema => 'bool',
800 description => <<'MARKDOWN',
801  
802 When rsync-ing non-repos, by default `--del` option is not used for more safety
803 because rsync is a one-way action. To add rsync `--del` option, enable this
804  
805 MARKDOWN
806 },
807 skip_mtime_check => {
808 summary => 'Whether or not, when rsync-ing non-repos, '.
809 'we check mtime first',
810 schema => ['bool'],
811 description => <<'MARKDOWN',
812  
813 By default when we rsync a non-repo file/dir from source to target and both
814 exist, to protect wrong direction of sync-ing we find the newest mtime in source
815 or dir (if dir, then the dir is recursively traversed to find the file/subdir
816 with the newest mtime). If target contains the newer mtime, the sync for that
817 non-repo file/dir is aborted. If you want to force the rsync anyway, use this
818 option.
819  
820 MARKDOWN
821 cmdline_aliases => {M=>{}},
822 },
823 create_bare_target => {
824 summary => 'Whether to create bare git repo '.
825 'when target does not exist',
826 schema => ['bool'],
827 description => <<'MARKDOWN',
828  
829 When target repo does not exist, gitbunch can either copy the source repo using
830 `rsync` (the default, if this setting is undefined), or it can create target
831 repo with `git init --bare` (if this setting is set to 1), or it can create
832 target repo with `git init` (if this setting is set to 0).
833  
834 Bare git repositories contain only contents of the .git folder inside the
835 directory and no working copies of your source files.
836  
837 Creating bare repos are apt for backup purposes since they are more
838 space-efficient.
839  
840 Non-repos will still be copied/rsync-ed.
841  
842 MARKDOWN
843 cmdline_aliases => {
844 # old name, deprecated since v0.29, remove in later releases
845 use_bare => {},
846 },
847 },
848 backup => {
849 summary => 'Whether doing backup to target',
850 schema => ['bool'],
851 description => <<'MARKDOWN',
852  
853 This setting lets you express that you want to perform synchronizing to a backup
854 target, and that you do not do work on the target. Thus, you do not care about
855 uncommitted or untracked files/dirs in the target repos (might happen if you
856 also do periodic copying of repos to backup using cp/rsync). When this setting
857 is turned on, the function will first do a `git clean -f -d` (to delete
858 untracked files/dirs) and then `git checkout .` (to discard all uncommitted
859 changes). This setting will also implicitly turn on `create_bare` setting
860 (unless that setting has been explicitly enabled/disabled).
861  
862 MARKDOWN
863 },
864 action => {
865 schema => ['str*', in=>[
866 'sync',
867 'list-source-repos',
868 ]],
869 default => 'sync',
870 },
871 },
872 deps => {
873 all => [
874 {prog => 'git'},
875 {prog => 'rsync'},
876 {prog => 'rsync-new2old'},
877 {prog => 'touch'},
878 ],
879 },
880 features => {
881 progress => 1,
882 dry_run => 1,
883 },
884 };
885 0 ▶sub sync_bunch {
Subroutine sync_bunch: not called CC=48 SCAR=77.6
Pod documented
886 0 require Capture::Tiny;
887 0 require UUID::Random;
888 0 require App::reposdb;
889  
890 0 my %args = @_;
891 0 my $res;
892  
893 0 my $progress = $args{-progress};
894  
895 # XXX schema
896 0 $res = _check_common_args(\%args, 1);
897 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
898 0 ▶ my $delete_branch = $args{delete_branch} // 0;
Condition0%0/2
$args{"delete_branch"} // 0
l!l
00
MC/DC0%0/1
$args{"delete_branch"} // 0
$args{"delete_branch"}
Truth table0%0/2
A
A= $args{"delete_branch"}
Aresult
11
00
899 0 my $source = $args{source};
900 0 my $target = $args{target};
901 0 my $create_bare = $args{create_bare_target};
902 0 my $backup = $args{backup};
903 0 ▶ my $action = $args{action} // 'sync';
Condition0%0/2
$args{"action"} // "sync"
l!l
00
MC/DC0%0/1
$args{"action"} // "sync"
$args{"action"}
Truth table0%0/2
A
A= $args{"action"}
Aresult
11
00
904 0 my $exit;
905  
906 0 ▶ $create_bare //= 1 if $backup;
Branch: 1 branch
BranchTrueFalseTotal
if $backup 0 0 0
Condition0%0/2
$create_bare //= 1
l!l
00
MC/DC0%0/1
$create_bare //= 1
$create_bare
Truth table0%0/2
A
A= $create_bare
Aresult
11
00
907  
908 0 my $cmd;
909  
910 0 ▶ unless ((-d $target) || $args{-dry_run} || $action eq 'list-source-repos') {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $target or $args{"-dry_run"} or $action eq "list-source-repos") 0 0 0
Condition0%0/6
-d $target or $args{"-dry_run"} or $action eq "list-source-repos"
l!l&&r!l&&!r
000
-d $target or $args{"-dry_run"}
l!l&&r!l&&!r
000
MC/DC0%0/3
-d $target or $args{"-dry_run"} or $action eq "list-source-repos"
-d $target $args{"-dry_run"} $action eq "list-source-repos"
Truth table0%0/4
A or B or C
A= -d $targetB= $args{"-dry_run"}C= $action eq "list-source-repos"
ABCresult
1XX1
01X1
0011
0000
911 0 log_debug("Creating target directory %s ...", $target);
912 0 ▶ make_path($target)
Branch: 1 branch
BranchTrueFalseTotal
make_path($target) or return [500, "Can't create target directory ${target}: $!"] 0 0 0
913 or return [500, "Can't create target directory $target: $!"];
914 }
915 0 $target = Cwd::abs_path($target);
916  
917 0 my $dbh_target;
918 $dbh_target = App::reposdb::_connect_db({
919 reposdb_path => "$target/repos.db",
920 0 ▶ }) unless $args{-dry_run} || $action eq 'list-source-repos';
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-dry_run"} or $action eq "list-source-repos" 0 0 0
Condition0%0/3
$args{"-dry_run"} or $action eq "list-source-repos"
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"-dry_run"} or $action eq "list-source-repos"
$args{"-dry_run"} $action eq "list-source-repos"
Truth table0%0/3
A or B
A= $args{"-dry_run"}B= $action eq "list-source-repos"
ABresult
1X1
011
000
921  
922 0 ▶ my $_a = $args{rsync_opt_maintain_ownership} ? "aH" : "rlptDH";
Branch: 1 branch
BranchTrueFalseTotal
$args{'rsync_opt_maintain_ownership'} ? : 0 0 0
923  
924 0 $source = Cwd::abs_path($source);
925  
926 0 local $CWD = $source;
927 0 my @entries = _list(\%args);
928 0 @entries = _sort_entries_by_recent(@entries);# if $args{min_repo_access_time};
929 #log_trace("entries: %s", \@entries);
930  
931 0 ▶ $CWD = $target unless $action eq 'list-source-repos';
Branch: 1 branch
BranchTrueFalseTotal
unless $action eq "list-source-repos" 0 0 0
932  
933 0 my %res;
934 0 my $i = 0;
935 0 ▶ $progress->pos(0) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
936 0 ▶ $progress->target(scalar @entries) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
937  
938 0 my @res;
939  
940 ENTRY:
941 0 for my $e (@entries) {
942 0 ++$i;
943 0 ▶ next ENTRY if _skip_process_entry($e, \%args, "$source/$e->{name}");
Branch: 1 branch
BranchTrueFalseTotal
if _skip_process_entry($e, \%args, qq[$source/$e->{"name"}]) 0 0 0
944 0 my $is_repo = _is_repo("$source/$e->{name}");
945  
946 0 ▶ if ($action eq 'list-source-repos') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "list-source-repos") 0 0 0
947 0 ▶ push @res, $e->{name} if $is_repo;
Branch: 1 branch
BranchTrueFalseTotal
if $is_repo 0 0 0
948 0 next ENTRY;
949 }
950  
951 0 ▶ if (!$is_repo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_repo) 0 0 0
952 0 my $file_or_dir = $e->{name};
953 0 ▶ $progress->update(pos => $i,
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
954 message =>
955 "Sync-ing non-git file/directory $file_or_dir ...")
956 if $progress;
957  
958 0 my $prog;
959 my @extra_opts;
960 0 ▶ if ($args{skip_mtime_check} || $args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'skip_mtime_check'} or $args{'-dry_run'}) { } 0 0 0
Condition0%0/3
$args{"skip_mtime_check"} or $args{"-dry_run"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"skip_mtime_check"} or $args{"-dry_run"}
$args{"skip_mtime_check"} $args{"-dry_run"}
Truth table0%0/3
A or B
A= $args{"skip_mtime_check"}B= $args{"-dry_run"}
ABresult
1X1
011
000
961 0 $prog = "rsync";
962 } else {
963 0 $prog = "rsync-new2old";
964 0 push @extra_opts, "--create-target-if-not-exists";
965 }
966  
967 # just some random unique string so we can detect whether any
968 # file/dir is modified/added to target. to check files deleted in
969 # target, we use /^deleting /x
970 0 my $uuid = UUID::Random::generate();
971 0 ▶ my $_v = log_is_debug() ? "-v" : "";
Branch: 1 branch
BranchTrueFalseTotal
log_is_debug() ? : 0 0 0
972 0 ▶ my $del = $args{rsync_del} ? "--del" : "";
Branch: 1 branch
BranchTrueFalseTotal
$args{'rsync_del'} ? : 0 0 0
973 0 ▶ push @extra_opts, "--log-format=$uuid" unless $args{-dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-dry_run"} 0 0 0
974 $cmd = join(
975 "",
976 $prog,
977 0 ▶ $args{-dry_run} ? " --dry-run" : "",
Branch: 2 branches
BranchTrueFalseTotal
$args{'-dry_run'} ? : 0 0 0
@extra_opts ? : 0 0 0
978 @extra_opts ? " " . join(" ", @extra_opts) : "",
979 " -${_a}z $_v $del",
980 " --force",
981 " " . shell_quote("$source/$file_or_dir"),
982 " .",
983 );
984 my ($stdout, @result) = log_is_debug() ?
985 0 ▶ Capture::Tiny::tee_stdout (sub { system($cmd) }) :
Subroutine __ANON__: not called CC=1 SCAR=6.9
986 0 ▶ Capture::Tiny::capture_stdout(sub { system($cmd) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
log_is_debug() ? : 0 0 0
987 0 ▶ if ($args{-dry_run}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'-dry_run'}) { } 0 0 0
elsif ($result[0]) { } 0 0 0
988 0 $res{$file_or_dir} = [304, "dry-run"];
989 0 next ENTRY;
990 } elsif ($result[0]) {
991 0 log_warn("Rsync failed, please check: $result[0]");
992 0 $res{$file_or_dir} = [500, "rsync failed: $result[0]"];
993 } else {
994 0 ▶ if ($stdout =~ /^(deleting |\Q$uuid\E)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout =~ /^(deleting |\Q$uuid\E)/m) 0 0 0
995 0 log_warn("Non-git file/dir '$file_or_dir' updated");
996 }
997 0 $res{$file_or_dir} = [200, "rsync-ed"];
998 }
999 0 next ENTRY;
1000 }
1001  
1002 0 my $repo = $e->{name};
1003 0 my $created;
1004 0 ▶ if (!(-e $repo)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $repo) 0 0 0
1005 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
1006 0 log_warn("[DRY RUN] Copying repo '%s'", $repo);
1007 0 next ENTRY;
1008 }
1009 0 ▶ if ($create_bare) {
Branch: 2 branches
BranchTrueFalseTotal
if ($create_bare) { } 0 0 0
elsif (defined $create_bare) { } 0 0 0
1010 0 log_info("Initializing target repo $repo (bare) ...");
1011 0 $cmd = "mkdir ".shell_quote($repo)." && cd ".shell_quote($repo).
1012 " && git init --bare";
1013 0 system($cmd);
1014 0 $exit = $? >> 8;
1015 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
1016 0 log_warn("Git init failed, please check: $exit");
1017 0 $res{$repo} = [500, "git init --bare failed: $exit"];
1018 0 next ENTRY;
1019 }
1020 0 $created++;
1021 # continue to sync-ing
1022 } elsif (defined $create_bare) {
1023 0 log_info("Initializing target repo $repo (non-bare) ...");
1024 0 $cmd = "mkdir ".shell_quote($repo)." && cd ".shell_quote($repo).
1025 " && git init";
1026 0 system($cmd);
1027 0 $exit = $? >> 8;
1028 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 0 0
1029 0 log_warn("Git init failed, please check: $exit");
1030 0 $res{$repo} = [500, "git init failed: $exit"];
1031 0 next ENTRY;
1032 }
1033 0 $created++;
1034 # continue to sync-ing
1035 } else {
1036 0 ▶ $progress->update(pos => $i,
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
1037 message =>
1038 "Copying repo $repo ...")
1039 if $progress;
1040 0 $cmd = "rsync -${_a}z ".shell_quote("$source/$repo")." .";
1041 0 system($cmd);
1042 0 $exit = $? >> 8;
1043 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) { } 0 0 0
1044 0 log_warn("Rsync failed, please check: $exit");
1045 0 $res{$repo} = [500, "rsync failed: $exit"];
1046 } else {
1047 0 $res{$repo} = [200, "rsync-ed"];
1048 }
1049 0 log_warn("Repo $repo copied");
1050 # touch pull time
1051 0 $dbh_target->do("INSERT OR IGNORE INTO repos (name) VALUES (?)",
1052 {}, $repo);
1053 0 $dbh_target->do("UPDATE repos SET pull_time=? WHERE name=?",
1054 {}, time(), $repo);
1055 0 next ENTRY;
1056 }
1057 }
1058  
1059 0 ▶ $progress->update(pos => $i, message => "Sync-ing repo $repo ...")
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
1060 if $progress;
1061  
1062 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
1063 0 log_warn("[DRY RUN] Sync-ing repo '%s'", $repo);
1064 0 next ENTRY;
1065 }
1066  
1067 0 ▶ if ($backup && !$created) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backup and not $created) 0 0 0
Condition0%0/3
$backup and not $created
!ll&&!rl&&r
000
MC/DC0%0/2
$backup and not $created
$backup not $created
Truth table0%0/3
A and not(B)
A= $backupB= not $created
ABresult
0X0
100
111
1068 0 log_debug("Discarding changes in target repo $repo ...");
1069 0 local $CWD = $repo;
1070 0 system "git clean -f -d && git checkout .";
1071 # ignore error for now, let's go ahead and sync anyway
1072 }
1073  
1074 0 my $res = _sync_repo(
1075 $source, $target, $repo,
1076 {delete_branch => $delete_branch},
1077 );
1078 # touch pull time
1079 0 ▶ if ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 200) 0 0 0
1080 0 $dbh_target->do("INSERT OR IGNORE INTO repos (name) VALUES (?)",
1081 {}, $repo);
1082 0 $dbh_target->do("UPDATE repos SET pull_time=? WHERE name=?",
1083 {}, time(), $repo);
1084 }
1085 0 $res{$repo} = $res;
1086 }
1087  
1088 0 ▶ $progress->finish if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
1089  
1090 0 ▶ if ($action eq 'list-source-repos') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "list-source-repos") 0 0 0
1091 0 return [200, "OK", \@res];
1092 }
1093  
1094 0 system "touch", "$target/.gitbunch-sync-timestamp";
1095  
1096 0 [200,
1097 "OK",
1098 \%res,
1099 {"cmdline.result" => ''}];
1100 }
1101  
1102 $SPEC{exec_bunch} = {
1103 2 v => 1.1,
1104 summary =>
1105 'Execute a command for each repo in the bunch',
1106 description => <<'MARKDOWN',
1107  
1108 For each git repository in the bunch, will chdir to it and execute specified
1109 command.
1110  
1111 MARKDOWN
1112 args => {
1113 %common_args,
1114 command => {
1115 summary => 'Command to execute',
1116 schema => ['str*'],
1117 req => 1,
1118 pos => 1,
1119 greedy => 1,
1120 },
1121 %command_opts_args,
1122 },
1123 features => {
1124 dry_run => 1,
1125 },
1126 };
1127 0 ▶sub exec_bunch {
Subroutine exec_bunch: not called CC=11 SCAR=48.8
Pod documented
1128 0 my %args = @_;
1129 0 my $res;
1130 my $exit;
1131  
1132 # XXX schema
1133 0 $res = _check_common_args(\%args);
1134 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
1135 0 my $source = $args{source};
1136 0 my $command = $args{command};
1137 0 ▶ defined($command) or return [400, "Please specify command"];
Branch: 1 branch
BranchTrueFalseTotal
defined $command or return [400, "Please specify command"] 0 0 0
1138 0 ▶ my $command_opts = $args{command_opts} // {};
Condition0%0/2
$args{"command_opts"} // {}
l!l
00
MC/DC0%0/1
$args{"command_opts"} // {}
$args{"command_opts"}
Truth table0%0/2
A
A= $args{"command_opts"}
Aresult
11
00
1139  
1140 0 local $CWD = $source;
1141 0 my %res;
1142 0 my $i = 0;
1143 0 my @entries = _list(\%args);
1144 0 ▶ @entries = _sort_entries_by_recent(@entries) if $args{min_repo_access_time};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"min_repo_access_time"} 0 0 0
1145 #log_trace("entries: %s", \@entries);
1146  
1147 0 my @filtered_entries;
1148 0 for my $e (@entries) {
1149 0 ▶ next if _skip_process_repo($e, \%args, ".");
Branch: 1 branch
BranchTrueFalseTotal
if _skip_process_repo($e, \%args, ".") 0 0 0
1150 0 push @filtered_entries, $e;
1151 }
1152  
1153 REPO:
1154 0 for my $e (@filtered_entries) {
1155 0 my $repo = $e->{name};
1156 0 ▶ $CWD = $i++ ? "../$repo" : $repo;
Branch: 1 branch
BranchTrueFalseTotal
$i++ ? : 0 0 0
1157 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
1158 0 log_info("[DRY-RUN] [%d/%d] Executing command (%s, %s) on $repo ...",
1159 $i,
1160 scalar(@filtered_entries),
1161 $command_opts, $command);
1162 0 next REPO;
1163 }
1164 0 log_info("[%d/%d] Executing command (%s, %s) on $repo ...",
1165 $i,
1166 scalar(@filtered_entries),
1167 $command_opts, $command);
1168 0 system($command_opts, $command);
1169 0 $exit = $? >> 8;
1170 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) { } 0 0 0
1171 0 log_warn("Command failed: $exit");
1172 0 $res{$repo} = [500, "Command failed: $exit"];
1173 } else {
1174 0 $res{$repo} = [200, "Command successful"];
1175 }
1176 0 next REPO;
1177 }
1178  
1179 0 [200,
1180 "OK",
1181 \%res,
1182 {"cmdline.result" => ''}];
1183 }
1184  
1185 $SPEC{commit_bunch} = {
1186 2 v => 1.1,
1187 summary => 'Commit all uncommitted repos in the bunch',
1188 description => <<'MARKDOWN',
1189  
1190 For each git repository in the bunch, will first check whether the repo is
1191 "uncommitted" state, which means either has the status of "Needs commit" or "Has
1192 untracked files". The default mode is dry-run/simulation. If the `--no-dry-run`
1193 flag is not specified, will just show the status of these repos for you. If the
1194 `--no-dry-run` (can be as short as `--no-d` or `-N`) flag is specified, will
1195 'git add'+'git commit' all these repos with the same commit message for each,
1196 specified in `--message` (or just "Committed using 'gitbunch commit'" as the
1197 default message).
1198  
1199 MARKDOWN
1200 args => {
1201 %common_args,
1202 %sort_args,
1203 message => {
1204 summary => 'Commit message',
1205 schema => 'str*',
1206 default => "Committed using 'gitbunch commit'",
1207 cmdline_aliases => {m=>{}},
1208 },
1209 %command_opts_args,
1210 },
1211 features => {
1212 dry_run => {default=>1},
1213 },
1214 deps => {
1215 all => [
1216 {prog => 'git'},
1217 {prog => 'hr'},
1218 {prog => 'pwd'},
1219 ],
1220 },
1221 };
1222 0 ▶sub commit_bunch {
Subroutine commit_bunch: not called CC=6 SCAR=37.4
Pod documented
1223 0 require String::ShellQuote;
1224  
1225 0 my %args = @_;
1226 0 my $message = delete $args{message};
1227 0 ▶ my $command_opts = delete($args{command_opts}) // {};
Condition0%0/2
delete $args{"command_opts"} // {}
l!l
00
MC/DC0%0/1
delete $args{"command_opts"} // {}
delete $args{"command_opts"}
Truth table0%0/2
A
A= delete $args{"command_opts"}
Aresult
11
00
1228  
1229 0 my $check_res;
1230 {
1231 0 log_info("Checking status of repos");
1232 0 $check_res = check_bunch(%args, _loglevel=>'trace', -dry_run=>0);
1233 0 ▶ die "Can't check bunch: $check_res->[0] - $check_res->[1]"
Branch: 1 branch
BranchTrueFalseTotal
unless $check_res->[0] == 200 0 0 0
1234 unless $check_res->[0] == 200;
1235 }
1236  
1237 #my $exec_res;
1238 {
1239 0 my @repos;
1240 0 for my $repo_name (keys %{ $check_res->[2] }) {
1241 0 my $repo_res = $check_res->[2]{$repo_name};
1242 0 ▶ next unless $repo_res->[1] =~ /^(Needs commit|Has untracked files)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $repo_res->[1] =~ /^(Needs commit|Has untracked files)$/ 0 0 0
1243 0 push @repos, $repo_name;
1244 }
1245 0 log_info "Numbers of repo to commit: %d", scalar(@repos);
1246  
1247 my $cmd = $args{-dry_run} ?
1248 0 ▶ "pwd; git status; hr" :
Branch: 1 branch
BranchTrueFalseTotal
$args{'-dry_run'} ? : 0 0 0
1249 "pwd; git add .;git commit -am ".String::ShellQuote::shell_quote($message)."; hr";
1250 0 exec_bunch(%args, -dry_run=>0, include_repos=>\@repos, command=>$cmd, command_opts=>$command_opts);
1251 }
1252  
1253 0 [200];
1254 }
1255  
1256 $SPEC{list_bunches} = {
1257 2 v => 1.1,
1258 summary =>
1259 'List bunches defined in configuration',
1260 description => <<'MARKDOWN',
1261  
1262 A gitbunch directory is defined in the configuration file as a section, e.g.:
1263  
1264 [bunch NAME]
1265 path = SOMEPATH
1266  
1267 [bunch NAME2]
1268 path = !path ~/SOMEPATH
1269  
1270 MARKDOWN
1271 args => {
1272 detail => {
1273 schema => 'bool*',
1274 cmdline_aliases => {l=>{}},
1275 },
1276 },
1277 deps => {
1278 },
1279 features => {
1280 },
1281 };
1282 0 ▶sub list_bunches {
Subroutine list_bunches: not called CC=7 SCAR=40.3
Pod documented
1283 0 my %args = @_;
1284 0 my $cmdline = $args{-cmdline};
1285 0 my $cmdline_r = $args{-cmdline_r};
1286  
1287 0 my $config = $cmdline_r->{config};
1288 my @section_names0 = sort {
1289 0 ▶ my $order_a = $cmdline_r->{_config_section_read_order}{$a} // [9999, 9999];
Condition0%0/2
$cmdline_r->{"_config_section_read_order"}{$a} // [9999, 9999]
l!l
00
MC/DC0%0/1
$cmdline_r->{"_config_section_read_order"}{$a} // [9999, 9999]
$cmdline_r->{"_config_section_read_order"}{$a}
Truth table0%0/2
A
A= $cmdline_r->{"_config_section_read_order"}{$a}
Aresult
11
00
1290 0 ▶ my $order_b = $cmdline_r->{_config_section_read_order}{$b} // [9999, 9999];
Condition0%0/2
$cmdline_r->{"_config_section_read_order"}{$b} // [9999, 9999]
l!l
00
MC/DC0%0/1
$cmdline_r->{"_config_section_read_order"}{$b} // [9999, 9999]
$cmdline_r->{"_config_section_read_order"}{$b}
Truth table0%0/2
A
A= $cmdline_r->{"_config_section_read_order"}{$b}
Aresult
11
00
1291 0 ▶ $order_a->[0] <=> $order_b->[0] ||
Condition0%0/5
$order_a->[0] <=> $order_b->[0] || $order_a->[1] <=> $order_b->[1] || 0
l!l
00
$order_a->[0] <=> $order_b->[0] or $order_a->[1] <=> $order_b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$order_a->[0] <=> $order_b->[0] || $order_a->[1] <=> $order_b->[1] || 0
$order_a->[0] <=> $order_b->[0] $order_a->[1] <=> $order_b->[1]
Truth table0%0/3
A or B
A= $order_a->[0] <=> $order_b->[0]B= $order_a->[1] <=> $order_b->[1]
ABresult
1X1
011
000
1292 $order_a->[1] <=> $order_b->[1] || 0;
1293 0 } grep { /^bunch\s/ } keys %$config;
1294  
1295 0 my @bunches;
1296 0 for my $section_name0 (@section_names0) {
1297 0 my $section_config = $config->{$section_name0};
1298 0 (my $section_name = $section_name0) =~ s/(\s+\S+=\S*)+$//;
1299 0 (my $bunch_name = $section_name) =~ s/^bunch\s+//;
1300 0 $bunch_name =~ s/^"(.+)"$/$1/;
1301 push @bunches, {
1302 name => $bunch_name,
1303 path => $section_config->{path},
1304 0 };
1305 } # for section_name0
1306  
1307 0 ▶ unless ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"detail"}) 0 0 0
1308 0 @bunches = map { $_->{name} } @bunches;
1309 }
1310  
1311 0 [200, "OK", \@bunches];
1312 }
1313  
1314 2 1;
1315 # ABSTRACT: Manage gitbunch directory (directory which contain git repos)
1316  
1317 __END__
↑ Summary