mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-28 06:47:53 +00:00
reset: make git_reset() cope with an orphaned HEAD
This commit is contained in:
parent
cd1ef82253
commit
c436ed26e0
46
src/reset.c
46
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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user