| 1 | # ABSTRACT: A libgit2 remote (fetch / push) | ||
| 2 | |||
| 3 | package Git::Native::Remote; | ||
| 4 | use Moo; | ||
| 5 | use Carp (); | ||
| 6 | use Git::Libgit2 qw( check_rc ); | ||
| 7 | use Git::Libgit2::FFI (); | ||
| 8 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 9 | use FFI::Platypus::Memory qw( memcpy malloc free ); | ||
| 10 | use Git::Native::Credential (); | ||
| 11 | |||
| 12 | # libgit2 1.5.x struct layouts (probed). 1.9.x add fields at the end of | ||
| 13 | # git_remote_callbacks but the offsets up through `payload` are stable. | ||
| 14 | # Allocate buffers a bit larger than the C struct for forward-compat. | ||
| 15 | use constant { | ||
| 16 | GIT_REMOTE_CALLBACKS_VERSION => 1, | ||
| 17 | GIT_FETCH_OPTIONS_VERSION => 1, | ||
| 18 | GIT_PUSH_OPTIONS_VERSION => 1, | ||
| 19 | |||
| 20 | CALLBACKS_SIZE => 256, # actual 1.5: 120; 1.9: ~152 | ||
| 21 | FETCH_OPTIONS_SIZE => 384, # actual 1.5: 208 | ||
| 22 | PUSH_OPTIONS_SIZE => 384, # actual 1.5: 192 | ||
| 23 | |||
| 24 | CALLBACKS_CRED_OFFSET => 24, # credentials cb pointer | ||
| 25 | CALLBACKS_PAYLOAD_OFFSET => 104, # payload void* | ||
| 26 | |||
| 27 | FETCH_OPTS_CALLBACKS_OFFSET => 8, # callbacks struct (embedded) | ||
| 28 | FETCH_OPTS_PRUNE_OFFSET => 128, # int (8 + 120) | ||
| 29 | |||
| 30 | PUSH_OPTS_CALLBACKS_OFFSET => 8, | ||
| 31 | |||
| 32 | GIT_PASSTHROUGH => -30, | ||
| 33 | |||
| 34 | GIT_DIRECTION_FETCH => 0, | ||
| 35 | GIT_DIRECTION_PUSH => 1, | ||
| 36 | |||
| 37 | REMOTE_HEAD_NAME_OFFSET => 48, | ||
| 38 | REMOTE_HEAD_SIZE => 64, | ||
| 39 | PTR_SIZE => 8, | ||
| 40 | }; | ||
| 41 | |||
| 42 | has _handle => ( is => 'rw', required => 1 ); | ||
| 43 | has _owner => ( is => 'ro', required => 1 ); # Repository | ||
| 44 | |||
| 45 | sub url { Git::Libgit2::FFI::git_remote_url( $_[0]->_handle ) } | ||
| 46 | sub name { Git::Libgit2::FFI::git_remote_name( $_[0]->_handle ) } | ||
| 47 | |||
| 48 | # ---------- fetch / push ---------- | ||
| 49 | |||
| 50 | # fetch(refspecs => [...], credentials => sub { ... }, prune => 0|1, | ||
| 51 | # reflog_message => '...') | ||
| 52 | sub fetch { | ||
| 53 | my ( $self, %args ) = @_; | ||
| 54 | my $refspecs_ref = $args{refspecs}; | ||
| 55 | my ( $sa_ptr, $sa_keep ) = _build_strarray( $refspecs_ref ); | ||
| 56 | |||
| 57 | my ( $opts_ptr, $opts_keep ) | ||
| 58 | = _build_fetch_options( $args{credentials}, $args{prune} ); | ||
| 59 | |||
| 60 | my $rc = Git::Libgit2::FFI::git_remote_fetch( | ||
| 61 | $self->_handle, $sa_ptr, $opts_ptr, | ||
| 62 | $args{reflog_message} // 'fetch', | ||
| 63 | ); | ||
| 64 | check_rc $rc; | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | # push(refspecs => [...], credentials => sub { ... }, prune => 0|1) | ||
| 69 | sub push { | ||
| 70 | my ( $self, %args ) = @_; | ||
| 71 | my $original_refspecs = $args{refspecs} // []; | ||
| 72 | my $refspecs_ref = $self->_expand_push_refspecs($original_refspecs); | ||
| 73 | |||
| 74 | # --prune: connect, list remote refs in our refspec's destination | ||
| 75 | # namespace, emit delete refspecs for the ones we don't have locally. | ||
| 76 | # Pass ORIGINAL refspecs (still containing wildcards) so we can | ||
| 77 | # recover the namespace pattern. | ||
| 78 | if ( $args{prune} && @$original_refspecs ) { | ||
| 79 | my @delete = $self->_compute_prune_deletes( | ||
| 80 | $original_refspecs, $args{credentials}, | ||
| 81 | ); | ||
| 82 | CORE::push @$refspecs_ref, @delete; | ||
| 83 | } | ||
| 84 | |||
| 85 | my ( $sa_ptr, $sa_keep ) = _build_strarray( $refspecs_ref ); | ||
| 86 | |||
| 87 | my ( $opts_ptr, $opts_keep ) | ||
| 88 | = _build_push_options( $args{credentials} ); | ||
| 89 | |||
| 90 | my $rc = Git::Libgit2::FFI::git_remote_push( | ||
| 91 | $self->_handle, $sa_ptr, $opts_ptr, | ||
| 92 | ); | ||
| 93 | check_rc $rc; | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | # List the remote-side refs (requires connecting first). Returns an | ||
| 98 | # arrayref of names. Caller passes credentials cb so private remotes work. | ||
| 99 | sub list_refs { | ||
| 100 | my ( $self, %args ) = @_; | ||
| 101 | $self->_connect( GIT_DIRECTION_FETCH, $args{credentials} ); | ||
| 102 | my @names; | ||
| 103 | eval { | ||
| 104 | check_rc Git::Libgit2::FFI::git_remote_ls( | ||
| 105 | \my $heads_arr, \my $count, $self->_handle, | ||
| 106 | ); | ||
| 107 | # heads_arr is git_remote_head**: an array of $count pointers, | ||
| 108 | # each pointing to a git_remote_head whose .name (char*) lives at | ||
| 109 | # offset REMOTE_HEAD_NAME_OFFSET. | ||
| 110 | my $ffi = Git::Libgit2::FFI::ffi(); | ||
| 111 | for ( my $i = 0; $i < $count; $i++ ) { | ||
| 112 | my $head_ptr = unpack 'J', | ||
| 113 | _peek_bytes( $heads_arr + $i * PTR_SIZE, PTR_SIZE ); | ||
| 114 | my $name_ptr = unpack 'J', | ||
| 115 | _peek_bytes( $head_ptr + REMOTE_HEAD_NAME_OFFSET, PTR_SIZE ); | ||
| 116 | my $name = $ffi->cast( 'opaque' => 'string', $name_ptr ); | ||
| 117 | CORE::push @names, $name; | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | my $err = $@; | ||
| 121 | Git::Libgit2::FFI::git_remote_disconnect( $self->_handle ); | ||
| 122 | die $err if $err; | ||
| 123 | return \@names; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _connect { | ||
| 127 | my ( $self, $direction, $cred_cb ) = @_; | ||
| 128 | # Build a callbacks struct on the stack-ish (Perl-owned buffer). | ||
| 129 | my $cb = "\0" x CALLBACKS_SIZE; | ||
| 130 | my ($cb_ptr) = scalar_to_buffer($cb); | ||
| 131 | check_rc Git::Libgit2::FFI::git_remote_init_callbacks( | ||
| 132 | $cb_ptr, GIT_REMOTE_CALLBACKS_VERSION, | ||
| 133 | ); | ||
| 134 | my @keep = ( \$cb ); | ||
| 135 | if ($cred_cb) { | ||
| 136 | my ( $thunk, $thunk_keep ) = _make_credential_thunk($cred_cb); | ||
| 137 | CORE::push @keep, $thunk_keep; | ||
| 138 | my $ptr_val = Git::Libgit2::FFI::ffi->cast( | ||
| 139 | 'git_credential_acquire_cb' => 'opaque', $thunk, | ||
| 140 | ); | ||
| 141 | my $pkt = pack 'J', $ptr_val; | ||
| 142 | my ($pkt_p) = scalar_to_buffer($pkt); | ||
| 143 | memcpy( $cb_ptr + CALLBACKS_CRED_OFFSET, $pkt_p, 8 ); | ||
| 144 | CORE::push @keep, \$pkt; | ||
| 145 | } | ||
| 146 | check_rc Git::Libgit2::FFI::git_remote_connect( | ||
| 147 | $self->_handle, $direction, $cb_ptr, 0, 0, | ||
| 148 | ); | ||
| 149 | # Hold keepalive on $self so it survives until the next call frees it. | ||
| 150 | $self->{_connect_keep} = \@keep; | ||
| 151 | return $self; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Compute delete refspecs for `--prune`: for each `[+]src:dst` with `*`, | ||
| 155 | # list remote refs matching the dst pattern, and emit a delete for each | ||
| 156 | # one whose local counterpart no longer exists. | ||
| 157 | sub _compute_prune_deletes { | ||
| 158 | my ( $self, $refspecs, $cred_cb ) = @_; | ||
| 159 | my $remote_names = $self->list_refs( credentials => $cred_cb ); | ||
| 160 | my %local; | ||
| 161 | $local{$_} = 1 for @{ $self->_owner->reference_names }; | ||
| 162 | |||
| 163 | my @deletes; | ||
| 164 | my %seen; | ||
| 165 | # Walk *original* user refspecs to figure out the dst-pattern namespace. | ||
| 166 | # We can't recover the dst-pattern from already-expanded specs. | ||
| 167 | for my $rs (@$refspecs) { | ||
| 168 | my ( $force, $src, $dst ) = $rs =~ /\A(\+?)([^:]+):(.+)\z/; | ||
| 169 | next unless defined $src && $dst =~ /\*/; | ||
| 170 | # Map remote ref → expected local name using dst→src. | ||
| 171 | my $dst_re = quotemeta($dst); $dst_re =~ s/\\\*/(.*)/; | ||
| 172 | $dst_re = qr/\A${dst_re}\z/; | ||
| 173 | my $src_template = $src; | ||
| 174 | for my $rname (@$remote_names) { | ||
| 175 | my ($cap) = $rname =~ $dst_re; | ||
| 176 | next unless defined $cap; | ||
| 177 | my $expected_local = $src_template; | ||
| 178 | $expected_local =~ s/\*/$cap/; | ||
| 179 | next if $local{$expected_local}; | ||
| 180 | next if $seen{$rname}++; | ||
| 181 | CORE::push @deletes, ":${rname}"; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | return @deletes; | ||
| 185 | } | ||
| 186 | |||
| 187 | # Read N bytes from a raw C address into a Perl scalar. | ||
| 188 | sub _peek_bytes { | ||
| 189 | my ( $addr, $len ) = @_; | ||
| 190 | my $buf = "\0" x $len; | ||
| 191 | my ($bp) = scalar_to_buffer($buf); | ||
| 192 | memcpy( $bp, $addr, $len ); | ||
| 193 | return $buf; | ||
| 194 | } | ||
| 195 | |||
| 196 | # libgit2 git_remote_push does NOT expand wildcard refspecs (unlike CLI | ||
| 197 | # git). We do it here: for each `+?src:dst` refspec containing `*`, | ||
| 198 | # enumerate matching local refs and emit one explicit refspec per ref. | ||
| 199 | sub _expand_push_refspecs { | ||
| 200 | my ( $self, $refspecs ) = @_; | ||
| 201 | $refspecs //= []; | ||
| 202 | my @out; | ||
| 203 | for my $rs (@$refspecs) { | ||
| 204 | my ( $force, $src, $dst ) = $rs =~ /\A(\+?)([^:]+):(.+)\z/; | ||
| 205 | if ( !defined $src || ( index( $src, '*' ) < 0 && index( $dst, '*' ) < 0 ) ) { | ||
| 206 | CORE::push @out, $rs; | ||
| 207 | next; | ||
| 208 | } | ||
| 209 | my $src_re = quotemeta($src); | ||
| 210 | $src_re =~ s/\\\*/(.*)/; | ||
| 211 | $src_re = qr/\A${src_re}\z/; | ||
| 212 | |||
| 213 | my $names = $self->_owner->reference_names( glob => $src ); | ||
| 214 | for my $name (@$names) { | ||
| 215 | my ($cap) = $name =~ $src_re; | ||
| 216 | next unless defined $cap; | ||
| 217 | my $expanded_dst = $dst; | ||
| 218 | $expanded_dst =~ s/\*/$cap/; | ||
| 219 | CORE::push @out, "${force}${name}:${expanded_dst}"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | return \@out; | ||
| 223 | } | ||
| 224 | |||
| 225 | # ---------- internals ---------- | ||
| 226 | |||
| 227 | # Build a git_strarray pointing into Perl-owned memory. Returns | ||
| 228 | # ($strarray_ptr, $keepalive_scalars_ref). Caller must hold | ||
| 229 | # $keepalive_scalars_ref alive across the C call. | ||
| 230 | sub _build_strarray { | ||
| 231 | my ($refspecs) = @_; | ||
| 232 | $refspecs //= []; | ||
| 233 | Carp::croak "_build_strarray: refspecs must be an arrayref" | ||
| 234 | if ref $refspecs ne 'ARRAY'; | ||
| 235 | # Empty list → NULL strarray pointer, which libgit2 reads as | ||
| 236 | # "use configured refspecs from .git/config". | ||
| 237 | return ( 0, [] ) unless @$refspecs; | ||
| 238 | |||
| 239 | # Copy each string so we have stable storage we control. | ||
| 240 | my @copies = map { "$_" } @$refspecs; | ||
| 241 | my @ptrs; | ||
| 242 | for my $s (@copies) { | ||
| 243 | my ($p) = scalar_to_buffer($s); | ||
| 244 | CORE::push @ptrs, $p; | ||
| 245 | } | ||
| 246 | my $strings_buf = pack 'J*', @ptrs; | ||
| 247 | my ($strings_ptr) = scalar_to_buffer($strings_buf); | ||
| 248 | |||
| 249 | my $strarray = pack 'JJ', $strings_ptr, scalar(@copies); | ||
| 250 | my ($sa_ptr) = scalar_to_buffer($strarray); | ||
| 251 | |||
| 252 | # Keep refs to every buffer that owns memory referenced from $strarray. | ||
| 253 | return ( $sa_ptr, [ \@copies, \$strings_buf, \$strarray ] ); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub _build_fetch_options { | ||
| 257 | my ( $cred_cb, $prune ) = @_; | ||
| 258 | |||
| 259 | my $opts = "\0" x FETCH_OPTIONS_SIZE; | ||
| 260 | my ($opts_ptr) = scalar_to_buffer($opts); | ||
| 261 | check_rc Git::Libgit2::FFI::git_fetch_options_init( | ||
| 262 | $opts_ptr, GIT_FETCH_OPTIONS_VERSION, | ||
| 263 | ); | ||
| 264 | |||
| 265 | my @keep = ( \$opts ); | ||
| 266 | |||
| 267 | if ($cred_cb) { | ||
| 268 | my ( $cb_thunk, $cb_keep ) = _make_credential_thunk($cred_cb); | ||
| 269 | CORE::push @keep, $cb_keep; | ||
| 270 | |||
| 271 | # Write the closure's C pointer into callbacks.credentials. | ||
| 272 | my $cb_ptr_val = Git::Libgit2::FFI::ffi->cast( | ||
| 273 | 'git_credential_acquire_cb' => 'opaque', $cb_thunk, | ||
| 274 | ); | ||
| 275 | my $cb_buf = pack 'J', $cb_ptr_val; | ||
| 276 | my ($cb_buf_ptr) = scalar_to_buffer($cb_buf); | ||
| 277 | memcpy( $opts_ptr + FETCH_OPTS_CALLBACKS_OFFSET + CALLBACKS_CRED_OFFSET, | ||
| 278 | $cb_buf_ptr, 8 ); | ||
| 279 | CORE::push @keep, \$cb_buf; | ||
| 280 | } | ||
| 281 | |||
| 282 | if ( defined $prune ) { | ||
| 283 | my $val = $prune ? 1 : 2; # 1 = PRUNE, 2 = NO_PRUNE | ||
| 284 | my $pb = pack 'l', $val; | ||
| 285 | my ($pbp) = scalar_to_buffer($pb); | ||
| 286 | memcpy( $opts_ptr + FETCH_OPTS_PRUNE_OFFSET, $pbp, 4 ); | ||
| 287 | CORE::push @keep, \$pb; | ||
| 288 | } | ||
| 289 | |||
| 290 | return ( $opts_ptr, \@keep ); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _build_push_options { | ||
| 294 | my ($cred_cb) = @_; | ||
| 295 | |||
| 296 | my $opts = "\0" x PUSH_OPTIONS_SIZE; | ||
| 297 | my ($opts_ptr) = scalar_to_buffer($opts); | ||
| 298 | check_rc Git::Libgit2::FFI::git_push_options_init( | ||
| 299 | $opts_ptr, GIT_PUSH_OPTIONS_VERSION, | ||
| 300 | ); | ||
| 301 | |||
| 302 | my @keep = ( \$opts ); | ||
| 303 | |||
| 304 | if ($cred_cb) { | ||
| 305 | my ( $cb_thunk, $cb_keep ) = _make_credential_thunk($cred_cb); | ||
| 306 | CORE::push @keep, $cb_keep; | ||
| 307 | |||
| 308 | my $cb_ptr_val = Git::Libgit2::FFI::ffi->cast( | ||
| 309 | 'git_credential_acquire_cb' => 'opaque', $cb_thunk, | ||
| 310 | ); | ||
| 311 | my $cb_buf = pack 'J', $cb_ptr_val; | ||
| 312 | my ($cb_buf_ptr) = scalar_to_buffer($cb_buf); | ||
| 313 | memcpy( $opts_ptr + PUSH_OPTS_CALLBACKS_OFFSET + CALLBACKS_CRED_OFFSET, | ||
| 314 | $cb_buf_ptr, 8 ); | ||
| 315 | CORE::push @keep, \$cb_buf; | ||
| 316 | } | ||
| 317 | |||
| 318 | return ( $opts_ptr, \@keep ); | ||
| 319 | } | ||
| 320 | |||
| 321 | # Wrap a user coderef so it conforms to git_credential_acquire_cb. | ||
| 322 | # Returns ($closure, $keepalive). The closure must outlive the C call — | ||
| 323 | # the keepalive bundle is what the Remote method holds onto. | ||
| 324 | sub _make_credential_thunk { | ||
| 325 | my ($user_cb) = @_; | ||
| 326 | my $ffi = Git::Libgit2::FFI::ffi(); | ||
| 327 | |||
| 328 | my $closure = $ffi->closure(sub { | ||
| 329 | my ( $out_ptr, $url, $username_from_url, $allowed_types, $payload ) = @_; | ||
| 330 | my $cred = eval { | ||
| 331 | $user_cb->( | ||
| 332 | url => $url, | ||
| 333 | username_from_url => $username_from_url, | ||
| 334 | allowed_types => $allowed_types, | ||
| 335 | ); | ||
| 336 | }; | ||
| 337 | if ($@) { | ||
| 338 | warn "credential callback died: $@"; | ||
| 339 | return -1; | ||
| 340 | } | ||
| 341 | return GIT_PASSTHROUGH unless defined $cred; | ||
| 342 | Carp::croak "credentials callback must return a Git::Native::Credential" | ||
| 343 | unless ref $cred && $cred->isa('Git::Native::Credential'); | ||
| 344 | |||
| 345 | # Disown the wrapper — libgit2 takes ownership on return 0. | ||
| 346 | my $cred_handle = $cred->_disown; | ||
| 347 | |||
| 348 | # *out_ptr = cred_handle (write 8 bytes of pointer to the address | ||
| 349 | # the caller gave us) | ||
| 350 | my $pkt = pack 'J', $cred_handle; | ||
| 351 | my ($pkt_p) = scalar_to_buffer($pkt); | ||
| 352 | memcpy( $out_ptr, $pkt_p, 8 ); | ||
| 353 | |||
| 354 | return 0; | ||
| 355 | }); | ||
| 356 | |||
| 357 | # `sticky` would survive process-lifetime; we only need until the C | ||
| 358 | # call returns, so just hand the closure to the caller's keepalive. | ||
| 359 | return ( $closure, [ \$closure ] ); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub DEMOLISH { | ||
| 363 | my $self = shift; | ||
| 364 | if ( my $h = $self->{_handle} ) { | ||
| 365 | Git::Libgit2::FFI::git_remote_free($h); | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | 1; | ||
| 370 | |||
| 371 | __END__ |