From 93dc16483a312b77c0b6533a6c2705d1df3e3687 Mon Sep 17 00:00:00 2001 From: Bence Ferdinandy Date: Sun, 26 Jan 2025 23:02:11 +0100 Subject: fetch set_head: fix non-mirror remotes in bare repositories In b1b713f722 (fetch set_head: handle mirrored bare repositories, 2024-11-22) it was implicitly assumed that all remotes will be mirrors in a bare repository, thus fetching a non-mirrored remote could lead to HEAD pointing to a non-existent reference. Make sure we only overwrite HEAD if we are in a bare repository and fetching from a mirror. Otherwise, proceed as normally, and create refs/remotes//HEAD instead. Reported-by: Christian Hesse Signed-off-by: Bence Ferdinandy Signed-off-by: Junio C Hamano --- builtin/fetch.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'builtin/fetch.c') diff --git a/builtin/fetch.c b/builtin/fetch.c index 3167b055d1..1c740d5aac 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1619,7 +1619,7 @@ static void report_set_head(const char *remote, const char *head_name, static int set_head(const struct ref *remote_refs, struct remote *remote) { - int result = 0, create_only, is_bare, was_detached; + int result = 0, create_only, baremirror, was_detached; struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, b_local_head = STRBUF_INIT; int follow_remote_head = remote->follow_remote_head; @@ -1655,9 +1655,9 @@ static int set_head(const struct ref *remote_refs, struct remote *remote) if (!head_name) goto cleanup; - is_bare = is_bare_repository(); - create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare; - if (is_bare) { + baremirror = is_bare_repository() && remote->mirror; + create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !baremirror; + if (baremirror) { strbuf_addstr(&b_head, "HEAD"); strbuf_addf(&b_remote_head, "refs/heads/%s", head_name); } else { @@ -1665,7 +1665,7 @@ static int set_head(const struct ref *remote_refs, struct remote *remote) strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name); } /* make sure it's valid */ - if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) { + if (!baremirror && !refs_ref_exists(refs, b_remote_head.buf)) { result = 1; goto cleanup; } -- cgit v1.2.3