feat(commit): add ability to create commit with different number of parents

This commit is contained in:
Aleksey Kulikov 2021-08-30 16:45:37 +03:00
parent c90561ed8f
commit 94b4116adf
15 changed files with 217 additions and 260 deletions

View file

@ -11,6 +11,11 @@ void main() {
late Repository repo;
final tmpDir = '${Directory.systemTemp.path}/commit_testrepo/';
const message = "Commit message.\n\nSome description.\n";
const tree = '7796359a96eb722939c24bafdb1afe9f07f2f628';
late Signature author;
late Signature commiter;
setUp(() async {
if (await Directory(tmpDir).exists()) {
await Directory(tmpDir).delete(recursive: true);
@ -20,152 +25,135 @@ void main() {
to: await Directory(tmpDir).create(),
);
repo = Repository.open(tmpDir);
author = Signature.create(
name: 'Author Name',
email: 'author@email.com',
time: 123,
);
commiter = Signature.create(
name: 'Commiter',
email: 'commiter@email.com',
time: 124,
);
});
tearDown(() async {
author.free();
commiter.free();
repo.free();
await Directory(tmpDir).delete(recursive: true);
});
group('lookup', () {
test('successful when 40 char sha hex is provided', () {
final commit = repo[mergeCommit];
expect(commit, isA<Commit>());
commit.free();
});
test('successful when sha hex is short', () {
final commit = repo[mergeCommit.substring(0, 5)];
expect(commit, isA<Commit>());
commit.free();
});
test('throws when provided sha hex is invalid', () {
expect(() => repo['invalid'], throwsA(isA<ArgumentError>()));
});
test('throws when nothing found', () {
expect(() => repo['970ae5c'], throwsA(isA<LibGit2Error>()));
});
test('returns with correct fields', () {
final signature = Signature.create(
name: 'Aleksey Kulikov',
email: 'skinny.mind@gmail.com',
time: 1626091184,
offset: 180,
);
final commit = repo[mergeCommit];
final parents = commit.parents;
expect(commit.messageEncoding, 'utf-8');
expect(commit.message, 'Merge branch \'feature\'\n');
expect(commit.id.sha, mergeCommit);
expect(parents.length, 2);
expect(
parents[0].id.sha,
'c68ff54aabf660fcdd9a2838d401583fe31249e3',
);
expect(commit.time, 1626091184);
expect(commit.committer, signature);
expect(commit.author, signature);
expect(commit.tree.sha, '7796359a96eb722939c24bafdb1afe9f07f2f628');
for (var p in parents) {
p.free();
}
signature.free();
commit.free();
});
test('successfully returns when 40 char sha hex is provided', () {
final commit = repo[mergeCommit];
expect(commit, isA<Commit>());
commit.free();
});
group('.create()', () {
test('successfuly creates commit', () {
const message = "Commit message.\n\nSome description.\n";
const tree = '7796359a96eb722939c24bafdb1afe9f07f2f628';
final author = Signature.create(
name: 'Author Name',
email: 'author@email.com',
time: 123,
);
final commiter = Signature.create(
name: 'Commiter',
email: 'commiter@email.com',
time: 124,
);
test('successfully returns when sha hex is short', () {
final commit = repo[mergeCommit.substring(0, 5)];
expect(commit, isA<Commit>());
commit.free();
});
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree,
parentsSHA: [mergeCommit],
);
test('successfully creates commit', () {
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree,
parents: [mergeCommit],
);
final commit = repo[oid.sha];
final parents = commit.parents;
final commit = repo[oid.sha];
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(parents.length, 1);
expect(parents[0].id.sha, mergeCommit);
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.messageEncoding, 'utf-8');
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(commit.parents.length, 1);
expect(commit.parents[0].sha, mergeCommit);
for (var p in parents) {
p.free();
}
author.free();
commiter.free();
commit.free();
});
commit.free();
});
test('successfuly creates commit with short sha of tree', () {
const message = "Commit message.\n\nSome description.\n";
const tree = '7796359a96eb722939c24bafdb1afe9f07f2f628';
final author = Signature.create(
name: 'Author Name',
email: 'author@email.com',
time: 123,
);
final commiter = Signature.create(
name: 'Commiter',
email: 'commiter@email.com',
time: 124,
);
test('successfully creates commit without parents', () {
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree,
parents: [],
);
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree.substring(0, 5),
parentsSHA: [mergeCommit],
);
final commit = repo[oid.sha];
final commit = repo[oid.sha];
final parents = commit.parents;
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.messageEncoding, 'utf-8');
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(commit.parents.length, 0);
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(parents.length, 1);
expect(parents[0].id.sha, mergeCommit);
commit.free();
});
for (var p in parents) {
p.free();
}
author.free();
commiter.free();
commit.free();
});
test('successfully creates commit with 2 parents', () {
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree,
parents: [mergeCommit, 'fc38877b2552ab554752d9a77e1f48f738cca79b'],
);
final commit = repo[oid.sha];
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.messageEncoding, 'utf-8');
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(commit.parents.length, 2);
expect(commit.parents[0].sha, mergeCommit);
expect(commit.parents[1].sha, 'fc38877b2552ab554752d9a77e1f48f738cca79b');
commit.free();
});
test('successfully creates commit with short sha of tree', () {
final oid = Commit.create(
repo: repo,
message: message,
author: author,
commiter: commiter,
treeSHA: tree.substring(0, 5),
parents: [mergeCommit],
);
final commit = repo[oid.sha];
expect(commit.id.sha, oid.sha);
expect(commit.message, message);
expect(commit.messageEncoding, 'utf-8');
expect(commit.author, author);
expect(commit.committer, commiter);
expect(commit.time, 124);
expect(commit.tree.sha, tree);
expect(commit.parents.length, 1);
expect(commit.parents[0].sha, mergeCommit);
commit.free();
});
});
}

View file

@ -33,11 +33,8 @@ void main() {
});
test('finds object by short oid', () {
final shortSha = '78b8bf';
final odb = repo.odb;
final oid = Oid.fromShortSHA(shortSha, odb);
final oid = Oid.fromSHA(repo, lastCommit.substring(0, 5));
expect(oid.sha, lastCommit);
odb.free();
});
});
}

View file

@ -1,5 +1,5 @@
import 'dart:io';
import 'dart:ffi';
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
@ -30,46 +30,50 @@ void main() {
});
group('fromSHA()', () {
test('initializes successfully', () {
final oid = Oid.fromSHA(sha);
final oid = Oid.fromSHA(repo, sha);
expect(oid, isA<Oid>());
expect(oid.sha, sha);
});
test('initializes successfully from short hex string', () {
final oid = Oid.fromSHA(repo, sha.substring(0, 5));
expect(oid, isA<Oid>());
expect(oid.sha, sha);
});
});
group('fromShortSHA()', () {
test('initializes successfully from short hex string', () {
final odb = repo.odb;
final oid = Oid.fromShortSHA(sha.substring(0, 5), odb);
group('fromRaw()', () {
test('initializes successfully', () {
final sourceOid = Oid.fromSHA(repo, sha);
final oid = Oid.fromRaw(sourceOid.pointer.ref);
expect(oid, isA<Oid>());
expect(oid.sha, sha);
odb.free();
});
});
test('returns sha hex string', () {
final oid = Oid.fromSHA(sha);
final oid = Oid.fromSHA(repo, sha);
expect(oid.sha, equals(sha));
});
group('compare', () {
test('< and <=', () {
final oid1 = Oid.fromSHA(sha);
final oid2 = Oid.fromSHA(biggerSha);
final oid1 = Oid.fromSHA(repo, sha);
final oid2 = Oid.fromSHA(repo, biggerSha);
expect(oid1 < oid2, true);
expect(oid1 <= oid2, true);
});
test('==', () {
final oid1 = Oid.fromSHA(sha);
final oid2 = Oid.fromSHA(sha);
final oid1 = Oid.fromSHA(repo, sha);
final oid2 = Oid.fromSHA(repo, sha);
expect(oid1 == oid2, true);
});
test('> and >=', () {
final oid1 = Oid.fromSHA(sha);
final oid2 = Oid.fromSHA(lesserSha);
final oid1 = Oid.fromSHA(repo, sha);
final oid2 = Oid.fromSHA(repo, lesserSha);
expect(oid1 > oid2, true);
expect(oid1 >= oid2, true);
});