summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-10-20 12:09:02 +0200
committernulltoken <emeric.fermas@gmail.com>2012-10-22 19:46:00 +0200
commitc436ed26e08fde69579d5d0ac31e775cfbd06290 (patch)
treeb107ddb3e8e21ec1ba35adce60bbe246277f24e4
parentcd1ef82253b5f4242ed9aed5e9abf7bceb33ec04 (diff)
downloadlibgit2-c436ed26e08fde69579d5d0ac31e775cfbd06290.tar.gz
reset: make git_reset() cope with an orphaned HEAD
-rw-r--r--src/reset.c46
-rw-r--r--tests-clar/reset/soft.c21
2 files changed, 61 insertions, 6 deletions
diff --git a/src/reset.c b/src/reset.c
index dfa095be4..560ae17b1 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 3c678ad73..1872baf3b 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);
+}