Conformance/Snapshot/Directory.pm

filter: Statementstmt 35.0% 27 / 77 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.9% 9 / 22 Podpod 100% 11 / 11 totaltotal 29.1% 47 / 161 CC 21 SCAR 22.9
CC
21
Cov
23%
__ANON__
20.0
install_hook
20.0
new
12.0
CRAP 9.9
« Conformance/Check/DataSchema.pm ↑ Summary Conformance/Check/ClaudeMd.pm »
1 package Dist::Conformance::Snapshot::Directory 0.011;
2  
3 # ABSTRACT: A snapshot of a checkout on this machine, which an update can change
4  
5 3 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::Base 'Dist::Conformance::Snapshot', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use File::Find();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Slurper::Temp();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Mojo::File();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Dist::Conformance::Source::Git();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 3 has 'root';
19  
20 # What git hashes: the target of a symbolic link, or the content of a file.
21 0 ▶my sub bytes_at ($name) {
Subroutine bytes_at: not called CC=2 SCAR=17.9
22 0 ▶ return readlink $name if -l $name;
Branch: 1 branch
BranchTrueFalseTotal
if -l $name 0 0 0
23 0 return Mojo::File->new($name)->slurp;
24 }
25  
26  
27 0 ▶sub new ( $class, @args ) {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
28 0 my $self = $class->SUPER::new(@args);
29 0 ▶ die "root is required\n" if !defined $self->root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->root 0 0 0
30 0 ▶ die( 'root ' . $self->root . " is not a directory\n" ) if !-d $self->root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->root 0 0 0
31 0 $self->root( Mojo::File->new( $self->root ) );
32 0 return $self;
33 }
34  
35 # The L<Mojo::File> of C<$path> under the root.
36 # @param path Str the path, relative to the root
37 # @returns InstanceOf["Mojo::File"] the file, which may not exist
38 # @throws on a path that leaves the root, as L<Dist::Conformance::Schema/"relative_path($path, $inside)"> says
39 # @example my $file = $snapshot->file('CLAUDE.md');
40 0 ▶sub file ( $self, $path ) {
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
41 0 Dist::Conformance::Schema::relative_path( $path, 'the checkout' );
42 0 return $self->root->child( split m{/}, $path );
43 }
44  
45  
46 0 ▶sub paths ($self) {
Subroutine paths: not called CC=1 SCAR=6.9
Pod documented
47 0 my $root = $self->root;
48 0 my %paths;
49 File::Find::find(
50 {
51 no_chdir => 1,
52 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
53 0 my $name = $File::Find::name;
54 0 ▶ if ( $_ =~ m{(?:\A|/)[.]git\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ m[(?:\A|/)[.]git\z]aasx) 0 0 0
55 0 $File::Find::prune = 1;
56 0 return;
57 }
58 0 ▶ return if !-l $name && -d _;
Branch: 1 branch
BranchTrueFalseTotal
if not -l $name and -d _ 0 0 0
Condition0%0/3
not -l $name and -d _
!ll&&!rl&&r
000
MC/DC0%0/2
not -l $name and -d _
not -l $name -d _
Truth table0%0/3
not(A) and B
A= not -l $nameB= -d _
ABresult
0X0
100
111
59 0 my $relative = Mojo::File->new($name)->to_rel($root)->to_string;
60 0 $paths{$relative} = Dist::Conformance::Snapshot::blob_sha( bytes_at($name) );
61 },
62 },
63 0 "$root",
64 );
65 0 return \%paths;
66 }
67  
68  
69 0 ▶sub contains ( $self, $path ) {
Subroutine contains: not called CC=3 SCAR=24.8
Pod documented
70 0 my $file = $self->file($path);
71 0 ▶ return -l $file || ( -e _ && !-d _ );
Condition0%0/6
-l $file || -e _ && !-d(_)
l!l&&r!l&&!r
000
-e _ and not -d _
!ll&&!rl&&r
000
MC/DC0%0/3
-l $file || -e _ && !-d(_)
-l $file -e _ not -d _
Truth table0%0/4
A || B and not(C)
A= -l $fileB= -e _C= not -d _
ABCresult
1XX1
0111
00X0
0100
72 }
73  
74  
75 0 ▶sub sha ( $self, $path ) {
Subroutine sha: not called CC=2 SCAR=17.9
Pod documented
76 0 ▶ return undef if !$self->contains($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->contains($path) 0 0 0
77 0 return Dist::Conformance::Snapshot::blob_sha( bytes_at( $self->file($path) ) );
78 }
79  
80  
81 0 ▶sub content ( $self, $path ) {
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
82 0 my $file = $self->file($path);
83 0 ▶ return undef if !-e $file || -d _;
Branch: 1 branch
BranchTrueFalseTotal
if not -e $file or -d _ 0 0 0
Condition0%0/3
not -e $file or -d _
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e $file or -d _
not -e $file -d _
Truth table0%0/3
not(A) or B
A= not -e $fileB= -d _
ABresult
1X1
011
000
84 0 return $file->slurp;
85 }
86  
87  
88 0 ▶sub writable ($) { return 1 }
Subroutine writable: not called CC=1 SCAR=6.9
Pod documented
89  
90 # Writes C<$bytes> to C<$path> through a temporary file and a rename, so that no
91 # reader sees half a file. Makes the missing directories.
92 # @param path Str the path, relative to the root
93 # @param bytes Str the content
94 # @param mode Int optional: the permissions, default 0644
95 # @returns InstanceOf["Mojo::File"] the file that it wrote
96 # @throws on a path that leaves the root, and when it cannot write the file or set its mode
97 # @example $snapshot->write( '.perltidyrc', "-l=400\n" );
98 0 ▶sub write ( $self, $path, $bytes, $mode = oct 644 ) { ## no critic (Subroutines::ProhibitBuiltinHomonyms) -- the method of a writable snapshot, never called as the builtin
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
99 0 my $file = $self->file($path);
100 0 $file->dirname->make_path;
101 0 File::Slurper::Temp::write_binary( "$file", $bytes );
102 0 ▶ chmod $mode, "$file" or die("Cannot chmod $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, "$file" or die "Cannot chmod ${file}: $!\n" 0 0 0
103 0 return $file;
104 }
105  
106 # The directory that git runs the hooks of this checkout from. That is what
107 # C<git rev-parse --git-path hooks> answers in the root: F<.git/hooks>, or in a
108 # worktree the F<hooks> of the main checkout, unless C<core.hooksPath> names
109 # another directory. A relative C<core.hooksPath> is relative to the root, so a
110 # C<core.hooksPath> of F<git-hooks> makes each worktree run the hooks that its
111 # own branch tracks. It needs git 2.10 or later, the first whose C<--git-path>
112 # honors C<core.hooksPath>.
113 # @returns InstanceOf["Mojo::File"]|Undef the directory, or C<undef> when the root
114 # has no F<.git>
115 # @throws with the error of git when git finds no repository in a root that has
116 # a F<.git>
117 # @example my $hooks = $snapshot->hooks_dir;
118 0 ▶sub hooks_dir ($self) {
Subroutine hooks_dir: not called CC=3 SCAR=24.8
Pod documented
119 0 my $root = $self->root;
120 0 ▶ return undef if !-e $root->child('.git');
Branch: 1 branch
BranchTrueFalseTotal
unless -e $root->child(".git") 0 0 0
121  
122 # The root decides, not a repository that the environment names.
123 0 delete local @ENV{qw{GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR}};
124 0 chomp( my $path = Dist::Conformance::Source::Git::git( q{-C}, "$root", qw{rev-parse --git-path hooks} ) );
125 0 my $hooks = Mojo::File->new($path);
126  
127 # Mojo::File's to_abs resolves against the working directory, never a base.
128 0 ▶ return $hooks->is_abs ? $hooks : $root->child($path);
Branch: 1 branch
BranchTrueFalseTotal
$hooks->is_abs ? : 0 0 0
129 }
130  
131 # The bytes of the hook C<$name> that git runs now.
132 # @param name Str the name of the hook, such as C<pre-commit>
133 # @returns Str|Undef the bytes, or C<undef> when the hook or the checkout is not there
134 # @throws as L</hooks_dir> does
135 # @example my $bytes = $snapshot->installed_hook('pre-commit');
136 0 ▶sub installed_hook ( $self, $name ) {
Subroutine installed_hook: not called CC=3 SCAR=24.8
Pod documented
137 0 ▶ my $dir = $self->hooks_dir // return undef;
Condition0%0/2
$self->hooks_dir // (return undef)
l!l
00
MC/DC0%0/1
$self->hooks_dir // (return undef)
$self->hooks_dir
Truth table0%0/2
A
A= $self->hooks_dir
Aresult
11
00
138 0 my $hook = $dir->child($name);
139 0 ▶ return -e $hook ? $hook->slurp : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $hook ? : 0 0 0
140 }
141  
142 # Writes the hook C<$name> into L</hooks_dir>, executable.
143 # @param name Str the name of the hook, such as C<pre-commit>
144 # @param bytes Str the content of the hook
145 # @returns InstanceOf["Mojo::File"] the hook that it wrote
146 # @throws when the name is not that of a hook, when the root is not a checkout,
147 # and when it cannot write the hook or make it executable
148 # @example $snapshot->install_hook( 'pre-commit', $bytes );
149 0 ▶sub install_hook ( $self, $name, $bytes ) {
Subroutine install_hook: not called CC=4 SCAR=30.0
Pod documented
150 0 ▶ die "$name is not a hook name\n" if $name !~ m{\A[[:lower:]][[:lower:]-]*\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[[:lower:]][[:lower:]-]*\z/aasx 0 0 0
151 0 ▶ my $dir = $self->hooks_dir // die( $self->root . " is not a git checkout\n" );
Condition0%0/2
$self->hooks_dir // die($self->root . " is not a git checkout\n")
l!l
00
MC/DC0%0/1
$self->hooks_dir // die($self->root . " is not a git checkout\n")
$self->hooks_dir
Truth table0%0/2
A
A= $self->hooks_dir
Aresult
11
00
152 0 $dir->make_path;
153 0 my $hook = $dir->child($name);
154 0 File::Slurper::Temp::write_binary( "$hook", $bytes );
155 0 ▶ chmod oct 755, "$hook" or die("Cannot chmod $hook: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
chmod 0755, "$hook" or die "Cannot chmod ${hook}: $!\n" 0 0 0
156 0 return $hook;
157 }
158  
159 __END__
« Conformance/Check/DataSchema.pm ↑ Summary Conformance/Check/ClaudeMd.pm »