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 5/7] parse_refname_prefix(): new function
Date: Tue,  1 May 2012 01:02:53 +0200	[thread overview]
Message-ID: <1335826975-3093-6-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>

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

  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 ` [RFC 4/7] parse_refname_component(): parse until terminator mhagger
2012-04-30 23:02 ` mhagger [this message]
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-6-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).