summaryrefslogtreecommitdiff
path: root/src/reset.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reset.c')
-rw-r--r--src/reset.c46
1 files changed, 40 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);