Code

Catch invalid --depth option passed to clone or fetch
[git.git] / attr.c
diff --git a/attr.c b/attr.c
index 17f6a4dca521d9690377f2e93a0192d8a874d2ad..f6b3f7e850f9fcf5672031049ef1d6f43e619f63 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -1,4 +1,6 @@
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "exec_cmd.h"
 #include "attr.h"
 
 const char git_attr__true[] = "(builtin)true";
@@ -9,6 +11,8 @@ static const char git_attr__unknown[] = "(builtin)unknown";
 #define ATTR__UNSET NULL
 #define ATTR__UNKNOWN git_attr__unknown
 
+static const char *attributes_file;
+
 /*
  * The basic design decision here is that we are not going to have
  * insanely large number of attributes.
@@ -34,8 +38,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++;
@@ -65,7 +68,7 @@ static int invalid_attr_name(const char *name, int namelen)
        return 0;
 }
 
-struct git_attr *git_attr(const char *name, int len)
+static struct git_attr *git_attr_internal(const char *name, int len)
 {
        unsigned hval = hash_name(name, len);
        unsigned pos = hval % HASHSIZE;
@@ -95,6 +98,11 @@ struct git_attr *git_attr(const char *name, int len)
        return a;
 }
 
+struct git_attr *git_attr(const char *name)
+{
+       return git_attr_internal(name, strlen(name));
+}
+
 /*
  * .gitattributes file is one line per record, each of which is
  *
@@ -162,7 +170,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
                else {
                        e->setto = xmemdupz(equals + 1, ep - equals - 1);
                }
-               e->attr = git_attr(cp, len);
+               e->attr = git_attr_internal(cp, len);
        }
        (*num_attr)++;
        return ep + strspn(ep, blank);
@@ -221,9 +229,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
                              sizeof(struct attr_state) * num_attr +
                              (is_macro ? 0 : namelen + 1));
                if (is_macro)
-                       res->u.attr = git_attr(name, namelen);
+                       res->u.attr = git_attr_internal(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;
                }
@@ -274,7 +282,7 @@ static void free_attr_elem(struct attr_stack *e)
                            setto == ATTR__UNKNOWN)
                                ;
                        else
-                               free((char*) setto);
+                               free((char *) setto);
                }
                free(a);
        }
@@ -282,7 +290,7 @@ static void free_attr_elem(struct attr_stack *e)
 }
 
 static const char *builtin_attr[] = {
-       "[attr]binary -diff -crlf",
+       "[attr]binary -diff -text",
        NULL,
 };
 
@@ -318,6 +326,9 @@ static struct attr_stack *read_attr_from_array(const char **list)
        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 = fopen(path, "r");
@@ -340,9 +351,10 @@ static void *read_index_data(const char *path)
        unsigned long sz;
        enum object_type type;
        void *data;
+       struct index_state *istate = use_index ? use_index : &the_index;
 
        len = strlen(path);
-       pos = cache_name_pos(path, len);
+       pos = index_name_pos(istate, path, len);
        if (pos < 0) {
                /*
                 * We might be in the middle of a merge, in which
@@ -350,15 +362,15 @@ static void *read_index_data(const char *path)
                 */
                int i;
                for (i = -pos - 1;
-                    (pos < 0 && i < active_nr &&
-                     !strcmp(active_cache[i]->name, path));
+                    (pos < 0 && i < istate->cache_nr &&
+                     !strcmp(istate->cache[i]->name, path));
                     i++)
-                       if (ce_stage(active_cache[i]) == 2)
+                       if (ce_stage(istate->cache[i]) == 2)
                                pos = i;
        }
        if (pos < 0)
                return NULL;
-       data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
+       data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
        if (!data || type != OBJ_BLOB) {
                free(data);
                return NULL;
@@ -366,27 +378,17 @@ static void *read_index_data(const char *path)
        return data;
 }
 
-static struct attr_stack *read_attr(const char *path, int macro_ok)
+static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
 {
        struct attr_stack *res;
        char *buf, *sp;
        int lineno = 0;
 
-       res = read_attr_from_file(path, macro_ok);
-       if (res)
-               return res;
-
-       res = xcalloc(1, sizeof(*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.
-        */
        buf = read_index_data(path);
        if (!buf)
-               return res;
+               return NULL;
 
+       res = xcalloc(1, sizeof(*res));
        for (sp = buf; *sp; ) {
                char *ep;
                int more;
@@ -401,6 +403,32 @@ static struct attr_stack *read_attr(const char *path, int macro_ok)
        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;
+}
+
 #if DEBUG_ATTR
 static void debug_info(const char *what, struct attr_stack *elem)
 {
@@ -428,6 +456,36 @@ 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 const char *git_etc_gitattributes(void)
+{
+       static const char *system_wide;
+       if (!system_wide)
+               system_wide = system_path(ETC_GITATTRIBUTES);
+       return system_wide;
+}
+
+static int git_attr_system(void)
+{
+       return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
+}
+
+static int git_attr_config(const char *var, const char *value, void *dummy)
+{
+       if (!strcmp(var, "core.attributesfile"))
+               return git_config_pathname(&attributes_file, var, value);
+
+       return 0;
+}
+
 static void bootstrap_attr_stack(void)
 {
        if (!attr_stack) {
@@ -438,7 +496,26 @@ static void bootstrap_attr_stack(void)
                elem->prev = attr_stack;
                attr_stack = elem;
 
-               if (!is_bare_repository()) {
+               if (git_attr_system()) {
+                       elem = read_attr_from_file(git_etc_gitattributes(), 1);
+                       if (elem) {
+                               elem->origin = NULL;
+                               elem->prev = attr_stack;
+                               attr_stack = elem;
+                       }
+               }
+
+               git_config(git_attr_config, NULL);
+               if (attributes_file) {
+                       elem = read_attr_from_file(attributes_file, 1);
+                       if (elem) {
+                               elem->origin = NULL;
+                               elem->prev = attr_stack;
+                               attr_stack = elem;
+                       }
+               }
+
+               if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
                        elem = read_attr(GITATTRIBUTES_FILE, 1);
                        elem->origin = strdup("");
                        elem->prev = attr_stack;
@@ -465,7 +542,9 @@ static void prepare_attr_stack(const char *path, int dirlen)
 
        /*
         * At the bottom of the attribute stack is the built-in
-        * set of attribute definitions.  Then, contents from
+        * set of attribute definitions, followed by the contents
+        * of $(prefix)/etc/gitattributes and a file specified by
+        * core.attributesfile.  Then, contents from
         * .gitattribute files from directories closer to the
         * root to the ones in deeper directories are pushed
         * to the stack.  Finally, at the very top of the stack
@@ -505,7 +584,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
        /*
         * Read from parent directories and push them down
         */
-       if (!is_bare_repository()) {
+       if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
                while (1) {
                        char *cp;
 
@@ -526,6 +605,8 @@ static void prepare_attr_stack(const char *path, int dirlen)
                }
        }
 
+       strbuf_release(&pathbuf);
+
        /*
         * Finally push the "info" one at the top of the stack.
         */
@@ -558,20 +639,25 @@ static int path_matches(const char *pathname, int pathlen,
        return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
 }
 
+static int macroexpand_one(int attr_nr, int rem);
+
 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++) {
+       for (i = a->num_attr - 1; 0 < rem && 0 <= i; 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);
+                       debug_set(what,
+                                 a->is_macro ? a->u.attr->name : a->u.pattern,
+                                 attr, v);
                        *n = v;
                        rem--;
+                       rem = macroexpand_one(attr->attr_nr, rem);
                }
        }
        return rem;
@@ -593,19 +679,27 @@ static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
        return rem;
 }
 
-static int macroexpand(struct attr_stack *stk, int rem)
+static int macroexpand_one(int attr_nr, int rem)
 {
+       struct attr_stack *stk;
+       struct match_attr *a = NULL;
        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].value != ATTR__TRUE)
-                       continue;
+       if (check_all_attr[attr_nr].value != ATTR__TRUE)
+               return rem;
+
+       for (stk = attr_stack; !a && stk; stk = stk->prev)
+               for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
+                       struct match_attr *ma = stk->attrs[i];
+                       if (!ma->is_macro)
+                               continue;
+                       if (ma->u.attr->attr_nr == attr_nr)
+                               a = ma;
+               }
+
+       if (a)
                rem = fill_one("expand", a, rem);
-       }
+
        return rem;
 }
 
@@ -630,9 +724,6 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
        for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
                rem = fill(path, pathlen, stk, rem);
 
-       for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
-               rem = macroexpand(stk, rem);
-
        for (i = 0; i < num; i++) {
                const char *value = check_all_attr[check[i].attr->attr_nr].value;
                if (value == ATTR__UNKNOWN)
@@ -642,3 +733,16 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
 
        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;
+}