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 --- t/t5505-remote.sh | 10 ++++++++++ t/t5510-fetch.sh | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 't') diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 519f7973e3..66e373f71d 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -589,6 +589,16 @@ test_expect_success 'add --mirror setting HEAD' ' ) ' +test_expect_success 'non-mirror fetch does not interfere with mirror' ' + test_when_finished rm -rf headnotmain && + ( + git init --bare -b notmain headnotmain && + cd headnotmain && + git remote add -f other ../two && + test "$(git symbolic-ref HEAD)" = "refs/heads/notmain" + ) +' + test_expect_success 'add --mirror=fetch' ' mkdir mirror-fetch && git init -b main mirror-fetch/parent && diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh index 2d9587059f..c9d7b46c87 100755 --- a/t/t5510-fetch.sh +++ b/t/t5510-fetch.sh @@ -84,6 +84,23 @@ test_expect_success "fetch test remote HEAD" ' branch=$(git rev-parse refs/remotes/origin/main) && test "z$head" = "z$branch"' +test_expect_success "fetch test remote HEAD in bare repository" ' + test_when_finished rm -rf barerepo && + ( + cd "$D" && + git init --bare barerepo && + cd barerepo && + git remote add upstream ../two && + git fetch upstream && + git rev-parse --verify refs/remotes/upstream/HEAD && + git rev-parse --verify refs/remotes/upstream/main && + head=$(git rev-parse refs/remotes/upstream/HEAD) && + branch=$(git rev-parse refs/remotes/upstream/main) && + test "z$head" = "z$branch" + ) +' + + test_expect_success "fetch test remote HEAD change" ' cd "$D" && cd two && -- cgit v1.3