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