refs: deploy git_repository_set_head() usage
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
diff --git a/src/refs.c b/src/refs.c
index 2a3b8dd..3b33a32 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1343,6 +1343,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
unsigned int normalization_flags;
git_buf aux_path = GIT_BUF_INIT;
char normalized[GIT_REFNAME_MAX];
+ bool should_head_be_updated = false;
const char *head_target = NULL;
git_reference *head = NULL;
@@ -1367,6 +1368,15 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
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
* named `new_name`. Note that using the internal `reference_delete`
* 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)
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) {
- giterr_set(GITERR_REFERENCE,
- "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) {
+ if (should_head_be_updated &&
+ git_repository_set_head(ref->owner, new_name) < 0) {
giterr_set(GITERR_REFERENCE,
"Failed to update HEAD after renaming reference");
goto cleanup;
- }
}
/*
@@ -1426,12 +1424,10 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
/* The reference is no longer packed */
ref->flags &= ~GIT_REF_PACKED;
- git_reference_free(head);
git_buf_free(&aux_path);
return 0;
cleanup:
- git_reference_free(head);
git_buf_free(&aux_path);
return -1;