Coverage Report

Created: 2024-09-08 06:23

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