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