From: Junio C Hamano <gitster@pobox.com>
To: Elia Pinto <gitter.spiros@gmail.com>
Cc: git@vger.kernel.org, "Harald Nordgren" <haraldnordgren@gmail.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH v3] status: reword message for a missing upstream branch
Date: Thu, 19 Nov 2026 10:05:37 -0800 [thread overview]
Message-ID: <xmqqjym8y9vy.fsf@gitster.g> (raw)
In-Reply-To: <20261119055726.774485-1-gitter.spiros@gmail.com> (Elia Pinto's message of "Thu, 19 Nov 2026 06:57:26 +0100")
Elia Pinto <gitter.spiros@gmail.com> 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.<name>.pushRemote (or remote.pushDefault) configuration is
set to a remote that is different from branch.<name>.remote, because
"git push" would detect misconfiguration and immediately abort like
fatal: You are pushing to remote '<remote>', which is not the
upstream of your current branch '<name>', 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
prev parent reply other threads:[~2026-11-19 18:05 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-19 17:57 [PATCH] status: reword message for a missing upstream branch Elia Pinto
2026-10-19 18:04 ` Harald Nordgren
2026-10-19 21:42 ` Junio C Hamano
2026-10-20 7:29 ` Elia Pinto
2026-10-21 7:21 ` [PATCH v2] " Elia Pinto
2026-10-30 9:05 ` Elia Pinto
2026-11-17 21:27 ` Elia Pinto
2026-11-17 22:03 ` Harald Nordgren
2026-11-17 23:29 ` Junio C Hamano
2026-11-18 15:51 ` Junio C Hamano
2026-11-19 5:50 ` Elia Pinto
2026-11-19 16:41 ` Junio C Hamano
2026-11-19 5:57 ` [PATCH v3] " Elia Pinto
2026-11-19 18:05 ` Junio C Hamano [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqjym8y9vy.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitter.spiros@gmail.com \
--cc=haraldnordgren@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox