@matheusbsbs / matheus-bagliano / commits / a471214bd6
Fork 0

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 24, 2020 at 14:46 UTC a471214bd610bb415a37d5b2c3506b83549acd57
2 files changed +21 -4
remote.c
+11 -3
@@ -276,7 +276,7 @@ static void read_branches_file(struct remote *remote)
276
277 /*
278 * The branches file would have URL and optionally
279 - * #branch specified. The "master" (or specified) branch is
279 + * #branch specified. The default (or specified) branch is
280 * fetched and stored in the local branch matching the
281 * remote name.
282 */
@@ -284,7 +284,7 @@ static void read_branches_file(struct remote *remote)
284 if (frag)
285 *(frag++) = '\0';
286 else
287 - frag = "master";
287 + frag = (char *)git_default_branch_name();
288
289 add_url_alias(remote, strbuf_detach(&buf, NULL));
290 strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
@@ -2097,8 +2097,16 @@ struct ref *guess_remote_head(const struct ref *head,
2097 if (head->symref)
2098 return copy_ref(find_ref_by_name(refs, head->symref));
2099
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. */
2101 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 */
2110 r = find_ref_by_name(refs, "refs/heads/master");
2111 if (r && oideq(&r->old_oid, &head->old_oid))
2112 return copy_ref(r);
t/t5606-clone-options.sh
+10 -1
@@ -47,7 +47,16 @@ test_expect_success 'guesses initial branch name correctly' '
47 test_commit -C initial-branch no-spoilers &&
48 git -C initial-branch branch abc guess &&
49 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)
60 '
61
62 test_done