git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: mhagger@alum.mit.edu
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
	Michael Haggerty <mhagger@alum.mit.edu>
Subject: [RFC 4/7] parse_refname_component(): parse until terminator
Date: Tue,  1 May 2012 01:02:52 +0200	[thread overview]
Message-ID: <1335826975-3093-5-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1335826975-3093-1-git-send-email-mhagger@alum.mit.edu>

From: Michael Haggerty <mhagger@alum.mit.edu>

Rename check_refname_component() to parse_refname_component() and
change it to parse whatever part of the beginning of the string that
can be considered a valid refname component.  The old version reported
an error if a refname component was terminated by anything other then
end-of-string or '/'; this version just reports the length of the part
of the string that can be considered a valid refname component
(whether it is terminated by end-of-string, '/', "..", "@{", or some
character that is illegal in a refname component).

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   64 +++++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 43 insertions(+), 21 deletions(-)

diff --git a/refs.c b/refs.c
index 39008f2..6e4cba0 100644
--- a/refs.c
+++ b/refs.c
@@ -29,25 +29,32 @@ static inline int bad_ref_char(int ch)
 }
 
 /*
- * Try to read one refname component from the first refnamelen
- * characters of refname.  Return the length of the component found,
- * or -1 if the component is not legal.
+ * Try to read one refname component from the front of refname.
+ * Return the length of the part of the string that could constitute a
+ * valid refname component (which might be zero), or -1 if the start
+ * of the string looks like a refname component but is formatted
+ * illegally.  A refname component can be terminated by the end of the
+ * string, '/', any character forbidden by bad_ref_char(), "..", or
+ * "@{".
  */
-static int check_refname_component(const char *refname, int refnamelen, int flags)
+static int parse_refname_component(const char *refname, int refnamelen, int flags)
 {
 	int i;
 	char last = '\0';
 
 	for (i = 0; i < refnamelen; i++) {
 		char ch = refname[i];
-		if (ch == '/')
+		if (ch == '/') {
 			break;
-		if (bad_ref_char(ch))
-			return -1; /* Illegal character in refname. */
-		if (last == '.' && ch == '.')
-			return -1; /* Refname contains "..". */
-		if (last == '@' && ch == '{')
-			return -1; /* Refname contains "@{". */
+		} else if (bad_ref_char(ch)) {
+			break; /* Component terminated by illegal character. */
+		} else if (last == '.' && ch == '.') {
+			i--;
+			break; /* Component terminated by "..". */
+		} else if (last == '@' && ch == '{') {
+			i--;
+			break; /* Refname terminated by "@{". */
+		}
 		last = ch;
 	}
 	if (i == 0)
@@ -57,13 +64,14 @@ static int check_refname_component(const char *refname, int refnamelen, int flag
 			return -1; /* Component starts with '.'. */
 		/*
 		 * Even if leading dots are allowed, don't allow "."
-		 * as a component (".." is prevented by a rule above).
+		 * as a component (".." is treated as a terminator by
+		 * a rule above).
 		 */
 		if (i == 1)
 			return -1; /* Component equals ".". */
 	}
 	if (i >= 5 && !memcmp(&refname[i - 5], ".lock", 5))
-		return -1; /* Refname ends with ".lock". */
+		return -1; /* Component is not allowed to end with ".lock". */
 	return i;
 }
 
@@ -74,11 +82,14 @@ int check_refname_format(const char *refname, int flags)
 
 	while (1) {
 		/* We are at the start of a path component. */
-		component_len = check_refname_component(refname, refnamelen, flags);
-		if (component_len <= 0) {
+		component_len = parse_refname_component(refname, refnamelen, 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) &&
-					refname[0] == '*' &&
-					(refname[1] == '\0' || refname[1] == '/')) {
+					refnamelen >= 1 && refname[0] == '*' &&
+					(refnamelen == 1 || refname[1] == '/')) {
 				/* Accept one wildcard as a full refname component. */
 				flags &= ~REFNAME_REFSPEC_PATTERN;
 				component_len = 1;
@@ -87,11 +98,22 @@ int check_refname_format(const char *refname, int flags)
 			}
 		}
 		component_count++;
-		if (refname[component_len] == '\0')
+		/* See what terminated the component: */
+		if (component_len == refnamelen) {
+			/* We've parsed the whole string: */
 			break;
-		/* Skip to next component. */
-		refname += component_len + 1;
-		refnamelen -= component_len + 1;
+		} else if (refname[component_len] == '/') {
+			/* Skip to the start of the next component: */
+			refname += component_len + 1;
+			refnamelen -= component_len + 1;
+		} else {
+			/*
+			 * The component was ended by something else
+			 * (something that cannot be part of a legal
+			 * refname).
+			 */
+			return -1;
+		}
 	}
 
 	if (refname[component_len - 1] == '.')
-- 
1.7.10

  parent reply	other threads:[~2012-04-30 23:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-30 23:02 [RFC 0/7] Make check_refname_format() more flexible mhagger
2012-04-30 23:02 ` [RFC 1/7] check_refname_component(): iterate via index rather than via pointer mhagger
2012-04-30 23:02 ` [RFC 2/7] check_refname_component(): fix check with REFNAME_DOT_COMPONENT mhagger
2012-04-30 23:02 ` [RFC 3/7] parse_refname_component(): accept a length parameter mhagger
2012-04-30 23:02 ` mhagger [this message]
2012-04-30 23:02 ` [RFC 5/7] parse_refname_prefix(): new function mhagger
2012-04-30 23:02 ` [RFC 6/7] check_refname_format(): add REFNAME_FULL option mhagger
2012-04-30 23:02 ` [RFC 7/7] check_refname_format(): implement REFNAME_RELAXED option mhagger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1335826975-3093-5-git-send-email-mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).