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