feat(reference): add ability to get log of reference

This commit is contained in:
Aleksey Kulikov 2021-08-06 12:31:07 +03:00
parent a97dcaa0d3
commit 58fa54f24a
5 changed files with 30 additions and 7 deletions

View file

@ -43,12 +43,13 @@ String entryMessage(Pointer<git_reflog_entry> entry) {
return result.cast<Utf8>().toDartString(); return result.cast<Utf8>().toDartString();
} }
/// Get the committer of this entry. /// Get the committer of this entry (name, email, seconds from epoch).
Map<String, String> entryCommiter(Pointer<git_reflog_entry> entry) { Map<String, Object> entryCommiter(Pointer<git_reflog_entry> entry) {
final result = libgit2.git_reflog_entry_committer(entry); final result = libgit2.git_reflog_entry_committer(entry);
var committer = <String, String>{}; var committer = <String, Object>{};
committer['name'] = result.ref.name.cast<Utf8>().toDartString(); committer['name'] = result.ref.name.cast<Utf8>().toDartString();
committer['email'] = result.ref.email.cast<Utf8>().toDartString(); committer['email'] = result.ref.email.cast<Utf8>().toDartString();
committer['when'] = result.ref.when.time;
return committer; return committer;
} }

View file

@ -2,6 +2,7 @@ import 'dart:ffi';
import 'bindings/libgit2_bindings.dart'; import 'bindings/libgit2_bindings.dart';
import 'bindings/reference.dart' as bindings; import 'bindings/reference.dart' as bindings;
import 'oid.dart'; import 'oid.dart';
import 'reflog.dart';
import 'util.dart'; import 'util.dart';
enum ReferenceType { direct, symbolic } enum ReferenceType { direct, symbolic }
@ -145,6 +146,18 @@ class Reference {
return bindings.hasLog(repo, name); return bindings.hasLog(repo, name);
} }
/// Returns a list with entries of reference log.
List<RefLogEntry> get log {
final reflog = RefLog(this);
var log = <RefLogEntry>[];
for (var i = 0; i < reflog.count; i++) {
log.add(reflog.entryAt(i));
}
return log;
}
/// Checks if a reference is a local branch. /// Checks if a reference is a local branch.
bool get isBranch => bindings.isBranch(_refPointer); bool get isBranch => bindings.isBranch(_refPointer);

View file

@ -47,6 +47,6 @@ class RefLogEntry {
/// Returns the log message. /// Returns the log message.
String get message => bindings.entryMessage(_entryPointer); String get message => bindings.entryMessage(_entryPointer);
/// Returns the committer of this entry. /// Returns the committer of this entry (name, email, seconds from epoch).
Map<String, String> get committer => bindings.entryCommiter(_entryPointer); Map<String, Object> get committer => bindings.entryCommiter(_entryPointer);
} }

View file

@ -82,7 +82,8 @@ void main() {
final reflogEntry = reflog.entryAt(0); final reflogEntry = reflog.entryAt(0);
expect(reflogEntry.message, 'log message'); expect(reflogEntry.message, 'log message');
expect(reflogEntry.committer, {'name': 'name', 'email': 'email'}); expect(reflogEntry.committer['name'], 'name');
expect(reflogEntry.committer['email'], 'email');
reflog.free(); reflog.free();
ref.delete(); ref.delete();
@ -222,7 +223,8 @@ void main() {
final reflogEntry = reflog.entryAt(0); final reflogEntry = reflog.entryAt(0);
expect(reflogEntry.message, 'log message'); expect(reflogEntry.message, 'log message');
expect(reflogEntry.committer, {'name': 'name', 'email': 'email'}); expect(reflogEntry.committer['name'], 'name');
expect(reflogEntry.committer['email'], 'email');
// set HEAD back to master // set HEAD back to master
repo repo
@ -335,6 +337,12 @@ void main() {
}); });
}); });
test('returns log for reference', () {
final ref = repo.getReference('refs/heads/master');
expect(ref.log.last.message, 'commit (initial): init');
ref.free();
});
group('isValidName()', () { group('isValidName()', () {
test('returns true for valid names', () { test('returns true for valid names', () {
expect(Reference.isValidName('HEAD'), true); expect(Reference.isValidName('HEAD'), true);

View file

@ -51,6 +51,7 @@ void main() {
final entry = reflog.entryAt(0); final entry = reflog.entryAt(0);
expect(entry.committer['name'], 'Aleksey Kulikov'); expect(entry.committer['name'], 'Aleksey Kulikov');
expect(entry.committer['email'], 'skinny.mind@gmail.com'); expect(entry.committer['email'], 'skinny.mind@gmail.com');
expect(entry.committer['when'], 1626091184);
}); });
}); });
} }