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