Commit f3cc7834 by nulltoken

refs: deploy git_repository_set_head() usage

parent 7eca3c56
...@@ -1343,6 +1343,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) ...@@ -1343,6 +1343,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
unsigned int normalization_flags; unsigned int normalization_flags;
git_buf aux_path = GIT_BUF_INIT; git_buf aux_path = GIT_BUF_INIT;
char normalized[GIT_REFNAME_MAX]; char normalized[GIT_REFNAME_MAX];
bool should_head_be_updated = false;
const char *head_target = NULL; const char *head_target = NULL;
git_reference *head = NULL; git_reference *head = NULL;
...@@ -1367,6 +1368,15 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) ...@@ -1367,6 +1368,15 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
return -1; return -1;
/* /*
* Check if we have to update HEAD.
*/
if (git_repository_head(&head, ref->owner) < 0)
goto cleanup;
should_head_be_updated = !strcmp(git_reference_name(head), ref->name);
git_reference_free(head);
/*
* Now delete the old ref and remove an possibly existing directory * Now delete the old ref and remove an possibly existing directory
* named `new_name`. Note that using the internal `reference_delete` * named `new_name`. Note that using the internal `reference_delete`
* method deletes the ref from disk but doesn't free the pointer, so * method deletes the ref from disk but doesn't free the pointer, so
...@@ -1390,25 +1400,13 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) ...@@ -1390,25 +1400,13 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
goto rollback; goto rollback;
/* /*
* Check if we have to update HEAD. * Update HEAD it was poiting to the reference being renamed.
*/ */
if (git_reference_lookup(&head, ref->owner, GIT_HEAD_FILE) < 0) { if (should_head_be_updated &&
giterr_set(GITERR_REFERENCE, git_repository_set_head(ref->owner, new_name) < 0) {
"Failed to update HEAD after renaming reference");
goto cleanup;
}
head_target = git_reference_target(head);
if (head_target && !strcmp(head_target, ref->name)) {
git_reference_free(head);
head = NULL;
if (git_reference_create_symbolic(&head, ref->owner, GIT_HEAD_FILE, new_name, 1) < 0) {
giterr_set(GITERR_REFERENCE, giterr_set(GITERR_REFERENCE,
"Failed to update HEAD after renaming reference"); "Failed to update HEAD after renaming reference");
goto cleanup; goto cleanup;
}
} }
/* /*
...@@ -1426,12 +1424,10 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) ...@@ -1426,12 +1424,10 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
/* The reference is no longer packed */ /* The reference is no longer packed */
ref->flags &= ~GIT_REF_PACKED; ref->flags &= ~GIT_REF_PACKED;
git_reference_free(head);
git_buf_free(&aux_path); git_buf_free(&aux_path);
return 0; return 0;
cleanup: cleanup:
git_reference_free(head);
git_buf_free(&aux_path); git_buf_free(&aux_path);
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment