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

[PATCH 2/1] peel_onion: do not assume length of x_type globals

From
Jeff King <peff@peff.net>
Date
Sep 3, 2013, 20:20 UTC
Message-ID
<20130903202041.GA7463@sigill.intra.peff.net>
In-Reply-To
<1378237816-28671-1-git-send-email-rhansen@bbn.com>

When we are parsing "rev^{foo}", we check "foo" against the various global type strings, like "commit_type", "tree_type", etc. This is nicely abstracted, but then we destroy the abstraction completely by using magic numbers that must match the length of the type strings.

We can avoid these magic numbers by using skip_prefix. In an ideal world, the compiler would compute the magic number without even calling strlen() at runtime. However, it cannot do so in this case because we hide the definition of commit_type and friends behind an extern declaration.

This shouldn't matter, though, as this is not a performance critical code path. A few short strlens inside get_sha1() will not even be measurable. And if we care, we can adjust the declaration of commit_type and friends later on.

Signed-off-by: Jeff King <peff@peff.net>
---
This is the potential cleanup we discussed. Having written that commit
message, I wonder if it is really even worth using commit_type at all.
It is not like that variable can ever change without the git data
structure changing, and even if it did, we would almost certainly want
to revisit how that change affects the user-visible "rev^{}" syntax
anyway.
So maybe simply:
  if (!prefixcmp("commit}"))

would be fine, and it is way more readable. That's what we do already for "rev^{object}".

 sha1_name.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 6dc496d..23d0d71 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -652,7 +652,7 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 static int peel_onion(const char *name, int len, unsigned char *sha1)
 {
 	unsigned char outer[20];
-	const char *sp;
+	const char *sp, *x;
 	unsigned int expected_type = 0;
 	unsigned lookup_flags = 0;
 	struct object *o;
@@ -677,13 +677,13 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 		return -1;
 
 	sp++; /* beginning of type name, or closing brace for empty */
-	if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
+	if ((x = skip_prefix(sp, commit_type)) && *x == '}')
 		expected_type = OBJ_COMMIT;
-	else if (!strncmp(tag_type, sp, 3) && sp[3] == '}')
+	else if ((x = skip_prefix(sp, tag_type)) && *x == '}')
 		expected_type = OBJ_TAG;
-	else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
+	else if ((x = skip_prefix(sp, tree_type)) && *x == '}')
 		expected_type = OBJ_TREE;
-	else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
+	else if ((x = skip_prefix(sp, blob_type)) && *x == '}')
 		expected_type = OBJ_BLOB;
 	else if (!prefixcmp(sp, "object}"))
 		expected_type = OBJ_ANY;
-- 
1.8.4.2.g87d4a77
Previous: Junio C HamanoNext: Jeff King
Message 3 of 5 in “peel_onion(): add support for <rev>^{tag}”
  1. peel_onion(): add support for <rev>^{tag}Richard Hansen, Sep 3, 2013
  2. Junio C HamanoSep 3, 2013
  3. 2/1 peel_onion: do not assume length of x_type globalsJeff King, Sep 3, 2013
  4. 2/1 peel_onion: do not assume length of x_type globalsJeff King, Sep 3, 2013
  5. Junio C HamanoSep 3, 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.