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