Skip to content

Commit 17a93af

Browse files
committed
Merge pull request libgit2#3757 from johnhaley81/jh/fix-create-initial-commit
Fix `git_commit_create` for an initial commit
2 parents 2e43a37 + 5785ae9 commit 17a93af

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int validate_tree_and_parents(git_array_oid_t *parents, git_repository *r
104104
i++;
105105
}
106106

107-
if (current_id && git_oid_cmp(current_id, git_array_get(*parents, 0))) {
107+
if (current_id && (parents->size == 0 || git_oid_cmp(current_id, git_array_get(*parents, 0)))) {
108108
giterr_set(GITERR_OBJECT, "failed to create commit: current tip is not the first parent");
109109
error = GIT_EMODIFIED;
110110
goto on_error;

tests/commit/commit.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,66 @@ void test_commit_commit__create_unexisting_update_ref(void)
5151
git_reference_free(ref);
5252
}
5353

54+
void test_commit_commit__create_initial_commit(void)
55+
{
56+
git_oid oid;
57+
git_tree *tree;
58+
git_commit *commit;
59+
git_signature *s;
60+
git_reference *ref;
61+
62+
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
63+
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
64+
65+
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
66+
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
67+
68+
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
69+
70+
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
71+
cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
72+
NULL, "initial commit", tree, 0, NULL));
73+
74+
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
75+
76+
cl_assert_equal_oid(&oid, git_reference_target(ref));
77+
78+
git_tree_free(tree);
79+
git_commit_free(commit);
80+
git_signature_free(s);
81+
git_reference_free(ref);
82+
}
83+
84+
void test_commit_commit__create_initial_commit_parent_not_current(void)
85+
{
86+
git_oid oid;
87+
git_oid original_oid;
88+
git_tree *tree;
89+
git_commit *commit;
90+
git_signature *s;
91+
92+
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
93+
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
94+
95+
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
96+
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
97+
98+
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
99+
100+
cl_git_pass(git_reference_name_to_id(&original_oid, _repo, "HEAD"));
101+
102+
cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s,
103+
NULL, "initial commit", tree, 0, NULL));
104+
105+
cl_git_pass(git_reference_name_to_id(&oid, _repo, "HEAD"));
106+
107+
cl_assert_equal_oid(&oid, &original_oid);
108+
109+
git_tree_free(tree);
110+
git_commit_free(commit);
111+
git_signature_free(s);
112+
}
113+
54114
void assert_commit_summary(const char *expected, const char *given)
55115
{
56116
git_commit *dummy;

0 commit comments

Comments
 (0)