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

[PATCH] create_ref_entry(): move check_refname_format() call to callers

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 29, 2012, 06:18 UTC
Message-ID
<1335680288-5128-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>

This allows the caller to decide what flags to use for check_refname_format().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
On 04/27/2012 05:06 PM, Junio C Hamano wrote:
Show 20 quoted lines
> Jeff King<peff@peff.net>  writes:
> 
>> I upgraded git on a machine recently, and it created problems for a repo
>> with a bogus character in a ref name.  Older versions of git never
>> complained about it. Newer ones, containing your dce4bab ("add_ref():
>> verify that the refname is formatted correctly") do. That's fine; it's
>> bogus and git _should_ complain about it.
>>
>> However, recovering from the situation is unnecessarily hard, ...
>> ...
>> I seem to recall discussing this format-tightening and trying to be sure
>> that users were left with a way forward for fixing their repos. But I
>> can't find the discussion, and I don't recall any conclusion we came to.
> 
> I haven't dug the archive but I do recall pointing many issues out
> around the theme "be liberal in what you accept and strict in what you
> produce" on this topic, and loosening one or two showstoppers during the
> review cycle, but obviously we did not catch all of them.
> 
> Michael?

I will work on providing more infrastructure for checking refnames at varying levels of strictness, but I don't know enough about the code paths to be able to find the places where the strictness levels need tweaking.

For this to work, the various callers of check_refname_format() will have to be able to influence the level of strictness that they want to enforce. This patch is one trivial step in that direction.

 refs.c |   23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/refs.c b/refs.c
index 09322fe..bc735e7 100644
--- a/refs.c
+++ b/refs.c
@@ -172,15 +172,11 @@ struct ref_entry {
 };
 
 static struct ref_entry *create_ref_entry(const char *refname,
-					  const unsigned char *sha1, int flag,
-					  int check_name)
+					  const unsigned char *sha1, int flag)
 {
 	int len;
 	struct ref_entry *ref;
 
-	if (check_name &&
-	    check_refname_format(refname, REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT))
-		die("Reference has invalid format: '%s'", refname);
 	len = strlen(refname) + 1;
 	ref = xmalloc(sizeof(struct ref_entry) + len);
 	hashcpy(ref->u.value.sha1, sha1);
@@ -710,7 +706,11 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 
 		refname = parse_ref_line(refline, sha1);
 		if (refname) {
-			last = create_ref_entry(refname, sha1, flag, 1);
+			if (check_refname_format(refname,
+						 REFNAME_ALLOW_ONELEVEL|
+						 REFNAME_DOT_COMPONENT))
+				die("Packed reference has invalid format: '%s'", refname);
+			last = create_ref_entry(refname, sha1, flag);
 			add_ref(dir, last);
 			continue;
 		}
@@ -745,8 +745,11 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
 
 void add_packed_ref(const char *refname, const unsigned char *sha1)
 {
+	if (check_refname_format(refname,
+				 REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT))
+		die("Reference has invalid format: '%s'", refname);
 	add_ref(get_packed_refs(get_ref_cache(NULL)),
-			create_ref_entry(refname, sha1, REF_ISPACKED, 1));
+			create_ref_entry(refname, sha1, REF_ISPACKED));
 }
 
 static void get_ref_dir(struct ref_cache *refs, const char *base,
@@ -805,7 +808,11 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
 				hashclr(sha1);
 				flag |= REF_ISBROKEN;
 			}
-			add_ref(dir, create_ref_entry(refname, sha1, flag, 1));
+			if (check_refname_format(refname,
+						 REFNAME_ALLOW_ONELEVEL|
+						 REFNAME_DOT_COMPONENT))
+				die("Loose reference has invalid format: '%s'", refname);
+			add_ref(dir, create_ref_entry(refname, sha1, flag));
 		}
 		free(refname);
 		closedir(d);
-- 
1.7.10
Next: Jeff King
Message 1 of 8 in “create_ref_entry(): move check_refname_format() call to callers”
  1. create_ref_entry(): move check_refname_format() call to callersmhagger@alum.mit.edu, Apr 29, 2012
  2. Jeff KingApr 29, 2012
  3. Junio C HamanoApr 30, 2012
  4. Michael HaggertyApr 30, 2012
  5. Junio C HamanoApr 30, 2012
  6. Michael HaggertyApr 30, 2012
  7. Junio C HamanoApr 30, 2012
  8. Michael HaggertyMay 2, 2012

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.