1#define NO_THE_INDEX_COMPATIBILITY_MACROS
2#include "cache.h"
3#include "exec_cmd.h"
4#include "attr.h"
5
6const char git_attr__true[] = "(builtin)true";
7const char git_attr__false[] = "\0(builtin)false";
8static const char git_attr__unknown[] = "(builtin)unknown";
9#define ATTR__TRUE git_attr__true
10#define ATTR__FALSE git_attr__false
11#define ATTR__UNSET NULL
12#define ATTR__UNKNOWN git_attr__unknown
13
14static const char *attributes_file;
15
16/*
17 * The basic design decision here is that we are not going to have
18 * insanely large number of attributes.
19 *
20 * This is a randomly chosen prime.
21 */
22#define HASHSIZE 257
23
24#ifndef DEBUG_ATTR
25#define DEBUG_ATTR 0
26#endif
27
28struct git_attr {
29 struct git_attr *next;
30 unsigned h;
31 int attr_nr;
32 char name[FLEX_ARRAY];
33};
34static int attr_nr;
35
36static struct git_attr_check *check_all_attr;
37static struct git_attr *(git_attr_hash[HASHSIZE]);
38
39static unsigned hash_name(const char *name, int namelen)
40{
41 unsigned val = 0, c;
42
43 while (namelen--) {
44 c = *name++;
45 val = ((val << 7) | (val >> 22)) ^ c;
46 }
47 return val;
48}
49
50static int invalid_attr_name(const char *name, int namelen)
51{
52 /*
53 * Attribute name cannot begin with '-' and from
54 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
55 * as we might later want to allow non-binary value for
56 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
57 */
58 if (*name == '-')
59 return -1;
60 while (namelen--) {
61 char ch = *name++;
62 if (! (ch == '-' || ch == '.' || ch == '_' ||
63 ('0' <= ch && ch <= '9') ||
64 ('a' <= ch && ch <= 'z') ||
65 ('A' <= ch && ch <= 'Z')) )
66 return -1;
67 }
68 return 0;
69}
70
71static struct git_attr *git_attr_internal(const char *name, int len)
72{
73 unsigned hval = hash_name(name, len);
74 unsigned pos = hval % HASHSIZE;
75 struct git_attr *a;
76
77 for (a = git_attr_hash[pos]; a; a = a->next) {
78 if (a->h == hval &&
79 !memcmp(a->name, name, len) && !a->name[len])
80 return a;
81 }
82
83 if (invalid_attr_name(name, len))
84 return NULL;
85
86 a = xmalloc(sizeof(*a) + len + 1);
87 memcpy(a->name, name, len);
88 a->name[len] = 0;
89 a->h = hval;
90 a->next = git_attr_hash[pos];
91 a->attr_nr = attr_nr++;
92 git_attr_hash[pos] = a;
93
94 check_all_attr = xrealloc(check_all_attr,
95 sizeof(*check_all_attr) * attr_nr);
96 check_all_attr[a->attr_nr].attr = a;
97 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
98 return a;
99}
100
101struct git_attr *git_attr(const char *name)
102{
103 return git_attr_internal(name, strlen(name));
104}
105
106/*
107 * .gitattributes file is one line per record, each of which is
108 *
109 * (1) glob pattern.
110 * (2) whitespace
111 * (3) whitespace separated list of attribute names, each of which
112 * could be prefixed with '-' to mean "set to false", '!' to mean
113 * "unset".
114 */
115
116/* What does a matched pattern decide? */
117struct attr_state {
118 struct git_attr *attr;
119 const char *setto;
120};
121
122struct match_attr {
123 union {
124 char *pattern;
125 struct git_attr *attr;
126 } u;
127 char is_macro;
128 unsigned num_attr;
129 struct attr_state state[FLEX_ARRAY];
130};
131
132static const char blank[] = " \t\r\n";
133
134static const char *parse_attr(const char *src, int lineno, const char *cp,
135 int *num_attr, struct match_attr *res)
136{
137 const char *ep, *equals;
138 int len;
139
140 ep = cp + strcspn(cp, blank);
141 equals = strchr(cp, '=');
142 if (equals && ep < equals)
143 equals = NULL;
144 if (equals)
145 len = equals - cp;
146 else
147 len = ep - cp;
148 if (!res) {
149 if (*cp == '-' || *cp == '!') {
150 cp++;
151 len--;
152 }
153 if (invalid_attr_name(cp, len)) {
154 fprintf(stderr,
155 "%.*s is not a valid attribute name: %s:%d\n",
156 len, cp, src, lineno);
157 return NULL;
158 }
159 } else {
160 struct attr_state *e;
161
162 e = &(res->state[*num_attr]);
163 if (*cp == '-' || *cp == '!') {
164 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
165 cp++;
166 len--;
167 }
168 else if (!equals)
169 e->setto = ATTR__TRUE;
170 else {
171 e->setto = xmemdupz(equals + 1, ep - equals - 1);
172 }
173 e->attr = git_attr_internal(cp, len);
174 }
175 (*num_attr)++;
176 return ep + strspn(ep, blank);
177}
178
179static struct match_attr *parse_attr_line(const char *line, const char *src,
180 int lineno, int macro_ok)
181{
182 int namelen;
183 int num_attr;
184 const char *cp, *name;
185 struct match_attr *res = NULL;
186 int pass;
187 int is_macro;
188
189 cp = line + strspn(line, blank);
190 if (!*cp || *cp == '#')
191 return NULL;
192 name = cp;
193 namelen = strcspn(name, blank);
194 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
195 !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
196 if (!macro_ok) {
197 fprintf(stderr, "%s not allowed: %s:%d\n",
198 name, src, lineno);
199 return NULL;
200 }
201 is_macro = 1;
202 name += strlen(ATTRIBUTE_MACRO_PREFIX);
203 name += strspn(name, blank);
204 namelen = strcspn(name, blank);
205 if (invalid_attr_name(name, namelen)) {
206 fprintf(stderr,
207 "%.*s is not a valid attribute name: %s:%d\n",
208 namelen, name, src, lineno);
209 return NULL;
210 }
211 }
212 else
213 is_macro = 0;
214
215 for (pass = 0; pass < 2; pass++) {
216 /* pass 0 counts and allocates, pass 1 fills */
217 num_attr = 0;
218 cp = name + namelen;
219 cp = cp + strspn(cp, blank);
220 while (*cp) {
221 cp = parse_attr(src, lineno, cp, &num_attr, res);
222 if (!cp)
223 return NULL;
224 }
225 if (pass)
226 break;
227 res = xcalloc(1,
228 sizeof(*res) +
229 sizeof(struct attr_state) * num_attr +
230 (is_macro ? 0 : namelen + 1));
231 if (is_macro)
232 res->u.attr = git_attr_internal(name, namelen);
233 else {
234 res->u.pattern = (char *)&(res->state[num_attr]);
235 memcpy(res->u.pattern, name, namelen);
236 res->u.pattern[namelen] = 0;
237 }
238 res->is_macro = is_macro;
239 res->num_attr = num_attr;
240 }
241 return res;
242}
243
244/*
245 * Like info/exclude and .gitignore, the attribute information can
246 * come from many places.
247 *
248 * (1) .gitattribute file of the same directory;
249 * (2) .gitattribute file of the parent directory if (1) does not have
250 * any match; this goes recursively upwards, just like .gitignore.
251 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
252 *
253 * In the same file, later entries override the earlier match, so in the
254 * global list, we would have entries from info/attributes the earliest
255 * (reading the file from top to bottom), .gitattribute of the root
256 * directory (again, reading the file from top to bottom) down to the
257 * current directory, and then scan the list backwards to find the first match.
258 * This is exactly the same as what excluded() does in dir.c to deal with
259 * .gitignore
260 */
261
262static struct attr_stack {
263 struct attr_stack *prev;
264 char *origin;
265 unsigned num_matches;
266 unsigned alloc;
267 struct match_attr **attrs;
268} *attr_stack;
269
270static void free_attr_elem(struct attr_stack *e)
271{
272 int i;
273 free(e->origin);
274 for (i = 0; i < e->num_matches; i++) {
275 struct match_attr *a = e->attrs[i];
276 int j;
277 for (j = 0; j < a->num_attr; j++) {
278 const char *setto = a->state[j].setto;
279 if (setto == ATTR__TRUE ||
280 setto == ATTR__FALSE ||
281 setto == ATTR__UNSET ||
282 setto == ATTR__UNKNOWN)
283 ;
284 else
285 free((char *) setto);
286 }
287 free(a);
288 }
289 free(e->attrs);
290 free(e);
291}
292
293static const char *builtin_attr[] = {
294 "[attr]binary -diff -text",
295 NULL,
296};
297
298static void handle_attr_line(struct attr_stack *res,
299 const char *line,
300 const char *src,
301 int lineno,
302 int macro_ok)
303{
304 struct match_attr *a;
305
306 a = parse_attr_line(line, src, lineno, macro_ok);
307 if (!a)
308 return;
309 if (res->alloc <= res->num_matches) {
310 res->alloc = alloc_nr(res->num_matches);
311 res->attrs = xrealloc(res->attrs,
312 sizeof(struct match_attr *) *
313 res->alloc);
314 }
315 res->attrs[res->num_matches++] = a;
316}
317
318static struct attr_stack *read_attr_from_array(const char **list)
319{
320 struct attr_stack *res;
321 const char *line;
322 int lineno = 0;
323
324 res = xcalloc(1, sizeof(*res));
325 while ((line = *(list++)) != NULL)
326 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
327 return res;
328}
329
330static enum git_attr_direction direction;
331static struct index_state *use_index;
332
333static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
334{
335 FILE *fp = fopen(path, "r");
336 struct attr_stack *res;
337 char buf[2048];
338 int lineno = 0;
339
340 if (!fp)
341 return NULL;
342 res = xcalloc(1, sizeof(*res));
343 while (fgets(buf, sizeof(buf), fp))
344 handle_attr_line(res, buf, path, ++lineno, macro_ok);
345 fclose(fp);
346 return res;
347}
348
349static void *read_index_data(const char *path)
350{
351 int pos, len;
352 unsigned long sz;
353 enum object_type type;
354 void *data;
355 struct index_state *istate = use_index ? use_index : &the_index;
356
357 len = strlen(path);
358 pos = index_name_pos(istate, path, len);
359 if (pos < 0) {
360 /*
361 * We might be in the middle of a merge, in which
362 * case we would read stage #2 (ours).
363 */
364 int i;
365 for (i = -pos - 1;
366 (pos < 0 && i < istate->cache_nr &&
367 !strcmp(istate->cache[i]->name, path));
368 i++)
369 if (ce_stage(istate->cache[i]) == 2)
370 pos = i;
371 }
372 if (pos < 0)
373 return NULL;
374 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
375 if (!data || type != OBJ_BLOB) {
376 free(data);
377 return NULL;
378 }
379 return data;
380}
381
382static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
383{
384 struct attr_stack *res;
385 char *buf, *sp;
386 int lineno = 0;
387
388 buf = read_index_data(path);
389 if (!buf)
390 return NULL;
391
392 res = xcalloc(1, sizeof(*res));
393 for (sp = buf; *sp; ) {
394 char *ep;
395 int more;
396 for (ep = sp; *ep && *ep != '\n'; ep++)
397 ;
398 more = (*ep == '\n');
399 *ep = '\0';
400 handle_attr_line(res, sp, path, ++lineno, macro_ok);
401 sp = ep + more;
402 }
403 free(buf);
404 return res;
405}
406
407static struct attr_stack *read_attr(const char *path, int macro_ok)
408{
409 struct attr_stack *res;
410
411 if (direction == GIT_ATTR_CHECKOUT) {
412 res = read_attr_from_index(path, macro_ok);
413 if (!res)
414 res = read_attr_from_file(path, macro_ok);
415 }
416 else if (direction == GIT_ATTR_CHECKIN) {
417 res = read_attr_from_file(path, macro_ok);
418 if (!res)
419 /*
420 * There is no checked out .gitattributes file there, but
421 * we might have it in the index. We allow operation in a
422 * sparsely checked out work tree, so read from it.
423 */
424 res = read_attr_from_index(path, macro_ok);
425 }
426 else
427 res = read_attr_from_index(path, macro_ok);
428 if (!res)
429 res = xcalloc(1, sizeof(*res));
430 return res;
431}
432
433#if DEBUG_ATTR
434static void debug_info(const char *what, struct attr_stack *elem)
435{
436 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
437}
438static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
439{
440 const char *value = v;
441
442 if (ATTR_TRUE(value))
443 value = "set";
444 else if (ATTR_FALSE(value))
445 value = "unset";
446 else if (ATTR_UNSET(value))
447 value = "unspecified";
448
449 fprintf(stderr, "%s: %s => %s (%s)\n",
450 what, attr->name, (char *) value, match);
451}
452#define debug_push(a) debug_info("push", (a))
453#define debug_pop(a) debug_info("pop", (a))
454#else
455#define debug_push(a) do { ; } while (0)
456#define debug_pop(a) do { ; } while (0)
457#define debug_set(a,b,c,d) do { ; } while (0)
458#endif
459
460static void drop_attr_stack(void)
461{
462 while (attr_stack) {
463 struct attr_stack *elem = attr_stack;
464 attr_stack = elem->prev;
465 free_attr_elem(elem);
466 }
467}
468
469static const char *git_etc_gitattributes(void)
470{
471 static const char *system_wide;
472 if (!system_wide)
473 system_wide = system_path(ETC_GITATTRIBUTES);
474 return system_wide;
475}
476
477static int git_attr_system(void)
478{
479 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
480}
481
482static int git_attr_config(const char *var, const char *value, void *dummy)
483{
484 if (!strcmp(var, "core.attributesfile"))
485 return git_config_pathname(&attributes_file, var, value);
486
487 return 0;
488}
489
490static void bootstrap_attr_stack(void)
491{
492 struct attr_stack *elem;
493
494 if (attr_stack)
495 return;
496
497 elem = read_attr_from_array(builtin_attr);
498 elem->origin = NULL;
499 elem->prev = attr_stack;
500 attr_stack = elem;
501
502 if (git_attr_system()) {
503 elem = read_attr_from_file(git_etc_gitattributes(), 1);
504 if (elem) {
505 elem->origin = NULL;
506 elem->prev = attr_stack;
507 attr_stack = elem;
508 }
509 }
510
511 git_config(git_attr_config, NULL);
512 if (attributes_file) {
513 elem = read_attr_from_file(attributes_file, 1);
514 if (elem) {
515 elem->origin = NULL;
516 elem->prev = attr_stack;
517 attr_stack = elem;
518 }
519 }
520
521 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
522 elem = read_attr(GITATTRIBUTES_FILE, 1);
523 elem->origin = strdup("");
524 elem->prev = attr_stack;
525 attr_stack = elem;
526 debug_push(elem);
527 }
528
529 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
530 if (!elem)
531 elem = xcalloc(1, sizeof(*elem));
532 elem->origin = NULL;
533 elem->prev = attr_stack;
534 attr_stack = elem;
535}
536
537static void prepare_attr_stack(const char *path, int dirlen)
538{
539 struct attr_stack *elem, *info;
540 int len;
541 struct strbuf pathbuf;
542
543 strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
544
545 /*
546 * At the bottom of the attribute stack is the built-in
547 * set of attribute definitions, followed by the contents
548 * of $(prefix)/etc/gitattributes and a file specified by
549 * core.attributesfile. Then, contents from
550 * .gitattribute files from directories closer to the
551 * root to the ones in deeper directories are pushed
552 * to the stack. Finally, at the very top of the stack
553 * we always keep the contents of $GIT_DIR/info/attributes.
554 *
555 * When checking, we use entries from near the top of the
556 * stack, preferring $GIT_DIR/info/attributes, then
557 * .gitattributes in deeper directories to shallower ones,
558 * and finally use the built-in set as the default.
559 */
560 if (!attr_stack)
561 bootstrap_attr_stack();
562
563 /*
564 * Pop the "info" one that is always at the top of the stack.
565 */
566 info = attr_stack;
567 attr_stack = info->prev;
568
569 /*
570 * Pop the ones from directories that are not the prefix of
571 * the path we are checking. Break out of the loop when we see
572 * the root one (whose origin is an empty string "") or the builtin
573 * one (whose origin is NULL) without popping it.
574 */
575 while (attr_stack->origin) {
576 int namelen = strlen(attr_stack->origin);
577
578 elem = attr_stack;
579 if (namelen <= dirlen &&
580 !strncmp(elem->origin, path, namelen) &&
581 (!namelen || path[namelen] == '/'))
582 break;
583
584 debug_pop(elem);
585 attr_stack = elem->prev;
586 free_attr_elem(elem);
587 }
588
589 /*
590 * Read from parent directories and push them down
591 */
592 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
593 /*
594 * bootstrap_attr_stack() should have added, and the
595 * above loop should have stopped before popping, the
596 * root element whose attr_stack->origin is set to an
597 * empty string.
598 */
599 assert(attr_stack->origin);
600 while (1) {
601 char *cp;
602
603 len = strlen(attr_stack->origin);
604 if (dirlen <= len)
605 break;
606 strbuf_reset(&pathbuf);
607 strbuf_add(&pathbuf, path, dirlen);
608 strbuf_addch(&pathbuf, '/');
609 cp = strchr(pathbuf.buf + len + 1, '/');
610 strcpy(cp + 1, GITATTRIBUTES_FILE);
611 elem = read_attr(pathbuf.buf, 0);
612 *cp = '\0';
613 elem->origin = strdup(pathbuf.buf);
614 elem->prev = attr_stack;
615 attr_stack = elem;
616 debug_push(elem);
617 }
618 }
619
620 strbuf_release(&pathbuf);
621
622 /*
623 * Finally push the "info" one at the top of the stack.
624 */
625 info->prev = attr_stack;
626 attr_stack = info;
627}
628
629static int path_matches(const char *pathname, int pathlen,
630 const char *pattern,
631 const char *base, int baselen)
632{
633 if (!strchr(pattern, '/')) {
634 /* match basename */
635 const char *basename = strrchr(pathname, '/');
636 basename = basename ? basename + 1 : pathname;
637 return (fnmatch(pattern, basename, 0) == 0);
638 }
639 /*
640 * match with FNM_PATHNAME; the pattern has base implicitly
641 * in front of it.
642 */
643 if (*pattern == '/')
644 pattern++;
645 if (pathlen < baselen ||
646 (baselen && pathname[baselen] != '/') ||
647 strncmp(pathname, base, baselen))
648 return 0;
649 if (baselen != 0)
650 baselen++;
651 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
652}
653
654static int macroexpand_one(int attr_nr, int rem);
655
656static int fill_one(const char *what, struct match_attr *a, int rem)
657{
658 struct git_attr_check *check = check_all_attr;
659 int i;
660
661 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
662 struct git_attr *attr = a->state[i].attr;
663 const char **n = &(check[attr->attr_nr].value);
664 const char *v = a->state[i].setto;
665
666 if (*n == ATTR__UNKNOWN) {
667 debug_set(what,
668 a->is_macro ? a->u.attr->name : a->u.pattern,
669 attr, v);
670 *n = v;
671 rem--;
672 rem = macroexpand_one(attr->attr_nr, rem);
673 }
674 }
675 return rem;
676}
677
678static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
679{
680 int i;
681 const char *base = stk->origin ? stk->origin : "";
682
683 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
684 struct match_attr *a = stk->attrs[i];
685 if (a->is_macro)
686 continue;
687 if (path_matches(path, pathlen,
688 a->u.pattern, base, strlen(base)))
689 rem = fill_one("fill", a, rem);
690 }
691 return rem;
692}
693
694static int macroexpand_one(int attr_nr, int rem)
695{
696 struct attr_stack *stk;
697 struct match_attr *a = NULL;
698 int i;
699
700 if (check_all_attr[attr_nr].value != ATTR__TRUE)
701 return rem;
702
703 for (stk = attr_stack; !a && stk; stk = stk->prev)
704 for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
705 struct match_attr *ma = stk->attrs[i];
706 if (!ma->is_macro)
707 continue;
708 if (ma->u.attr->attr_nr == attr_nr)
709 a = ma;
710 }
711
712 if (a)
713 rem = fill_one("expand", a, rem);
714
715 return rem;
716}
717
718int git_checkattr(const char *path, int num, struct git_attr_check *check)
719{
720 struct attr_stack *stk;
721 const char *cp;
722 int dirlen, pathlen, i, rem;
723
724 bootstrap_attr_stack();
725 for (i = 0; i < attr_nr; i++)
726 check_all_attr[i].value = ATTR__UNKNOWN;
727
728 pathlen = strlen(path);
729 cp = strrchr(path, '/');
730 if (!cp)
731 dirlen = 0;
732 else
733 dirlen = cp - path;
734 prepare_attr_stack(path, dirlen);
735 rem = attr_nr;
736 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
737 rem = fill(path, pathlen, stk, rem);
738
739 for (i = 0; i < num; i++) {
740 const char *value = check_all_attr[check[i].attr->attr_nr].value;
741 if (value == ATTR__UNKNOWN)
742 value = ATTR__UNSET;
743 check[i].value = value;
744 }
745
746 return 0;
747}
748
749void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
750{
751 enum git_attr_direction old = direction;
752
753 if (is_bare_repository() && new != GIT_ATTR_INDEX)
754 die("BUG: non-INDEX attr direction in a bare repo");
755
756 direction = new;
757 if (new != old)
758 drop_attr_stack();
759 use_index = istate;
760}