| 1 | # ABSTRACT: A libgit2 OID (20-byte SHA-1) | ||
| 2 | |||
| 3 | package Git::Native::Oid; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | use Moo; | ||
| 6 | use Carp (); | ||
| 7 | use Git::Libgit2 qw( oid_from_hex oid_to_hex ); | ||
| 8 | use Git::Libgit2::FFI (); | ||
| 9 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 10 | |||
| 11 | # Every wrapper method that accepts "Oid|hex" funnels the caller's string in | ||
| 12 | # here (Repository->object / tree / commit / blob / commit_create / tag_create / | ||
| 13 | # reference_create, Reference->set_target, Revwalker->push_oid / hide_oid, | ||
| 14 | # TreeBuilder->insert), so a croak from from_hex would otherwise blame the | ||
| 15 | # wrapper's line inside this distribution instead of the line that passed the | ||
| 16 | # bad string. @CARP_NOT makes Carp walk past those frames to the caller - the | ||
| 17 | # same diagnosis object_by_prefix and commit_create give for their own argument | ||
| 18 | # checks. A caller class missing from this list only gets a worse blame line, | ||
| 19 | # never a different failure. | ||
| 20 | our @CARP_NOT = qw( | ||
| 21 | Git::Native::Repository | ||
| 22 | Git::Native::Reference | ||
| 23 | Git::Native::Revwalker | ||
| 24 | Git::Native::TreeBuilder | ||
| 25 | ); | ||
| 26 | |||
| 27 | # Holds the raw 20-byte SHA. All other forms (hex, short, pointer) are | ||
| 28 | # derived. The scalar lives as long as the Oid object - that's the | ||
| 29 | # anchor libgit2 dereferences via the pointer we hand it. | ||
| 30 | has raw => ( | ||
| 31 | is => 'ro', | ||
| 32 | required => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | # A malformed OID string is a caller error, not a libgit2 error, and dies as a | ||
| 36 | # croak on purpose - the reasoning is in the POD under from_hex. The check is | ||
| 37 | # duplicated from Git::Libgit2::oid_from_hex (which croaks to the same effect) | ||
| 38 | # so the message names Git::Native::Oid->from_hex rather than an FFI helper the | ||
| 39 | # caller never called; oid_from_hex's own croak is unreachable from here. | ||
| 40 | sub from_hex { | ||
| 41 | my ( $class, $hex ) = @_; | ||
| 42 | unless ( defined $hex && $hex =~ /\A[0-9a-fA-F]{40}\z/ ) { | ||
| 43 | my $got = defined $hex | ||
| 44 | ? sprintf( "'%s' (%d characters)", $hex, length $hex ) | ||
| 45 | : 'undef'; | ||
| 46 | # A short run of hex digits is almost always an abbreviated OID, which is | ||
| 47 | # a repository lookup rather than a conversion - name the method that does | ||
| 48 | # it instead of leaving the caller to find it. | ||
| 49 | my $hint = defined $hex && $hex =~ /\A[0-9a-fA-F]{1,39}\z/ | ||
| 50 | ? ', use Git::Native::Repository->object_by_prefix to resolve an' | ||
| 51 | . ' abbreviated OID against a repository' | ||
| 52 | : ''; | ||
| 53 | Carp::croak | ||
| 54 | "Git::Native::Oid->from_hex requires a 40-character hex OID, got $got$hint"; | ||
| 55 | } | ||
| 56 | return $class->new( raw => oid_from_hex($hex) ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub from_raw { | ||
| 60 | my ( $class, $raw ) = @_; | ||
| 61 | Carp::croak 'Git::Native::Oid->from_raw requires exactly 20 bytes of raw OID, got ' | ||
| 62 | . ( defined $raw ? length($raw) . ' bytes' : 'undef' ) | ||
| 63 | unless defined $raw && length($raw) == 20; | ||
| 64 | return $class->new( raw => $raw ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # Construct from a pointer libgit2 returned (e.g. git_reference_target). | ||
| 68 | # We copy the 20 bytes out so the resulting Oid owns its memory and | ||
| 69 | # doesn't dangle when the source handle is freed. | ||
| 70 | sub from_ptr { | ||
| 71 | my ( $class, $ptr ) = @_; | ||
| 72 | Carp::croak 'Git::Native::Oid->from_ptr: null pointer' unless $ptr; | ||
| 73 | my $copy = Git::Libgit2::FFI::ffi()->cast( 'opaque', 'string(20)', $ptr ); | ||
| 74 | return $class->new( raw => $copy ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub hex { | ||
| 78 | my $self = shift; | ||
| 79 | my ($ptr) = scalar_to_buffer( $self->{raw} ); | ||
| 80 | return oid_to_hex($ptr); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub short { | ||
| 84 | my ( $self, $n ) = @_; | ||
| 85 | $n //= 7; | ||
| 86 | return substr( $self->hex, 0, $n ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub ptr { | ||
| 90 | my $self = shift; | ||
| 91 | my ($p) = scalar_to_buffer( $self->{raw} ); | ||
| 92 | return $p; | ||
| 93 | } | ||
| 94 | |||
| 95 | use overload | ||
| 96 | '""' => sub { $_[0]->hex }, | ||
| 97 | # Compare on the hex form so an Oid is equal both to another Oid and to | ||
| 98 | # its hex string (the common `$ref->target eq $known_sha` pattern). raw-vs- | ||
| 99 | # raw would also work for Oid pairs, but raw-vs-hex-string never matches. | ||
| 100 | 'eq' => sub { $_[0]->hex eq (ref $_[1] ? $_[1]->hex : $_[1]) }, | ||
| 101 | fallback => 1; | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ |