blib/lib/App/GitUtils.pm

filter: Statementstmt 16.4% 43 / 261 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 28.0% 7 / 25 Podpod 100% 12 / 12 totaltotal 13.3% 62 / 466 CC 85 SCAR 55.1
CC
85
Cov
10%
status
756.0
split_commit_add_untracked
272.0
list_committing_large_files
90.0
CRAP 247.9
↑ Summary
1 package App::GitUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Cwd qw(getcwd abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IPC::System::Options 'system', -log=>1, -die=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 1 our $DATE = '2025-09-24'; # DATE
14 1 our $DIST = 'App-GitUtils'; # DIST
15 1 our $VERSION = '0.091'; # VERSION
16  
17 1 our %SPEC;
18  
19 1 $SPEC{':package'} = {
20 v => 1.1,
21 summary => 'Some additional command-line utilities for git',
22 };
23  
24 1 our %argspecopt_dir = (
25 dir => {
26 summary => 'A directory inside git repo',
27 schema => 'dirname*',
28 description => <<'MARKDOWN',
29  
30 If not specified, will assume current directory is inside git repository and
31 will search `.git` upwards.
32  
33 MARKDOWN
34 },
35 );
36  
37 1 our %argspec_target_dir = (
38 target_dir => {
39 summary => 'Target repo directory',
40 schema => 'dirname*',
41 description => <<'MARKDOWN',
42  
43 If not specified, defaults to `$repodir.bare/`.
44  
45 MARKDOWN
46 },
47 );
48  
49 our %argspecopt_include_untracked = (
50 include_untracked => {
51 schema => 'bool*',
52 default => 1,
53 cmdline_aliases => {
54 0 ▶ u => {code=>sub { $_[0]{include_untracked} = 1 }, is_flag=>1, summary=>'Include untracked files (the default)'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 ▶ U => {code=>sub { $_[0]{include_untracked} = 0 }, is_flag=>1, summary=>'Do not include untracked files'},
Subroutine __ANON__: not called CC=1 SCAR=1.2
56 },
57 },
58 );
59  
60 0 ▶our $_complete_hook = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
61 0 my %args = @_;
62  
63 0 ▶ my $word = $args{word} // '';
Condition0%0/2
$args{"word"} // ""
l!l
00
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table0%0/2
A
A= $args{"word"}
Aresult
11
00
64 0 my $res = list_hooks();
65 0 ▶ return [] unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
66 0 return $res->[2];
67 1 };
68  
69 0 ▶sub _search_git_dir {
Subroutine _search_git_dir: not called CC=6 SCAR=37.4
70 0 my $args = shift;
71  
72 0 my $orig_wd = getcwd;
73  
74 0 my $cwd;
75 0 ▶ if (defined $args->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'dir'}) { } 0 0 0
76 0 $cwd = $args->{dir};
77 } else {
78 0 $cwd = $orig_wd;
79 }
80  
81 0 my $res;
82 0 while (1) {
83 0 ▶ log_trace "Checking for .git/ in $cwd ..." if $ENV{GITUTILS_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GITUTILS_TRACE"} 0 0 0
84 0 ▶ do { $res = "$cwd/.git"; last } if -d "$cwd/.git";
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$cwd/.git") 0 0 0
85 0 ▶ chdir ".." or goto EXIT;
Branch: 1 branch
BranchTrueFalseTotal
chdir ".." or goto EXIT 0 0 0
86 0 ▶ $cwd =~ s!(.+)/.+!$1! or last;
Branch: 1 branch
BranchTrueFalseTotal
$cwd =~ s[(.+)/.+][$1] or last 0 0 0
87 }
88  
89 EXIT:
90 0 chdir $orig_wd;
91 0 return $res;
92 }
93  
94 $SPEC{get_git_dir} = {
95 1 v => 1.1,
96 summary => 'Get the path to the .git directory',
97 description => <<'MARKDOWN',
98  
99 Basically just the C<git_dir> information from `gu info`. Useful in shell
100 scripts.
101  
102 MARKDOWN
103 args => {
104 %argspecopt_dir,
105 },
106 deps => {
107 prog => {name=>'git', min_version=>'2.22.0'}, # for --show-current option
108 },
109 tags => [
110 'find-dotgit-dir', # we accept 'dir' arg for these
111 ],
112 };
113 0 ▶sub get_git_dir {
Subroutine get_git_dir: not called CC=3 SCAR=24.8
Pod documented
114 0 my %args = @_;
115  
116 0 my $git_dir = _search_git_dir(\%args);
117 0 ▶ return [412, "Can't find .git dir, make sure ".($args{dir} // "the current directory")." is a git repo"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_dir 0 0 0
Condition0%0/2
$args{"dir"} // "the current directory"
l!l
00
MC/DC0%0/1
$args{"dir"} // "the current directory"
$args{"dir"}
Truth table0%0/2
A
A= $args{"dir"}
Aresult
11
00
118 unless defined $git_dir;
119  
120 0 [200, "OK", $git_dir];
121 }
122  
123 $SPEC{info} = {
124 1 v => 1.1,
125 summary => 'Return information about git repository',
126 description => <<'MARKDOWN',
127  
128 Information include:
129  
130 - Path of the git directory
131 - Repository name
132 - Current/active branch
133  
134 Will return status 412 if working directory is not inside a git repository. Will
135 return status 500 on errors, e.g. if `git` command cannot recognize the
136 repository.
137  
138 MARKDOWN
139 args => {
140 %argspecopt_dir,
141 },
142 deps => {
143 prog => {name=>'git', min_version=>'2.22.0'}, # for --show-current option
144 },
145 tags => [
146 'find-dotgit-dir', # we accept 'dir' arg for these
147 ],
148 };
149 0 ▶sub info {
Subroutine info: not called CC=5 SCAR=34.0
Pod documented
150 0 my %args = @_;
151  
152 0 my $git_dir = _search_git_dir(\%args);
153 0 ▶ return [412, "Can't find .git dir, make sure ".($args{dir} // "the current directory")." is a git repo"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_dir 0 0 0
Condition0%0/2
$args{"dir"} // "the current directory"
l!l
00
MC/DC0%0/1
$args{"dir"} // "the current directory"
$args{"dir"}
Truth table0%0/2
A
A= $args{"dir"}
Aresult
11
00
154 unless defined $git_dir;
155  
156 0 ▶ my ($repo_name) = $git_dir =~ m!(?:.+/)?([^/]+)/\.git\z!
Branch: 1 branch
BranchTrueFalseTotal
my($repo_name) = $git_dir =~ m[(?:.+/)?([^/]+)/\.git\z] or return [500, "Can't extract repo name from git dir '${git_dir}'"] 0 0 0
157 or return [500, "Can't extract repo name from git dir '$git_dir'"];
158  
159 0 local $CWD = $git_dir;
160 0 my $current_branch = `git branch --show-current`;
161 0 ▶ return [500, "Can't execute git to find out current branch: $!"] if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
162 0 chomp $current_branch;
163  
164 0 [200, "OK", {
165 git_dir => $git_dir,
166 repo_name => $repo_name,
167 current_branch => $current_branch,
168 # more information in the future
169 }];
170 }
171  
172 $SPEC{list_hooks} = {
173 1 v => 1.1,
174 summary => 'List available hooks for the repository',
175 args => {
176 %argspecopt_dir,
177 },
178 deps => {
179 prog => 'git',
180 },
181 tags => [
182 'find-dotgit-dir', # we accept 'dir' arg for these
183 ],
184 };
185 0 ▶sub list_hooks {
Subroutine list_hooks: not called CC=6 SCAR=37.4
Pod documented
186 0 my %args = @_;
187  
188 0 my $git_dir = _search_git_dir(\%args);
189 0 ▶ return [412, "Can't find .git dir, make sure you're inside a git repo"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_dir 0 0 0
190 unless defined $git_dir;
191  
192 0 my $hooks_dir = "$git_dir/hooks";
193 0 opendir my($dh), $hooks_dir;
194 0 my @res;
195 0 for (sort readdir $dh) {
196 0 ▶ next if /\.sample\z/; # skip sample names
Branch: 1 branch
BranchTrueFalseTotal
if /\.sample\z/ 0 0 0
197 0 ▶ next unless -f "$hooks_dir/$_" && -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$hooks_dir/$_" and -x _ 0 0 0
Condition0%0/3
-f "$hooks_dir/$_" and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$hooks_dir/$_" and -x _
-f "$hooks_dir/$_" -x _
Truth table0%0/3
A and B
A= -f "$hooks_dir/$_"B= -x _
ABresult
0X0
100
111
198 0 push @res, $_;
199 }
200 0 [200, "OK", \@res];
201 }
202  
203 $SPEC{run_hook} = {
204 1 v => 1.1,
205 summary => 'Run a hook',
206 description => <<'MARKDOWN',
207  
208 Basically the same as:
209  
210 % .git/hooks/<hook-name>
211  
212 except can be done anywhere inside git repo and provides tab completion.
213  
214 MARKDOWN
215 args => {
216 %argspecopt_dir,
217 name => {
218 summary => 'Hook name, e.g. post-commit',
219 schema => ['str*', match => '\A[A-Za-z0-9-]+\z'],
220 req => 1,
221 pos => 0,
222 completion => $_complete_hook,
223 },
224 },
225 deps => {
226 prog => 'git',
227 },
228 tags => [
229 'find-dotgit-dir', # we accept 'dir' arg for these
230 ],
231 };
232 0 ▶sub run_hook {
Subroutine run_hook: not called CC=3 SCAR=24.8
Pod documented
233 0 my %args = @_;
234  
235 0 my $git_dir = _search_git_dir(\%args);
236 0 ▶ return [412, "Can't find .git dir, make sure you're inside a git repo"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_dir 0 0 0
237 unless defined $git_dir;
238  
239 0 my $name = $args{name};
240  
241 0 ▶ (-x "$git_dir/hooks/$name") or
Branch: 1 branch
BranchTrueFalseTotal
-x "$git_dir/hooks/$name" or return [400, "Unknown or non-executable git hook: $name"] 0 0 0
242 return [400, "Unknown or non-executable git hook: $name"];
243  
244 0 local $CWD = "$git_dir/..";
245 0 exec ".git/hooks/$name";
246 #[200]; # unreached
247 }
248  
249 $SPEC{post_commit} = {
250 1 v => 1.1,
251 summary => 'Run post-commit hook',
252 description => <<'MARKDOWN',
253  
254 Basically the same as:
255  
256 % .git/hooks/post-commit
257  
258 except can be done anywhere inside git repo.
259  
260 MARKDOWN
261 args => {
262 %argspecopt_dir,
263 },
264 tags => [
265 'find-dotgit-dir', # we accept 'dir' arg for these
266 ],
267 };
268 0 ▶sub post_commit {
Subroutine post_commit: not called CC=1 SCAR=6.9
Pod documented
269 0 run_hook(name => 'post-commit');
270 }
271  
272 $SPEC{pre_commit} = {
273 1 v => 1.1,
274 summary => 'Run pre-commit hook',
275 description => <<'MARKDOWN',
276  
277 Basically the same as:
278  
279 % .git/hooks/pre-commit
280  
281 except can be done anywhere inside git repo.
282  
283 MARKDOWN
284 args => {
285 %argspecopt_dir,
286 },
287 tags => [
288 'find-dotgit-dir', # we accept 'dir' arg for these
289 ],
290 };
291 0 ▶sub pre_commit {
Subroutine pre_commit: not called CC=1 SCAR=6.9
Pod documented
292 0 run_hook(name => 'pre-commit');
293 }
294  
295 $SPEC{clone_to_bare} = {
296 1 v => 1.1,
297 summary => 'Clone repository to a bare repository',
298 args => {
299 %argspecopt_dir,
300 %argspec_target_dir,
301 },
302 tags => [
303 'find-dotgit-dir', # we accept 'dir' arg for these
304 ],
305 };
306 0 ▶sub clone_to_bare {
Subroutine clone_to_bare: not called CC=6 SCAR=37.4
Pod documented
307 0 require IPC::System::Options;
308  
309 0 my %args = @_;
310  
311 0 my $res = info(%args);
312 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
313  
314 0 my $src_dir = "$res->[2]{git_dir}/..";
315 0 ▶ my $target_dir = abs_path($args{target_dir} // "$src_dir/../$res->[2]{repo_name}.bare");
Condition0%0/2
$args{"target_dir"} // qq[$src_dir/../$res->[2]{"repo_name"}.bare]
l!l
00
MC/DC0%0/1
$args{"target_dir"} // qq[$src_dir/../$res->[2]{"repo_name"}.bare]
$args{"target_dir"}
Truth table0%0/2
A
A= $args{"target_dir"}
Aresult
11
00
316 0 ▶ (-d $target_dir) and return [412, "Target dir '$target_dir' already exists"];
Branch: 1 branch
BranchTrueFalseTotal
-d $target_dir and return [412, "Target dir '${target_dir}' already exists"] 0 0 0
317 0 ▶ (-e $target_dir) and return [412, "Target '$target_dir' already exists but not a dir"];
Branch: 1 branch
BranchTrueFalseTotal
-e $target_dir and return [412, "Target '${target_dir}' already exists but not a dir"] 0 0 0
318  
319 0 ▶ mkdir $target_dir, 0755 or return [500, "Can't mkdir target dir '$target_dir': $!"];
Branch: 1 branch
BranchTrueFalseTotal
mkdir $target_dir, 493 or return [500, "Can't mkdir target dir '${target_dir}': $!"] 0 0 0
320 0 IPC::System::Options::system(
321 {log=>1, die=>1},
322 "git", "init", "--bare", $target_dir,
323 );
324  
325 0 local $CWD = $src_dir;
326 0 IPC::System::Options::system(
327 {log=>1, die=>1},
328 "git", "push", "--all", $target_dir,
329 );
330 0 [200];
331 }
332  
333 $SPEC{status} = {
334 1 v => 1.1,
335 summary => 'Run `git status` and return information as a data structure',
336 description => <<'MARKDOWN',
337  
338 Currently incomplete!
339  
340 MARKDOWN
341 args => {
342 untracked => {
343 summary => "Untracked files option, will be passed as `-u` option to `git status`",
344 schema => ['str*' => {in => ['no', 'normal', 'all']}],
345 default => 'normal',
346 cmdline_aliases => {u=>{}},
347 description => <<'MARKDOWN',
348  
349 This will be passed to `git status` in the `-u` option.
350  
351 MARKDOWN
352 },
353 },
354 };
355 0 ▶sub status {
Subroutine status: not called CC=27 SCAR=66.3
Pod documented
356 0 require IPC::System::Options;
357  
358 0 my %args = @_;
359 0 ▶ my $untracked = $args{untracked} // 'normal';
Condition0%0/2
$args{"untracked"} // "normal"
l!l
00
MC/DC0%0/1
$args{"untracked"} // "normal"
$args{"untracked"}
Truth table0%0/2
A
A= $args{"untracked"}
Aresult
11
00
360  
361 0 my $stdout;
362 0 IPC::System::Options::system(
363 {log=>1, die=>1, capture_stdout => \$stdout},
364 "git", "status", "-u$untracked",
365 );
366  
367 0 my $res = [200, "OK", {}];
368 0 ▶ $stdout =~ /^On branch (.+)/ or do {
Branch: 1 branch
BranchTrueFalseTotal
$stdout =~ /^On branch (.+)/ or do { log_warn("Can't extract branch name") } 0 0 0
369 0 log_warn "Can't extract branch name";
370 };
371 0 $res->[2]{branch} = $1;
372 0 my @stdout_lines = split /^/m, $stdout;
373  
374 LIST_STAGED:
375 {
376 0 my $in_staged;
377 0 my (@new_files, @modified, @deleted);
378 0 for my $line (@stdout_lines) {
379 0 ▶ if (!$in_staged) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_staged) { } 0 0 0
elsif ($in_staged == 1) { } 0 0 0
380 0 ▶ if ($line =~ /^Changes to be committed:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Changes to be committed:/) 0 0 0
381 0 $in_staged = 1;
382 0 next;
383 }
384 } elsif ($in_staged == 1) {
385 0 ▶ if ($line =~ /^\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\S/) { } 0 0 0
elsif (() = $line =~ /^\s+(new file:|modified:|deleted: )\s\s\s(.+)\R/) { } 0 0 0
386 0 $in_staged = 2;
387 0 next;
388 } elsif (my ($op, $path) = $line =~ /^\s+(new file:|modified:|deleted: )\s\s\s(.+)\R/) {
389 0 ▶ if ($op eq 'new file:') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'new file:') { } 0 0 0
elsif ($op eq 'modified:') { } 0 0 0
elsif ($op eq 'deleted: ') { } 0 0 0
390 0 push @new_files, $path;
391 } elsif ($op eq 'modified:') {
392 0 push @modified, $path;
393 } elsif ($op eq 'deleted: ') {
394 0 push @deleted, $path;
395 }
396 }
397 } else {
398 0 last;
399 }
400 }
401 $res->[2]{staged} = {
402 0 new_files => \@new_files,
403 modified => \@modified,
404 deleted => \@deleted,
405 };
406 } # LIST_STAGED
407  
408 LIST_UNSTAGED:
409 {
410 0 my $in_unstaged;
411 0 my (@new_files, @modified, @deleted);
412 0 for my $line (@stdout_lines) {
413 0 ▶ if (!$in_unstaged) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_unstaged) { } 0 0 0
elsif ($in_unstaged == 1) { } 0 0 0
414 0 ▶ if ($line =~ /^Changes not staged for commit:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Changes not staged for commit:/) 0 0 0
415 0 $in_unstaged = 1;
416 0 next;
417 }
418 } elsif ($in_unstaged == 1) {
419 0 ▶ if ($line =~ /^\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\S/) { } 0 0 0
elsif (() = $line =~ /^\s+(new file:|modified:|deleted: )\s\s\s(.+)\R/) { } 0 0 0
420 0 $in_unstaged = 2;
421 0 next;
422 } elsif (my ($op, $path) = $line =~ /^\s+(new file:|modified:|deleted: )\s\s\s(.+)\R/) {
423 0 ▶ if ($op eq 'new file:') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'new file:') { } 0 0 0
elsif ($op eq 'modified:') { } 0 0 0
elsif ($op eq 'deleted: ') { } 0 0 0
424 0 push @new_files, $path;
425 } elsif ($op eq 'modified:') {
426 0 push @modified, $path;
427 } elsif ($op eq 'deleted: ') {
428 0 push @deleted, $path;
429 }
430 }
431 } else {
432 0 last;
433 }
434 }
435 $res->[2]{unstaged} = {
436 0 new_files => \@new_files,
437 modified => \@modified,
438 deleted => \@deleted,
439 };
440 } # LIST_UNSTAGED
441  
442 LIST_UNTRACKED:
443 {
444 0 my $in_untracked;
445 my (@paths);
446 0 for my $line (@stdout_lines) {
447 0 ▶ if (!$in_untracked) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_untracked) { } 0 0 0
elsif ($in_untracked == 1) { } 0 0 0
448 0 ▶ if ($line =~ /^Untracked files:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Untracked files:/) 0 0 0
449 0 $in_untracked = 1;
450 0 next;
451 }
452 } elsif ($in_untracked == 1) {
453 0 ▶ if ($line =~ /^\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\S/) { } 0 0 0
elsif (my($path) = $line =~ /^\t(.+)\R/) { } 0 0 0
454 0 $in_untracked = 2;
455 0 next;
456 } elsif (my ($path) = $line =~ /^\t(.+)\R/) {
457 0 push @paths, $path;
458 }
459 } else {
460 0 last;
461 }
462 }
463 0 $res->[2]{untracked} = \@paths;
464 } # LIST_UNTRACKED
465  
466 0 $res;
467 }
468  
469 $SPEC{list_committing_large_files} = {
470 1 v => 1.1,
471 summary => 'Check that added/modified files in staged/unstaged do not exceed a certain size',
472 description => <<'MARKDOWN',
473  
474 Will return an enveloped result with payload true containing added/modified
475 files in staged/unstaged that are larger than a certain specified `max_size`.
476  
477 To be used in a pre-commit script, for example.
478  
479 Some applications: Github for example warns when a file is above 50MB and
480 rejects when a file is above 100MB in size.
481  
482 MARKDOWN
483 args => {
484 max_size => {
485 schema => 'datasize*',
486 default => 100*1024*1024 - 1*1024, # safe margin
487 pos => 0,
488 cmdline_aliases => {s=>{}},
489 },
490 %argspecopt_include_untracked,
491 detail => {
492 schema => 'bool*',
493 cmdline_aliases => {l=>{}},
494 },
495 },
496 };
497 0 ▶sub list_committing_large_files {
Subroutine list_committing_large_files: not called CC=9 SCAR=45.0
Pod documented
498 0 my %args = @_;
499 0 ▶ my $max_size = $args{max_size} or return [400, "Please specify max_size"];
Branch: 1 branch
BranchTrueFalseTotal
my $max_size = $args{"max_size"} or return [400, "Please specify max_size"] 0 0 0
500 0 ▶ my $include_untracked = $args{include_untracked} // 1;
Condition0%0/2
$args{"include_untracked"} // 1
l!l
00
MC/DC0%0/1
$args{"include_untracked"} // 1
$args{"include_untracked"}
Truth table0%0/2
A
A= $args{"include_untracked"}
Aresult
11
00
501 0 my $detail = $args{detail};
502  
503 0 my $res = status(untracked => 'all');
504 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
505  
506 0 my @files;
507 my %section_files = (
508 staged_new_files => $res->[2]{staged}{new_files},
509 staged_modified => $res->[2]{staged}{modified},
510 unstaged_new_files => $res->[2]{unstaged}{new_files},
511 unstaged_modified => $res->[2]{unstaged}{modified},
512 0 ▶ ( $include_untracked ? (untracked => $res->[2]{untracked}) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$include_untracked ? : 0 0 0
513 );
514 0 for my $section (sort keys %section_files) {
515 0 for my $file (@{ $section_files{$section} }) {
516 0 my $size = -s $file;
517 0 ▶ next unless $size > $max_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size > $max_size 0 0 0
518 0 ▶ push @files, $detail ? {section=>$section, file=>$file, size=>$size} : $file;
Branch: 1 branch
BranchTrueFalseTotal
$detail ? : 0 0 0
519 }
520 }
521 0 [200, "OK", \@files];
522 }
523  
524 0 ▶sub _calc_totsize_recurse {
Subroutine _calc_totsize_recurse: not called CC=1 SCAR=6.9
525 0 require File::Find;
526  
527 0 my $path = shift;
528 0 my $totsize = 0;
529  
530 File::Find::find(
531 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
532 0 ▶ unless (-d $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $_) 0 0 0
533 0 $totsize += -s $_;
534 }
535 },
536 0 $path,
537 );
538 0 $totsize;
539 }
540  
541 $SPEC{calc_untracked_total_size} = {
542 1 v => 1.1,
543 summary => 'Check the disk usage of untracked files',
544 description => <<'MARKDOWN',
545  
546 This routine basically just grabs the list of untracked files returned by
547 `status()` (`gu status`) then checks their disk usage and totals them.
548  
549 MARKDOWN
550 args => {
551 detail => {
552 schema => 'bool*',
553 cmdline_aliases => {l=>{}},
554 },
555 },
556 };
557 0 ▶sub calc_untracked_total_size {
Subroutine calc_untracked_total_size: not called CC=6 SCAR=37.4
Pod documented
558 0 my %args = @_;
559  
560 0 my $res = status(untracked => 'all');
561 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
562  
563 0 my $totsize = 0;
564 0 my @sizes;
565 0 for my $file (@{ $res->[2]{untracked} }) {
566 0 my $size;
567 0 ▶ if ($file =~ m!/\z!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[/\z]) { } 0 0 0
568 # with "git status -uall" should not happen
569 0 $size += _calc_totsize_recurse($file);
570 } else {
571 0 $size += -s $file;
572 }
573 0 $totsize += $size;
574 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"detail"}) 0 0 0
575 0 push @sizes, [$file, $size];
576 }
577 }
578  
579 0 ▶ [200, "OK", $args{detail} ? \@sizes : $totsize];
Branch: 1 branch
BranchTrueFalseTotal
$args{'detail'} ? : 0 0 0
580 }
581  
582 $SPEC{calc_committing_total_size} = {
583 1 v => 1.1,
584 summary => 'Calculate the total sizes of files to add/delete/modify',
585 description => <<'MARKDOWN',
586  
587 To be used in pre-commit script, for example.
588  
589 Some applications: Github limits commit total size to 2GB.
590  
591 MARKDOWN
592 args => {
593 %argspecopt_include_untracked
594 },
595 };
596 0 ▶sub calc_committing_total_size {
Subroutine calc_committing_total_size: not called CC=5 SCAR=34.0
Pod documented
597 0 my %args = @_;
598 0 ▶ my $include_untracked = $args{include_untracked} // 1;
Condition0%0/2
$args{"include_untracked"} // 1
l!l
00
MC/DC0%0/1
$args{"include_untracked"} // 1
$args{"include_untracked"}
Truth table0%0/2
A
A= $args{"include_untracked"}
Aresult
11
00
599  
600 0 my $res = status(untracked => 'all');
601 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
602  
603 # TODO: calculate deleted
604  
605 0 my $totsize = 0;
606 0 for my $file (
607 0 @{ $res->[2]{staged}{new_files} },
608 0 @{ $res->[2]{staged}{modified} },
609 0 @{ $res->[2]{unstaged}{new_files} },
610 0 @{ $res->[2]{unstaged}{modified} },
611 0 ▶ @{ $res->[2]{unstaged}{modified} },
Branch: 1 branch
BranchTrueFalseTotal
$include_untracked ? : 0 0 0
612 0 ( $include_untracked ? @{ $res->[2]{untracked} } : ()),
613 ) {
614 0 my $size = -s $file;
615 0 $totsize += $size;
616 }
617  
618 0 [200, "OK", $totsize];
619 }
620  
621 $SPEC{split_commit_add_untracked} = {
622 1 v => 1.1,
623 summary => 'Commit untracked files, possibly over several commits, keeping commit size under certain limit',
624 description => <<'MARKDOWN',
625  
626  
627 MARKDOWN
628 args => {
629 max_size => {
630 schema => 'datasize*',
631 default => 2*1024*1024*1024 - 1*1024*1024, # 1MB safe margin
632 pos => 0,
633 cmdline_aliases => {s=>{}},
634 },
635 },
636 features => {
637 dry_run => 1,
638 },
639 };
640 0 ▶sub split_commit_add_untracked {
Subroutine split_commit_add_untracked: not called CC=16 SCAR=56.1
Pod documented
641 0 my %args = @_;
642 0 ▶ my $max_size = $args{max_size} // 2*1024*1024*1024 - 1*1024*1024;
Condition0%0/2
$args{"max_size"} // 2146435072
l!l
00
MC/DC0%0/1
$args{"max_size"} // 2146435072
$args{"max_size"}
Truth table0%0/2
A
A= $args{"max_size"}
Aresult
11
00
643  
644 0 my $res_status = status();
645 0 ▶ return [500, "Can't status(): $res_status->[0] - $res_status->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res_status->[0] == 200 0 0 0
646 unless $res_status->[0] == 200;
647  
648 return [304, "Nothing to commit"]
649 0 ▶ unless @{ $res_status->[2]{untracked} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res_status->[2]{"untracked"};} 0 0 0
650 return [409, "Please make we are not committing anything yet"]
651 if (
652 0 @{ $res_status->[2]{staged}{deleted} } ||
653 0 @{ $res_status->[2]{staged}{modified} } ||
654 0 @{ $res_status->[2]{staged}{new_files} } ||
655 0 @{ $res_status->[2]{unstaged}{deleted} } ||
656 0 @{ $res_status->[2]{unstaged}{modified} } ||
657 0 ▶ @{ $res_status->[2]{unstaged}{new_files} }
Branch: 1 branch
BranchTrueFalseTotal
if @{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};} or @{$res_status->[2]{"unstaged"}{"deleted"};} or @{$res_status->[2]{"unstaged"}{"modified"};} or @{$res_status->[2]{"unstaged"}{"new_files"};} 0 0 0
Condition0%0/15
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};} or @{$res_status->[2]{"unstaged"}{"deleted"};} or @{$res_status->[2]{"unstaged"}{"modified"};} or @{$res_status->[2]{"unstaged"}{"new_files"};}
l!l&&r!l&&!r
000
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};} or @{$res_status->[2]{"unstaged"}{"deleted"};} or @{$res_status->[2]{"unstaged"}{"modified"};}
l!l&&r!l&&!r
000
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};} or @{$res_status->[2]{"unstaged"}{"deleted"};}
l!l&&r!l&&!r
000
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};}
l!l&&r!l&&!r
000
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};}
l!l&&r!l&&!r
000
MC/DC0%0/6
@{$res_status->[2]{"staged"}{"deleted"};} or @{$res_status->[2]{"staged"}{"modified"};} or @{$res_status->[2]{"staged"}{"new_files"};} or @{$res_status->[2]{"unstaged"}{"deleted"};} or @{$res_status->[2]{"unstaged"}{"modified"};} or @{$res_status->[2]{"unstaged"}{"new_files"};}
@{$res_status->[2]{"staged"}{"deleted"};} @{$res_status->[2]{"staged"}{"modified"};} @{$res_status->[2]{"staged"}{"new_files"};} @{$res_status->[2]{"unstaged"}{"deleted"};} @{$res_status->[2]{"unstaged"}{"modified"};} @{$res_status->[2]{"unstaged"}{"new_files"};}
Truth table0%0/7
A or B or C or D or E or F
A= @{$res_status->[2]{"staged"}{"deleted"};}B= @{$res_status->[2]{"staged"}{"modified"};}C= @{$res_status->[2]{"staged"}{"new_files"};}D= @{$res_status->[2]{"unstaged"}{"deleted"};}E= @{$res_status->[2]{"unstaged"}{"modified"};}F= @{$res_status->[2]{"unstaged"}{"new_files"};}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
658 );
659  
660 0 my @items;
661 0 for my $file (@{ $res_status->[2]{untracked} }) {
662 0 my $size;
663 0 ▶ if ($file =~ m!/\z!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[/\z]) { } 0 0 0
664 0 $size = _calc_totsize_recurse($file);
665 } else {
666 0 $size = -s $file;
667 }
668 0 ▶ if ($size > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $max_size) 0 0 0
669 0 return [412, "One file is larger than max_size ($max_size): $file ($size)"];
670 }
671 0 push @items, [$file, $size];
672 }
673  
674 0 require App::BinPackUtils;
675 0 my $res_pack = App::BinPackUtils::pack_bins(
676 bin_size => $max_size,
677 items => \@items,
678 );
679 0 ▶ return [500, "Can't pack_bins(): $res_pack->[0] - $res_pack->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res_pack->[0] == 200 0 0 0
680 unless $res_pack->[0] == 200;
681  
682 # TODO: split 'git add' if there are many files
683 0 my $i = 0;
684 0 my $num_bins = @{ $res_pack->[2] };
685 0 for my $bin (@{ $res_pack->[2] }) {
686 0 $i++;
687 0 my @files;
688 0 for my $item (@{ $bin->{items} }) {
689 0 push @files, $item->{label};
690 }
691 0 system("git", "add", @files);
692  
693 # TODO: let user customize commit message
694 0 system("git", "commit", "-m", "Commited by gu split-commit-add-untracked #$i/$num_bins");
695 }
696 0 [200, "OK"];
697 }
698  
699  
700 1 1;
701 # ABSTRACT: Some additional command-line utilities for git
702  
703 __END__
↑ Summary