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