From: Michael Haggerty <mhagger@xxxxxxxxxxxx> 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@xxxxxxxxxxxx> --- 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 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html