Database/Backend/Git/Wrapper.pm

filter: Statementstmt 92.0% 58 / 63 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 8 totaltotal 81.7% 85 / 104 SCAR 23.2
CC
10
Cov
88%
all_digests
5.3
get_object_attributes
5.0
get_object_meta
3.0
CRAP 10.2
« Database/Backend/Git.pm ↑ Summary Database/Backend/Git/Sub.pm »
1 package Git::Database::Backend::Git::Wrapper;
2 8 $Git::Database::Backend::Git::Wrapper::VERSION = '0.012';
3 8 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Git::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Git::Version::Compare qw( ge_git );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 with
12 'Git::Database::Role::Backend',
13 'Git::Database::Role::ObjectReader',
14 'Git::Database::Role::ObjectWriter',
15 'Git::Database::Role::RefReader',
16 'Git::Database::Role::RefWriter',
17 ;
18  
19 has '+store' => (
20 isa => quote_sub( q{
21 die 'store is not a Git::Wrapper object'
22 if !eval { $_[0]->isa('Git::Wrapper') }
23 } ),
24 0 ▶ default => sub { Git::Wrapper->new( cwd() ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 8 );
26  
27 # Git::Database::Role::Backend
28 sub hash_object {
29 95 ▶ my ( $self, $object ) = @_;
Subroutine hash_object: called CC=1 SCAR=0.0
Pod undocumented
30 95 my @out = $self->store->hash_object( { -STDIN => $object->content },
31 '--stdin', '-t', $object->kind );
32 95 return shift @out;
33 }
34  
35 # Git::Database::Role::ObjectReader
36 sub get_object_meta {
37 57 ▶ my ( $self, $digest ) = @_;
Subroutine get_object_meta: called CC=3 SCAR=11.1
Pod undocumented
38 57 my ($meta) =
39 $self->store->cat_file( { -STDIN => "$digest\n" }, '--batch-check' );
40 57 ▶ warn join $/, @{ $self->store->ERR }, '' if @{ $self->store->ERR };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->store->ERR;} 0 57 57
41  
42 # protect against weird cases like if $digest contains a space
43 57 my @parts = split / /, $meta;
44 57 ▶ return ( $digest, 'missing', undef ) if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 33 24 57
45  
46 24 my ( $kind, $size ) = splice @parts, -2;
47 24 return join( ' ', @parts ), $kind, $size;
48 }
49  
50 sub get_object_attributes {
51 156 ▶ my ( $self, $digest ) = @_;
Subroutine get_object_attributes: called CC=5 SCAR=16.1
Pod undocumented
52  
53 156 my @out = $self->store->cat_file( { -STDIN => "$digest\n" }, '--batch' );
54 156 my $meta = shift @out;
55 156 ▶ warn join $/, @{ $self->store->ERR }, '' if @{ $self->store->ERR };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->store->ERR;} 1 155 156
56  
57 # protect against weird cases like if $digest contains a space
58 156 my ( $sha1, $kind, $size ) = my @parts = split / /, $meta;
59  
60 # git versions >= 2.11.0.rc0 throw more verbose errors
61 156 ▶ return undef if $parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] =~ /^(?:symlink|dangling|loop|notdir)$/ 0 156 156
62  
63 # git versions >= 2.21.0.rc0 explicitely say if a sha1 is ambiguous
64 156 ▶ return undef if $kind eq 'ambiguous';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "ambiguous" 1 155 156
65  
66 # object does not exist in the git object database
67 155 ▶ return undef if $parts[-1] eq 'missing';
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] eq "missing" 59 96 155
68  
69 return {
70 96 kind => $kind,
71 size => $size,
72 content => join( $/, @out ), # I expect this to break on binary data
73 digest => $sha1
74 };
75 }
76  
77 sub all_digests {
78 11 ▶ my ( $self, $kind ) = @_;
Subroutine all_digests: called CC=4 SCAR=16.6
Pod undocumented
79 11 my $store = $self->store;
80 11 ▶ my $re = $kind ? qr/ \Q$kind\E / : qr/ /;
Branch: 1 branch
BranchTrueFalseTotal
$kind ? : 8 3 11
81  
82 # the --batch-all-objects option appeared in v2.6.0-rc0
83 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
84 11 return map +( split / / )[0],
85 grep /$re/,
86 $store->cat_file(qw( --batch-check --batch-all-objects ));
87 }
88 else { # this won't return unreachable objects
89 0 my $revs = join "\n", map +( split / / )[0],
90 sort $store->rev_list(qw( --all --objects ));
91 0 ▶ return if !length $revs;
Branch: 1 branch
BranchTrueFalseTotal
unless length $revs 0 0 0
92 0 return map +( split / / )[0], grep /$re/,
93 $store->cat_file( qw( --batch-check ), { -STDIN => "$revs\n" } );
94 }
95 }
96  
97 # Git::Database::Role::ObjectWriter
98 sub put_object {
99 13 ▶ my ( $self, $object ) = @_;
Subroutine put_object: called CC=1 SCAR=0.0
Pod undocumented
100 13 my ($hash) = $self->store->hash_object( '-t', $object->kind, '-w',
101 { stdin => 1, -STDIN => $object->content } );
102 13 return $hash;
103 }
104  
105 # Git::Database::Role::RefReader
106 sub refs {
107 20 ▶ my ($self) = @_;
Subroutine refs: called CC=1 SCAR=0.0
Pod undocumented
108 return {
109 20 reverse map +( split / / ),
110 $self->store->show_ref( { head => 1 } )
111 };
112 }
113  
114 # Git::Database::Role::RefWriter
115 sub put_ref {
116 2 ▶ my ($self, $refname, $digest ) = @_;
Subroutine put_ref: called CC=1 SCAR=0.0
Pod undocumented
117 2 $self->store->update_ref( $refname, $digest );
118 }
119  
120 sub delete_ref {
121 2 ▶ my ($self, $refname ) = @_;
Subroutine delete_ref: called CC=1 SCAR=0.0
Pod undocumented
122 2 $self->store->update_ref( '-d', $refname );
123 }
124  
125 8 1;
126  
127 __END__
128  
129 =pod
130  
131 =for Pod::Coverage
132 hash_object
133 get_object_attributes
134 get_object_meta
135 all_digests
136 put_object
137 refs
138 put_ref
139 delete_ref
140  
141 =head1 NAME
142  
143 Git::Database::Backend::Git::Wrapper - A Git::Database backend based on Git::Wrapper
144  
145 =head1 VERSION
146  
147 version 0.012
148  
149 =head1 SYNOPSIS
150  
151 # get a store
152 my $r = Git::Wrapper->new('/var/foo');
153  
154 # let Git::Database produce the backend
155 my $db = Git::Database->new( store => $r );
156  
157 =head1 DESCRIPTION
158  
159 This backend reads and writes data from a Git repository using the
160 L<Git::Wrapper> module.
161  
162 =head2 Git Database Roles
163  
164 This backend does the following roles
165 (check their documentation for a list of supported methods):
166 L<Git::Database::Role::Backend>,
167 L<Git::Database::Role::ObjectReader>,
168 L<Git::Database::Role::ObjectWriter>,
169 L<Git::Database::Role::RefReader>,
170 L<Git::Database::Role::RefWriter>.
171  
172 =head1 AUTHORS
173  
174 Philippe Bruhat (BooK) <book@cpan.org>
175  
176 Sergey Romanov provided the code to support the
177 L<Git::Database::Role::ObjectWriter>, L<Git::Database::Role::RefReader>,
178 and L<Git::Database::Role::RefWriter> roles.
179  
180 =head1 COPYRIGHT
181  
182 Copyright 2016-2019 Philippe Bruhat (BooK), all rights reserved.
183  
184 =head1 LICENSE
185  
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
188  
189 =cut
« Database/Backend/Git.pm ↑ Summary Database/Backend/Git/Sub.pm »