From 225cb8809ee5ec830fcfcca40efd62d1eba8241d Mon Sep 17 00:00:00 2001 From: John Haley Date: Tue, 26 Apr 2016 08:09:04 -0700 Subject: [PATCH 1/3] Fix `git_commit_create` for an initial commit When calling `git_commit_create` with an empty array of `parents` and `parent_count == 0` the call will segfault at https://github.com/libgit2/libgit2/blob/master/src/commit.c#L107 when it's trying to compare `current_id` to a null parent oid. This just puts in a check to stop that segfault. --- src/commit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commit.c b/src/commit.c index 5456751fe..79ddf2a63 100644 --- a/src/commit.c +++ b/src/commit.c @@ -104,7 +104,7 @@ static int validate_tree_and_parents(git_array_oid_t *parents, git_repository *r i++; } - if (current_id && git_oid_cmp(current_id, git_array_get(*parents, 0))) { + if (current_id && (parents->size == 0 || git_oid_cmp(current_id, git_array_get(*parents, 0)))) { giterr_set(GITERR_OBJECT, "failed to create commit: current tip is not the first parent"); error = GIT_EMODIFIED; goto on_error; From 4f22ccb9793d765d09032898e913690d088d6518 Mon Sep 17 00:00:00 2001 From: John Haley Date: Tue, 3 May 2016 13:32:22 -0700 Subject: [PATCH 2/3] Add tests for creating an initial commit --- tests/commit/commit.c | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/commit/commit.c b/tests/commit/commit.c index c052cd568..05a602fbd 100644 --- a/tests/commit/commit.c +++ b/tests/commit/commit.c @@ -51,6 +51,91 @@ void test_commit_commit__create_unexisting_update_ref(void) git_reference_free(ref); } +void test_commit_commit__create_initial_commit(void) +{ + git_oid oid; + git_tree *tree; + git_commit *commit; + git_signature *s; + git_reference *ref; + + git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); + + git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + cl_git_pass(git_tree_lookup(&tree, _repo, &oid)); + + cl_git_pass(git_signature_now(&s, "alice", "alice@example.com")); + + cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar")); + cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s, + NULL, "initial commit", tree, 0, NULL)); + + cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar")); + + cl_assert_equal_oid(&oid, git_reference_target(ref)); + + git_tree_free(tree); + git_commit_free(commit); + git_signature_free(s); + git_reference_free(ref); +} + +void test_commit_commit__create_initial_commit_parent_not_current(void) +{ + git_oid oid; + git_tree *tree; + git_commit *commit; + git_signature *s; + git_reference *origRef; + git_reference *origRefTarget; + git_reference *ref; + git_reference *refTarget; + + git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); + + git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + cl_git_pass(git_tree_lookup(&tree, _repo, &oid)); + + cl_git_pass(git_signature_now(&s, "alice", "alice@example.com")); + + cl_git_pass(git_reference_lookup(&origRef, _repo, "HEAD")); + + cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s, + NULL, "initial commit", tree, 0, NULL)); + + cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD")); + + cl_git_pass( + git_reference_lookup( + &origRefTarget, + _repo, + git_reference_symbolic_target(origRef) + ) + ); + cl_git_pass( + git_reference_lookup( + &refTarget, + _repo, + git_reference_symbolic_target(ref) + ) + ); + + cl_assert_equal_oid( + git_reference_target(origRefTarget), + git_reference_target(refTarget) + ); + + git_tree_free(tree); + git_commit_free(commit); + git_signature_free(s); + git_reference_free(origRef); + git_reference_free(origRefTarget); + git_reference_free(ref); + git_reference_free(refTarget); +} + void assert_commit_summary(const char *expected, const char *given) { git_commit *dummy; From 5785ae9b5e315a2aca64ee4bac1b31bdab84c657 Mon Sep 17 00:00:00 2001 From: John Haley Date: Wed, 4 May 2016 11:14:17 -0700 Subject: [PATCH 3/3] Fix initial commit test `test_commit_commit__create_initial_commit_parent_not_current` was not correctly testing that `HEAD` was not changed. Now we grab the oid that it was pointing to before the call to `git_commit_create` and the oid that it's pointing to afterwards and compare those. --- tests/commit/commit.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/tests/commit/commit.c b/tests/commit/commit.c index 05a602fbd..b03169bc8 100644 --- a/tests/commit/commit.c +++ b/tests/commit/commit.c @@ -84,13 +84,10 @@ void test_commit_commit__create_initial_commit(void) void test_commit_commit__create_initial_commit_parent_not_current(void) { git_oid oid; + git_oid original_oid; git_tree *tree; git_commit *commit; git_signature *s; - git_reference *origRef; - git_reference *origRefTarget; - git_reference *ref; - git_reference *refTarget; git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); @@ -100,40 +97,18 @@ void test_commit_commit__create_initial_commit_parent_not_current(void) cl_git_pass(git_signature_now(&s, "alice", "alice@example.com")); - cl_git_pass(git_reference_lookup(&origRef, _repo, "HEAD")); + cl_git_pass(git_reference_name_to_id(&original_oid, _repo, "HEAD")); cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s, NULL, "initial commit", tree, 0, NULL)); - cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD")); + cl_git_pass(git_reference_name_to_id(&oid, _repo, "HEAD")); - cl_git_pass( - git_reference_lookup( - &origRefTarget, - _repo, - git_reference_symbolic_target(origRef) - ) - ); - cl_git_pass( - git_reference_lookup( - &refTarget, - _repo, - git_reference_symbolic_target(ref) - ) - ); - - cl_assert_equal_oid( - git_reference_target(origRefTarget), - git_reference_target(refTarget) - ); + cl_assert_equal_oid(&oid, &original_oid); git_tree_free(tree); git_commit_free(commit); git_signature_free(s); - git_reference_free(origRef); - git_reference_free(origRefTarget); - git_reference_free(ref); - git_reference_free(refTarget); } void assert_commit_summary(const char *expected, const char *given)