Skip to content

Commit 447cdec

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. Signed-off-by: Bence Ferdinandy <[email protected]> Reported-by: Christian Hesse <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbe8d30 commit 447cdec

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

builtin/fetch.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,9 +1618,9 @@ static void report_set_head(const char *remote, const char *head_name,
16181618
}
16191619

16201620
static int set_head(const struct ref *remote_refs, int follow_remote_head,
1621-
const char *no_warn_branch)
1621+
const char *no_warn_branch, int mirror)
16221622
{
1623-
int result = 0, create_only, is_bare, was_detached;
1623+
int result = 0, create_only, baremirror, was_detached;
16241624
struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
16251625
b_local_head = STRBUF_INIT;
16261626
const char *remote = gtransport->remote->name;
@@ -1655,17 +1655,17 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head,
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() && 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);
16651665
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, 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
}
@@ -1925,7 +1925,8 @@ static int do_fetch(struct transport *transport,
19251925
}
19261926
}
19271927
if (set_head(remote_refs, transport->remote->follow_remote_head,
1928-
transport->remote->no_warn_branch))
1928+
transport->remote->no_warn_branch,
1929+
transport->remote->mirror))
19291930
;
19301931
/*
19311932
* Way too many cases where this can go wrong

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+
mkdir headnotmain &&
594+
(
595+
cd headnotmain &&
596+
git init --bare -b notmain &&
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ 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+
cd "$D" &&
89+
git init --bare barerepo &&
90+
cd barerepo &&
91+
git remote add upstream ../two &&
92+
git fetch upstream &&
93+
git rev-parse --verify refs/remotes/upstream/HEAD &&
94+
git rev-parse --verify refs/remotes/upstream/main &&
95+
head=$(git rev-parse refs/remotes/upstream/HEAD) &&
96+
branch=$(git rev-parse refs/remotes/upstream/main) &&
97+
test "z$head" = "z$branch"'
98+
99+
87100
test_expect_success "fetch test remote HEAD change" '
88101
cd "$D" &&
89102
cd two &&

0 commit comments

Comments
 (0)