Skilletor/Gitignore.pm

filter: Statementstmt 89.2% 124 / 139 Branchbran 55.7% 29 / 52 Conditioncond 50.0% 18 / 36 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 93.3% 28 / 30 Podpod 100% 8 / 8 totaltotal 74.7% 213 / 285 CC 40 SCAR 20.4
CC
40
Cov
75%
_update_block_file
17.9
is_git_work_tree
12.0
_git
10.5
CRAP 7.7
« Skilletor/Catalog.pm ↑ Summary Skilletor/Source/Git.pm »
1 package Skilletor::Gitignore;
2 # ABSTRACT: Managed .gitignore and .worktreeinclude blocks, skill .gitignore files, git's view (spec ยง6.4)
3  
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Skilletor::FSUtil qw( encode_text path_join read_text write_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Skilletor::GitEnv qw( git_env );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use Skilletor::JSON qw( js_sort_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Skilletor::JSString qw( js_blank );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use Skilletor::Process qw( run_command );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 31 our $VERSION = '0.001';
16  
17 31 our @EXPORT_OK = qw(
18 SKILL_GITIGNORE PROJECT_CLAUDE_ENTRIES LOCAL_ENTRIES CODEX_ENTRIES WORKTREE_INCLUDE
19 update_gitignore update_worktree_include block_entries has_block
20 skill_gitignore_path with_skill_gitignore git_tracked is_git_work_tree
21 );
22  
23 31 ▶use constant BEGIN_LINE => '# >>> skilletor >>>';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 31 ▶use constant END_LINE => '# <<< skilletor <<<';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 31 ▶use constant SKILL_GITIGNORE => qq{# installed by skilletor, not committed ("gitignore": false in skilletor.json to commit)\n*\n};
Subroutine BEGIN: called CC=1 SCAR=0.0
27 31 ▶use constant PROJECT_CLAUDE_ENTRIES => qw( skilletor.lock.json skilletor.local.json agents/**/.local.* rules/**/.local.* );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 31 ▶use constant LOCAL_ENTRIES => qw( agents/**/.local.* rules/**/.local.* );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 31 ▶use constant CODEX_ENTRIES => qw( agents/**/.local.* skilletor-rules.md );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 31 ▶use constant WORKTREE_INCLUDE => '.worktreeinclude';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 75 ▶sub update_gitignore {
Subroutine update_gitignore: called CC=1 SCAR=0.0
Pod documented
33 75 my ( %opts ) = @_;
34 75 return _update_block_file( path_join( $opts{dir}, '.gitignore' ), %opts );
35 }
36  
37 3 ▶sub update_worktree_include {
Subroutine update_worktree_include: called CC=1 SCAR=0.0
Pod documented
38 3 my ( %opts ) = @_;
39 3 return _update_block_file( path_join( $opts{dir}, WORKTREE_INCLUDE ), %opts );
40 }
41  
42 # JavaScript's "a\nb".split("\n") (a trailing newline gives a final "").
43 3 ▶sub _lines { my ( $text ) = @_; return split /\n/, $text, -1 }
Subroutine _lines: called CC=1 SCAR=0.0
44  
45 81 ▶sub _block_of {
Subroutine _block_of: called CC=9 SCAR=22.1
46 81 my ( $lines ) = @_;
47 81 my ( $begin, $end ) = ( -1, -1 );
48 81 for my $i ( 0 .. $#$lines ) {
49 36 ▶ $begin = $i if $begin == -1 && $lines->[$i] eq BEGIN_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if $begin == -1 and $lines->[$i] eq "# >>> skilletor >>>" 3 33 36
Condition66%2/3
$begin == -1 and $lines->[$i] eq "# >>> skilletor >>>"
!ll&&!rl&&r
3303
MC/DC50%1/2
$begin == -1 and $lines->[$i] eq "# >>> skilletor >>>"
$begin == -1 $lines->[$i] eq "# >>> skilletor >>>"
Truth table66%2/3
A and B
A= $begin == -1B= $lines->[$i] eq "# >>> skilletor >>>"
ABresult
0X0
100
111
50 36 ▶ $end = $i if $end == -1 && $lines->[$i] eq END_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if $end == -1 and $lines->[$i] eq "# <<< skilletor <<<" 3 33 36
Condition100%3/3
$end == -1 and $lines->[$i] eq "# <<< skilletor <<<"
!ll&&!rl&&r
3303
MC/DC100%2/2
$end == -1 and $lines->[$i] eq "# <<< skilletor <<<"
$end == -1 $lines->[$i] eq "# <<< skilletor <<<"
Truth table100%3/3
A and B
A= $end == -1B= $lines->[$i] eq "# <<< skilletor <<<"
ABresult
0X0
100
111
51 }
52 81 ▶ return ( $begin, $end, ( $begin != -1 && $end != -1 && $end > $begin ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$begin != -1 && $end != -1 && $end > $begin ? : 3 78 81
Condition66%4/6
$begin != -1 and $end != -1 and $end > $begin
!ll&&!rl&&r
7803
$begin != -1 and $end != -1
!ll&&!rl&&r
7803
MC/DC33%1/3
$begin != -1 and $end != -1 and $end > $begin
$begin != -1 $end != -1 $end > $begin
Truth table50%2/4
A and B and C
A= $begin != -1B= $end != -1C= $end > $begin
ABCresult
0XX0
10X0
1100
1111
53 }
54  
55 3 ▶sub block_entries {
Subroutine block_entries: called CC=3 SCAR=11.3
Pod documented
56 3 my ( $path ) = @_;
57 3 ▶ my @lines = eval { _lines( read_text($path) ) } or return ();
Branch: 1 branch
BranchTrueFalseTotal
my(@lines) = eval { do { _lines(read_text($path)) } } or return () 3 0 3
58 3 my ( $begin, $end, $has ) = _block_of( \@lines );
59 3 ▶ return $has ? @lines[ $begin + 1 .. $end - 1 ] : ();
Branch: 1 branch
BranchTrueFalseTotal
$has ? : 3 0 3
60 }
61  
62 0 ▶sub has_block {
Subroutine has_block: not called CC=2 SCAR=17.9
Pod documented
63 0 my ( $dir ) = @_;
64 0 ▶ my @lines = eval { _lines( read_text( path_join( $dir, '.gitignore' ) ) ) } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@lines) = eval { do { _lines(read_text(path_join($dir, ".gitignore"))) } } or return 0 0 0 0
65 0 return ( _block_of( \@lines ) )[2];
66 }
67  
68 78 ▶sub _trim_trailing_empty {
Subroutine _trim_trailing_empty: called CC=3 SCAR=12.5
69 78 my ( @lines ) = @_;
70 78 ▶ pop @lines while @lines && js_blank( $lines[-1] );
Branch: 1 branch
BranchTrueFalseTotal
@lines and js_blank($lines[-1]) and do { pop @lines } 0 78 78
Condition33%1/3
@lines and js_blank($lines[-1])
!ll&&!rl&&r
7800
MC/DC0%0/2
@lines and js_blank($lines[-1])
@lines js_blank($lines[-1])
Truth table33%1/3
A and B
A= @linesB= js_blank($lines[-1])
ABresult
0X0
100
111
71 78 return @lines;
72 }
73  
74 78 ▶sub _update_block_file {
Subroutine _update_block_file: called CC=15 SCAR=28.8
75 78 my ( $path, %opts ) = @_;
76 78 my $bytes = encode_text($path);
77 78 my $existed = -e $bytes;
78 78 ▶ my $existing = $existed ? read_text($path) : '';
Branch: 1 branch
BranchTrueFalseTotal
$existed ? : 0 78 78
79 78 ▶ my @lines = length $existing ? _lines($existing) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $existing ? : 0 78 78
80 78 my ( $begin, $end, $has ) = _block_of( \@lines );
81 78 ▶ my @outside = $has ? ( @lines[ 0 .. $begin - 1 ], @lines[ $end + 1 .. $#lines ] ) : @lines;
Branch: 1 branch
BranchTrueFalseTotal
$has ? : 0 78 78
82  
83 78 my %seen;
84 78 my @entries = js_sort_keys( grep { !$seen{$_}++ } @{ $opts{entries} } );
85 78 ▶ my $write = $opts{enabled} && @entries;
Condition66%2/3
$opts{'enabled'} && @entries
!ll&&!rl&&r
7206
MC/DC50%1/2
$opts{'enabled'} && @entries
$opts{'enabled'} @entries
Truth table66%2/3
A && B
A= $opts{'enabled'}B= @entries
ABresult
0X0
100
111
86 78 my ( @out, $change );
87 78 ▶ if ($write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write) { } 6 72 78
88 6 my @block = ( BEGIN_LINE, @entries, END_LINE );
89 6 ▶ if ($has) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has) { } 0 6 6
90 0 @out = ( @lines[ 0 .. $begin - 1 ], @block, @lines[ $end + 1 .. $#lines ] );
91 0 ▶ $change = join( "\n", @lines[ $begin + 1 .. $end - 1 ] ) eq join( "\n", @entries ) ? 'unchanged' : 'changed';
Branch: 1 branch
BranchTrueFalseTotal
join("\n", @lines[$begin + 1 .. $end - 1]) eq join("\n", @entries) ? : 0 0 0
92 }
93 else {
94 6 my @trimmed = _trim_trailing_empty(@outside);
95 6 ▶ @out = @trimmed ? ( @trimmed, '', @block ) : @block;
Branch: 1 branch
BranchTrueFalseTotal
@trimmed ? : 0 6 6
96 6 $change = 'created';
97 }
98 }
99 else {
100 72 @out = _trim_trailing_empty(@outside);
101 72 ▶ $change = $has ? 'removed' : 'unchanged';
Branch: 1 branch
BranchTrueFalseTotal
$has ? : 0 72 72
102 }
103  
104 78 ▶ my $result = ( @out && grep { !js_blank($_) } @out ) ? ( join( "\n", @out ) =~ s/\n*\z//r )."\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
@out && grep({not js_blank($_);} @out) ? : 6 72 78
Condition66%2/3
@out and grep {not js_blank($_);} @out
!ll&&!rl&&r
7206
MC/DC50%1/2
@out and grep {not js_blank($_);} @out
@out grep {not js_blank($_);} @out
Truth table66%2/3
A and B
A= @outB= grep {not js_blank($_);} @out
ABresult
0X0
100
111
105 78 ▶ if ( $result eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq "") 72 6 78
106 72 ▶ unlink $bytes if $existed;
Branch: 1 branch
BranchTrueFalseTotal
if $existed 0 72 72
107 72 return $change;
108 }
109 6 ▶ write_text( $path, $result ) if $result ne $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $result ne $existing 6 0 6
110 6 return $change;
111 }
112  
113 95 ▶sub skill_gitignore_path {
Subroutine skill_gitignore_path: called CC=1 SCAR=0.0
Pod documented
114 95 my ( $name ) = @_;
115 95 return path_join( 'skills', $name, '.gitignore' );
116 }
117  
118 # The output with skilletor's `.gitignore` at the skill's root, replacing one the source ships
119 # there: [ [ rel, bytes ], ... ], order kept.
120 28 ▶sub with_skill_gitignore {
Subroutine with_skill_gitignore: called CC=3 SCAR=11.7
Pod documented
121 28 my ( $output, $name ) = @_;
122 28 my $rel = skill_gitignore_path($name);
123 28 my $bytes = encode_text(SKILL_GITIGNORE);
124 28 my $found = 0;
125 0 ▶ my @out = map { $_->[0] eq $rel ? do { $found = 1; [ $rel, $bytes ] } : $_ } @$output;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq $rel ? : 0 28 28
126 28 ▶ push @out, [ $rel, $bytes ] unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 28 0 28
127 28 return \@out;
128 }
129  
130 31 ▶use constant LS_FILES_BATCH => 500;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 5 ▶sub _git {
Subroutine _git: called CC=6 SCAR=23.5
133 5 my ( @args ) = @_;
134 5 my $r = eval { run_command( cmd => [ 'git', @args ], env => git_env(), timeout_ms => 5_000 ) };
135 3 ▶ return ( $r && !$r->{spawn_error} && !$r->{timed_out} && defined $r->{code} && $r->{code} == 0 ) ? $r->{stdout} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$r && !$r->{'spawn_error'} && !$r->{'timed_out'} && defined $r->{'code'} && $r->{'code'} == 0 ? : 0 3 3
Condition33%4/12
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0
!ll&&!rl&&r
030
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"}
!ll&&!rl&&r
003
$r and not $r->{"spawn_error"} and not $r->{"timed_out"}
!ll&&!rl&&r
003
$r and not $r->{"spawn_error"}
!ll&&!rl&&r
003
MC/DC0%0/5
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0
$r not $r->{"spawn_error"} not $r->{"timed_out"} defined $r->{"code"} $r->{"code"} == 0
Truth table16%1/6
A and not(B) and not(C) and D and E
A= $rB= not $r->{"spawn_error"}C= not $r->{"timed_out"}D= defined $r->{"code"}E= $r->{"code"} == 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
136 }
137  
138 # Which of `paths` (relative to $dir) git tracks: one `git ls-files` per 500 paths. Outside a
139 # work tree, or when git is missing or fails, none.
140 5 ▶sub git_tracked {
Subroutine git_tracked: called CC=4 SCAR=16.0
Pod documented
141 5 my ( $dir, $paths ) = @_;
142 5 my @out;
143 5 ▶ for ( my $i = 0; $i < @$paths; $i += LS_FILES_BATCH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$paths) 5 0 5
144 5 my $last = $i + LS_FILES_BATCH - 1;
145 5 ▶ $last = $#$paths if $last > $#$paths;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $paths->$#* 5 0 5
146 5 my $listed = _git( '-C', $dir, '--literal-pathspecs', 'ls-files', '-z', '--', @{$paths}[ $i .. $last ] );
147 3 ▶ return [] unless defined $listed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $listed 3 0 3
148 0 push @out, map { Encode::decode( 'UTF-8', $_ ) } grep { length } split /\0/, $listed;
149 }
150 0 return \@out;
151 }
152  
153 0 ▶sub is_git_work_tree {
Subroutine is_git_work_tree: not called CC=3 SCAR=24.8
Pod documented
154 0 my ( $dir ) = @_;
155 0 my $out = _git( '-C', $dir, 'rev-parse', '--is-inside-work-tree' );
156 0 ▶ return ( defined $out && $out =~ /\A\s*true\s*\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $out && $out =~ /\A\s*true\s*\z/ ? : 0 0 0
Condition0%0/3
defined $out and $out =~ /\A\s*true\s*\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and $out =~ /\A\s*true\s*\z/
defined $out $out =~ /\A\s*true\s*\z/
Truth table0%0/3
A and B
A= defined $outB= $out =~ /\A\s*true\s*\z/
ABresult
0X0
100
111
157 }
158  
159 31 1;
160  
161 __END__
« Skilletor/Catalog.pm ↑ Summary Skilletor/Source/Git.pm »