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