1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
5 const char git_attr__true[] = "(builtin)true";
6 const char git_attr__false[] = "\0(builtin)false";
7 static const char git_attr__unknown[] = "(builtin)unknown";
8 #define ATTR__TRUE git_attr__true
9 #define ATTR__FALSE git_attr__false
10 #define ATTR__UNSET NULL
11 #define ATTR__UNKNOWN git_attr__unknown
14 * The basic design decision here is that we are not going to have
15 * insanely large number of attributes.
17 * This is a randomly chosen prime.
26 struct git_attr *next;
29 char name[FLEX_ARRAY];
33 static struct git_attr_check *check_all_attr;
34 static struct git_attr *(git_attr_hash[HASHSIZE]);
36 static unsigned hash_name(const char *name, int namelen)
42 val = ((val << 7) | (val >> 22)) ^ c;
47 static int invalid_attr_name(const char *name, int namelen)
50 * Attribute name cannot begin with '-' and from
51 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
52 * as we might later want to allow non-binary value for
53 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
59 if (! (ch == '-' || ch == '.' || ch == '_' ||
60 ('0' <= ch && ch <= '9') ||
61 ('a' <= ch && ch <= 'z') ||
62 ('A' <= ch && ch <= 'Z')) )
68 static struct git_attr *git_attr_internal(const char *name, int len)
70 unsigned hval = hash_name(name, len);
71 unsigned pos = hval % HASHSIZE;
74 for (a = git_attr_hash[pos]; a; a = a->next) {
76 !memcmp(a->name, name, len) && !a->name[len])
80 if (invalid_attr_name(name, len))
83 a = xmalloc(sizeof(*a) + len + 1);
84 memcpy(a->name, name, len);
87 a->next = git_attr_hash[pos];
88 a->attr_nr = attr_nr++;
89 git_attr_hash[pos] = a;
91 check_all_attr = xrealloc(check_all_attr,
92 sizeof(*check_all_attr) * attr_nr);
93 check_all_attr[a->attr_nr].attr = a;
94 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
98 struct git_attr *git_attr(const char *name)
100 return git_attr_internal(name, strlen(name));
104 * .gitattributes file is one line per record, each of which is
108 * (3) whitespace separated list of attribute names, each of which
109 * could be prefixed with '-' to mean "set to false", '!' to mean
113 /* What does a matched pattern decide? */
115 struct git_attr *attr;
122 struct git_attr *attr;
126 struct attr_state state[FLEX_ARRAY];
129 static const char blank[] = " \t\r\n";
131 static const char *parse_attr(const char *src, int lineno, const char *cp,
132 int *num_attr, struct match_attr *res)
134 const char *ep, *equals;
137 ep = cp + strcspn(cp, blank);
138 equals = strchr(cp, '=');
139 if (equals && ep < equals)
146 if (*cp == '-' || *cp == '!') {
150 if (invalid_attr_name(cp, len)) {
152 "%.*s is not a valid attribute name: %s:%d\n",
153 len, cp, src, lineno);
157 struct attr_state *e;
159 e = &(res->state[*num_attr]);
160 if (*cp == '-' || *cp == '!') {
161 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
166 e->setto = ATTR__TRUE;
168 e->setto = xmemdupz(equals + 1, ep - equals - 1);
170 e->attr = git_attr_internal(cp, len);
173 return ep + strspn(ep, blank);
176 static struct match_attr *parse_attr_line(const char *line, const char *src,
177 int lineno, int macro_ok)
181 const char *cp, *name;
182 struct match_attr *res = NULL;
186 cp = line + strspn(line, blank);
187 if (!*cp || *cp == '#')
190 namelen = strcspn(name, blank);
191 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
192 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
194 fprintf(stderr, "%s not allowed: %s:%d\n",
199 name += strlen(ATTRIBUTE_MACRO_PREFIX);
200 name += strspn(name, blank);
201 namelen = strcspn(name, blank);
202 if (invalid_attr_name(name, namelen)) {
204 "%.*s is not a valid attribute name: %s:%d\n",
205 namelen, name, src, lineno);
212 for (pass = 0; pass < 2; pass++) {
213 /* pass 0 counts and allocates, pass 1 fills */
216 cp = cp + strspn(cp, blank);
218 cp = parse_attr(src, lineno, cp, &num_attr, res);
226 sizeof(struct attr_state) * num_attr +
227 (is_macro ? 0 : namelen + 1));
229 res->u.attr = git_attr_internal(name, namelen);
231 res->u.pattern = (char *)&(res->state[num_attr]);
232 memcpy(res->u.pattern, name, namelen);
233 res->u.pattern[namelen] = 0;
235 res->is_macro = is_macro;
236 res->num_attr = num_attr;
242 * Like info/exclude and .gitignore, the attribute information can
243 * come from many places.
245 * (1) .gitattribute file of the same directory;
246 * (2) .gitattribute file of the parent directory if (1) does not have
247 * any match; this goes recursively upwards, just like .gitignore.
248 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
250 * In the same file, later entries override the earlier match, so in the
251 * global list, we would have entries from info/attributes the earliest
252 * (reading the file from top to bottom), .gitattribute of the root
253 * directory (again, reading the file from top to bottom) down to the
254 * current directory, and then scan the list backwards to find the first match.
255 * This is exactly the same as what excluded() does in dir.c to deal with
259 static struct attr_stack {
260 struct attr_stack *prev;
262 unsigned num_matches;
264 struct match_attr **attrs;
267 static void free_attr_elem(struct attr_stack *e)
271 for (i = 0; i < e->num_matches; i++) {
272 struct match_attr *a = e->attrs[i];
274 for (j = 0; j < a->num_attr; j++) {
275 const char *setto = a->state[j].setto;
276 if (setto == ATTR__TRUE ||
277 setto == ATTR__FALSE ||
278 setto == ATTR__UNSET ||
279 setto == ATTR__UNKNOWN)
282 free((char *) setto);
289 static const char *builtin_attr[] = {
290 "[attr]binary -diff -crlf",
294 static void handle_attr_line(struct attr_stack *res,
300 struct match_attr *a;
302 a = parse_attr_line(line, src, lineno, macro_ok);
305 if (res->alloc <= res->num_matches) {
306 res->alloc = alloc_nr(res->num_matches);
307 res->attrs = xrealloc(res->attrs,
308 sizeof(struct match_attr *) *
311 res->attrs[res->num_matches++] = a;
314 static struct attr_stack *read_attr_from_array(const char **list)
316 struct attr_stack *res;
320 res = xcalloc(1, sizeof(*res));
321 while ((line = *(list++)) != NULL)
322 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
326 static enum git_attr_direction direction;
327 static struct index_state *use_index;
329 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
331 FILE *fp = fopen(path, "r");
332 struct attr_stack *res;
338 res = xcalloc(1, sizeof(*res));
339 while (fgets(buf, sizeof(buf), fp))
340 handle_attr_line(res, buf, path, ++lineno, macro_ok);
345 static void *read_index_data(const char *path)
349 enum object_type type;
351 struct index_state *istate = use_index ? use_index : &the_index;
354 pos = index_name_pos(istate, path, len);
357 * We might be in the middle of a merge, in which
358 * case we would read stage #2 (ours).
362 (pos < 0 && i < istate->cache_nr &&
363 !strcmp(istate->cache[i]->name, path));
365 if (ce_stage(istate->cache[i]) == 2)
370 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
371 if (!data || type != OBJ_BLOB) {
378 static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
380 struct attr_stack *res;
384 buf = read_index_data(path);
388 res = xcalloc(1, sizeof(*res));
389 for (sp = buf; *sp; ) {
392 for (ep = sp; *ep && *ep != '\n'; ep++)
394 more = (*ep == '\n');
396 handle_attr_line(res, sp, path, ++lineno, macro_ok);
403 static struct attr_stack *read_attr(const char *path, int macro_ok)
405 struct attr_stack *res;
407 if (direction == GIT_ATTR_CHECKOUT) {
408 res = read_attr_from_index(path, macro_ok);
410 res = read_attr_from_file(path, macro_ok);
412 else if (direction == GIT_ATTR_CHECKIN) {
413 res = read_attr_from_file(path, macro_ok);
416 * There is no checked out .gitattributes file there, but
417 * we might have it in the index. We allow operation in a
418 * sparsely checked out work tree, so read from it.
420 res = read_attr_from_index(path, macro_ok);
423 res = read_attr_from_index(path, macro_ok);
425 res = xcalloc(1, sizeof(*res));
430 static void debug_info(const char *what, struct attr_stack *elem)
432 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
434 static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
436 const char *value = v;
438 if (ATTR_TRUE(value))
440 else if (ATTR_FALSE(value))
442 else if (ATTR_UNSET(value))
443 value = "unspecified";
445 fprintf(stderr, "%s: %s => %s (%s)\n",
446 what, attr->name, (char *) value, match);
448 #define debug_push(a) debug_info("push", (a))
449 #define debug_pop(a) debug_info("pop", (a))
451 #define debug_push(a) do { ; } while (0)
452 #define debug_pop(a) do { ; } while (0)
453 #define debug_set(a,b,c,d) do { ; } while (0)
456 static void drop_attr_stack(void)
459 struct attr_stack *elem = attr_stack;
460 attr_stack = elem->prev;
461 free_attr_elem(elem);
465 static void bootstrap_attr_stack(void)
468 struct attr_stack *elem;
470 elem = read_attr_from_array(builtin_attr);
472 elem->prev = attr_stack;
475 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
476 elem = read_attr(GITATTRIBUTES_FILE, 1);
477 elem->origin = strdup("");
478 elem->prev = attr_stack;
483 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
485 elem = xcalloc(1, sizeof(*elem));
487 elem->prev = attr_stack;
492 static void prepare_attr_stack(const char *path, int dirlen)
494 struct attr_stack *elem, *info;
496 struct strbuf pathbuf;
498 strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
501 * At the bottom of the attribute stack is the built-in
502 * set of attribute definitions. Then, contents from
503 * .gitattribute files from directories closer to the
504 * root to the ones in deeper directories are pushed
505 * to the stack. Finally, at the very top of the stack
506 * we always keep the contents of $GIT_DIR/info/attributes.
508 * When checking, we use entries from near the top of the
509 * stack, preferring $GIT_DIR/info/attributes, then
510 * .gitattributes in deeper directories to shallower ones,
511 * and finally use the built-in set as the default.
514 bootstrap_attr_stack();
517 * Pop the "info" one that is always at the top of the stack.
520 attr_stack = info->prev;
523 * Pop the ones from directories that are not the prefix of
524 * the path we are checking.
526 while (attr_stack && attr_stack->origin) {
527 int namelen = strlen(attr_stack->origin);
530 if (namelen <= dirlen &&
531 !strncmp(elem->origin, path, namelen))
535 attr_stack = elem->prev;
536 free_attr_elem(elem);
540 * Read from parent directories and push them down
542 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
546 len = strlen(attr_stack->origin);
549 strbuf_reset(&pathbuf);
550 strbuf_add(&pathbuf, path, dirlen);
551 strbuf_addch(&pathbuf, '/');
552 cp = strchr(pathbuf.buf + len + 1, '/');
553 strcpy(cp + 1, GITATTRIBUTES_FILE);
554 elem = read_attr(pathbuf.buf, 0);
556 elem->origin = strdup(pathbuf.buf);
557 elem->prev = attr_stack;
563 strbuf_release(&pathbuf);
566 * Finally push the "info" one at the top of the stack.
568 info->prev = attr_stack;
572 static int path_matches(const char *pathname, int pathlen,
574 const char *base, int baselen)
576 if (!strchr(pattern, '/')) {
578 const char *basename = strrchr(pathname, '/');
579 basename = basename ? basename + 1 : pathname;
580 return (fnmatch(pattern, basename, 0) == 0);
583 * match with FNM_PATHNAME; the pattern has base implicitly
588 if (pathlen < baselen ||
589 (baselen && pathname[baselen] != '/') ||
590 strncmp(pathname, base, baselen))
594 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
597 static int macroexpand_one(int attr_nr, int rem);
599 static int fill_one(const char *what, struct match_attr *a, int rem)
601 struct git_attr_check *check = check_all_attr;
604 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
605 struct git_attr *attr = a->state[i].attr;
606 const char **n = &(check[attr->attr_nr].value);
607 const char *v = a->state[i].setto;
609 if (*n == ATTR__UNKNOWN) {
611 a->is_macro ? a->u.attr->name : a->u.pattern,
615 rem = macroexpand_one(attr->attr_nr, rem);
621 static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
624 const char *base = stk->origin ? stk->origin : "";
626 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
627 struct match_attr *a = stk->attrs[i];
630 if (path_matches(path, pathlen,
631 a->u.pattern, base, strlen(base)))
632 rem = fill_one("fill", a, rem);
637 static int macroexpand_one(int attr_nr, int rem)
639 struct attr_stack *stk;
640 struct match_attr *a = NULL;
643 if (check_all_attr[attr_nr].value != ATTR__TRUE)
646 for (stk = attr_stack; !a && stk; stk = stk->prev)
647 for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
648 struct match_attr *ma = stk->attrs[i];
651 if (ma->u.attr->attr_nr == attr_nr)
656 rem = fill_one("expand", a, rem);
661 int git_checkattr(const char *path, int num, struct git_attr_check *check)
663 struct attr_stack *stk;
665 int dirlen, pathlen, i, rem;
667 bootstrap_attr_stack();
668 for (i = 0; i < attr_nr; i++)
669 check_all_attr[i].value = ATTR__UNKNOWN;
671 pathlen = strlen(path);
672 cp = strrchr(path, '/');
677 prepare_attr_stack(path, dirlen);
679 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
680 rem = fill(path, pathlen, stk, rem);
682 for (i = 0; i < num; i++) {
683 const char *value = check_all_attr[check[i].attr->attr_nr].value;
684 if (value == ATTR__UNKNOWN)
686 check[i].value = value;
692 void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
694 enum git_attr_direction old = direction;
696 if (is_bare_repository() && new != GIT_ATTR_INDEX)
697 die("BUG: non-INDEX attr direction in a bare repo");