reset: make git_reset() cope with an orphaned HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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);
+}