feat(signature): add bindings and api for default signature

This commit is contained in:
Aleksey Kulikov 2021-09-21 15:28:58 +03:00
parent 72107dea60
commit 3b883c49e3
5 changed files with 55 additions and 9 deletions

View file

@ -50,5 +50,22 @@ Pointer<git_signature> 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<git_signature> defaultSignature(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_signature>>();
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. /// Free an existing signature.
void free(Pointer<git_signature> sig) => libgit2.git_signature_free(sig); void free(Pointer<git_signature> sig) => libgit2.git_signature_free(sig);

View file

@ -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. /// 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). /// If [sorting] isn't provided default will be used (reverse chronological order, like in git).

View file

@ -2,6 +2,7 @@ import 'dart:ffi';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'bindings/libgit2_bindings.dart'; import 'bindings/libgit2_bindings.dart';
import 'bindings/signature.dart' as bindings; import 'bindings/signature.dart' as bindings;
import 'repository.dart';
import 'util.dart'; import 'util.dart';
class Signature { class Signature {
@ -39,6 +40,15 @@ class Signature {
/// Pointer to memory address for allocated signature object. /// Pointer to memory address for allocated signature object.
Pointer<git_signature> get pointer => _signaturePointer; Pointer<git_signature> 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. /// Returns full name of the author.
String get name => _signaturePointer.ref.name.cast<Utf8>().toDartString(); String get name => _signaturePointer.ref.name.cast<Utf8>().toDartString();

View file

@ -374,6 +374,19 @@ void main() {
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
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();
});
}); });
}); });
} }

View file

@ -26,18 +26,16 @@ void main() {
}); });
test('successfully creates without provided time and offset', () { test('successfully creates without provided time and offset', () {
final defaultSignature = final sig = Signature.create(name: 'Name', email: 'email@example.com');
Signature.create(name: 'Name', email: 'email@example.com'); expect(sig, isA<Signature>());
expect(defaultSignature, isA<Signature>()); expect(sig.name, 'Name');
expect(defaultSignature.name, 'Name'); expect(sig.email, 'email@example.com');
expect(defaultSignature.email, 'email@example.com');
expect( expect(
defaultSignature.time - sig.time - (DateTime.now().millisecondsSinceEpoch / 1000).truncate(),
(DateTime.now().millisecondsSinceEpoch / 1000).truncate(),
lessThan(5), lessThan(5),
); );
expect(defaultSignature.offset, 180); expect(sig.offset, 180);
defaultSignature.free(); sig.free();
}); });
test('returns correct values', () { test('returns correct values', () {