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