attr: change validity check for attribute names to use positive logic
[gitweb.git] / attr.c
diff --git a/attr.c b/attr.c
index 2f180d609f591674b5e82a08ac613fc65f4db475..9fe848f59e598f77b519554652ff8a5d0c5ef037 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -74,23 +74,33 @@ static unsigned hash_name(const char *name, int namelen)
        return val;
 }
 
-static int invalid_attr_name(const char *name, int namelen)
+static int attr_name_valid(const char *name, size_t namelen)
 {
        /*
         * Attribute name cannot begin with '-' and must consist of
         * characters from [-A-Za-z0-9_.].
         */
        if (namelen <= 0 || *name == '-')
-               return -1;
+               return 0;
        while (namelen--) {
                char ch = *name++;
                if (! (ch == '-' || ch == '.' || ch == '_' ||
                       ('0' <= ch && ch <= '9') ||
                       ('a' <= ch && ch <= 'z') ||
                       ('A' <= ch && ch <= 'Z')) )
-                       return -1;
+                       return 0;
        }
-       return 0;
+       return 1;
+}
+
+static void report_invalid_attr(const char *name, size_t len,
+                               const char *src, int lineno)
+{
+       struct strbuf err = STRBUF_INIT;
+       strbuf_addf(&err, _("%.*s is not a valid attribute name"),
+                   (int) len, name);
+       fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno);
+       strbuf_release(&err);
 }
 
 static struct git_attr *git_attr_internal(const char *name, int len)
@@ -105,7 +115,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
                        return a;
        }
 
-       if (invalid_attr_name(name, len))
+       if (!attr_name_valid(name, len))
                return NULL;
 
        FLEX_ALLOC_MEM(a, name, name, len);
@@ -196,17 +206,15 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
                        cp++;
                        len--;
                }
-               if (invalid_attr_name(cp, len)) {
-                       fprintf(stderr,
-                               "%.*s is not a valid attribute name: %s:%d\n",
-                               len, cp, src, lineno);
+               if (!attr_name_valid(cp, len)) {
+                       report_invalid_attr(cp, len, src, lineno);
                        return NULL;
                }
        } else {
                /*
                 * As this function is always called twice, once with
                 * e == NULL in the first pass and then e != NULL in
-                * the second pass, no need for invalid_attr_name()
+                * the second pass, no need for attr_name_valid()
                 * check here.
                 */
                if (*cp == '-' || *cp == '!') {
@@ -258,10 +266,8 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
                name += strlen(ATTRIBUTE_MACRO_PREFIX);
                name += strspn(name, blank);
                namelen = strcspn(name, blank);
-               if (invalid_attr_name(name, namelen)) {
-                       fprintf(stderr,
-                               "%.*s is not a valid attribute name: %s:%d\n",
-                               namelen, name, src, lineno);
+               if (!attr_name_valid(name, namelen)) {
+                       report_invalid_attr(name, namelen, src, lineno);
                        goto fail_return;
                }
        }
@@ -370,6 +376,75 @@ static void free_attr_elem(struct attr_stack *e)
        free(e);
 }
 
+struct attr_check *attr_check_alloc(void)
+{
+       return xcalloc(1, sizeof(struct attr_check));
+}
+
+struct attr_check *attr_check_initl(const char *one, ...)
+{
+       struct attr_check *check;
+       int cnt;
+       va_list params;
+       const char *param;
+
+       va_start(params, one);
+       for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)
+               ;
+       va_end(params);
+
+       check = attr_check_alloc();
+       check->nr = cnt;
+       check->alloc = cnt;
+       check->items = xcalloc(cnt, sizeof(struct attr_check_item));
+
+       check->items[0].attr = git_attr(one);
+       va_start(params, one);
+       for (cnt = 1; cnt < check->nr; cnt++) {
+               const struct git_attr *attr;
+               param = va_arg(params, const char *);
+               if (!param)
+                       die("BUG: counted %d != ended at %d",
+                           check->nr, cnt);
+               attr = git_attr(param);
+               if (!attr)
+                       die("BUG: %s: not a valid attribute name", param);
+               check->items[cnt].attr = attr;
+       }
+       va_end(params);
+       return check;
+}
+
+struct attr_check_item *attr_check_append(struct attr_check *check,
+                                         const struct git_attr *attr)
+{
+       struct attr_check_item *item;
+
+       ALLOC_GROW(check->items, check->nr + 1, check->alloc);
+       item = &check->items[check->nr++];
+       item->attr = attr;
+       return item;
+}
+
+void attr_check_reset(struct attr_check *check)
+{
+       check->nr = 0;
+}
+
+void attr_check_clear(struct attr_check *check)
+{
+       free(check->items);
+       check->items = NULL;
+       check->alloc = 0;
+       check->nr = 0;
+}
+
+void attr_check_free(struct attr_check *check)
+{
+       attr_check_clear(check);
+       free(check);
+}
+
 static const char *builtin_attr[] = {
        "[attr]binary -diff -merge -text",
        NULL,
@@ -777,9 +852,7 @@ static int macroexpand_one(int nr, int rem)
  * check_all_attr. If num is non-zero, only attributes in check[] are
  * collected. Otherwise all attributes are collected.
  */
-static void collect_some_attrs(const char *path, int num,
-                              struct attr_check_item *check)
-
+static void collect_some_attrs(const char *path, struct attr_check *check)
 {
        struct attr_stack *stk;
        int i, pathlen, rem, dirlen;
@@ -802,17 +875,18 @@ static void collect_some_attrs(const char *path, int num,
        prepare_attr_stack(path, dirlen);
        for (i = 0; i < attr_nr; i++)
                check_all_attr[i].value = ATTR__UNKNOWN;
-       if (num && !cannot_trust_maybe_real) {
+       if (check->nr && !cannot_trust_maybe_real) {
                rem = 0;
-               for (i = 0; i < num; i++) {
-                       if (!check[i].attr->maybe_real) {
+               for (i = 0; i < check->nr; i++) {
+                       const struct git_attr *a = check->items[i].attr;
+                       if (!a->maybe_real) {
                                struct attr_check_item *c;
-                               c = check_all_attr + check[i].attr->attr_nr;
+                               c = check_all_attr + a->attr_nr;
                                c->value = ATTR__UNSET;
                                rem++;
                        }
                }
-               if (rem == num)
+               if (rem == check->nr)
                        return;
        }
 
@@ -821,48 +895,38 @@ static void collect_some_attrs(const char *path, int num,
                rem = fill(path, pathlen, basename_offset, stk, rem);
 }
 
-int git_check_attrs(const char *path, int num, struct attr_check_item *check)
+int git_check_attr(const char *path, struct attr_check *check)
 {
        int i;
 
-       collect_some_attrs(path, num, check);
+       collect_some_attrs(path, check);
 
-       for (i = 0; i < num; i++) {
-               const char *value = check_all_attr[check[i].attr->attr_nr].value;
+       for (i = 0; i < check->nr; i++) {
+               const char *value = check_all_attr[check->items[i].attr->attr_nr].value;
                if (value == ATTR__UNKNOWN)
                        value = ATTR__UNSET;
-               check[i].value = value;
+               check->items[i].value = value;
        }
 
        return 0;
 }
 
-int git_all_attrs(const char *path, int *num, struct attr_check_item **check)
+void git_all_attrs(const char *path, struct attr_check *check)
 {
-       int i, count, j;
+       int i;
 
-       collect_some_attrs(path, 0, NULL);
+       attr_check_reset(check);
+       collect_some_attrs(path, check);
 
-       /* Count the number of attributes that are set. */
-       count = 0;
-       for (i = 0; i < attr_nr; i++) {
-               const char *value = check_all_attr[i].value;
-               if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
-                       ++count;
-       }
-       *num = count;
-       ALLOC_ARRAY(*check, count);
-       j = 0;
        for (i = 0; i < attr_nr; i++) {
+               const char *name = check_all_attr[i].attr->name;
                const char *value = check_all_attr[i].value;
-               if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
-                       (*check)[j].attr = check_all_attr[i].attr;
-                       (*check)[j].value = value;
-                       ++j;
-               }
+               struct attr_check_item *item;
+               if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
+                       continue;
+               item = attr_check_append(check, git_attr(name));
+               item->value = value;
        }
-
-       return 0;
 }
 
 void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)