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