Skip to contentSkip to conversation

status: reword message for a missing upstream branch

RFC PATCH In review Elia Pinto 2 participants 2 messages Oct 9

lore
Share & tools
lore

Elia Pinto

author
#0 lore raw

RFC PATCH 1/1 status: reword message for a missing upstream branch

Clone an empty repository, make a commit and run "git status":

    $ git init --bare empty.git
    $ git clone empty.git work && cd work
    $ git commit --allow-empty -m initial
    $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)

The upstream is not gone, it was never there: the remote is empty, so origin/master has not been created yet. The advice is also not what the user needs; "git push" creates origin/master and the message goes away.

The message was added in f2e087395b (branch: report invalid tracking branch as gone, 2013-08-26), assuming that a configured upstream that cannot be resolved must have been deleted. But "git clone" of an empty repository configures the upstream anyway, and Git has no way to tell "deleted" from "not created yet".

Since we cannot tell the two cases apart, change the message for both, including the "deleted" case that has been reported as "gone" since 2013, so that it is correct either way:

Your branch is set up to track 'origin/master', which does not exist.
(use "git push" to create it)
(use "git branch --unset-upstream" to stop tracking it)

Only suggest "git push" when it would actually create the upstream, that is when the push destination of the branch (what "@{push}" resolves to) is the upstream itself. This is not the case, for example, with push.default set to "nothing", or when pushing to a different remote (branch.<name>.pushRemote or remote.pushDefault). It is also not the case with push.default set to "matching": it only pushes branches that already exist on the remote, so it would not create the missing upstream. In all these cases only suggest "git branch --unset-upstream".

"git checkout" prints the same message, so it changes too. The "[gone]" shown by "git status -sb", "git branch -vv" and "%(upstream:track)" is not changed, as scripts may depend on it.

---
This is an RFC mainly about the wording. The case of a freshly cloned empty repository has been confusing users for a while, e.g.
https://stackoverflow.com/questions/24870145

I could not find a previous discussion of this case on the list. Opinions on whether "status:" or "remote:" is the better area prefix are also welcome, as "git checkout" shows the same message.

 remote.c                 |  34 +++++++++--
 t/t6040-tracking-info.sh | 120 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 146 insertions(+), 8 deletions(-)
remote.c +28 -6
index 71170f36a9..4ed4d1ee2f 100644
@@ -2442,6 +2442,31 @@ static void format_branch_comparison(struct strbuf *sb,
 	}
 }
 
+/*
+ * Would a plain "git push" create the remote-tracking ref "upstream"?
+ * Not with "matching", which only updates branches that already exist
+ * on the remote.
+ */
+static bool push_creates(const char *push_ref, const char *upstream)
+{
+	return push_ref && !strcmp(push_ref, upstream) &&
+	       repo_config_values(the_repository)->push_default !=
+	       PUSH_DEFAULT_MATCHING;
+}
+
+static void format_missing_upstream(struct strbuf *sb, const char *name,
+				    bool suggest_push)
+{
+	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 (suggest_push)
+		strbuf_addstr(sb, _("  (use \"git push\" to create it)\n"));
+	strbuf_addstr(sb, _("  (use \"git branch --unset-upstream\" "
+			    "to stop tracking it)\n"));
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2503,12 +2528,9 @@ 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, short_ref,
+							push_creates(push_ref,
+								     full_ref));
 				reported = 1;
 			}
 			free(full_ref);
t/t6040-tracking-info.sh +118 -2

132 lines not on this page · raw message

base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd

Signature · 1 line
2.56.0.116.g6de20f6092.dirty
#1 lore raw
Quoted 10 lines

Clone an empty repository, make a commit and run "git status":

    $ git init --bare empty.git
    $ git clone empty.git work && cd work
    $ git commit --allow-empty -m initial
    $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
Quoted 2 lines

The upstream is not gone, it was never there: the remote is empty, so origin/master has not been created yet.

The idea makes sense to me!

Harald