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