git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] push: reorganize get_upstream_ref

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jul 19, 2021, 03:47 UTC
Message-ID
<20210719034758.1288766-2-felipe.contreras@gmail.com>
In-Reply-To
<20210719034758.1288766-1-felipe.contreras@gmail.com>

Instead of dying inside the function if there's no upstream branch configured, return NULL and let the callers do that themselves.

In the next patch we won't always want to die.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin/push.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index e8b10a9b7e..3ee7d6ada1 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -185,16 +185,10 @@ static const char message_detached_head_die[] =
 	   "\n"
 	   "    git push %s HEAD:<name-of-remote-branch>\n");
 
-static const char *get_upstream_ref(struct branch *branch, const char *remote_name)
+static const char *get_upstream_ref(struct branch *branch)
 {
 	if (!branch->merge_nr || !branch->merge || !branch->remote_name)
-		die(_("The current branch %s has no upstream branch.\n"
-		    "To push the current branch and set the remote as upstream, use\n"
-		    "\n"
-		    "    git push --set-upstream %s %s\n"),
-		    branch->name,
-		    remote_name,
-		    branch->name);
+		return NULL;
 	if (branch->merge_nr != 1)
 		die(_("The current branch %s has multiple upstream branches, "
 		    "refusing to push."), branch->name);
@@ -234,7 +228,16 @@ static void setup_default_push_refspecs(struct remote *remote)
 	case PUSH_DEFAULT_SIMPLE:
 		if (!same_remote)
 			break;
-		if (strcmp(branch->refname, get_upstream_ref(branch, remote->name)))
+		dst = get_upstream_ref(branch);
+		if (!dst)
+			die(_("The current branch %s has no upstream branch.\n"
+			    "To push the current branch and set the remote as upstream, use\n"
+			    "\n"
+			    "    git push --set-upstream %s %s\n"),
+			    branch->name,
+			    remote->name,
+			    branch->name);
+		if (strcmp(branch->refname, dst))
 			die_push_simple(branch, remote);
 		break;
 
@@ -244,7 +247,15 @@ static void setup_default_push_refspecs(struct remote *remote)
 			      "your current branch '%s', without telling me what to push\n"
 			      "to update which remote branch."),
 			    remote->name, branch->name);
-		dst = get_upstream_ref(branch, remote->name);
+		dst = get_upstream_ref(branch);
+		if (!dst)
+			die(_("The current branch %s has no upstream branch.\n"
+			    "To push the current branch and set the remote as upstream, use\n"
+			    "\n"
+			    "    git push --set-upstream %s %s\n"),
+			    branch->name,
+			    remote->name,
+			    branch->name);
 		break;
 
 	case PUSH_DEFAULT_CURRENT:
-- 
2.32.0.40.gb9b36f9b52
Previous: Felipe ContrerasNext: Felipe Contreras
Message 3 of 4 in “push: make default consistent”
  1. push: make default consistentFelipe Contreras, Jul 14, 2021
  2. 0/2 push: fix default actionFelipe Contreras, Jul 19, 2021
  3. 1/2 push: reorganize get_upstream_refFelipe Contreras, Jul 19, 2021
  4. 2/2 push: make default consistentFelipe Contreras, Jul 19, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.