Commit c436ed26e08fde69579d5d0ac31e775cfbd06290

nulltoken 2012-10-20T12:09:02

reset: make git_reset() cope with an orphaned HEAD

diff --git a/src/reset.c b/src/reset.c
index dfa095b..560ae17 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -19,6 +19,45 @@ static int reset_error_invalid(const char *msg)
 	return -1;
 }
 
+static int update_head(git_repository *repo, git_object *commit)
+{
+	int error;
+	git_reference *head = NULL, *target = NULL;
+
+	error = git_repository_head(&head, repo);
+
+	if (error < 0 && error != GIT_EORPHANEDHEAD)
+		return error;
+
+	if (error == GIT_EORPHANEDHEAD) {
+		giterr_clear();
+
+		/*
+		 * TODO: This is a bit weak as this doesn't support chained
+		 * symbolic references. yet.
+		 */
+		if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
+			goto cleanup;
+
+		if ((error = git_reference_create_oid(
+			&target,
+			repo,
+			git_reference_target(head),
+			git_object_id(commit), 0)) < 0)
+				goto cleanup;
+	} else {
+		if ((error = git_reference_set_oid(head, git_object_id(commit))) < 0)
+			goto cleanup;
+	}
+
+	error = 0;
+
+cleanup:
+	git_reference_free(head);
+	git_reference_free(target);
+	return error;
+}
+
 int git_reset(
 	git_repository *repo,
 	git_object *target,
@@ -29,7 +68,6 @@ int git_reset(
 	git_tree *tree = NULL;
 	int error = -1;
 	git_checkout_opts opts;
-	git_reference *head = NULL;
 
 	assert(repo && target);
 	assert(reset_type == GIT_RESET_SOFT
@@ -52,10 +90,7 @@ int git_reset(
 
 	//TODO: Check for unmerged entries
 
-	if (git_repository_head(&head, repo) < 0)
-		goto cleanup;
-
-	if (git_reference_set_oid(head, git_object_id(commit)) < 0)
+	if (update_head(repo, commit) < 0)
 		goto cleanup;
 
 	if (reset_type == GIT_RESET_SOFT) {
@@ -102,7 +137,6 @@ int git_reset(
 	error = 0;
 
 cleanup:
-	git_reference_free(head);
 	git_object_free(commit);
 	git_index_free(index);
 	git_tree_free(tree);
diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c
index 3c678ad..1872baf 100644
--- a/tests-clar/reset/soft.c
+++ b/tests-clar/reset/soft.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "reset_helpers.h"
+#include "repo/repo_helpers.h"
 
 static git_repository *repo;
 static git_object *target;
@@ -89,3 +90,23 @@ void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
 	retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
 	cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
 }
+
+void test_reset_soft__resetting_against_an_orphaned_head_repo_makes_the_head_no_longer_orphaned(void)
+{
+	git_reference *head;
+
+	retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+
+	make_head_orphaned(repo, NON_EXISTING_HEAD);
+
+	cl_assert_equal_i(true, git_repository_head_orphan(repo));
+
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+
+	cl_assert_equal_i(false, git_repository_head_orphan(repo));
+
+	cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD));
+	cl_assert_equal_i(0, git_oid_streq(git_reference_oid(head), KNOWN_COMMIT_IN_BARE_REPO));
+
+	git_reference_free(head);
+}