add name_to_id binding and headCommit to repo

- implemented git_reference_name_to_id binding
- created repository.headCommit getter
This commit is contained in:
Viktor Borisov 2023-02-25 02:43:34 +07:00
parent b66662f33c
commit 35afc87b58
5 changed files with 58 additions and 0 deletions

View file

@ -449,5 +449,31 @@ Pointer<git_reference> duplicate(Pointer<git_reference> source) {
return result; return result;
} }
/// Lookup a reference by name and resolve immediately to OID.
///
/// This function provides a quick way to resolve a reference name straight
/// through to the object id that it refers to. This avoids having to
/// allocate or free any `git_reference` objects for simple situations.
///
/// The name will be checked for validity.
/// See [createSymbolic] for rules about valid names.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> nameToId({
required Pointer<git_repository> repoPointer,
required String refName,
}) {
final result = calloc<git_oid>();
final nameC = refName.toChar();
final error = libgit2.git_reference_name_to_id(result, repoPointer, nameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return result;
}
}
/// Free the given reference. /// Free the given reference.
void free(Pointer<git_reference> ref) => libgit2.git_reference_free(ref); void free(Pointer<git_reference> ref) => libgit2.git_reference_free(ref);

View file

@ -191,6 +191,24 @@ class Reference extends Equatable {
bindings.ensureLog(repoPointer: repo.pointer, refName: refName); bindings.ensureLog(repoPointer: repo.pointer, refName: refName);
} }
/// Lookup a reference by name and resolve immediately to OID.
///
/// This function provides a quick way to resolve a reference name straight
/// through to the object id that it refers to. This avoids having to
/// allocate or free any `git_reference` objects for simple situations.
///
/// The name will be checked for validity.
/// See [createSymbolic] for rules about valid names.
///
/// Throws a [LibGit2Error] if error occured.
static Oid nameToId({
required Repository repo,
required String refName,
}) {
final res = bindings.nameToId(repoPointer: repo.pointer, refName: refName);
return Oid(res);
}
/// Creates a copy of an existing reference. /// Creates a copy of an existing reference.
Reference duplicate() => Reference(bindings.duplicate(_refPointer)); Reference duplicate() => Reference(bindings.duplicate(_refPointer));

View file

@ -339,6 +339,11 @@ class Repository extends Equatable {
); );
} }
/// Retrieve the commit that HEAD is currently pointing to
Commit get headCommit {
return Commit.lookup(repo: this, oid: head.target);
}
/// Removes all the metadata associated with an ongoing command like /// Removes all the metadata associated with an ongoing command like
/// merge, revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. /// merge, revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc.
/// ///

View file

@ -530,6 +530,11 @@ void main() {
); );
}); });
test('get oid by name', () {
final oid = Reference.nameToId(repo: repo, refName: 'HEAD');
expect(oid, repo.head.target);
});
test('manually releases allocated memory', () { test('manually releases allocated memory', () {
final ref = Reference.lookup(repo: repo, name: 'refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
expect(() => ref.free(), returnsNormally); expect(() => ref.free(), returnsNormally);

View file

@ -246,6 +246,10 @@ void main() {
expect(() => repo.free(), returnsNormally); expect(() => repo.free(), returnsNormally);
}); });
test('get head commit', () {
expect(repo.headCommit.oid.sha, lastCommit);
});
test('returns string representation of Repository object', () { test('returns string representation of Repository object', () {
expect(repo.toString(), contains('Repository{')); expect(repo.toString(), contains('Repository{'));
}); });