Skip to content

Commit a8c2077

Browse files
committed
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
2 parents d690c44 + ec0f362 commit a8c2077

File tree

10 files changed

+44
-13
lines changed

10 files changed

+44
-13
lines changed

advice.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static struct {
5151
[ADVICE_AM_WORK_DIR] = { "amWorkDir" },
5252
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName" },
5353
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
54+
[ADVICE_DEFAULT_BRANCH_NAME] = { "defaultBranchName" },
5455
[ADVICE_DETACHED_HEAD] = { "detachedHead" },
5556
[ADVICE_DIVERGING] = { "diverging" },
5657
[ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum advice_type {
1818
ADVICE_AM_WORK_DIR,
1919
ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
2020
ADVICE_COMMIT_BEFORE_MERGE,
21+
ADVICE_DEFAULT_BRANCH_NAME,
2122
ADVICE_DETACHED_HEAD,
2223
ADVICE_DIVERGING,
2324
ADVICE_FETCH_SET_HEAD_WARN,

builtin/clone.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,9 @@ static struct ref *wanted_peer_refs(struct clone_opts *opts,
452452
if (head)
453453
tail_link_ref(head, &tail);
454454
if (option_single_branch)
455-
refs = to_free = guess_remote_head(head, refs, 0);
455+
refs = to_free =
456+
guess_remote_head(head, refs,
457+
REMOTE_GUESS_HEAD_QUIET);
456458
} else if (option_single_branch) {
457459
local_refs = NULL;
458460
tail = &local_refs;
@@ -1525,7 +1527,8 @@ int cmd_clone(int argc,
15251527
}
15261528

15271529
remote_head = find_ref_by_name(refs, "HEAD");
1528-
remote_head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
1530+
remote_head_points_at = guess_remote_head(remote_head, mapped_refs,
1531+
REMOTE_GUESS_HEAD_QUIET);
15291532

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

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
16381638

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

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
511511

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

refs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ char *repo_default_branch_name(struct repository *r, int quiet)
664664
if (!ret) {
665665
ret = xstrdup("master");
666666
if (!quiet)
667-
advise(_(default_branch_name_advice), ret);
667+
advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
668+
_(default_branch_name_advice), ret);
668669
}
669670

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

remote.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ struct ref *get_local_heads(void)
22972297

22982298
struct ref *guess_remote_head(const struct ref *head,
22992299
const struct ref *refs,
2300-
int all)
2300+
unsigned flags)
23012301
{
23022302
const struct ref *r;
23032303
struct ref *list = NULL;
@@ -2315,8 +2315,10 @@ struct ref *guess_remote_head(const struct ref *head,
23152315
return copy_ref(find_ref_by_name(refs, head->symref));
23162316

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

23222324
r = find_ref_by_name(refs, ref);
@@ -2339,7 +2341,7 @@ struct ref *guess_remote_head(const struct ref *head,
23392341
oideq(&r->old_oid, &head->old_oid)) {
23402342
*tail = copy_ref(r);
23412343
tail = &((*tail)->next);
2342-
if (!all)
2344+
if (!(flags & REMOTE_GUESS_HEAD_ALL))
23432345
break;
23442346
}
23452347
}

remote.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,18 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
387387
int show_divergence_advice);
388388

389389
struct ref *get_local_heads(void);
390+
390391
/*
391392
* Find refs from a list which are likely to be pointed to by the given HEAD
392-
* ref. If 'all' is false, returns the most likely ref; otherwise, returns a
393-
* list of all candidate refs. If no match is found (or 'head' is NULL),
394-
* returns NULL. All returns are newly allocated and should be freed.
393+
* ref. If REMOTE_GUESS_HEAD_ALL is set, return a list of all candidate refs;
394+
* otherwise, return the most likely ref. If no match is found (or 'head' is
395+
* NULL), returns NULL. All returns are newly allocated and should be freed.
395396
*/
397+
#define REMOTE_GUESS_HEAD_ALL (1 << 0)
398+
#define REMOTE_GUESS_HEAD_QUIET (1 << 1)
396399
struct ref *guess_remote_head(const struct ref *head,
397400
const struct ref *refs,
398-
int all);
401+
unsigned flags);
399402

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

t/t0001-init.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,14 @@ test_expect_success 'advice on unconfigured init.defaultBranch' '
830830
test_grep "<YELLOW>hint: " decoded
831831
'
832832

833+
test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
834+
test_when_finished "rm -rf no-advice" &&
835+
836+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
837+
git -c advice.defaultBranchName=false init no-advice 2>err &&
838+
test_grep ! "hint: " err
839+
'
840+
833841
test_expect_success 'overridden default main branch name (env)' '
834842
test_config_global init.defaultBranch nmb &&
835843
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&

t/t5607-clone-bundle.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,16 @@ test_expect_success 'git bundle v3 rejects unknown capabilities' '
211211
test_grep "unknown capability .unknown=silly." output
212212
'
213213

214+
test_expect_success 'cloning bundle suppresses default branch name advice' '
215+
test_when_finished "rm -rf bundle-repo clone-repo" &&
216+
217+
git init bundle-repo &&
218+
git -C bundle-repo commit --allow-empty -m init &&
219+
git -C bundle-repo bundle create repo.bundle --all &&
220+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
221+
git clone --single-branch bundle-repo/repo.bundle clone-repo 2>err &&
222+
223+
test_grep ! "hint: " err
224+
'
225+
214226
test_done

0 commit comments

Comments
 (0)