InGit/MutableTree.pm

filter: Statementstmt 95.1% 99 / 104 Branchbran 75.0% 54 / 72 Conditioncond 63.6% 28 / 44 MC/DCmcdc 44.0% 11 / 25 Subroutinesub 100% 12 / 12 Podpod 83.3% 5 / 6 totaltotal 79.4% 209 / 263 CC 57 SCAR 24.8
CC
57
Cov
82%
commit
17.3
set_path
15.9
_mkfile
13.1
CRAP 12.0
« InGit/MirrorTree.pm ↑ Summary InGit/Server.pm »
1 package CPAN::InGit::MutableTree;
2 # ABSTRACT: Utility object that represents a Git Tree and pending changes
3 5 our $VERSION = '0.003'; # VERSION
4  
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Git::Raw::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 5 has parent => ( is => 'ro', required => 1 );
13 5 has tree => ( is => 'rw' );
14 5 has branch => ( is => 'rw' );
15 5 has _changes => ( is => 'rw' );
16 5 has has_changes => ( is => 'rw' );
17 5 has use_workdir => ( is => 'rw' );
18 42 ▶sub git_repo { shift->parent->git_repo }
Subroutine git_repo: called CC=1 SCAR=0.0
Pod documented
19  
20 11 ▶sub BUILD($self, $args, @) {
Subroutine BUILD: called CC=6 SCAR=20.3
Pod undocumented
21 # branch supplied by name? look it up
22 11 ▶ if (defined $self->{branch} && !ref $self->{branch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"branch"} and not ref $self->{"branch"}) 0 11 11
Condition66%2/3
defined $self->{"branch"} and not ref $self->{"branch"}
!ll&&!rl&&r
650
MC/DC0%0/2
defined $self->{"branch"} and not ref $self->{"branch"}
defined $self->{"branch"} not ref $self->{"branch"}
Truth table66%2/3
A and not(B)
A= defined $self->{"branch"}B= not ref $self->{"branch"}
ABresult
0X0
100
111
23 0 ▶ my $b= Git::Raw::Branch->lookup($self->git_repo, $self->{branch}, 1)
Branch: 1 branch
BranchTrueFalseTotal
my $b = "Git::Raw::Branch"->lookup($self->git_repo, $self->{"branch"}, 1) or croak(qq[No local branch named '$self->{"branch"}']) 0 0 0
24 or croak "No local branch named '$self->{branch}'";
25 0 $self->{branch}= $b;
26 }
27 # If branch supplied and tree was not, look up the tree
28 11 ▶ if ($self->{branch} && !$self->{tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"branch"} and not $self->{"tree"}) 1 10 11
Condition100%3/3
$self->{"branch"} and not $self->{"tree"}
!ll&&!rl&&r
641
MC/DC100%2/2
$self->{"branch"} and not $self->{"tree"}
$self->{"branch"} not $self->{"tree"}
Truth table100%3/3
A and not(B)
A= $self->{"branch"}B= not $self->{"tree"}
ABresult
0X0
100
111
29 1 $self->{tree}= $self->{branch}->peel('tree');
30 }
31 }
32  
33  
34 42 ▶sub get_path($self, $path) {
Subroutine get_path: called CC=11 SCAR=24.0
Pod documented
35 42 ▶ if ($self->has_changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_changes) 11 31 42
36 11 ▶ if (keys $self->_changes->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->_changes}) 10 1 11
37 10 my $node= $self->_changes;
38 10 my @path= split '/', $path;
39 10 my $basename= pop @path;
40 10 for (@path) {
41 33 ▶ $node= $node->{$_} if defined $node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node 31 2 33
42 }
43 10 ▶ return $node->{$basename} if ref $node eq 'HASH' && $node->{$basename};
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" and $node->{$basename} 6 4 10
Condition100%3/3
ref $node eq "HASH" and $node->{$basename}
!ll&&!rl&&r
226
MC/DC100%2/2
ref $node eq "HASH" and $node->{$basename}
ref $node eq "HASH" $node->{$basename}
Truth table100%3/3
A and B
A= ref $node eq "HASH"B= $node->{$basename}
ABresult
0X0
100
111
44 }
45 5 ▶ if ($self->use_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_workdir) 1 4 5
46 1 my $ent= $self->git_repo->index->find($path);
47 1 ▶ return [ $ent->blob, $ent->mode ]
Branch: 1 branch
BranchTrueFalseTotal
if $ent 1 0 1
48 if $ent;
49 }
50 }
51 35 ▶ if ($self->tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tree) 30 5 35
52 30 ▶ my $dirent= $self->tree->entry_bypath($path)
Branch: 1 branch
BranchTrueFalseTotal
my $dirent = $self->tree->entry_bypath($path) or return undef 24 6 30
53 or return undef;
54 24 return [ $dirent->object, $dirent->file_mode ];
55 }
56 5 return undef;
57 }
58  
59  
60 22 ▶sub set_path($self, $path, $data, %opts) {
Subroutine set_path: called CC=15 SCAR=27.7
Pod documented
61 # Two modes: we can be writing to the working directory and index, or be building a new tree
62 # (which may or may not be connected to a branch)
63 22 my $repo= $self->git_repo;
64 22 ▶ my $mode= $opts{mode} // 0100644;
Condition100%2/2
$opts{"mode"} // 33188
l!l
319
MC/DC100%1/1
$opts{"mode"} // 33188
$opts{"mode"}
Truth table100%2/2
A
A= $opts{"mode"}
Aresult
11
00
65 22 my @path= split m{/+}, $path;
66 22 my $basename= pop @path;
67 22 ▶ if ($self->use_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_workdir) { } 1 21 22
68 1 my $fullpath= $self->git_repo->workdir;
69 # create missing directories
70 1 for (@path) {
71 1 $fullpath .= '/'.$_;
72 1 ▶ mkdir $fullpath || die "mkdir($fullpath): $!"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fullpath 1 0 1
Condition50%1/2
$fullpath || die("mkdir($fullpath): $!")
l!l
10
MC/DC0%0/1
$fullpath || die("mkdir($fullpath): $!")
$fullpath
Truth table50%1/2
A
A= $fullpath
Aresult
11
00
73 unless -d $fullpath;
74 }
75 1 $fullpath .= '/'.$basename;
76 1 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) { } 0 1 1
77 0 unlink($fullpath);
78 0 $self->git_repo->index->remove($path);
79 } else {
80 # a shame there's no way to add the blob directly...
81 1 ▶ $data= \$data->content if ref($data)->isa('Git::Raw::Blob');
Branch: 1 branch
BranchTrueFalseTotal
if ref($data)->isa("Git::Raw::Blob") 0 1 1
82 # Write file
83 1 _mkfile($fullpath, $data, $mode);
84 # Add to the index
85 1 $self->git_repo->index->add_frombuffer($path, $data, $mode);
86 }
87 }
88 else {
89 21 ▶ my $node= ($self->{_changes} //= {});
Condition100%2/2
$self->{"_changes"} //= {}
l!l
156
MC/DC100%1/1
$self->{"_changes"} //= {}
$self->{"_changes"}
Truth table100%2/2
A
A= $self->{"_changes"}
Aresult
11
00
90 21 for (@path) {
91 54 ▶ $node= ($node->{$_} //= {});
Condition100%2/2
$node->{$_} //= {}
l!l
2232
MC/DC100%1/1
$node->{$_} //= {}
$node->{$_}
Truth table100%2/2
A
A= $node->{$_}
Aresult
11
00
92 54 ▶ ref $node eq 'HASH' or die "Can't set '$path'; '$_' is not a directory";
Branch: 1 branch
BranchTrueFalseTotal
ref $node eq "HASH" or die "Can't set '${path}'; '${_}' is not a directory" 54 0 54
93 }
94 # Content may either be a Blob object or a scalar-ref of bytes
95 21 ▶ if (ref $data eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "SCALAR") 18 3 21
96 18 $data= Git::Raw::Blob->create($repo, $$data);
97 }
98 21 ▶ $node->{$basename}= defined $data? [ $data, $mode ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 21 0 21
99 }
100 22 $self->has_changes(1);
101 22 ▶ $self->{_changes} //= {};
Condition100%2/2
$self->{"_changes"} //= {}
l!l
211
MC/DC100%1/1
$self->{"_changes"} //= {}
$self->{"_changes"}
Truth table100%2/2
A
A= $self->{"_changes"}
Aresult
11
00
102 22 $self;
103 }
104  
105 1 ▶sub _mkfile($path, $scalarref, $mode) {
Subroutine _mkfile: called CC=7 SCAR=25.7
106 1 ▶ open my $fh, '>', $path or die "open($path): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "open($path): $!" 1 0 1
107 1 ▶ $fh->print($$scalarref) or die "write($path): $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($$scalarref) or die "write($path): $!" 1 0 1
108 1 ▶ $fh->close or die "close($path): $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or die "close($path): $!" 1 0 1
109 1 ▶ chmod($path, $mode) || die "chmod($path, $mode): $!"
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode and $mode != 33188 0 1 1
Condition25%2/8
defined $mode && $mode != 33188 && (chmod($path, $mode) || die("chmod($path, $mode): $!"))
!ll&&!rl&&r
100
defined $mode and $mode != 33188
!ll&&!rl&&r
010
chmod $path, $mode or die "chmod($path, $mode): $!"
l!l
00
MC/DC0%0/3
defined $mode && $mode != 33188 && (chmod($path, $mode) || die("chmod($path, $mode): $!"))
defined $mode $mode != 33188 chmod $path, $mode
Truth table25%1/4
A and B && C
A= defined $modeB= $mode != 33188C= chmod $path, $mode
ABCresult
0XX0
10X0
1100
1111
110 if defined $mode && $mode != 0100644;
111 }
112  
113  
114 9 ▶sub update_tree($self) {
Subroutine update_tree: called CC=2 SCAR=6.9
Pod documented
115 # If using the Index, the index can write the new tree
116 9 ▶ if ($self->use_workdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_workdir) { } 2 7 9
117 2 $self->tree($self->git_repo->index->write_tree);
118 } else {
119 7 $self->tree(_assemble_tree($self->git_repo, $self->tree, $self->_changes));
120 7 $self->_changes({}); # reset the changes hash
121 }
122 # don't reset has_changes until it has been committed
123 }
124  
125 # merge a hashref of changes into the previous Tree, and return the new Tree
126 # Changes look like:
127 # {
128 # "path1" => {
129 # "filename" => [ $blob, $mode ],
130 # "fname2" => [ $blob, $mode ],
131 # }
132 # }
133 30 ▶sub _assemble_tree($repo, $tree, $changes) {
Subroutine _assemble_tree: called CC=7 SCAR=19.6
134 30 ▶ my $treebuilder= Git::Raw::Tree::Builder->new($repo, ($tree? ($tree) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$tree ? : 5 25 30
135 30 for my $name (keys %$changes) {
136 39 my $ent= $changes->{$name};
137 39 ▶ if (!defined $ent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ent) { } 0 39 39
138 0 $treebuilder->remove($name);
139 }
140 else {
141 39 ▶ if (ref $ent eq 'HASH') { # a subdirectory
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ent eq "HASH") 23 16 39
142 23 my $dirent= $treebuilder->get($name);
143 23 ▶ my $subdir= $dirent && $dirent->type == Git::Raw::Object::TREE()
Branch: 1 branch
BranchTrueFalseTotal
$dirent && $dirent->type == Git::Raw::Object::TREE() ? : 3 20 23
Condition66%2/3
$dirent and $dirent->type == Git::Raw::Object::TREE()
!ll&&!rl&&r
2003
MC/DC50%1/2
$dirent and $dirent->type == Git::Raw::Object::TREE()
$dirent $dirent->type == Git::Raw::Object::TREE()
Truth table66%2/3
A and B
A= $direntB= $dirent->type == Git::Raw::Object::TREE()
ABresult
0X0
100
111
144 ? Git::Raw::Tree->lookup($repo, $dirent->id) : undef;
145 23 $ent= [ _assemble_tree($repo, $subdir, $ent), 0040000 ];
146 }
147 39 $treebuilder->insert($name, @$ent);
148 }
149 }
150 30 return $treebuilder->write; # returns Git::Raw::Tree
151 }
152  
153  
154 7 ▶sub commit($self, $message, %opts) {
Subroutine commit: called CC=15 SCAR=28.5
Pod documented
155 7 ▶ croak "No changes added" unless $self->has_changes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_changes 0 7 7
156 7 my $repo= $self->git_repo;
157 7 $self->update_tree;
158 7 my $branch= $self->branch;
159 7 my $cur_sig= $self->parent->new_signature;
160 7 ▶ my $author= $opts{author} // $cur_sig;
Condition33%1/3
$opts{"author"} // $cur_sig
l!l&&r!l&&!r
070
MC/DC0%0/2
$opts{"author"} // $cur_sig
$opts{"author"} $cur_sig
Truth table33%1/3
A // B
A= $opts{"author"}B= $cur_sig
ABresult
1X1
011
000
161 7 ▶ my $update_head= $self->use_workdir // $opts{update_head};
Condition66%2/3
$self->use_workdir // $opts{"update_head"}
l!l&&r!l&&!r
106
MC/DC50%1/2
$self->use_workdir // $opts{"update_head"}
$self->use_workdir $opts{"update_head"}
Truth table66%2/3
A // B
A= $self->use_workdirB= $opts{"update_head"}
ABresult
1X1
011
000
162 7 ▶ my $committer= $opts{committer} // $cur_sig;
Condition33%1/3
$opts{"committer"} // $cur_sig
l!l&&r!l&&!r
070
MC/DC0%0/2
$opts{"committer"} // $cur_sig
$opts{"committer"} $cur_sig
Truth table33%1/3
A // B
A= $opts{"committer"}B= $cur_sig
ABresult
1X1
011
000
163 my $parents= $self->use_workdir? (
164 # dies on new repo if HEAD doesn't exist yet, in which case no parents
165 1 ▶ eval { [ $self->git_repo->head->target ] } || []
Branch: 2 branches
BranchTrueFalseTotal
$branch ? : 1 5 6
length $opts{'create_branch'} ? : 5 0 5
166 )
167 : $branch? [ $self->branch->peel('commit') ]
168 7 ▶ : length $opts{create_branch}? [] # fresh branch, no parent commit
Branch: 1 branch
BranchTrueFalseTotal
$self->use_workdir ? : 1 6 7
Condition50%1/2
eval { do { [$self->git_repo->head->target] } } or []
l!l
01
MC/DC0%0/1
eval { do { [$self->git_repo->head->target] } } or []
eval { do { [$self->git_repo->head->target] } }
Truth table50%1/2
A
A= eval { do { [$self->git_repo->head->target] } }
Aresult
11
00
169 : croak "Can't commit without a branch or use_workdir or option create_branch";
170 # undef final param means don't update HEAD
171 7 ▶ my $commit= Git::Raw::Commit->create($repo, $message, $author, $committer, $parents, $self->tree, undef)
Branch: 1 branch
BranchTrueFalseTotal
my $commit = "Git::Raw::Commit"->create($repo, $message, $author, $committer, $parents, $self->tree, undef) or croak("commit failed") 7 0 7
172 or croak "commit failed";
173 7 ▶ if ($opts{create_branch}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'create_branch'}) { } 6 1 7
elsif ($branch) { } 1 0 1
174 6 $branch= $repo->branch($opts{create_branch}, $commit);
175 6 $self->branch($branch);
176 } elsif ($branch) {
177 # Update the branch
178 1 $branch->target($commit);
179 }
180 7 ▶ $repo->head($branch) if $branch && $update_head;
Branch: 1 branch
BranchTrueFalseTotal
if $branch and $update_head 1 6 7
Condition66%2/3
$branch and $update_head
!ll&&!rl&&r
061
MC/DC50%1/2
$branch and $update_head
$branch $update_head
Truth table66%2/3
A and B
A= $branchB= $update_head
ABresult
0X0
100
111
181 # persist the index state to disk, which clears the staged changes and brings the index
182 # in sync with the commit
183 7 ▶ $repo->index->write if $self->use_workdir;
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_workdir 1 6 7
184 7 $self->has_changes(0);
185 7 return $commit;
186 }
187  
188 5 1;
189  
190 __END__
« InGit/MirrorTree.pm ↑ Summary InGit/Server.pm »