Database/Backend/Git.pm

filter: Statementstmt 83.1% 79 / 95 Branchbran 77.2% 17 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 9 totaltotal 78.1% 111 / 142 SCAR 25.5
CC
12
Cov
82%
get_object_attributes
7.2
all_digests
4.1
DEMOLISH
4.0
CRAP 12.8
« Database/Object/Tag.pm ↑ Summary Database/Backend/Git/Wrapper.pm »
1 package Git::Database::Backend::Git;
2 8 $Git::Database::Backend::Git::VERSION = '0.012';
3 8 ▶use Git::Version::Compare qw( ge_git );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 with
10 'Git::Database::Role::Backend',
11 'Git::Database::Role::ObjectReader',
12 'Git::Database::Role::ObjectWriter',
13 'Git::Database::Role::RefReader',
14 'Git::Database::Role::RefWriter',
15 ;
16  
17 8 has '+store' => (
18 isa => quote_sub( q{
19 die 'store is not a Git object'
20 if !eval { $_[0]->isa('Git') }
21 } ),
22 );
23  
24 has object_factory => (
25 is => 'lazy',
26 init_arg => undef,
27 0 ▶ builder => sub { [ $_[0]->store->command_bidi_pipe( 'cat-file', '--batch' ) ] },
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_object_factory: called CC=1 SCAR=1.2
28 8 predicate => 1,
29 clearer => 1,
30 );
31  
32 has object_checker => (
33 is => 'lazy',
34 init_arg => undef,
35 11 ▶ builder => sub { [ $_[0]->store->command_bidi_pipe( 'cat-file', '--batch-check' ) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 8 predicate => 1,
37 clearer => 1,
38 );
39  
40 # Git::Database::Role::Backend
41 sub hash_object {
42 95 ▶ my ( $self, $object ) = @_;
Subroutine hash_object: called CC=1 SCAR=0.0
Pod undocumented
43 95 my ( $pid, $in, $out, $ctx ) =
44 $self->store->command_bidi_pipe( 'hash-object', '-t', $object->kind,
45 '--stdin' );
46 95 print {$out} $object->content;
47 95 close $out;
48 95 chomp( my $digest = <$in> );
49 95 $self->store->command_close_bidi_pipe( $pid, $in, undef, $ctx ); # $out closed
50 95 return $digest;
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 $checker = $self->object_checker;
57  
58 # request the object
59 57 print { $checker->[2] } $digest, "\n";
60  
61 # process the reply
62 57 local $/ = "\012";
63 57 chomp( my $reply = $checker->[1]->getline );
64  
65 # protect against weird cases like if $digest contains a space
66 57 my @parts = split / /, $reply;
67 57 ▶ return ( $digest, 'missing', undef ) if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 33 24 57
68  
69 24 my ( $kind, $size ) = splice @parts, -2;
70 24 return join( ' ', @parts ), $kind, $size;
71 }
72  
73 sub get_object_attributes {
74 156 ▶ my ( $self, $digest ) = @_;
Subroutine get_object_attributes: called CC=6 SCAR=19.8
Pod undocumented
75 156 my $factory = $self->object_factory;
76  
77 # request the object
78 156 print { $factory->[2]} $digest, "\n";
79  
80 # process the reply
81 156 my $out = $factory->[1];
82 156 local $/ = "\012";
83 156 chomp( my $reply = <$out> );
84  
85 # protect against weird cases like if $sha1 contains a space
86 156 my ( $sha1, $kind, $size ) = my @parts = split / /, $reply;
87  
88 # git versions >= 2.11.0.rc0 throw more verbose errors
89 156 ▶ if ( $parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/) 0 156 156
90 0 <$out>; # eat the next line
91 0 return undef;
92 }
93  
94 # object does not exist in the git object database
95 156 ▶ return undef if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 59 97 156
96  
97 # git versions >= 2.21.0.rc0 explicitely say if a sha1 is ambiguous
98 97 ▶ return undef if $kind eq 'ambiguous';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "ambiguous" 1 96 97
99  
100 # read the whole content in memory at once
101 96 my $res = read $out, (my $content), $size;
102 96 ▶ if( $res != $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res != $size) 0 96 96
103 0 $factory->close; # in case the exception is trapped
104 0 $self->clear_object_factory;
105 0 die "Read $res/$size of content from git";
106 }
107  
108 # read the last byte
109 96 $res = read $out, (my $junk), 1;
110 96 ▶ if( $res != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res != 1) 0 96 96
111 0 $factory->close; # in case the exception is trapped
112 0 $self->clear_object_factory;
113 0 die "Unable to finish reading content from git";
114 }
115  
116 # careful with utf-8!
117 # create a new hash with kind, digest, content and size
118 return {
119 96 kind => $kind,
120 size => $size,
121 content => $content,
122 digest => $sha1
123 };
124 }
125  
126 sub all_digests {
127 11 ▶ my ( $self, $kind ) = @_;
Subroutine all_digests: called CC=3 SCAR=14.2
Pod undocumented
128 11 my $store = $self->store;
129 11 ▶ my $re = $kind ? qr/ \Q$kind\E / : qr/ /;
Branch: 1 branch
BranchTrueFalseTotal
$kind ? : 8 3 11
130  
131 # the --batch-all-objects option appeared in v2.6.0-rc0
132 11 ▶ if ( ge_git $store->version, '2.6.0.rc0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ge_git($store->version, '2.6.0.rc0')) { } 11 0 11
133 11 return map +( split / / )[0],
134 grep /$re/,
135 $store->command(qw( cat-file --batch-check --batch-all-objects ));
136 }
137 else { # this won't return unreachable objects
138 0 my ( $pid, $in, $out, $ctx ) =
139 $store->command_bidi_pipe(qw( cat-file --batch-check ));
140  
141 my @digests =
142 map +( split / / )[0], grep /$re/,
143 0 map { print {$out} ( split / / )[0], "\n"; $in->getline }
144 sort $store->command(qw( rev-list --all --objects ));
145 0 $store->command_close_bidi_pipe( $pid, $in, $out, $ctx );
146 0 return @digests;
147 }
148 }
149  
150 # Git::Database::Role::ObjectWriter
151 sub put_object {
152 13 ▶ my ( $self, $object ) = @_;
Subroutine put_object: called CC=1 SCAR=0.0
Pod undocumented
153 13 my ( $pid, $in, $out, $ctx ) =
154 $self->store->command_bidi_pipe( 'hash-object', '-t', $object->kind,
155 '-w', '--stdin' );
156 13 print {$out} $object->content;
157 13 close $out;
158 13 chomp( my $digest = <$in> );
159 13 $self->store->command_close_bidi_pipe( $pid, $in, undef, $ctx ); # $out closed
160 13 return $digest;
161 }
162  
163 # Git::Database::Role::RefReader
164 sub refs {
165 20 ▶ my ($self) = @_;
Subroutine refs: called CC=1 SCAR=0.0
Pod undocumented
166 return {
167 20 reverse map +( split / / ),
168 $self->store->command(qw( show-ref --head ))
169 };
170 }
171  
172 # Git::Database::Role::RefWriter
173 sub put_ref {
174 2 ▶ my ($self, $refname, $digest ) = @_;
Subroutine put_ref: called CC=1 SCAR=0.0
Pod undocumented
175 2 $self->store->command( 'update-ref', $refname, $digest );
176 }
177  
178 sub delete_ref {
179 2 ▶ my ($self, $refname ) = @_;
Subroutine delete_ref: called CC=1 SCAR=0.0
Pod undocumented
180 2 $self->store->command( 'update-ref', '-d', $refname );
181 }
182  
183 sub DEMOLISH {
184 46 ▶ my ( $self, $in_global_destruction ) = @_;
Subroutine DEMOLISH: called CC=4 SCAR=13.9
Pod undocumented
185 46 ▶ return if $in_global_destruction; # why bother?
Branch: 1 branch
BranchTrueFalseTotal
if $in_global_destruction 0 46 46
186  
187 46 ▶ $self->store->command_close_bidi_pipe( @{ $self->object_factory } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_object_factory 37 9 46
188 if $self->has_object_factory;
189 46 ▶ $self->store->command_close_bidi_pipe( @{ $self->object_checker } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_object_checker 11 35 46
190 if $self->has_object_checker;
191 }
192  
193 8 1;
194  
195 __END__
196  
197 =pod
198  
199 =for Pod::Coverage
200 has_object_checker
201 has_object_factory
202 DEMOLISH
203 hash_object
204 get_object_attributes
205 get_object_meta
206 all_digests
207 put_object
208 refs
209 put_ref
210 delete_ref
211  
212 =head1 NAME
213  
214 Git::Database::Backend::Git - A Git::Database backend based on Git
215  
216 =head1 VERSION
217  
218 version 0.012
219  
220 =head1 SYNOPSIS
221  
222 # get a store
223 my $r = Git->new();
224  
225 # let Git::Database produce the backend
226 my $db = Git::Database->new( store => $r );
227  
228 =head1 DESCRIPTION
229  
230 This backend reads and writes data from a Git repository using the
231 L<Git> Git wrapper.
232  
233 =head2 Git Database Roles
234  
235 This backend does the following roles
236 (check their documentation for a list of supported methods):
237 L<Git::Database::Role::Backend>,
238 L<Git::Database::Role::ObjectReader>,
239 L<Git::Database::Role::ObjectWriter>,
240 L<Git::Database::Role::RefReader>,
241 L<Git::Database::Role::RefWriter>.
242  
243 =head1 AUTHOR
244  
245 Philippe Bruhat (BooK) <book@cpan.org>
246  
247 =head1 COPYRIGHT
248  
249 Copyright 2016-2019 Philippe Bruhat (BooK), all rights reserved.
250  
251 =head1 LICENSE
252  
253 This program is free software; you can redistribute it and/or modify it
254 under the same terms as Perl itself.
255  
256 =cut
« Database/Object/Tag.pm ↑ Summary Database/Backend/Git/Wrapper.pm »