mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
refactor(revparse)!: add ability to get different git objects based on spec (#46)
This commit is contained in:
parent
e83d6ab29a
commit
d0f7746a01
3 changed files with 48 additions and 19 deletions
|
@ -1,6 +1,7 @@
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'package:libgit2dart/libgit2dart.dart';
|
import 'package:libgit2dart/libgit2dart.dart';
|
||||||
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||||
|
import 'package:libgit2dart/src/bindings/object.dart' as object_bindings;
|
||||||
import 'package:libgit2dart/src/bindings/revparse.dart' as bindings;
|
import 'package:libgit2dart/src/bindings/revparse.dart' as bindings;
|
||||||
|
|
||||||
class RevParse {
|
class RevParse {
|
||||||
|
@ -40,18 +41,35 @@ class RevParse {
|
||||||
/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
|
/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
|
||||||
/// for information on the syntax accepted.
|
/// for information on the syntax accepted.
|
||||||
///
|
///
|
||||||
|
/// Returned object should be explicitly downcasted to one of four of git
|
||||||
|
/// object types.
|
||||||
|
///
|
||||||
|
/// ```dart
|
||||||
|
/// final commit = RevParse.single(repo: repo, spec: 'HEAD') as Commit;
|
||||||
|
/// final tree = RevParse.single(repo: repo, spec: 'HEAD^{tree}') as Tree;
|
||||||
|
/// final blob = RevParse.single(repo: repo, spec: 'HEAD:file.txt') as Blob;
|
||||||
|
/// final tag = RevParse.single(repo: repo, spec: 'v1.0') as Tag;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// **IMPORTANT**: Should be freed to release allocated memory.
|
/// **IMPORTANT**: Should be freed to release allocated memory.
|
||||||
///
|
///
|
||||||
/// Throws a [LibGit2Error] if error occured.
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
static Commit single({required Repository repo, required String spec}) {
|
static Object single({required Repository repo, required String spec}) {
|
||||||
return Commit(
|
final object = bindings.revParseSingle(
|
||||||
bindings
|
|
||||||
.revParseSingle(
|
|
||||||
repoPointer: repo.pointer,
|
repoPointer: repo.pointer,
|
||||||
spec: spec,
|
spec: spec,
|
||||||
)
|
|
||||||
.cast(),
|
|
||||||
);
|
);
|
||||||
|
final objectType = object_bindings.type(object);
|
||||||
|
|
||||||
|
if (objectType == GitObject.commit.value) {
|
||||||
|
return Commit(object.cast());
|
||||||
|
} else if (objectType == GitObject.tree.value) {
|
||||||
|
return Tree(object.cast());
|
||||||
|
} else if (objectType == GitObject.blob.value) {
|
||||||
|
return Blob(object.cast());
|
||||||
|
} else {
|
||||||
|
return Tag(object.cast());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a revision string for from, to, and intent.
|
/// Parses a revision string for from, to, and intent.
|
||||||
|
|
|
@ -23,19 +23,30 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
group('revParse', () {
|
group('revParse', () {
|
||||||
test('.single() returns commit with different spec strings', () {
|
test('.single() returns correct objects with different spec strings', () {
|
||||||
final headCommit = RevParse.single(repo: repo, spec: 'HEAD');
|
final commit = RevParse.single(repo: repo, spec: 'HEAD') as Commit;
|
||||||
expect(headCommit.oid.sha, headSHA);
|
expect(commit, isA<Commit>());
|
||||||
|
expect(commit.oid.sha, headSHA);
|
||||||
|
|
||||||
final parentCommit = RevParse.single(repo: repo, spec: 'HEAD^');
|
final tree = RevParse.single(repo: repo, spec: 'HEAD^{tree}') as Tree;
|
||||||
expect(parentCommit.oid.sha, parentSHA);
|
expect(tree, isA<Tree>());
|
||||||
|
expect(tree.length, isNonZero);
|
||||||
|
|
||||||
final initCommit = RevParse.single(repo: repo, spec: '@{-1}');
|
final blob = RevParse.single(
|
||||||
expect(initCommit.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4');
|
repo: repo,
|
||||||
|
spec: 'HEAD:feature_file',
|
||||||
|
) as Blob;
|
||||||
|
expect(blob, isA<Blob>());
|
||||||
|
expect(blob.content, 'Feature edit\n');
|
||||||
|
|
||||||
headCommit.free();
|
final tag = RevParse.single(repo: repo, spec: 'v0.2') as Tag;
|
||||||
parentCommit.free();
|
expect(tag, isA<Tag>());
|
||||||
initCommit.free();
|
expect(tag.message, 'annotated tag\n');
|
||||||
|
|
||||||
|
commit.free();
|
||||||
|
tree.free();
|
||||||
|
blob.free();
|
||||||
|
tag.free();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('.single() throws when spec string not found or invalid', () {
|
test('.single() throws when spec string not found or invalid', () {
|
||||||
|
|
|
@ -55,7 +55,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('creates worktree at provided path from provided reference', () {
|
test('creates worktree at provided path from provided reference', () {
|
||||||
final head = RevParse.single(repo: repo, spec: 'HEAD');
|
final head = RevParse.single(repo: repo, spec: 'HEAD') as Commit;
|
||||||
final worktreeBranch = Branch.create(
|
final worktreeBranch = Branch.create(
|
||||||
repo: repo,
|
repo: repo,
|
||||||
name: 'v1',
|
name: 'v1',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue