Skip to content

Commit

Permalink
fetch set_head: fix non-mirror remotes in bare repositories
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ferdinandyb authored and dscho committed Feb 6, 2025
1 parent 5cc2d6c commit c4c6f21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
15 changes: 8 additions & 7 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1618,9 +1618,9 @@ static void report_set_head(const char *remote, const char *head_name,
}

static int set_head(const struct ref *remote_refs, int follow_remote_head,
const char *no_warn_branch)
const char *no_warn_branch, int mirror)
{
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;
const char *remote = gtransport->remote->name;
Expand Down Expand Up @@ -1655,17 +1655,17 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head,

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() && 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 {
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, 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;
}
Expand Down Expand Up @@ -1925,7 +1925,8 @@ static int do_fetch(struct transport *transport,
}
}
if (set_head(remote_refs, transport->remote->follow_remote_head,
transport->remote->no_warn_branch))
transport->remote->no_warn_branch,
transport->remote->mirror))
;
/*
* Way too many cases where this can go wrong
Expand Down
10 changes: 10 additions & 0 deletions t/t5505-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ test_expect_success 'add --mirror setting HEAD' '
)
'

test_expect_success 'non-mirror fetch does not interfere with mirror' '
mkdir headnotmain &&
(
cd headnotmain &&
git init --bare -b notmain &&
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 &&
Expand Down
13 changes: 13 additions & 0 deletions t/t5510-fetch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ 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" '
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 &&
Expand Down

0 comments on commit c4c6f21

Please sign in to comment.