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

[PATCH v2 01/13] push: create new get_upstream_ref() helper

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
May 31, 2021, 19:51 UTC
Message-ID
<20210531195124.218325-2-felipe.contreras@gmail.com>
In-Reply-To
<20210531195124.218325-1-felipe.contreras@gmail.com>
This code is duplicated among multiple functions.
No functional changes.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin/push.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 29fea70ff1..e3e792c69c 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -185,29 +185,37 @@ static const char message_detached_head_die[] =
 	   "\n"
 	   "    git push %s HEAD:<name-of-remote-branch>\n");
 
-static void setup_push_upstream(struct remote *remote, struct branch *branch,
-				int same_remote)
+static const char *get_upstream_ref(struct branch *branch, const char *remote_name)
 {
-	if (!branch)
-		die(_(message_detached_head_die), remote->name);
 	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,
+		    remote_name,
 		    branch->name);
 	if (branch->merge_nr != 1)
 		die(_("The current branch %s has multiple upstream branches, "
 		    "refusing to push."), branch->name);
+
+	return branch->merge[0]->src;
+}
+
+static void setup_push_upstream(struct remote *remote, struct branch *branch,
+				int same_remote)
+{
+	const char *upstream_ref;
+	if (!branch)
+		die(_(message_detached_head_die), remote->name);
+	upstream_ref = get_upstream_ref(branch, remote->name);
 	if (!same_remote)
 		die(_("You are pushing to remote '%s', which is not the upstream of\n"
 		      "your current branch '%s', without telling me what to push\n"
 		      "to update which remote branch."),
 		    remote->name, branch->name);
 
-	refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
+	refspec_appendf(&rs, "%s:%s", branch->refname, upstream_ref);
 }
 
 static void setup_push_current(struct remote *remote, struct branch *branch)
@@ -223,20 +231,12 @@ static void setup_push_simple(struct remote *remote, struct branch *branch, int
 		die(_(message_detached_head_die), remote->name);
 
 	if (same_remote) {
-		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);
-		if (branch->merge_nr != 1)
-			die(_("The current branch %s has multiple upstream branches, "
-			    "refusing to push."), branch->name);
+		const char *upstream_ref;
+
+		upstream_ref = get_upstream_ref(branch, remote->name);
 
 		/* Additional safety */
-		if (strcmp(branch->refname, branch->merge[0]->src))
+		if (strcmp(branch->refname, upstream_ref))
 			die_push_simple(branch, remote);
 	}
 	refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
-- 
2.32.0.rc0
Previous: Felipe ContrerasNext: Felipe Contreras
Message 10 of 24 in “Unconvolutize push.default=simple”
  1. 0/7 Unconvolutize push.default=simpleFelipe Contreras, May 31, 2021
  2. 1/7 push: rename !triangular to same_remoteFelipe Contreras, May 31, 2021
  3. 2/7 push: hedge code of default=simpleFelipe Contreras, May 31, 2021
  4. 3/7 push: copy code to setup_push_simple()Felipe Contreras, May 31, 2021
  5. 4/7 push: reorganize setup_push_simple()Felipe Contreras, May 31, 2021
  6. 5/7 push: simplify setup_push_simple()Felipe Contreras, May 31, 2021
  7. 7/7 doc: push: explain default=simple correctlyFelipe Contreras, May 31, 2021
  8. 6/7 push: remove unused code in setup_push_upstream()Felipe Contreras, May 31, 2021
  9. 00/13 push: revamp push.defaultFelipe Contreras, May 31, 2021
  10. 01/13 push: create new get_upstream_ref() helperFelipe Contreras, May 31, 2021
  11. 02/13 push: return immediately in trivial switch caseFelipe Contreras, May 31, 2021
  12. 03/13 push: split switch casesFelipe Contreras, May 31, 2021
  13. 04/13 push: factor out null branch checkFelipe Contreras, May 31, 2021
  14. 05/13 push: only get the branch when neededFelipe Contreras, May 31, 2021
  15. 08/13 push: get rid of all the setup_push_* functionsFelipe Contreras, May 31, 2021
  16. 07/13 push: trivial simplificationsFelipe Contreras, May 31, 2021
  17. 09/13 push: factor out the typical caseFelipe Contreras, May 31, 2021
  18. 12/13 push: only check same_remote when neededFelipe Contreras, May 31, 2021
  19. 11/13 push: remove trivial functionFelipe Contreras, May 31, 2021
  20. 06/13 push: make setup_push_* return the dstFelipe Contreras, May 31, 2021
  21. 13/13 push: don't get a full remote objectFelipe Contreras, May 31, 2021
  22. 10/13 push: remove redundant checkFelipe Contreras, May 31, 2021
  23. Junio C HamanoJun 2, 2021
  24. Felipe ContrerasJun 2, 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.