Smart-http: check if repository is OK to export before serving it
[gitweb.git] / attr.c
diff --git a/attr.c b/attr.c
index 410bca613cd83cb233889e50d0424f9e589c1ea0..55bdb7cdebea7f7ea551231fe7801f272d128d69 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -1,6 +1,15 @@
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "attr.h"
 
+const char git_attr__true[] = "(builtin)true";
+const char git_attr__false[] = "\0(builtin)false";
+static const char git_attr__unknown[] = "(builtin)unknown";
+#define ATTR__TRUE git_attr__true
+#define ATTR__FALSE git_attr__false
+#define ATTR__UNSET NULL
+#define ATTR__UNKNOWN git_attr__unknown
+
 /*
  * The basic design decision here is that we are not going to have
  * insanely large number of attributes.
@@ -26,8 +35,7 @@ static struct git_attr *(git_attr_hash[HASHSIZE]);
 
 static unsigned hash_name(const char *name, int namelen)
 {
-       unsigned val = 0;
-       unsigned char c;
+       unsigned val = 0, c;
 
        while (namelen--) {
                c = *name++;
@@ -36,6 +44,27 @@ static unsigned hash_name(const char *name, int namelen)
        return val;
 }
 
+static int invalid_attr_name(const char *name, int namelen)
+{
+       /*
+        * Attribute name cannot begin with '-' and from
+        * [-A-Za-z0-9_.].  We'd specifically exclude '=' for now,
+        * as we might later want to allow non-binary value for
+        * attributes, e.g. "*.svg      merge=special-merge-program-for-svg"
+        */
+       if (*name == '-')
+               return -1;
+       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;
+}
+
 struct git_attr *git_attr(const char *name, int len)
 {
        unsigned hval = hash_name(name, len);
@@ -48,6 +77,9 @@ struct git_attr *git_attr(const char *name, int len)
                        return a;
        }
 
+       if (invalid_attr_name(name, len))
+               return NULL;
+
        a = xmalloc(sizeof(*a) + len + 1);
        memcpy(a->name, name, len);
        a->name[len] = 0;
@@ -59,6 +91,7 @@ struct git_attr *git_attr(const char *name, int len)
        check_all_attr = xrealloc(check_all_attr,
                                  sizeof(*check_all_attr) * attr_nr);
        check_all_attr[a->attr_nr].attr = a;
+       check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
        return a;
 }
 
@@ -68,12 +101,14 @@ struct git_attr *git_attr(const char *name, int len)
  * (1) glob pattern.
  * (2) whitespace
  * (3) whitespace separated list of attribute names, each of which
- *     could be prefixed with '!' to mean "not set".
+ *     could be prefixed with '-' to mean "set to false", '!' to mean
+ *     "unset".
  */
 
+/* What does a matched pattern decide? */
 struct attr_state {
-       int unset;
        struct git_attr *attr;
+       const char *setto;
 };
 
 struct match_attr {
@@ -88,13 +123,58 @@ struct match_attr {
 
 static const char blank[] = " \t\r\n";
 
+static const char *parse_attr(const char *src, int lineno, const char *cp,
+                             int *num_attr, struct match_attr *res)
+{
+       const char *ep, *equals;
+       int len;
+
+       ep = cp + strcspn(cp, blank);
+       equals = strchr(cp, '=');
+       if (equals && ep < equals)
+               equals = NULL;
+       if (equals)
+               len = equals - cp;
+       else
+               len = ep - cp;
+       if (!res) {
+               if (*cp == '-' || *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);
+                       return NULL;
+               }
+       } else {
+               struct attr_state *e;
+
+               e = &(res->state[*num_attr]);
+               if (*cp == '-' || *cp == '!') {
+                       e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
+                       cp++;
+                       len--;
+               }
+               else if (!equals)
+                       e->setto = ATTR__TRUE;
+               else {
+                       e->setto = xmemdupz(equals + 1, ep - equals - 1);
+               }
+               e->attr = git_attr(cp, len);
+       }
+       (*num_attr)++;
+       return ep + strspn(ep, blank);
+}
+
 static struct match_attr *parse_attr_line(const char *line, const char *src,
                                          int lineno, int macro_ok)
 {
        int namelen;
        int num_attr;
        const char *cp, *name;
-       struct match_attr *res = res;
+       struct match_attr *res = NULL;
        int pass;
        int is_macro;
 
@@ -114,6 +194,12 @@ 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);
+                       return NULL;
+               }
        }
        else
                is_macro = 0;
@@ -124,24 +210,12 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
                cp = name + namelen;
                cp = cp + strspn(cp, blank);
                while (*cp) {
-                       const char *ep;
-                       ep = cp + strcspn(cp, blank);
-                       if (pass) {
-                               struct attr_state *e;
-
-                               e = &(res->state[num_attr]);
-                               if (*cp == '!') {
-                                       e->unset = 1;
-                                       cp++;
-                               }
-                               e->attr = git_attr(cp, ep - cp);
-                       }
-                       num_attr++;
-                       cp = ep + strspn(ep, blank);
+                       cp = parse_attr(src, lineno, cp, &num_attr, res);
+                       if (!cp)
+                               return NULL;
                }
                if (pass)
                        break;
-
                res = xcalloc(1,
                              sizeof(*res) +
                              sizeof(struct attr_state) * num_attr +
@@ -149,7 +223,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
                if (is_macro)
                        res->u.attr = git_attr(name, namelen);
                else {
-                       res->u.pattern = (char*)&(res->state[num_attr]);
+                       res->u.pattern = (char *)&(res->state[num_attr]);
                        memcpy(res->u.pattern, name, namelen);
                        res->u.pattern[namelen] = 0;
                }
@@ -164,9 +238,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
  * come from many places.
  *
  * (1) .gitattribute file of the same directory;
- * (2) .gitattribute file of the parent directory if (1) does not have any match;
- *     this goes recursively upwards, just like .gitignore
- * (3) perhaps $GIT_DIR/info/attributes, as the final fallback.
+ * (2) .gitattribute file of the parent directory if (1) does not have
+ *      any match; this goes recursively upwards, just like .gitignore.
+ * (3) $GIT_DIR/info/attributes, which overrides both of the above.
  *
  * In the same file, later entries override the earlier match, so in the
  * global list, we would have entries from info/attributes the earliest
@@ -181,6 +255,7 @@ static struct attr_stack {
        struct attr_stack *prev;
        char *origin;
        unsigned num_matches;
+       unsigned alloc;
        struct match_attr **attrs;
 } *attr_stack;
 
@@ -188,16 +263,49 @@ static void free_attr_elem(struct attr_stack *e)
 {
        int i;
        free(e->origin);
-       for (i = 0; i < e->num_matches; i++)
-               free(e->attrs[i]);
+       for (i = 0; i < e->num_matches; i++) {
+               struct match_attr *a = e->attrs[i];
+               int j;
+               for (j = 0; j < a->num_attr; j++) {
+                       const char *setto = a->state[j].setto;
+                       if (setto == ATTR__TRUE ||
+                           setto == ATTR__FALSE ||
+                           setto == ATTR__UNSET ||
+                           setto == ATTR__UNKNOWN)
+                               ;
+                       else
+                               free((char *) setto);
+               }
+               free(a);
+       }
        free(e);
 }
 
 static const char *builtin_attr[] = {
-       "[attr]binary !diff !crlf",
+       "[attr]binary -diff -crlf",
        NULL,
 };
 
+static void handle_attr_line(struct attr_stack *res,
+                            const char *line,
+                            const char *src,
+                            int lineno,
+                            int macro_ok)
+{
+       struct match_attr *a;
+
+       a = parse_attr_line(line, src, lineno, macro_ok);
+       if (!a)
+               return;
+       if (res->alloc <= res->num_matches) {
+               res->alloc = alloc_nr(res->num_matches);
+               res->attrs = xrealloc(res->attrs,
+                                     sizeof(struct match_attr *) *
+                                     res->alloc);
+       }
+       res->attrs[res->num_matches++] = a;
+}
+
 static struct attr_stack *read_attr_from_array(const char **list)
 {
        struct attr_stack *res;
@@ -205,40 +313,111 @@ static struct attr_stack *read_attr_from_array(const char **list)
        int lineno = 0;
 
        res = xcalloc(1, sizeof(*res));
-       while ((line = *(list++)) != NULL) {
-               struct match_attr *a;
-
-               a = parse_attr_line(line, "[builtin]", ++lineno, 1);
-               if (!a)
-                       continue;
-               res->attrs = xrealloc(res->attrs, res->num_matches + 1);
-               res->attrs[res->num_matches++] = a;
-       }
+       while ((line = *(list++)) != NULL)
+               handle_attr_line(res, line, "[builtin]", ++lineno, 1);
        return res;
 }
 
+static enum git_attr_direction direction;
+static struct index_state *use_index;
+
 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
 {
-       FILE *fp;
+       FILE *fp = fopen(path, "r");
        struct attr_stack *res;
        char buf[2048];
        int lineno = 0;
 
-       res = xcalloc(1, sizeof(*res));
-       fp = fopen(path, "r");
        if (!fp)
-               return res;
+               return NULL;
+       res = xcalloc(1, sizeof(*res));
+       while (fgets(buf, sizeof(buf), fp))
+               handle_attr_line(res, buf, path, ++lineno, macro_ok);
+       fclose(fp);
+       return res;
+}
 
-       while (fgets(buf, sizeof(buf), fp)) {
-               struct match_attr *a;
+static void *read_index_data(const char *path)
+{
+       int pos, len;
+       unsigned long sz;
+       enum object_type type;
+       void *data;
+       struct index_state *istate = use_index ? use_index : &the_index;
+
+       len = strlen(path);
+       pos = index_name_pos(istate, path, len);
+       if (pos < 0) {
+               /*
+                * We might be in the middle of a merge, in which
+                * case we would read stage #2 (ours).
+                */
+               int i;
+               for (i = -pos - 1;
+                    (pos < 0 && i < istate->cache_nr &&
+                     !strcmp(istate->cache[i]->name, path));
+                    i++)
+                       if (ce_stage(istate->cache[i]) == 2)
+                               pos = i;
+       }
+       if (pos < 0)
+               return NULL;
+       data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
+       if (!data || type != OBJ_BLOB) {
+               free(data);
+               return NULL;
+       }
+       return data;
+}
 
-               a = parse_attr_line(buf, path, ++lineno, macro_ok);
-               if (!a)
-                       continue;
-               res->attrs = xrealloc(res->attrs, res->num_matches + 1);
-               res->attrs[res->num_matches++] = a;
+static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
+{
+       struct attr_stack *res;
+       char *buf, *sp;
+       int lineno = 0;
+
+       buf = read_index_data(path);
+       if (!buf)
+               return NULL;
+
+       res = xcalloc(1, sizeof(*res));
+       for (sp = buf; *sp; ) {
+               char *ep;
+               int more;
+               for (ep = sp; *ep && *ep != '\n'; ep++)
+                       ;
+               more = (*ep == '\n');
+               *ep = '\0';
+               handle_attr_line(res, sp, path, ++lineno, macro_ok);
+               sp = ep + more;
        }
-       fclose(fp);
+       free(buf);
+       return res;
+}
+
+static struct attr_stack *read_attr(const char *path, int macro_ok)
+{
+       struct attr_stack *res;
+
+       if (direction == GIT_ATTR_CHECKOUT) {
+               res = read_attr_from_index(path, macro_ok);
+               if (!res)
+                       res = read_attr_from_file(path, macro_ok);
+       }
+       else if (direction == GIT_ATTR_CHECKIN) {
+               res = read_attr_from_file(path, macro_ok);
+               if (!res)
+                       /*
+                        * There is no checked out .gitattributes file there, but
+                        * we might have it in the index.  We allow operation in a
+                        * sparsely checked out work tree, so read from it.
+                        */
+                       res = read_attr_from_index(path, macro_ok);
+       }
+       else
+               res = read_attr_from_index(path, macro_ok);
+       if (!res)
+               res = xcalloc(1, sizeof(*res));
        return res;
 }
 
@@ -247,10 +426,19 @@ static void debug_info(const char *what, struct attr_stack *elem)
 {
        fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
 }
-static void debug_set(const char *what, const char *match, struct git_attr *attr, int set)
+static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
 {
-       fprintf(stderr, "%s: %s => %d (%s)\n",
-               what, attr->name, set, match);
+       const char *value = v;
+
+       if (ATTR_TRUE(value))
+               value = "set";
+       else if (ATTR_FALSE(value))
+               value = "unset";
+       else if (ATTR_UNSET(value))
+               value = "unspecified";
+
+       fprintf(stderr, "%s: %s => %s (%s)\n",
+               what, attr->name, (char *) value, match);
 }
 #define debug_push(a) debug_info("push", (a))
 #define debug_pop(a) debug_info("pop", (a))
@@ -260,6 +448,15 @@ static void debug_set(const char *what, const char *match, struct git_attr *attr
 #define debug_set(a,b,c,d) do { ; } while (0)
 #endif
 
+static void drop_attr_stack(void)
+{
+       while (attr_stack) {
+               struct attr_stack *elem = attr_stack;
+               attr_stack = elem->prev;
+               free_attr_elem(elem);
+       }
+}
+
 static void bootstrap_attr_stack(void)
 {
        if (!attr_stack) {
@@ -270,13 +467,17 @@ static void bootstrap_attr_stack(void)
                elem->prev = attr_stack;
                attr_stack = elem;
 
-               elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
-               elem->origin = strdup("");
-               elem->prev = attr_stack;
-               attr_stack = elem;
-               debug_push(elem);
+               if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
+                       elem = read_attr(GITATTRIBUTES_FILE, 1);
+                       elem->origin = strdup("");
+                       elem->prev = attr_stack;
+                       attr_stack = elem;
+                       debug_push(elem);
+               }
 
                elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
+               if (!elem)
+                       elem = xcalloc(1, sizeof(*elem));
                elem->origin = NULL;
                elem->prev = attr_stack;
                attr_stack = elem;
@@ -287,7 +488,9 @@ static void prepare_attr_stack(const char *path, int dirlen)
 {
        struct attr_stack *elem, *info;
        int len;
-       char pathbuf[PATH_MAX];
+       struct strbuf pathbuf;
+
+       strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
 
        /*
         * At the bottom of the attribute stack is the built-in
@@ -331,24 +534,29 @@ static void prepare_attr_stack(const char *path, int dirlen)
        /*
         * Read from parent directories and push them down
         */
-       while (1) {
-               char *cp;
-
-               len = strlen(attr_stack->origin);
-               if (dirlen <= len)
-                       break;
-               memcpy(pathbuf, path, dirlen);
-               memcpy(pathbuf + dirlen, "/", 2);
-               cp = strchr(pathbuf + len + 1, '/');
-               strcpy(cp + 1, GITATTRIBUTES_FILE);
-               elem = read_attr_from_file(pathbuf, 0);
-               *cp = '\0';
-               elem->origin = strdup(pathbuf);
-               elem->prev = attr_stack;
-               attr_stack = elem;
-               debug_push(elem);
+       if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
+               while (1) {
+                       char *cp;
+
+                       len = strlen(attr_stack->origin);
+                       if (dirlen <= len)
+                               break;
+                       strbuf_reset(&pathbuf);
+                       strbuf_add(&pathbuf, path, dirlen);
+                       strbuf_addch(&pathbuf, '/');
+                       cp = strchr(pathbuf.buf + len + 1, '/');
+                       strcpy(cp + 1, GITATTRIBUTES_FILE);
+                       elem = read_attr(pathbuf.buf, 0);
+                       *cp = '\0';
+                       elem->origin = strdup(pathbuf.buf);
+                       elem->prev = attr_stack;
+                       attr_stack = elem;
+                       debug_push(elem);
+               }
        }
 
+       strbuf_release(&pathbuf);
+
        /*
         * Finally push the "info" one at the top of the stack.
         */
@@ -373,62 +581,61 @@ static int path_matches(const char *pathname, int pathlen,
        if (*pattern == '/')
                pattern++;
        if (pathlen < baselen ||
-           (baselen && pathname[baselen - 1] != '/') ||
+           (baselen && pathname[baselen] != '/') ||
            strncmp(pathname, base, baselen))
                return 0;
+       if (baselen != 0)
+               baselen++;
        return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
 }
 
+static int fill_one(const char *what, struct match_attr *a, int rem)
+{
+       struct git_attr_check *check = check_all_attr;
+       int i;
+
+       for (i = 0; 0 < rem && i < a->num_attr; i++) {
+               struct git_attr *attr = a->state[i].attr;
+               const char **n = &(check[attr->attr_nr].value);
+               const char *v = a->state[i].setto;
+
+               if (*n == ATTR__UNKNOWN) {
+                       debug_set(what, a->u.pattern, attr, v);
+                       *n = v;
+                       rem--;
+               }
+       }
+       return rem;
+}
+
 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
 {
+       int i;
        const char *base = stk->origin ? stk->origin : "";
-       int i, j;
-       struct git_attr_check *check = check_all_attr;
 
        for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
                struct match_attr *a = stk->attrs[i];
                if (a->is_macro)
                        continue;
                if (path_matches(path, pathlen,
-                                a->u.pattern, base, strlen(base))) {
-                       for (j = 0; 0 < rem && j < a->num_attr; j++) {
-                               struct git_attr *attr = a->state[j].attr;
-                               int set = !a->state[j].unset;
-                               int *n = &(check[attr->attr_nr].isset);
-
-                               if (*n < 0) {
-                                       debug_set("fill", a->u.pattern, attr, set);
-                                       *n = set;
-                                       rem--;
-                               }
-                       }
-               }
+                                a->u.pattern, base, strlen(base)))
+                       rem = fill_one("fill", a, rem);
        }
        return rem;
 }
 
 static int macroexpand(struct attr_stack *stk, int rem)
 {
-       int i, j;
+       int i;
        struct git_attr_check *check = check_all_attr;
 
        for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
                struct match_attr *a = stk->attrs[i];
                if (!a->is_macro)
                        continue;
-               if (check[a->u.attr->attr_nr].isset < 0)
+               if (check[a->u.attr->attr_nr].value != ATTR__TRUE)
                        continue;
-               for (j = 0; 0 < rem && j < a->num_attr; j++) {
-                       struct git_attr *attr = a->state[j].attr;
-                       int set = !a->state[j].unset;
-                       int *n = &(check[attr->attr_nr].isset);
-
-                       if (*n < 0) {
-                               debug_set("expand", a->u.attr->name, attr, set);
-                               *n = set;
-                               rem--;
-                       }
-               }
+               rem = fill_one("expand", a, rem);
        }
        return rem;
 }
@@ -441,7 +648,7 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
 
        bootstrap_attr_stack();
        for (i = 0; i < attr_nr; i++)
-               check_all_attr[i].isset = -1;
+               check_all_attr[i].value = ATTR__UNKNOWN;
 
        pathlen = strlen(path);
        cp = strrchr(path, '/');
@@ -457,8 +664,25 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
        for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
                rem = macroexpand(stk, rem);
 
-       for (i = 0; i < num; i++)
-               check[i].isset = check_all_attr[check[i].attr->attr_nr].isset;
+       for (i = 0; i < num; i++) {
+               const char *value = check_all_attr[check[i].attr->attr_nr].value;
+               if (value == ATTR__UNKNOWN)
+                       value = ATTR__UNSET;
+               check[i].value = value;
+       }
 
        return 0;
 }
+
+void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
+{
+       enum git_attr_direction old = direction;
+
+       if (is_bare_repository() && new != GIT_ATTR_INDEX)
+               die("BUG: non-INDEX attr direction in a bare repo");
+
+       direction = new;
+       if (new != old)
+               drop_attr_stack();
+       use_index = istate;
+}