Native/Repository.pm untested

filter: Statementstmt 0.0% 0 / 271 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 40 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 57 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 475 CC 35 SCAR 20.7
CC
35
Cov
0%
tag_create
30.0
commit_create
20.0
reference_create
12.0
CRAP 7.9
1 # ABSTRACT: A libgit2 repository handle
2  
3 package Git::Native::Repository;
4 use Moo;
5 use Carp ();
6 use Git::Libgit2 qw( check_rc GIT_OBJECT_BLOB GIT_OBJECT_TREE GIT_OBJECT_COMMIT );
7 use Git::Libgit2::FFI ();
8 use FFI::Platypus::Buffer qw( scalar_to_buffer );
9 use Git::Native::Reference ();
10 use Git::Native::Blob ();
11 use Git::Native::Tree ();
12 use Git::Native::TreeBuilder ();
13 use Git::Native::Commit ();
14 use Git::Native::Config ();
15 use Git::Native::Signature ();
16 use Git::Native::Oid ();
17 use Git::Native::Remote ();
18 use Git::Native::Revwalker ();
19 use Git::Native::Branch ();
20 use Git::Native::Tag ();
21 use FFI::Platypus 2.00 ();
22  
23 has _handle => ( is => 'ro', required => 1 );
24  
25 sub workdir { Git::Libgit2::FFI::git_repository_workdir( $_[0]->_handle ) }
26 sub gitdir { Git::Libgit2::FFI::git_repository_path( $_[0]->_handle ) }
27 sub is_bare { Git::Libgit2::FFI::git_repository_is_bare( $_[0]->_handle ) ? 1 : 0 }
28  
29 # ---------- references ----------
30  
31 sub reference {
32 my ( $self, $name ) = @_;
33 check_rc Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $name );
34 return Git::Native::Reference->new( _handle => $ref, _owner => $self );
35 }
36  
37 sub reference_create {
38 my ( $self, $name, $oid, %opts ) = @_;
39 $oid = Git::Native::Oid->from_hex($oid) if !ref $oid;
40 check_rc Git::Libgit2::FFI::git_reference_create(
41 \my $ref, $self->_handle, $name, $oid->ptr,
42 $opts{force} ? 1 : 0,
43 $opts{message} // '',
44 );
45 return Git::Native::Reference->new( _handle => $ref, _owner => $self );
46 }
47  
48 sub reference_delete {
49 my ( $self, $name ) = @_;
50 check_rc Git::Libgit2::FFI::git_reference_remove( $self->_handle, $name );
51 return $self;
52 }
53  
54 sub reference_exists {
55 my ( $self, $name ) = @_;
56 my $rc = Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $name );
57 if ( $rc == 0 ) {
58 Git::Libgit2::FFI::git_reference_free($ref);
59 return 1;
60 }
61 return 0;
62 }
63  
64 # Returns list of full ref names. Optional `glob` filters libgit2-side.
65 sub reference_names {
66 my ( $self, %opts ) = @_;
67 my $iter;
68 if ( $opts{glob} ) {
69 check_rc Git::Libgit2::FFI::git_reference_iterator_glob_new(
70 \$iter, $self->_handle, $opts{glob},
71 );
72 }
73 else {
74 check_rc Git::Libgit2::FFI::git_reference_iterator_new( \$iter, $self->_handle );
75 }
76 my @names;
77 while (1) {
78 my $rc = Git::Libgit2::FFI::git_reference_next_name( \my $name, $iter );
79 last if $rc == -31; # GIT_ITEROVER
80 check_rc $rc;
81 push @names, $name;
82 }
83 Git::Libgit2::FFI::git_reference_iterator_free($iter);
84 return \@names;
85 }
86  
87 # Resolved HEAD reference, or undef when HEAD is unborn / missing
88 # (fresh repo with no commits yet).
89 sub head {
90 my $self = shift;
91 my $rc = Git::Libgit2::FFI::git_repository_head( \my $ref, $self->_handle );
92 return undef if $rc == -9 || $rc == -3; # GIT_EUNBORNBRANCH / GIT_ENOTFOUND
93 check_rc $rc;
94 return Git::Native::Reference->new( _handle => $ref, _owner => $self );
95 }
96  
97 sub head_unborn {
98 my $rc = Git::Libgit2::FFI::git_repository_head_unborn( $_[0]->_handle );
99 check_rc $rc if $rc < 0;
100 return $rc ? 1 : 0;
101 }
102  
103 sub head_detached {
104 my $rc = Git::Libgit2::FFI::git_repository_head_detached( $_[0]->_handle );
105 check_rc $rc if $rc < 0;
106 return $rc ? 1 : 0;
107 }
108  
109 # Point HEAD at a branch refname (e.g. 'refs/heads/main'). The branch may
110 # be unborn - this is how you pin 'main' on a freshly init'd repo.
111 sub set_head {
112 my ( $self, $refname ) = @_;
113 check_rc Git::Libgit2::FFI::git_repository_set_head( $self->_handle, $refname );
114 return $self;
115 }
116  
117 sub reference_symbolic_create {
118 my ( $self, $name, $target, %opts ) = @_;
119 check_rc Git::Libgit2::FFI::git_reference_symbolic_create(
120 \my $ref, $self->_handle, $name, $target,
121 $opts{force} ? 1 : 0,
122 $opts{message} // '',
123 );
124 return Git::Native::Reference->new( _handle => $ref, _owner => $self );
125 }
126  
127 # ---------- blobs / trees / commits ----------
128  
129 sub blob_create_frombuffer {
130 my ( $self, $content ) = @_;
131 my $raw = "\0" x 20;
132 my ($oid_p) = scalar_to_buffer($raw);
133 my ($content_p) = scalar_to_buffer($content);
134 check_rc Git::Libgit2::FFI::git_blob_create_from_buffer(
135 $oid_p, $self->_handle, $content_p, length($content),
136 );
137 return Git::Native::Oid->from_raw($raw);
138 }
139  
140 sub blob {
141 my ( $self, $oid ) = @_;
142 $oid = Git::Native::Oid->from_hex($oid) if !ref $oid;
143 check_rc Git::Libgit2::FFI::git_blob_lookup( \my $b, $self->_handle, $oid->ptr );
144 return Git::Native::Blob->new( _handle => $b, _owner => $self );
145 }
146  
147 sub tree {
148 my ( $self, $oid ) = @_;
149 $oid = Git::Native::Oid->from_hex($oid) if !ref $oid;
150 check_rc Git::Libgit2::FFI::git_tree_lookup( \my $t, $self->_handle, $oid->ptr );
151 return Git::Native::Tree->new( _handle => $t, _owner => $self );
152 }
153  
154 sub tree_builder {
155 my $self = shift;
156 check_rc Git::Libgit2::FFI::git_treebuilder_new( \my $tb, $self->_handle, undef );
157 return Git::Native::TreeBuilder->new( _handle => $tb, _owner => $self );
158 }
159  
160 sub commit {
161 my ( $self, $oid ) = @_;
162 $oid = Git::Native::Oid->from_hex($oid) if !ref $oid;
163 check_rc Git::Libgit2::FFI::git_commit_lookup( \my $c, $self->_handle, $oid->ptr );
164 return Git::Native::Commit->new( _handle => $c, _owner => $self );
165 }
166  
167 # commit_create(%args): tree => Oid|hex, parents => [Oid|hex, ...],
168 # message => str, update_ref => 'HEAD', author => Signature, committer => Signature
169 sub commit_create {
170 my ( $self, %args ) = @_;
171  
172 my $tree_oid = $args{tree};
173 $tree_oid = Git::Native::Oid->from_hex($tree_oid) if !ref $tree_oid;
174  
175 # commit_create takes git_tree*, so we need to look it up.
176 check_rc Git::Libgit2::FFI::git_tree_lookup( \my $tree_h, $self->_handle, $tree_oid->ptr );
177  
178 my $sig_author = $args{author} // $self->signature_default;
179 my $sig_committer = $args{committer} // $sig_author;
180  
181 # Parents: libgit2 wants an array of git_commit*. We pass undef for 0,
182 # otherwise look up each parent into commits and pass an opaque[] array.
183 # FFI::Platypus passes Perl arrays of opaque via 'opaque[]'.
184 my @parent_oids = @{ $args{parents} // [] };
185 my @parent_handles;
186 for my $p (@parent_oids) {
187 $p = Git::Native::Oid->from_hex($p) if !ref $p;
188 check_rc Git::Libgit2::FFI::git_commit_lookup( \my $c, $self->_handle, $p->ptr );
189 push @parent_handles, $c;
190 }
191  
192 my $raw = "\0" x 20;
193 my ($oid_p) = scalar_to_buffer($raw);
194  
195 # Build a parents-array pointer if non-empty.
196 # FFI::Platypus 2: we declared parents as 'opaque' — accepting NULL or a pointer.
197 # To pass an array we need a temporary buffer of pointers. For MVP, support 0..1 parent.
198 if ( @parent_handles == 0 ) {
199 check_rc Git::Libgit2::FFI::git_commit_create(
200 $oid_p, $self->_handle, $args{update_ref},
201 $sig_author->_handle, $sig_committer->_handle,
202 $args{message_encoding} // 'UTF-8',
203 $args{message},
204 $tree_h,
205 0, undef,
206 );
207 }
208 else {
209 # Pack pointer array. Each pointer is a 64-bit value on x86_64.
210 my $parents_buf = pack 'J*', @parent_handles;
211 my ($parents_p) = scalar_to_buffer($parents_buf);
212 check_rc Git::Libgit2::FFI::git_commit_create(
213 $oid_p, $self->_handle, $args{update_ref},
214 $sig_author->_handle, $sig_committer->_handle,
215 $args{message_encoding} // 'UTF-8',
216 $args{message},
217 $tree_h,
218 scalar(@parent_handles), $parents_p,
219 );
220 }
221  
222 Git::Libgit2::FFI::git_commit_free($_) for @parent_handles;
223 Git::Libgit2::FFI::git_tree_free($tree_h);
224  
225 return Git::Native::Oid->from_raw($raw);
226 }
227  
228 # ---------- remotes ----------
229  
230 sub remote {
231 my ( $self, $name ) = @_;
232 check_rc Git::Libgit2::FFI::git_remote_lookup( \my $r, $self->_handle, $name );
233 return Git::Native::Remote->new( _handle => $r, _owner => $self );
234 }
235  
236 sub remote_create {
237 my ( $self, $name, $url ) = @_;
238 check_rc Git::Libgit2::FFI::git_remote_create( \my $r, $self->_handle, $name, $url );
239 return Git::Native::Remote->new( _handle => $r, _owner => $self );
240 }
241  
242 sub remote_anonymous {
243 my ( $self, $url ) = @_;
244 check_rc Git::Libgit2::FFI::git_remote_create_anonymous( \my $r, $self->_handle, $url );
245 return Git::Native::Remote->new( _handle => $r, _owner => $self );
246 }
247  
248 sub has_remote {
249 my ( $self, $name ) = @_;
250 my $rc = Git::Libgit2::FFI::git_remote_lookup( \my $r, $self->_handle, $name );
251 if ( $rc == 0 ) {
252 Git::Libgit2::FFI::git_remote_free($r);
253 return 1;
254 }
255 return 0;
256 }
257  
258 # ---------- config ----------
259  
260 # Live, writable config (use set_string here).
261 sub config {
262 my $self = shift;
263 check_rc Git::Libgit2::FFI::git_repository_config( \my $cfg, $self->_handle );
264 return Git::Native::Config->new( _handle => $cfg, _owner => $self );
265 }
266  
267 # Read-only snapshot - required for reliable git_config_get_string.
268 sub config_snapshot {
269 my $self = shift;
270 check_rc Git::Libgit2::FFI::git_repository_config_snapshot( \my $cfg, $self->_handle );
271 return Git::Native::Config->new( _handle => $cfg, _owner => $self );
272 }
273  
274 # Convenience: read one key off a fresh snapshot. undef when unset.
275 sub config_string {
276 my ( $self, $key ) = @_;
277 return $self->config_snapshot->get_string($key);
278 }
279  
280 # ---------- revwalk ----------
281  
282 sub revwalker {
283 my $self = shift;
284 check_rc Git::Libgit2::FFI::git_revwalk_new( \my $w, $self->_handle );
285 return Git::Native::Revwalker->new( _handle => $w, _owner => $self );
286 }
287  
288 # ---------- branches ----------
289  
290 sub branch {
291 my ( $self, $name, %opts ) = @_;
292 my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_LOCAL;
293 check_rc Git::Libgit2::FFI::git_branch_lookup( \my $ref, $self->_handle, $name, $type );
294 return Git::Native::Branch->new( _handle => $ref, _owner => $self, type => $type );
295 }
296  
297 sub has_branch {
298 my ( $self, $name, %opts ) = @_;
299 my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_LOCAL;
300 my $rc = Git::Libgit2::FFI::git_branch_lookup( \my $ref, $self->_handle, $name, $type );
301 if ( $rc == 0 ) {
302 Git::Libgit2::FFI::git_reference_free($ref);
303 return 1;
304 }
305 return 0;
306 }
307  
308 sub branch_create {
309 my ( $self, $name, $target, %opts ) = @_;
310 my $oid = ref($target) && $target->isa('Git::Native::Oid')
311 ? $target : Git::Native::Oid->from_hex($target);
312 check_rc Git::Libgit2::FFI::git_commit_lookup( \my $commit_h, $self->_handle, $oid->ptr );
313 my $rc = Git::Libgit2::FFI::git_branch_create(
314 \my $ref, $self->_handle, $name, $commit_h, $opts{force} ? 1 : 0,
315 );
316 Git::Libgit2::FFI::git_commit_free($commit_h);
317 check_rc $rc;
318 return Git::Native::Branch->new(
319 _handle => $ref, _owner => $self,
320 type => Git::Native::Branch::GIT_BRANCH_LOCAL,
321 );
322 }
323  
324 sub branches {
325 my ( $self, %opts ) = @_;
326 my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_ALL;
327 check_rc Git::Libgit2::FFI::git_branch_iterator_new( \my $iter, $self->_handle, $type );
328 my @out;
329 while (1) {
330 my $rc = Git::Libgit2::FFI::git_branch_next( \my $ref, \my $branch_type, $iter );
331 last if $rc == -31; # GIT_ITEROVER
332 if ( $rc != 0 ) {
333 Git::Libgit2::FFI::git_branch_iterator_free($iter);
334 check_rc $rc;
335 }
336 push @out, Git::Native::Branch->new(
337 _handle => $ref, _owner => $self, type => $branch_type,
338 );
339 }
340 Git::Libgit2::FFI::git_branch_iterator_free($iter);
341 return \@out;
342 }
343  
344 # ---------- tags ----------
345  
346 sub tag {
347 my ( $self, $name ) = @_;
348 # Resolve refs/tags/$name -> object id -> git_tag_lookup.
349 my $refname = $name =~ m{^refs/tags/} ? $name : "refs/tags/$name";
350 check_rc Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $refname );
351 my $oidp = Git::Libgit2::FFI::git_reference_target($ref);
352 my $oid = Git::Native::Oid->from_ptr($oidp);
353 Git::Libgit2::FFI::git_reference_free($ref);
354 my $rc = Git::Libgit2::FFI::git_tag_lookup( \my $tag, $self->_handle, $oid->ptr );
355 if ( $rc != 0 ) {
356 # Not an annotated tag - lightweight. Return undef; caller should use ->reference.
357 return undef;
358 }
359 return Git::Native::Tag->new( _handle => $tag, _owner => $self );
360 }
361  
362 sub tag_create {
363 my ( $self, $name, $target, %args ) = @_;
364 my $oid = ref($target) && $target->isa('Git::Native::Oid')
365 ? $target : Git::Native::Oid->from_hex($target);
366 # Look up target object generically (commit / tree / blob - GIT_OBJECT_ANY = -2).
367 check_rc Git::Libgit2::FFI::git_object_lookup( \my $obj, $self->_handle, $oid->ptr, -2 );
368  
369 my $raw = "\0" x 20;
370 my ($oid_p) = FFI::Platypus::Buffer::scalar_to_buffer($raw);
371  
372 if ( defined $args{message} ) {
373 my $tagger = $args{tagger} // $self->signature_default;
374 my $rc = Git::Libgit2::FFI::git_tag_create(
375 $oid_p, $self->_handle, $name, $obj,
376 $tagger->_handle, $args{message},
377 $args{force} ? 1 : 0,
378 );
379 Git::Libgit2::FFI::git_object_free($obj);
380 check_rc $rc;
381 }
382 else {
383 my $rc = Git::Libgit2::FFI::git_tag_create_lightweight(
384 $oid_p, $self->_handle, $name, $obj, $args{force} ? 1 : 0,
385 );
386 Git::Libgit2::FFI::git_object_free($obj);
387 check_rc $rc;
388 }
389 return Git::Native::Oid->from_raw($raw);
390 }
391  
392 sub tag_delete {
393 my ( $self, $name ) = @_;
394 check_rc Git::Libgit2::FFI::git_tag_delete( $self->_handle, $name );
395 return $self;
396 }
397  
398 sub tag_names {
399 my ( $self, %opts ) = @_;
400 # git_strarray on stack: {char **strings; size_t count} = 16 bytes.
401 my $buf = "\0" x 16;
402 my ($p) = FFI::Platypus::Buffer::scalar_to_buffer($buf);
403 if ( $opts{pattern} ) {
404 check_rc Git::Libgit2::FFI::git_tag_list_match( $p, $opts{pattern}, $self->_handle );
405 }
406 else {
407 check_rc Git::Libgit2::FFI::git_tag_list( $p, $self->_handle );
408 }
409 # Unpack strings ptr (at offset 0) and count (at offset 8).
410 my ( $strings_ptr, $count ) = unpack 'Q Q', $buf;
411 my @names;
412 if ( $count > 0 && $strings_ptr ) {
413 my $ffi = Git::Libgit2::FFI::ffi();
414 for my $i ( 0 .. $count - 1 ) {
415 my $sp = $ffi->cast( 'opaque', 'opaque[' . ( $i + 1 ) . ']', $strings_ptr )->[$i];
416 my $s = $ffi->cast( 'opaque', 'string', $sp );
417 push @names, $s;
418 }
419 }
420 Git::Libgit2::FFI::git_strarray_free($p);
421 return \@names;
422 }
423  
424 # ---------- status ----------
425  
426 # status() returns hashref { path => status_flags, ... }.
427 # status flags are the GIT_STATUS_* bitfield from libgit2.
428 sub status {
429 my $self = shift;
430 my %out;
431 my $ffi = Git::Libgit2::FFI::ffi();
432 my $cb = $ffi->closure( sub {
433 my ( $path, $flags, $payload ) = @_;
434 $out{$path} = $flags;
435 return 0;
436 });
437 check_rc Git::Libgit2::FFI::git_status_foreach( $self->_handle, $cb, undef );
438 return \%out;
439 }
440  
441 sub status_for_path {
442 my ( $self, $path ) = @_;
443 check_rc Git::Libgit2::FFI::git_status_file( \my $flags, $self->_handle, $path );
444 return $flags;
445 }
446  
447 sub signature_default {
448 my $self = shift;
449 my $rc = Git::Libgit2::FFI::git_signature_default( \my $sig, $self->_handle );
450 if ( $rc == 0 ) {
451 # We got an allocated git_signature*; wrap it without going through
452 # Signature::_build_handle.
453 my $obj = Git::Native::Signature->new(
454 name => '<from-config>', # placeholder; we own the C handle
455 email => '<from-config>',
456 );
457 $obj->{_handle} = $sig;
458 return $obj;
459 }
460 # Fallback if no user.name/email configured.
461 return Git::Native::Signature->new(
462 name => 'Git::Native',
463 email => 'unconfigured@example.invalid',
464 );
465 }
466  
467 sub DEMOLISH {
468 my $self = shift;
469 Git::Libgit2::FFI::git_repository_free( $self->{_handle} )
470 if $self->{_handle};
471 }
472  
473 1;
474  
475 __END__