Merge branch 'jt/clone-guess-remote-head-fix'

"git clone" still gave the message about the default branch name;
this message has been turned into an advice message that can be
turned off.

* jt/clone-guess-remote-head-fix:
  advice: allow disabling default branch name advice
  builtin/clone: suppress unexpected default branch advice
  remote: allow `guess_remote_head()` to suppress advice
main
Junio C Hamano 2025-04-15 13:50:16 -07:00
commit a8c207797f
10 changed files with 44 additions and 13 deletions

View File

@ -51,6 +51,7 @@ static struct {
[ADVICE_AM_WORK_DIR] = { "amWorkDir" }, [ADVICE_AM_WORK_DIR] = { "amWorkDir" },
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName" }, [ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName" },
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" }, [ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
[ADVICE_DEFAULT_BRANCH_NAME] = { "defaultBranchName" },
[ADVICE_DETACHED_HEAD] = { "detachedHead" }, [ADVICE_DETACHED_HEAD] = { "detachedHead" },
[ADVICE_DIVERGING] = { "diverging" }, [ADVICE_DIVERGING] = { "diverging" },
[ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" }, [ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },

View File

@ -18,6 +18,7 @@ enum advice_type {
ADVICE_AM_WORK_DIR, ADVICE_AM_WORK_DIR,
ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME, ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
ADVICE_COMMIT_BEFORE_MERGE, ADVICE_COMMIT_BEFORE_MERGE,
ADVICE_DEFAULT_BRANCH_NAME,
ADVICE_DETACHED_HEAD, ADVICE_DETACHED_HEAD,
ADVICE_DIVERGING, ADVICE_DIVERGING,
ADVICE_FETCH_SET_HEAD_WARN, ADVICE_FETCH_SET_HEAD_WARN,

View File

@ -452,7 +452,9 @@ static struct ref *wanted_peer_refs(struct clone_opts *opts,
if (head) if (head)
tail_link_ref(head, &tail); tail_link_ref(head, &tail);
if (option_single_branch) if (option_single_branch)
refs = to_free = guess_remote_head(head, refs, 0); refs = to_free =
guess_remote_head(head, refs,
REMOTE_GUESS_HEAD_QUIET);
} else if (option_single_branch) { } else if (option_single_branch) {
local_refs = NULL; local_refs = NULL;
tail = &local_refs; tail = &local_refs;
@ -1525,7 +1527,8 @@ int cmd_clone(int argc,
} }


remote_head = find_ref_by_name(refs, "HEAD"); remote_head = find_ref_by_name(refs, "HEAD");
remote_head_points_at = guess_remote_head(remote_head, mapped_refs, 0); remote_head_points_at = guess_remote_head(remote_head, mapped_refs,
REMOTE_GUESS_HEAD_QUIET);


if (option_branch) { if (option_branch) {
our_head_points_at = find_remote_branch(mapped_refs, option_branch); our_head_points_at = find_remote_branch(mapped_refs, option_branch);

View File

@ -1638,7 +1638,7 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)


get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
fetch_map, 1); fetch_map, REMOTE_GUESS_HEAD_ALL);
for (ref = matches; ref; ref = ref->next) { for (ref = matches; ref; ref = ref->next) {
string_list_append(&heads, strip_refshead(ref->name)); string_list_append(&heads, strip_refshead(ref->name));
} }

View File

@ -511,7 +511,7 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat


get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
fetch_map, 1); fetch_map, REMOTE_GUESS_HEAD_ALL);
for (ref = matches; ref; ref = ref->next) for (ref = matches; ref; ref = ref->next)
string_list_append(&states->heads, abbrev_branch(ref->name)); string_list_append(&states->heads, abbrev_branch(ref->name));



3
refs.c
View File

@ -664,7 +664,8 @@ char *repo_default_branch_name(struct repository *r, int quiet)
if (!ret) { if (!ret) {
ret = xstrdup("master"); ret = xstrdup("master");
if (!quiet) if (!quiet)
advise(_(default_branch_name_advice), ret); advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
_(default_branch_name_advice), ret);
} }


full_ref = xstrfmt("refs/heads/%s", ret); full_ref = xstrfmt("refs/heads/%s", ret);

View File

@ -2297,7 +2297,7 @@ struct ref *get_local_heads(void)


struct ref *guess_remote_head(const struct ref *head, struct ref *guess_remote_head(const struct ref *head,
const struct ref *refs, const struct ref *refs,
int all) unsigned flags)
{ {
const struct ref *r; const struct ref *r;
struct ref *list = NULL; struct ref *list = NULL;
@ -2315,8 +2315,10 @@ struct ref *guess_remote_head(const struct ref *head,
return copy_ref(find_ref_by_name(refs, head->symref)); return copy_ref(find_ref_by_name(refs, head->symref));


/* If a remote branch exists with the default branch name, let's use it. */ /* If a remote branch exists with the default branch name, let's use it. */
if (!all) { if (!(flags & REMOTE_GUESS_HEAD_ALL)) {
char *default_branch = repo_default_branch_name(the_repository, 0); char *default_branch =
repo_default_branch_name(the_repository,
flags & REMOTE_GUESS_HEAD_QUIET);
char *ref = xstrfmt("refs/heads/%s", default_branch); char *ref = xstrfmt("refs/heads/%s", default_branch);


r = find_ref_by_name(refs, ref); r = find_ref_by_name(refs, ref);
@ -2339,7 +2341,7 @@ struct ref *guess_remote_head(const struct ref *head,
oideq(&r->old_oid, &head->old_oid)) { oideq(&r->old_oid, &head->old_oid)) {
*tail = copy_ref(r); *tail = copy_ref(r);
tail = &((*tail)->next); tail = &((*tail)->next);
if (!all) if (!(flags & REMOTE_GUESS_HEAD_ALL))
break; break;
} }
} }

View File

@ -387,15 +387,18 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
int show_divergence_advice); int show_divergence_advice);


struct ref *get_local_heads(void); struct ref *get_local_heads(void);

/* /*
* Find refs from a list which are likely to be pointed to by the given HEAD * Find refs from a list which are likely to be pointed to by the given HEAD
* ref. If 'all' is false, returns the most likely ref; otherwise, returns a * ref. If REMOTE_GUESS_HEAD_ALL is set, return a list of all candidate refs;
* list of all candidate refs. If no match is found (or 'head' is NULL), * otherwise, return the most likely ref. If no match is found (or 'head' is
* returns NULL. All returns are newly allocated and should be freed. * NULL), returns NULL. All returns are newly allocated and should be freed.
*/ */
#define REMOTE_GUESS_HEAD_ALL (1 << 0)
#define REMOTE_GUESS_HEAD_QUIET (1 << 1)
struct ref *guess_remote_head(const struct ref *head, struct ref *guess_remote_head(const struct ref *head,
const struct ref *refs, const struct ref *refs,
int all); unsigned flags);


/* Return refs which no longer exist on remote */ /* Return refs which no longer exist on remote */
struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map); struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map);

View File

@ -830,6 +830,14 @@ test_expect_success 'advice on unconfigured init.defaultBranch' '
test_grep "<YELLOW>hint: " decoded test_grep "<YELLOW>hint: " decoded
' '


test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
test_when_finished "rm -rf no-advice" &&

GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
git -c advice.defaultBranchName=false init no-advice 2>err &&
test_grep ! "hint: " err
'

test_expect_success 'overridden default main branch name (env)' ' test_expect_success 'overridden default main branch name (env)' '
test_config_global init.defaultBranch nmb && test_config_global init.defaultBranch nmb &&
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env && GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&

View File

@ -211,4 +211,16 @@ test_expect_success 'git bundle v3 rejects unknown capabilities' '
test_grep "unknown capability .unknown=silly." output test_grep "unknown capability .unknown=silly." output
' '


test_expect_success 'cloning bundle suppresses default branch name advice' '
test_when_finished "rm -rf bundle-repo clone-repo" &&

git init bundle-repo &&
git -C bundle-repo commit --allow-empty -m init &&
git -C bundle-repo bundle create repo.bundle --all &&
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
git clone --single-branch bundle-repo/repo.bundle clone-repo 2>err &&

test_grep ! "hint: " err
'

test_done test_done