refactor!: use Finalizer to automatically free allocated memory for objects (#48)

BREAKING CHANGE: signature change for remote and repository callbacks during repository clone operation.
This commit is contained in:
Aleksey Kulikov 2022-04-28 11:04:48 +03:00 committed by GitHub
parent 94c40f9a94
commit a3213a88a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
103 changed files with 2278 additions and 2595 deletions

View file

@ -34,18 +34,13 @@ void main() {
});
tearDown(() {
author.free();
committer.free();
tree.free();
repo.free();
tmpDir.deleteSync(recursive: true);
});
group('Commit', () {
test('lookups commit for provided oid', () {
final commit = Commit.lookup(repo: repo, oid: tip);
expect(commit, isA<Commit>());
commit.free();
expect(Commit.lookup(repo: repo, oid: tip), isA<Commit>());
});
test('throws when trying to lookup with invalid oid', () {
@ -63,18 +58,14 @@ void main() {
test('reverts commit affecting index and workdir', () {
final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
final index = repo.index;
final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt'));
expect(index.find('dir/dir_file.txt'), true);
expect(repo.index.find('dir/dir_file.txt'), true);
expect(file.existsSync(), true);
commit.revert();
expect(index.find('dir/dir_file.txt'), false);
expect(repo.index.find('dir/dir_file.txt'), false);
expect(file.existsSync(), false);
index.free();
commit.free();
});
test('throws when trying to revert and error occurs', () {
@ -82,21 +73,16 @@ void main() {
});
test('reverts commit to provided commit', () {
final to = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
final from = Commit.lookup(repo: repo, oid: repo['821ed6e']);
final index = repo.index;
final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt'));
expect(index.find('dir/dir_file.txt'), true);
expect(repo.index.find('dir/dir_file.txt'), true);
expect(file.existsSync(), true);
final revertIndex = from.revertTo(commit: to);
final from = Commit.lookup(repo: repo, oid: repo['821ed6e']);
final revertIndex = from.revertTo(
commit: Commit.lookup(repo: repo, oid: repo['78b8bf1']),
);
expect(revertIndex.find('dir/dir_file.txt'), false);
expect(file.existsSync(), true);
revertIndex.free();
index.free();
to.free();
from.free();
});
test('throws when trying to revert commit and error occurs', () {
@ -114,13 +100,9 @@ void main() {
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(
repo: repo,
updateRef: 'HEAD',
@ -128,7 +110,7 @@ void main() {
author: author,
committer: committer,
tree: tree,
parents: [parent],
parents: [Commit.lookup(repo: repo, oid: tip)],
);
final commit = Commit.lookup(repo: repo, oid: oid);
@ -146,13 +128,9 @@ void main() {
expect(commit.treeOid, tree.oid);
expect(commit.parents.length, 1);
expect(commit.parents[0], tip);
commit.free();
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',
@ -174,12 +152,9 @@ 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(
repo: repo,
updateRef: 'HEAD',
@ -187,7 +162,7 @@ Some description.
author: author,
committer: committer,
tree: tree,
parents: [parent],
parents: [Commit.lookup(repo: repo, oid: tip)],
);
const expected = """
@ -202,8 +177,6 @@ Some description.
""";
expect(commit, expected);
parent.free();
});
test('creates commit without parents', () {
@ -227,8 +200,6 @@ Some description.
expect(commit.time, 124);
expect(commit.treeOid, tree.oid);
expect(commit.parents.length, 0);
commit.free();
});
test('creates commit with 2 parents', () {
@ -255,59 +226,44 @@ Some description.
expect(commit.time, 124);
expect(commit.treeOid, tree.oid);
expect(commit.parents.length, 2);
expect(commit.parents[0], tip);
expect(commit.parents[0], parent1.oid);
expect(commit.parents[1], parent2.oid);
parent1.free();
parent2.free();
commit.free();
});
test('throws when trying to create commit and error occurs', () {
final parent = Commit.lookup(repo: repo, oid: tip);
final nullRepo = Repository(nullptr);
expect(
() => Commit.create(
repo: nullRepo,
repo: Repository(nullptr),
updateRef: 'HEAD',
message: message,
author: author,
committer: committer,
tree: tree,
parents: [parent],
parents: [Commit.lookup(repo: repo, oid: tip)],
),
throwsA(isA<LibGit2Error>()),
);
parent.free();
});
test('throws when trying to write commit into a buffer and error occurs',
() {
final parent = Commit.lookup(repo: repo, oid: tip);
final nullRepo = Repository(nullptr);
expect(
() => Commit.createBuffer(
repo: nullRepo,
repo: Repository(nullptr),
updateRef: 'HEAD',
message: message,
author: author,
committer: committer,
tree: tree,
parents: [parent],
parents: [Commit.lookup(repo: repo, oid: tip)],
),
throwsA(isA<LibGit2Error>()),
);
parent.free();
});
test('amends commit with default arguments', () {
final oldHead = repo.head;
final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
expect(commit.oid, oldHead.target);
expect(commit.oid, repo.head.target);
final amendedOid = Commit.amend(
repo: repo,
@ -316,25 +272,18 @@ Some description.
updateRef: 'HEAD',
);
final amendedCommit = Commit.lookup(repo: repo, oid: amendedOid);
final newHead = repo.head;
expect(amendedCommit.oid, newHead.target);
expect(amendedCommit.oid, repo.head.target);
expect(amendedCommit.message, 'amended commit\n');
expect(amendedCommit.author, commit.author);
expect(amendedCommit.committer, commit.committer);
expect(amendedCommit.treeOid, commit.treeOid);
expect(amendedCommit.parents, commit.parents);
amendedCommit.free();
commit.free();
newHead.free();
oldHead.free();
});
test('amends commit with provided arguments', () {
final oldHead = repo.head;
final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
expect(commit.oid, oldHead.target);
expect(commit.oid, repo.head.target);
final amendedOid = Commit.amend(
repo: repo,
@ -346,25 +295,18 @@ Some description.
tree: tree,
);
final amendedCommit = Commit.lookup(repo: repo, oid: amendedOid);
final newHead = repo.head;
expect(amendedCommit.oid, newHead.target);
expect(amendedCommit.oid, repo.head.target);
expect(amendedCommit.message, 'amended commit\n');
expect(amendedCommit.author, author);
expect(amendedCommit.committer, committer);
expect(amendedCommit.treeOid, tree.oid);
expect(amendedCommit.parents, commit.parents);
amendedCommit.free();
commit.free();
newHead.free();
oldHead.free();
});
test('amends commit that is not the tip of the branch', () {
final head = repo.head;
final commit = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
expect(commit.oid, isNot(head.target));
expect(commit.oid, isNot(repo.head.target));
expect(
() => Commit.amend(
@ -375,17 +317,13 @@ Some description.
),
returnsNormally,
);
commit.free();
head.free();
});
test(
'throws when trying to amend commit that is not the tip of the branch '
'with HEAD provided as update reference', () {
final head = repo.head;
final commit = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
expect(commit.oid, isNot(head.target));
expect(commit.oid, isNot(repo.head.target));
expect(
() => Commit.amend(
@ -396,9 +334,6 @@ Some description.
),
throwsA(isA<LibGit2Error>()),
);
commit.free();
head.free();
});
test('creates an in-memory copy of a commit', () {
@ -406,9 +341,6 @@ Some description.
final dupCommit = commit.duplicate();
expect(dupCommit.oid, commit.oid);
dupCommit.free();
commit.free();
});
test('returns header field', () {
@ -417,7 +349,6 @@ Some description.
commit.headerField('parent'),
'78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8',
);
commit.free();
});
test('throws when header field not found', () {
@ -426,24 +357,19 @@ Some description.
() => commit.headerField('not-there'),
throwsA(isA<LibGit2Error>()),
);
commit.free();
});
test('returns nth generation ancestor commit', () {
final commit = Commit.lookup(repo: repo, oid: tip);
final ancestor = commit.nthGenAncestor(3);
final ancestor = Commit.lookup(repo: repo, oid: tip).nthGenAncestor(3);
expect(ancestor.oid.sha, 'f17d0d48eae3aa08cecf29128a35e310c97b3521');
ancestor.free();
commit.free();
});
test('throws when trying to get nth generation ancestor and none exists',
() {
final commit = Commit.lookup(repo: repo, oid: tip);
expect(() => commit.nthGenAncestor(10), throwsA(isA<LibGit2Error>()));
commit.free();
expect(
() => Commit.lookup(repo: repo, oid: tip).nthGenAncestor(10),
throwsA(isA<LibGit2Error>()),
);
});
test('returns parent at specified position', () {
@ -453,22 +379,23 @@ Some description.
expect(firstParent.oid.sha, 'c68ff54aabf660fcdd9a2838d401583fe31249e3');
expect(secondParent.oid.sha, 'fc38877b2552ab554752d9a77e1f48f738cca79b');
secondParent.free();
firstParent.free();
commit.free();
});
test('throws when trying to get the parent at invalid position', () {
expect(
() => Commit.lookup(repo: repo, oid: tip).parent(10),
throwsA(isA<LibGit2Error>()),
);
});
test('manually releases allocated memory', () {
final commit = Commit.lookup(repo: repo, oid: tip);
expect(() => commit.parent(10), throwsA(isA<LibGit2Error>()));
commit.free();
expect(() => commit.free(), returnsNormally);
});
test('returns string representation of Commit object', () {
final commit = Commit.lookup(repo: repo, oid: tip);
expect(commit.toString(), contains('Commit{'));
commit.free();
});
});
}