diff --git a/lib/src/bindings/signature.dart b/lib/src/bindings/signature.dart index abc301c..fcd0c6c 100644 --- a/lib/src/bindings/signature.dart +++ b/lib/src/bindings/signature.dart @@ -50,5 +50,22 @@ Pointer now(String name, String email) { } } +/// Create a new action signature with default user and now timestamp. +/// +/// This looks up the user.name and user.email from the configuration and uses the +/// current time as the timestamp, and creates a new signature based on that information. +/// +/// Throws a [LibGit2Error] if error occured. +Pointer defaultSignature(Pointer repo) { + final out = calloc>(); + final error = libgit2.git_signature_default(out, repo); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } +} + /// Free an existing signature. void free(Pointer sig) => libgit2.git_signature_free(sig); diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 7bdc363..6ae61ed 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -361,6 +361,14 @@ class Repository { } } + /// Creates a new action signature with default user and now timestamp. + /// + /// This looks up the user.name and user.email from the configuration and uses the + /// current time as the timestamp, and creates a new signature based on that information. + /// + /// Throws a [LibGit2Error] if error occured. + Signature get defaultSignature => Signature.defaultSignature(this); + /// Returns the list of commits starting from provided [sha] hex string. /// /// If [sorting] isn't provided default will be used (reverse chronological order, like in git). diff --git a/lib/src/signature.dart b/lib/src/signature.dart index 948b3f8..925e93a 100644 --- a/lib/src/signature.dart +++ b/lib/src/signature.dart @@ -2,6 +2,7 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'bindings/libgit2_bindings.dart'; import 'bindings/signature.dart' as bindings; +import 'repository.dart'; import 'util.dart'; class Signature { @@ -39,6 +40,15 @@ class Signature { /// Pointer to memory address for allocated signature object. Pointer get pointer => _signaturePointer; + /// Creates a new action signature with default user and now timestamp. + /// + /// This looks up the user.name and user.email from the configuration and uses the + /// current time as the timestamp, and creates a new signature based on that information. + /// + /// Throws a [LibGit2Error] if error occured. + static Signature defaultSignature(Repository repo) => + Signature(bindings.defaultSignature(repo.pointer)); + /// Returns full name of the author. String get name => _signaturePointer.ref.name.cast().toDartString(); diff --git a/test/repository_test.dart b/test/repository_test.dart index 7a047c9..d5ef3c0 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -374,6 +374,19 @@ void main() { throwsA(isA()), ); }); + + test('returns default signature', () { + final config = repo.config; + config['user.name'] = 'Some Name'; + config['user.email'] = 'some@email.com'; + + final signature = repo.defaultSignature; + expect(signature.name, 'Some Name'); + expect(signature.email, 'some@email.com'); + + signature.free(); + config.free(); + }); }); }); } diff --git a/test/signature_test.dart b/test/signature_test.dart index bee634c..a152705 100644 --- a/test/signature_test.dart +++ b/test/signature_test.dart @@ -26,18 +26,16 @@ void main() { }); test('successfully creates without provided time and offset', () { - final defaultSignature = - Signature.create(name: 'Name', email: 'email@example.com'); - expect(defaultSignature, isA()); - expect(defaultSignature.name, 'Name'); - expect(defaultSignature.email, 'email@example.com'); + final sig = Signature.create(name: 'Name', email: 'email@example.com'); + expect(sig, isA()); + expect(sig.name, 'Name'); + expect(sig.email, 'email@example.com'); expect( - defaultSignature.time - - (DateTime.now().millisecondsSinceEpoch / 1000).truncate(), + sig.time - (DateTime.now().millisecondsSinceEpoch / 1000).truncate(), lessThan(5), ); - expect(defaultSignature.offset, 180); - defaultSignature.free(); + expect(sig.offset, 180); + sig.free(); }); test('returns correct values', () {