]> git.ipfire.org Git - thirdparty/git.git/blame - attr.c
t2203: fix wrong commit command
[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 /*
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
7fb0eaa2 71static struct git_attr *git_attr_internal(const char *name, int len)
d0bfd026
JH
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
e4aee10a
JH
83 if (invalid_attr_name(name, len))
84 return NULL;
85
d0bfd026
JH
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];
f48fd688 91 a->attr_nr = attr_nr++;
d0bfd026 92 git_attr_hash[pos] = a;
f48fd688
JH
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;
515106fa 97 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
d0bfd026
JH
98 return a;
99}
100
7fb0eaa2
JH
101struct git_attr *git_attr(const char *name)
102{
103 return git_attr_internal(name, strlen(name));
104}
105
d0bfd026
JH
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
515106fa
JH
112 * could be prefixed with '-' to mean "set to false", '!' to mean
113 * "unset".
d0bfd026
JH
114 */
115
515106fa 116/* What does a matched pattern decide? */
d0bfd026 117struct attr_state {
d0bfd026 118 struct git_attr *attr;
a5e92abd 119 const char *setto;
d0bfd026
JH
120};
121
122struct match_attr {
f48fd688
JH
123 union {
124 char *pattern;
125 struct git_attr *attr;
126 } u;
127 char is_macro;
d0bfd026
JH
128 unsigned num_attr;
129 struct attr_state state[FLEX_ARRAY];
130};
131
132static const char blank[] = " \t\r\n";
133
515106fa
JH
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 {
182af834 171 e->setto = xmemdupz(equals + 1, ep - equals - 1);
515106fa 172 }
7fb0eaa2 173 e->attr = git_attr_internal(cp, len);
515106fa
JH
174 }
175 (*num_attr)++;
176 return ep + strspn(ep, blank);
177}
178
f48fd688
JH
179static struct match_attr *parse_attr_line(const char *line, const char *src,
180 int lineno, int macro_ok)
d0bfd026
JH
181{
182 int namelen;
183 int num_attr;
184 const char *cp, *name;
515106fa 185 struct match_attr *res = NULL;
d0bfd026 186 int pass;
f48fd688 187 int is_macro;
d0bfd026
JH
188
189 cp = line + strspn(line, blank);
190 if (!*cp || *cp == '#')
191 return NULL;
192 name = cp;
193 namelen = strcspn(name, blank);
f48fd688
JH
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);
e4aee10a
JH
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 }
f48fd688
JH
211 }
212 else
213 is_macro = 0;
d0bfd026
JH
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);
d7b0a093 220 while (*cp) {
515106fa 221 cp = parse_attr(src, lineno, cp, &num_attr, res);
d7b0a093
SP
222 if (!cp)
223 return NULL;
224 }
d0bfd026
JH
225 if (pass)
226 break;
227 res = xcalloc(1,
228 sizeof(*res) +
229 sizeof(struct attr_state) * num_attr +
f48fd688 230 (is_macro ? 0 : namelen + 1));
515106fa 231 if (is_macro)
7fb0eaa2 232 res->u.attr = git_attr_internal(name, namelen);
f48fd688 233 else {
4b25d091 234 res->u.pattern = (char *)&(res->state[num_attr]);
f48fd688
JH
235 memcpy(res->u.pattern, name, namelen);
236 res->u.pattern[namelen] = 0;
237 }
238 res->is_macro = is_macro;
d0bfd026
JH
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;
515106fa
JH
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.
d0bfd026
JH
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;
a4413118 266 unsigned alloc;
d0bfd026
JH
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);
515106fa
JH
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++) {
a5e92abd 278 const char *setto = a->state[j].setto;
515106fa
JH
279 if (setto == ATTR__TRUE ||
280 setto == ATTR__FALSE ||
281 setto == ATTR__UNSET ||
282 setto == ATTR__UNKNOWN)
283 ;
284 else
4b25d091 285 free((char *) setto);
515106fa
JH
286 }
287 free(a);
288 }
d0bfd026
JH
289 free(e);
290}
291
292static const char *builtin_attr[] = {
5ec3e670 293 "[attr]binary -diff -text",
d0bfd026
JH
294 NULL,
295};
296
a4413118
JH
297static void handle_attr_line(struct attr_stack *res,
298 const char *line,
299 const char *src,
300 int lineno,
301 int macro_ok)
302{
303 struct match_attr *a;
304
305 a = parse_attr_line(line, src, lineno, macro_ok);
306 if (!a)
307 return;
308 if (res->alloc <= res->num_matches) {
309 res->alloc = alloc_nr(res->num_matches);
310 res->attrs = xrealloc(res->attrs,
311 sizeof(struct match_attr *) *
312 res->alloc);
313 }
314 res->attrs[res->num_matches++] = a;
315}
316
d0bfd026
JH
317static struct attr_stack *read_attr_from_array(const char **list)
318{
319 struct attr_stack *res;
320 const char *line;
f48fd688 321 int lineno = 0;
d0bfd026
JH
322
323 res = xcalloc(1, sizeof(*res));
a4413118
JH
324 while ((line = *(list++)) != NULL)
325 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
d0bfd026
JH
326 return res;
327}
328
06f33c17
JH
329static enum git_attr_direction direction;
330static struct index_state *use_index;
331
f48fd688 332static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
d0bfd026 333{
a4413118 334 FILE *fp = fopen(path, "r");
d0bfd026
JH
335 struct attr_stack *res;
336 char buf[2048];
f48fd688 337 int lineno = 0;
d0bfd026 338
d0bfd026 339 if (!fp)
a4413118
JH
340 return NULL;
341 res = xcalloc(1, sizeof(*res));
342 while (fgets(buf, sizeof(buf), fp))
343 handle_attr_line(res, buf, path, ++lineno, macro_ok);
344 fclose(fp);
345 return res;
346}
d0bfd026 347
1a9d7e9b
JH
348static void *read_index_data(const char *path)
349{
350 int pos, len;
351 unsigned long sz;
352 enum object_type type;
353 void *data;
06f33c17 354 struct index_state *istate = use_index ? use_index : &the_index;
1a9d7e9b
JH
355
356 len = strlen(path);
06f33c17 357 pos = index_name_pos(istate, path, len);
1a9d7e9b
JH
358 if (pos < 0) {
359 /*
360 * We might be in the middle of a merge, in which
361 * case we would read stage #2 (ours).
362 */
363 int i;
364 for (i = -pos - 1;
06f33c17
JH
365 (pos < 0 && i < istate->cache_nr &&
366 !strcmp(istate->cache[i]->name, path));
1a9d7e9b 367 i++)
06f33c17 368 if (ce_stage(istate->cache[i]) == 2)
1a9d7e9b
JH
369 pos = i;
370 }
371 if (pos < 0)
372 return NULL;
06f33c17 373 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
1a9d7e9b
JH
374 if (!data || type != OBJ_BLOB) {
375 free(data);
376 return NULL;
377 }
378 return data;
379}
380
06f33c17 381static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
a4413118
JH
382{
383 struct attr_stack *res;
1a9d7e9b
JH
384 char *buf, *sp;
385 int lineno = 0;
f48fd688 386
1a9d7e9b
JH
387 buf = read_index_data(path);
388 if (!buf)
06f33c17 389 return NULL;
1a9d7e9b 390
06f33c17 391 res = xcalloc(1, sizeof(*res));
1a9d7e9b
JH
392 for (sp = buf; *sp; ) {
393 char *ep;
394 int more;
395 for (ep = sp; *ep && *ep != '\n'; ep++)
396 ;
397 more = (*ep == '\n');
398 *ep = '\0';
399 handle_attr_line(res, sp, path, ++lineno, macro_ok);
400 sp = ep + more;
401 }
402 free(buf);
d0bfd026
JH
403 return res;
404}
405
06f33c17
JH
406static struct attr_stack *read_attr(const char *path, int macro_ok)
407{
408 struct attr_stack *res;
409
410 if (direction == GIT_ATTR_CHECKOUT) {
411 res = read_attr_from_index(path, macro_ok);
412 if (!res)
413 res = read_attr_from_file(path, macro_ok);
414 }
4191e80a 415 else if (direction == GIT_ATTR_CHECKIN) {
06f33c17
JH
416 res = read_attr_from_file(path, macro_ok);
417 if (!res)
418 /*
419 * There is no checked out .gitattributes file there, but
420 * we might have it in the index. We allow operation in a
421 * sparsely checked out work tree, so read from it.
422 */
423 res = read_attr_from_index(path, macro_ok);
424 }
4191e80a
NTND
425 else
426 res = read_attr_from_index(path, macro_ok);
06f33c17
JH
427 if (!res)
428 res = xcalloc(1, sizeof(*res));
429 return res;
430}
431
d0bfd026
JH
432#if DEBUG_ATTR
433static void debug_info(const char *what, struct attr_stack *elem)
434{
435 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
436}
cf94ccda 437static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
f48fd688 438{
515106fa
JH
439 const char *value = v;
440
441 if (ATTR_TRUE(value))
442 value = "set";
443 else if (ATTR_FALSE(value))
444 value = "unset";
445 else if (ATTR_UNSET(value))
446 value = "unspecified";
447
448 fprintf(stderr, "%s: %s => %s (%s)\n",
449 what, attr->name, (char *) value, match);
f48fd688 450}
d0bfd026
JH
451#define debug_push(a) debug_info("push", (a))
452#define debug_pop(a) debug_info("pop", (a))
453#else
454#define debug_push(a) do { ; } while (0)
455#define debug_pop(a) do { ; } while (0)
f48fd688 456#define debug_set(a,b,c,d) do { ; } while (0)
d0bfd026
JH
457#endif
458
06f33c17
JH
459static void drop_attr_stack(void)
460{
461 while (attr_stack) {
462 struct attr_stack *elem = attr_stack;
463 attr_stack = elem->prev;
464 free_attr_elem(elem);
465 }
466}
467
c5147722 468static const char *git_etc_gitattributes(void)
6df42ab9
PO
469{
470 static const char *system_wide;
471 if (!system_wide)
472 system_wide = system_path(ETC_GITATTRIBUTES);
473 return system_wide;
474}
475
c5147722 476static int git_attr_system(void)
6df42ab9
PO
477{
478 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
479}
480
6df42ab9
PO
481static int git_attr_config(const char *var, const char *value, void *dummy)
482{
483 if (!strcmp(var, "core.attributesfile"))
484 return git_config_pathname(&attributes_file, var, value);
485
486 return 0;
487}
488
f48fd688
JH
489static void bootstrap_attr_stack(void)
490{
909ca7b9 491 struct attr_stack *elem;
f48fd688 492
909ca7b9
JH
493 if (attr_stack)
494 return;
f48fd688 495
909ca7b9
JH
496 elem = read_attr_from_array(builtin_attr);
497 elem->origin = NULL;
498 elem->prev = attr_stack;
499 attr_stack = elem;
6df42ab9 500
909ca7b9
JH
501 if (git_attr_system()) {
502 elem = read_attr_from_file(git_etc_gitattributes(), 1);
503 if (elem) {
504 elem->origin = NULL;
505 elem->prev = attr_stack;
506 attr_stack = elem;
6df42ab9 507 }
909ca7b9 508 }
6df42ab9 509
909ca7b9
JH
510 git_config(git_attr_config, NULL);
511 if (attributes_file) {
512 elem = read_attr_from_file(attributes_file, 1);
513 if (elem) {
514 elem->origin = NULL;
2d35d556
RS
515 elem->prev = attr_stack;
516 attr_stack = elem;
2d35d556 517 }
909ca7b9 518 }
f48fd688 519
909ca7b9
JH
520 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
521 elem = read_attr(GITATTRIBUTES_FILE, 1);
522 elem->origin = strdup("");
f48fd688
JH
523 elem->prev = attr_stack;
524 attr_stack = elem;
909ca7b9 525 debug_push(elem);
f48fd688 526 }
909ca7b9
JH
527
528 elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
529 if (!elem)
530 elem = xcalloc(1, sizeof(*elem));
531 elem->origin = NULL;
532 elem->prev = attr_stack;
533 attr_stack = elem;
f48fd688
JH
534}
535
d0bfd026
JH
536static void prepare_attr_stack(const char *path, int dirlen)
537{
538 struct attr_stack *elem, *info;
539 int len;
f66cf96d
DP
540 struct strbuf pathbuf;
541
542 strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
d0bfd026
JH
543
544 /*
545 * At the bottom of the attribute stack is the built-in
6df42ab9
PO
546 * set of attribute definitions, followed by the contents
547 * of $(prefix)/etc/gitattributes and a file specified by
548 * core.attributesfile. Then, contents from
d0bfd026
JH
549 * .gitattribute files from directories closer to the
550 * root to the ones in deeper directories are pushed
551 * to the stack. Finally, at the very top of the stack
552 * we always keep the contents of $GIT_DIR/info/attributes.
553 *
554 * When checking, we use entries from near the top of the
555 * stack, preferring $GIT_DIR/info/attributes, then
556 * .gitattributes in deeper directories to shallower ones,
557 * and finally use the built-in set as the default.
558 */
f48fd688
JH
559 if (!attr_stack)
560 bootstrap_attr_stack();
d0bfd026
JH
561
562 /*
563 * Pop the "info" one that is always at the top of the stack.
564 */
565 info = attr_stack;
566 attr_stack = info->prev;
567
568 /*
569 * Pop the ones from directories that are not the prefix of
c432ef99
JH
570 * the path we are checking. Break out of the loop when we see
571 * the root one (whose origin is an empty string "") or the builtin
572 * one (whose origin is NULL) without popping it.
d0bfd026 573 */
77f7f822 574 while (attr_stack->origin) {
d0bfd026
JH
575 int namelen = strlen(attr_stack->origin);
576
577 elem = attr_stack;
578 if (namelen <= dirlen &&
1afca444
JK
579 !strncmp(elem->origin, path, namelen) &&
580 (!namelen || path[namelen] == '/'))
d0bfd026
JH
581 break;
582
583 debug_pop(elem);
584 attr_stack = elem->prev;
585 free_attr_elem(elem);
586 }
587
588 /*
589 * Read from parent directories and push them down
590 */
4191e80a 591 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
c432ef99
JH
592 /*
593 * bootstrap_attr_stack() should have added, and the
594 * above loop should have stopped before popping, the
595 * root element whose attr_stack->origin is set to an
596 * empty string.
597 */
598 assert(attr_stack->origin);
2d35d556
RS
599 while (1) {
600 char *cp;
601
602 len = strlen(attr_stack->origin);
603 if (dirlen <= len)
604 break;
f66cf96d
DP
605 strbuf_reset(&pathbuf);
606 strbuf_add(&pathbuf, path, dirlen);
607 strbuf_addch(&pathbuf, '/');
608 cp = strchr(pathbuf.buf + len + 1, '/');
2d35d556 609 strcpy(cp + 1, GITATTRIBUTES_FILE);
f66cf96d 610 elem = read_attr(pathbuf.buf, 0);
2d35d556 611 *cp = '\0';
f66cf96d 612 elem->origin = strdup(pathbuf.buf);
2d35d556
RS
613 elem->prev = attr_stack;
614 attr_stack = elem;
615 debug_push(elem);
616 }
d0bfd026
JH
617 }
618
d4c98565
RS
619 strbuf_release(&pathbuf);
620
d0bfd026
JH
621 /*
622 * Finally push the "info" one at the top of the stack.
623 */
624 info->prev = attr_stack;
625 attr_stack = info;
626}
627
628static int path_matches(const char *pathname, int pathlen,
629 const char *pattern,
630 const char *base, int baselen)
631{
632 if (!strchr(pattern, '/')) {
633 /* match basename */
634 const char *basename = strrchr(pathname, '/');
635 basename = basename ? basename + 1 : pathname;
636 return (fnmatch(pattern, basename, 0) == 0);
637 }
638 /*
639 * match with FNM_PATHNAME; the pattern has base implicitly
640 * in front of it.
641 */
642 if (*pattern == '/')
643 pattern++;
644 if (pathlen < baselen ||
cf94ccda 645 (baselen && pathname[baselen] != '/') ||
d0bfd026
JH
646 strncmp(pathname, base, baselen))
647 return 0;
82881b38
MO
648 if (baselen != 0)
649 baselen++;
650 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
d0bfd026
JH
651}
652
ec775c41
HG
653static int macroexpand_one(int attr_nr, int rem);
654
515106fa
JH
655static int fill_one(const char *what, struct match_attr *a, int rem)
656{
657 struct git_attr_check *check = check_all_attr;
658 int i;
659
969f9d73 660 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
515106fa 661 struct git_attr *attr = a->state[i].attr;
a5e92abd
JH
662 const char **n = &(check[attr->attr_nr].value);
663 const char *v = a->state[i].setto;
515106fa
JH
664
665 if (*n == ATTR__UNKNOWN) {
426c27b7
HG
666 debug_set(what,
667 a->is_macro ? a->u.attr->name : a->u.pattern,
668 attr, v);
515106fa
JH
669 *n = v;
670 rem--;
ec775c41 671 rem = macroexpand_one(attr->attr_nr, rem);
515106fa
JH
672 }
673 }
674 return rem;
675}
676
f48fd688 677static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
d0bfd026 678{
515106fa 679 int i;
d0bfd026
JH
680 const char *base = stk->origin ? stk->origin : "";
681
682 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
683 struct match_attr *a = stk->attrs[i];
f48fd688
JH
684 if (a->is_macro)
685 continue;
d0bfd026 686 if (path_matches(path, pathlen,
515106fa
JH
687 a->u.pattern, base, strlen(base)))
688 rem = fill_one("fill", a, rem);
d0bfd026
JH
689 }
690 return rem;
691}
692
ec775c41 693static int macroexpand_one(int attr_nr, int rem)
f48fd688 694{
ec775c41
HG
695 struct attr_stack *stk;
696 struct match_attr *a = NULL;
515106fa 697 int i;
f48fd688 698
ec775c41
HG
699 if (check_all_attr[attr_nr].value != ATTR__TRUE)
700 return rem;
701
702 for (stk = attr_stack; !a && stk; stk = stk->prev)
703 for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
704 struct match_attr *ma = stk->attrs[i];
705 if (!ma->is_macro)
706 continue;
707 if (ma->u.attr->attr_nr == attr_nr)
708 a = ma;
709 }
710
711 if (a)
515106fa 712 rem = fill_one("expand", a, rem);
ec775c41 713
f48fd688
JH
714 return rem;
715}
716
d0bfd026
JH
717int git_checkattr(const char *path, int num, struct git_attr_check *check)
718{
719 struct attr_stack *stk;
720 const char *cp;
721 int dirlen, pathlen, i, rem;
722
f48fd688
JH
723 bootstrap_attr_stack();
724 for (i = 0; i < attr_nr; i++)
515106fa 725 check_all_attr[i].value = ATTR__UNKNOWN;
d0bfd026
JH
726
727 pathlen = strlen(path);
728 cp = strrchr(path, '/');
729 if (!cp)
730 dirlen = 0;
731 else
732 dirlen = cp - path;
733 prepare_attr_stack(path, dirlen);
f48fd688
JH
734 rem = attr_nr;
735 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
736 rem = fill(path, pathlen, stk, rem);
737
515106fa 738 for (i = 0; i < num; i++) {
a5e92abd 739 const char *value = check_all_attr[check[i].attr->attr_nr].value;
515106fa
JH
740 if (value == ATTR__UNKNOWN)
741 value = ATTR__UNSET;
742 check[i].value = value;
743 }
f48fd688 744
d0bfd026
JH
745 return 0;
746}
06f33c17
JH
747
748void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
749{
750 enum git_attr_direction old = direction;
4191e80a
NTND
751
752 if (is_bare_repository() && new != GIT_ATTR_INDEX)
753 die("BUG: non-INDEX attr direction in a bare repo");
754
06f33c17
JH
755 direction = new;
756 if (new != old)
757 drop_attr_stack();
758 use_index = istate;
759}