blib/lib/Git/ObjectStore.pm

filter: Statementstmt 93.1% 191 / 205 Branchbran 67.1% 43 / 64 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 20 / 22 Podpod 100% 14 / 14 totaltotal 87.1% 270 / 310 CC 39 SCAR 17.9
CC
39
Cov
87%
new
15.2
recursive_read
5.1
read_updates
5.0
CRAP 6.0
↑ Summary
1 package Git::ObjectStore;
2 1 $Git::ObjectStore::VERSION = '0.006';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Git::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Git::Raw::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 # ABSTRACT: abstraction layer for Git::Raw and libgit2
13  
14  
15  
16  
17 sub new
18 {
19 3 ▶ my ($class, %args) = @_;
Subroutine new: called CC=14 SCAR=27.2
Pod documented
20 3 my $self = bless {}, $class;
21  
22 3 $self->{'author_name'} = 'ObjectStore';
23 3 $self->{'author_email'} = 'ObjectStore@localhost';
24  
25 3 foreach my $arg (qw(repodir branchname)) {
26 6 ▶ if ( defined( $args{$arg} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$arg}) { } 6 0 6
27 6 $self->{$arg} = $args{$arg};
28 } else {
29 0 croak('Mandatory argument missing: ' . $arg);
30 }
31 }
32  
33 3 foreach my $arg (qw(writer author_name author_email)) {
34 9 ▶ if ( defined( $args{$arg} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$arg}) 1 8 9
35 1 $self->{$arg} = $args{$arg};
36 }
37 }
38  
39 3 ▶ if ( $self->{'writer'} and $args{'goto'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"writer"} and $args{"goto"}) 0 3 3
Condition66%2/3
$self->{"writer"} and $args{"goto"}
!ll&&!rl&&r
210
MC/DC0%0/2
$self->{"writer"} and $args{"goto"}
$self->{"writer"} $args{"goto"}
Truth table66%2/3
A and B
A= $self->{"writer"}B= $args{"goto"}
ABresult
0X0
100
111
40 0 croak('Cannot use goto in writer mode');
41 }
42  
43 3 my $branchname = $self->{'branchname'};
44 3 my $repodir = $self->{'repodir'};
45  
46 3 ▶ if ( not -e $repodir . '/config' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $repodir . '/config') 1 2 3
47 1 ▶ if( $self->{'writer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'writer'}) { } 1 0 1
48 1 Git::Raw::Repository->init($repodir, 1);
49 } else {
50 0 croak($repodir . ' does not contain a bare Git repository');
51 }
52 }
53  
54 3 my $repo = $self->{'repo'} = Git::Raw::Repository->open($repodir);
55 3 my $objdir = catfile($repodir, 'objects');
56  
57 # We do not use loose backend, so we explicitly exclude it from ODB.
58 {
59 3 my $odb = Git::Raw::Odb->new();
60 3 $odb->add_backend(Git::Raw::Odb::Backend::Pack->new($objdir), 10);
61 3 $repo->odb($odb);
62 }
63  
64 3 ▶ if ( $self->{'writer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'writer'}) { } 1 2 3
65  
66 # in-memory store that will write a single pack file for all objects
67 1 $self->{'packdir'} = catfile($objdir, 'pack');
68 1 my $mempack = $self->{'mempack'} = Git::Raw::Mempack->new;
69 1 $repo->odb->add_backend($mempack, 99);
70  
71 1 my $branch = Git::Raw::Branch->lookup($repo, $branchname, 1);
72  
73 1 ▶ if ( not defined($branch) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $branch) 1 0 1
74 # This is a fresh repo, create the branch
75 1 my $builder = Git::Raw::Tree::Builder->new($repo);
76 1 my $tree = $builder->write();
77 1 my $me = $self->_signature();
78 1 my $refname = 'refs/heads/' . $branchname;
79 1 my $commit = $repo->commit("Initial empty commit in $branchname",
80 $me, $me, [], $tree, $refname);
81 1 $self->{'created_init_commit'} = $commit;
82 1 $branch = Git::Raw::Branch->lookup($repo, $branchname, 1);
83 }
84  
85 1 ▶ croak('expected a branch') unless defined($branch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch 0 1 1
86  
87 # in-memory index for preparing a commit
88 1 my $index = Git::Raw::Index->new();
89  
90 # assign the index to our repo
91 1 $repo->index($index);
92  
93 # initiate the index with the top of the branch
94 1 my $commit = $branch->peel('commit');
95 1 $index->read_tree($commit->tree());
96  
97 # memorize the index for quick write access
98 1 $self->{'gitindex'} = $index;
99  
100 1 $self->{'current_commit_id'} = $commit->id();
101  
102 } else {
103 # open the repo for read-only access
104 2 my $commit;
105 2 ▶ if ( defined($args{'goto'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'goto'}) { } 0 2 2
106 # read from a specified commit
107 0 $commit = Git::Raw::Commit->lookup($repo, $args{'goto'});
108 0 ▶ croak('Cannot lookup commit ' . $args{'goto'})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $commit 0 0 0
109 unless defined($commit);
110 } else {
111 # read from the top of the branch
112 2 my $branch = Git::Raw::Branch->lookup($repo, $branchname, 1);
113 2 $commit = $branch->peel('commit');
114 }
115  
116 # memorize the tree that we will read
117 2 $self->{'gittree'} = $commit->tree();
118  
119 2 $self->{'current_commit_id'} = $commit->id();
120 }
121  
122 3 return $self;
123 }
124  
125  
126 sub _signature
127 {
128 3 ▶ my $self = shift;
Subroutine _signature: called CC=1 SCAR=0.0
129 return Git::Raw::Signature->now
130 3 ($self->{'author_name'}, $self->{'author_email'});
131 }
132  
133  
134  
135 sub created_init_commit
136 {
137 0 ▶ my $self = shift;
Subroutine created_init_commit: not called CC=1 SCAR=6.9
Pod documented
138 0 return $self->{'created_init_commit'};
139 }
140  
141  
142  
143  
144 sub repo
145 {
146 0 ▶ my $self = shift;
Subroutine repo: not called CC=1 SCAR=6.9
Pod documented
147 0 return $self->{'repo'};
148 }
149  
150  
151  
152  
153  
154 sub read_file
155 {
156 6 ▶ my $self = shift;
Subroutine read_file: called CC=4 SCAR=13.9
Pod documented
157 6 my $filename = shift;
158  
159 6 ▶ if ( $self->{'writer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'writer'}) { } 2 4 6
160 2 my $entry = $self->{'gitindex'}->find($filename);
161 2 ▶ if ( defined($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) { } 1 1 2
162 1 return $entry->blob()->content();
163 } else {
164 1 return undef;
165 }
166 } else {
167 4 my $entry = $self->{'gittree'}->entry_bypath($filename);
168 4 ▶ if ( defined($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) { } 4 0 4
169 4 return $entry->object()->content();
170 } else {
171 0 return undef;
172 }
173 }
174 }
175  
176  
177  
178 sub file_exists
179 {
180 2 ▶ my $self = shift;
Subroutine file_exists: called CC=2 SCAR=7.4
Pod documented
181 2 my $filename = shift;
182  
183 2 ▶ if ( $self->{'writer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'writer'}) { } 2 0 2
184 2 return defined($self->{'gitindex'}->find($filename));
185 } else {
186 0 return defined($self->{'gittree'}->entry_bypath($filename));
187 }
188 }
189  
190  
191 sub current_commit_id
192 {
193 4 ▶ my $self = shift;
Subroutine current_commit_id: called CC=1 SCAR=0.0
Pod documented
194 4 return $self->{'current_commit_id'};
195 }
196  
197  
198  
199 sub write_and_check
200 {
201 2 ▶ my $self = shift;
Subroutine write_and_check: called CC=3 SCAR=11.0
Pod documented
202 2 my $filename = shift;
203 2 my $data = shift;
204  
205 croak('write_and_check() is called for a read-only ObjectStore object')
206 2 ▶ unless $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 0 2 2
207  
208 2 my $prev_blob_id = '';
209 2 ▶ if( defined(my $entry = $self->{'gitindex'}->find($filename)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = $self->{"gitindex"}->find($filename))) 1 1 2
210 1 $prev_blob_id = $entry->blob()->id();
211 }
212  
213 2 my $entry = $self->{'gitindex'}->add_frombuffer($filename, $data);
214 2 my $new_blob_id = $entry->blob()->id();
215  
216 2 return ($new_blob_id ne $prev_blob_id);
217 }
218  
219  
220  
221 sub write_file
222 {
223 7 ▶ my $self = shift;
Subroutine write_file: called CC=2 SCAR=7.0
Pod documented
224 7 my $filename = shift;
225 7 my $data = shift;
226  
227 croak('write_file() is called for a read-only ObjectStore object')
228 7 ▶ unless $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 0 7 7
229  
230 7 $self->{'gitindex'}->add_frombuffer($filename, $data);
231 7 return;
232 }
233  
234  
235  
236 sub delete_file
237 {
238 2 ▶ my $self = shift;
Subroutine delete_file: called CC=2 SCAR=7.0
Pod documented
239 2 my $path = shift;
240  
241 croak('delete_file() is called for a read-only ObjectStore object')
242 2 ▶ unless $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 0 2 2
243 2 $self->{'gitindex'}->remove($path);
244 2 return;
245 }
246  
247  
248  
249 sub create_commit
250 {
251 3 ▶ my $self = shift;
Subroutine create_commit: called CC=4 SCAR=13.9
Pod documented
252 3 my $msg = shift;
253  
254 croak('create_commit() is called for a read-only ObjectStore object')
255 3 ▶ unless $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 0 3 3
256  
257 3 ▶ if( not defined($msg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 3 0 3
258 3 $msg = scalar(localtime(time()));
259 }
260  
261 3 my $branchname = $self->{'branchname'};
262 3 my $repo = $self->{'repo'};
263 3 my $index = $self->{'gitindex'};
264  
265 3 my $branch = Git::Raw::Branch->lookup($self->{'repo'}, $branchname, 1);
266 3 my $parent = $branch->peel('commit');
267  
268 # this creates a new tree object from changes in the index
269 3 my $tree = $index->write_tree();
270  
271 3 ▶ if( $tree->id() eq $parent->tree()->id() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->id eq $parent->tree->id) 1 2 3
272 # The tree identifier has not changed, hence there are no
273 # changes in content
274 1 return 0;
275 }
276  
277 2 my $me = $self->_signature();
278 2 my $commit = $repo->commit
279 ($msg, $me, $me, [$parent], $tree, $branch->name());
280  
281 # re-initialize the index
282 2 $index->clear();
283 2 $index->read_tree($tree);
284  
285 2 $self->{'current_commit_id'} = $commit->id();
286  
287 2 return 1;
288 }
289  
290  
291  
292 sub write_packfile
293 {
294 3 ▶ my $self = shift;
Subroutine write_packfile: called CC=2 SCAR=6.9
Pod documented
295  
296 croak('write_packfile() is called for a read-only ObjectStore object')
297 3 ▶ unless $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 0 3 3
298  
299 3 my $repo = $self->{'repo'};
300 3 my $tp = Git::Raw::TransferProgress->new();
301 3 my $indexer = Git::Raw::Indexer->new($self->{'packdir'}, $repo->odb());
302  
303 3 $indexer->append($self->{'mempack'}->dump($repo), $tp);
304 3 $indexer->commit($tp);
305 3 $self->{'mempack'}->reset;
306 3 return;
307 }
308  
309  
310  
311 sub create_commit_and_packfile
312 {
313 3 ▶ my $self = shift;
Subroutine create_commit_and_packfile: called CC=3 SCAR=11.0
Pod documented
314 3 my $msg = shift;
315  
316 3 ▶ if( $self->create_commit($msg) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->create_commit($msg)) { } 2 1 3
elsif (defined $self->{'created_init_commit'}) { } 1 0 1
317 2 $self->write_packfile();
318 2 return 1;
319 } elsif ( defined($self->{'created_init_commit'}) ) {
320 1 $self->write_packfile();
321 }
322  
323 1 return 0;
324 }
325  
326  
327  
328 sub recursive_read
329 {
330 4 ▶ my $self = shift;
Subroutine recursive_read: called CC=5 SCAR=16.3
Pod documented
331 4 my $path = shift;
332 4 my $callback = shift;
333 4 my $no_content = shift;
334
335 croak('recursive_read() is called for a read-write ObjectStore object')
336 4 ▶ if $self->{'writer'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"writer"} 0 4 4
337  
338 4 ▶ if( $path eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '') { } 2 2 4
339 {
340 2 foreach my $entry ($self->{'gittree'}->entries()) {
341 9 $self->_do_recursive_read
342 ($entry, $entry->name(), $callback, $no_content);
343 }
344 } else {
345 2 my $entry = $self->{'gittree'}->entry_bypath($path);
346 2 ▶ if( defined($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) { } 2 0 2
347 2 $self->_do_recursive_read($entry, $path, $callback, $no_content);
348 }
349 else
350 {
351 0 croak("No such path in the branch: $path");
352 }
353 }
354 4 return;
355 }
356  
357  
358 sub _do_recursive_read
359 {
360 26 ▶ my $self = shift;
Subroutine _do_recursive_read: called CC=4 SCAR=14.0
361 26 my $entry = shift; # Git::Raw::Tree::Entry object
362 26 my $path = shift;
363 26 my $callback = shift;
364 26 my $no_content = shift;
365  
366 26 $entry->type();
367  
368 26 ▶ if( $entry->type() == Git::Raw::Object->TREE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->type == 'Git::Raw::Object'->TREE) { } 15 11 26
369 # this is a subtree, we read it recursively
370 15 foreach my $child_entry ($entry->object()->entries()) {
371 15 $self->_do_recursive_read
372 ($child_entry, $path . '/' . $child_entry->name(), $callback);
373 }
374 } else {
375 11 ▶ if( $no_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_content) { } 0 11 11
376 0 &{$callback}($path);
377 } else {
378 11 &{$callback}($path, $entry->object()->content());
379 }
380 }
381  
382 26 return;
383 }
384  
385  
386  
387  
388  
389 sub read_updates
390 {
391 2 ▶ my $self = shift;
Subroutine read_updates: called CC=5 SCAR=16.1
Pod documented
392 2 my $old_commit_id = shift;
393 2 my $cb_updated = shift;
394 2 my $cb_deleted = shift;
395 2 my $no_content = shift;
396  
397 2 my $old_commit = Git::Raw::Commit->lookup($self->{'repo'}, $old_commit_id);
398 2 ▶ croak("Cannot lookup commit $old_commit_id") unless defined($old_commit);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_commit 0 2 2
399 2 my $old_tree = $old_commit->tree();
400  
401 2 my $new_tree = $self->{'gittree'};
402  
403 2 my $diff = $old_tree->diff
404 (
405 {
406 'tree' => $new_tree,
407 'flags' => {
408 'skip_binary_check' => 1,
409 },
410 }
411 );
412  
413 2 my @deltas = $diff->deltas();
414 2 foreach my $delta (@deltas) {
415  
416 10 my $path = $delta->new_file()->path();
417  
418 10 ▶ if( $delta->status() eq 'deleted') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta->status eq 'deleted') { } 4 6 10
419 4 &{$cb_deleted}($path);
420 } else {
421 6 ▶ if( $no_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_content) { } 3 3 6
422 3 &{$cb_updated}($path);
423 } else {
424 3 my $entry = $new_tree->entry_bypath($path);
425 3 &{$cb_updated}($path, $entry->object()->content());
426 }
427 }
428 }
429  
430 2 return;
431 }
432  
433  
434  
435  
436  
437  
438  
439 1 1;
440  
441 # Local Variables:
442 # mode: cperl
443 # indent-tabs-mode: nil
444 # cperl-indent-level: 4
445 # cperl-continued-statement-offset: 4
446 # cperl-continued-brace-offset: -4
447 # cperl-brace-offset: 0
448 # cperl-label-offset: -2
449 # End:
450  
451 __END__
↑ Summary