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