Skip to content

Commit c3fffcf

Browse files
committed
Merge branch 'bf/fetch-set-head-fix'
Fetching into a bare repository incorrectly assumed it always used a mirror layout when deciding to update remote-tracking HEAD, which has been corrected. * bf/fetch-set-head-fix: fetch set_head: fix non-mirror remotes in bare repositories fetch set_head: refactor to use remote directly
2 parents 09e74b0 + 93dc164 commit c3fffcf

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

builtin/fetch.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,13 +1617,13 @@ static void report_set_head(const char *remote, const char *head_name,
16171617
strbuf_release(&buf_prefix);
16181618
}
16191619

1620-
static int set_head(const struct ref *remote_refs, int follow_remote_head,
1621-
const char *no_warn_branch)
1620+
static int set_head(const struct ref *remote_refs, struct remote *remote)
16221621
{
1623-
int result = 0, create_only, is_bare, was_detached;
1622+
int result = 0, create_only, baremirror, was_detached;
16241623
struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
16251624
b_local_head = STRBUF_INIT;
1626-
const char *remote = gtransport->remote->name;
1625+
int follow_remote_head = remote->follow_remote_head;
1626+
const char *no_warn_branch = remote->no_warn_branch;
16271627
char *head_name = NULL;
16281628
struct ref *ref, *matches;
16291629
struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
@@ -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() && 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 {
1664-
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
1665-
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
1664+
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote->name);
1665+
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
}
@@ -1678,7 +1678,7 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head,
16781678
if (verbosity >= 0 &&
16791679
follow_remote_head == FOLLOW_REMOTE_WARN &&
16801680
(!no_warn_branch || strcmp(no_warn_branch, head_name)))
1681-
report_set_head(remote, head_name, &b_local_head, was_detached);
1681+
report_set_head(remote->name, head_name, &b_local_head, was_detached);
16821682

16831683
cleanup:
16841684
free(head_name);
@@ -1924,8 +1924,7 @@ static int do_fetch(struct transport *transport,
19241924
"you need to specify exactly one branch with the --set-upstream option"));
19251925
}
19261926
}
1927-
if (set_head(remote_refs, transport->remote->follow_remote_head,
1928-
transport->remote->no_warn_branch))
1927+
if (set_head(remote_refs, transport->remote))
19291928
;
19301929
/*
19311930
* 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+
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
@@ -81,6 +81,23 @@ test_expect_success "fetch test remote HEAD" '
8181
branch=$(git rev-parse refs/remotes/origin/main) &&
8282
test "z$head" = "z$branch"'
8383

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

0 commit comments

Comments
 (0)