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