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

[PATCH 7/7] sha1_name: simplify track finding

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Dec 8, 2013, 05:57 UTC
Message-ID
<1386482220-18558-8-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1386482220-18558-1-git-send-email-felipe.contreras@gmail.com>

It's more efficient to check for the braces first, and we already have the length of the whole thing.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 sha1_name.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index e27db88..1a68103 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -417,7 +417,7 @@ static int ambiguous_path(const char *path, int len)
 
 static inline int upstream_mark(const char *string, int len)
 {
-	const char *suffix[] = { "@{upstream}", "@{u}" };
+	const char *suffix[] = { "upstream", "u" };
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(suffix); i++) {
@@ -475,7 +475,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 					nth_prior = 1;
 					continue;
 				}
-				if (!upstream_mark(str + at, len - at)) {
+				if (!upstream_mark(str + at + 2, len - at - 3)) {
 					reflog_len = (len-1) - (at+2);
 					len = at;
 				}
@@ -1074,7 +1074,6 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
 	char *cp;
 	struct branch *branch;
 	int len = interpret_nth_prior_checkout(name, buf);
-	int tmp_len;
 
 	if (!namelen)
 		namelen = strlen(name);
@@ -1096,11 +1095,13 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
 	if (len > 0)
 		return reinterpret(name, namelen, len, buf);
 
-	tmp_len = upstream_mark(cp, namelen - (cp - name));
-	if (!tmp_len)
+	len = namelen - (cp - name);
+	if (cp[1] != '{' && cp[len - 1] != '}')
+		return -1;
+
+	if (!upstream_mark(cp + 2, len - 3))
 		return -1;
 
-	len = cp + tmp_len - name;
 	cp = xstrndup(name, cp - name);
 	branch = branch_get(*cp ? cp : NULL);
 	/*
@@ -1125,7 +1126,7 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
 	strbuf_reset(buf);
 	strbuf_addstr(buf, cp);
 	free(cp);
-	return len;
+	return namelen;
 }
 
 int strbuf_branchname(struct strbuf *sb, const char *name)
-- 
1.8.5.1+fc1.2.gebd1fb1
Previous: Philip Oakley
Message 11 of 11 in “Trivial patches”
  1. 0/7 Trivial patchesFelipe Contreras, Dec 8, 2013
  2. 1/7 config: avoid yoda conditionsFelipe Contreras, Dec 8, 2013
  3. 2/7 add: avoid yoda conditionsFelipe Contreras, Dec 8, 2013
  4. 3/7 abspath: trivial style fixFelipe Contreras, Dec 8, 2013
  5. Junio C HamanoDec 9, 2013
  6. 4/7 t: trivial whitespace cleanupsFelipe Contreras, Dec 8, 2013
  7. 5/7 fetch: add missing documentationFelipe Contreras, Dec 8, 2013
  8. Junio C HamanoDec 9, 2013
  9. 6/7 sha1_name: cleanup interpret_branch_name()Felipe Contreras, Dec 8, 2013
  10. Philip OakleyDec 8, 2013
  11. 7/7 sha1_name: simplify track findingFelipe Contreras, Dec 8, 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.