]> git.ipfire.org Git - thirdparty/git.git/blame - attr.c
test-ref-store: remove force-create argument for create-reflog
[thirdparty/git.git] / attr.c
CommitLineData
86ab7f0c
MH
1/*
2 * Handle git attributes. See gitattributes(5) for a description of
3a1b3415 3 * the file syntax, and attr.h for a description of the API.
86ab7f0c
MH
4 *
5 * One basic design decision here is that we are not going to support
6 * an insanely large number of attributes.
7 */
8
d0bfd026 9#include "cache.h"
b2141fc1 10#include "config.h"
d807c4a0 11#include "exec-cmd.h"
d0bfd026 12#include "attr.h"
6eba6210 13#include "dir.h"
27547e5f 14#include "utf8.h"
860a74d9 15#include "quote.h"
1a600b75 16#include "thread-utils.h"
77efbb36 17#include "dir.h"
d0bfd026 18
a5e92abd
JH
19const char git_attr__true[] = "(builtin)true";
20const char git_attr__false[] = "\0(builtin)false";
21static const char git_attr__unknown[] = "(builtin)unknown";
22#define ATTR__TRUE git_attr__true
23#define ATTR__FALSE git_attr__false
24#define ATTR__UNSET NULL
25#define ATTR__UNKNOWN git_attr__unknown
515106fa 26
d0bfd026
JH
27#ifndef DEBUG_ATTR
28#define DEBUG_ATTR 0
29#endif
30
31struct git_attr {
1a600b75 32 int attr_nr; /* unique attribute number */
1a600b75 33 char name[FLEX_ARRAY]; /* attribute name */
d0bfd026 34};
7d42ec54 35
ec4d77aa 36const char *git_attr_name(const struct git_attr *attr)
352404ac
MH
37{
38 return attr->name;
39}
40
1a600b75
BW
41struct attr_hashmap {
42 struct hashmap map;
1a600b75 43 pthread_mutex_t mutex;
1a600b75
BW
44};
45
46static inline void hashmap_lock(struct attr_hashmap *map)
47{
1a600b75 48 pthread_mutex_lock(&map->mutex);
1a600b75
BW
49}
50
51static inline void hashmap_unlock(struct attr_hashmap *map)
d0bfd026 52{
1a600b75 53 pthread_mutex_unlock(&map->mutex);
1a600b75 54}
d0bfd026 55
1a600b75
BW
56/* The container for objects stored in "struct attr_hashmap" */
57struct attr_hash_entry {
e2b5038d 58 struct hashmap_entry ent;
1a600b75
BW
59 const char *key; /* the key; memory should be owned by value */
60 size_t keylen; /* length of the key */
61 void *value; /* the stored value */
62};
63
64/* attr_hashmap comparison function */
201c14e3 65static int attr_hash_entry_cmp(const void *unused_cmp_data,
939af16e
EW
66 const struct hashmap_entry *eptr,
67 const struct hashmap_entry *entry_or_key,
201c14e3 68 const void *unused_keydata)
1a600b75 69{
939af16e
EW
70 const struct attr_hash_entry *a, *b;
71
72 a = container_of(eptr, const struct attr_hash_entry, ent);
73 b = container_of(entry_or_key, const struct attr_hash_entry, ent);
1a600b75
BW
74 return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
75}
76
b19315d8
EN
77/*
78 * The global dictionary of all interned attributes. This
79 * is a singleton object which is shared between threads.
80 * Access to this dictionary must be surrounded with a mutex.
81 */
82static struct attr_hashmap g_attr_hashmap = {
83 HASHMAP_INIT(attr_hash_entry_cmp, NULL)
84};
1a600b75
BW
85
86/*
87 * Retrieve the 'value' stored in a hashmap given the provided 'key'.
88 * If there is no matching entry, return NULL.
89 */
90static void *attr_hashmap_get(struct attr_hashmap *map,
91 const char *key, size_t keylen)
92{
93 struct attr_hash_entry k;
94 struct attr_hash_entry *e;
95
d22245a2 96 hashmap_entry_init(&k.ent, memhash(key, keylen));
1a600b75
BW
97 k.key = key;
98 k.keylen = keylen;
404ab78e 99 e = hashmap_get_entry(&map->map, &k, ent, NULL);
1a600b75
BW
100
101 return e ? e->value : NULL;
102}
103
104/* Add 'value' to a hashmap based on the provided 'key'. */
105static void attr_hashmap_add(struct attr_hashmap *map,
106 const char *key, size_t keylen,
107 void *value)
108{
109 struct attr_hash_entry *e;
110
1a600b75 111 e = xmalloc(sizeof(struct attr_hash_entry));
d22245a2 112 hashmap_entry_init(&e->ent, memhash(key, keylen));
1a600b75
BW
113 e->key = key;
114 e->keylen = keylen;
115 e->value = value;
116
b94e5c1d 117 hashmap_add(&map->map, &e->ent);
d0bfd026
JH
118}
119
685b2925
BW
120struct all_attrs_item {
121 const struct git_attr *attr;
122 const char *value;
60a12722
BW
123 /*
124 * If 'macro' is non-NULL, indicates that 'attr' is a macro based on
125 * the current attribute stack and contains a pointer to the match_attr
126 * definition of the macro
127 */
128 const struct match_attr *macro;
685b2925
BW
129};
130
131/*
132 * Reallocate and reinitialize the array of all attributes (which is used in
133 * the attribute collection process) in 'check' based on the global dictionary
134 * of attributes.
135 */
136static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
137{
138 int i;
8b604d19 139 unsigned int size;
685b2925
BW
140
141 hashmap_lock(map);
142
8b604d19
JH
143 size = hashmap_get_size(&map->map);
144 if (size < check->all_attrs_nr)
033abf97 145 BUG("interned attributes shouldn't be deleted");
685b2925
BW
146
147 /*
148 * If the number of attributes in the global dictionary has increased
149 * (or this attr_check instance doesn't have an initialized all_attrs
150 * field), reallocate the provided attr_check instance's all_attrs
151 * field and fill each entry with its corresponding git_attr.
152 */
8b604d19 153 if (size != check->all_attrs_nr) {
685b2925
BW
154 struct attr_hash_entry *e;
155 struct hashmap_iter iter;
685b2925 156
8b604d19
JH
157 REALLOC_ARRAY(check->all_attrs, size);
158 check->all_attrs_nr = size;
685b2925 159
87571c3f 160 hashmap_for_each_entry(&map->map, &iter, e,
87571c3f 161 ent /* member name */) {
685b2925
BW
162 const struct git_attr *a = e->value;
163 check->all_attrs[a->attr_nr].attr = a;
164 }
165 }
166
167 hashmap_unlock(map);
168
169 /*
170 * Re-initialize every entry in check->all_attrs.
171 * This re-initialization can live outside of the locked region since
172 * the attribute dictionary is no longer being accessed.
173 */
174 for (i = 0; i < check->all_attrs_nr; i++) {
175 check->all_attrs[i].value = ATTR__UNKNOWN;
60a12722 176 check->all_attrs[i].macro = NULL;
685b2925
BW
177 }
178}
179
428103c7 180static int attr_name_valid(const char *name, size_t namelen)
e4aee10a
JH
181{
182 /*
d42453ab
MH
183 * Attribute name cannot begin with '-' and must consist of
184 * characters from [-A-Za-z0-9_.].
e4aee10a 185 */
c0b13b21 186 if (namelen <= 0 || *name == '-')
428103c7 187 return 0;
e4aee10a
JH
188 while (namelen--) {
189 char ch = *name++;
190 if (! (ch == '-' || ch == '.' || ch == '_' ||
191 ('0' <= ch && ch <= '9') ||
192 ('a' <= ch && ch <= 'z') ||
193 ('A' <= ch && ch <= 'Z')) )
428103c7 194 return 0;
e4aee10a 195 }
428103c7
JH
196 return 1;
197}
198
199static void report_invalid_attr(const char *name, size_t len,
200 const char *src, int lineno)
201{
202 struct strbuf err = STRBUF_INIT;
203 strbuf_addf(&err, _("%.*s is not a valid attribute name"),
204 (int) len, name);
205 fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno);
206 strbuf_release(&err);
e4aee10a
JH
207}
208
1a600b75
BW
209/*
210 * Given a 'name', lookup and return the corresponding attribute in the global
211 * dictionary. If no entry is found, create a new attribute and store it in
212 * the dictionary.
213 */
e810e063 214static const struct git_attr *git_attr_internal(const char *name, int namelen)
d0bfd026 215{
d0bfd026
JH
216 struct git_attr *a;
217
1a600b75 218 if (!attr_name_valid(name, namelen))
e4aee10a
JH
219 return NULL;
220
1a600b75
BW
221 hashmap_lock(&g_attr_hashmap);
222
223 a = attr_hashmap_get(&g_attr_hashmap, name, namelen);
224
225 if (!a) {
226 FLEX_ALLOC_MEM(a, name, name, namelen);
8b604d19 227 a->attr_nr = hashmap_get_size(&g_attr_hashmap.map);
1a600b75
BW
228
229 attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a);
8b604d19
JH
230 assert(a->attr_nr ==
231 (hashmap_get_size(&g_attr_hashmap.map) - 1));
1a600b75
BW
232 }
233
234 hashmap_unlock(&g_attr_hashmap);
f48fd688 235
d0bfd026
JH
236 return a;
237}
238
e810e063 239const struct git_attr *git_attr(const char *name)
7fb0eaa2
JH
240{
241 return git_attr_internal(name, strlen(name));
242}
243
515106fa 244/* What does a matched pattern decide? */
d0bfd026 245struct attr_state {
e810e063 246 const struct git_attr *attr;
a5e92abd 247 const char *setto;
d0bfd026
JH
248};
249
82dce998
NTND
250struct pattern {
251 const char *pattern;
252 int patternlen;
253 int nowildcardlen;
4ff89ee5 254 unsigned flags; /* PATTERN_FLAG_* */
82dce998
NTND
255};
256
ba845b75
MH
257/*
258 * One rule, as from a .gitattributes file.
259 *
260 * If is_macro is true, then u.attr is a pointer to the git_attr being
261 * defined.
262 *
4894f4ff
JH
263 * If is_macro is false, then u.pat is the filename pattern to which the
264 * rule applies.
ba845b75
MH
265 *
266 * In either case, num_attr is the number of attributes affected by
267 * this rule, and state is an array listing them. The attributes are
268 * listed as they appear in the file (macros unexpanded).
269 */
d0bfd026 270struct match_attr {
f48fd688 271 union {
82dce998 272 struct pattern pat;
e810e063 273 const struct git_attr *attr;
f48fd688
JH
274 } u;
275 char is_macro;
d0bfd026
JH
276 unsigned num_attr;
277 struct attr_state state[FLEX_ARRAY];
278};
279
280static const char blank[] = " \t\r\n";
281
dbf387d5
JK
282/* Flags usable in read_attr() and parse_attr_line() family of functions. */
283#define READ_ATTR_MACRO_OK (1<<0)
2ef579e2 284#define READ_ATTR_NOFOLLOW (1<<1)
dbf387d5 285
d1751298
MH
286/*
287 * Parse a whitespace-delimited attribute state (i.e., "attr",
288 * "-attr", "!attr", or "attr=value") from the string starting at src.
289 * If e is not NULL, write the results to *e. Return a pointer to the
290 * remainder of the string (with leading whitespace removed), or NULL
291 * if there was an error.
292 */
515106fa 293static const char *parse_attr(const char *src, int lineno, const char *cp,
d1751298 294 struct attr_state *e)
515106fa
JH
295{
296 const char *ep, *equals;
297 int len;
298
299 ep = cp + strcspn(cp, blank);
300 equals = strchr(cp, '=');
301 if (equals && ep < equals)
302 equals = NULL;
303 if (equals)
304 len = equals - cp;
305 else
306 len = ep - cp;
d1751298 307 if (!e) {
515106fa
JH
308 if (*cp == '-' || *cp == '!') {
309 cp++;
310 len--;
311 }
428103c7
JH
312 if (!attr_name_valid(cp, len)) {
313 report_invalid_attr(cp, len, src, lineno);
515106fa
JH
314 return NULL;
315 }
316 } else {
5a884019
JH
317 /*
318 * As this function is always called twice, once with
319 * e == NULL in the first pass and then e != NULL in
428103c7 320 * the second pass, no need for attr_name_valid()
5a884019
JH
321 * check here.
322 */
515106fa
JH
323 if (*cp == '-' || *cp == '!') {
324 e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
325 cp++;
326 len--;
327 }
328 else if (!equals)
329 e->setto = ATTR__TRUE;
330 else {
182af834 331 e->setto = xmemdupz(equals + 1, ep - equals - 1);
515106fa 332 }
7fb0eaa2 333 e->attr = git_attr_internal(cp, len);
515106fa 334 }
515106fa
JH
335 return ep + strspn(ep, blank);
336}
337
f48fd688 338static struct match_attr *parse_attr_line(const char *line, const char *src,
dbf387d5 339 int lineno, unsigned flags)
d0bfd026
JH
340{
341 int namelen;
d68e1c18 342 int num_attr, i;
85c4a0d0 343 const char *cp, *name, *states;
515106fa 344 struct match_attr *res = NULL;
f48fd688 345 int is_macro;
860a74d9 346 struct strbuf pattern = STRBUF_INIT;
d0bfd026
JH
347
348 cp = line + strspn(line, blank);
349 if (!*cp || *cp == '#')
350 return NULL;
351 name = cp;
860a74d9
NTND
352
353 if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
354 name = pattern.buf;
355 namelen = pattern.len;
356 } else {
357 namelen = strcspn(name, blank);
358 states = name + namelen;
359 }
360
f48fd688 361 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
59556548 362 starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
dbf387d5 363 if (!(flags & READ_ATTR_MACRO_OK)) {
ad8f8f4a
NTND
364 fprintf_ln(stderr, _("%s not allowed: %s:%d"),
365 name, src, lineno);
62af8969 366 goto fail_return;
f48fd688
JH
367 }
368 is_macro = 1;
369 name += strlen(ATTRIBUTE_MACRO_PREFIX);
370 name += strspn(name, blank);
371 namelen = strcspn(name, blank);
428103c7
JH
372 if (!attr_name_valid(name, namelen)) {
373 report_invalid_attr(name, namelen, src, lineno);
62af8969 374 goto fail_return;
e4aee10a 375 }
f48fd688
JH
376 }
377 else
378 is_macro = 0;
d0bfd026 379
85c4a0d0
MH
380 states += strspn(states, blank);
381
d68e1c18
MH
382 /* First pass to count the attr_states */
383 for (cp = states, num_attr = 0; *cp; num_attr++) {
384 cp = parse_attr(src, lineno, cp, NULL);
385 if (!cp)
62af8969 386 goto fail_return;
d0bfd026 387 }
d68e1c18
MH
388
389 res = xcalloc(1,
390 sizeof(*res) +
391 sizeof(struct attr_state) * num_attr +
392 (is_macro ? 0 : namelen + 1));
fad32bcd 393 if (is_macro) {
d68e1c18 394 res->u.attr = git_attr_internal(name, namelen);
fad32bcd 395 } else {
82dce998
NTND
396 char *p = (char *)&(res->state[num_attr]);
397 memcpy(p, name, namelen);
398 res->u.pat.pattern = p;
65edd96a 399 parse_path_pattern(&res->u.pat.pattern,
82dce998
NTND
400 &res->u.pat.patternlen,
401 &res->u.pat.flags,
402 &res->u.pat.nowildcardlen);
4ff89ee5 403 if (res->u.pat.flags & PATTERN_FLAG_NEGATIVE) {
8b1bd024
TR
404 warning(_("Negative patterns are ignored in git attributes\n"
405 "Use '\\!' for literal leading exclamation."));
62af8969 406 goto fail_return;
8b1bd024 407 }
d0bfd026 408 }
d68e1c18
MH
409 res->is_macro = is_macro;
410 res->num_attr = num_attr;
411
412 /* Second pass to fill the attr_states */
413 for (cp = states, i = 0; *cp; i++) {
414 cp = parse_attr(src, lineno, cp, &(res->state[i]));
415 }
416
860a74d9 417 strbuf_release(&pattern);
d0bfd026 418 return res;
62af8969
JH
419
420fail_return:
860a74d9 421 strbuf_release(&pattern);
62af8969
JH
422 free(res);
423 return NULL;
d0bfd026
JH
424}
425
426/*
427 * Like info/exclude and .gitignore, the attribute information can
428 * come from many places.
429 *
bb101aaf
RD
430 * (1) .gitattributes file of the same directory;
431 * (2) .gitattributes file of the parent directory if (1) does not have
515106fa
JH
432 * any match; this goes recursively upwards, just like .gitignore.
433 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
d0bfd026
JH
434 *
435 * In the same file, later entries override the earlier match, so in the
436 * global list, we would have entries from info/attributes the earliest
bb101aaf 437 * (reading the file from top to bottom), .gitattributes of the root
d0bfd026
JH
438 * directory (again, reading the file from top to bottom) down to the
439 * current directory, and then scan the list backwards to find the first match.
6d24e7a8 440 * This is exactly the same as what is_excluded() does in dir.c to deal with
6f416cba 441 * .gitignore file and info/excludes file as a fallback.
d0bfd026
JH
442 */
443
dc81cf37 444struct attr_stack {
d0bfd026
JH
445 struct attr_stack *prev;
446 char *origin;
cd6a0b26 447 size_t originlen;
d0bfd026 448 unsigned num_matches;
a4413118 449 unsigned alloc;
d0bfd026 450 struct match_attr **attrs;
dc81cf37 451};
d0bfd026 452
dc81cf37 453static void attr_stack_free(struct attr_stack *e)
d0bfd026
JH
454{
455 int i;
456 free(e->origin);
515106fa
JH
457 for (i = 0; i < e->num_matches; i++) {
458 struct match_attr *a = e->attrs[i];
459 int j;
460 for (j = 0; j < a->num_attr; j++) {
a5e92abd 461 const char *setto = a->state[j].setto;
515106fa
JH
462 if (setto == ATTR__TRUE ||
463 setto == ATTR__FALSE ||
464 setto == ATTR__UNSET ||
465 setto == ATTR__UNKNOWN)
466 ;
467 else
4b25d091 468 free((char *) setto);
515106fa
JH
469 }
470 free(a);
471 }
37475f97 472 free(e->attrs);
d0bfd026
JH
473 free(e);
474}
475
dc81cf37
BW
476static void drop_attr_stack(struct attr_stack **stack)
477{
478 while (*stack) {
479 struct attr_stack *elem = *stack;
480 *stack = elem->prev;
481 attr_stack_free(elem);
482 }
483}
484
485/* List of all attr_check structs; access should be surrounded by mutex */
486static struct check_vector {
487 size_t nr;
488 size_t alloc;
489 struct attr_check **checks;
dc81cf37 490 pthread_mutex_t mutex;
dc81cf37
BW
491} check_vector;
492
493static inline void vector_lock(void)
494{
dc81cf37 495 pthread_mutex_lock(&check_vector.mutex);
dc81cf37
BW
496}
497
498static inline void vector_unlock(void)
499{
dc81cf37 500 pthread_mutex_unlock(&check_vector.mutex);
dc81cf37
BW
501}
502
503static void check_vector_add(struct attr_check *c)
504{
505 vector_lock();
506
507 ALLOC_GROW(check_vector.checks,
508 check_vector.nr + 1,
509 check_vector.alloc);
510 check_vector.checks[check_vector.nr++] = c;
511
512 vector_unlock();
513}
514
515static void check_vector_remove(struct attr_check *check)
516{
517 int i;
518
519 vector_lock();
520
521 /* Find entry */
522 for (i = 0; i < check_vector.nr; i++)
523 if (check_vector.checks[i] == check)
524 break;
525
526 if (i >= check_vector.nr)
033abf97 527 BUG("no entry found");
dc81cf37
BW
528
529 /* shift entries over */
530 for (; i < check_vector.nr - 1; i++)
531 check_vector.checks[i] = check_vector.checks[i + 1];
532
533 check_vector.nr--;
534
535 vector_unlock();
536}
537
538/* Iterate through all attr_check instances and drop their stacks */
539static void drop_all_attr_stacks(void)
540{
541 int i;
542
543 vector_lock();
544
545 for (i = 0; i < check_vector.nr; i++) {
546 drop_attr_stack(&check_vector.checks[i]->stack);
547 }
548
549 vector_unlock();
550}
551
37293768
JH
552struct attr_check *attr_check_alloc(void)
553{
dc81cf37
BW
554 struct attr_check *c = xcalloc(1, sizeof(struct attr_check));
555
556 /* save pointer to the check struct */
557 check_vector_add(c);
558
559 return c;
37293768
JH
560}
561
562struct attr_check *attr_check_initl(const char *one, ...)
563{
564 struct attr_check *check;
565 int cnt;
566 va_list params;
567 const char *param;
568
569 va_start(params, one);
570 for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)
571 ;
572 va_end(params);
573
574 check = attr_check_alloc();
575 check->nr = cnt;
576 check->alloc = cnt;
ca56dadb 577 CALLOC_ARRAY(check->items, cnt);
37293768
JH
578
579 check->items[0].attr = git_attr(one);
580 va_start(params, one);
581 for (cnt = 1; cnt < check->nr; cnt++) {
582 const struct git_attr *attr;
583 param = va_arg(params, const char *);
584 if (!param)
033abf97 585 BUG("counted %d != ended at %d",
37293768
JH
586 check->nr, cnt);
587 attr = git_attr(param);
588 if (!attr)
033abf97 589 BUG("%s: not a valid attribute name", param);
37293768
JH
590 check->items[cnt].attr = attr;
591 }
592 va_end(params);
593 return check;
594}
595
b0db7046
BW
596struct attr_check *attr_check_dup(const struct attr_check *check)
597{
598 struct attr_check *ret;
599
600 if (!check)
601 return NULL;
602
603 ret = attr_check_alloc();
604
605 ret->nr = check->nr;
606 ret->alloc = check->alloc;
607 ALLOC_ARRAY(ret->items, ret->nr);
608 COPY_ARRAY(ret->items, check->items, ret->nr);
609
610 return ret;
611}
612
37293768
JH
613struct attr_check_item *attr_check_append(struct attr_check *check,
614 const struct git_attr *attr)
615{
616 struct attr_check_item *item;
617
618 ALLOC_GROW(check->items, check->nr + 1, check->alloc);
619 item = &check->items[check->nr++];
620 item->attr = attr;
621 return item;
622}
623
624void attr_check_reset(struct attr_check *check)
625{
626 check->nr = 0;
627}
628
629void attr_check_clear(struct attr_check *check)
630{
6a83d902 631 FREE_AND_NULL(check->items);
37293768
JH
632 check->alloc = 0;
633 check->nr = 0;
685b2925 634
6a83d902 635 FREE_AND_NULL(check->all_attrs);
685b2925 636 check->all_attrs_nr = 0;
dc81cf37
BW
637
638 drop_attr_stack(&check->stack);
37293768
JH
639}
640
641void attr_check_free(struct attr_check *check)
642{
dc81cf37
BW
643 if (check) {
644 /* Remove check from the check vector */
645 check_vector_remove(check);
646
647 attr_check_clear(check);
648 free(check);
649 }
37293768
JH
650}
651
d0bfd026 652static const char *builtin_attr[] = {
155a4b71 653 "[attr]binary -diff -merge -text",
d0bfd026
JH
654 NULL,
655};
656
a4413118
JH
657static void handle_attr_line(struct attr_stack *res,
658 const char *line,
659 const char *src,
660 int lineno,
dbf387d5 661 unsigned flags)
a4413118
JH
662{
663 struct match_attr *a;
664
dbf387d5 665 a = parse_attr_line(line, src, lineno, flags);
a4413118
JH
666 if (!a)
667 return;
3a7fa03d 668 ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
a4413118
JH
669 res->attrs[res->num_matches++] = a;
670}
671
d0bfd026
JH
672static struct attr_stack *read_attr_from_array(const char **list)
673{
674 struct attr_stack *res;
675 const char *line;
f48fd688 676 int lineno = 0;
d0bfd026 677
ca56dadb 678 CALLOC_ARRAY(res, 1);
a4413118 679 while ((line = *(list++)) != NULL)
dbf387d5
JK
680 handle_attr_line(res, line, "[builtin]", ++lineno,
681 READ_ATTR_MACRO_OK);
d0bfd026
JH
682 return res;
683}
684
7d42ec54 685/*
f0dd0421
BW
686 * Callers into the attribute system assume there is a single, system-wide
687 * global state where attributes are read from and when the state is flipped by
688 * calling git_attr_set_direction(), the stack frames that have been
abcb66c6 689 * constructed need to be discarded so that subsequent calls into the
f0dd0421
BW
690 * attribute system will lazily read from the right place. Since changing
691 * direction causes a global paradigm shift, it should not ever be called while
692 * another thread could potentially be calling into the attribute system.
7d42ec54 693 */
06f33c17 694static enum git_attr_direction direction;
06f33c17 695
c4500e25 696void git_attr_set_direction(enum git_attr_direction new_direction)
f0dd0421
BW
697{
698 if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
033abf97 699 BUG("non-INDEX attr direction in a bare repo");
f0dd0421
BW
700
701 if (new_direction != direction)
702 drop_all_attr_stacks();
703
704 direction = new_direction;
f0dd0421
BW
705}
706
dbf387d5 707static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
d0bfd026 708{
2ef579e2
JK
709 int fd;
710 FILE *fp;
d0bfd026
JH
711 struct attr_stack *res;
712 char buf[2048];
f48fd688 713 int lineno = 0;
d0bfd026 714
2ef579e2
JK
715 if (flags & READ_ATTR_NOFOLLOW)
716 fd = open_nofollow(path, O_RDONLY);
717 else
718 fd = open(path, O_RDONLY);
719
720 if (fd < 0) {
721 warn_on_fopen_errors(path);
a4413118 722 return NULL;
2ef579e2
JK
723 }
724 fp = xfdopen(fd, "r");
725
ca56dadb 726 CALLOC_ARRAY(res, 1);
27547e5f
JH
727 while (fgets(buf, sizeof(buf), fp)) {
728 char *bufp = buf;
729 if (!lineno)
730 skip_utf8_bom(&bufp, strlen(bufp));
dbf387d5 731 handle_attr_line(res, bufp, path, ++lineno, flags);
27547e5f 732 }
a4413118
JH
733 fclose(fp);
734 return res;
735}
d0bfd026 736
847a9e5d 737static struct attr_stack *read_attr_from_index(struct index_state *istate,
7a400a2c 738 const char *path,
dbf387d5 739 unsigned flags)
a4413118
JH
740{
741 struct attr_stack *res;
1a9d7e9b
JH
742 char *buf, *sp;
743 int lineno = 0;
f48fd688 744
c4500e25 745 if (!istate)
7a400a2c
NTND
746 return NULL;
747
77efbb36
DS
748 /*
749 * The .gitattributes file only applies to files within its
750 * parent directory. In the case of cone-mode sparse-checkout,
751 * the .gitattributes file is sparse if and only if all paths
752 * within that directory are also sparse. Thus, don't load the
753 * .gitattributes file since it will not matter.
754 *
755 * In the case of a sparse index, it is critical that we don't go
756 * looking for a .gitattributes file, as doing so would cause the
757 * index to expand.
758 */
759 if (!path_in_cone_mode_sparse_checkout(path, istate))
760 return NULL;
761
c4500e25 762 buf = read_blob_data_from_index(istate, path, NULL);
1a9d7e9b 763 if (!buf)
06f33c17 764 return NULL;
1a9d7e9b 765
ca56dadb 766 CALLOC_ARRAY(res, 1);
1a9d7e9b
JH
767 for (sp = buf; *sp; ) {
768 char *ep;
769 int more;
263434f2
JH
770
771 ep = strchrnul(sp, '\n');
1a9d7e9b
JH
772 more = (*ep == '\n');
773 *ep = '\0';
dbf387d5 774 handle_attr_line(res, sp, path, ++lineno, flags);
1a9d7e9b
JH
775 sp = ep + more;
776 }
777 free(buf);
d0bfd026
JH
778 return res;
779}
780
847a9e5d 781static struct attr_stack *read_attr(struct index_state *istate,
dbf387d5 782 const char *path, unsigned flags)
06f33c17 783{
0787dafd 784 struct attr_stack *res = NULL;
06f33c17 785
0787dafd 786 if (direction == GIT_ATTR_INDEX) {
dbf387d5 787 res = read_attr_from_index(istate, path, flags);
0787dafd
BW
788 } else if (!is_bare_repository()) {
789 if (direction == GIT_ATTR_CHECKOUT) {
dbf387d5 790 res = read_attr_from_index(istate, path, flags);
0787dafd 791 if (!res)
dbf387d5 792 res = read_attr_from_file(path, flags);
0787dafd 793 } else if (direction == GIT_ATTR_CHECKIN) {
dbf387d5 794 res = read_attr_from_file(path, flags);
0787dafd
BW
795 if (!res)
796 /*
797 * There is no checked out .gitattributes file
798 * there, but we might have it in the index.
799 * We allow operation in a sparsely checked out
800 * work tree, so read from it.
801 */
dbf387d5 802 res = read_attr_from_index(istate, path, flags);
0787dafd 803 }
06f33c17 804 }
0787dafd 805
06f33c17 806 if (!res)
ca56dadb 807 CALLOC_ARRAY(res, 1);
06f33c17
JH
808 return res;
809}
810
d0bfd026
JH
811#if DEBUG_ATTR
812static void debug_info(const char *what, struct attr_stack *elem)
813{
814 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
815}
cf94ccda 816static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
f48fd688 817{
515106fa
JH
818 const char *value = v;
819
820 if (ATTR_TRUE(value))
821 value = "set";
822 else if (ATTR_FALSE(value))
823 value = "unset";
824 else if (ATTR_UNSET(value))
825 value = "unspecified";
826
827 fprintf(stderr, "%s: %s => %s (%s)\n",
828 what, attr->name, (char *) value, match);
f48fd688 829}
d0bfd026
JH
830#define debug_push(a) debug_info("push", (a))
831#define debug_pop(a) debug_info("pop", (a))
832#else
833#define debug_push(a) do { ; } while (0)
834#define debug_pop(a) do { ; } while (0)
f48fd688 835#define debug_set(a,b,c,d) do { ; } while (0)
034d35c9 836#endif /* DEBUG_ATTR */
d0bfd026 837
c5147722 838static const char *git_etc_gitattributes(void)
6df42ab9
PO
839{
840 static const char *system_wide;
841 if (!system_wide)
842 system_wide = system_path(ETC_GITATTRIBUTES);
843 return system_wide;
844}
845
dc81cf37
BW
846static const char *get_home_gitattributes(void)
847{
848 if (!git_attributes_file)
849 git_attributes_file = xdg_config_home("attributes");
850
851 return git_attributes_file;
852}
853
c5147722 854static int git_attr_system(void)
6df42ab9
PO
855{
856 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
857}
858
f932729c
JK
859static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
860
4c0ce074
JH
861static void push_stack(struct attr_stack **attr_stack_p,
862 struct attr_stack *elem, char *origin, size_t originlen)
863{
864 if (elem) {
865 elem->origin = origin;
866 if (origin)
867 elem->originlen = originlen;
868 elem->prev = *attr_stack_p;
869 *attr_stack_p = elem;
870 }
871}
872
847a9e5d 873static void bootstrap_attr_stack(struct index_state *istate,
7a400a2c 874 struct attr_stack **stack)
f48fd688 875{
dc81cf37 876 struct attr_stack *e;
dbf387d5 877 unsigned flags = READ_ATTR_MACRO_OK;
f48fd688 878
dc81cf37 879 if (*stack)
909ca7b9 880 return;
f48fd688 881
dc81cf37
BW
882 /* builtin frame */
883 e = read_attr_from_array(builtin_attr);
884 push_stack(stack, e, NULL, 0);
6df42ab9 885
dc81cf37
BW
886 /* system-wide frame */
887 if (git_attr_system()) {
dbf387d5 888 e = read_attr_from_file(git_etc_gitattributes(), flags);
dc81cf37
BW
889 push_stack(stack, e, NULL, 0);
890 }
f48fd688 891
dc81cf37
BW
892 /* home directory */
893 if (get_home_gitattributes()) {
dbf387d5 894 e = read_attr_from_file(get_home_gitattributes(), flags);
dc81cf37 895 push_stack(stack, e, NULL, 0);
f48fd688 896 }
909ca7b9 897
dc81cf37 898 /* root directory */
2ef579e2 899 e = read_attr(istate, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);
dc81cf37 900 push_stack(stack, e, xstrdup(""), 0);
f0056f64 901
dc81cf37
BW
902 /* info frame */
903 if (startup_info->have_repository)
dbf387d5 904 e = read_attr_from_file(git_path_info_attributes(), flags);
dc81cf37
BW
905 else
906 e = NULL;
907 if (!e)
ca56dadb 908 CALLOC_ARRAY(e, 1);
dc81cf37 909 push_stack(stack, e, NULL, 0);
f48fd688
JH
910}
911
847a9e5d 912static void prepare_attr_stack(struct index_state *istate,
7a400a2c 913 const char *path, int dirlen,
dc81cf37 914 struct attr_stack **stack)
d0bfd026 915{
dc81cf37 916 struct attr_stack *info;
0787dafd 917 struct strbuf pathbuf = STRBUF_INIT;
a8727017 918
d0bfd026
JH
919 /*
920 * At the bottom of the attribute stack is the built-in
6df42ab9
PO
921 * set of attribute definitions, followed by the contents
922 * of $(prefix)/etc/gitattributes and a file specified by
923 * core.attributesfile. Then, contents from
bb101aaf 924 * .gitattributes files from directories closer to the
d0bfd026
JH
925 * root to the ones in deeper directories are pushed
926 * to the stack. Finally, at the very top of the stack
927 * we always keep the contents of $GIT_DIR/info/attributes.
928 *
929 * When checking, we use entries from near the top of the
930 * stack, preferring $GIT_DIR/info/attributes, then
931 * .gitattributes in deeper directories to shallower ones,
932 * and finally use the built-in set as the default.
933 */
7a400a2c 934 bootstrap_attr_stack(istate, stack);
d0bfd026
JH
935
936 /*
937 * Pop the "info" one that is always at the top of the stack.
938 */
dc81cf37
BW
939 info = *stack;
940 *stack = info->prev;
d0bfd026
JH
941
942 /*
943 * Pop the ones from directories that are not the prefix of
c432ef99
JH
944 * the path we are checking. Break out of the loop when we see
945 * the root one (whose origin is an empty string "") or the builtin
946 * one (whose origin is NULL) without popping it.
d0bfd026 947 */
dc81cf37
BW
948 while ((*stack)->origin) {
949 int namelen = (*stack)->originlen;
950 struct attr_stack *elem;
d0bfd026 951
dc81cf37 952 elem = *stack;
d0bfd026 953 if (namelen <= dirlen &&
1afca444
JK
954 !strncmp(elem->origin, path, namelen) &&
955 (!namelen || path[namelen] == '/'))
d0bfd026
JH
956 break;
957
958 debug_pop(elem);
dc81cf37
BW
959 *stack = elem->prev;
960 attr_stack_free(elem);
d0bfd026
JH
961 }
962
963 /*
0787dafd
BW
964 * bootstrap_attr_stack() should have added, and the
965 * above loop should have stopped before popping, the
966 * root element whose attr_stack->origin is set to an
967 * empty string.
d0bfd026 968 */
0787dafd
BW
969 assert((*stack)->origin);
970
971 strbuf_addstr(&pathbuf, (*stack)->origin);
972 /* Build up to the directory 'path' is in */
973 while (pathbuf.len < dirlen) {
974 size_t len = pathbuf.len;
975 struct attr_stack *next;
976 char *origin;
977
978 /* Skip path-separator */
979 if (len < dirlen && is_dir_sep(path[len]))
980 len++;
981 /* Find the end of the next component */
982 while (len < dirlen && !is_dir_sep(path[len]))
983 len++;
984
985 if (pathbuf.len > 0)
986 strbuf_addch(&pathbuf, '/');
987 strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));
988 strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE);
989
2ef579e2 990 next = read_attr(istate, pathbuf.buf, READ_ATTR_NOFOLLOW);
0787dafd
BW
991
992 /* reset the pathbuf to not include "/.gitattributes" */
993 strbuf_setlen(&pathbuf, len);
994
995 origin = xstrdup(pathbuf.buf);
996 push_stack(stack, next, origin, len);
97410b27 997 }
d4c98565 998
d0bfd026
JH
999 /*
1000 * Finally push the "info" one at the top of the stack.
1001 */
dc81cf37 1002 push_stack(stack, info, NULL, 0);
0787dafd
BW
1003
1004 strbuf_release(&pathbuf);
d0bfd026
JH
1005}
1006
1007static int path_matches(const char *pathname, int pathlen,
bd2f371d 1008 int basename_offset,
82dce998 1009 const struct pattern *pat,
d0bfd026
JH
1010 const char *base, int baselen)
1011{
82dce998
NTND
1012 const char *pattern = pat->pattern;
1013 int prefix = pat->nowildcardlen;
dc09e9ec 1014 int isdir = (pathlen && pathname[pathlen - 1] == '/');
82dce998 1015
4ff89ee5 1016 if ((pat->flags & PATTERN_FLAG_MUSTBEDIR) && !isdir)
94bc671a
JNA
1017 return 0;
1018
4ff89ee5 1019 if (pat->flags & PATTERN_FLAG_NODIR) {
bd2f371d 1020 return match_basename(pathname + basename_offset,
dc09e9ec 1021 pathlen - basename_offset - isdir,
82dce998
NTND
1022 pattern, prefix,
1023 pat->patternlen, pat->flags);
d0bfd026 1024 }
dc09e9ec 1025 return match_pathname(pathname, pathlen - isdir,
82dce998
NTND
1026 base, baselen,
1027 pattern, prefix, pat->patternlen, pat->flags);
d0bfd026
JH
1028}
1029
685b2925 1030static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem);
ec775c41 1031
685b2925 1032static int fill_one(const char *what, struct all_attrs_item *all_attrs,
60a12722 1033 const struct match_attr *a, int rem)
515106fa 1034{
515106fa
JH
1035 int i;
1036
685b2925 1037 for (i = a->num_attr - 1; rem > 0 && i >= 0; i--) {
e810e063 1038 const struct git_attr *attr = a->state[i].attr;
685b2925 1039 const char **n = &(all_attrs[attr->attr_nr].value);
a5e92abd 1040 const char *v = a->state[i].setto;
515106fa
JH
1041
1042 if (*n == ATTR__UNKNOWN) {
426c27b7 1043 debug_set(what,
712efb1a 1044 a->is_macro ? a->u.attr->name : a->u.pat.pattern,
426c27b7 1045 attr, v);
515106fa
JH
1046 *n = v;
1047 rem--;
685b2925 1048 rem = macroexpand_one(all_attrs, attr->attr_nr, rem);
515106fa
JH
1049 }
1050 }
1051 return rem;
1052}
1053
bd2f371d 1054static int fill(const char *path, int pathlen, int basename_offset,
dc81cf37
BW
1055 const struct attr_stack *stack,
1056 struct all_attrs_item *all_attrs, int rem)
d0bfd026 1057{
dc81cf37
BW
1058 for (; rem > 0 && stack; stack = stack->prev) {
1059 int i;
1060 const char *base = stack->origin ? stack->origin : "";
1061
1062 for (i = stack->num_matches - 1; 0 < rem && 0 <= i; i--) {
1063 const struct match_attr *a = stack->attrs[i];
1064 if (a->is_macro)
1065 continue;
1066 if (path_matches(path, pathlen, basename_offset,
1067 &a->u.pat, base, stack->originlen))
1068 rem = fill_one("fill", all_attrs, a, rem);
1069 }
d0bfd026 1070 }
dc81cf37 1071
d0bfd026
JH
1072 return rem;
1073}
1074
685b2925 1075static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem)
f48fd688 1076{
60a12722 1077 const struct all_attrs_item *item = &all_attrs[nr];
f48fd688 1078
60a12722
BW
1079 if (item->macro && item->value == ATTR__TRUE)
1080 return fill_one("expand", all_attrs, item->macro, rem);
1081 else
ec775c41 1082 return rem;
60a12722 1083}
ec775c41 1084
60a12722
BW
1085/*
1086 * Marks the attributes which are macros based on the attribute stack.
1087 * This prevents having to search through the attribute stack each time
1088 * a macro needs to be expanded during the fill stage.
1089 */
1090static void determine_macros(struct all_attrs_item *all_attrs,
1091 const struct attr_stack *stack)
1092{
1093 for (; stack; stack = stack->prev) {
1094 int i;
1095 for (i = stack->num_matches - 1; i >= 0; i--) {
1096 const struct match_attr *ma = stack->attrs[i];
1097 if (ma->is_macro) {
1098 int n = ma->u.attr->attr_nr;
1099 if (!all_attrs[n].macro) {
1100 all_attrs[n].macro = ma;
1101 }
1102 }
ec775c41 1103 }
4b0c6961 1104 }
f48fd688
JH
1105}
1106
2d721744 1107/*
685b2925
BW
1108 * Collect attributes for path into the array pointed to by check->all_attrs.
1109 * If check->check_nr is non-zero, only attributes in check[] are collected.
1110 * Otherwise all attributes are collected.
2d721744 1111 */
847a9e5d 1112static void collect_some_attrs(struct index_state *istate,
7a400a2c
NTND
1113 const char *path,
1114 struct attr_check *check)
d0bfd026 1115{
7b95849b 1116 int pathlen, rem, dirlen;
bd2f371d
JH
1117 const char *cp, *last_slash = NULL;
1118 int basename_offset;
9db9eecf
NTND
1119
1120 for (cp = path; *cp; cp++) {
1121 if (*cp == '/' && cp[1])
1122 last_slash = cp;
1123 }
1124 pathlen = cp - path;
1125 if (last_slash) {
bd2f371d 1126 basename_offset = last_slash + 1 - path;
9db9eecf
NTND
1127 dirlen = last_slash - path;
1128 } else {
bd2f371d 1129 basename_offset = 0;
9db9eecf
NTND
1130 dirlen = 0;
1131 }
d0bfd026 1132
7a400a2c 1133 prepare_attr_stack(istate, path, dirlen, &check->stack);
685b2925 1134 all_attrs_init(&g_attr_hashmap, check);
dc81cf37 1135 determine_macros(check->all_attrs, check->stack);
685b2925 1136
685b2925 1137 rem = check->all_attrs_nr;
dc81cf37 1138 fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
2d721744
MH
1139}
1140
847a9e5d 1141void git_check_attr(struct index_state *istate,
d64324cb
TB
1142 const char *path,
1143 struct attr_check *check)
2d721744
MH
1144{
1145 int i;
1146
7a400a2c 1147 collect_some_attrs(istate, path, check);
f48fd688 1148
6bc2e3f7 1149 for (i = 0; i < check->nr; i++) {
685b2925
BW
1150 size_t n = check->items[i].attr->attr_nr;
1151 const char *value = check->all_attrs[n].value;
515106fa
JH
1152 if (value == ATTR__UNKNOWN)
1153 value = ATTR__UNSET;
6bc2e3f7 1154 check->items[i].value = value;
515106fa 1155 }
d0bfd026 1156}
06f33c17 1157
847a9e5d 1158void git_all_attrs(struct index_state *istate,
7a400a2c 1159 const char *path, struct attr_check *check)
ee548df3 1160{
7f864111 1161 int i;
ee548df3 1162
7f864111 1163 attr_check_reset(check);
7a400a2c 1164 collect_some_attrs(istate, path, check);
ee548df3 1165
685b2925
BW
1166 for (i = 0; i < check->all_attrs_nr; i++) {
1167 const char *name = check->all_attrs[i].attr->name;
1168 const char *value = check->all_attrs[i].value;
7f864111
JH
1169 struct attr_check_item *item;
1170 if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
1171 continue;
1172 item = attr_check_append(check, git_attr(name));
1173 item->value = value;
ee548df3 1174 }
ee548df3
MH
1175}
1176
1a600b75
BW
1177void attr_start(void)
1178{
1a600b75 1179 pthread_mutex_init(&g_attr_hashmap.mutex, NULL);
dc81cf37 1180 pthread_mutex_init(&check_vector.mutex, NULL);
1a600b75 1181}