Skip to content

Commit 93dc164

Browse files
ferdinandybgitster
authored andcommitted
fetch set_head: fix non-mirror remotes in bare repositories
In b1b713f (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/<nonmirrorremote>/HEAD instead. Reported-by: Christian Hesse <[email protected]> Signed-off-by: Bence Ferdinandy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 638060d commit 93dc164

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

builtin/fetch.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ static void report_set_head(const char *remote, const char *head_name,
16191619

16201620
static int set_head(const struct ref *remote_refs, struct remote *remote)
16211621
{
1622-
int result = 0, create_only, is_bare, was_detached;
1622+
int result = 0, create_only, baremirror, was_detached;
16231623
struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
16241624
b_local_head = STRBUF_INIT;
16251625
int follow_remote_head = remote->follow_remote_head;
@@ -1655,17 +1655,17 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
16551655

16561656
if (!head_name)
16571657
goto cleanup;
1658-
is_bare = is_bare_repository();
1659-
create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare;
1660-
if (is_bare) {
1658+
baremirror = is_bare_repository() && remote->mirror;
1659+
create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !baremirror;
1660+
if (baremirror) {
16611661
strbuf_addstr(&b_head, "HEAD");
16621662
strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
16631663
} else {
16641664
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote->name);
16651665
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name);
16661666
}
16671667
/* make sure it's valid */
1668-
if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
1668+
if (!baremirror && !refs_ref_exists(refs, b_remote_head.buf)) {
16691669
result = 1;
16701670
goto cleanup;
16711671
}

t/t5505-remote.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,16 @@ test_expect_success 'add --mirror setting HEAD' '
589589
)
590590
'
591591

592+
test_expect_success 'non-mirror fetch does not interfere with mirror' '
593+
test_when_finished rm -rf headnotmain &&
594+
(
595+
git init --bare -b notmain headnotmain &&
596+
cd headnotmain &&
597+
git remote add -f other ../two &&
598+
test "$(git symbolic-ref HEAD)" = "refs/heads/notmain"
599+
)
600+
'
601+
592602
test_expect_success 'add --mirror=fetch' '
593603
mkdir mirror-fetch &&
594604
git init -b main mirror-fetch/parent &&

t/t5510-fetch.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ test_expect_success "fetch test remote HEAD" '
8484
branch=$(git rev-parse refs/remotes/origin/main) &&
8585
test "z$head" = "z$branch"'
8686

87+
test_expect_success "fetch test remote HEAD in bare repository" '
88+
test_when_finished rm -rf barerepo &&
89+
(
90+
cd "$D" &&
91+
git init --bare barerepo &&
92+
cd barerepo &&
93+
git remote add upstream ../two &&
94+
git fetch upstream &&
95+
git rev-parse --verify refs/remotes/upstream/HEAD &&
96+
git rev-parse --verify refs/remotes/upstream/main &&
97+
head=$(git rev-parse refs/remotes/upstream/HEAD) &&
98+
branch=$(git rev-parse refs/remotes/upstream/main) &&
99+
test "z$head" = "z$branch"
100+
)
101+
'
102+
103+
87104
test_expect_success "fetch test remote HEAD change" '
88105
cd "$D" &&
89106
cd two &&

0 commit comments

Comments
 (0)