]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - attr.c
Documentation: fix a typo
[thirdparty/git.git] / attr.c
... / ...
CommitLineData
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 push_stack(struct attr_stack **attr_stack_p,
514 struct attr_stack *elem, char *origin, size_t originlen)
515{
516 if (elem) {
517 elem->origin = origin;
518 if (origin)
519 elem->originlen = originlen;
520 elem->prev = *attr_stack_p;
521 *attr_stack_p = elem;
522 }
523}
524
525static void bootstrap_attr_stack(void)
526{
527 struct attr_stack *elem;
528
529 if (attr_stack)
530 return;
531
532 push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0);
533
534 if (git_attr_system())
535 push_stack(&attr_stack,
536 read_attr_from_file(git_etc_gitattributes(), 1),
537 NULL, 0);
538
539 if (!git_attributes_file)
540 git_attributes_file = xdg_config_home("attributes");
541 if (git_attributes_file)
542 push_stack(&attr_stack,
543 read_attr_from_file(git_attributes_file, 1),
544 NULL, 0);
545
546 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
547 elem = read_attr(GITATTRIBUTES_FILE, 1);
548 push_stack(&attr_stack, elem, xstrdup(""), 0);
549 debug_push(elem);
550 }
551
552 if (startup_info->have_repository)
553 elem = read_attr_from_file(git_path_info_attributes(), 1);
554 else
555 elem = NULL;
556
557 if (!elem)
558 elem = xcalloc(1, sizeof(*elem));
559 push_stack(&attr_stack, elem, NULL, 0);
560}
561
562static void prepare_attr_stack(const char *path, int dirlen)
563{
564 struct attr_stack *elem, *info;
565 const char *cp;
566
567 /*
568 * At the bottom of the attribute stack is the built-in
569 * set of attribute definitions, followed by the contents
570 * of $(prefix)/etc/gitattributes and a file specified by
571 * core.attributesfile. Then, contents from
572 * .gitattribute files from directories closer to the
573 * root to the ones in deeper directories are pushed
574 * to the stack. Finally, at the very top of the stack
575 * we always keep the contents of $GIT_DIR/info/attributes.
576 *
577 * When checking, we use entries from near the top of the
578 * stack, preferring $GIT_DIR/info/attributes, then
579 * .gitattributes in deeper directories to shallower ones,
580 * and finally use the built-in set as the default.
581 */
582 bootstrap_attr_stack();
583
584 /*
585 * Pop the "info" one that is always at the top of the stack.
586 */
587 info = attr_stack;
588 attr_stack = info->prev;
589
590 /*
591 * Pop the ones from directories that are not the prefix of
592 * the path we are checking. Break out of the loop when we see
593 * the root one (whose origin is an empty string "") or the builtin
594 * one (whose origin is NULL) without popping it.
595 */
596 while (attr_stack->origin) {
597 int namelen = strlen(attr_stack->origin);
598
599 elem = attr_stack;
600 if (namelen <= dirlen &&
601 !strncmp(elem->origin, path, namelen) &&
602 (!namelen || path[namelen] == '/'))
603 break;
604
605 debug_pop(elem);
606 attr_stack = elem->prev;
607 free_attr_elem(elem);
608 }
609
610 /*
611 * Read from parent directories and push them down
612 */
613 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
614 /*
615 * bootstrap_attr_stack() should have added, and the
616 * above loop should have stopped before popping, the
617 * root element whose attr_stack->origin is set to an
618 * empty string.
619 */
620 struct strbuf pathbuf = STRBUF_INIT;
621
622 assert(attr_stack->origin);
623 while (1) {
624 size_t len = strlen(attr_stack->origin);
625 char *origin;
626
627 if (dirlen <= len)
628 break;
629 cp = memchr(path + len + 1, '/', dirlen - len - 1);
630 if (!cp)
631 cp = path + dirlen;
632 strbuf_addf(&pathbuf,
633 "%.*s/%s", (int)(cp - path), path,
634 GITATTRIBUTES_FILE);
635 elem = read_attr(pathbuf.buf, 0);
636 strbuf_setlen(&pathbuf, cp - path);
637 origin = strbuf_detach(&pathbuf, &len);
638 push_stack(&attr_stack, elem, origin, len);
639 debug_push(elem);
640 }
641
642 strbuf_release(&pathbuf);
643 }
644
645 /*
646 * Finally push the "info" one at the top of the stack.
647 */
648 push_stack(&attr_stack, info, NULL, 0);
649}
650
651static int path_matches(const char *pathname, int pathlen,
652 int basename_offset,
653 const struct pattern *pat,
654 const char *base, int baselen)
655{
656 const char *pattern = pat->pattern;
657 int prefix = pat->nowildcardlen;
658 int isdir = (pathlen && pathname[pathlen - 1] == '/');
659
660 if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
661 return 0;
662
663 if (pat->flags & EXC_FLAG_NODIR) {
664 return match_basename(pathname + basename_offset,
665 pathlen - basename_offset - isdir,
666 pattern, prefix,
667 pat->patternlen, pat->flags);
668 }
669 return match_pathname(pathname, pathlen - isdir,
670 base, baselen,
671 pattern, prefix, pat->patternlen, pat->flags);
672}
673
674static int macroexpand_one(int attr_nr, int rem);
675
676static int fill_one(const char *what, struct match_attr *a, int rem)
677{
678 struct git_attr_check *check = check_all_attr;
679 int i;
680
681 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
682 struct git_attr *attr = a->state[i].attr;
683 const char **n = &(check[attr->attr_nr].value);
684 const char *v = a->state[i].setto;
685
686 if (*n == ATTR__UNKNOWN) {
687 debug_set(what,
688 a->is_macro ? a->u.attr->name : a->u.pat.pattern,
689 attr, v);
690 *n = v;
691 rem--;
692 rem = macroexpand_one(attr->attr_nr, rem);
693 }
694 }
695 return rem;
696}
697
698static int fill(const char *path, int pathlen, int basename_offset,
699 struct attr_stack *stk, int rem)
700{
701 int i;
702 const char *base = stk->origin ? stk->origin : "";
703
704 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
705 struct match_attr *a = stk->attrs[i];
706 if (a->is_macro)
707 continue;
708 if (path_matches(path, pathlen, basename_offset,
709 &a->u.pat, base, stk->originlen))
710 rem = fill_one("fill", a, rem);
711 }
712 return rem;
713}
714
715static int macroexpand_one(int nr, int rem)
716{
717 struct attr_stack *stk;
718 int i;
719
720 if (check_all_attr[nr].value != ATTR__TRUE ||
721 !check_all_attr[nr].attr->maybe_macro)
722 return rem;
723
724 for (stk = attr_stack; stk; stk = stk->prev) {
725 for (i = stk->num_matches - 1; 0 <= i; i--) {
726 struct match_attr *ma = stk->attrs[i];
727 if (!ma->is_macro)
728 continue;
729 if (ma->u.attr->attr_nr == nr)
730 return fill_one("expand", ma, rem);
731 }
732 }
733
734 return rem;
735}
736
737/*
738 * Collect attributes for path into the array pointed to by
739 * check_all_attr. If num is non-zero, only attributes in check[] are
740 * collected. Otherwise all attributes are collected.
741 */
742static void collect_some_attrs(const char *path, int num,
743 struct git_attr_check *check)
744
745{
746 struct attr_stack *stk;
747 int i, pathlen, rem, dirlen;
748 const char *cp, *last_slash = NULL;
749 int basename_offset;
750
751 for (cp = path; *cp; cp++) {
752 if (*cp == '/' && cp[1])
753 last_slash = cp;
754 }
755 pathlen = cp - path;
756 if (last_slash) {
757 basename_offset = last_slash + 1 - path;
758 dirlen = last_slash - path;
759 } else {
760 basename_offset = 0;
761 dirlen = 0;
762 }
763
764 prepare_attr_stack(path, dirlen);
765 for (i = 0; i < attr_nr; i++)
766 check_all_attr[i].value = ATTR__UNKNOWN;
767 if (num && !cannot_trust_maybe_real) {
768 rem = 0;
769 for (i = 0; i < num; i++) {
770 if (!check[i].attr->maybe_real) {
771 struct git_attr_check *c;
772 c = check_all_attr + check[i].attr->attr_nr;
773 c->value = ATTR__UNSET;
774 rem++;
775 }
776 }
777 if (rem == num)
778 return;
779 }
780
781 rem = attr_nr;
782 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
783 rem = fill(path, pathlen, basename_offset, stk, rem);
784}
785
786int git_check_attr(const char *path, int num, struct git_attr_check *check)
787{
788 int i;
789
790 collect_some_attrs(path, num, check);
791
792 for (i = 0; i < num; i++) {
793 const char *value = check_all_attr[check[i].attr->attr_nr].value;
794 if (value == ATTR__UNKNOWN)
795 value = ATTR__UNSET;
796 check[i].value = value;
797 }
798
799 return 0;
800}
801
802int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
803{
804 int i, count, j;
805
806 collect_some_attrs(path, 0, NULL);
807
808 /* Count the number of attributes that are set. */
809 count = 0;
810 for (i = 0; i < attr_nr; i++) {
811 const char *value = check_all_attr[i].value;
812 if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
813 ++count;
814 }
815 *num = count;
816 ALLOC_ARRAY(*check, count);
817 j = 0;
818 for (i = 0; i < attr_nr; i++) {
819 const char *value = check_all_attr[i].value;
820 if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
821 (*check)[j].attr = check_all_attr[i].attr;
822 (*check)[j].value = value;
823 ++j;
824 }
825 }
826
827 return 0;
828}
829
830void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
831{
832 enum git_attr_direction old = direction;
833
834 if (is_bare_repository() && new != GIT_ATTR_INDEX)
835 die("BUG: non-INDEX attr direction in a bare repo");
836
837 direction = new;
838 if (new != old)
839 drop_attr_stack();
840 use_index = istate;
841}