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

[PATCH 2/7] sha1_name: factor out die_no_upstream()

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
May 23, 2013, 15:12 UTC
Message-ID
<1369321970-7759-3-git-send-email-artagnon@gmail.com>
In-Reply-To
<1369321970-7759-1-git-send-email-artagnon@gmail.com>

Currently interpret_branch_name() tries to parse various things, and finally falls back to parsing <branch>@{u[pstream]}. It dies if the input string contained an "@{u[pstream]}" but an upstream could not be found. The logic can be generalized to check for any branch property after branch_get(). In preparation for introducing more special @{...} forms, factor out die_no_upstream().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 sha1_name.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 766e4e9..7aabd94 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -998,6 +998,24 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
 	return st;
 }
 
+static void die_no_upstream(struct branch *upstream, char *name) {
+	/*
+	 * Upstream can be NULL only if cp refers to HEAD and HEAD
+	 * points to something different than a branch.
+	 */
+	if (!upstream)
+		die(_("HEAD does not point to a branch"));
+	if (!upstream->merge || !upstream->merge[0]->dst) {
+		if (!ref_exists(upstream->refname))
+			die(_("No such branch: '%s'"), name);
+		if (!upstream->merge)
+			die(_("No upstream configured for branch '%s'"),
+				upstream->name);
+		die(_("Upstream branch '%s' not stored as a remote-tracking branch"),
+			upstream->merge[0]->src);
+	}
+}
+
 /*
  * This reads short-hand syntax that not only evaluates to a commit
  * object name, but also can act as if the end user spelled the name
@@ -1022,7 +1040,7 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
 int interpret_branch_name(const char *name, struct strbuf *buf)
 {
 	char *cp;
-	struct branch *upstream;
+	struct branch *branch;
 	int namelen = strlen(name);
 	int len = interpret_nth_prior_checkout(name, buf);
 	int tmp_len;
@@ -1059,24 +1077,10 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
 		return -1;
 	len = cp + tmp_len - name;
 	cp = xstrndup(name, cp - name);
-	upstream = branch_get(*cp ? cp : NULL);
-	/*
-	 * Upstream can be NULL only if cp refers to HEAD and HEAD
-	 * points to something different than a branch.
-	 */
-	if (!upstream)
-		die(_("HEAD does not point to a branch"));
-	if (!upstream->merge || !upstream->merge[0]->dst) {
-		if (!ref_exists(upstream->refname))
-			die(_("No such branch: '%s'"), cp);
-		if (!upstream->merge)
-			die(_("No upstream configured for branch '%s'"),
-				upstream->name);
-		die(_("Upstream branch '%s' not stored as a remote-tracking branch"),
-			upstream->merge[0]->src);
-	}
+	branch = branch_get(*cp ? cp : NULL);
+	die_no_upstream(branch, cp);
 	free(cp);
-	cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
+	cp = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
 	strbuf_reset(buf);
 	strbuf_addstr(buf, cp);
 	free(cp);
-- 
1.8.3.rc3.17.gd95ec6c.dirty
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 3 of 17 in “Let's get that @{push}!”
  1. 0/7 Let's get that @{push}!Ramkumar Ramachandra, May 23, 2013
  2. 1/7 sha1_name: abstract upstream_mark() logicRamkumar Ramachandra, May 23, 2013
  3. 2/7 sha1_name: factor out die_no_upstream()Ramkumar Ramachandra, May 23, 2013
  4. 3/7 sha1_name: remove upstream_mark()Ramkumar Ramachandra, May 23, 2013
  5. 4/7 remote: expose parse_push_refspec()Ramkumar Ramachandra, May 23, 2013
  6. 5/7 remote: expose get_ref_match()Ramkumar Ramachandra, May 23, 2013
  7. 6/7 sha1_name: prepare to introduce AT_KIND_PUSHRamkumar Ramachandra, May 23, 2013
  8. Felipe ContrerasMay 24, 2013
  9. Ramkumar RamachandraMay 24, 2013
  10. 7/7 sha1_name: implement finding @{push}Ramkumar Ramachandra, May 23, 2013
  11. Duy NguyenMay 24, 2013
  12. Ramkumar RamachandraMay 24, 2013
  13. Duy NguyenMay 24, 2013
  14. Ramkumar RamachandraMay 24, 2013
  15. Junio C HamanoMay 24, 2013
  16. Ramkumar RamachandraMay 24, 2013
  17. Junio C HamanoMay 24, 2013

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.