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