Native/Repository.pm untested

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