| 1 | # ABSTRACT: Native Git for Perl via libgit2 (FFI, no fork/exec) | ||
| 2 | |||
| 3 | package Git::Native; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | 42 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 42 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 42 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Git::Libgit2 qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | init_lib | ||
| 10 | GIT_REPOSITORY_INIT_BARE | ||
| 11 | GIT_OPT_SET_SEARCH_PATH | ||
| 12 | GIT_CONFIG_LEVEL_PROGRAMDATA | ||
| 13 | GIT_CONFIG_LEVEL_SYSTEM | ||
| 14 | GIT_CONFIG_LEVEL_XDG | ||
| 15 | GIT_CONFIG_LEVEL_GLOBAL | ||
| 16 | 0 | ); | |
| 17 | use Git::Libgit2::FFI (); | ||
| 18 | use Git::Native::Error qw( check_rc ); | ||
| 19 | use Git::Native::Repository (); | ||
| 20 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 21 | |||
| 22 | use constant { | ||
| 23 | GIT_CLONE_OPTIONS_VERSION => 1, | ||
| 24 | # git_clone_options on libgit2 1.5 is ~312 bytes; over-allocate for | ||
| 25 | # forward compat with newer libgit2 versions. | ||
| 26 | CLONE_OPTIONS_SIZE => 512, | ||
| 27 | }; | ||
| 28 | |||
| 29 | # Ensure libgit2 is initialised before first use. | ||
| 30 | my $_init_count = 0; | ||
| 31 | sub _ensure_init { | ||
| 32 | return if $_init_count; | ||
| 33 | $_init_count = init_lib(); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub open { | ||
| 37 | my ( $class, $path ) = @_; | ||
| 38 | Carp::croak "Git::Native->open requires a path" unless defined $path; | ||
| 39 | _ensure_init(); | ||
| 40 | my $repo; | ||
| 41 | check_rc Git::Libgit2::FFI::git_repository_open( \$repo, $path ); | ||
| 42 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub open_ext { | ||
| 46 | my ( $class, $start_path, %opts ) = @_; | ||
| 47 | _ensure_init(); | ||
| 48 | my $repo; | ||
| 49 | check_rc Git::Libgit2::FFI::git_repository_open_ext( | ||
| 50 | \$repo, $start_path, | ||
| 51 | $opts{flags} // 0, | ||
| 52 | $opts{ceiling_dirs}, | ||
| 53 | ); | ||
| 54 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub init { | ||
| 58 | my ( $class, $path, %opts ) = @_; | ||
| 59 | Carp::croak "Git::Native->init requires a path" unless defined $path; | ||
| 60 | _ensure_init(); | ||
| 61 | my $repo; | ||
| 62 | my $flags = $opts{bare} ? GIT_REPOSITORY_INIT_BARE : 0; | ||
| 63 | check_rc Git::Libgit2::FFI::git_repository_init( \$repo, $path, $flags ); | ||
| 64 | my $r = Git::Native::Repository->new( _handle => $repo ); | ||
| 65 | # Pin HEAD at the requested branch regardless of the compiled-in default | ||
| 66 | # or ambient init.defaultBranch (sterile CI containers default to | ||
| 67 | # 'master'). The branch may be unborn at this point - that's fine. | ||
| 68 | if ( defined( my $branch = $opts{initial_branch} ) ) { | ||
| 69 | $branch = "refs/heads/$branch" unless $branch =~ m{^refs/}; | ||
| 70 | $r->set_head($branch); | ||
| 71 | } | ||
| 72 | return $r; | ||
| 73 | } | ||
| 74 | |||
| 75 | # clone($url, $local_path) - non-bare only for now. | ||
| 76 | # Auth via credentials => sub {...} not yet plumbed; the clone_options | ||
| 77 | # struct embeds a fetch_options whose callback offset we'd need to probe | ||
| 78 | # per libgit2 version. Bare clones go through init+fetch+HEAD instead - | ||
| 79 | # the offset of `bare` is past two large embedded structs and isn't | ||
| 80 | # stable across libgit2 versions worth pinning here. | ||
| 81 | sub clone { | ||
| 82 | my ( $class, $url, $local_path, %opts ) = @_; | ||
| 83 | Carp::croak "Git::Native->clone requires url and local_path" | ||
| 84 | unless defined $url && defined $local_path; | ||
| 85 | Carp::croak "bare clones not yet supported by Git::Native->clone - use init(bare=>1) + remote + fetch" | ||
| 86 | if $opts{bare}; | ||
| 87 | _ensure_init(); | ||
| 88 | |||
| 89 | my $buf = "\0" x CLONE_OPTIONS_SIZE; | ||
| 90 | my ($buf_p) = scalar_to_buffer($buf); | ||
| 91 | check_rc Git::Libgit2::FFI::git_clone_options_init( $buf_p, GIT_CLONE_OPTIONS_VERSION ); | ||
| 92 | |||
| 93 | my $repo; | ||
| 94 | check_rc Git::Libgit2::FFI::git_clone( \$repo, $url, $local_path, $buf_p ); | ||
| 95 | return Git::Native::Repository->new( _handle => $repo ); | ||
| 96 | } | ||
| 97 | |||
| 98 | # reference_name_is_valid($name) - does libgit2 accept this refname? | ||
| 99 | # No repository required. Returns 1 (valid) or 0 (invalid). | ||
| 100 | sub reference_name_is_valid { | ||
| 101 | my ( $class, $name ) = @_; | ||
| 102 | return 0 unless defined $name; | ||
| 103 | _ensure_init(); | ||
| 104 | my $rc = Git::Libgit2::FFI::git_reference_name_is_valid( \my $valid, $name ); | ||
| 105 | return ( $rc == 0 && $valid ) ? 1 : 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | # set_config_search_path(system => $dir, global => $dir, ...) | ||
| 109 | # | ||
| 110 | # Class method on purpose: git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH) mutates | ||
| 111 | # one process-global table inside libgit2, not any repository's state. It | ||
| 112 | # does not change what an already-open repository reads (libgit2 resolved | ||
| 113 | # and cached that when the repository was opened) - only what the next | ||
| 114 | # git_repository_open sees. A ->set_config_search_path on | ||
| 115 | # Git::Native::Repository would therefore be a method that provably does | ||
| 116 | # nothing to the object it is called on. | ||
| 117 | my %CONFIG_LEVEL = ( | ||
| 118 | programdata => GIT_CONFIG_LEVEL_PROGRAMDATA, | ||
| 119 | system => GIT_CONFIG_LEVEL_SYSTEM, | ||
| 120 | xdg => GIT_CONFIG_LEVEL_XDG, | ||
| 121 | global => GIT_CONFIG_LEVEL_GLOBAL, | ||
| 122 | ); | ||
| 123 | |||
| 124 | sub set_config_search_path { | ||
| 125 | my ( $class, %levels ) = @_; | ||
| 126 | Carp::croak "Git::Native->set_config_search_path needs at least one level" | ||
| 127 | unless %levels; | ||
| 128 | _ensure_init(); | ||
| 129 | for my $name ( sort keys %levels ) { | ||
| 130 | my $level = $CONFIG_LEVEL{ lc $name } | ||
| 131 | or Carp::croak "Git::Native->set_config_search_path: unknown config level " | ||
| 132 | . "'$name' - expected one of: " . join( ', ', sort keys %CONFIG_LEVEL ); | ||
| 133 | my $dir = $levels{$name}; | ||
| 134 | check_rc Git::Libgit2::FFI::git_libgit2_opts( | ||
| 135 | GIT_OPT_SET_SEARCH_PATH, $level, defined $dir ? "$dir" : undef ); | ||
| 136 | } | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ | ||