feat(repository): add api method for revparse_single

This commit is contained in:
Aleksey Kulikov 2021-09-01 10:42:24 +03:00
parent 2e0486c641
commit 3b13646b1b
5 changed files with 43 additions and 5 deletions

View file

@ -0,0 +1,6 @@
import 'dart:ffi';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Get the object type of an object.
int type(Pointer<git_object> obj) => libgit2.git_object_type(obj);

View file

@ -511,6 +511,8 @@ Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
} }
/// Find a single object, as specified by a [spec] string. /// Find a single object, as specified by a [spec] string.
/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
/// for information on the syntax accepted.
/// ///
/// The returned object should be released when no longer needed. /// The returned object should be released when no longer needed.
/// ///

View file

@ -17,3 +17,5 @@ enum GitFilemode { undreadable, tree, blob, blobExecutable, link, commit }
/// [reverse] Iterate through the repository contents in reverse /// [reverse] Iterate through the repository contents in reverse
/// order; this sorting mode can be combined with any of the above. /// order; this sorting mode can be combined with any of the above.
enum GitSort { none, topological, time, reverse } enum GitSort { none, topological, time, reverse }
enum GitObject { commit, tree, blob, tag }

View file

@ -1,15 +1,14 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:libgit2dart/src/enums.dart'; import 'bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/revwalk.dart'; import 'bindings/repository.dart' as bindings;
import 'commit.dart'; import 'commit.dart';
import 'config.dart'; import 'config.dart';
import 'index.dart'; import 'index.dart';
import 'odb.dart'; import 'odb.dart';
import 'oid.dart'; import 'oid.dart';
import 'reference.dart'; import 'reference.dart';
import 'bindings/libgit2_bindings.dart'; import 'revwalk.dart';
import 'bindings/repository.dart' as bindings; import 'enums.dart';
import 'util.dart'; import 'util.dart';
class Repository { class Repository {
@ -324,6 +323,17 @@ class Repository {
return Commit.lookup(this, oid); return Commit.lookup(this, oid);
} }
/// Find a single object, as specified by a [spec] string.
/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
/// for information on the syntax accepted.
///
/// The returned object should be released when no longer needed.
///
/// Throws a [LibGit2Error] if error occured.
Commit revParseSingle(String spec) {
return Commit(bindings.revParseSingle(_repoPointer, spec).cast());
}
/// Returns the list of commits starting from provided [oid]. /// Returns the list of commits starting from provided [oid].
/// ///
/// 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

@ -199,6 +199,24 @@ void main() {
} }
}); });
test('returns commit with different spec strings', () {
const headSHA = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8';
const parentSHA = 'c68ff54aabf660fcdd9a2838d401583fe31249e3';
final headCommit = repo.revParseSingle('HEAD');
expect(headCommit.id.sha, headSHA);
final parentCommit = repo.revParseSingle('HEAD^');
expect(parentCommit.id.sha, parentSHA);
final initCommit = repo.revParseSingle('@{-1}');
expect(initCommit.id.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4');
headCommit.free();
parentCommit.free();
initCommit.free();
});
group('.discover()', () { group('.discover()', () {
test('discovers repository', () async { test('discovers repository', () async {
final subDir = '${tmpDir}subdir1/subdir2/'; final subDir = '${tmpDir}subdir1/subdir2/';