| 1 | # ABSTRACT: Native Git for Perl via libgit2 (FFI, no fork/exec) | ||
| 2 | |||
| 3 | package Git::Native; | ||
| 4 | our $VERSION = '0.003'; | ||
| 5 | 15 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 15 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 15 | ▶ | use Git::Libgit2 qw( init_lib check_rc GIT_REPOSITORY_INIT_BARE ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Git::Libgit2::FFI (); | ||
| 9 | use Git::Native::Repository (); | ||
| 10 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | GIT_CLONE_OPTIONS_VERSION => 1, | ||
| 14 | # git_clone_options on libgit2 1.5 is ~312 bytes; over-allocate for | ||
| 15 | # forward compat with newer libgit2 versions. | ||
| 16 | CLONE_OPTIONS_SIZE => 512, | ||
| 17 | }; | ||
| 18 | |||
| 19 | # Ensure libgit2 is initialised before first use. | ||
| 20 | my $_init_count = 0; | ||
| 21 | sub _ensure_init { | ||
| 22 | return if $_init_count; | ||
| 23 | $_init_count = init_lib(); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub open { | ||
| 27 | my ( $class, $path ) = @_; | ||
| 28 | Carp::croak "Git::Native->open requires a path" unless defined $path; | ||
| 29 | _ensure_init(); | ||
| 30 | my $repo; | ||
| 31 | check_rc Git::Libgit2::FFI::git_repository_open( \$repo, $path ); | ||
| 32 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub open_ext { | ||
| 36 | my ( $class, $start_path, %opts ) = @_; | ||
| 37 | _ensure_init(); | ||
| 38 | my $repo; | ||
| 39 | check_rc Git::Libgit2::FFI::git_repository_open_ext( | ||
| 40 | \$repo, $start_path, | ||
| 41 | $opts{flags} // 0, | ||
| 42 | $opts{ceiling_dirs}, | ||
| 43 | ); | ||
| 44 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub init { | ||
| 48 | my ( $class, $path, %opts ) = @_; | ||
| 49 | Carp::croak "Git::Native->init requires a path" unless defined $path; | ||
| 50 | _ensure_init(); | ||
| 51 | my $repo; | ||
| 52 | my $flags = $opts{bare} ? GIT_REPOSITORY_INIT_BARE : 0; | ||
| 53 | check_rc Git::Libgit2::FFI::git_repository_init( \$repo, $path, $flags ); | ||
| 54 | my $r = Git::Native::Repository->new( _handle => $repo ); | ||
| 55 | # Pin HEAD at the requested branch regardless of the compiled-in default | ||
| 56 | # or ambient init.defaultBranch (sterile CI containers default to | ||
| 57 | # 'master'). The branch may be unborn at this point - that's fine. | ||
| 58 | if ( defined( my $branch = $opts{initial_branch} ) ) { | ||
| 59 | $branch = "refs/heads/$branch" unless $branch =~ m{^refs/}; | ||
| 60 | $r->set_head($branch); | ||
| 61 | } | ||
| 62 | return $r; | ||
| 63 | } | ||
| 64 | |||
| 65 | # clone($url, $local_path) - non-bare only for now. | ||
| 66 | # Auth via credentials => sub {...} not yet plumbed; the clone_options | ||
| 67 | # struct embeds a fetch_options whose callback offset we'd need to probe | ||
| 68 | # per libgit2 version. Bare clones go through init+fetch+HEAD instead - | ||
| 69 | # the offset of `bare` is past two large embedded structs and isn't | ||
| 70 | # stable across libgit2 versions worth pinning here. | ||
| 71 | sub clone { | ||
| 72 | my ( $class, $url, $local_path, %opts ) = @_; | ||
| 73 | Carp::croak "Git::Native->clone requires url and local_path" | ||
| 74 | unless defined $url && defined $local_path; | ||
| 75 | Carp::croak "bare clones not yet supported by Git::Native->clone - use init(bare=>1) + remote + fetch" | ||
| 76 | if $opts{bare}; | ||
| 77 | _ensure_init(); | ||
| 78 | |||
| 79 | my $buf = "\0" x CLONE_OPTIONS_SIZE; | ||
| 80 | my ($buf_p) = scalar_to_buffer($buf); | ||
| 81 | check_rc Git::Libgit2::FFI::git_clone_options_init( $buf_p, GIT_CLONE_OPTIONS_VERSION ); | ||
| 82 | |||
| 83 | my $repo; | ||
| 84 | check_rc Git::Libgit2::FFI::git_clone( \$repo, $url, $local_path, $buf_p ); | ||
| 85 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 86 | } | ||
| 87 | |||
| 88 | # reference_name_is_valid($name) - does libgit2 accept this refname? | ||
| 89 | # No repository required. Returns 1 (valid) or 0 (invalid). | ||
| 90 | sub reference_name_is_valid { | ||
| 91 | my ( $class, $name ) = @_; | ||
| 92 | return 0 unless defined $name; | ||
| 93 | _ensure_init(); | ||
| 94 | my $rc = Git::Libgit2::FFI::git_reference_name_is_valid( \my $valid, $name ); | ||
| 95 | return ( $rc == 0 && $valid ) ? 1 : 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ | ||