]>
Commit | Line | Data |
---|---|---|
86ab7f0c MH |
1 | /* |
2 | * Handle git attributes. See gitattributes(5) for a description of | |
3 | * the file syntax, and Documentation/technical/api-gitattributes.txt | |
4 | * for a description of the API. | |
5 | * | |
6 | * One basic design decision here is that we are not going to support | |
7 | * an insanely large number of attributes. | |
8 | */ | |
9 | ||
06f33c17 | 10 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
d0bfd026 | 11 | #include "cache.h" |
6df42ab9 | 12 | #include "exec_cmd.h" |
d0bfd026 | 13 | #include "attr.h" |
6eba6210 | 14 | #include "dir.h" |
27547e5f | 15 | #include "utf8.h" |
860a74d9 | 16 | #include "quote.h" |
d0bfd026 | 17 | |
a5e92abd JH |
18 | const char git_attr__true[] = "(builtin)true"; |
19 | const char git_attr__false[] = "\0(builtin)false"; | |
20 | static const char git_attr__unknown[] = "(builtin)unknown"; | |
21 | #define ATTR__TRUE git_attr__true | |
22 | #define ATTR__FALSE git_attr__false | |
23 | #define ATTR__UNSET NULL | |
24 | #define ATTR__UNKNOWN git_attr__unknown | |
515106fa | 25 | |
86ab7f0c | 26 | /* This is a randomly chosen prime. */ |
d0bfd026 JH |
27 | #define HASHSIZE 257 |
28 | ||
29 | #ifndef DEBUG_ATTR | |
30 | #define DEBUG_ATTR 0 | |
31 | #endif | |
32 | ||
7d42ec54 JH |
33 | /* |
34 | * NEEDSWORK: the global dictionary of the interned attributes | |
35 | * must stay a singleton even after we become thread-ready. | |
36 | * Access to these must be surrounded with mutex when it happens. | |
37 | */ | |
d0bfd026 JH |
38 | struct git_attr { |
39 | struct git_attr *next; | |
40 | unsigned h; | |
f48fd688 | 41 | int attr_nr; |
fad32bcd | 42 | int maybe_macro; |
06a604e6 | 43 | int maybe_real; |
d0bfd026 JH |
44 | char name[FLEX_ARRAY]; |
45 | }; | |
f48fd688 | 46 | static int attr_nr; |
7d42ec54 JH |
47 | static struct git_attr *(git_attr_hash[HASHSIZE]); |
48 | ||
49 | /* | |
50 | * NEEDSWORK: maybe-real, maybe-macro are not property of | |
51 | * an attribute, as it depends on what .gitattributes are | |
52 | * read. Once we introduce per git_attr_check attr_stack | |
53 | * and check_all_attr, the optimization based on them will | |
54 | * become unnecessary and can go away. So is this variable. | |
55 | */ | |
06a604e6 | 56 | static int cannot_trust_maybe_real; |
d0bfd026 | 57 | |
7d42ec54 | 58 | /* NEEDSWORK: This will become per git_attr_check */ |
7bd18054 | 59 | static struct attr_check_item *check_all_attr; |
d0bfd026 | 60 | |
ec4d77aa | 61 | const char *git_attr_name(const struct git_attr *attr) |
352404ac MH |
62 | { |
63 | return attr->name; | |
64 | } | |
65 | ||
d0bfd026 JH |
66 | static unsigned hash_name(const char *name, int namelen) |
67 | { | |
48fb7deb | 68 | unsigned val = 0, c; |
d0bfd026 JH |
69 | |
70 | while (namelen--) { | |
71 | c = *name++; | |
72 | val = ((val << 7) | (val >> 22)) ^ c; | |
73 | } | |
74 | return val; | |
75 | } | |
76 | ||
e4aee10a JH |
77 | static int invalid_attr_name(const char *name, int namelen) |
78 | { | |
79 | /* | |
d42453ab MH |
80 | * Attribute name cannot begin with '-' and must consist of |
81 | * characters from [-A-Za-z0-9_.]. | |
e4aee10a | 82 | */ |
c0b13b21 | 83 | if (namelen <= 0 || *name == '-') |
e4aee10a JH |
84 | return -1; |
85 | while (namelen--) { | |
86 | char ch = *name++; | |
87 | if (! (ch == '-' || ch == '.' || ch == '_' || | |
88 | ('0' <= ch && ch <= '9') || | |
89 | ('a' <= ch && ch <= 'z') || | |
90 | ('A' <= ch && ch <= 'Z')) ) | |
91 | return -1; | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | ||
7fb0eaa2 | 96 | static struct git_attr *git_attr_internal(const char *name, int len) |
d0bfd026 JH |
97 | { |
98 | unsigned hval = hash_name(name, len); | |
99 | unsigned pos = hval % HASHSIZE; | |
100 | struct git_attr *a; | |
101 | ||
102 | for (a = git_attr_hash[pos]; a; a = a->next) { | |
103 | if (a->h == hval && | |
104 | !memcmp(a->name, name, len) && !a->name[len]) | |
105 | return a; | |
106 | } | |
107 | ||
e4aee10a JH |
108 | if (invalid_attr_name(name, len)) |
109 | return NULL; | |
110 | ||
96ffc06f | 111 | FLEX_ALLOC_MEM(a, name, name, len); |
d0bfd026 JH |
112 | a->h = hval; |
113 | a->next = git_attr_hash[pos]; | |
f48fd688 | 114 | a->attr_nr = attr_nr++; |
fad32bcd | 115 | a->maybe_macro = 0; |
06a604e6 | 116 | a->maybe_real = 0; |
d0bfd026 | 117 | git_attr_hash[pos] = a; |
f48fd688 | 118 | |
7d42ec54 JH |
119 | /* |
120 | * NEEDSWORK: per git_attr_check check_all_attr | |
121 | * will be initialized a lot more lazily, not | |
122 | * like this, and not here. | |
123 | */ | |
2756ca43 | 124 | REALLOC_ARRAY(check_all_attr, attr_nr); |
f48fd688 | 125 | check_all_attr[a->attr_nr].attr = a; |
515106fa | 126 | check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; |
d0bfd026 JH |
127 | return a; |
128 | } | |
129 | ||
7fb0eaa2 JH |
130 | struct git_attr *git_attr(const char *name) |
131 | { | |
132 | return git_attr_internal(name, strlen(name)); | |
133 | } | |
134 | ||
515106fa | 135 | /* What does a matched pattern decide? */ |
d0bfd026 | 136 | struct attr_state { |
d0bfd026 | 137 | struct git_attr *attr; |
a5e92abd | 138 | const char *setto; |
d0bfd026 JH |
139 | }; |
140 | ||
82dce998 NTND |
141 | struct pattern { |
142 | const char *pattern; | |
143 | int patternlen; | |
144 | int nowildcardlen; | |
f8708998 | 145 | unsigned flags; /* EXC_FLAG_* */ |
82dce998 NTND |
146 | }; |
147 | ||
ba845b75 MH |
148 | /* |
149 | * One rule, as from a .gitattributes file. | |
150 | * | |
151 | * If is_macro is true, then u.attr is a pointer to the git_attr being | |
152 | * defined. | |
153 | * | |
4894f4ff JH |
154 | * If is_macro is false, then u.pat is the filename pattern to which the |
155 | * rule applies. | |
ba845b75 MH |
156 | * |
157 | * In either case, num_attr is the number of attributes affected by | |
158 | * this rule, and state is an array listing them. The attributes are | |
159 | * listed as they appear in the file (macros unexpanded). | |
160 | */ | |
d0bfd026 | 161 | struct match_attr { |
f48fd688 | 162 | union { |
82dce998 | 163 | struct pattern pat; |
f48fd688 JH |
164 | struct git_attr *attr; |
165 | } u; | |
166 | char is_macro; | |
d0bfd026 JH |
167 | unsigned num_attr; |
168 | struct attr_state state[FLEX_ARRAY]; | |
169 | }; | |
170 | ||
171 | static const char blank[] = " \t\r\n"; | |
172 | ||
d1751298 MH |
173 | /* |
174 | * Parse a whitespace-delimited attribute state (i.e., "attr", | |
175 | * "-attr", "!attr", or "attr=value") from the string starting at src. | |
176 | * If e is not NULL, write the results to *e. Return a pointer to the | |
177 | * remainder of the string (with leading whitespace removed), or NULL | |
178 | * if there was an error. | |
179 | */ | |
515106fa | 180 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
d1751298 | 181 | struct attr_state *e) |
515106fa JH |
182 | { |
183 | const char *ep, *equals; | |
184 | int len; | |
185 | ||
186 | ep = cp + strcspn(cp, blank); | |
187 | equals = strchr(cp, '='); | |
188 | if (equals && ep < equals) | |
189 | equals = NULL; | |
190 | if (equals) | |
191 | len = equals - cp; | |
192 | else | |
193 | len = ep - cp; | |
d1751298 | 194 | if (!e) { |
515106fa JH |
195 | if (*cp == '-' || *cp == '!') { |
196 | cp++; | |
197 | len--; | |
198 | } | |
199 | if (invalid_attr_name(cp, len)) { | |
200 | fprintf(stderr, | |
201 | "%.*s is not a valid attribute name: %s:%d\n", | |
202 | len, cp, src, lineno); | |
203 | return NULL; | |
204 | } | |
205 | } else { | |
5a884019 JH |
206 | /* |
207 | * As this function is always called twice, once with | |
208 | * e == NULL in the first pass and then e != NULL in | |
209 | * the second pass, no need for invalid_attr_name() | |
210 | * check here. | |
211 | */ | |
515106fa JH |
212 | if (*cp == '-' || *cp == '!') { |
213 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; | |
214 | cp++; | |
215 | len--; | |
216 | } | |
217 | else if (!equals) | |
218 | e->setto = ATTR__TRUE; | |
219 | else { | |
182af834 | 220 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
515106fa | 221 | } |
7fb0eaa2 | 222 | e->attr = git_attr_internal(cp, len); |
515106fa | 223 | } |
515106fa JH |
224 | return ep + strspn(ep, blank); |
225 | } | |
226 | ||
f48fd688 JH |
227 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
228 | int lineno, int macro_ok) | |
d0bfd026 JH |
229 | { |
230 | int namelen; | |
d68e1c18 | 231 | int num_attr, i; |
85c4a0d0 | 232 | const char *cp, *name, *states; |
515106fa | 233 | struct match_attr *res = NULL; |
f48fd688 | 234 | int is_macro; |
860a74d9 | 235 | struct strbuf pattern = STRBUF_INIT; |
d0bfd026 JH |
236 | |
237 | cp = line + strspn(line, blank); | |
238 | if (!*cp || *cp == '#') | |
239 | return NULL; | |
240 | name = cp; | |
860a74d9 NTND |
241 | |
242 | if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) { | |
243 | name = pattern.buf; | |
244 | namelen = pattern.len; | |
245 | } else { | |
246 | namelen = strcspn(name, blank); | |
247 | states = name + namelen; | |
248 | } | |
249 | ||
f48fd688 | 250 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
59556548 | 251 | starts_with(name, ATTRIBUTE_MACRO_PREFIX)) { |
f48fd688 JH |
252 | if (!macro_ok) { |
253 | fprintf(stderr, "%s not allowed: %s:%d\n", | |
254 | name, src, lineno); | |
62af8969 | 255 | goto fail_return; |
f48fd688 JH |
256 | } |
257 | is_macro = 1; | |
258 | name += strlen(ATTRIBUTE_MACRO_PREFIX); | |
259 | name += strspn(name, blank); | |
260 | namelen = strcspn(name, blank); | |
e4aee10a JH |
261 | if (invalid_attr_name(name, namelen)) { |
262 | fprintf(stderr, | |
263 | "%.*s is not a valid attribute name: %s:%d\n", | |
264 | namelen, name, src, lineno); | |
62af8969 | 265 | goto fail_return; |
e4aee10a | 266 | } |
f48fd688 JH |
267 | } |
268 | else | |
269 | is_macro = 0; | |
d0bfd026 | 270 | |
85c4a0d0 MH |
271 | states += strspn(states, blank); |
272 | ||
d68e1c18 MH |
273 | /* First pass to count the attr_states */ |
274 | for (cp = states, num_attr = 0; *cp; num_attr++) { | |
275 | cp = parse_attr(src, lineno, cp, NULL); | |
276 | if (!cp) | |
62af8969 | 277 | goto fail_return; |
d0bfd026 | 278 | } |
d68e1c18 MH |
279 | |
280 | res = xcalloc(1, | |
281 | sizeof(*res) + | |
282 | sizeof(struct attr_state) * num_attr + | |
283 | (is_macro ? 0 : namelen + 1)); | |
fad32bcd | 284 | if (is_macro) { |
d68e1c18 | 285 | res->u.attr = git_attr_internal(name, namelen); |
fad32bcd NTND |
286 | res->u.attr->maybe_macro = 1; |
287 | } else { | |
82dce998 NTND |
288 | char *p = (char *)&(res->state[num_attr]); |
289 | memcpy(p, name, namelen); | |
290 | res->u.pat.pattern = p; | |
291 | parse_exclude_pattern(&res->u.pat.pattern, | |
292 | &res->u.pat.patternlen, | |
293 | &res->u.pat.flags, | |
294 | &res->u.pat.nowildcardlen); | |
8b1bd024 TR |
295 | if (res->u.pat.flags & EXC_FLAG_NEGATIVE) { |
296 | warning(_("Negative patterns are ignored in git attributes\n" | |
297 | "Use '\\!' for literal leading exclamation.")); | |
62af8969 | 298 | goto fail_return; |
8b1bd024 | 299 | } |
d0bfd026 | 300 | } |
d68e1c18 MH |
301 | res->is_macro = is_macro; |
302 | res->num_attr = num_attr; | |
303 | ||
304 | /* Second pass to fill the attr_states */ | |
305 | for (cp = states, i = 0; *cp; i++) { | |
306 | cp = parse_attr(src, lineno, cp, &(res->state[i])); | |
06a604e6 NTND |
307 | if (!is_macro) |
308 | res->state[i].attr->maybe_real = 1; | |
309 | if (res->state[i].attr->maybe_macro) | |
310 | cannot_trust_maybe_real = 1; | |
d68e1c18 MH |
311 | } |
312 | ||
860a74d9 | 313 | strbuf_release(&pattern); |
d0bfd026 | 314 | return res; |
62af8969 JH |
315 | |
316 | fail_return: | |
860a74d9 | 317 | strbuf_release(&pattern); |
62af8969 JH |
318 | free(res); |
319 | return NULL; | |
d0bfd026 JH |
320 | } |
321 | ||
322 | /* | |
323 | * Like info/exclude and .gitignore, the attribute information can | |
324 | * come from many places. | |
325 | * | |
326 | * (1) .gitattribute file of the same directory; | |
515106fa JH |
327 | * (2) .gitattribute file of the parent directory if (1) does not have |
328 | * any match; this goes recursively upwards, just like .gitignore. | |
329 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. | |
d0bfd026 JH |
330 | * |
331 | * In the same file, later entries override the earlier match, so in the | |
332 | * global list, we would have entries from info/attributes the earliest | |
333 | * (reading the file from top to bottom), .gitattribute of the root | |
334 | * directory (again, reading the file from top to bottom) down to the | |
335 | * current directory, and then scan the list backwards to find the first match. | |
6d24e7a8 | 336 | * This is exactly the same as what is_excluded() does in dir.c to deal with |
6f416cba | 337 | * .gitignore file and info/excludes file as a fallback. |
d0bfd026 JH |
338 | */ |
339 | ||
7d42ec54 | 340 | /* NEEDSWORK: This will become per git_attr_check */ |
d0bfd026 JH |
341 | static struct attr_stack { |
342 | struct attr_stack *prev; | |
343 | char *origin; | |
cd6a0b26 | 344 | size_t originlen; |
d0bfd026 | 345 | unsigned num_matches; |
a4413118 | 346 | unsigned alloc; |
d0bfd026 JH |
347 | struct match_attr **attrs; |
348 | } *attr_stack; | |
349 | ||
350 | static void free_attr_elem(struct attr_stack *e) | |
351 | { | |
352 | int i; | |
353 | free(e->origin); | |
515106fa JH |
354 | for (i = 0; i < e->num_matches; i++) { |
355 | struct match_attr *a = e->attrs[i]; | |
356 | int j; | |
357 | for (j = 0; j < a->num_attr; j++) { | |
a5e92abd | 358 | const char *setto = a->state[j].setto; |
515106fa JH |
359 | if (setto == ATTR__TRUE || |
360 | setto == ATTR__FALSE || | |
361 | setto == ATTR__UNSET || | |
362 | setto == ATTR__UNKNOWN) | |
363 | ; | |
364 | else | |
4b25d091 | 365 | free((char *) setto); |
515106fa JH |
366 | } |
367 | free(a); | |
368 | } | |
37475f97 | 369 | free(e->attrs); |
d0bfd026 JH |
370 | free(e); |
371 | } | |
372 | ||
37293768 JH |
373 | struct attr_check *attr_check_alloc(void) |
374 | { | |
375 | return xcalloc(1, sizeof(struct attr_check)); | |
376 | } | |
377 | ||
378 | struct attr_check *attr_check_initl(const char *one, ...) | |
379 | { | |
380 | struct attr_check *check; | |
381 | int cnt; | |
382 | va_list params; | |
383 | const char *param; | |
384 | ||
385 | va_start(params, one); | |
386 | for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++) | |
387 | ; | |
388 | va_end(params); | |
389 | ||
390 | check = attr_check_alloc(); | |
391 | check->nr = cnt; | |
392 | check->alloc = cnt; | |
393 | check->items = xcalloc(cnt, sizeof(struct attr_check_item)); | |
394 | ||
395 | check->items[0].attr = git_attr(one); | |
396 | va_start(params, one); | |
397 | for (cnt = 1; cnt < check->nr; cnt++) { | |
398 | const struct git_attr *attr; | |
399 | param = va_arg(params, const char *); | |
400 | if (!param) | |
401 | die("BUG: counted %d != ended at %d", | |
402 | check->nr, cnt); | |
403 | attr = git_attr(param); | |
404 | if (!attr) | |
405 | die("BUG: %s: not a valid attribute name", param); | |
406 | check->items[cnt].attr = attr; | |
407 | } | |
408 | va_end(params); | |
409 | return check; | |
410 | } | |
411 | ||
412 | struct attr_check_item *attr_check_append(struct attr_check *check, | |
413 | const struct git_attr *attr) | |
414 | { | |
415 | struct attr_check_item *item; | |
416 | ||
417 | ALLOC_GROW(check->items, check->nr + 1, check->alloc); | |
418 | item = &check->items[check->nr++]; | |
419 | item->attr = attr; | |
420 | return item; | |
421 | } | |
422 | ||
423 | void attr_check_reset(struct attr_check *check) | |
424 | { | |
425 | check->nr = 0; | |
426 | } | |
427 | ||
428 | void attr_check_clear(struct attr_check *check) | |
429 | { | |
430 | free(check->items); | |
431 | check->items = NULL; | |
432 | check->alloc = 0; | |
433 | check->nr = 0; | |
434 | } | |
435 | ||
436 | void attr_check_free(struct attr_check *check) | |
437 | { | |
438 | attr_check_clear(check); | |
439 | free(check); | |
440 | } | |
441 | ||
d0bfd026 | 442 | static const char *builtin_attr[] = { |
155a4b71 | 443 | "[attr]binary -diff -merge -text", |
d0bfd026 JH |
444 | NULL, |
445 | }; | |
446 | ||
a4413118 JH |
447 | static void handle_attr_line(struct attr_stack *res, |
448 | const char *line, | |
449 | const char *src, | |
450 | int lineno, | |
451 | int macro_ok) | |
452 | { | |
453 | struct match_attr *a; | |
454 | ||
455 | a = parse_attr_line(line, src, lineno, macro_ok); | |
456 | if (!a) | |
457 | return; | |
3a7fa03d | 458 | ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc); |
a4413118 JH |
459 | res->attrs[res->num_matches++] = a; |
460 | } | |
461 | ||
d0bfd026 JH |
462 | static struct attr_stack *read_attr_from_array(const char **list) |
463 | { | |
464 | struct attr_stack *res; | |
465 | const char *line; | |
f48fd688 | 466 | int lineno = 0; |
d0bfd026 JH |
467 | |
468 | res = xcalloc(1, sizeof(*res)); | |
a4413118 JH |
469 | while ((line = *(list++)) != NULL) |
470 | handle_attr_line(res, line, "[builtin]", ++lineno, 1); | |
d0bfd026 JH |
471 | return res; |
472 | } | |
473 | ||
7d42ec54 JH |
474 | /* |
475 | * NEEDSWORK: these two are tricky. The callers assume there is a | |
476 | * single, system-wide global state "where we read attributes from?" | |
477 | * and when the state is flipped by calling git_attr_set_direction(), | |
478 | * attr_stack is discarded so that subsequent attr_check will lazily | |
479 | * read from the right place. And they do not know or care who called | |
480 | * by them uses the attribute subsystem, hence have no knowledge of | |
481 | * existing git_attr_check instances or future ones that will be | |
482 | * created). | |
483 | * | |
484 | * Probably we need a thread_local that holds these two variables, | |
485 | * and a list of git_attr_check instances (which need to be maintained | |
486 | * by hooking into git_attr_check_alloc(), git_attr_check_initl(), and | |
487 | * git_attr_check_clear(). Then git_attr_set_direction() updates the | |
488 | * fields in that thread_local for these two variables, iterate over | |
489 | * all the active git_attr_check instances and discard the attr_stack | |
490 | * they hold. Yuck, but it sounds doable. | |
491 | */ | |
06f33c17 JH |
492 | static enum git_attr_direction direction; |
493 | static struct index_state *use_index; | |
494 | ||
f48fd688 | 495 | static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) |
d0bfd026 | 496 | { |
a4413118 | 497 | FILE *fp = fopen(path, "r"); |
d0bfd026 JH |
498 | struct attr_stack *res; |
499 | char buf[2048]; | |
f48fd688 | 500 | int lineno = 0; |
d0bfd026 | 501 | |
11e50b27 | 502 | if (!fp) { |
8e950dab | 503 | if (errno != ENOENT && errno != ENOTDIR) |
55b38a48 | 504 | warn_on_inaccessible(path); |
a4413118 | 505 | return NULL; |
11e50b27 | 506 | } |
a4413118 | 507 | res = xcalloc(1, sizeof(*res)); |
27547e5f JH |
508 | while (fgets(buf, sizeof(buf), fp)) { |
509 | char *bufp = buf; | |
510 | if (!lineno) | |
511 | skip_utf8_bom(&bufp, strlen(bufp)); | |
512 | handle_attr_line(res, bufp, path, ++lineno, macro_ok); | |
513 | } | |
a4413118 JH |
514 | fclose(fp); |
515 | return res; | |
516 | } | |
d0bfd026 | 517 | |
06f33c17 | 518 | static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) |
a4413118 JH |
519 | { |
520 | struct attr_stack *res; | |
1a9d7e9b JH |
521 | char *buf, *sp; |
522 | int lineno = 0; | |
f48fd688 | 523 | |
ff366825 | 524 | buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL); |
1a9d7e9b | 525 | if (!buf) |
06f33c17 | 526 | return NULL; |
1a9d7e9b | 527 | |
06f33c17 | 528 | res = xcalloc(1, sizeof(*res)); |
1a9d7e9b JH |
529 | for (sp = buf; *sp; ) { |
530 | char *ep; | |
531 | int more; | |
263434f2 JH |
532 | |
533 | ep = strchrnul(sp, '\n'); | |
1a9d7e9b JH |
534 | more = (*ep == '\n'); |
535 | *ep = '\0'; | |
536 | handle_attr_line(res, sp, path, ++lineno, macro_ok); | |
537 | sp = ep + more; | |
538 | } | |
539 | free(buf); | |
d0bfd026 JH |
540 | return res; |
541 | } | |
542 | ||
06f33c17 JH |
543 | static struct attr_stack *read_attr(const char *path, int macro_ok) |
544 | { | |
545 | struct attr_stack *res; | |
546 | ||
547 | if (direction == GIT_ATTR_CHECKOUT) { | |
548 | res = read_attr_from_index(path, macro_ok); | |
549 | if (!res) | |
550 | res = read_attr_from_file(path, macro_ok); | |
551 | } | |
4191e80a | 552 | else if (direction == GIT_ATTR_CHECKIN) { |
06f33c17 JH |
553 | res = read_attr_from_file(path, macro_ok); |
554 | if (!res) | |
555 | /* | |
556 | * There is no checked out .gitattributes file there, but | |
557 | * we might have it in the index. We allow operation in a | |
558 | * sparsely checked out work tree, so read from it. | |
559 | */ | |
560 | res = read_attr_from_index(path, macro_ok); | |
561 | } | |
4191e80a NTND |
562 | else |
563 | res = read_attr_from_index(path, macro_ok); | |
06f33c17 JH |
564 | if (!res) |
565 | res = xcalloc(1, sizeof(*res)); | |
566 | return res; | |
567 | } | |
568 | ||
d0bfd026 JH |
569 | #if DEBUG_ATTR |
570 | static void debug_info(const char *what, struct attr_stack *elem) | |
571 | { | |
572 | fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); | |
573 | } | |
cf94ccda | 574 | static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v) |
f48fd688 | 575 | { |
515106fa JH |
576 | const char *value = v; |
577 | ||
578 | if (ATTR_TRUE(value)) | |
579 | value = "set"; | |
580 | else if (ATTR_FALSE(value)) | |
581 | value = "unset"; | |
582 | else if (ATTR_UNSET(value)) | |
583 | value = "unspecified"; | |
584 | ||
585 | fprintf(stderr, "%s: %s => %s (%s)\n", | |
586 | what, attr->name, (char *) value, match); | |
f48fd688 | 587 | } |
d0bfd026 JH |
588 | #define debug_push(a) debug_info("push", (a)) |
589 | #define debug_pop(a) debug_info("pop", (a)) | |
590 | #else | |
591 | #define debug_push(a) do { ; } while (0) | |
592 | #define debug_pop(a) do { ; } while (0) | |
f48fd688 | 593 | #define debug_set(a,b,c,d) do { ; } while (0) |
034d35c9 | 594 | #endif /* DEBUG_ATTR */ |
d0bfd026 | 595 | |
06f33c17 JH |
596 | static void drop_attr_stack(void) |
597 | { | |
598 | while (attr_stack) { | |
599 | struct attr_stack *elem = attr_stack; | |
600 | attr_stack = elem->prev; | |
601 | free_attr_elem(elem); | |
602 | } | |
603 | } | |
604 | ||
c5147722 | 605 | static const char *git_etc_gitattributes(void) |
6df42ab9 PO |
606 | { |
607 | static const char *system_wide; | |
608 | if (!system_wide) | |
609 | system_wide = system_path(ETC_GITATTRIBUTES); | |
610 | return system_wide; | |
611 | } | |
612 | ||
c5147722 | 613 | static int git_attr_system(void) |
6df42ab9 PO |
614 | { |
615 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); | |
616 | } | |
617 | ||
f932729c JK |
618 | static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE) |
619 | ||
4c0ce074 JH |
620 | static void push_stack(struct attr_stack **attr_stack_p, |
621 | struct attr_stack *elem, char *origin, size_t originlen) | |
622 | { | |
623 | if (elem) { | |
624 | elem->origin = origin; | |
625 | if (origin) | |
626 | elem->originlen = originlen; | |
627 | elem->prev = *attr_stack_p; | |
628 | *attr_stack_p = elem; | |
629 | } | |
630 | } | |
631 | ||
f48fd688 JH |
632 | static void bootstrap_attr_stack(void) |
633 | { | |
909ca7b9 | 634 | struct attr_stack *elem; |
f48fd688 | 635 | |
909ca7b9 JH |
636 | if (attr_stack) |
637 | return; | |
f48fd688 | 638 | |
4c0ce074 JH |
639 | push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0); |
640 | ||
641 | if (git_attr_system()) | |
642 | push_stack(&attr_stack, | |
643 | read_attr_from_file(git_etc_gitattributes(), 1), | |
644 | NULL, 0); | |
6df42ab9 | 645 | |
2527bbce PT |
646 | if (!git_attributes_file) |
647 | git_attributes_file = xdg_config_home("attributes"); | |
4c0ce074 JH |
648 | if (git_attributes_file) |
649 | push_stack(&attr_stack, | |
650 | read_attr_from_file(git_attributes_file, 1), | |
651 | NULL, 0); | |
f48fd688 | 652 | |
909ca7b9 JH |
653 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
654 | elem = read_attr(GITATTRIBUTES_FILE, 1); | |
4c0ce074 | 655 | push_stack(&attr_stack, elem, xstrdup(""), 0); |
909ca7b9 | 656 | debug_push(elem); |
f48fd688 | 657 | } |
909ca7b9 | 658 | |
f0056f64 JK |
659 | if (startup_info->have_repository) |
660 | elem = read_attr_from_file(git_path_info_attributes(), 1); | |
661 | else | |
662 | elem = NULL; | |
663 | ||
909ca7b9 JH |
664 | if (!elem) |
665 | elem = xcalloc(1, sizeof(*elem)); | |
4c0ce074 | 666 | push_stack(&attr_stack, elem, NULL, 0); |
f48fd688 JH |
667 | } |
668 | ||
9db9eecf | 669 | static void prepare_attr_stack(const char *path, int dirlen) |
d0bfd026 JH |
670 | { |
671 | struct attr_stack *elem, *info; | |
a8727017 MH |
672 | const char *cp; |
673 | ||
d0bfd026 JH |
674 | /* |
675 | * At the bottom of the attribute stack is the built-in | |
6df42ab9 PO |
676 | * set of attribute definitions, followed by the contents |
677 | * of $(prefix)/etc/gitattributes and a file specified by | |
678 | * core.attributesfile. Then, contents from | |
d0bfd026 JH |
679 | * .gitattribute files from directories closer to the |
680 | * root to the ones in deeper directories are pushed | |
681 | * to the stack. Finally, at the very top of the stack | |
682 | * we always keep the contents of $GIT_DIR/info/attributes. | |
683 | * | |
684 | * When checking, we use entries from near the top of the | |
685 | * stack, preferring $GIT_DIR/info/attributes, then | |
686 | * .gitattributes in deeper directories to shallower ones, | |
687 | * and finally use the built-in set as the default. | |
688 | */ | |
7373eab4 | 689 | bootstrap_attr_stack(); |
d0bfd026 JH |
690 | |
691 | /* | |
692 | * Pop the "info" one that is always at the top of the stack. | |
693 | */ | |
694 | info = attr_stack; | |
695 | attr_stack = info->prev; | |
696 | ||
697 | /* | |
698 | * Pop the ones from directories that are not the prefix of | |
c432ef99 JH |
699 | * the path we are checking. Break out of the loop when we see |
700 | * the root one (whose origin is an empty string "") or the builtin | |
701 | * one (whose origin is NULL) without popping it. | |
d0bfd026 | 702 | */ |
77f7f822 | 703 | while (attr_stack->origin) { |
d0bfd026 JH |
704 | int namelen = strlen(attr_stack->origin); |
705 | ||
706 | elem = attr_stack; | |
707 | if (namelen <= dirlen && | |
1afca444 JK |
708 | !strncmp(elem->origin, path, namelen) && |
709 | (!namelen || path[namelen] == '/')) | |
d0bfd026 JH |
710 | break; |
711 | ||
712 | debug_pop(elem); | |
713 | attr_stack = elem->prev; | |
714 | free_attr_elem(elem); | |
715 | } | |
716 | ||
717 | /* | |
718 | * Read from parent directories and push them down | |
719 | */ | |
4191e80a | 720 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
c432ef99 JH |
721 | /* |
722 | * bootstrap_attr_stack() should have added, and the | |
723 | * above loop should have stopped before popping, the | |
724 | * root element whose attr_stack->origin is set to an | |
725 | * empty string. | |
726 | */ | |
97410b27 | 727 | struct strbuf pathbuf = STRBUF_INIT; |
2d35d556 | 728 | |
c432ef99 | 729 | assert(attr_stack->origin); |
97410b27 | 730 | while (1) { |
4c0ce074 JH |
731 | size_t len = strlen(attr_stack->origin); |
732 | char *origin; | |
733 | ||
2d35d556 RS |
734 | if (dirlen <= len) |
735 | break; | |
97410b27 BC |
736 | cp = memchr(path + len + 1, '/', dirlen - len - 1); |
737 | if (!cp) | |
738 | cp = path + dirlen; | |
4c0ce074 JH |
739 | strbuf_addf(&pathbuf, |
740 | "%.*s/%s", (int)(cp - path), path, | |
741 | GITATTRIBUTES_FILE); | |
f66cf96d | 742 | elem = read_attr(pathbuf.buf, 0); |
97410b27 | 743 | strbuf_setlen(&pathbuf, cp - path); |
4c0ce074 JH |
744 | origin = strbuf_detach(&pathbuf, &len); |
745 | push_stack(&attr_stack, elem, origin, len); | |
2d35d556 RS |
746 | debug_push(elem); |
747 | } | |
d0bfd026 | 748 | |
97410b27 BC |
749 | strbuf_release(&pathbuf); |
750 | } | |
d4c98565 | 751 | |
d0bfd026 JH |
752 | /* |
753 | * Finally push the "info" one at the top of the stack. | |
754 | */ | |
4c0ce074 | 755 | push_stack(&attr_stack, info, NULL, 0); |
d0bfd026 JH |
756 | } |
757 | ||
758 | static int path_matches(const char *pathname, int pathlen, | |
bd2f371d | 759 | int basename_offset, |
82dce998 | 760 | const struct pattern *pat, |
d0bfd026 JH |
761 | const char *base, int baselen) |
762 | { | |
82dce998 NTND |
763 | const char *pattern = pat->pattern; |
764 | int prefix = pat->nowildcardlen; | |
dc09e9ec | 765 | int isdir = (pathlen && pathname[pathlen - 1] == '/'); |
82dce998 | 766 | |
dc09e9ec | 767 | if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir) |
94bc671a JNA |
768 | return 0; |
769 | ||
82dce998 | 770 | if (pat->flags & EXC_FLAG_NODIR) { |
bd2f371d | 771 | return match_basename(pathname + basename_offset, |
dc09e9ec | 772 | pathlen - basename_offset - isdir, |
82dce998 NTND |
773 | pattern, prefix, |
774 | pat->patternlen, pat->flags); | |
d0bfd026 | 775 | } |
dc09e9ec | 776 | return match_pathname(pathname, pathlen - isdir, |
82dce998 NTND |
777 | base, baselen, |
778 | pattern, prefix, pat->patternlen, pat->flags); | |
d0bfd026 JH |
779 | } |
780 | ||
ec775c41 HG |
781 | static int macroexpand_one(int attr_nr, int rem); |
782 | ||
515106fa JH |
783 | static int fill_one(const char *what, struct match_attr *a, int rem) |
784 | { | |
7bd18054 | 785 | struct attr_check_item *check = check_all_attr; |
515106fa JH |
786 | int i; |
787 | ||
969f9d73 | 788 | for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) { |
515106fa | 789 | struct git_attr *attr = a->state[i].attr; |
a5e92abd JH |
790 | const char **n = &(check[attr->attr_nr].value); |
791 | const char *v = a->state[i].setto; | |
515106fa JH |
792 | |
793 | if (*n == ATTR__UNKNOWN) { | |
426c27b7 | 794 | debug_set(what, |
712efb1a | 795 | a->is_macro ? a->u.attr->name : a->u.pat.pattern, |
426c27b7 | 796 | attr, v); |
515106fa JH |
797 | *n = v; |
798 | rem--; | |
ec775c41 | 799 | rem = macroexpand_one(attr->attr_nr, rem); |
515106fa JH |
800 | } |
801 | } | |
802 | return rem; | |
803 | } | |
804 | ||
bd2f371d | 805 | static int fill(const char *path, int pathlen, int basename_offset, |
4742d136 | 806 | struct attr_stack *stk, int rem) |
d0bfd026 | 807 | { |
515106fa | 808 | int i; |
d0bfd026 JH |
809 | const char *base = stk->origin ? stk->origin : ""; |
810 | ||
811 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { | |
812 | struct match_attr *a = stk->attrs[i]; | |
f48fd688 JH |
813 | if (a->is_macro) |
814 | continue; | |
bd2f371d | 815 | if (path_matches(path, pathlen, basename_offset, |
82dce998 | 816 | &a->u.pat, base, stk->originlen)) |
515106fa | 817 | rem = fill_one("fill", a, rem); |
d0bfd026 JH |
818 | } |
819 | return rem; | |
820 | } | |
821 | ||
aa7710e0 | 822 | static int macroexpand_one(int nr, int rem) |
f48fd688 | 823 | { |
ec775c41 | 824 | struct attr_stack *stk; |
515106fa | 825 | int i; |
f48fd688 | 826 | |
fad32bcd NTND |
827 | if (check_all_attr[nr].value != ATTR__TRUE || |
828 | !check_all_attr[nr].attr->maybe_macro) | |
ec775c41 HG |
829 | return rem; |
830 | ||
4b0c6961 JH |
831 | for (stk = attr_stack; stk; stk = stk->prev) { |
832 | for (i = stk->num_matches - 1; 0 <= i; i--) { | |
ec775c41 HG |
833 | struct match_attr *ma = stk->attrs[i]; |
834 | if (!ma->is_macro) | |
835 | continue; | |
aa7710e0 | 836 | if (ma->u.attr->attr_nr == nr) |
4b0c6961 | 837 | return fill_one("expand", ma, rem); |
ec775c41 | 838 | } |
4b0c6961 | 839 | } |
ec775c41 | 840 | |
f48fd688 JH |
841 | return rem; |
842 | } | |
843 | ||
2d721744 | 844 | /* |
06a604e6 NTND |
845 | * Collect attributes for path into the array pointed to by |
846 | * check_all_attr. If num is non-zero, only attributes in check[] are | |
847 | * collected. Otherwise all attributes are collected. | |
2d721744 | 848 | */ |
06a604e6 | 849 | static void collect_some_attrs(const char *path, int num, |
7bd18054 | 850 | struct attr_check_item *check) |
06a604e6 | 851 | |
d0bfd026 JH |
852 | { |
853 | struct attr_stack *stk; | |
9db9eecf | 854 | int i, pathlen, rem, dirlen; |
bd2f371d JH |
855 | const char *cp, *last_slash = NULL; |
856 | int basename_offset; | |
9db9eecf NTND |
857 | |
858 | for (cp = path; *cp; cp++) { | |
859 | if (*cp == '/' && cp[1]) | |
860 | last_slash = cp; | |
861 | } | |
862 | pathlen = cp - path; | |
863 | if (last_slash) { | |
bd2f371d | 864 | basename_offset = last_slash + 1 - path; |
9db9eecf NTND |
865 | dirlen = last_slash - path; |
866 | } else { | |
bd2f371d | 867 | basename_offset = 0; |
9db9eecf NTND |
868 | dirlen = 0; |
869 | } | |
d0bfd026 | 870 | |
9db9eecf | 871 | prepare_attr_stack(path, dirlen); |
f48fd688 | 872 | for (i = 0; i < attr_nr; i++) |
515106fa | 873 | check_all_attr[i].value = ATTR__UNKNOWN; |
06a604e6 NTND |
874 | if (num && !cannot_trust_maybe_real) { |
875 | rem = 0; | |
876 | for (i = 0; i < num; i++) { | |
877 | if (!check[i].attr->maybe_real) { | |
7bd18054 | 878 | struct attr_check_item *c; |
06a604e6 NTND |
879 | c = check_all_attr + check[i].attr->attr_nr; |
880 | c->value = ATTR__UNSET; | |
881 | rem++; | |
882 | } | |
883 | } | |
884 | if (rem == num) | |
885 | return; | |
886 | } | |
d0bfd026 | 887 | |
f48fd688 JH |
888 | rem = attr_nr; |
889 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) | |
bd2f371d | 890 | rem = fill(path, pathlen, basename_offset, stk, rem); |
2d721744 MH |
891 | } |
892 | ||
7bd18054 | 893 | int git_check_attrs(const char *path, int num, struct attr_check_item *check) |
2d721744 MH |
894 | { |
895 | int i; | |
896 | ||
06a604e6 | 897 | collect_some_attrs(path, num, check); |
f48fd688 | 898 | |
515106fa | 899 | for (i = 0; i < num; i++) { |
a5e92abd | 900 | const char *value = check_all_attr[check[i].attr->attr_nr].value; |
515106fa JH |
901 | if (value == ATTR__UNKNOWN) |
902 | value = ATTR__UNSET; | |
903 | check[i].value = value; | |
904 | } | |
f48fd688 | 905 | |
d0bfd026 JH |
906 | return 0; |
907 | } | |
06f33c17 | 908 | |
7bd18054 | 909 | int git_all_attrs(const char *path, int *num, struct attr_check_item **check) |
ee548df3 MH |
910 | { |
911 | int i, count, j; | |
912 | ||
06a604e6 | 913 | collect_some_attrs(path, 0, NULL); |
ee548df3 MH |
914 | |
915 | /* Count the number of attributes that are set. */ | |
916 | count = 0; | |
917 | for (i = 0; i < attr_nr; i++) { | |
918 | const char *value = check_all_attr[i].value; | |
919 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) | |
920 | ++count; | |
921 | } | |
922 | *num = count; | |
b32fa95f | 923 | ALLOC_ARRAY(*check, count); |
ee548df3 MH |
924 | j = 0; |
925 | for (i = 0; i < attr_nr; i++) { | |
926 | const char *value = check_all_attr[i].value; | |
927 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) { | |
928 | (*check)[j].attr = check_all_attr[i].attr; | |
929 | (*check)[j].value = value; | |
930 | ++j; | |
931 | } | |
932 | } | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
37293768 JH |
937 | int git_check_attr(const char *path, struct attr_check *check) |
938 | { | |
939 | return git_check_attrs(path, check->nr, check->items); | |
940 | } | |
941 | ||
06f33c17 JH |
942 | void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate) |
943 | { | |
944 | enum git_attr_direction old = direction; | |
4191e80a NTND |
945 | |
946 | if (is_bare_repository() && new != GIT_ATTR_INDEX) | |
947 | die("BUG: non-INDEX attr direction in a bare repo"); | |
948 | ||
06f33c17 JH |
949 | direction = new; |
950 | if (new != old) | |
951 | drop_attr_stack(); | |
952 | use_index = istate; | |
953 | } |