lib/Skilletor/Gitignore.pm

filter: Statementstmt 93.7% 151 / 161 Branchbran 75.0% 45 / 60 Conditioncond 64.5% 31 / 48 MC/DCmcdc 48.1% 13 / 27 Subroutinesub 96.5% 28 / 29 Podpod 100% 8 / 8 totaltotal 82.8% 276 / 333 CC 49 SCAR 22.5
CC
49
Cov
84%
_update_block_file
24.2
_block_of
9.0
_git
6.4
CRAP 9.5
« /usr/local/lib/perl5/5.44.0/x86_64-linux/POSIX.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Module/Implementation.pm »
1 package Skilletor::Gitignore;
2 # ABSTRACT: Managed .gitignore and .worktreeinclude blocks, skill .gitignore files, git's view (spec ยง6.4)
3  
4 402 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 402 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 402 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 402 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 402 ▶use Skilletor::FSUtil qw( encode_text path_join read_text write_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 402 ▶use Skilletor::GitEnv qw( git_env );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 402 ▶use Skilletor::JSON qw( js_sort_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 402 ▶use Skilletor::Process qw( run_command );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 402 our $VERSION = '0.008000';
15  
16 402 our @EXPORT_OK = qw(
17 SKILL_GITIGNORE PROJECT_CLAUDE_ENTRIES LOCAL_ENTRIES CODEX_ENTRIES WORKTREE_INCLUDE
18 update_gitignore update_worktree_include block_entries has_block
19 skill_gitignore_path with_skill_gitignore git_tracked is_git_work_tree
20 );
21  
22 402 ▶use constant BEGIN_LINE => '# >>> skilletor >>>';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 402 ▶use constant END_LINE => '# <<< skilletor <<<';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 402 ▶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
26 402 ▶use constant PROJECT_CLAUDE_ENTRIES => qw( skilletor.lock.json skilletor.local.json agents/**/.local.* rules/**/.local.* );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 402 ▶use constant LOCAL_ENTRIES => qw( agents/**/.local.* rules/**/.local.* );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 402 ▶use constant CODEX_ENTRIES => qw( agents/**/.local.* skilletor-rules.md );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 402 ▶use constant WORKTREE_INCLUDE => '.worktreeinclude';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 14296 ▶sub update_gitignore {
Subroutine update_gitignore: called CC=1 SCAR=0.0
Pod documented
32 14296 my ( %opts ) = @_;
33 14296 return _update_block_file( path_join( $opts{dir}, '.gitignore' ), %opts );
34 }
35  
36 2348 ▶sub update_worktree_include {
Subroutine update_worktree_include: called CC=1 SCAR=0.0
Pod documented
37 2348 my ( %opts ) = @_;
38 2348 return _update_block_file( path_join( $opts{dir}, WORKTREE_INCLUDE ), %opts );
39 }
40  
41 # Lines without their CRLF/LF endings, with exact offsets for surgical edits.
42 19223 ▶sub _text_lines {
Subroutine _text_lines: called CC=5 SCAR=16.4
43 19223 my ( $text ) = @_;
44 19223 my @lines;
45 19223 my $start = 0;
46 19223 ▶ while ( $start < length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < length $text) 41348 19223 60571
47 41348 my $newline = index( $text, "\n", $start );
48 41348 ▶ if ( $newline == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newline == -1) 0 41348 41348
49 0 push @lines, { text => substr( $text, $start ), start => $start, end => length($text), eol => '' };
50 0 last;
51 }
52 41348 ▶ my $content_end = $newline > $start && substr( $text, $newline - 1, 1 ) eq "\r" ? $newline - 1 : $newline;
Branch: 1 branch
BranchTrueFalseTotal
$newline > $start && substr($text, $newline - 1, 1) eq "\r" ? : 73 41275 41348
Condition66%2/3
$newline > $start and substr($text, $newline - 1, 1) eq "\r"
!ll&&!rl&&r
04127573
MC/DC50%1/2
$newline > $start and substr($text, $newline - 1, 1) eq "\r"
$newline > $start substr($text, $newline - 1, 1) eq "\r"
Truth table66%2/3
A and B
A= $newline > $startB= substr($text, $newline - 1, 1) eq "\r"
ABresult
0X0
100
111
53 41348 push @lines, {
54 text => substr( $text, $start, $content_end - $start ),
55 start => $start,
56 end => $newline + 1,
57 eol => substr( $text, $content_end, $newline + 1 - $content_end ),
58 };
59 41348 $start = $newline + 1;
60 }
61 19223 return @lines;
62 }
63  
64 19223 ▶sub _block_of {
Subroutine _block_of: called CC=9 SCAR=22.0
65 19223 my ( $lines ) = @_;
66 19223 my ( $begin, $end ) = ( -1, -1 );
67 19223 for my $i ( 0 .. $#$lines ) {
68 41348 ▶ $begin = $i if $begin == -1 && $lines->[$i]{text} eq BEGIN_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if $begin == -1 and $lines->[$i]{"text"} eq "# >>> skilletor >>>" 4315 37033 41348
Condition100%3/3
$begin == -1 and $lines->[$i]{"text"} eq "# >>> skilletor >>>"
!ll&&!rl&&r
37002314315
MC/DC100%2/2
$begin == -1 and $lines->[$i]{"text"} eq "# >>> skilletor >>>"
$begin == -1 $lines->[$i]{"text"} eq "# >>> skilletor >>>"
Truth table100%3/3
A and B
A= $begin == -1B= $lines->[$i]{"text"} eq "# >>> skilletor >>>"
ABresult
0X0
100
111
69 41348 ▶ $end = $i if $end == -1 && $lines->[$i]{text} eq END_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if $end == -1 and $lines->[$i]{"text"} eq "# <<< skilletor <<<" 4315 37033 41348
Condition100%3/3
$end == -1 and $lines->[$i]{"text"} eq "# <<< skilletor <<<"
!ll&&!rl&&r
9370244315
MC/DC100%2/2
$end == -1 and $lines->[$i]{"text"} eq "# <<< skilletor <<<"
$end == -1 $lines->[$i]{"text"} eq "# <<< skilletor <<<"
Truth table100%3/3
A and B
A= $end == -1B= $lines->[$i]{"text"} eq "# <<< skilletor <<<"
ABresult
0X0
100
111
70 }
71 19223 ▶ return ( $begin, $end, ( $begin != -1 && $end != -1 && $end > $begin ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$begin != -1 && $end != -1 && $end > $begin ? : 4315 14908 19223
Condition66%4/6
$begin != -1 and $end != -1 and $end > $begin
!ll&&!rl&&r
1490804315
$begin != -1 and $end != -1
!ll&&!rl&&r
1490804315
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
72 }
73  
74 4548 ▶sub block_entries {
Subroutine block_entries: called CC=3 SCAR=11.0
Pod documented
75 4548 my ( $path ) = @_;
76 4548 ▶ my $lines = eval { [ _text_lines( read_text($path) ) ] } or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $lines = eval { do { [_text_lines(read_text($path))] } } or return () 2577 1971 4548
77 2577 my ( $begin, $end, $has ) = _block_of($lines);
78 2577 ▶ return $has ? map { $lines->[$_]{text} } $begin + 1 .. $end - 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$has ? : 2575 2 2577
79 }
80  
81 2762 ▶sub has_block {
Subroutine has_block: called CC=2 SCAR=6.9
Pod documented
82 2762 my ( $dir ) = @_;
83 2762 ▶ my $lines = eval { [ _text_lines( read_text( path_join( $dir, '.gitignore' ) ) ) ] } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $lines = eval { do { [_text_lines(read_text(path_join($dir, ".gitignore")))] } } or return 0 2 2760 2762
84 2 return ( _block_of($lines) )[2];
85 }
86  
87 0 ▶sub _ends_with {
Subroutine _ends_with: not called CC=2 SCAR=17.9
88 0 my ( $text, $suffix ) = @_;
89 0 ▶ return length($suffix) <= length($text) && substr( $text, -length($suffix) ) eq $suffix;
Condition0%0/3
length $suffix <= length $text && substr($text, -length($suffix)) eq $suffix
!ll&&!rl&&r
000
MC/DC0%0/2
length $suffix <= length $text && substr($text, -length($suffix)) eq $suffix
length $suffix <= length $text substr($text, -length($suffix)) eq $suffix
Truth table0%0/3
A && B
A= length $suffix <= length $textB= substr($text, -length($suffix)) eq $suffix
ABresult
0X0
100
111
90 }
91  
92 16644 ▶sub _update_block_file {
Subroutine _update_block_file: called CC=21 SCAR=31.9
93 16644 my ( $path, %opts ) = @_;
94 16644 my $bytes = encode_text($path);
95 16644 my $existed = -e $bytes;
96 16644 ▶ my $existing = $existed ? read_text($path) : '';
Branch: 1 branch
BranchTrueFalseTotal
$existed ? : 1739 14905 16644
97 16644 my @lines = _text_lines($existing);
98 16644 my ( $begin, $end, $has ) = _block_of( \@lines );
99 16644 my %seen;
100 16644 my @entries = js_sort_keys( grep { !$seen{$_}++ } @{ $opts{entries} } );
101 16644 ▶ my $write = $opts{enabled} && @entries;
Condition100%3/3
$opts{'enabled'} && @entries
!ll&&!rl&&r
1292643714
MC/DC100%2/2
$opts{'enabled'} && @entries
$opts{'enabled'} @entries
Truth table100%3/3
A && B
A= $opts{'enabled'}B= @entries
ABresult
0X0
100
111
102 16644 my ( $result, $change ) = ( $existing, undef );
103  
104 16644 ▶ if ($has) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has) { } 1739 14905 16644
elsif ($write) { } 2010 12895 14905
105 1739 my @current = map { $lines[$_]{text} } $begin + 1 .. $end - 1;
106 1739 my $same = @current == @entries;
107 1739 ▶ for my $i ( 0 .. $#entries ) { $same = 0 if !$same || $current[$i] ne $entries[$i] }
Branch: 1 branch
BranchTrueFalseTotal
if not $same or $current[$i] ne $entries[$i] 2 9450 9452
Condition66%2/3
not $same or $current[$i] ne $entries[$i]
l!l&&r!l&&!r
029450
MC/DC50%1/2
not $same or $current[$i] ne $entries[$i]
not $same $current[$i] ne $entries[$i]
Truth table66%2/3
not(A) or B
A= not $sameB= $current[$i] ne $entries[$i]
ABresult
1X1
011
000
108 1739 ▶ return 'unchanged' if $write && $same;
Branch: 1 branch
BranchTrueFalseTotal
if $write and $same 1702 37 1739
Condition100%3/3
$write and $same
!ll&&!rl&&r
3521702
MC/DC100%2/2
$write and $same
$write $same
Truth table100%3/3
A and B
A= $writeB= $same
ABresult
0X0
100
111
109 37 my ( $first, $last ) = ( $lines[$begin], $lines[$end] );
110 37 ▶ if ($write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write) { } 2 35 37
111 2 my ($with_eol) = grep { length $_->{eol} } @lines;
112 2 ▶ my $newline = $first->{eol} || $last->{eol} || ( $with_eol ? $with_eol->{eol} : "\n" );
Branch: 1 branch
BranchTrueFalseTotal
$with_eol ? : 0 0 0
Condition33%2/6
$first->{'eol'} || $last->{'eol'} || ($with_eol ? $with_eol->{'eol'} : "\n")
l!l&&r!l&&!r
200
$first->{"eol"} or $last->{"eol"}
l!l&&r!l&&!r
200
MC/DC0%0/3
$first->{'eol'} || $last->{'eol'} || ($with_eol ? $with_eol->{'eol'} : "\n")
$first->{"eol"} $last->{"eol"} ($with_eol ? $with_eol->{'eol'} : "\n")
Truth table25%1/4
A or B || C
A= $first->{"eol"}B= $last->{"eol"}C= ($with_eol ? $with_eol->{'eol'} : "\n")
ABCresult
1XX1
01X1
0011
0000
113 2 my $replacement = join( $newline, BEGIN_LINE, @entries, END_LINE ).$last->{eol};
114 2 $result = substr( $existing, 0, $first->{start} ).$replacement.substr( $existing, $last->{end} );
115 2 $change = 'changed';
116 }
117 else {
118 35 my $before = substr( $existing, 0, $first->{start} );
119 35 my $after = substr( $existing, $last->{end} );
120 35 $result = $before.$after;
121 35 $change = 'removed';
122 }
123 }
124 elsif ($write) {
125 0 my ($with_eol) = grep { length $_->{eol} } @lines;
126 2010 ▶ my $newline = $with_eol ? $with_eol->{eol} : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$with_eol ? : 0 2010 2010
127 2010 my $separator = '';
128 2010 ▶ if ( length $existing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $existing) 0 2010 2010
129 0 ▶ $separator = _ends_with( $existing, $newline.$newline ) ? '' : _ends_with( $existing, $newline ) ? $newline : $newline.$newline;
Branch: 2 branches
BranchTrueFalseTotal
_ends_with($existing, $newline . $newline) ? : 0 0 0
_ends_with($existing, $newline) ? : 0 0 0
130 }
131 2010 $result = $existing.$separator.join( $newline, BEGIN_LINE, @entries, END_LINE ).$newline;
132 2010 $change = 'created';
133 }
134 else {
135 12895 return 'unchanged';
136 }
137  
138 2047 ▶ if ( $result eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq "") 31 2016 2047
139 31 ▶ unlink $bytes if $existed;
Branch: 1 branch
BranchTrueFalseTotal
if $existed 31 0 31
140 31 return $change;
141 }
142 2016 ▶ write_text( $path, $result ) if $result ne $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $result ne $existing 2016 0 2016
143 2016 return $change;
144 }
145  
146 10614 ▶sub skill_gitignore_path {
Subroutine skill_gitignore_path: called CC=1 SCAR=0.0
Pod documented
147 10614 my ( $name ) = @_;
148 10614 return path_join( 'skills', $name, '.gitignore' );
149 }
150  
151 # The output with skilletor's `.gitignore` at the skill's root, replacing one the source ships
152 # there: [ [ rel, bytes ], ... ], order kept.
153 2654 ▶sub with_skill_gitignore {
Subroutine with_skill_gitignore: called CC=3 SCAR=11.7
Pod documented
154 2654 my ( $output, $name ) = @_;
155 2654 my $rel = skill_gitignore_path($name);
156 2654 my $bytes = encode_text(SKILL_GITIGNORE);
157 2654 my $found = 0;
158 0 ▶ my @out = map { $_->[0] eq $rel ? do { $found = 1; [ $rel, $bytes ] } : $_ } @$output;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq $rel ? : 0 2754 2754
159 2654 ▶ push @out, [ $rel, $bytes ] unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 2654 0 2654
160 2654 return \@out;
161 }
162  
163 402 ▶use constant LS_FILES_BATCH => 500;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 148 ▶sub _git {
Subroutine _git: called CC=6 SCAR=18.6
166 148 my ( @args ) = @_;
167 148 my $r = eval { run_command( cmd => [ 'git', @args ], env => git_env(), timeout_ms => 5_000 ) };
168 137 ▶ 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 ? : 42 95 137
Condition66%8/12
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"} and $r->{"code"} == 0
!ll&&!rl&&r
59042
$r and not $r->{"spawn_error"} and not $r->{"timed_out"} and defined $r->{"code"}
!ll&&!rl&&r
50132
$r and not $r->{"spawn_error"} and not $r->{"timed_out"}
!ll&&!rl&&r
05132
$r and not $r->{"spawn_error"}
!ll&&!rl&&r
00137
MC/DC40%2/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 table50%3/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
169 }
170  
171 # Which of `paths` (relative to $dir) git tracks: one `git ls-files` per 500 paths. Outside a
172 # work tree, or when git is missing or fails, none.
173 135 ▶sub git_tracked {
Subroutine git_tracked: called CC=4 SCAR=14.0
Pod documented
174 135 my ( $dir, $paths ) = @_;
175 135 my @out;
176 135 ▶ for ( my $i = 0; $i < @$paths; $i += LS_FILES_BATCH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$paths) 135 42 177
177 135 my $last = $i + LS_FILES_BATCH - 1;
178 135 ▶ $last = $#$paths if $last > $#$paths;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $paths->$#* 135 0 135
179 135 my $listed = _git( '-C', $dir, '--literal-pathspecs', 'ls-files', '-z', '--', @{$paths}[ $i .. $last ] );
180 127 ▶ return [] unless defined $listed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $listed 85 42 127
181 0 push @out, map { Encode::decode( 'UTF-8', $_ ) } grep { length } split /\0/, $listed;
182 }
183 42 return \@out;
184 }
185  
186 13 ▶sub is_git_work_tree {
Subroutine is_git_work_tree: called CC=3 SCAR=12.5
Pod documented
187 13 my ( $dir ) = @_;
188 13 my $out = _git( '-C', $dir, 'rev-parse', '--is-inside-work-tree' );
189 10 ▶ return ( defined $out && $out =~ /\A\s*true\s*\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $out && $out =~ /\A\s*true\s*\z/ ? : 0 10 10
Condition33%1/3
defined $out and $out =~ /\A\s*true\s*\z/
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $out and $out =~ /\A\s*true\s*\z/
defined $out $out =~ /\A\s*true\s*\z/
Truth table33%1/3
A and B
A= defined $outB= $out =~ /\A\s*true\s*\z/
ABresult
0X0
100
111
190 }
191  
192 402 1;
193  
194 __END__
« /usr/local/lib/perl5/5.44.0/x86_64-linux/POSIX.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Module/Implementation.pm »