PurePerl.pm

filter: Statementstmt 91.6% 295 / 322 Branchbran 61.8% 47 / 76 Conditioncond 37.5% 18 / 48 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 97.0% 66 / 68 Podpod 30.4% 7 / 23 totaltotal 76.5% 433 / 566 SCAR 45.4
CC
64
Cov
81%
ref_sha1
28.6
checkout
12.5
ref_names
7.0
CRAP 93.4
« PurePerl/Protocol.pm ↑ Summary PurePerl/Pack.pm »
1 package Git::PurePerl;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Compress::Zlib qw(uncompress);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Data::Stream::Bulk;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::Stream::Bulk::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Stream::Bulk::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Git::PurePerl::Actor;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Git::PurePerl::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Git::PurePerl::DirectoryEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Git::PurePerl::Loose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Git::PurePerl::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Git::PurePerl::NewDirectoryEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Git::PurePerl::NewObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Git::PurePerl::NewObject::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Git::PurePerl::NewObject::Commit;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Git::PurePerl::NewObject::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Git::PurePerl::NewObject::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Git::PurePerl::Object::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Git::PurePerl::Object::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Git::PurePerl::Object::Commit;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Git::PurePerl::Object::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Git::PurePerl::Object::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Git::PurePerl::Pack;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Git::PurePerl::Pack::WithIndex;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use Git::PurePerl::Pack::WithoutIndex;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Git::PurePerl::PackIndex;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Git::PurePerl::PackIndex::Version1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use Git::PurePerl::PackIndex::Version2;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Git::PurePerl::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use IO::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 4 our $VERSION = '0.52';
41 4 $VERSION = eval $VERSION;
42  
43 4 has 'directory' => (
44 is => 'ro',
45 isa => 'Path::Class::Dir',
46 required => 0,
47 coerce => 1
48 );
49  
50 4 has 'gitdir' => (
51 is => 'ro',
52 isa => 'Path::Class::Dir',
53 required => 1,
54 coerce => 1
55 );
56  
57 4 has 'loose' => (
58 is => 'rw',
59 isa => 'Git::PurePerl::Loose',
60 required => 0,
61 lazy_build => 1,
62 );
63  
64 4 has 'packs' => (
65 is => 'rw',
66 isa => 'ArrayRef[Git::PurePerl::Pack]',
67 required => 0,
68 auto_deref => 1,
69 lazy_build => 1,
70 );
71  
72 has 'description' => (
73 is => 'rw',
74 isa => 'Str',
75 lazy => 1,
76 default => sub {
77 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
78 2 file( $self->gitdir, 'description' )->slurp( chomp => 1 );
79 }
80 4 );
81  
82 has 'config' => (
83 is => 'ro',
84 isa => 'Git::PurePerl::Config',
85 lazy => 1,
86 default => sub {
87 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
88 8 Git::PurePerl::Config->new(git => $self);
89 }
90 4 );
91  
92 4 __PACKAGE__->meta->make_immutable;
93  
94 sub BUILDARGS {
95 9 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod documented
96 9 my $params = $class->SUPER::BUILDARGS(@_);
97  
98 9 ▶ $params->{'gitdir'} ||= dir( $params->{'directory'}, '.git' );
Condition66%2/3
$params->{"gitdir"} ||= dir($params->{"directory"}, ".git")
l!l&&r!l&&!r
450
MC/DC0%0/2
$params->{"gitdir"} ||= dir($params->{"directory"}, ".git")
$params->{"gitdir"} dir($params->{"directory"}, ".git")
Truth table66%2/3
A ||= B
A= $params->{"gitdir"}B= dir($params->{"directory"}, ".git")
ABresult
1X1
011
000
99 9 return $params;
100 }
101  
102 sub BUILD {
103 9 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=16.4
Pod undocumented
104  
105 9 ▶ unless ( -d $self->gitdir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->gitdir) 0 9 9
106 0 confess $self->gitdir . ' is not a directory';
107 }
108 9 ▶ unless ( not defined $self->directory or -d $self->directory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $self->directory or -d $self->directory) 0 9 9
Condition66%2/3
not defined $self->directory or -d $self->directory
l!l&&r!l&&!r
270
MC/DC0%0/2
not defined $self->directory or -d $self->directory
not defined $self->directory -d $self->directory
Truth table66%2/3
not(A) or B
A= not defined $self->directoryB= -d $self->directory
ABresult
1X1
011
000
109 0 confess $self->directory . ' is not a directory';
110 }
111 }
112  
113 sub _build_loose {
114 8 ▶ my $self = shift;
Subroutine _build_loose: called CC=1 SCAR=0.0
115 8 my $loose_dir = dir( $self->gitdir, 'objects' );
116 8 return Git::PurePerl::Loose->new( directory => $loose_dir );
117 }
118  
119 sub _build_packs {
120 8 ▶ my $self = shift;
Subroutine _build_packs: called CC=3 SCAR=11.0
121 8 my $pack_dir = dir( $self->gitdir, 'objects', 'pack' );
122 8 my @packs;
123 8 foreach my $filename ( $pack_dir->children ) {
124 4 ▶ next unless $filename =~ /\.pack$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /\.pack$/ 2 2 4
125 2 push @packs,
126 Git::PurePerl::Pack::WithIndex->new( filename => $filename );
127 }
128 8 return \@packs;
129 }
130  
131 sub _ref_names_recursive {
132 22 ▶ my ( $dir, $base, $names ) = @_;
Subroutine _ref_names_recursive: called CC=3 SCAR=13.3
133  
134 22 foreach my $file ( $dir->children ) {
135 5 ▶ if ( -d $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) { } 0 5 5
136 0 my $reldir = $file->relative($dir);
137 0 my $subbase = $base . $reldir . "/";
138 0 _ref_names_recursive( $file, $subbase, $names );
139 } else {
140 5 push @$names, $base . $file->basename;
141 }
142 }
143 }
144  
145 sub ref_names {
146 11 ▶ my $self = shift;
Subroutine ref_names: called CC=7 SCAR=19.5
Pod undocumented
147 11 my @names;
148 11 foreach my $type (qw(heads remotes tags)) {
149 33 my $dir = dir( $self->gitdir, 'refs', $type );
150 33 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 11 22 33
151 22 my $base = "refs/$type/";
152 22 _ref_names_recursive( $dir, $base, \@names );
153 }
154 11 my $packed_refs = file( $self->gitdir, 'packed-refs' );
155 11 ▶ if ( -f $packed_refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $packed_refs) 6 5 11
156 6 foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
157 12 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 6 6 12
158 6 ▶ next if $line =~ /^\^/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\^/ 0 6 6
159 6 my ( $sha1, $name ) = split ' ', $line;
160 6 push @names, $name;
161 }
162 }
163 11 return @names;
164 }
165  
166 sub refs_sha1 {
167 3 ▶ my $self = shift;
Subroutine refs_sha1: called CC=1 SCAR=0.0
Pod undocumented
168 3 return map { $self->ref_sha1($_) } $self->ref_names;
169 }
170  
171 sub refs {
172 3 ▶ my $self = shift;
Subroutine refs: called CC=1 SCAR=0.0
Pod undocumented
173 3 return map { $self->ref($_) } $self->ref_names;
174 }
175  
176 sub ref_sha1 {
177 30 ▶ my ( $self, $wantref ) = @_;
Subroutine ref_sha1: called CC=15 SCAR=33.5
Pod undocumented
178 30 my $dir = dir( $self->gitdir, 'refs' );
179 30 ▶ return unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 30 30
180  
181 30 ▶ if ($wantref eq "HEAD") {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantref eq "HEAD") 0 30 30
182 0 my $file = file($self->gitdir, 'HEAD');
183 0 ▶ my $sha1 = file($file)->slurp
Condition0%0/3
file($file)->slurp || confess("Error reading ${file}: $!")
l!l&&r!l&&!r
000
MC/DC0%0/2
file($file)->slurp || confess("Error reading ${file}: $!")
file($file)->slurp confess("Error reading ${file}: $!")
Truth table0%0/3
A || B
A= file($file)->slurpB= confess("Error reading ${file}: $!")
ABresult
1X1
011
000
184 || confess("Error reading $file: $!");
185 0 chomp $sha1;
186 0 return _ensure_sha1_is_sha1( $self, $sha1 );
187 }
188  
189 30 foreach my $file ( File::Find::Rule->new->file->in($dir) ) {
190 16 my $ref = 'refs/' . file($file)->relative($dir)->as_foreign('Unix');
191 16 ▶ if ( $ref eq $wantref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq $wantref) 16 0 16
192 16 ▶ my $sha1 = file($file)->slurp
Condition33%1/3
file($file)->slurp || confess("Error reading ${file}: $!")
l!l&&r!l&&!r
1600
MC/DC0%0/2
file($file)->slurp || confess("Error reading ${file}: $!")
file($file)->slurp confess("Error reading ${file}: $!")
Truth table33%1/3
A || B
A= file($file)->slurpB= confess("Error reading ${file}: $!")
ABresult
1X1
011
000
193 || confess("Error reading $file: $!");
194 16 chomp $sha1;
195 16 return _ensure_sha1_is_sha1( $self, $sha1 );
196 }
197 }
198  
199 14 my $packed_refs = file( $self->gitdir, 'packed-refs' );
200 14 ▶ if ( -f $packed_refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $packed_refs) 14 0 14
201 14 my $last_name;
202 my $last_sha1;
203 14 foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
204 28 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 14 14 28
205 14 my ( $sha1, $name ) = split ' ', $line;
206 14 $sha1 =~ s/^\^//;
207 14 ▶ $name ||= $last_name;
Condition33%1/3
$name ||= $last_name
l!l&&r!l&&!r
1400
MC/DC0%0/2
$name ||= $last_name
$name $last_name
Truth table33%1/3
A ||= B
A= $nameB= $last_name
ABresult
1X1
011
000
208  
209 14 ▶ return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name and $last_name eq $wantref and $name ne $wantref;
Branch: 1 branch
BranchTrueFalseTotal
if $last_name and $last_name eq $wantref and $name ne $wantref 0 14 14
Condition33%2/6
$last_name and $last_name eq $wantref and $name ne $wantref
!ll&&!rl&&r
1400
$last_name and $last_name eq $wantref
!ll&&!rl&&r
1400
MC/DC0%0/3
$last_name and $last_name eq $wantref and $name ne $wantref
$last_name $last_name eq $wantref $name ne $wantref
Truth table25%1/4
A and B and C
A= $last_nameB= $last_name eq $wantrefC= $name ne $wantref
ABCresult
0XX0
10X0
1100
1111
210  
211 14 $last_name = $name;
212 14 $last_sha1 = $sha1;
213 }
214 14 ▶ return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name eq $wantref;
Branch: 1 branch
BranchTrueFalseTotal
if $last_name eq $wantref 14 0 14
215 }
216 0 return undef;
217 }
218  
219 sub _ensure_sha1_is_sha1 {
220 30 ▶ my ( $self, $sha1 ) = @_;
Subroutine _ensure_sha1_is_sha1: called CC=2 SCAR=7.1
221 30 ▶ return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $sha1 =~ /^ref: (.*)/ 0 30 30
222 30 return $sha1;
223 }
224  
225 sub ref {
226 21 ▶ my ( $self, $wantref ) = @_;
Subroutine ref: called CC=1 SCAR=0.0
Pod undocumented
227 21 return $self->get_object( $self->ref_sha1($wantref) );
228 }
229  
230 sub master_sha1 {
231 3 ▶ my $self = shift;
Subroutine master_sha1: called CC=1 SCAR=0.0
Pod undocumented
232 3 return $self->ref_sha1('refs/heads/master');
233 }
234  
235 sub master {
236 9 ▶ my $self = shift;
Subroutine master: called CC=1 SCAR=0.0
Pod documented
237 9 return $self->ref('refs/heads/master');
238 }
239  
240 sub head_sha1 {
241 0 ▶ my $self = shift;
Subroutine head_sha1: not called CC=1 SCAR=6.9
Pod undocumented
242 0 return $self->ref_sha1('HEAD');
243 }
244  
245 sub head {
246 0 ▶ my $self = shift;
Subroutine head: not called CC=1 SCAR=6.9
Pod undocumented
247 0 return $self->ref('HEAD');
248 }
249  
250 sub get_object {
251 117 ▶ my ( $self, $sha1 ) = @_;
Subroutine get_object: called CC=3 SCAR=11.0
Pod documented
252 117 ▶ return unless $sha1;
Branch: 1 branch
BranchTrueFalseTotal
unless $sha1 3 114 117
253 114 ▶ return $self->get_object_packed($sha1) || $self->get_object_loose($sha1);
Condition66%2/3
$self->get_object_packed($sha1) || $self->get_object_loose($sha1)
l!l&&r!l&&!r
46680
MC/DC0%0/2
$self->get_object_packed($sha1) || $self->get_object_loose($sha1)
$self->get_object_packed($sha1) $self->get_object_loose($sha1)
Truth table66%2/3
A || B
A= $self->get_object_packed($sha1)B= $self->get_object_loose($sha1)
ABresult
1X1
011
000
254 }
255  
256 sub get_objects {
257 9 ▶ my ( $self, @sha1s ) = @_;
Subroutine get_objects: called CC=1 SCAR=0.0
Pod undocumented
258 9 return map { $self->get_object($_) } @sha1s;
259 }
260  
261 sub get_object_packed {
262 114 ▶ my ( $self, $sha1 ) = @_;
Subroutine get_object_packed: called CC=5 SCAR=18.6
Pod documented
263  
264 114 foreach my $pack ( $self->packs ) {
265 46 my ( $kind, $size, $content ) = $pack->get_object($sha1);
266 46 ▶ if ( defined($kind) && defined($size) && defined($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $kind and defined $size and defined $content) 46 0 46
Condition33%2/6
defined $kind and defined $size and defined $content
!ll&&!rl&&r
0046
defined $kind and defined $size
!ll&&!rl&&r
0046
MC/DC0%0/3
defined $kind and defined $size and defined $content
defined $kind defined $size defined $content
Truth table25%1/4
A and B and C
A= defined $kindB= defined $sizeC= defined $content
ABCresult
0XX0
10X0
1100
1111
267 46 return $self->create_object( $sha1, $kind, $size, $content );
268 }
269 }
270 }
271  
272 sub get_object_loose {
273 68 ▶ my ( $self, $sha1 ) = @_;
Subroutine get_object_loose: called CC=4 SCAR=16.4
Pod documented
274  
275 68 my ( $kind, $size, $content ) = $self->loose->get_object($sha1);
276 68 ▶ if ( defined($kind) && defined($size) && defined($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $kind and defined $size and defined $content) 68 0 68
Condition33%2/6
defined $kind and defined $size and defined $content
!ll&&!rl&&r
0068
defined $kind and defined $size
!ll&&!rl&&r
0068
MC/DC0%0/3
defined $kind and defined $size and defined $content
defined $kind defined $size defined $content
Truth table25%1/4
A and B and C
A= defined $kindB= defined $sizeC= defined $content
ABCresult
0XX0
10X0
1100
1111
277 68 return $self->create_object( $sha1, $kind, $size, $content );
278 }
279 }
280  
281 sub create_object {
282 114 ▶ my ( $self, $sha1, $kind, $size, $content ) = @_;
Subroutine create_object: called CC=5 SCAR=17.8
Pod documented
283 114 ▶ if ( $kind eq 'commit' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($kind eq 'commit') { } 47 67 114
elsif ($kind eq 'tree') { } 30 37 67
elsif ($kind eq 'blob') { } 37 0 37
elsif ($kind eq 'tag') { } 0 0 0
284 47 return Git::PurePerl::Object::Commit->new(
285 sha1 => $sha1,
286 kind => $kind,
287 size => $size,
288 content => $content,
289 git => $self,
290 );
291 } elsif ( $kind eq 'tree' ) {
292 30 return Git::PurePerl::Object::Tree->new(
293 sha1 => $sha1,
294 kind => $kind,
295 size => $size,
296 content => $content,
297 git => $self,
298 );
299 } elsif ( $kind eq 'blob' ) {
300 37 return Git::PurePerl::Object::Blob->new(
301 sha1 => $sha1,
302 kind => $kind,
303 size => $size,
304 content => $content,
305 git => $self,
306 );
307 } elsif ( $kind eq 'tag' ) {
308 0 return Git::PurePerl::Object::Tag->new(
309 sha1 => $sha1,
310 kind => $kind,
311 size => $size,
312 content => $content,
313 git => $self,
314 );
315 } else {
316 0 confess "unknown kind $kind: $content";
317 }
318 }
319  
320 sub all_sha1s {
321 18 ▶ my $self = shift;
Subroutine all_sha1s: called CC=2 SCAR=6.9
Pod documented
322 18 my $dir = dir( $self->gitdir, 'objects' );
323  
324 18 my @streams;
325 18 push @streams, $self->loose->all_sha1s;
326  
327 18 foreach my $pack ( $self->packs ) {
328 4 push @streams, $pack->all_sha1s;
329 }
330  
331 18 return Data::Stream::Bulk::Cat->new( streams => \@streams );
332 }
333  
334 sub all_objects {
335 9 ▶ my $self = shift;
Subroutine all_objects: called CC=1 SCAR=0.0
Pod undocumented
336 9 my $stream = $self->all_sha1s;
337 return Data::Stream::Bulk::Filter->new(
338 9 ▶ filter => sub { return [ $self->get_objects(@$_) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
339 9 stream => $stream,
340 );
341 }
342  
343 sub put_object {
344 14 ▶ my ( $self, $object, $ref ) = @_;
Subroutine put_object: called CC=3 SCAR=11.0
Pod undocumented
345 14 $self->loose->put_object($object);
346  
347 14 ▶ if ( $object->kind eq 'commit' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->kind eq "commit") 4 10 14
348 4 ▶ $ref = 'master' unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 4 0 4
349 4 $self->update_ref( $ref, $object->sha1 );
350 }
351 }
352  
353 sub update_ref {
354 4 ▶ my ( $self, $refname, $sha1 ) = @_;
Subroutine update_ref: called CC=3 SCAR=11.1
Pod undocumented
355 4 my $ref = file( $self->gitdir, 'refs', 'heads', $refname );
356 4 $ref->parent->mkpath;
357 4 my $ref_fh = $ref->openw;
358 4 ▶ $ref_fh->print($sha1) || die "Error writing to $ref";
Branch: 1 branch
BranchTrueFalseTotal
$ref_fh->print($sha1) or die "Error writing to $ref" 0 4 4
359  
360 # FIXME is this always what we want?
361 4 my $head = file( $self->gitdir, 'HEAD' );
362 4 my $head_fh = $head->openw;
363 4 ▶ $head_fh->print("ref: refs/heads/$refname")
Branch: 1 branch
BranchTrueFalseTotal
$head_fh->print("ref: refs/heads/$refname") or die "Error writing to $head" 0 4 4
364 || die "Error writing to $head";
365 }
366  
367 sub init {
368 3 ▶ my ( $class, %arguments ) = @_;
Subroutine init: called CC=5 SCAR=16.1
Pod undocumented
369  
370 3 my $directory = $arguments{directory};
371 3 my $git_dir;
372  
373 3 ▶ unless ( defined $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory) { } 2 1 3
374 $git_dir = $arguments{gitdir}
375 1 ▶ || confess
Condition33%1/3
$arguments{'gitdir'} || confess(q[init() needs either a 'directory' or a 'gitdir' argument])
l!l&&r!l&&!r
100
MC/DC0%0/2
$arguments{'gitdir'} || confess(q[init() needs either a 'directory' or a 'gitdir' argument])
$arguments{'gitdir'} confess(q[init() needs either a 'directory' or a 'gitdir' argument])
Truth table33%1/3
A || B
A= $arguments{'gitdir'}B= confess(q[init() needs either a 'directory' or a 'gitdir' argument])
ABresult
1X1
011
000
376 "init() needs either a 'directory' or a 'gitdir' argument";
377 } else {
378 2 ▶ if ( not defined $arguments{gitdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arguments{"gitdir"}) 2 0 2
379 2 $git_dir = $arguments{gitdir} = dir( $directory, '.git' );
380 }
381 2 dir($directory)->mkpath;
382 }
383  
384 3 dir($git_dir)->mkpath;
385 3 dir( $git_dir, 'refs', 'tags' )->mkpath;
386 3 dir( $git_dir, 'objects', 'info' )->mkpath;
387 3 dir( $git_dir, 'objects', 'pack' )->mkpath;
388 3 dir( $git_dir, 'branches' )->mkpath;
389 3 dir( $git_dir, 'hooks' )->mkpath;
390  
391 3 ▶ my $bare = defined($directory) ? 'false' : 'true';
Branch: 1 branch
BranchTrueFalseTotal
defined $directory ? : 2 1 3
392 3 $class->_add_file(
393 file( $git_dir, 'config' ),
394 "[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = $bare\n\tlogallrefupdates = true\n"
395 );
396 3 $class->_add_file( file( $git_dir, 'description' ),
397 "Unnamed repository; edit this file to name it for gitweb.\n" );
398 3 $class->_add_file(
399 file( $git_dir, 'hooks', 'applypatch-msg' ),
400 "# add shell script and make executable to enable\n"
401 );
402 3 $class->_add_file( file( $git_dir, 'hooks', 'post-commit' ),
403 "# add shell script and make executable to enable\n" );
404 3 $class->_add_file(
405 file( $git_dir, 'hooks', 'post-receive' ),
406 "# add shell script and make executable to enable\n"
407 );
408 3 $class->_add_file( file( $git_dir, 'hooks', 'post-update' ),
409 "# add shell script and make executable to enable\n" );
410 3 $class->_add_file(
411 file( $git_dir, 'hooks', 'pre-applypatch' ),
412 "# add shell script and make executable to enable\n"
413 );
414 3 $class->_add_file( file( $git_dir, 'hooks', 'pre-commit' ),
415 "# add shell script and make executable to enable\n" );
416 3 $class->_add_file( file( $git_dir, 'hooks', 'pre-rebase' ),
417 "# add shell script and make executable to enable\n" );
418 3 $class->_add_file( file( $git_dir, 'hooks', 'update' ),
419 "# add shell script and make executable to enable\n" );
420  
421 3 dir( $git_dir, 'info' )->mkpath;
422 3 $class->_add_file( file( $git_dir, 'info', 'exclude' ),
423 "# *.[oa]\n# *~\n" );
424  
425 3 return $class->new(%arguments);
426 }
427  
428 sub checkout {
429 4 ▶ my ( $self, $directory, $tree ) = @_;
Subroutine checkout: called CC=8 SCAR=25.3
Pod undocumented
430 4 ▶ $directory ||= $self->directory;
Condition33%1/3
$directory ||= $self->directory
l!l&&r!l&&!r
400
MC/DC0%0/2
$directory ||= $self->directory
$directory $self->directory
Truth table33%1/3
A ||= B
A= $directoryB= $self->directory
ABresult
1X1
011
000
431 4 ▶ $tree ||= $self->master->tree;
Condition33%1/3
$tree ||= $self->master->tree
l!l&&r!l&&!r
040
MC/DC0%0/2
$tree ||= $self->master->tree
$tree $self->master->tree
Truth table33%1/3
A ||= B
A= $treeB= $self->master->tree
ABresult
1X1
011
000
432 4 ▶ confess("Missing tree") unless $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree 0 4 4
433 4 foreach my $directory_entry ( $tree->directory_entries ) {
434 5 my $filename = file( $directory, $directory_entry->filename );
435 5 my $sha1 = $directory_entry->sha1;
436 5 my $mode = $directory_entry->mode;
437 5 my $object = $self->get_object($sha1);
438 5 ▶ if ( $object->kind eq 'blob' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->kind eq 'blob') { } 5 0 5
elsif ($object->kind eq 'tree') { } 0 0 0
439 5 $self->_add_file( $filename, $object->content );
440 5 ▶ chmod( oct( '0' . $mode ), $filename )
Branch: 1 branch
BranchTrueFalseTotal
chmod oct '0' . $mode, $filename or die "Error chmoding $filename to ${mode}: $!" 0 5 5
441 || die "Error chmoding $filename to $mode: $!";
442 } elsif ( $object->kind eq 'tree' ) {
443 0 dir($filename)->mkpath;
444 0 $self->checkout( $filename, $object );
445 } else {
446 0 die $object->kind;
447 }
448 }
449 }
450  
451 sub clone {
452 1 ▶ my $self = shift;
Subroutine clone: called CC=3 SCAR=14.2
Pod undocumented
453  
454 1 my $remote;
455 1 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 0 1
456 # For backwards compatibility
457 1 $remote = "git://$_[0]";
458 1 ▶ $remote .= "/" unless $_[1] =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ m[^/] 0 1 1
459 1 $remote .= $_[1];
460 } else {
461 0 $remote = shift;
462 }
463  
464 1 my $protocol = Git::PurePerl::Protocol->new(
465 remote => $remote,
466 );
467  
468 1 my $sha1s = $protocol->connect;
469 0 my $head = $sha1s->{HEAD};
470 0 my $data = $protocol->fetch_pack($head);
471  
472 0 my $filename
473 = file( $self->gitdir, 'objects', 'pack', 'pack-' . $head . '.pack' );
474 0 $self->_add_file( $filename, $data );
475  
476 0 my $pack
477 = Git::PurePerl::Pack::WithoutIndex->new( filename => $filename );
478 0 $pack->create_index();
479  
480 0 $self->update_ref( master => $head );
481 }
482  
483 sub _add_file {
484 38 ▶ my ( $class, $filename, $contents ) = @_;
Subroutine _add_file: called CC=4 SCAR=15.2
485 38 ▶ my $fh = $filename->openw || confess "Error opening to $filename: $!";
Condition33%1/3
$filename->openw || confess("Error opening to ${filename}: $!")
l!l&&r!l&&!r
3800
MC/DC0%0/2
$filename->openw || confess("Error opening to ${filename}: $!")
$filename->openw confess("Error opening to ${filename}: $!")
Truth table33%1/3
A || B
A= $filename->openwB= confess("Error opening to ${filename}: $!")
ABresult
1X1
011
000
486 38 binmode($fh); #important for Win32
487 38 ▶ $fh->print($contents) || confess "Error writing to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($contents) or confess("Error writing to ${filename}: $!") 0 38 38
488 38 ▶ $fh->close || confess "Error closing $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or confess("Error closing ${filename}: $!") 0 38 38
489 }
490  
491 4 1;
492  
493 __END__
494  
495 =head1 NAME
496  
497 Git::PurePerl - A Pure Perl interface to Git repositories
498  
499 =head1 SYNOPSIS
500  
501 my $git = Git::PurePerl->new(
502 directory => '/path/to/git/'
503 );
504 $git->master->committer;
505 $git->master->comment;
506 $git->get_object($git->master->tree);
507  
508 =head1 DESCRIPTION
509  
510 This module is a Pure Perl interface to Git repositories.
511  
512 It was mostly based on Grit L<http://grit.rubyforge.org/>.
513  
514 =head1 METHODS
515  
516 =over 4
517  
518 =item master
519  
520 =item get_object
521  
522 =item get_object_packed
523  
524 =item get_object_loose
525  
526 =item create_object
527  
528 =item all_sha1s
529  
530 =back
531  
532 =head1 MAINTAINANCE
533  
534 This module is maintained in git at L<http://github.com/broquaint/git-pureperl/>.
535  
536 Patches are welcome, please come speak to one of the L<Gitalist> team
537 on C<< #gitalist >>.
538  
539 =head1 AUTHOR
540  
541 Leon Brocard <acme@astray.com>
542  
543 =head1 CONTRIBUTORS
544  
545 =over 4
546  
547 =item Chris Reinhardt
548  
549 =item Tomas (t0m) Doran
550  
551 =item Dan (broquaint) Brook
552  
553 =item Alex Vandiver
554  
555 =item Dagfinn Ilmari MannsE<aring>ker
556  
557 =back
558  
559 =head1 COPYRIGHT
560  
561 Copyright (C) 2008, Leon Brocard and the above mentioned contributors.
562  
563 =head1 LICENSE
564  
565 This module is free software; you can redistribute it or
566 modify it under the same terms as Perl itself.
567  
568 =cut
« PurePerl/Protocol.pm ↑ Summary PurePerl/Pack.pm »