Commit b4b79ac3dbca9088539a10d8d65bfc06504c3c2e

schu 2012-02-15T00:12:53

commit: actually allow yet to be born update_ref git_commit_create is supposed to update the given reference "update_ref", but segfaulted in case of a yet to be born reference. Fix it. Signed-off-by: schu <schu-github@schulog.org>

diff --git a/include/git2/commit.h b/include/git2/commit.h
index 6d8cf53..c274b6b 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -191,7 +191,8 @@ GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned i
  *	will be updated to point to 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
- *	make it point to this commit
+ *	make it point to this commit. If the reference doesn't
+ *	exist yet, it will be created.
  *
  * @param author Signature representing the author and the authory
  *	time of this commit
diff --git a/src/commit.c b/src/commit.c
index 64db0a7..189d8fe 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -146,10 +146,14 @@ int git_commit_create(
 		git_reference *target;
 
 		error = git_reference_lookup(&head, repo, update_ref);
-		if (error < GIT_SUCCESS)
+		if (error < GIT_SUCCESS && error != GIT_ENOTFOUND)
 			return git__rethrow(error, "Failed to create commit");
 
-		error = git_reference_resolve(&target, head);
+		if (error != GIT_ENOTFOUND) {
+			update_ref = git_reference_target(head);
+			error = git_reference_resolve(&target, head);
+		}
+
 		if (error < GIT_SUCCESS) {
 			if (error != GIT_ENOTFOUND) {
 				git_reference_free(head);
@@ -162,7 +166,7 @@ int git_commit_create(
 		 * point to) or after an orphan checkout, so if the target
 		 * branch doesn't exist yet, create it and return.
 		 */
-			error = git_reference_create_oid(&target, repo, git_reference_target(head), oid, 1);
+			error = git_reference_create_oid(&target, repo, update_ref, oid, 1);
 
 			git_reference_free(head);
 			if (error == GIT_SUCCESS)
diff --git a/tests-clar/commit/commit.c b/tests-clar/commit/commit.c
new file mode 100644
index 0000000..1205e52
--- /dev/null
+++ b/tests-clar/commit/commit.c
@@ -0,0 +1,44 @@
+#include "clar_libgit2.h"
+
+static git_repository *_repo;
+
+void test_commit_commit__initialize(void)
+{
+	cl_fixture_sandbox("testrepo.git");
+	cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
+}
+
+void test_commit_commit__cleanup(void)
+{
+	git_repository_free(_repo);
+	cl_fixture_cleanup("testrepo.git");
+}
+
+void test_commit_commit__create_unexisting_update_ref(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, "some msg", tree, 1, (const git_commit **) &commit));
+
+	cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
+	cl_assert(!git_oid_cmp(&oid, git_reference_oid(ref)));
+
+	git_tree_free(tree);
+	git_commit_free(commit);
+	git_signature_free(s);
+	git_reference_free(ref);
+}