| 1 | # ABSTRACT: A Git author/committer signature | ||
| 2 | |||
| 3 | package Git::Native::Signature; | ||
| 4 | use Moo; | ||
| 5 | use Git::Libgit2 qw( check_rc ); | ||
| 6 | use Git::Libgit2::FFI (); | ||
| 7 | |||
| 8 | has name => ( is => 'ro', required => 1 ); | ||
| 9 | has email => ( is => 'ro', required => 1 ); | ||
| 10 | has when => ( is => 'ro' ); # epoch seconds; default = now | ||
| 11 | has offset => ( is => 'ro', default => 0 ); # minutes | ||
| 12 | |||
| 13 | # Underlying git_signature*; allocated lazily, freed in DESTROY. | ||
| 14 | has _handle => ( | ||
| 15 | is => 'lazy', | ||
| 16 | builder => '_build_handle', | ||
| 17 | clearer => '_clear_handle', | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub _build_handle { | ||
| 21 | my $self = shift; | ||
| 22 | my $sig; | ||
| 23 | if ( defined $self->when ) { | ||
| 24 | check_rc Git::Libgit2::FFI::git_signature_new( | ||
| 25 | \$sig, $self->name, $self->email, $self->when, $self->offset, | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | check_rc Git::Libgit2::FFI::git_signature_now( | ||
| 30 | \$sig, $self->name, $self->email, | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | return $sig; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub DEMOLISH { | ||
| 37 | my $self = shift; | ||
| 38 | Git::Libgit2::FFI::git_signature_free( $self->{_handle} ) | ||
| 39 | if $self->{_handle}; | ||
| 40 | } | ||
| 41 | |||
| 42 | 1; | ||
| 43 | |||
| 44 | __END__ |