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

[PATCH 1/5] for-each-ref: refactor get_short_ref function

From
Jeff King <peff@peff.net>
Date
Apr 7, 2009, 07:05 UTC
Message-ID
<20090407070501.GA2924@coredump.intra.peff.net>
In-Reply-To
<20090407070254.GA2870@coredump.intra.peff.net>

This function took a "refinfo" object which is unnecessarily restrictive; it only ever looked at the refname field. This patch refactors it to take just the ref name as a string.

While we're touching the relevant lines, let's give it consistent memory semantics. Previously, some code paths would return an allocated string and some would return the original string; now it will always return a malloc'd string.

This doesn't actually fix a bug or a leak, because for-each-ref doesn't clean up its memory, but it makes the function a lot less surprising for reuse (which will happen in a later patch).

Signed-off-by: Jeff King <peff@peff.net>
---
Actually, as the modified version is always prefix-shortened,
it should be possible to rewrite this in a way that never allocates
memory. I picked the least-invasive and most lazy approach.
 builtin-for-each-ref.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 5cbb4b0..4aaf75c 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -569,7 +569,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
 /*
  * Shorten the refname to an non-ambiguous form
  */
-static char *get_short_ref(struct refinfo *ref)
+static char *get_short_ref(const char *ref)
 {
 	int i;
 	static char **scanf_fmts;
@@ -598,17 +598,17 @@ static char *get_short_ref(struct refinfo *ref)
 
 	/* bail out if there are no rules */
 	if (!nr_rules)
-		return ref->refname;
+		return xstrdup(ref);
 
-	/* buffer for scanf result, at most ref->refname must fit */
-	short_name = xstrdup(ref->refname);
+	/* buffer for scanf result, at most ref must fit */
+	short_name = xstrdup(ref);
 
 	/* skip first rule, it will always match */
 	for (i = nr_rules - 1; i > 0 ; --i) {
 		int j;
 		int short_name_len;
 
-		if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
+		if (1 != sscanf(ref, scanf_fmts[i], short_name))
 			continue;
 
 		short_name_len = strlen(short_name);
@@ -642,7 +642,7 @@ static char *get_short_ref(struct refinfo *ref)
 	}
 
 	free(short_name);
-	return ref->refname;
+	return xstrdup(ref);
 }
 
 
@@ -684,7 +684,7 @@ static void populate_value(struct refinfo *ref)
 			if (formatp) {
 				formatp++;
 				if (!strcmp(formatp, "short"))
-					refname = get_short_ref(ref);
+					refname = get_short_ref(ref->refname);
 				else
 					die("unknown refname format %s",
 					    formatp);
-- 
1.6.2.2.450.gd6aa9.dirty
Previous: Jeff KingNext: Jeff King
Message 2 of 24 in “making upstream branch information accessible”
  1. 0/5 making upstream branch information accessibleJeff King, Apr 7, 2009
  2. 1/5 for-each-ref: refactor get_short_ref functionJeff King, Apr 7, 2009
  3. 2/5 for-each-ref: refactor refname handlingJeff King, Apr 7, 2009
  4. Junio C HamanoApr 8, 2009
  5. Jeff KingApr 8, 2009
  6. 3/5 for-each-ref: add "upstream" format fieldJeff King, Apr 7, 2009
  7. 4/5 make get_short_ref a public functionJeff King, Apr 7, 2009
  8. Bert WesargApr 7, 2009
  9. Jeff KingApr 9, 2009
  10. Bert WesargApr 9, 2009
  11. Jeff KingApr 13, 2009
  12. Michael J GruberApr 7, 2009
  13. 5/5 branch: show upstream branch when double verboseJeff King, Apr 7, 2009
  14. Michael J GruberApr 7, 2009
  15. Jeff KingApr 9, 2009
  16. Santi BéjarApr 9, 2009
  17. Jeff KingApr 13, 2009
  18. Wincent ColaiutaApr 13, 2009
  19. Paolo CiarrocchiApr 7, 2009
  20. for-each-ref: remove multiple xstrdup() in get_short_ref()Bert Wesarg, Apr 7, 2009
  21. Jeff KingApr 7, 2009
  22. Bert WesargApr 7, 2009
  23. Jeff KingApr 7, 2009
  24. Bert WesargApr 7, 2009

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.