diff --git a/lib/src/bindings/commit.dart b/lib/src/bindings/commit.dart index 7a2cb69..d1aa89f 100644 --- a/lib/src/bindings/commit.dart +++ b/lib/src/bindings/commit.dart @@ -347,18 +347,10 @@ Pointer treeOid(Pointer commit) { } /// Get the tree pointed to by a commit. -/// -/// Throws a [LibGit2Error] if error occured. Pointer tree(Pointer commit) { final out = calloc>(); - final error = libgit2.git_commit_tree(out, commit); - - if (error < 0) { - calloc.free(out); - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } + libgit2.git_commit_tree(out, commit); + return out.value; } /// Reverts the given commit, producing changes in the index and working diff --git a/lib/src/bindings/index.dart b/lib/src/bindings/index.dart index e7d7033..faa77ce 100644 --- a/lib/src/bindings/index.dart +++ b/lib/src/bindings/index.dart @@ -525,9 +525,5 @@ void conflictCleanup(Pointer index) { } } -/// Get the repository this index relates to. -Pointer owner(Pointer index) => - libgit2.git_index_owner(index); - /// Free an existing index object. void free(Pointer index) => libgit2.git_index_free(index); diff --git a/lib/src/bindings/tree.dart b/lib/src/bindings/tree.dart index c7f6b33..33d1f10 100644 --- a/lib/src/bindings/tree.dart +++ b/lib/src/bindings/tree.dart @@ -26,10 +26,6 @@ Pointer lookup({ } } -/// Get the repository that contains the tree. -Pointer owner(Pointer tree) => - libgit2.git_tree_owner(tree); - /// Lookup a tree entry by its position in the tree. /// /// This returns a tree entry that is owned by the tree. You don't have to free diff --git a/lib/src/checkout.dart b/lib/src/checkout.dart index eb4aa5c..ced1b95 100644 --- a/lib/src/checkout.dart +++ b/lib/src/checkout.dart @@ -3,7 +3,7 @@ import 'package:libgit2dart/src/bindings/checkout.dart' as bindings; import 'package:libgit2dart/src/bindings/object.dart' as object_bindings; class Checkout { - const Checkout._(); + const Checkout._(); // coverage:ignore-line /// Updates files in the index and the working tree to match the content of /// the commit pointed at by HEAD. diff --git a/lib/src/commit.dart b/lib/src/commit.dart index 6d8c487..73fcead 100644 --- a/lib/src/commit.dart +++ b/lib/src/commit.dart @@ -314,11 +314,11 @@ class Commit { /// /// Note that a commit is not considered a descendant of itself, in contrast /// to `git merge-base --is-ancestor`. - bool descendantOf(Commit ancestor) { + bool descendantOf(Oid ancestor) { return graph_bindings.descendantOf( repoPointer: bindings.owner(_commitPointer), commitPointer: bindings.id(_commitPointer), - ancestorPointer: bindings.id(ancestor.pointer), + ancestorPointer: ancestor.pointer, ); } diff --git a/lib/src/libgit2.dart b/lib/src/libgit2.dart index 3f52a8e..f935d42 100644 --- a/lib/src/libgit2.dart +++ b/lib/src/libgit2.dart @@ -5,7 +5,7 @@ import 'package:libgit2dart/libgit2dart.dart'; import 'package:libgit2dart/src/util.dart'; class Libgit2 { - Libgit2._(); + Libgit2._(); // coverage:ignore-line /// Returns libgit2 version number. static String get version { diff --git a/lib/src/merge.dart b/lib/src/merge.dart index fefc36d..98f78b7 100644 --- a/lib/src/merge.dart +++ b/lib/src/merge.dart @@ -5,7 +5,7 @@ import 'package:libgit2dart/src/bindings/merge.dart' as bindings; import 'package:libgit2dart/src/util.dart'; class Merge { - const Merge._(); + const Merge._(); // coverage:ignore-line /// Finds a merge base between [commits]. /// diff --git a/test/branch_test.dart b/test/branch_test.dart index 041b016..d8bdbd1 100644 --- a/test/branch_test.dart +++ b/test/branch_test.dart @@ -29,30 +29,39 @@ void main() { test('returns a list of all branches', () { const branchesExpected = ['feature', 'master', 'origin/master']; final branches = Branch.list(repo: repo); + final aliasBranches = repo.branches; for (var i = 0; i < branches.length; i++) { expect(branches[i].name, branchesExpected[i]); + expect(aliasBranches[i].name, branchesExpected[i]); branches[i].free(); + aliasBranches[i].free(); } }); test('returns a list of local branches', () { const branchesExpected = ['feature', 'master']; final branches = Branch.list(repo: repo, type: GitBranch.local); + final aliasBranches = repo.branchesLocal; for (var i = 0; i < branches.length; i++) { expect(branches[i].name, branchesExpected[i]); + expect(aliasBranches[i].name, branchesExpected[i]); branches[i].free(); + aliasBranches[i].free(); } }); test('returns a list of remote branches', () { const branchesExpected = ['origin/master']; final branches = Branch.list(repo: repo, type: GitBranch.remote); + final aliasBranches = repo.branchesRemote; for (var i = 0; i < branches.length; i++) { expect(branches[i].name, branchesExpected[i]); + expect(aliasBranches[i].name, branchesExpected[i]); branches[i].free(); + aliasBranches[i].free(); } }); @@ -226,7 +235,7 @@ void main() { test('throws when trying to get upstream remote of a remote branch', () { final branch = Branch.list(repo: repo, type: GitBranch.remote).first; - expect(() => branch.upstreamName, throwsA(isA())); + expect(() => branch.upstreamRemote, throwsA(isA())); branch.free(); }); diff --git a/test/commit_test.dart b/test/commit_test.dart index 639a5e2..4d77b58 100644 --- a/test/commit_test.dart +++ b/test/commit_test.dart @@ -107,6 +107,18 @@ void main() { ); }); + test('checks if commit is a descendant of another commit', () { + final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e8']); + final commit2 = Commit.lookup(repo: repo, oid: repo['78b8bf12']); + + expect(commit1.descendantOf(commit2.oid), true); + expect(commit1.descendantOf(commit1.oid), false); + expect(commit2.descendantOf(commit1.oid), false); + + commit1.free(); + commit2.free(); + }); + test('creates commit', () { final parent = Commit.lookup(repo: repo, oid: tip); final oid = Commit.create( @@ -139,6 +151,33 @@ void main() { parent.free(); }); + test('writes commit without parents into the buffer', () { + final parent = Commit.lookup(repo: repo, oid: tip); + final commit = Commit.createBuffer( + repo: repo, + updateRef: 'HEAD', + message: message, + author: author, + committer: committer, + tree: tree, + parents: [], + ); + + const expected = """ +tree a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f +author Author Name 123 +0000 +committer Commiter 124 +0000 + +Commit message. + +Some description. +"""; + + expect(commit, expected); + + parent.free(); + }); + test('writes commit into the buffer', () { final parent = Commit.lookup(repo: repo, oid: tip); final commit = Commit.createBuffer( diff --git a/test/diff_test.dart b/test/diff_test.dart index 1d27832..0c7b30f 100644 --- a/test/diff_test.dart +++ b/test/diff_test.dart @@ -86,6 +86,20 @@ void main() { 'subdir/modified_file', ]; + const treeToEmptyTree = [ + 'current_file', + 'file_deleted', + 'modified_file', + 'staged_changes', + 'staged_changes_file_deleted', + 'staged_changes_file_modified', + 'staged_delete', + 'staged_delete_file_modified', + 'subdir/current_file', + 'subdir/deleted_file', + 'subdir/modified_file', + ]; + const patchText = """ diff --git a/subdir/modified_file b/subdir/modified_file index e69de29..c217c63 100644 @@ -150,6 +164,25 @@ index e69de29..c217c63 100644 index.free(); }); + test('returns diff between index and empty tree', () { + final index = repo.index; + final head = repo.head; + final commit = Commit.lookup(repo: repo, oid: head.target); + final tree = commit.tree; + final diff = Diff.treeToIndex(repo: repo, tree: null, index: index); + + expect(diff.length, 12); + for (var i = 0; i < diff.deltas.length; i++) { + expect(diff.deltas[i].newFile.path, indexToIndex[i]); + } + + commit.free(); + head.free(); + tree.free(); + diff.free(); + index.free(); + }); + test('returns diff between tree and workdir', () { final head = repo.head; final commit = Commit.lookup(repo: repo, oid: head.target); @@ -223,6 +256,42 @@ index e69de29..c217c63 100644 diff.free(); }); + test('returns diff between tree and empty tree', () { + final head = repo.head; + final commit = Commit.lookup(repo: repo, oid: head.target); + final tree = commit.tree; + + final diff = Diff.treeToTree(repo: repo, oldTree: tree, newTree: null); + + expect(diff.length, 11); + for (var i = 0; i < diff.deltas.length; i++) { + expect(diff.deltas[i].newFile.path, treeToEmptyTree[i]); + } + + commit.free(); + head.free(); + tree.free(); + diff.free(); + }); + + test('returns diff between empty tree and tree', () { + final head = repo.head; + final commit = Commit.lookup(repo: repo, oid: head.target); + final tree = commit.tree; + + final diff = Diff.treeToTree(repo: repo, oldTree: null, newTree: tree); + + expect(diff.length, 11); + for (var i = 0; i < diff.deltas.length; i++) { + expect(diff.deltas[i].newFile.path, treeToEmptyTree[i]); + } + + commit.free(); + head.free(); + tree.free(); + diff.free(); + }); + test('throws when trying to diff between tree and tree and error occurs', () { final nullTree = Tree(nullptr); @@ -405,6 +474,19 @@ index e69de29..c217c63 100644 diff.free(); }); + test('does not apply hunk with non existing index', () { + final diff = Diff.parse(patchText); + final file = File(p.join(tmpDir.path, 'subdir', 'modified_file')); + + Checkout.head(repo: repo, strategy: {GitCheckout.force}); + expect(file.readAsStringSync(), ''); + + diff.apply(repo: repo, hunkIndex: 10); + expect(file.readAsStringSync(), ''); + + diff.free(); + }); + test('applies diff to tree', () { final diff = Diff.parse(patchText); diff --git a/test/index_test.dart b/test/index_test.dart index 608e316..1834251 100644 --- a/test/index_test.dart +++ b/test/index_test.dart @@ -341,6 +341,7 @@ void main() { test('adds conflict entry', () { expect(index.conflicts, isEmpty); index.addConflict( + ancestorEntry: index['file'], ourEntry: index['file'], theirEntry: index['feature_file'], ); diff --git a/test/patch_test.dart b/test/patch_test.dart index f8ea295..81f8e38 100644 --- a/test/patch_test.dart +++ b/test/patch_test.dart @@ -152,6 +152,19 @@ index e69de29..0000000 patch.free(); }); + test('creates from empty blob and buffer', () { + final patch = Patch.fromBlobAndBuffer( + blob: null, + buffer: newBuffer, + blobPath: path, + bufferPath: path, + ); + + expect(patch.text, blobPatchAdd); + + patch.free(); + }); + test('throws when trying to create from diff and error occurs', () { expect( () => Patch.fromDiff(diff: Diff(nullptr), index: 0), diff --git a/test/reflog_test.dart b/test/reflog_test.dart index 904876e..d7dad43 100644 --- a/test/reflog_test.dart +++ b/test/reflog_test.dart @@ -141,6 +141,17 @@ void main() { expect(newReflog.length, 3); }); + test('throws when trying to write reflog to disk and error occurs', () { + final ref = Reference.lookup(repo: repo, name: 'refs/heads/feature'); + final reflog = ref.log; + Reference.delete(repo: repo, name: ref.name); + + expect(() => reflog.write(), throwsA(isA())); + + reflog.free(); + ref.free(); + }); + test('returns string representation of RefLogEntry object', () { expect(reflog[0].toString(), contains('RefLogEntry{')); }); diff --git a/test/remote_prune_test.dart b/test/remote_prune_test.dart index b9a143e..7b435fa 100644 --- a/test/remote_prune_test.dart +++ b/test/remote_prune_test.dart @@ -11,7 +11,9 @@ void main() { late Repository clonedRepo; late Directory tmpDir; late Remote remote; - final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned')); + final cloneDir = Directory( + p.join(Directory.systemTemp.path, 'remote_prune_cloned'), + ); setUp(() { tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); diff --git a/test/repository_clone_test.dart b/test/repository_clone_test.dart index 599c4b8..db1cee4 100644 --- a/test/repository_clone_test.dart +++ b/test/repository_clone_test.dart @@ -11,7 +11,9 @@ import 'helpers/util.dart'; void main() { late Repository repo; late Directory tmpDir; - final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned')); + final cloneDir = Directory( + p.join(Directory.systemTemp.path, 'repository_cloned'), + ); setUp(() { tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); diff --git a/test/repository_test.dart b/test/repository_test.dart index abb0485..49f6e66 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -249,18 +249,6 @@ void main() { expect(repo.getAttribute(path: 'file.sh', name: 'eol'), 'lf'); }); - test('checks if commit is a descendant of another commit', () { - final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e8']); - final commit2 = Commit.lookup(repo: repo, oid: repo['78b8bf12']); - - expect(commit1.descendantOf(commit2), true); - expect(commit1.descendantOf(commit1), false); - expect(commit2.descendantOf(commit1), false); - - commit1.free(); - commit2.free(); - }); - test('returns number of ahead behind commits', () { final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e']); final commit2 = Commit.lookup(repo: repo, oid: repo['c68ff54']); diff --git a/test/tag_test.dart b/test/tag_test.dart index 444c02b..48e6760 100644 --- a/test/tag_test.dart +++ b/test/tag_test.dart @@ -352,7 +352,7 @@ void main() { repo: repo, tagName: '', target: repo['9c78c21'], - targetType: GitObject.commit, + targetType: GitObject.any, tagger: Signature(nullptr), message: '', ), @@ -366,7 +366,7 @@ void main() { repo: repo, tagName: '', target: repo['9c78c21'], - targetType: GitObject.commit, + targetType: GitObject.any, ), throwsA(isA()), );