From: Junio C Hamano Date: Thu, 19 Nov 2026 18:05:37 GMT Subject: Re: [PATCH v3] status: reword message for a missing upstream branch Message-ID: In-Reply-To: <20261119055726.774485-1-gitter.spiros@gmail.com> Elia Pinto writes: > diff --git a/remote.c b/remote.c > index 49ee2ea318..7e26088f65 100644 > --- a/remote.c > +++ b/remote.c > @@ -2449,6 +2449,47 @@ static void format_branch_comparison(struct strbuf *sb, > } > } > > +/* > + * Would a plain "git push" create the remote-tracking ref "upstream"? > + * > + * Only if "push_ref", what @{push} resolves to, is the upstream. That > + * rules out push.default=nothing, push refspecs that do not include > + * the branch, and pushes that go elsewhere, e.g. a mirror remote when > + * the branch tracks a branch of a different name: @{push} does not > + * resolve or names another ref. > + * > + * Even then, "matching" only updates branches that already exist on > + * the remote, unless push refspecs or mirroring override it, as in > + * branch_get_push_1(). > + */ > +static bool push_creates(struct branch *branch, const char *push_ref, > + const char *upstream) > +{ > + struct remote *remote; > + > + if (!push_ref || strcmp(push_ref, upstream)) > + return false; > + remote = pushremote_get(pushremote_for_branch(branch, NULL)); > + if (remote && (remote->push.nr || remote->mirror)) > + return true; > + return repo_config_values(the_repository)->push_default != > + PUSH_DEFAULT_MATCHING; > +} > + > +static void format_missing_upstream(struct strbuf *sb, struct branch *branch, > + const char *name, const char *push_ref, > + const char *upstream) > +{ > + strbuf_addf(sb, _("Your branch is set up to track '%s', " > + "which does not exist.\n"), name); > + if (!advice_enabled(ADVICE_STATUS_HINTS)) > + return; > + if (push_creates(branch, push_ref, upstream)) > + strbuf_addstr(sb, _(" (use \"git push\" to create it)\n")); > + strbuf_addstr(sb, _(" (use \"git branch --unset-upstream\" " > + "to stop tracking it)\n")); > +} OK. This kills two birds with one stone. An early return based on ADVICE_STATUS_HINTS omits the call to push_creates() that is only relevant when giving the advance, and the overly long lines in the caller due to a deeply nested block are now much more readable. > /* > * Return true when there is anything to report, otherwise false. > */ > @@ -2510,12 +2551,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb, > > if (cmp < 0) { > if (is_upstream) { > - strbuf_addf(sb, > - _("Your branch is based on '%s', but the upstream is gone.\n"), > - short_ref); > - if (advice_enabled(ADVICE_STATUS_HINTS)) > - strbuf_addstr(sb, > - _(" (use \"git branch --unset-upstream\" to fixup)\n")); > + format_missing_upstream(sb, branch, short_ref, > + push_ref, full_ref); > reported = 1; > } > free(full_ref); When the push.default configuration is set to 'upstream', and the branch..pushRemote (or remote.pushDefault) configuration is set to a remote that is different from branch..remote, because "git push" would detect misconfiguration and immediately abort like fatal: You are pushing to remote '', which is not the upstream of your current branch '', without telling me what to push to update which remote branch. Yet, we suggest '(use "git push" to create it)' with the advice, it seems (reproduction added at the end). branch_get_push_1() returns the upstream for PUSH_DEFAULT_UPSTREAM without checking whether the push remote matches the upstream remote, and that is what comes back from branch_get_push() and stored in push_ref. push_creates() checks !remote->push.nr and !remote->mirror and push_default != PUSH_DEFAULT_MATCHING to decide if "git push" would create the upstream we want, which says "yes" unfortunately in this situation. But in builtin/push.c, setup_default_push_refspecs() has a guard "if (!same_remote)" in PUSH_DEFAULT_UPSTREAM before allowing the branch to be used as the destination. That is what makes the advice message incorrect in this case. I am not sure what the right fix should be. Intuitively, the correct change may be to update branch_get_push_1() to pay attention to the remote being the same (in other words, we are not in a triangular workflow), as that would match what "git push" does. Such a change, however, may have fallouts and break other callers of branch_get_push() that have been assuming the current behaviour. Adding a band-aid guard to push_creates() to specifically detect this particular case would be safer, but it is far less satisfactory and makes me feel dirty, playing whack-a-mole with the code. Perhaps branch_get_push_simple(), which is a relatively recent addition, might point us in a good direction? I dunno. t/t6040-tracking-info.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git c/t/t6040-tracking-info.sh w/t/t6040-tracking-info.sh index 1f0583b391..4752559280 100755 --- c/t/t6040-tracking-info.sh +++ w/t/t6040-tracking-info.sh @@ -221,6 +221,14 @@ test_expect_success 'status (missing upstream, pushRemote is another remote)' ' test_cmp expect-no-push actual ' +test_expect_success 'status (missing upstream, push.default=upstream with pushRemote)' ' + test_must_fail git -C empty-clone -c push.default=upstream \ + -c branch.main.pushRemote=fork push && + git -C empty-clone -c push.default=upstream \ + -c branch.main.pushRemote=fork status >actual && + test_cmp expect-no-push actual +' + test_expect_success 'status (missing upstream, advice.statusHints=false)' ' cat >expect <<-EOF && On branch main