Database/Backend/Git/Sub.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 87.5% 14 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 8 totaltotal 86.9% 100 / 115 SCAR 23.0
CC
10
Cov
94%
__ANON__
4.1
get_object_attributes
4.0
all_digests
3.0
CRAP 10.0
« Database/Backend/Git/Wrapper.pm ↑ Summary Database/Backend/Git/Raw/Repository.pm »
1 package Git::Database::Backend::Git::Sub;
2 8 $Git::Database::Backend::Git::Sub::VERSION = '0.012';
3 8 use Git::Sub qw(
4 cat_file
5 hash_object
6 rev_list
7 show_ref
8 update_ref
9 version
10 8 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Git::Version::Compare qw( ge_git );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 with
19 'Git::Database::Role::Backend',
20 'Git::Database::Role::ObjectReader',
21 'Git::Database::Role::ObjectWriter',
22 'Git::Database::Role::RefReader',
23 'Git::Database::Role::RefWriter',
24 ;
25  
26 # the store attribute is a directory name
27 # or an object representing a directory
28 # (e.g. Path::Class, Path::Tiny, File::Fu)
29 has '+store' => (
30 is => 'ro',
31 required => 1,
32 predicate => 1,
33 coerce => sub {
34 46 ▶ my $dir = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
35 return # coerce to an absolute path
36 File::Spec->file_name_is_absolute($dir) ? $dir
37 46 ▶ : ref $dir ? eval { ref($dir)->new( File::Spec->rel2abs($dir) ) }
Branch: 2 branches
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($dir) ? : 37 9 46
ref $dir ? : 6 3 9
Condition33%1/3
eval { or "File::Spec"->rel2abs($dir)
l!l&&r!l&&!r
600
MC/DC0%0/2
eval { do { ref($dir)->new("File::Spec"->rel2abs($dir)) } } or "File::Spec"->rel2abs($dir)
eval { do { ref($dir)->new("File::Spec"->rel2abs($dir)) } } "File::Spec"->rel2abs($dir)
Truth table33%1/3
A or B
A= eval { do { ref($dir)->new("File::Spec"->rel2abs($dir)) } }B= "File::Spec"->rel2abs($dir)
ABresult
1X1
011
000
38 || File::Spec->rel2abs($dir)
39 : File::Spec->rel2abs($dir);
40 },
41 8 );
42  
43 # Git::Database::Role::Backend
44 sub hash_object {
45 95 ▶ my ( $self, $object ) = @_;
Subroutine hash_object: called CC=1 SCAR=0.0
Pod undocumented
46 95 my $keeper = pushd $self->store;
47 95 my $hash = git::hash_object
48 '-t' => $object->kind,
49 '--stdin' => \$object->content;
50 95 return $hash;
51 }
52  
53 # Git::Database::Role::ObjectReader
54 sub get_object_meta {
55 57 ▶ my ( $self, $digest ) = @_;
Subroutine get_object_meta: called CC=2 SCAR=6.9
Pod undocumented
56 57 my $keeper = pushd $self->store;
57 57 my $meta = git::cat_file
58 '--batch-check' => \"$digest\n";
59  
60 # protect against weird cases like if $digest contains a space
61 57 my @parts = split / /, $meta;
62 57 ▶ return ( $digest, 'missing', undef ) if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 33 24 57
63  
64 24 my ( $kind, $size ) = splice @parts, -2;
65 24 return join( ' ', @parts ), $kind, $size;
66 }
67  
68 sub get_object_attributes {
69 156 ▶ my ( $self, $digest ) = @_;
Subroutine get_object_attributes: called CC=4 SCAR=13.9
Pod undocumented
70  
71 156 my $out = do {
72 156 my $keeper = pushd $self->store;
73 156 local $/;
74 156 git::cat_file '--batch' => \"$digest\n";
75 };
76  
77 156 my ( $meta, $content ) = split "\n", $out, 2;
78  
79 # protect against weird cases like if $digest contains a space
80 156 my ( $sha1, $kind, $size ) = my @parts = split / /, $meta;
81  
82 # git versions >= 2.11.0.rc0 throw more verbose errors
83 156 ▶ return undef if $parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/ 0 156 156
84  
85 # object does not exist in the git object database
86 156 ▶ return undef if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 59 97 156
87  
88 # git versions >= 2.21.0.rc0 explicitely say if a sha1 is ambiguous
89 97 ▶ return undef if $kind eq 'ambiguous';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "ambiguous" 1 96 97
90  
91 return {
92 96 kind => $kind,
93 size => $size,
94 content => substr( $content, 0, $size ),
95 digest => $sha1
96 };
97 }
98  
99 sub all_digests {
100 11 ▶ my ( $self, $kind ) = @_;
Subroutine all_digests: called CC=3 SCAR=11.1
Pod undocumented
101  
102 11 ▶ my $re = $kind ? qr/ \Q$kind\E / : qr/ /;
Branch: 1 branch
BranchTrueFalseTotal
$kind ? : 8 3 11
103 11 my @digests;
104  
105 11 my $keeper = pushd $self->store;
106  
107 # the --batch-all-objects option appeared in v2.6.0-rc0
108 11 ▶ if ( ge_git git::version, '2.6.0.rc0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ge_git(git::version(), '2.6.0.rc0')) { } 11 0 11
109 11 @digests = map +( split / / )[0],
110 grep /$re/,
111 git::cat_file '--batch-check', '--batch-all-objects';
112 }
113 else { # this won't return unreachable objects
114 0 @digests =
115 map +( split / / )[0], grep /$re/,
116 git::cat_file '--batch-check', \join '', map +( split / / )[0] . "\n",
117 sort +git::rev_list '--all', '--objects';
118 }
119  
120 11 return @digests;
121 }
122  
123 # Git::Database::Role::ObjectWriter
124 sub put_object {
125 13 ▶ my ( $self, $object ) = @_;
Subroutine put_object: called CC=1 SCAR=0.0
Pod undocumented
126 13 my $keeper = pushd $self->store;
127  
128 13 my $hash = git::hash_object
129 '-w',
130 '-t' => $object->kind,
131 '--stdin' => \$object->content;
132 13 return $hash;
133 }
134  
135 # Git::Database::Role::RefReader
136 sub refs {
137 20 ▶ my ($self) = @_;
Subroutine refs: called CC=1 SCAR=0.0
Pod undocumented
138 20 my $keeper = pushd $self->store;
139 20 my %digest = reverse map +( split / / ),
140 git::show_ref '--head';
141 20 return \%digest;
142 }
143  
144 # Git::Database::Role::RefWriter
145 sub put_ref {
146 2 ▶ my ( $self, $refname, $digest ) = @_;
Subroutine put_ref: called CC=1 SCAR=0.0
Pod undocumented
147 2 my $keeper = pushd $self->store;
148 2 git::update_ref( $refname, $digest );
149 return
150 2 }
151  
152 sub delete_ref {
153 2 ▶ my ( $self, $refname ) = @_;
Subroutine delete_ref: called CC=1 SCAR=0.0
Pod undocumented
154 2 my $keeper = pushd $self->store;
155 2 git::update_ref( '-d', $refname );
156 return
157 2 }
158  
159 8 1;
160  
161 __END__
162  
163 =pod
164  
165 =for Pod::Coverage
166 has_object_checker
167 has_object_factory
168 DEMOLISH
169 hash_object
170 get_object_attributes
171 get_object_meta
172 all_digests
173 put_object
174 refs
175 put_ref
176 delete_ref
177  
178 =head1 NAME
179  
180 Git::Database::Backend::Git::Sub - A Git::Database backend based on Git::Sub
181  
182 =head1 VERSION
183  
184 version 0.012
185  
186 =head1 SYNOPSIS
187  
188 # Git::Sub does not offer an OO interface
189 $dir = 'path/to/some/git/repository/';
190  
191 # let Git::Database figure it out by itself
192 my $db = Git::Database->new( store => $dir );
193  
194 =head1 DESCRIPTION
195  
196 This backend reads and writes data from a Git repository using the
197 L<Git::Sub> Git wrapper.
198  
199 Since L<Git::Sub> has a functional interface, the
200 L<store|Git::Database::Role::Backend/store> attribute is simply the path
201 in which the git commands will run. If the path is a relative path,
202 it will be coerced to an absolute path.
203  
204 Note that overloaded objects (such as L<Path::Tiny>, L<Path::Class> and
205 others) that stringify to the actual path are supported. When coercion
206 to an absolute path occurs, it attempts to create an object of the same
207 class representing the absolute path. If the coercion fails to create an
208 object of the same class, the store attribute will be a string containing
209 the absolute path.
210  
211 =head2 Git Database Roles
212  
213 This backend does the following roles
214 (check their documentation for a list of supported methods):
215 L<Git::Database::Role::Backend>,
216 L<Git::Database::Role::ObjectReader>,
217 L<Git::Database::Role::ObjectWriter>,
218 L<Git::Database::Role::RefReader>,
219 L<Git::Database::Role::RefWriter>.
220  
221 =head1 CAVEAT
222  
223 This backend may have issues with Perl 5.8.9, they are fixed in L<Git::Sub> 0.163320.
224  
225 There is also a minimum requirement on L<System::Sub> 0.162800.
226  
227 =head1 AUTHOR
228  
229 Philippe Bruhat (BooK) <book@cpan.org>
230  
231 =head1 COPYRIGHT
232  
233 Copyright 2016-2019 Philippe Bruhat (BooK), all rights reserved.
234  
235 =head1 LICENSE
236  
237 This program is free software; you can redistribute it and/or modify it
238 under the same terms as Perl itself.
239  
240 =cut
« Database/Backend/Git/Wrapper.pm ↑ Summary Database/Backend/Git/Raw/Repository.pm »