remote: allow `guess_remote_head()` to suppress advice

The `repo_default_branch_name()` invoked through `guess_remote_head()` is configured to always display the default branch advice message. Adapt `guess_remote_head()` to accept flags and convert the `all` parameter to a flag. Add the `REMOTE_GUESS_HEAD_QUIET` flag to to enable suppression of advice messages. Call sites are updated accordingly. Signed-off-by: Justin Tobler <jltobler@gmail.com> Acked-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler committed Mar 24, 2025 at 19:51 UTC d5d284df910b5f03681b480ae061bb1435b3b4b2
4 files changed +15 -10
builtin/fetch.c
+1 -1
@@ -1638,7 +1638,7 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
1638
1639 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
1640 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
1641 - fetch_map, 1);
1641 + fetch_map, REMOTE_GUESS_HEAD_ALL);
1642 for (ref = matches; ref; ref = ref->next) {
1643 string_list_append(&heads, strip_refshead(ref->name));
1644 }
builtin/remote.c
+1 -1
@@ -511,7 +511,7 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
511
512 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
513 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
514 - fetch_map, 1);
514 + fetch_map, REMOTE_GUESS_HEAD_ALL);
515 for (ref = matches; ref; ref = ref->next)
516 string_list_append(&states->heads, abbrev_branch(ref->name));
517
remote.c
+6 -4
@@ -2297,7 +2297,7 @@ struct ref *get_local_heads(void)
2297
2298 struct ref *guess_remote_head(const struct ref *head,
2299 const struct ref *refs,
2300 - int all)
2300 + unsigned flags)
2301 {
2302 const struct ref *r;
2303 struct ref *list = NULL;
@@ -2315,8 +2315,10 @@ struct ref *guess_remote_head(const struct ref *head,
2315 return copy_ref(find_ref_by_name(refs, head->symref));
2316
2317 /* 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);
2322 char *ref = xstrfmt("refs/heads/%s", default_branch);
2323
2324 r = find_ref_by_name(refs, ref);
@@ -2339,7 +2341,7 @@ struct ref *guess_remote_head(const struct ref *head,
2341 oideq(&r->old_oid, &head->old_oid)) {
2342 *tail = copy_ref(r);
2343 tail = &((*tail)->next);
2342 - if (!all)
2344 + if (!(flags & REMOTE_GUESS_HEAD_ALL))
2345 break;
2346 }
2347 }
remote.h
+7 -4
@@ -387,15 +387,18 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
387 int show_divergence_advice);
388
389 struct ref *get_local_heads(void);
390 +
391 /*
392 * 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.
396 */
397 +#define REMOTE_GUESS_HEAD_ALL (1 << 0)
398 +#define REMOTE_GUESS_HEAD_QUIET (1 << 1)
399 struct ref *guess_remote_head(const struct ref *head,
400 const struct ref *refs,
398 - int all);
401 + unsigned flags);
402
403 /* Return refs which no longer exist on remote */
404 struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map);