2 * Handle git attributes. See gitattributes(5) for a description of
3 * the file syntax, and Documentation/technical/api-gitattributes.txt
4 * for a description of the API.
6 * One basic design decision here is that we are not going to support
7 * an insanely large number of attributes.
10 #define NO_THE_INDEX_COMPATIBILITY_MACROS
17 const char git_attr__true[] = "(builtin)true";
18 const char git_attr__false[] = "\0(builtin)false";
19 static const char git_attr__unknown[] = "(builtin)unknown";
20 #define ATTR__TRUE git_attr__true
21 #define ATTR__FALSE git_attr__false
22 #define ATTR__UNSET NULL
23 #define ATTR__UNKNOWN git_attr__unknown
25 /* This is a randomly chosen prime. */
33 struct git_attr *next;
38 char name[FLEX_ARRAY];
41 static int cannot_trust_maybe_real;
43 static struct git_attr_check *check_all_attr;
44 static struct git_attr *(git_attr_hash[HASHSIZE]);
46 char *git_attr_name(struct git_attr *attr)
51 static unsigned hash_name(const char *name, int namelen)
57 val = ((val << 7) | (val >> 22)) ^ c;
62 static int invalid_attr_name(const char *name, int namelen)
65 * Attribute name cannot begin with '-' and must consist of
66 * characters from [-A-Za-z0-9_.].
68 if (namelen <= 0 || *name == '-')
72 if (! (ch == '-' || ch == '.' || ch == '_' ||
73 ('0' <= ch && ch <= '9') ||
74 ('a' <= ch && ch <= 'z') ||
75 ('A' <= ch && ch <= 'Z')) )
81 static struct git_attr *git_attr_internal(const char *name, int len)
83 unsigned hval = hash_name(name, len);
84 unsigned pos = hval % HASHSIZE;
87 for (a = git_attr_hash[pos]; a; a = a->next) {
89 !memcmp(a->name, name, len) && !a->name[len])
93 if (invalid_attr_name(name, len))
96 FLEX_ALLOC_MEM(a, name, name, len);
98 a->next = git_attr_hash[pos];
99 a->attr_nr = attr_nr++;
102 git_attr_hash[pos] = a;
104 REALLOC_ARRAY(check_all_attr, attr_nr);
105 check_all_attr[a->attr_nr].attr = a;
106 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
110 struct git_attr *git_attr(const char *name)
112 return git_attr_internal(name, strlen(name));
115 /* What does a matched pattern decide? */
117 struct git_attr *attr;
125 unsigned flags; /* EXC_FLAG_* */
129 * One rule, as from a .gitattributes file.
131 * If is_macro is true, then u.attr is a pointer to the git_attr being
134 * If is_macro is false, then u.pattern points at the filename pattern
135 * to which the rule applies. (The memory pointed to is part of the
136 * memory block allocated for the match_attr instance.)
138 * In either case, num_attr is the number of attributes affected by
139 * this rule, and state is an array listing them. The attributes are
140 * listed as they appear in the file (macros unexpanded).
145 struct git_attr *attr;
149 struct attr_state state[FLEX_ARRAY];
152 static const char blank[] = " \t\r\n";
155 * Parse a whitespace-delimited attribute state (i.e., "attr",
156 * "-attr", "!attr", or "attr=value") from the string starting at src.
157 * If e is not NULL, write the results to *e. Return a pointer to the
158 * remainder of the string (with leading whitespace removed), or NULL
159 * if there was an error.
161 static const char *parse_attr(const char *src, int lineno, const char *cp,
162 struct attr_state *e)
164 const char *ep, *equals;
167 ep = cp + strcspn(cp, blank);
168 equals = strchr(cp, '=');
169 if (equals && ep < equals)
176 if (*cp == '-' || *cp == '!') {
180 if (invalid_attr_name(cp, len)) {
182 "%.*s is not a valid attribute name: %s:%d\n",
183 len, cp, src, lineno);
187 if (*cp == '-' || *cp == '!') {
188 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
193 e->setto = ATTR__TRUE;
195 e->setto = xmemdupz(equals + 1, ep - equals - 1);
197 e->attr = git_attr_internal(cp, len);
199 return ep + strspn(ep, blank);
202 static struct match_attr *parse_attr_line(const char *line, const char *src,
203 int lineno, int macro_ok)
207 const char *cp, *name, *states;
208 struct match_attr *res = NULL;
211 cp = line + strspn(line, blank);
212 if (!*cp || *cp == '#')
215 namelen = strcspn(name, blank);
216 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
217 starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
219 fprintf(stderr, "%s not allowed: %s:%d\n",
224 name += strlen(ATTRIBUTE_MACRO_PREFIX);
225 name += strspn(name, blank);
226 namelen = strcspn(name, blank);
227 if (invalid_attr_name(name, namelen)) {
229 "%.*s is not a valid attribute name: %s:%d\n",
230 namelen, name, src, lineno);
237 states = name + namelen;
238 states += strspn(states, blank);
240 /* First pass to count the attr_states */
241 for (cp = states, num_attr = 0; *cp; num_attr++) {
242 cp = parse_attr(src, lineno, cp, NULL);
249 sizeof(struct attr_state) * num_attr +
250 (is_macro ? 0 : namelen + 1));
252 res->u.attr = git_attr_internal(name, namelen);
253 res->u.attr->maybe_macro = 1;
255 char *p = (char *)&(res->state[num_attr]);
256 memcpy(p, name, namelen);
257 res->u.pat.pattern = p;
258 parse_exclude_pattern(&res->u.pat.pattern,
259 &res->u.pat.patternlen,
261 &res->u.pat.nowildcardlen);
262 if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
263 warning(_("Negative patterns are ignored in git attributes\n"
264 "Use '\\!' for literal leading exclamation."));
268 res->is_macro = is_macro;
269 res->num_attr = num_attr;
271 /* Second pass to fill the attr_states */
272 for (cp = states, i = 0; *cp; i++) {
273 cp = parse_attr(src, lineno, cp, &(res->state[i]));
275 res->state[i].attr->maybe_real = 1;
276 if (res->state[i].attr->maybe_macro)
277 cannot_trust_maybe_real = 1;
284 * Like info/exclude and .gitignore, the attribute information can
285 * come from many places.
287 * (1) .gitattribute file of the same directory;
288 * (2) .gitattribute file of the parent directory if (1) does not have
289 * any match; this goes recursively upwards, just like .gitignore.
290 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
292 * In the same file, later entries override the earlier match, so in the
293 * global list, we would have entries from info/attributes the earliest
294 * (reading the file from top to bottom), .gitattribute of the root
295 * directory (again, reading the file from top to bottom) down to the
296 * current directory, and then scan the list backwards to find the first match.
297 * This is exactly the same as what is_excluded() does in dir.c to deal with
301 static struct attr_stack {
302 struct attr_stack *prev;
305 unsigned num_matches;
307 struct match_attr **attrs;
310 static void free_attr_elem(struct attr_stack *e)
314 for (i = 0; i < e->num_matches; i++) {
315 struct match_attr *a = e->attrs[i];
317 for (j = 0; j < a->num_attr; j++) {
318 const char *setto = a->state[j].setto;
319 if (setto == ATTR__TRUE ||
320 setto == ATTR__FALSE ||
321 setto == ATTR__UNSET ||
322 setto == ATTR__UNKNOWN)
325 free((char *) setto);
333 static const char *builtin_attr[] = {
334 "[attr]binary -diff -merge -text",
338 static void handle_attr_line(struct attr_stack *res,
344 struct match_attr *a;
346 a = parse_attr_line(line, src, lineno, macro_ok);
349 ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
350 res->attrs[res->num_matches++] = a;
353 static struct attr_stack *read_attr_from_array(const char **list)
355 struct attr_stack *res;
359 res = xcalloc(1, sizeof(*res));
360 while ((line = *(list++)) != NULL)
361 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
365 static enum git_attr_direction direction;
366 static struct index_state *use_index;
368 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
370 FILE *fp = fopen(path, "r");
371 struct attr_stack *res;
376 if (errno != ENOENT && errno != ENOTDIR)
377 warn_on_inaccessible(path);
380 res = xcalloc(1, sizeof(*res));
381 while (fgets(buf, sizeof(buf), fp)) {
384 skip_utf8_bom(&bufp, strlen(bufp));
385 handle_attr_line(res, bufp, path, ++lineno, macro_ok);
391 static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
393 struct attr_stack *res;
397 buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL);
401 res = xcalloc(1, sizeof(*res));
402 for (sp = buf; *sp; ) {
405 for (ep = sp; *ep && *ep != '\n'; ep++)
407 more = (*ep == '\n');
409 handle_attr_line(res, sp, path, ++lineno, macro_ok);
416 static struct attr_stack *read_attr(const char *path, int macro_ok)
418 struct attr_stack *res;
420 if (direction == GIT_ATTR_CHECKOUT) {
421 res = read_attr_from_index(path, macro_ok);
423 res = read_attr_from_file(path, macro_ok);
425 else if (direction == GIT_ATTR_CHECKIN) {
426 res = read_attr_from_file(path, macro_ok);
429 * There is no checked out .gitattributes file there, but
430 * we might have it in the index. We allow operation in a
431 * sparsely checked out work tree, so read from it.
433 res = read_attr_from_index(path, macro_ok);
436 res = read_attr_from_index(path, macro_ok);
438 res = xcalloc(1, sizeof(*res));
443 static void debug_info(const char *what, struct attr_stack *elem)
445 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
447 static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
449 const char *value = v;
451 if (ATTR_TRUE(value))
453 else if (ATTR_FALSE(value))
455 else if (ATTR_UNSET(value))
456 value = "unspecified";
458 fprintf(stderr, "%s: %s => %s (%s)\n",
459 what, attr->name, (char *) value, match);
461 #define debug_push(a) debug_info("push", (a))
462 #define debug_pop(a) debug_info("pop", (a))
464 #define debug_push(a) do { ; } while (0)
465 #define debug_pop(a) do { ; } while (0)
466 #define debug_set(a,b,c,d) do { ; } while (0)
469 static void drop_attr_stack(void)
472 struct attr_stack *elem = attr_stack;
473 attr_stack = elem->prev;
474 free_attr_elem(elem);
478 static const char *git_etc_gitattributes(void)
480 static const char *system_wide;
482 system_wide = system_path(ETC_GITATTRIBUTES);
486 static int git_attr_system(void)
488 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
491 static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
493 static void bootstrap_attr_stack(void)
495 struct attr_stack *elem;
500 elem = read_attr_from_array(builtin_attr);
502 elem->prev = attr_stack;
505 if (git_attr_system()) {
506 elem = read_attr_from_file(git_etc_gitattributes(), 1);
509 elem->prev = attr_stack;
514 if (!git_attributes_file)
515 git_attributes_file = xdg_config_home("attributes");
516 if (git_attributes_file) {
517 elem = read_attr_from_file(git_attributes_file, 1);
520 elem->prev = attr_stack;
525 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
526 elem = read_attr(GITATTRIBUTES_FILE, 1);
527 elem->origin = xstrdup("");
529 elem->prev = attr_stack;
534 elem = read_attr_from_file(git_path_info_attributes(), 1);
536 elem = xcalloc(1, sizeof(*elem));
538 elem->prev = attr_stack;
542 static void prepare_attr_stack(const char *path, int dirlen)
544 struct attr_stack *elem, *info;
549 * At the bottom of the attribute stack is the built-in
550 * set of attribute definitions, followed by the contents
551 * of $(prefix)/etc/gitattributes and a file specified by
552 * core.attributesfile. Then, contents from
553 * .gitattribute files from directories closer to the
554 * root to the ones in deeper directories are pushed
555 * to the stack. Finally, at the very top of the stack
556 * we always keep the contents of $GIT_DIR/info/attributes.
558 * When checking, we use entries from near the top of the
559 * stack, preferring $GIT_DIR/info/attributes, then
560 * .gitattributes in deeper directories to shallower ones,
561 * and finally use the built-in set as the default.
563 bootstrap_attr_stack();
566 * Pop the "info" one that is always at the top of the stack.
569 attr_stack = info->prev;
572 * Pop the ones from directories that are not the prefix of
573 * the path we are checking. Break out of the loop when we see
574 * the root one (whose origin is an empty string "") or the builtin
575 * one (whose origin is NULL) without popping it.
577 while (attr_stack->origin) {
578 int namelen = strlen(attr_stack->origin);
581 if (namelen <= dirlen &&
582 !strncmp(elem->origin, path, namelen) &&
583 (!namelen || path[namelen] == '/'))
587 attr_stack = elem->prev;
588 free_attr_elem(elem);
592 * Read from parent directories and push them down
594 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
596 * bootstrap_attr_stack() should have added, and the
597 * above loop should have stopped before popping, the
598 * root element whose attr_stack->origin is set to an
601 struct strbuf pathbuf = STRBUF_INIT;
603 assert(attr_stack->origin);
605 len = strlen(attr_stack->origin);
608 cp = memchr(path + len + 1, '/', dirlen - len - 1);
611 strbuf_add(&pathbuf, path, cp - path);
612 strbuf_addch(&pathbuf, '/');
613 strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
614 elem = read_attr(pathbuf.buf, 0);
615 strbuf_setlen(&pathbuf, cp - path);
616 elem->origin = strbuf_detach(&pathbuf, &elem->originlen);
617 elem->prev = attr_stack;
622 strbuf_release(&pathbuf);
626 * Finally push the "info" one at the top of the stack.
628 info->prev = attr_stack;
632 static int path_matches(const char *pathname, int pathlen,
634 const struct pattern *pat,
635 const char *base, int baselen)
637 const char *pattern = pat->pattern;
638 int prefix = pat->nowildcardlen;
639 int isdir = (pathlen && pathname[pathlen - 1] == '/');
641 if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
644 if (pat->flags & EXC_FLAG_NODIR) {
645 return match_basename(pathname + basename_offset,
646 pathlen - basename_offset - isdir,
648 pat->patternlen, pat->flags);
650 return match_pathname(pathname, pathlen - isdir,
652 pattern, prefix, pat->patternlen, pat->flags);
655 static int macroexpand_one(int attr_nr, int rem);
657 static int fill_one(const char *what, struct match_attr *a, int rem)
659 struct git_attr_check *check = check_all_attr;
662 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
663 struct git_attr *attr = a->state[i].attr;
664 const char **n = &(check[attr->attr_nr].value);
665 const char *v = a->state[i].setto;
667 if (*n == ATTR__UNKNOWN) {
669 a->is_macro ? a->u.attr->name : a->u.pat.pattern,
673 rem = macroexpand_one(attr->attr_nr, rem);
679 static int fill(const char *path, int pathlen, int basename_offset,
680 struct attr_stack *stk, int rem)
683 const char *base = stk->origin ? stk->origin : "";
685 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
686 struct match_attr *a = stk->attrs[i];
689 if (path_matches(path, pathlen, basename_offset,
690 &a->u.pat, base, stk->originlen))
691 rem = fill_one("fill", a, rem);
696 static int macroexpand_one(int nr, int rem)
698 struct attr_stack *stk;
699 struct match_attr *a = NULL;
702 if (check_all_attr[nr].value != ATTR__TRUE ||
703 !check_all_attr[nr].attr->maybe_macro)
706 for (stk = attr_stack; !a && stk; stk = stk->prev)
707 for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
708 struct match_attr *ma = stk->attrs[i];
711 if (ma->u.attr->attr_nr == nr)
716 rem = fill_one("expand", a, rem);
722 * Collect attributes for path into the array pointed to by
723 * check_all_attr. If num is non-zero, only attributes in check[] are
724 * collected. Otherwise all attributes are collected.
726 static void collect_some_attrs(const char *path, int num,
727 struct git_attr_check *check)
730 struct attr_stack *stk;
731 int i, pathlen, rem, dirlen;
732 const char *cp, *last_slash = NULL;
735 for (cp = path; *cp; cp++) {
736 if (*cp == '/' && cp[1])
741 basename_offset = last_slash + 1 - path;
742 dirlen = last_slash - path;
748 prepare_attr_stack(path, dirlen);
749 for (i = 0; i < attr_nr; i++)
750 check_all_attr[i].value = ATTR__UNKNOWN;
751 if (num && !cannot_trust_maybe_real) {
753 for (i = 0; i < num; i++) {
754 if (!check[i].attr->maybe_real) {
755 struct git_attr_check *c;
756 c = check_all_attr + check[i].attr->attr_nr;
757 c->value = ATTR__UNSET;
766 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
767 rem = fill(path, pathlen, basename_offset, stk, rem);
770 int git_check_attr(const char *path, int num, struct git_attr_check *check)
774 collect_some_attrs(path, num, check);
776 for (i = 0; i < num; i++) {
777 const char *value = check_all_attr[check[i].attr->attr_nr].value;
778 if (value == ATTR__UNKNOWN)
780 check[i].value = value;
786 int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
790 collect_some_attrs(path, 0, NULL);
792 /* Count the number of attributes that are set. */
794 for (i = 0; i < attr_nr; i++) {
795 const char *value = check_all_attr[i].value;
796 if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
800 ALLOC_ARRAY(*check, count);
802 for (i = 0; i < attr_nr; i++) {
803 const char *value = check_all_attr[i].value;
804 if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
805 (*check)[j].attr = check_all_attr[i].attr;
806 (*check)[j].value = value;
814 void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
816 enum git_attr_direction old = direction;
818 if (is_bare_repository() && new != GIT_ATTR_INDEX)
819 die("BUG: non-INDEX attr direction in a bare repo");