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