GitStore.pm

filter: Statementstmt 99.1% 220 / 222 Branchbran 75.6% 56 / 74 Conditioncond 69.2% 18 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 97.6% 41 / 42 Podpod 75.0% 9 / 12 totaltotal 89.2% 349 / 391 SCAR 40.0
CC
53
Cov
91%
list
9.1
__ANON__
6.6
get_revision
6.1
CRAP 54.8
↑ Summary MooseX/Storage/IO/GitStore.pm »
1 package GitStore;
2 BEGIN {
3 14 ▶ $GitStore::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 #ABSTRACT: Git as versioned data store in Perl
6 14 $GitStore::VERSION = '0.16';
7 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Git::PurePerl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶use Path::Class qw/ dir file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶use List::Util qw/ first /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 ▶use GitStore::Revision;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 14 ▶no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 14 subtype 'PurePerlActor' =>
22 as 'Git::PurePerl::Actor';
23  
24 coerce PurePerlActor
25 => from 'Str'
26 => via {
27 0 ▶ s/<(.*?)>//;
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 Git::PurePerl::Actor->new( name => $_, email => $1 );
29 14 };
30  
31 14 has 'repo' => ( is => 'ro', isa => 'Str', required => 1 );
32  
33 14 has 'branch' => ( is => 'rw', isa => 'Str', default => 'master' );
34  
35 has author => (
36 is => 'rw',
37 isa => 'PurePerlActor',
38 default => sub {
39 16 ▶ Git::PurePerl::Actor->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
40 name => 'anonymous',
41 email => 'anon@127.0.0.1'
42 );
43 14 } );
44  
45 has serializer => (
46 is => 'ro',
47 default => sub {
48 14 ▶ require Storable;
Subroutine __ANON__: called CC=1 SCAR=0.0
49 4 ▶ return sub { return Storable::nfreeze($_[2]); }
Subroutine __ANON__: called CC=1 SCAR=0.0
50 14 },
51 14 );
52  
53 has deserializer => (
54 is => 'ro',
55 default => sub {
56 14 ▶ require Storable;
Subroutine __ANON__: called CC=1 SCAR=0.1
57  
58 return sub {
59 14 ▶ my $data = $_[2];
Subroutine __ANON__: called CC=6 SCAR=18.8
60  
61 14 my $magic = eval { Storable::read_magic($data); };
62  
63 14 ▶ return $data unless $magic && $magic->{major} && $magic->{major} >= 2 && $magic->{major} <= 5;
Branch: 1 branch
BranchTrueFalseTotal
unless $magic and $magic->{"major"} and $magic->{"major"} >= 2 and $magic->{"major"} <= 5 0 3 3
Condition66%6/9
$magic and $magic->{"major"} and $magic->{"major"} >= 2 and $magic->{"major"} <= 5
!ll&&!rl&&r
1103
$magic and $magic->{"major"} and $magic->{"major"} >= 2
!ll&&!rl&&r
1103
$magic and $magic->{"major"}
!ll&&!rl&&r
1103
MC/DC25%1/4
$magic and $magic->{"major"} and $magic->{"major"} >= 2 and $magic->{"major"} <= 5
$magic $magic->{"major"} $magic->{"major"} >= 2 $magic->{"major"} <= 5
Truth table40%2/5
A and B and C and D
A= $magicB= $magic->{"major"}C= $magic->{"major"} >= 2D= $magic->{"major"} <= 5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
64  
65 3 my $thawed = eval { Storable::thaw($data) };
66  
67 # false alarm... looked like a Storable, but wasn't.
68 3 ▶ return $@ ? $data : $thawed;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 3 3
69 }
70 14 },
71 14 );
72  
73 14 has autocommit => (
74 is => 'ro',
75 isa => 'Bool',
76 default => 0,
77 );
78  
79 sub _clean_directories {
80 38 ▶ my ( $self, $dir ) = @_;
Subroutine _clean_directories: called CC=4 SCAR=13.9
81  
82 38 ▶ $dir ||= $self->root;
Condition66%2/3
$dir ||= $self->root
l!l&&r!l&&!r
14240
MC/DC0%0/2
$dir ||= $self->root
$dir $self->root
Truth table66%2/3
A ||= B
A= $dirB= $self->root
ABresult
1X1
011
000
83  
84 38 my $nbr_files = keys %{ $dir->{FILES} };
85  
86 38 for my $d ( keys %{ $dir->{DIRS} } ) {
87 14 ▶ if( my $f = $self->_clean_directories( $dir->{DIRS}{$d} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->_clean_directories($dir->{'DIRS'}{$d})) { } 13 1 14
88 13 $nbr_files += $f;
89 }
90 else {
91 1 delete $dir->{DIRS}{$d};
92 }
93 }
94  
95 38 return $nbr_files;
96 }
97  
98 sub _expand_directories {
99 50 ▶ my( $self, $object ) = @_;
Subroutine _expand_directories: called CC=3 SCAR=11.0
100  
101 50 my %dir = ( DIRS => {}, FILES => {} );
102  
103 50 for my $entry ( map { $_->directory_entries } $object ) {
104 66 ▶ if ( $entry->object->isa( 'Git::PurePerl::Object::Tree' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->object->isa('Git::PurePerl::Object::Tree')) { } 17 49 66
105 17 $dir{DIRS}{$entry->filename}
106 = $self->_expand_directories( $entry->object );
107 }
108 else {
109 49 $dir{FILES}{$entry->filename} = $entry->sha1;
110 }
111 }
112  
113 50 return \%dir;
114 }
115  
116 16 ▶has 'root' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
117  
118 has 'git' => (
119 is => 'ro',
120 isa => 'Git::PurePerl',
121 lazy => 1,
122 default => sub {
123 16 ▶ my $repo = $_[0]->repo;
Subroutine __ANON__: called CC=2 SCAR=7.2
124 16 ▶ return Git::PurePerl->new(
Branch: 1 branch
BranchTrueFalseTotal
$repo =~ /\.git$/ ? : 0 16 16
125 ( $repo =~ m/\.git$/ ? 'gitdir' : 'directory') => $repo
126 );
127 }
128 14 );
129  
130 sub BUILD {
131 16 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
132
133 16 $self->load();
134
135 }
136  
137 sub BUILDARGS {
138 16 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod documented
139  
140 16 ▶ if ( @_ == 1 && ! ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 9 7 16
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
709
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
141 9 return { repo => $_[0] };
142 } else {
143 7 return $class->SUPER::BUILDARGS(@_);
144 }
145 }
146  
147 sub branch_head {
148 47 ▶ my ( $self, $branch ) = @_;
Subroutine branch_head: called CC=2 SCAR=7.6
Pod undocumented
149 47 ▶ $branch ||= $self->branch;
Condition33%1/3
$branch ||= $self->branch
l!l&&r!l&&!r
0470
MC/DC0%0/2
$branch ||= $self->branch
$branch $self->branch
Truth table33%1/3
A ||= B
A= $branchB= $self->branch
ABresult
1X1
011
000
150  
151 47 return $self->git->ref_sha1('refs/heads/' . $branch);
152 }
153  
154 sub create {
155 1 ▶ my( $class, $dir ) = @_;
Subroutine create: called CC=1 SCAR=0.0
Pod documented
156  
157 1 path($dir)->mkpath;
158 1 Git::PurePerl->init( directory => $dir );
159  
160 1 return $class->new($dir);
161 }
162  
163 # Load the current head version from repository.
164 sub load {
165 41 ▶ my $self = shift;
Subroutine load: called CC=2 SCAR=6.9
Pod undocumented
166
167 41 ▶ my $head = $self->branch_head or do {
Branch: 1 branch
BranchTrueFalseTotal
my $head = $self->branch_head or do { $self->root({"DIRS", {}, "FILES", {}}); return } 10 31 41
168 10 $self->root({ DIRS => {}, FILES => {} });
169 10 return;
170 };
171  
172 31 my $commit = $self->git->get_object($head);
173 31 my $tree = $commit->tree;
174  
175 31 my $root = $self->_expand_directories( $tree );
176 31 $self->root($root);
177  
178 }
179  
180 sub _normalize_path {
181 60 ▶ my ( $self, $path ) = @_;
Subroutine _normalize_path: called CC=2 SCAR=6.9
182  
183 60 ▶ $path = join '/', @$path if ref $path eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $path eq "ARRAY" 1 59 60
184  
185 # Git doesn't like paths prefixed with a '/'
186 60 $path =~ s#^/+##;
187  
188 60 return $path;
189 }
190  
191 sub get_revision {
192 1 ▶ my ( $self, $path ) = @_;
Subroutine get_revision: called CC=6 SCAR=18.2
Pod documented
193  
194 1 $path = file( $self->_normalize_path($path) );
195  
196 1 ▶ my $head = $self->branch_head
Branch: 1 branch
BranchTrueFalseTotal
my $head = $self->branch_head or return 0 1 1
197 or return;
198  
199 1 my $commit = $self->git->get_object($head);
200 1 my @q = ( $commit );
201  
202 1 ▶ my $file = $self->_find_file( $commit->tree, $path )
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_find_file($commit->tree, $path) or return 0 1 1
203 or return;
204  
205 1 my $latest_file_sha1 = $file->object->sha1;
206 1 my $last_commit;
207  
208 1 ▶ while ( @q ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@q) 1 1 2
209 1 push @q, $q[0]->parents;
210 1 $last_commit = $commit;
211 1 $commit = shift @q;
212  
213 1 ▶ my $f = $self->_find_file( $commit->tree, file($path) )
Branch: 1 branch
BranchTrueFalseTotal
my $f = $self->_find_file($commit->tree, file($path)) or last 0 1 1
214 or last;
215  
216 1 ▶ last if $f->object->sha1 ne $latest_file_sha1;
Branch: 1 branch
BranchTrueFalseTotal
if $f->object->sha1 ne $latest_file_sha1 0 1 1
217 }
218  
219 1 return GitStore::Revision->new(
220 gitstore => $self,
221 path => $path,
222 sha1 => $last_commit->sha1,
223 );
224 }
225  
226 sub get {
227 18 ▶ my ( $self, $path ) = @_;
Subroutine get: called CC=4 SCAR=14.0
Pod documented
228
229 18 $path = file( $self->_normalize_path($path) );
230  
231 18 ▶ my $dir = $self->_cd_dir($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_cd_dir($path) or return 0 18 18
232  
233 18 ▶ my $sha1 = $dir->{FILES}{$path->basename} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sha1 = $dir->{"FILES"}{$path->basename} or return 8 10 18
234  
235 10 ▶ my $object = $self->git->get_object($sha1) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $object = $self->git->get_object($sha1) or return 0 10 10
236  
237 10 return $self->deserializer->($self,$path,$object->content);
238 }
239  
240 sub exist {
241 3 ▶ my ( $self, $path ) = @_;
Subroutine exist: called CC=2 SCAR=7.0
Pod documented
242  
243 3 $path = file( $self->_normalize_path($path) );
244  
245 3 ▶ my $dir = $self->_cd_dir($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_cd_dir($path) or return 0 3 3
246  
247 3 return $dir->{FILES}{$path->basename};
248 }
249  
250  
251 sub set {
252 31 ▶ my ( $self, $path, $content ) = @_;
Subroutine set: called CC=3 SCAR=11.0
253
254 31 $path = file( $self->_normalize_path($path) );
255  
256 31 ▶ my $dir = $self->_cd_dir($path,1) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_cd_dir($path, 1) or return 0 31 31
257  
258 31 ▶ $content = $self->serializer->( $self, $path, $content ) if ref $content;
Branch: 1 branch
BranchTrueFalseTotal
if ref $content 5 26 31
259  
260 31 my $blob = Git::PurePerl::NewObject::Blob->new( content => $content );
261 31 $self->git->put_object($blob);
262  
263 31 return $dir->{FILES}{$path->basename} = $blob->sha1;
264 }
265  
266 after [ 'set', 'delete' ] => sub {
267 38 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
268 38 ▶ $self->commit if $self->autocommit;
Branch: 1 branch
BranchTrueFalseTotal
if $self->autocommit 3 35 38
269 14 };
270  
271 14 *remove = \&delete;
272 sub delete {
273 7 ▶ my ( $self, $path ) = @_;
Subroutine delete: called CC=2 SCAR=7.0
274
275 7 $path = file( $self->_normalize_path($path) );
276  
277 7 ▶ my $dir = $self->_cd_dir($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_cd_dir($path) or return 0 7 7
278  
279 7 return delete $dir->{FILES}{$path->basename};
280 }
281  
282 sub _cd_dir {
283 59 ▶ my( $self, $path, $create ) = @_;
Subroutine _cd_dir: called CC=4 SCAR=13.9
284  
285 59 my $dir = $self->root;
286  
287 59 for ( grep { !/^\.$/ } $path->dir->dir_list ) {
288 17 ▶ if ( $dir->{DIRS}{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->{'DIRS'}{$_}) { } 11 6 17
289 11 $dir = $dir->{DIRS}{$_};
290 }
291 else {
292 6 ▶ return unless $create;
Branch: 1 branch
BranchTrueFalseTotal
unless $create 0 6 6
293 6 $dir = $dir->{DIRS}{$_} = { DIRS => {}, FILES => {} };
294 }
295 }
296  
297 59 return $dir;
298 }
299  
300 sub _build_new_directory_entry {
301 39 ▶ my( $self, $dir ) = @_;
Subroutine _build_new_directory_entry: called CC=3 SCAR=11.0
302  
303 39 my @children;
304
305 39 ▶ while ( my( $filename, $sha1 ) = each %{ $dir->{FILES} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$dir{"FILES"};}) 35 39 74
306 35 push @children,
307 Git::PurePerl::NewDirectoryEntry->new(
308 mode => '100644',
309 filename => $filename,
310 sha1 => $sha1,
311 );
312 }
313  
314 39 ▶ while ( my( $dirname, $dir ) = each %{ $dir->{DIRS} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$dir{"DIRS"};}) 15 39 54
315 15 my $tree = $self->_build_new_directory_entry($dir);
316 15 push @children, Git::PurePerl::NewDirectoryEntry->new(
317 mode => '040000',
318 filename => $dirname,
319 sha1 => $tree->sha1,
320 );
321 }
322  
323 39 my $tree = Git::PurePerl::NewObject::Tree->new(
324 directory_entries => \@children,
325 );
326 39 $self->git->put_object($tree);
327  
328 39 return $tree;
329 }
330  
331 sub commit {
332 24 ▶ my ( $self, $message ) = @_;
Subroutine commit: called CC=3 SCAR=11.0
Pod documented
333  
334 24 ▶ unless ( $self->_clean_directories ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_clean_directories) 2 22 24
335 # TODO surely there's a better way?
336 2 $self->set( '.gitignore/dummy', 'dummy file to keep git happy' );
337 }
338
339 # TODO only commit if there were changes
340
341 24 my $tree = $self->_build_new_directory_entry( $self->root );
342  
343 # there might not be a parent, if it's a new branch
344 24 my $parent = eval { $self->git->ref( 'refs/heads/'.$self->branch )->sha1 };
345  
346 24 my $timestamp = DateTime->now;
347 24 ▶ my $commit = Git::PurePerl::NewObject::Commit->new(
Condition100%2/2
$message || ''
l!l
1410
MC/DC100%1/1
$message || ''
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
348 ( parent => $parent ) x !!$parent,
349 tree => $tree->sha1,
350 author => $self->author,
351 committer => $self->author,
352 comment => $message||'',
353 authored_time => $timestamp,
354 committed_time => $timestamp,
355 );
356 24 $self->git->put_object($commit);
357  
358 # reload
359 24 $self->load;
360 }
361  
362 sub discard {
363 1 ▶ my $self = shift;
Subroutine discard: called CC=1 SCAR=0.0
Pod documented
364  
365 1 $self->load;
366 }
367  
368 sub _find_file {
369 42 ▶ my( $self, $tree, $path ) = @_;
Subroutine _find_file: called CC=4 SCAR=13.9
370  
371 42 my @path = grep { !/^\.$/ } $path->dir->dir_list;
372  
373 42 ▶ if ( my $part = shift @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = shift @path) 18 24 42
374 18 ▶ my $entry = first { $_->filename eq $part } $tree->directory_entries
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
my $entry = &first(sub { $_->filename eq $part; } , $tree->directory_entries) or return 0 18 18
375 or return;
376  
377 18 my $object = $self->git->get_object( $entry->sha1 );
378  
379 18 ▶ return unless ref $object eq 'Git::PurePerl::Object::Tree';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object eq "Git::PurePerl::Object::Tree" 0 18 18
380  
381 18 return $self->_find_file( $object, file(@path,$path->basename) );
382 }
383  
384 24 ▶ return first { $_->filename eq $path->basename } $tree->directory_entries;
Subroutine __ANON__: called CC=1 SCAR=0.0
385 }
386  
387 sub history {
388 3 ▶ my ( $self, $path ) = @_;
Subroutine history: called CC=6 SCAR=17.9
Pod documented
389  
390 3 ▶ my $head = $self->branch_head
Branch: 1 branch
BranchTrueFalseTotal
my $head = $self->branch_head or return 0 3 3
391 or return;
392  
393 3 my @q = ( $self->git->get_object($head) );
394  
395 3 my @commits;
396 3 ▶ while ( @q ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@q) 16 3 19
397 16 push @q, $q[0]->parents;
398 16 unshift @commits, shift @q;
399 }
400  
401 3 my @history_commits;
402 my %sha1_seen;
403  
404 3 for my $c ( @commits ) {
405 16 ▶ my $file = $self->_find_file( $c->tree, file($path) ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->_find_file($c->tree, file($path)) or next 2 14 16
406 14 ▶ push @history_commits, $c unless $sha1_seen{ $file->object->sha1 }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $sha1_seen{$file->object->sha1}++ 6 8 14
407 }
408  
409 return map {
410 3 GitStore::Revision->new(
411 path => $path,
412 gitstore => $self,
413 sha1 => $_->sha1,
414 )
415 } @history_commits;
416  
417 }
418  
419 sub list {
420 2 ▶ my( $self, $regex ) = @_;
Subroutine list: called CC=9 SCAR=22.0
Pod documented
421  
422 2 ▶ croak "'$regex' is not a a regex"
Branch: 1 branch
BranchTrueFalseTotal
if $regex and ref $regex ne "Regexp" 0 2 2
Condition66%2/3
$regex and ref $regex ne "Regexp"
!ll&&!rl&&r
110
MC/DC0%0/2
$regex and ref $regex ne "Regexp"
$regex ref $regex ne "Regexp"
Truth table66%2/3
A and B
A= $regexB= ref $regex ne "Regexp"
ABresult
0X0
100
111
423 if $regex and ref $regex ne 'Regexp';
424  
425 2 ▶ my $head = $self->branch_head or return;
Branch: 1 branch
BranchTrueFalseTotal
my $head = $self->branch_head or return 0 2 2
426  
427 2 my $commit = $self->git->get_object($head);
428 2 my $tree = $commit->tree;
429  
430 2 my $root = $self->_expand_directories( $tree );
431  
432 2 my @dirs = ( [ '', $root ] );
433 2 my @entries;
434  
435 2 ▶ while( my $dir = shift @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = shift @dirs) 2 2 4
436 2 my $path = $dir->[0];
437 2 $dir = $dir->[1];
438 push @dirs, [ "$path/$_" => $dir->{DIRS}{$_} ]
439 2 for sort keys %{$dir->{DIRS}};
440  
441 2 for ( sort keys %{$dir->{FILES}} ) {
442 4 my $f = "$path/$_";
443 4 $f =~ s#^/##; # TODO improve this
444 4 ▶ next if $regex and $f !~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
if $regex and not $f =~ /$regex/ 1 3 4
Condition100%3/3
$regex and not $f =~ /$regex/
!ll&&!rl&&r
211
MC/DC100%2/2
$regex and not $f =~ /$regex/
$regex not $f =~ /$regex/
Truth table100%3/3
A and not(B)
A= $regexB= not $f =~ /$regex/
ABresult
0X0
100
111
445 3 push @entries, $f;
446 }
447 }
448  
449 2 return @entries;
450 }
451  
452  
453 14 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
454 14 __PACKAGE__->meta->make_immutable;
455  
456 14 1;
457  
458 __END__
459  
460 =pod
461  
462 =encoding UTF-8
463  
464 =head1 NAME
465  
466 GitStore - Git as versioned data store in Perl
467  
468 =head1 VERSION
469  
470 version 0.16
471  
472 =head1 SYNOPSIS
473  
474 use GitStore;
475  
476 my $gs = GitStore->new('/path/to/repo');
477 $gs->set( 'users/obj.txt', $obj );
478 $gs->set( ['config', 'wiki.txt'], { hash_ref => 1 } );
479 $gs->commit();
480 $gs->set( 'yyy/xxx.log', 'Log me' );
481 $gs->discard();
482
483 # later or in another pl
484 my $val = $gs->get( 'user/obj.txt' ); # $val is the same as $obj
485 my $val = $gs->get( 'config/wiki.txt' ); # $val is { hashref => 1 } );
486 my $val = $gs->get( ['yyy', 'xxx.log' ] ); # $val is undef since discard
487  
488 =head1 DESCRIPTION
489  
490 It is inspired by the Python and Ruby binding. check SEE ALSO
491  
492 =head1 CLASS FUNCTIONS
493  
494 =head2 create( $directory )
495  
496 Creates the directory, initialize it as a git repository,
497 and returns its associated C<GitStore> object.
498  
499 my $gs = GitStore->create( '/tmp/mystore' );
500  
501 =head1 METHODS
502  
503 =head2 new
504  
505 GitStore->new('/path/to/repo');
506 GitStore->new( repo => '/path/to/repo', branch => 'mybranch' );
507 GitStore->new( repo => '/path/to/repo', author => 'Someone Unknown <unknown\@what.com>' );
508  
509 =over 4
510  
511 =item repo
512  
513 your git directory or work directory (I<GitStore> will assume it's a work
514 directory if it doesn't end with C<.git>).
515  
516 =item branch
517  
518 your branch name, default is 'master'
519  
520 =item author
521  
522 It is used in the commit info
523  
524 =item serializer
525  
526 Can be used to define a serializing function that will be used if the value to
527 save is a reference. When invoked, the function will be passed a reference to
528 the store object, the path under which the value will be saved, and the value
529 itself. For example, one could do different serialization via:
530  
531 my $store = GitStore->new(
532 repo => '/path/to/repo',
533 serializer => sub {
534 my( $store, $path, $value ) = @_;
535  
536 if ( $path =~ m#^json# ) {
537 return encode_json($value);
538 }
539 else {
540 # defaults to YAML
541 return YAML::Dump($value);
542 }
543 },
544 );
545  
546 The default serializer uses L<Storable/nfreeze>.
547  
548 =item deserializer
549  
550 Called when a value is picked from the store to be (potentially) deserialized.
551 Just like the serializer function, it is passed three arguments: the store
552 object, the path of the value to deserialize and the value itself. To revisit
553 the example for C<serializer>, the full serializer/deserializer dance would
554 be:
555  
556 my $store = GitStore->new(
557 repo => '/path/to/repo',
558 serializer => sub {
559 my( $store, $path, $value ) = @_;
560  
561 return $path =~ m#^json#
562 ? encode_json($value)
563 : YAML::Dump($value)
564 ;
565 },
566 deserializer => sub {
567 my( $store, $path, $value ) = @_;
568
569 return $path =~ #^json#
570 ?decode_json($value)
571 : YAML::Load($value)
572 ;
573 },
574 );
575  
576 The default deserializer will try to deserialize the value
577 retrieved from the store via L<Storable/thaw> and, if this fails,
578 return the value verbatim.
579  
580 =item autocommit
581  
582 If set to C<true>, any call to C<set()> or C<delete()> will automatically call an
583 implicit C<commit()>. Defaults to C<false>.
584  
585 =back
586  
587 =head2 set($path, $val)
588  
589 $gs->set( 'yyy/xxx.log', 'Log me' );
590 $gs->set( ['config', 'wiki.txt'], { hash_ref => 1 } );
591 $gs->set( 'users/obj.txt', $obj );
592  
593 Store $val as a $path file in Git
594  
595 $path can be String or ArrayRef. Any leading slashes ('/') in the path
596 will be stripped, as to make it a valid Git path. The same
597 grooming is done for the C<get()> and C<delete()> methods.
598  
599 $val can be String or Ref[HashRef|ArrayRef|Ref[Ref]] or blessed Object
600  
601 =head2 get($path)
602  
603 $gs->get( 'user/obj.txt' );
604 $gs->get( ['yyy', 'xxx.log' ] );
605  
606 Get $val from the $path file
607  
608 $path can be String or ArrayRef
609  
610 =head2 get_revision( $path )
611  
612 Like C<get()>, but returns the L<GitStore::Revision> object corresponding to
613 the latest Git revision on the monitored branch for which C<$path> changed.
614  
615 =head2 delete($path)
616  
617 =head2 remove($path)
618  
619 remove $path from Git store
620  
621 =head2 commit
622  
623 $gs->commit();
624 $gs->commit('Your Comments Here');
625  
626 commit the B<set> changes into Git
627  
628 =head2 discard
629  
630 $gs->discard();
631  
632 discard the B<set> changes
633  
634 =head2 exist($path)
635  
636 Returns I<true> if an object exists at the given path.
637  
638 =head2 history($path)
639  
640 Returns a list of L<GitStore::Revision> objects representing the changes
641 brought to the I<$path>. The changes are returned in ascending commit order.
642  
643 =head2 list($regex)
644  
645 @entries = $gs->list( qr/\.txt$/ );
646  
647 Returns a list of all entries in the repository, possibly filtered by the
648 optional I<$regex>.
649  
650 =head1 FAQ
651  
652 =head2 why the files are B<not> there?
653  
654 run
655  
656 git checkout
657  
658 =head2 any example?
659  
660 # if you just need a local repo, that's all you need.
661 mkdir sandbox
662 cd sandbox
663 git init
664 # use GitStore->new('/path/to/this/sandbox')
665 # set something
666 git checkout
667
668 # follows are for remote git url
669 git remote add origin git@github.com:fayland/sandbox2.git
670 git push origin master
671 # do more GitStore->new('/path/to/this/sandbox') later
672 git checkout
673 git pull origin master
674 git push
675  
676 =head1 KNOWN BUGS
677  
678 If all files are deleted from the repository, a 'dummy' file
679 will be created to keep Git happy.
680  
681 =head1 SEE ALSO
682  
683 =over 4
684  
685 =item Article
686  
687 L<http://www.newartisans.com/2008/05/using-git-as-a-versioned-data-store-in-python.html>
688  
689 =item Python binding
690  
691 L<http://github.com/jwiegley/git-issues/tree/master>
692  
693 =item Ruby binding
694  
695 L<http://github.com/georgi/git_store/tree/master>
696  
697 =back
698  
699 =head1 Git URL
700  
701 L<http://github.com/fayland/perl-git-store/tree/master>
702  
703 =head1 AUTHORS
704  
705 =over 4
706  
707 =item *
708  
709 Fayland Lam <fayland@gmail.com>
710  
711 =item *
712  
713 Yanick Champoux <yanick@cpan.org>
714  
715 =back
716  
717 =head1 COPYRIGHT AND LICENSE
718  
719 This software is copyright (c) 2015 by Fayland Lam <fayland@gmail.com>.
720  
721 This is free software; you can redistribute it and/or modify it under
722 the same terms as the Perl 5 programming language system itself.
723  
724 =cut
↑ Summary MooseX/Storage/IO/GitStore.pm »