attr: support quoting pathname patterns in C style
[git/git.git] / attr.c
CommitLineData
86ab7f0c
MH
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
06f33c17 10#define NO_THE_INDEX_COMPATIBILITY_MACROS
d0bfd026 11#include "cache.h"
6df42ab9 12#include "exec_cmd.h"
d0bfd026 13#include "attr.h"
6eba6210 14#include "dir.h"
27547e5f 15#include "utf8.h"
860a74d9 16#include "quote.h"
d0bfd026 17
a5e92abd
JH
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
515106fa 25
86ab7f0c 26/* This is a randomly chosen prime. */
d0bfd026
JH
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;
f48fd688 36 int attr_nr;
fad32bcd 37 int maybe_macro;
06a604e6 38 int maybe_real;
d0bfd026
JH
39 char name[FLEX_ARRAY];
40};
f48fd688 41static int attr_nr;
06a604e6 42static int cannot_trust_maybe_real;
d0bfd026 43
f48fd688 44static struct git_attr_check *check_all_attr;
d0bfd026
JH
45static struct git_attr *(git_attr_hash[HASHSIZE]);
46
ec4d77aa 47const char *git_attr_name(const struct git_attr *attr)
352404ac
MH
48{
49 return attr->name;
50}
51
d0bfd026
JH
52static unsigned hash_name(const char *name, int namelen)
53{
48fb7deb 54 unsigned val = 0, c;
d0bfd026
JH
55
56 while (namelen--) {
57 c = *name++;
58 val = ((val << 7) | (val >> 22)) ^ c;
59 }
60 return val;
61}
62
e4aee10a
JH
63static int invalid_attr_name(const char *name, int namelen)
64{
65 /*
d42453ab
MH
66 * Attribute name cannot begin with '-' and must consist of
67 * characters from [-A-Za-z0-9_.].
e4aee10a 68 */
c0b13b21 69 if (namelen <= 0 || *name == '-')
e4aee10a
JH
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
7fb0eaa2 82static struct git_attr *git_attr_internal(const char *name, int len)
d0bfd026
JH
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
e4aee10a
JH
94 if (invalid_attr_name(name, len))
95 return NULL;
96
96ffc06f 97 FLEX_ALLOC_MEM(a, name, name, len);
d0bfd026
JH
98 a->h = hval;
99 a->next = git_attr_hash[pos];
f48fd688 100 a->attr_nr = attr_nr++;
fad32bcd 101 a->maybe_macro = 0;
06a604e6 102 a->maybe_real = 0;
d0bfd026 103 git_attr_hash[pos] = a;
f48fd688 104
2756ca43 105 REALLOC_ARRAY(check_all_attr, attr_nr);
f48fd688 106 check_all_attr[a->attr_nr].attr = a;
515106fa 107 check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
d0bfd026
JH
108 return a;
109}
110
7fb0eaa2
JH
111struct git_attr *git_attr(const char *name)
112{
113 return git_attr_internal(name, strlen(name));
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
82dce998
NTND
122struct pattern {
123 const char *pattern;
124 int patternlen;
125 int nowildcardlen;
f8708998 126 unsigned flags; /* EXC_FLAG_* */
82dce998
NTND
127};
128
ba845b75
MH
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 *
4894f4ff
JH
135 * If is_macro is false, then u.pat is the filename pattern to which the
136 * rule applies.
ba845b75
MH
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 */
d0bfd026 142struct match_attr {
f48fd688 143 union {
82dce998 144 struct pattern pat;
f48fd688
JH
145 struct git_attr *attr;
146 } u;
147 char is_macro;
d0bfd026
JH
148 unsigned num_attr;
149 struct attr_state state[FLEX_ARRAY];
150};
151
152static const char blank[] = " \t\r\n";
153
d1751298
MH
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 */
515106fa 161static const char *parse_attr(const char *src, int lineno, const char *cp,
d1751298 162 struct attr_state *e)
515106fa
JH
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;
d1751298 175 if (!e) {
515106fa
JH
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 {
5a884019
JH
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 */
515106fa
JH
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 {
182af834 201 e->setto = xmemdupz(equals + 1, ep - equals - 1);
515106fa 202 }
7fb0eaa2 203 e->attr = git_attr_internal(cp, len);
515106fa 204 }
515106fa
JH
205 return ep + strspn(ep, blank);
206}
207
f48fd688
JH
208static struct match_attr *parse_attr_line(const char *line, const char *src,
209 int lineno, int macro_ok)
d0bfd026
JH
210{
211 int namelen;
d68e1c18 212 int num_attr, i;
85c4a0d0 213 const char *cp, *name, *states;
515106fa 214 struct match_attr *res = NULL;
f48fd688 215 int is_macro;
860a74d9 216 struct strbuf pattern = STRBUF_INIT;
d0bfd026
JH
217
218 cp = line + strspn(line, blank);
219 if (!*cp || *cp == '#')
220 return NULL;
221 name = cp;
860a74d9
NTND
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
f48fd688 231 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
59556548 232 starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
f48fd688
JH
233 if (!macro_ok) {
234 fprintf(stderr, "%s not allowed: %s:%d\n",
235 name, src, lineno);
62af8969 236 goto fail_return;
f48fd688
JH
237 }
238 is_macro = 1;
239 name += strlen(ATTRIBUTE_MACRO_PREFIX);
240 name += strspn(name, blank);
241 namelen = strcspn(name, blank);
e4aee10a
JH
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);
62af8969 246 goto fail_return;
e4aee10a 247 }
f48fd688
JH
248 }
249 else
250 is_macro = 0;
d0bfd026 251
85c4a0d0
MH
252 states += strspn(states, blank);
253
d68e1c18
MH
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)
62af8969 258 goto fail_return;
d0bfd026 259 }
d68e1c18
MH
260
261 res = xcalloc(1,
262 sizeof(*res) +
263 sizeof(struct attr_state) * num_attr +
264 (is_macro ? 0 : namelen + 1));
fad32bcd 265 if (is_macro) {
d68e1c18 266 res->u.attr = git_attr_internal(name, namelen);
fad32bcd
NTND
267 res->u.attr->maybe_macro = 1;
268 } else {
82dce998
NTND
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);
8b1bd024
TR
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."));
62af8969 279 goto fail_return;
8b1bd024 280 }
d0bfd026 281 }
d68e1c18
MH
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]));
06a604e6
NTND
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;
d68e1c18
MH
292 }
293
860a74d9 294 strbuf_release(&pattern);
d0bfd026 295 return res;
62af8969
JH
296
297fail_return:
860a74d9 298 strbuf_release(&pattern);
62af8969
JH
299 free(res);
300 return NULL;
d0bfd026
JH
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;
515106fa
JH
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.
d0bfd026
JH
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.
6d24e7a8 317 * This is exactly the same as what is_excluded() does in dir.c to deal with
6f416cba 318 * .gitignore file and info/excludes file as a fallback.
d0bfd026
JH
319 */
320
321static struct attr_stack {
322 struct attr_stack *prev;
323 char *origin;
cd6a0b26 324 size_t originlen;
d0bfd026 325 unsigned num_matches;
a4413118 326 unsigned alloc;
d0bfd026
JH
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);
515106fa
JH
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++) {
a5e92abd 338 const char *setto = a->state[j].setto;
515106fa
JH
339 if (setto == ATTR__TRUE ||
340 setto == ATTR__FALSE ||
341 setto == ATTR__UNSET ||
342 setto == ATTR__UNKNOWN)
343 ;
344 else
4b25d091 345 free((char *) setto);
515106fa
JH
346 }
347 free(a);
348 }
37475f97 349 free(e->attrs);
d0bfd026
JH
350 free(e);
351}
352
353static const char *builtin_attr[] = {
155a4b71 354 "[attr]binary -diff -merge -text",
d0bfd026
JH
355 NULL,
356};
357
a4413118
JH
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;
3a7fa03d 369 ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
a4413118
JH
370 res->attrs[res->num_matches++] = a;
371}
372
d0bfd026
JH
373static struct attr_stack *read_attr_from_array(const char **list)
374{
375 struct attr_stack *res;
376 const char *line;
f48fd688 377 int lineno = 0;
d0bfd026
JH
378
379 res = xcalloc(1, sizeof(*res));
a4413118
JH
380 while ((line = *(list++)) != NULL)
381 handle_attr_line(res, line, "[builtin]", ++lineno, 1);
d0bfd026
JH
382 return res;
383}
384
06f33c17
JH
385static enum git_attr_direction direction;
386static struct index_state *use_index;
387
f48fd688 388static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
d0bfd026 389{
a4413118 390 FILE *fp = fopen(path, "r");
d0bfd026
JH
391 struct attr_stack *res;
392 char buf[2048];
f48fd688 393 int lineno = 0;
d0bfd026 394
11e50b27 395 if (!fp) {
8e950dab 396 if (errno != ENOENT && errno != ENOTDIR)
55b38a48 397 warn_on_inaccessible(path);
a4413118 398 return NULL;
11e50b27 399 }
a4413118 400 res = xcalloc(1, sizeof(*res));
27547e5f
JH
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 }
a4413118
JH
407 fclose(fp);
408 return res;
409}
d0bfd026 410
06f33c17 411static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
a4413118
JH
412{
413 struct attr_stack *res;
1a9d7e9b
JH
414 char *buf, *sp;
415 int lineno = 0;
f48fd688 416
ff366825 417 buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL);
1a9d7e9b 418 if (!buf)
06f33c17 419 return NULL;
1a9d7e9b 420
06f33c17 421 res = xcalloc(1, sizeof(*res));
1a9d7e9b
JH
422 for (sp = buf; *sp; ) {
423 char *ep;
424 int more;
263434f2
JH
425
426 ep = strchrnul(sp, '\n');
1a9d7e9b
JH
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);
d0bfd026
JH
433 return res;
434}
435
06f33c17
JH
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 }
4191e80a 445 else if (direction == GIT_ATTR_CHECKIN) {
06f33c17
JH
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 }
4191e80a
NTND
455 else
456 res = read_attr_from_index(path, macro_ok);
06f33c17
JH
457 if (!res)
458 res = xcalloc(1, sizeof(*res));
459 return res;
460}
461
d0bfd026
JH
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}
cf94ccda 467static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
f48fd688 468{
515106fa
JH
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);
f48fd688 480}
d0bfd026
JH
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)
f48fd688 486#define debug_set(a,b,c,d) do { ; } while (0)
034d35c9 487#endif /* DEBUG_ATTR */
d0bfd026 488
06f33c17
JH
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
c5147722 498static const char *git_etc_gitattributes(void)
6df42ab9
PO
499{
500 static const char *system_wide;
501 if (!system_wide)
502 system_wide = system_path(ETC_GITATTRIBUTES);
503 return system_wide;
504}
505
c5147722 506static int git_attr_system(void)
6df42ab9
PO
507{
508 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
509}
510
f932729c
JK
511static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
512
f48fd688
JH
513static void bootstrap_attr_stack(void)
514{
909ca7b9 515 struct attr_stack *elem;
f48fd688 516
909ca7b9
JH
517 if (attr_stack)
518 return;
f48fd688 519
909ca7b9
JH
520 elem = read_attr_from_array(builtin_attr);
521 elem->origin = NULL;
522 elem->prev = attr_stack;
523 attr_stack = elem;
6df42ab9 524
909ca7b9
JH
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;
6df42ab9 531 }
909ca7b9 532 }
6df42ab9 533
2527bbce
PT
534 if (!git_attributes_file)
535 git_attributes_file = xdg_config_home("attributes");
f0c1c15c
JK
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 }
909ca7b9 543 }
f48fd688 544
909ca7b9
JH
545 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
546 elem = read_attr(GITATTRIBUTES_FILE, 1);
6c65b5ea 547 elem->origin = xstrdup("");
cd6a0b26 548 elem->originlen = 0;
f48fd688
JH
549 elem->prev = attr_stack;
550 attr_stack = elem;
909ca7b9 551 debug_push(elem);
f48fd688 552 }
909ca7b9 553
f0056f64
JK
554 if (startup_info->have_repository)
555 elem = read_attr_from_file(git_path_info_attributes(), 1);
556 else
557 elem = NULL;
558
909ca7b9
JH
559 if (!elem)
560 elem = xcalloc(1, sizeof(*elem));
561 elem->origin = NULL;
562 elem->prev = attr_stack;
563 attr_stack = elem;
f48fd688
JH
564}
565
9db9eecf 566static void prepare_attr_stack(const char *path, int dirlen)
d0bfd026
JH
567{
568 struct attr_stack *elem, *info;
9db9eecf 569 int len;
a8727017
MH
570 const char *cp;
571
d0bfd026
JH
572 /*
573 * At the bottom of the attribute stack is the built-in
6df42ab9
PO
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
d0bfd026
JH
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 */
7373eab4 587 bootstrap_attr_stack();
d0bfd026
JH
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
c432ef99
JH
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.
d0bfd026 600 */
77f7f822 601 while (attr_stack->origin) {
d0bfd026
JH
602 int namelen = strlen(attr_stack->origin);
603
604 elem = attr_stack;
605 if (namelen <= dirlen &&
1afca444
JK
606 !strncmp(elem->origin, path, namelen) &&
607 (!namelen || path[namelen] == '/'))
d0bfd026
JH
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 */
4191e80a 618 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
c432ef99
JH
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 */
97410b27 625 struct strbuf pathbuf = STRBUF_INIT;
2d35d556 626
c432ef99 627 assert(attr_stack->origin);
97410b27 628 while (1) {
2d35d556
RS
629 len = strlen(attr_stack->origin);
630 if (dirlen <= len)
631 break;
97410b27
BC
632 cp = memchr(path + len + 1, '/', dirlen - len - 1);
633 if (!cp)
634 cp = path + dirlen;
635 strbuf_add(&pathbuf, path, cp - path);
f66cf96d 636 strbuf_addch(&pathbuf, '/');
97410b27 637 strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
f66cf96d 638 elem = read_attr(pathbuf.buf, 0);
97410b27 639 strbuf_setlen(&pathbuf, cp - path);
cd6a0b26 640 elem->origin = strbuf_detach(&pathbuf, &elem->originlen);
2d35d556
RS
641 elem->prev = attr_stack;
642 attr_stack = elem;
643 debug_push(elem);
644 }
d0bfd026 645
97410b27
BC
646 strbuf_release(&pathbuf);
647 }
d4c98565 648
d0bfd026
JH
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,
bd2f371d 657 int basename_offset,
82dce998 658 const struct pattern *pat,
d0bfd026
JH
659 const char *base, int baselen)
660{
82dce998
NTND
661 const char *pattern = pat->pattern;
662 int prefix = pat->nowildcardlen;
dc09e9ec 663 int isdir = (pathlen && pathname[pathlen - 1] == '/');
82dce998 664
dc09e9ec 665 if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
94bc671a
JNA
666 return 0;
667
82dce998 668 if (pat->flags & EXC_FLAG_NODIR) {
bd2f371d 669 return match_basename(pathname + basename_offset,
dc09e9ec 670 pathlen - basename_offset - isdir,
82dce998
NTND
671 pattern, prefix,
672 pat->patternlen, pat->flags);
d0bfd026 673 }
dc09e9ec 674 return match_pathname(pathname, pathlen - isdir,
82dce998
NTND
675 base, baselen,
676 pattern, prefix, pat->patternlen, pat->flags);
d0bfd026
JH
677}
678
ec775c41
HG
679static int macroexpand_one(int attr_nr, int rem);
680
515106fa
JH
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
969f9d73 686 for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
515106fa 687 struct git_attr *attr = a->state[i].attr;
a5e92abd
JH
688 const char **n = &(check[attr->attr_nr].value);
689 const char *v = a->state[i].setto;
515106fa
JH
690
691 if (*n == ATTR__UNKNOWN) {
426c27b7 692 debug_set(what,
712efb1a 693 a->is_macro ? a->u.attr->name : a->u.pat.pattern,
426c27b7 694 attr, v);
515106fa
JH
695 *n = v;
696 rem--;
ec775c41 697 rem = macroexpand_one(attr->attr_nr, rem);
515106fa
JH
698 }
699 }
700 return rem;
701}
702
bd2f371d 703static int fill(const char *path, int pathlen, int basename_offset,
4742d136 704 struct attr_stack *stk, int rem)
d0bfd026 705{
515106fa 706 int i;
d0bfd026
JH
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];
f48fd688
JH
711 if (a->is_macro)
712 continue;
bd2f371d 713 if (path_matches(path, pathlen, basename_offset,
82dce998 714 &a->u.pat, base, stk->originlen))
515106fa 715 rem = fill_one("fill", a, rem);
d0bfd026
JH
716 }
717 return rem;
718}
719
aa7710e0 720static int macroexpand_one(int nr, int rem)
f48fd688 721{
ec775c41 722 struct attr_stack *stk;
515106fa 723 int i;
f48fd688 724
fad32bcd
NTND
725 if (check_all_attr[nr].value != ATTR__TRUE ||
726 !check_all_attr[nr].attr->maybe_macro)
ec775c41
HG
727 return rem;
728
4b0c6961
JH
729 for (stk = attr_stack; stk; stk = stk->prev) {
730 for (i = stk->num_matches - 1; 0 <= i; i--) {
ec775c41
HG
731 struct match_attr *ma = stk->attrs[i];
732 if (!ma->is_macro)
733 continue;
aa7710e0 734 if (ma->u.attr->attr_nr == nr)
4b0c6961 735 return fill_one("expand", ma, rem);
ec775c41 736 }
4b0c6961 737 }
ec775c41 738
f48fd688
JH
739 return rem;
740}
741
2d721744 742/*
06a604e6
NTND
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.
2d721744 746 */
06a604e6
NTND
747static void collect_some_attrs(const char *path, int num,
748 struct git_attr_check *check)
749
d0bfd026
JH
750{
751 struct attr_stack *stk;
9db9eecf 752 int i, pathlen, rem, dirlen;
bd2f371d
JH
753 const char *cp, *last_slash = NULL;
754 int basename_offset;
9db9eecf
NTND
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) {
bd2f371d 762 basename_offset = last_slash + 1 - path;
9db9eecf
NTND
763 dirlen = last_slash - path;
764 } else {
bd2f371d 765 basename_offset = 0;
9db9eecf
NTND
766 dirlen = 0;
767 }
d0bfd026 768
9db9eecf 769 prepare_attr_stack(path, dirlen);
f48fd688 770 for (i = 0; i < attr_nr; i++)
515106fa 771 check_all_attr[i].value = ATTR__UNKNOWN;
06a604e6
NTND
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 }
d0bfd026 785
f48fd688
JH
786 rem = attr_nr;
787 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
bd2f371d 788 rem = fill(path, pathlen, basename_offset, stk, rem);
2d721744
MH
789}
790
d932f4eb 791int git_check_attr(const char *path, int num, struct git_attr_check *check)
2d721744
MH
792{
793 int i;
794
06a604e6 795 collect_some_attrs(path, num, check);
f48fd688 796
515106fa 797 for (i = 0; i < num; i++) {
a5e92abd 798 const char *value = check_all_attr[check[i].attr->attr_nr].value;
515106fa
JH
799 if (value == ATTR__UNKNOWN)
800 value = ATTR__UNSET;
801 check[i].value = value;
802 }
f48fd688 803
d0bfd026
JH
804 return 0;
805}
06f33c17 806
ee548df3
MH
807int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
808{
809 int i, count, j;
810
06a604e6 811 collect_some_attrs(path, 0, NULL);
ee548df3
MH
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;
b32fa95f 821 ALLOC_ARRAY(*check, count);
ee548df3
MH
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
06f33c17
JH
835void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
836{
837 enum git_attr_direction old = direction;
4191e80a
NTND
838
839 if (is_bare_repository() && new != GIT_ATTR_INDEX)
840 die("BUG: non-INDEX attr direction in a bare repo");
841
06f33c17
JH
842 direction = new;
843 if (new != old)
844 drop_attr_stack();
845 use_index = istate;
846}