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

[RFC 5/7] parse_refname_prefix(): new function

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 30, 2012, 23:02 UTC
Message-ID
<1335826975-3093-6-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1335826975-3093-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>

Add a new function, parse_refname_prefix(), which looks for a possible refname at the front of a string and returns its length. Use this function to implement check_refname_format().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)
diff --git a/refs.c b/refs.c
index 6e4cba0..70578ee 100644
--- a/refs.c
+++ b/refs.c
@@ -75,21 +75,28 @@ static int parse_refname_component(const char *refname, int refnamelen, int flag
 	return i;
 }
 
-int check_refname_format(const char *refname, int flags)
+/*
+ * Try to interpret the beginning of string refname as a reference
+ * name.  Return the length of the part of the string that could
+ * constitute a valid refname, or -1 if the start of the string cannot
+ * possibly be interpreted as a refname.  flags has the same
+ * interpretation as for check_refname_format().
+ */
+static int parse_refname_prefix(const char *refname, int refnamelen, int flags)
 {
-	int refnamelen = strlen(refname);
+	const char *p = refname;
+	int len = refnamelen;
 	int component_len, component_count = 0;
 
 	while (1) {
 		/* We are at the start of a path component. */
-		component_len = parse_refname_component(refname, refnamelen, flags);
+		component_len = parse_refname_component(p, len, flags);
 		if (component_len < 0) {
-			/* This case includes a refname that ends with '/': */
 			return -1;
 		} else if (component_len == 0) {
 			if ((flags & REFNAME_REFSPEC_PATTERN) &&
-					refnamelen >= 1 && refname[0] == '*' &&
-					(refnamelen == 1 || refname[1] == '/')) {
+					len >= 1 && p[0] == '*' &&
+					(len == 1 || p[1] == '/')) {
 				/* Accept one wildcard as a full refname component. */
 				flags &= ~REFNAME_REFSPEC_PATTERN;
 				component_len = 1;
@@ -99,28 +106,34 @@ int check_refname_format(const char *refname, int flags)
 		}
 		component_count++;
 		/* See what terminated the component: */
-		if (component_len == refnamelen) {
+		if (component_len == len) {
 			/* We've parsed the whole string: */
 			break;
-		} else if (refname[component_len] == '/') {
+		} else if (p[component_len] == '/') {
 			/* Skip to the start of the next component: */
-			refname += component_len + 1;
-			refnamelen -= component_len + 1;
+			p += component_len + 1;
+			len -= component_len + 1;
 		} else {
 			/*
 			 * The component was ended by something else
 			 * (something that cannot be part of a legal
 			 * refname).
 			 */
-			return -1;
+			break;
 		}
 	}
 
-	if (refname[component_len - 1] == '.')
+	if (p[component_len - 1] == '.')
 		return -1; /* Refname ends with '.'. */
 	if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
 		return -1; /* Refname has only one component. */
-	return 0;
+	return p + component_len - refname;
+}
+
+int check_refname_format(const char *refname, int flags)
+{
+	int refnamelen = strlen(refname);
+	return parse_refname_prefix(refname, refnamelen, flags) == refnamelen ? 0 : -1;
 }
 
 struct ref_entry;
-- 
1.7.10
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 6 of 8 in “[RFC 0/7] Make check_refname_format() more flexible”
  1. mhagger@alum.mit.eduApr 30, 2012
  2. 1/7 check_refname_component(): iterate via index rather than via pointermhagger@alum.mit.edu, Apr 30, 2012
  3. 2/7 check_refname_component(): fix check with REFNAME_DOT_COMPONENTmhagger@alum.mit.edu, Apr 30, 2012
  4. 3/7 parse_refname_component(): accept a length parametermhagger@alum.mit.edu, Apr 30, 2012
  5. 4/7 parse_refname_component(): parse until terminatormhagger@alum.mit.edu, Apr 30, 2012
  6. 5/7 parse_refname_prefix(): new functionmhagger@alum.mit.edu, Apr 30, 2012
  7. 6/7 check_refname_format(): add REFNAME_FULL optionmhagger@alum.mit.edu, Apr 30, 2012
  8. 7/7 check_refname_format(): implement REFNAME_RELAXED optionmhagger@alum.mit.edu, Apr 30, 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.