blib/lib/CPAN/InGit/MutableTree.pm

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