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