Skip to content

Commit 00a1b28

Browse files
committed
remote: use the configured default branch name when appropriate
When guessing the default branch name of a remote, and there are no refs to guess from, we want to go with the preference specified by the user for the fall-back, i.e. the default name to be used for the initial branch of new repositories (because as far as the user is concerned, a remote that has no branches yet is a new repository). At the same time, when talking to an older Git server that does not report a symref for `HEAD` (but instead reports a commit hash), let's try to guess the configured default branch name first. If it does not match the reported commit hash, let's fall back to `master` as before. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent c0d74ce commit 00a1b28

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

remote.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ static void read_branches_file(struct remote *remote)
276276

277277
/*
278278
* The branches file would have URL and optionally
279-
* #branch specified. The "master" (or specified) branch is
279+
* #branch specified. The main (or specified) branch is
280280
* fetched and stored in the local branch matching the
281281
* remote name.
282282
*/
283283
frag = strchr(buf.buf, '#');
284284
if (frag)
285285
*(frag++) = '\0';
286286
else
287-
frag = "master";
287+
frag = (char *)git_default_branch_name();
288288

289289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290290
strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
@@ -2097,8 +2097,16 @@ struct ref *guess_remote_head(const struct ref *head,
20972097
if (head->symref)
20982098
return copy_ref(find_ref_by_name(refs, head->symref));
20992099

2100-
/* If refs/heads/master could be right, it is. */
2100+
/* If a remote branch exists with the default branch name, let's use it. */
21012101
if (!all) {
2102+
char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
2103+
2104+
r = find_ref_by_name(refs, ref);
2105+
free(ref);
2106+
if (r && oideq(&r->old_oid, &head->old_oid))
2107+
return copy_ref(r);
2108+
2109+
/* Fall back to the hard-coded historical default */
21022110
r = find_ref_by_name(refs, "refs/heads/master");
21032111
if (r && oideq(&r->old_oid, &head->old_oid))
21042112
return copy_ref(r);

t/t5606-clone-options.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ test_expect_success 'guesses initial branch name correctly' '
4747
test_commit -C initial-branch no-spoilers &&
4848
git -C initial-branch branch abc guess &&
4949
git clone initial-branch is-it &&
50-
test refs/heads/guess = $(git -C is-it symbolic-ref HEAD)
50+
test refs/heads/guess = $(git -C is-it symbolic-ref HEAD) &&
51+
52+
git -c init.defaultBranch=none init --bare no-head &&
53+
git -C initial-branch push ../no-head guess abc &&
54+
git clone no-head is-it2 &&
55+
test_must_fail git -C is-it2 symbolic-ref refs/remotes/origin/HEAD &&
56+
git -C no-head update-ref --no-deref HEAD refs/heads/guess &&
57+
git -c init.defaultBranch=guess clone no-head is-it3 &&
58+
test refs/remotes/origin/guess = \
59+
$(git -C is-it3 symbolic-ref refs/remotes/origin/HEAD)
5160
'
5261

5362
test_done

0 commit comments

Comments
 (0)