blib/lib/Git/ObjectStore.pm

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