LowLevel/Tree.pm

filter: Statementstmt 64.5% 80 / 124 Branchbran 41.6% 15 / 36 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 77.7% 14 / 18 Podpod 100% 13 / 13 totaltotal 63.1% 127 / 201 CC 28 SCAR 25.8
CC
28
Cov
59%
del
56.0
timestamp_added
12.0
timestamp_last
12.0
CRAP 13.2
↑ Summary LowLevel/Blob.pm »
1 package Git::LowLevel::Tree;
2 # ABSTRACT: class representing a tree object in a GIT Repository
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use MooseX::Privacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Git::LowLevel::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 has 'repository' => (is => 'ro', isa => 'Git::LowLevel', required => 1);
9  
10 4 has 'reference' => (is => 'ro', isa => 'Git::LowLevel::Reference', required => 1);
11  
12 4 has 'path' => (is => 'rw',isa=>'Str', default=> '.');
13  
14 4 has 'hash' => (is => 'rw', isa => 'Str', required => 1);
15  
16 8 ▶has 'tree' => (is => 'rw',isa=> 'ArrayRef', default=> sub {return [];});
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 4 has 'parent' => (is => 'rw', isa => 'Git::LowLevel::Tree');
19  
20  
21 4 has 'changed' => (is => 'rw', isa => 'Bool', default=>0);
22  
23 sub timestamp_added
24 {
25 0 ▶ my $self = shift;
Subroutine timestamp_added: not called CC=3 SCAR=24.8
Pod documented
26 0 my $help = readpipe($self->repository->gitcmd() . " log --follow --diff-filter=A --date=raw " . $self->reference->reference . " -- " . $self->path . " 2>/dev/null");
27 0 my @lines = split /\n/,$help;
28 0 for my $line (@lines)
29 {
30 0 ▶ if ($line =~ /^Date:\s+(\d+)\s([+-]\d+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Date:\s+(\d+)\s([+-]\d+)$/) 0 0 0
31 {
32 0 return $1;
33 }
34 }
35  
36 0 return $help;
37 }
38  
39 sub timestamp_last
40 {
41 0 ▶ my $self = shift;
Subroutine timestamp_last: not called CC=3 SCAR=24.8
Pod documented
42 0 my $help = readpipe($self->repository->gitcmd() . " log --follow --date=raw " . $self->reference->reference . " -- " . $self->path . " 2>/dev/null");
43 0 my @lines = split /\n/,$help;
44 0 for my $line (@lines)
45 {
46 0 ▶ if ($line =~ /^Date:\s+(\d+)\s([+-]\d+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Date:\s+(\d+)\s([+-]\d+)$/) 0 0 0
47 {
48 0 return $1;
49 }
50 }
51  
52 0 return $help;
53 }
54  
55 sub committer
56 {
57 0 ▶ my $self = shift;
Subroutine committer: not called CC=3 SCAR=24.8
Pod documented
58 0 my $help = readpipe($self->repository->gitcmd() . " log --follow --date=raw " . $self->reference->reference . " -- " . $self->path . " 2>/dev/null");
59 0 my @lines = split /\n/,$help;
60 0 for my $line (@lines)
61 {
62 0 ▶ if ($line =~ /^Author:\s(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Author:\s(.*)$/) 0 0 0
63 {
64 0 return $1;
65 }
66 }
67  
68 0 return $help;
69 }
70  
71  
72 sub mypath
73 {
74 6 ▶ my $self = shift;
Subroutine mypath: called CC=1 SCAR=0.0
Pod documented
75 6 my $path = basename($self->path);
76  
77 6 return $path;
78 }
79  
80 sub get
81 {
82 13 ▶ my $self = shift;
Subroutine get: called CC=4 SCAR=15.2
Pod documented
83 13 my @ret;
84  
85 13 ▶ return @{$self->tree} unless @{$self->tree} == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->tree;} == 0 5 8 13
86  
87 8 my $help = readpipe($self->repository->gitcmd() . " ls-tree " . $self->reference->reference . " \"" . $self->path . "/\" 2>/dev/null");
88 8 my @lines = split /\n/,$help;
89 8 for my $line (@lines)
90 {
91 0 $line =~ /^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
92  
93 0 ▶ if ($2 eq "blob")
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq 'blob') { } 0 0 0
94 {
95 0 push(@ret, Git::LowLevel::Blob->new(repository=>$self->repository, hash => $3, path => $4, parent=>$self));
96 }
97 else
98 {
99 0 push(@ret, Git::LowLevel::Tree->new(repository=>$self->repository, reference=> $self->reference, hash => $3, path => $4 . "/", parent=>$self));
100 }
101  
102 }
103 8 $self->tree(\@ret);
104  
105 8 return @ret;
106 }
107  
108 sub empty
109 {
110 5 ▶ my $self = shift;
Subroutine empty: called CC=2 SCAR=6.9
Pod documented
111 5 $self->get();
112  
113 5 ▶ if (@{$self->tree} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->tree;} == 0) 3 2 5
114 {
115 3 return 1;
116 }
117  
118 2 return 0;
119 }
120  
121 sub newBlob
122 {
123 3 ▶ my $self = shift;
Subroutine newBlob: called CC=1 SCAR=0.0
Pod documented
124  
125 3 return Git::LowLevel::Blob->new(repository => $self->repository);
126 }
127  
128 sub newTree
129 {
130 1 ▶ my $self = shift;
Subroutine newTree: called CC=1 SCAR=0.0
Pod documented
131  
132 1 return Git::LowLevel::Tree->new(repository => $self->repository, reference => $self->reference, hash=>"empty", changed => 1);
133 }
134  
135 sub add
136 {
137 6 ▶ my $self = shift;
Subroutine add: called CC=6 SCAR=18.0
Pod documented
138 6 my $elem = shift;
139  
140 6 ▶ die("no supported object type " . ref($elem)) unless ref($elem) eq 'Git::LowLevel::Blob' || ref($elem) eq 'Git::LowLevel::Tree';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree" 2 4 6
Condition100%3/3
ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree"
l!l&&r!l&&!r
312
MC/DC100%2/2
ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree"
ref $elem eq "Git::LowLevel::Blob" ref $elem eq "Git::LowLevel::Tree"
Truth table100%3/3
A or B
A= ref $elem eq "Git::LowLevel::Blob"B= ref $elem eq "Git::LowLevel::Tree"
ABresult
1X1
011
000
141  
142 4 ▶ if (ref($elem) eq "Git::LowLevel::Blob")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq "Git::LowLevel::Blob") 3 1 4
143 {
144 3 ▶ die("path missing in Blob") unless length($elem->path)>0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $elem->path > 0 0 3 3
145 3 ▶ die("content missing in Blob") unless length($elem->_content)>0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $elem->_content > 0 0 3 3
146 }
147  
148 4 $self->get();
149 4 $self->changed(1);
150 4 $elem->parent($self);
151 4 push(@{$self->tree}, $elem);
152 }
153  
154 sub del
155 {
156 0 ▶ my $self = shift;
Subroutine del: not called CC=7 SCAR=40.3
Pod documented
157 0 my $elem = shift;
158  
159 0 ▶ die("no supported object type " . ref($elem)) unless ref($elem) eq 'Git::LowLevel::Blob' || ref($elem) eq 'Git::LowLevel::Tree';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree" 0 0 0
Condition0%0/3
ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $elem eq "Git::LowLevel::Blob" or ref $elem eq "Git::LowLevel::Tree"
ref $elem eq "Git::LowLevel::Blob" ref $elem eq "Git::LowLevel::Tree"
Truth table0%0/3
A or B
A= ref $elem eq "Git::LowLevel::Blob"B= ref $elem eq "Git::LowLevel::Tree"
ABresult
1X1
011
000
160  
161 0 $self->get();
162 0 my @tree;
163 0 for my $t (@{$self->tree})
164 {
165 0 my $path = fileparse($t->path);
166 0 ▶ if (length($path)==0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $path == 0) 0 0 0
167 {
168 0 $path = $t->path;
169 0 $path =~ s/\///;
170 }
171  
172 0 my $path2 = fileparse($elem->path);
173 0 ▶ if (length($path2)==0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $path2 == 0) 0 0 0
174 {
175 0 $path2 = $elem->path;
176 0 $path2 =~ s/\///;
177 }
178  
179 0 ▶ if ($path ne $path2)
Branch: 1 branch
BranchTrueFalseTotal
if ($path ne $path2) 0 0 0
180 {
181 0 push(@tree,$t);
182 }
183  
184 }
185 0 $self->changed(1);
186 0 $self->tree(\@tree);
187  
188 }
189  
190 sub save
191 {
192 4 ▶ my $self = shift;
Subroutine save: called CC=3 SCAR=11.0
Pod documented
193 4 my $str = "";
194  
195 4 for my $t (@{$self->tree})
196 {
197 4 $t->save;
198 4 $str .= $t->treeEntry;
199 }
200  
201 4 ▶ if ($self->changed()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->changed) 4 0 4
202 4 print "STR:" . $str . "\n";
203 4 my $help = readpipe("/bin/echo -e \"" . $str. "\" | " . $self->repository->gitcmd() . " mktree --batch ");
204 4 chomp($help);
205 4 $self->hash($help);
206 4 $self->changed(0);
207 }
208  
209 }
210 sub treeEntry
211 {
212 1 ▶ my $self = shift;
Subroutine treeEntry: called CC=2 SCAR=7.1
Pod documented
213  
214 1 ▶ return "" unless $self->hash ne "empty";
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hash ne "empty" 0 1 1
215  
216 1 return "040000 tree " . $self->hash . "\t" . $self->mypath() . "\n";
217 }
218  
219  
220 sub find
221 {
222 5 ▶ my $self = shift;
Subroutine find: called CC=4 SCAR=13.9
Pod documented
223 5 my $path = shift;
224  
225 5 ▶ return $self unless $self->mypath ne $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mypath ne $path 1 4 5
226  
227 4 $self->get();
228 4 for my $t (@{$self->tree})
229 {
230 3 my $r = $t->find($path);
231 3 ▶ if (defined($r))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 3 0 3
232 {
233 3 return $r;
234 }
235 }
236  
237 1 return;
238 }
239  
240 4 1;
241  
242 __END__
243  
244 =pod
245  
246 =encoding UTF-8
247  
248 =head1 NAME
249  
250 Git::LowLevel::Tree - class representing a tree object in a GIT Repository
251  
252 =head1 VERSION
253  
254 version 0.2
255  
256 =head1 ATTRIBUTES
257  
258 =head2 repository
259  
260 the repository the tree lives in
261  
262 =head2 reference
263  
264 the reference the tree lives in
265  
266 =head2 path
267  
268 the path to the tree object
269  
270 =head2 hash
271  
272 the hash identifying the tree object
273  
274 =head2 tree
275  
276 the tree entries
277  
278 =head2 parent
279  
280 the parent of the object within the tree
281  
282 =head2 changed
283  
284 identifies if the tree has been changed
285 add,del or initial create of the tree
286  
287 =head1 METHODS
288  
289 =head2 timestamp_added
290  
291 returns the timestamp of the commit where this object was added or undef if
292 object has not been commited yet.
293  
294 =head2 timestamp_last
295  
296 returns the timestamp of the last commit of this object or undef if
297 object has not been commited yet.
298  
299 =head2 committer
300  
301 returns the committer of the the object
302  
303 =head2 mypath
304  
305 =head2 get
306  
307 returns an array of all the tree objects
308  
309 =head2 empty
310  
311 tests if the tree is empty
312  
313 =head2 newBlob
314  
315 create a new blob entry
316  
317 =head2 newTree
318  
319 create a new tree entry
320  
321 =head2 add
322  
323 add a new tree element
324  
325 =head2 del
326  
327 delete an element from the tree
328  
329 =head2 save
330  
331 save the full tree to the git object safe
332  
333 =head2 treeEntry
334  
335 return a string representing a tree entry
336  
337 =head2 find
338  
339 find a path within the tree
340  
341 =head1 AUTHOR
342  
343 Dominik Meyer <dmeyer@federationhq.de>
344  
345 =head1 COPYRIGHT AND LICENSE
346  
347 This software is Copyright (c) 2018 by Dominik Meyer.
348  
349 This is free software, licensed under:
350  
351 The GNU General Public License, Version 2, June 1991
352  
353 =cut
↑ Summary LowLevel/Blob.pm »