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