feat(commit): make 'updateRef' to be required argument

This commit is contained in:
Aleksey Kulikov 2021-11-02 18:00:15 +03:00
parent 678f6208f6
commit c2da51af94
4 changed files with 27 additions and 20 deletions

View file

@ -65,7 +65,7 @@ void annotatedFree(Pointer<git_annotated_commit> commit) {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> create({ Pointer<git_oid> create({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
String? updateRef, required String updateRef,
required Pointer<git_signature> authorPointer, required Pointer<git_signature> authorPointer,
required Pointer<git_signature> committerPointer, required Pointer<git_signature> committerPointer,
String? messageEncoding, String? messageEncoding,
@ -75,7 +75,7 @@ Pointer<git_oid> create({
required List<Pointer<git_commit>> parents, required List<Pointer<git_commit>> parents,
}) { }) {
final out = calloc<git_oid>(); final out = calloc<git_oid>();
final updateRefC = updateRef?.toNativeUtf8().cast<Int8>() ?? nullptr; final updateRefC = updateRef.toNativeUtf8().cast<Int8>();
final messageEncodingC = final messageEncodingC =
messageEncoding?.toNativeUtf8().cast<Int8>() ?? nullptr; messageEncoding?.toNativeUtf8().cast<Int8>() ?? nullptr;
final messageC = message.toNativeUtf8().cast<Int8>(); final messageC = message.toNativeUtf8().cast<Int8>();

View file

@ -58,7 +58,7 @@ class Commit {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
static Oid create({ static Oid create({
required Repository repo, required Repository repo,
String? updateRef, required String updateRef,
required Signature author, required Signature author,
required Signature committer, required Signature committer,
String? messageEncoding, String? messageEncoding,
@ -103,10 +103,10 @@ class Commit {
static Oid amend({ static Oid amend({
required Repository repo, required Repository repo,
required Commit commit, required Commit commit,
required String? updateRef,
Signature? author, Signature? author,
Signature? committer, Signature? committer,
Tree? tree, Tree? tree,
String? updateRef,
String? message, String? message,
String? messageEncoding, String? messageEncoding,
}) { }) {

View file

@ -558,8 +558,8 @@ class Repository {
/// this commit. If the reference is not direct, it will be resolved to a /// this commit. If the reference is not direct, it will be resolved to a
/// direct reference. Use "HEAD" to update the HEAD of the current branch and /// direct reference. Use "HEAD" to update the HEAD of the current branch and
/// make it point to this commit. If the reference doesn't exist yet, it will /// make it point to this commit. If the reference doesn't exist yet, it will
/// be created. If it does exist, the first parent /// be created. If it does exist, the first parent must be the tip of this
/// must be the tip of this branch. /// branch.
/// ///
/// [author] is the signature with author and author time of commit. /// [author] is the signature with author and author time of commit.
/// ///
@ -580,21 +580,23 @@ class Repository {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Oid createCommit({ Oid createCommit({
required String updateRef,
required String message, required String message,
required Signature author, required Signature author,
required Signature commiter, required Signature commiter,
required Tree tree, required Tree tree,
required List<Commit> parents, required List<Commit> parents,
String? updateRef,
String? messageEncoding, String? messageEncoding,
}) { }) {
return Commit.create( return Commit.create(
repo: this, repo: this,
updateRef: updateRef,
message: message, message: message,
author: author, author: author,
committer: commiter, committer: commiter,
tree: tree, tree: tree,
parents: parents, parents: parents,
messageEncoding: messageEncoding,
); );
} }
@ -619,20 +621,20 @@ class Repository {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Oid amendCommit({ Oid amendCommit({
required Commit commit, required Commit commit,
required String? updateRef,
Signature? author, Signature? author,
Signature? committer, Signature? committer,
Tree? tree, Tree? tree,
String? updateRef,
String? message, String? message,
String? messageEncoding, String? messageEncoding,
}) { }) {
return Commit.amend( return Commit.amend(
repo: this, repo: this,
commit: commit, commit: commit,
updateRef: updateRef,
author: author, author: author,
committer: committer, committer: committer,
tree: tree, tree: tree,
updateRef: updateRef,
message: message, message: message,
messageEncoding: messageEncoding, messageEncoding: messageEncoding,
); );

View file

@ -12,7 +12,7 @@ void main() {
late Signature author; late Signature author;
late Signature commiter; late Signature commiter;
late Tree tree; late Tree tree;
late Oid mergeCommit; late Oid tip;
const message = "Commit message.\n\nSome description.\n"; const message = "Commit message.\n\nSome description.\n";
setUp(() { setUp(() {
@ -28,10 +28,10 @@ void main() {
email: 'commiter@email.com', email: 'commiter@email.com',
time: 124, time: 124,
); );
mergeCommit = repo['78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8']; tip = repo['821ed6e80627b8769d170a293862f9fc60825226'];
tree = Tree.lookup( tree = Tree.lookup(
repo: repo, repo: repo,
oid: repo['7796359a96eb722939c24bafdb1afe9f07f2f628'], oid: repo['a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f'],
); );
}); });
@ -45,7 +45,7 @@ void main() {
group('Commit', () { group('Commit', () {
test('successfully lookups for provided oid', () { test('successfully lookups for provided oid', () {
final commit = repo.lookupCommit(mergeCommit); final commit = repo.lookupCommit(tip);
expect(commit, isA<Commit>()); expect(commit, isA<Commit>());
commit.free(); commit.free();
}); });
@ -58,7 +58,7 @@ void main() {
}); });
test('successfully lookups annotated commit for provided oid', () { test('successfully lookups annotated commit for provided oid', () {
final annotated = AnnotatedCommit.lookup(repo: repo, oid: mergeCommit); final annotated = AnnotatedCommit.lookup(repo: repo, oid: tip);
expect(annotated, isA<AnnotatedCommit>()); expect(annotated, isA<AnnotatedCommit>());
annotated.free(); annotated.free();
}); });
@ -101,8 +101,9 @@ void main() {
}); });
test('successfully creates commit', () { test('successfully creates commit', () {
final parent = repo.lookupCommit(mergeCommit); final parent = repo.lookupCommit(tip);
final oid = repo.createCommit( final oid = repo.createCommit(
updateRef: 'HEAD',
message: message, message: message,
author: author, author: author,
commiter: commiter, commiter: commiter,
@ -120,7 +121,7 @@ void main() {
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.oid, tree.oid); expect(commit.tree.oid, tree.oid);
expect(commit.parents.length, 1); expect(commit.parents.length, 1);
expect(commit.parents[0], mergeCommit); expect(commit.parents[0], tip);
commit.free(); commit.free();
parent.free(); parent.free();
@ -128,6 +129,7 @@ void main() {
test('successfully creates commit without parents', () { test('successfully creates commit without parents', () {
final oid = repo.createCommit( final oid = repo.createCommit(
updateRef: 'refs/heads/new',
message: message, message: message,
author: author, author: author,
commiter: commiter, commiter: commiter,
@ -150,12 +152,13 @@ void main() {
}); });
test('successfully creates commit with 2 parents', () { test('successfully creates commit with 2 parents', () {
final parent1 = repo.lookupCommit(mergeCommit); final parent1 = repo.lookupCommit(tip);
final parent2 = repo.lookupCommit( final parent2 = repo.lookupCommit(
repo['fc38877b2552ab554752d9a77e1f48f738cca79b'], repo['fc38877b2552ab554752d9a77e1f48f738cca79b'],
); );
final oid = Commit.create( final oid = Commit.create(
updateRef: 'HEAD',
repo: repo, repo: repo,
message: message, message: message,
author: author, author: author,
@ -174,7 +177,7 @@ void main() {
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.oid, tree.oid); expect(commit.tree.oid, tree.oid);
expect(commit.parents.length, 2); expect(commit.parents.length, 2);
expect(commit.parents[0], mergeCommit); expect(commit.parents[0], tip);
expect(commit.parents[1], parent2.oid); expect(commit.parents[1], parent2.oid);
parent1.free(); parent1.free();
@ -183,11 +186,12 @@ void main() {
}); });
test('throws when trying to create commit and error occurs', () { test('throws when trying to create commit and error occurs', () {
final parent = repo.lookupCommit(mergeCommit); final parent = repo.lookupCommit(tip);
final nullRepo = Repository(nullptr); final nullRepo = Repository(nullptr);
expect( expect(
() => nullRepo.createCommit( () => nullRepo.createCommit(
updateRef: 'HEAD',
message: message, message: message,
author: author, author: author,
commiter: commiter, commiter: commiter,
@ -262,6 +266,7 @@ void main() {
expect( expect(
() => repo.amendCommit( () => repo.amendCommit(
updateRef: null,
commit: commit, commit: commit,
message: 'amended commit\n', message: 'amended commit\n',
), ),
@ -293,7 +298,7 @@ void main() {
}); });
test('returns string representation of Commit object', () { test('returns string representation of Commit object', () {
final commit = repo.lookupCommit(mergeCommit); final commit = repo.lookupCommit(tip);
expect(commit.toString(), contains('Commit{')); expect(commit.toString(), contains('Commit{'));
commit.free(); commit.free();
}); });