]>
Commit | Line | Data |
---|---|---|
453ec4bd LT |
1 | /* |
2 | * This handles recursive filename detection with exclude | |
3 | * files, index knowledge etc.. | |
4 | * | |
95a68344 AS |
5 | * See Documentation/technical/api-directory-listing.txt |
6 | * | |
453ec4bd LT |
7 | * Copyright (C) Linus Torvalds, 2005-2006 |
8 | * Junio Hamano, 2005-2006 | |
9 | */ | |
453ec4bd LT |
10 | #include "cache.h" |
11 | #include "dir.h" | |
09595258 | 12 | #include "refs.h" |
237ec6e4 | 13 | #include "wildmatch.h" |
64acde94 | 14 | #include "pathspec.h" |
dde843e7 | 15 | #include "utf8.h" |
83c094ad NTND |
16 | #include "varint.h" |
17 | #include "ewah/ewok.h" | |
453ec4bd | 18 | |
9fc42d60 LT |
19 | struct path_simplify { |
20 | int len; | |
21 | const char *path; | |
22 | }; | |
23 | ||
defd7c7b KB |
24 | /* |
25 | * Tells read_directory_recursive how a file or directory should be treated. | |
26 | * Values are ordered by significance, e.g. if a directory contains both | |
27 | * excluded and untracked files, it is listed as untracked because | |
28 | * path_untracked > path_excluded. | |
29 | */ | |
30 | enum path_treatment { | |
31 | path_none = 0, | |
32 | path_recurse, | |
33 | path_excluded, | |
34 | path_untracked | |
35 | }; | |
36 | ||
cf7c6148 NTND |
37 | /* |
38 | * Support data structure for our opendir/readdir/closedir wrappers | |
39 | */ | |
40 | struct cached_dir { | |
41 | DIR *fdir; | |
42 | struct untracked_cache_dir *untracked; | |
91a2288b NTND |
43 | int nr_files; |
44 | int nr_dirs; | |
45 | ||
cf7c6148 | 46 | struct dirent *de; |
91a2288b NTND |
47 | const char *file; |
48 | struct untracked_cache_dir *ucd; | |
cf7c6148 NTND |
49 | }; |
50 | ||
defd7c7b | 51 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
0dcb8d7f | 52 | const char *path, int len, struct untracked_cache_dir *untracked, |
09595258 | 53 | int check_only, const struct path_simplify *simplify); |
caa6b782 | 54 | static int get_dtype(struct dirent *de, const char *path, int len); |
09595258 | 55 | |
bac65a2b NTND |
56 | static struct trace_key trace_exclude = TRACE_KEY_INIT(EXCLUDE); |
57 | ||
8cf2a84e JJ |
58 | /* helper string functions with support for the ignore_case flag */ |
59 | int strcmp_icase(const char *a, const char *b) | |
60 | { | |
61 | return ignore_case ? strcasecmp(a, b) : strcmp(a, b); | |
62 | } | |
63 | ||
64 | int strncmp_icase(const char *a, const char *b, size_t count) | |
65 | { | |
66 | return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); | |
67 | } | |
68 | ||
69 | int fnmatch_icase(const char *pattern, const char *string, int flags) | |
70 | { | |
eb07894f NTND |
71 | return wildmatch(pattern, string, |
72 | flags | (ignore_case ? WM_CASEFOLD : 0), | |
73 | NULL); | |
8cf2a84e JJ |
74 | } |
75 | ||
1f26ce61 CB |
76 | int git_fnmatch(const struct pathspec_item *item, |
77 | const char *pattern, const char *string, | |
78 | int prefix) | |
5d74762d | 79 | { |
5d74762d | 80 | if (prefix > 0) { |
93d93537 | 81 | if (ps_strncmp(item, pattern, string, prefix)) |
eb07894f | 82 | return WM_NOMATCH; |
5d74762d NTND |
83 | pattern += prefix; |
84 | string += prefix; | |
85 | } | |
bd30c2e4 | 86 | if (item->flags & PATHSPEC_ONESTAR) { |
8c6abbcd NTND |
87 | int pattern_len = strlen(++pattern); |
88 | int string_len = strlen(string); | |
89 | return string_len < pattern_len || | |
93d93537 NTND |
90 | ps_strcmp(item, pattern, |
91 | string + string_len - pattern_len); | |
8c6abbcd | 92 | } |
bd30c2e4 | 93 | if (item->magic & PATHSPEC_GLOB) |
93d93537 NTND |
94 | return wildmatch(pattern, string, |
95 | WM_PATHNAME | | |
96 | (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0), | |
97 | NULL); | |
bd30c2e4 NTND |
98 | else |
99 | /* wildmatch has not learned no FNM_PATHNAME mode yet */ | |
eb07894f NTND |
100 | return wildmatch(pattern, string, |
101 | item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0, | |
102 | NULL); | |
5d74762d NTND |
103 | } |
104 | ||
0b6e56df JH |
105 | static int fnmatch_icase_mem(const char *pattern, int patternlen, |
106 | const char *string, int stringlen, | |
107 | int flags) | |
108 | { | |
109 | int match_status; | |
110 | struct strbuf pat_buf = STRBUF_INIT; | |
111 | struct strbuf str_buf = STRBUF_INIT; | |
112 | const char *use_pat = pattern; | |
113 | const char *use_str = string; | |
114 | ||
115 | if (pattern[patternlen]) { | |
116 | strbuf_add(&pat_buf, pattern, patternlen); | |
117 | use_pat = pat_buf.buf; | |
118 | } | |
119 | if (string[stringlen]) { | |
120 | strbuf_add(&str_buf, string, stringlen); | |
121 | use_str = str_buf.buf; | |
122 | } | |
123 | ||
f30366b2 JH |
124 | if (ignore_case) |
125 | flags |= WM_CASEFOLD; | |
126 | match_status = wildmatch(use_pat, use_str, flags, NULL); | |
0b6e56df JH |
127 | |
128 | strbuf_release(&pat_buf); | |
129 | strbuf_release(&str_buf); | |
130 | ||
131 | return match_status; | |
132 | } | |
133 | ||
827f4d6c | 134 | static size_t common_prefix_len(const struct pathspec *pathspec) |
3c6a370b | 135 | { |
827f4d6c | 136 | int n; |
4a085b16 | 137 | size_t max = 0; |
3c6a370b | 138 | |
93d93537 NTND |
139 | /* |
140 | * ":(icase)path" is treated as a pathspec full of | |
141 | * wildcard. In other words, only prefix is considered common | |
142 | * prefix. If the pathspec is abc/foo abc/bar, running in | |
143 | * subdir xyz, the common prefix is still xyz, not xuz/abc as | |
144 | * in non-:(icase). | |
145 | */ | |
5c6933d2 NTND |
146 | GUARD_PATHSPEC(pathspec, |
147 | PATHSPEC_FROMTOP | | |
148 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 149 | PATHSPEC_LITERAL | |
93d93537 | 150 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
151 | PATHSPEC_ICASE | |
152 | PATHSPEC_EXCLUDE); | |
4a085b16 | 153 | |
827f4d6c | 154 | for (n = 0; n < pathspec->nr; n++) { |
93d93537 | 155 | size_t i = 0, len = 0, item_len; |
ef79b1f8 NTND |
156 | if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) |
157 | continue; | |
93d93537 NTND |
158 | if (pathspec->items[n].magic & PATHSPEC_ICASE) |
159 | item_len = pathspec->items[n].prefix; | |
160 | else | |
161 | item_len = pathspec->items[n].nowildcard_len; | |
162 | while (i < item_len && (n == 0 || i < max)) { | |
827f4d6c NTND |
163 | char c = pathspec->items[n].match[i]; |
164 | if (c != pathspec->items[0].match[i]) | |
4a085b16 JH |
165 | break; |
166 | if (c == '/') | |
167 | len = i + 1; | |
827f4d6c | 168 | i++; |
4a085b16 | 169 | } |
827f4d6c | 170 | if (n == 0 || len < max) { |
4a085b16 JH |
171 | max = len; |
172 | if (!max) | |
173 | break; | |
174 | } | |
3c6a370b | 175 | } |
4a085b16 | 176 | return max; |
3c6a370b LT |
177 | } |
178 | ||
f950eb95 CB |
179 | /* |
180 | * Returns a copy of the longest leading path common among all | |
181 | * pathspecs. | |
182 | */ | |
827f4d6c | 183 | char *common_prefix(const struct pathspec *pathspec) |
f950eb95 CB |
184 | { |
185 | unsigned long len = common_prefix_len(pathspec); | |
186 | ||
827f4d6c | 187 | return len ? xmemdupz(pathspec->items[0].match, len) : NULL; |
f950eb95 CB |
188 | } |
189 | ||
7327d3d1 | 190 | int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) |
1d8842d9 | 191 | { |
4a085b16 | 192 | size_t len; |
1d8842d9 LT |
193 | |
194 | /* | |
195 | * Calculate common prefix for the pathspec, and | |
196 | * use that to optimize the directory walk | |
197 | */ | |
4a085b16 | 198 | len = common_prefix_len(pathspec); |
1d8842d9 LT |
199 | |
200 | /* Read the directory and prune it */ | |
b3920bbd | 201 | read_directory(dir, pathspec->nr ? pathspec->_raw[0] : "", len, pathspec); |
dba2e203 | 202 | return len; |
1d8842d9 LT |
203 | } |
204 | ||
bc96cc87 NTND |
205 | int within_depth(const char *name, int namelen, |
206 | int depth, int max_depth) | |
207 | { | |
208 | const char *cp = name, *cpe = name + namelen; | |
209 | ||
210 | while (cp < cpe) { | |
211 | if (*cp++ != '/') | |
212 | continue; | |
213 | depth++; | |
214 | if (depth > max_depth) | |
215 | return 0; | |
216 | } | |
217 | return 1; | |
218 | } | |
219 | ||
42b0874a | 220 | #define DO_MATCH_EXCLUDE 1 |
68690fdd | 221 | #define DO_MATCH_DIRECTORY 2 |
42b0874a | 222 | |
61cf2820 NTND |
223 | /* |
224 | * Does 'match' match the given name? | |
225 | * A match is found if | |
226 | * | |
227 | * (1) the 'match' string is leading directory of 'name', or | |
228 | * (2) the 'match' string is a wildcard and matches 'name', or | |
229 | * (3) the 'match' string is exactly the same as 'name'. | |
230 | * | |
231 | * and the return value tells which case it was. | |
232 | * | |
233 | * It returns 0 when there is no match. | |
234 | */ | |
235 | static int match_pathspec_item(const struct pathspec_item *item, int prefix, | |
42b0874a | 236 | const char *name, int namelen, unsigned flags) |
61cf2820 NTND |
237 | { |
238 | /* name/namelen has prefix cut off by caller */ | |
239 | const char *match = item->match + prefix; | |
240 | int matchlen = item->len - prefix; | |
241 | ||
93d93537 NTND |
242 | /* |
243 | * The normal call pattern is: | |
244 | * 1. prefix = common_prefix_len(ps); | |
245 | * 2. prune something, or fill_directory | |
854b0959 | 246 | * 3. match_pathspec() |
93d93537 NTND |
247 | * |
248 | * 'prefix' at #1 may be shorter than the command's prefix and | |
249 | * it's ok for #2 to match extra files. Those extras will be | |
250 | * trimmed at #3. | |
251 | * | |
252 | * Suppose the pathspec is 'foo' and '../bar' running from | |
253 | * subdir 'xyz'. The common prefix at #1 will be empty, thanks | |
254 | * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The | |
255 | * user does not want XYZ/foo, only the "foo" part should be | |
256 | * case-insensitive. We need to filter out XYZ/foo here. In | |
257 | * other words, we do not trust the caller on comparing the | |
258 | * prefix part when :(icase) is involved. We do exact | |
259 | * comparison ourselves. | |
260 | * | |
261 | * Normally the caller (common_prefix_len() in fact) does | |
262 | * _exact_ matching on name[-prefix+1..-1] and we do not need | |
263 | * to check that part. Be defensive and check it anyway, in | |
264 | * case common_prefix_len is changed, or a new caller is | |
265 | * introduced that does not use common_prefix_len. | |
266 | * | |
267 | * If the penalty turns out too high when prefix is really | |
268 | * long, maybe change it to | |
269 | * strncmp(match, name, item->prefix - prefix) | |
270 | */ | |
271 | if (item->prefix && (item->magic & PATHSPEC_ICASE) && | |
272 | strncmp(item->match, name - prefix, item->prefix)) | |
273 | return 0; | |
274 | ||
61cf2820 NTND |
275 | /* If the match was just the prefix, we matched */ |
276 | if (!*match) | |
277 | return MATCHED_RECURSIVELY; | |
278 | ||
93d93537 | 279 | if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { |
61cf2820 NTND |
280 | if (matchlen == namelen) |
281 | return MATCHED_EXACTLY; | |
282 | ||
283 | if (match[matchlen-1] == '/' || name[matchlen] == '/') | |
284 | return MATCHED_RECURSIVELY; | |
68690fdd NTND |
285 | } else if ((flags & DO_MATCH_DIRECTORY) && |
286 | match[matchlen - 1] == '/' && | |
287 | namelen == matchlen - 1 && | |
288 | !ps_strncmp(item, match, name, namelen)) | |
289 | return MATCHED_EXACTLY; | |
61cf2820 | 290 | |
5d74762d | 291 | if (item->nowildcard_len < item->len && |
bd30c2e4 | 292 | !git_fnmatch(item, match, name, |
8c6abbcd | 293 | item->nowildcard_len - prefix)) |
61cf2820 NTND |
294 | return MATCHED_FNMATCH; |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | /* | |
52ed1894 AS |
300 | * Given a name and a list of pathspecs, returns the nature of the |
301 | * closest (i.e. most specific) match of the name to any of the | |
302 | * pathspecs. | |
303 | * | |
304 | * The caller typically calls this multiple times with the same | |
305 | * pathspec and seen[] array but with different name/namelen | |
306 | * (e.g. entries from the index) and is interested in seeing if and | |
307 | * how each pathspec matches all the names it calls this function | |
308 | * with. A mark is left in the seen[] array for each pathspec element | |
309 | * indicating the closest type of match that element achieved, so if | |
310 | * seen[n] remains zero after multiple invocations, that means the nth | |
311 | * pathspec did not match any names, which could indicate that the | |
312 | * user mistyped the nth pathspec. | |
61cf2820 | 313 | */ |
854b0959 NTND |
314 | static int do_match_pathspec(const struct pathspec *ps, |
315 | const char *name, int namelen, | |
316 | int prefix, char *seen, | |
42b0874a | 317 | unsigned flags) |
61cf2820 | 318 | { |
42b0874a | 319 | int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; |
61cf2820 | 320 | |
5c6933d2 NTND |
321 | GUARD_PATHSPEC(ps, |
322 | PATHSPEC_FROMTOP | | |
323 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 324 | PATHSPEC_LITERAL | |
93d93537 | 325 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
326 | PATHSPEC_ICASE | |
327 | PATHSPEC_EXCLUDE); | |
8f4f8f45 | 328 | |
61cf2820 | 329 | if (!ps->nr) { |
6330a171 NTND |
330 | if (!ps->recursive || |
331 | !(ps->magic & PATHSPEC_MAXDEPTH) || | |
332 | ps->max_depth == -1) | |
61cf2820 NTND |
333 | return MATCHED_RECURSIVELY; |
334 | ||
335 | if (within_depth(name, namelen, 0, ps->max_depth)) | |
336 | return MATCHED_EXACTLY; | |
337 | else | |
338 | return 0; | |
339 | } | |
340 | ||
341 | name += prefix; | |
342 | namelen -= prefix; | |
343 | ||
344 | for (i = ps->nr - 1; i >= 0; i--) { | |
345 | int how; | |
ef79b1f8 NTND |
346 | |
347 | if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || | |
348 | ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) | |
349 | continue; | |
350 | ||
61cf2820 NTND |
351 | if (seen && seen[i] == MATCHED_EXACTLY) |
352 | continue; | |
ef79b1f8 NTND |
353 | /* |
354 | * Make exclude patterns optional and never report | |
355 | * "pathspec ':(exclude)foo' matches no files" | |
356 | */ | |
357 | if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) | |
358 | seen[i] = MATCHED_FNMATCH; | |
42b0874a NTND |
359 | how = match_pathspec_item(ps->items+i, prefix, name, |
360 | namelen, flags); | |
6330a171 NTND |
361 | if (ps->recursive && |
362 | (ps->magic & PATHSPEC_MAXDEPTH) && | |
363 | ps->max_depth != -1 && | |
61cf2820 NTND |
364 | how && how != MATCHED_FNMATCH) { |
365 | int len = ps->items[i].len; | |
366 | if (name[len] == '/') | |
367 | len++; | |
368 | if (within_depth(name+len, namelen-len, 0, ps->max_depth)) | |
369 | how = MATCHED_EXACTLY; | |
370 | else | |
371 | how = 0; | |
372 | } | |
373 | if (how) { | |
374 | if (retval < how) | |
375 | retval = how; | |
376 | if (seen && seen[i] < how) | |
377 | seen[i] = how; | |
378 | } | |
379 | } | |
380 | return retval; | |
381 | } | |
382 | ||
854b0959 NTND |
383 | int match_pathspec(const struct pathspec *ps, |
384 | const char *name, int namelen, | |
ae8d0824 | 385 | int prefix, char *seen, int is_dir) |
ef79b1f8 NTND |
386 | { |
387 | int positive, negative; | |
ae8d0824 | 388 | unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; |
42b0874a NTND |
389 | positive = do_match_pathspec(ps, name, namelen, |
390 | prefix, seen, flags); | |
ef79b1f8 NTND |
391 | if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) |
392 | return positive; | |
42b0874a NTND |
393 | negative = do_match_pathspec(ps, name, namelen, |
394 | prefix, seen, | |
395 | flags | DO_MATCH_EXCLUDE); | |
ef79b1f8 NTND |
396 | return negative ? 0 : positive; |
397 | } | |
398 | ||
777c55a6 JH |
399 | int report_path_error(const char *ps_matched, |
400 | const struct pathspec *pathspec, | |
401 | const char *prefix) | |
402 | { | |
403 | /* | |
404 | * Make sure all pathspec matched; otherwise it is an error. | |
405 | */ | |
777c55a6 JH |
406 | int num, errors = 0; |
407 | for (num = 0; num < pathspec->nr; num++) { | |
408 | int other, found_dup; | |
409 | ||
410 | if (ps_matched[num]) | |
411 | continue; | |
412 | /* | |
413 | * The caller might have fed identical pathspec | |
414 | * twice. Do not barf on such a mistake. | |
415 | * FIXME: parse_pathspec should have eliminated | |
416 | * duplicate pathspec. | |
417 | */ | |
418 | for (found_dup = other = 0; | |
419 | !found_dup && other < pathspec->nr; | |
420 | other++) { | |
421 | if (other == num || !ps_matched[other]) | |
422 | continue; | |
423 | if (!strcmp(pathspec->items[other].original, | |
424 | pathspec->items[num].original)) | |
425 | /* | |
426 | * Ok, we have a match already. | |
427 | */ | |
428 | found_dup = 1; | |
429 | } | |
430 | if (found_dup) | |
431 | continue; | |
432 | ||
433 | error("pathspec '%s' did not match any file(s) known to git.", | |
434 | pathspec->items[num].original); | |
435 | errors++; | |
436 | } | |
777c55a6 JH |
437 | return errors; |
438 | } | |
439 | ||
fcd631ed NTND |
440 | /* |
441 | * Return the length of the "simple" part of a path match limiter. | |
442 | */ | |
87323bda | 443 | int simple_length(const char *match) |
fcd631ed NTND |
444 | { |
445 | int len = -1; | |
446 | ||
447 | for (;;) { | |
448 | unsigned char c = *match++; | |
449 | len++; | |
450 | if (c == '\0' || is_glob_special(c)) | |
451 | return len; | |
452 | } | |
453 | } | |
454 | ||
87323bda | 455 | int no_wildcard(const char *string) |
68492fc7 | 456 | { |
fcd631ed | 457 | return string[simple_length(string)] == '\0'; |
68492fc7 LK |
458 | } |
459 | ||
82dce998 NTND |
460 | void parse_exclude_pattern(const char **pattern, |
461 | int *patternlen, | |
462 | int *flags, | |
463 | int *nowildcardlen) | |
84460eec NTND |
464 | { |
465 | const char *p = *pattern; | |
466 | size_t i, len; | |
467 | ||
468 | *flags = 0; | |
469 | if (*p == '!') { | |
470 | *flags |= EXC_FLAG_NEGATIVE; | |
471 | p++; | |
472 | } | |
473 | len = strlen(p); | |
474 | if (len && p[len - 1] == '/') { | |
475 | len--; | |
476 | *flags |= EXC_FLAG_MUSTBEDIR; | |
477 | } | |
478 | for (i = 0; i < len; i++) { | |
479 | if (p[i] == '/') | |
480 | break; | |
481 | } | |
482 | if (i == len) | |
483 | *flags |= EXC_FLAG_NODIR; | |
484 | *nowildcardlen = simple_length(p); | |
485 | /* | |
486 | * we should have excluded the trailing slash from 'p' too, | |
487 | * but that's one more allocation. Instead just make sure | |
488 | * nowildcardlen does not exceed real patternlen | |
489 | */ | |
490 | if (*nowildcardlen > len) | |
491 | *nowildcardlen = len; | |
492 | if (*p == '*' && no_wildcard(p + 1)) | |
493 | *flags |= EXC_FLAG_ENDSWITH; | |
494 | *pattern = p; | |
495 | *patternlen = len; | |
496 | } | |
497 | ||
453ec4bd | 498 | void add_exclude(const char *string, const char *base, |
c04318e4 | 499 | int baselen, struct exclude_list *el, int srcpos) |
453ec4bd | 500 | { |
d6b8fc30 | 501 | struct exclude *x; |
84460eec NTND |
502 | int patternlen; |
503 | int flags; | |
504 | int nowildcardlen; | |
453ec4bd | 505 | |
84460eec NTND |
506 | parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); |
507 | if (flags & EXC_FLAG_MUSTBEDIR) { | |
96ffc06f | 508 | FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen); |
d6b8fc30 JH |
509 | } else { |
510 | x = xmalloc(sizeof(*x)); | |
511 | x->pattern = string; | |
512 | } | |
84460eec NTND |
513 | x->patternlen = patternlen; |
514 | x->nowildcardlen = nowildcardlen; | |
453ec4bd LT |
515 | x->base = base; |
516 | x->baselen = baselen; | |
d6b8fc30 | 517 | x->flags = flags; |
c04318e4 | 518 | x->srcpos = srcpos; |
c62a9173 | 519 | string_list_init(&x->sticky_paths, 1); |
840fc334 AS |
520 | ALLOC_GROW(el->excludes, el->nr + 1, el->alloc); |
521 | el->excludes[el->nr++] = x; | |
c04318e4 | 522 | x->el = el; |
453ec4bd LT |
523 | } |
524 | ||
55fe6f51 NTND |
525 | static void *read_skip_worktree_file_from_index(const char *path, size_t *size, |
526 | struct sha1_stat *sha1_stat) | |
c28b3d6e NTND |
527 | { |
528 | int pos, len; | |
529 | unsigned long sz; | |
530 | enum object_type type; | |
531 | void *data; | |
c28b3d6e NTND |
532 | |
533 | len = strlen(path); | |
71261027 | 534 | pos = cache_name_pos(path, len); |
c28b3d6e NTND |
535 | if (pos < 0) |
536 | return NULL; | |
71261027 | 537 | if (!ce_skip_worktree(active_cache[pos])) |
c28b3d6e | 538 | return NULL; |
71261027 | 539 | data = read_sha1_file(active_cache[pos]->sha1, &type, &sz); |
c28b3d6e NTND |
540 | if (!data || type != OBJ_BLOB) { |
541 | free(data); | |
542 | return NULL; | |
543 | } | |
544 | *size = xsize_t(sz); | |
55fe6f51 NTND |
545 | if (sha1_stat) { |
546 | memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat)); | |
547 | hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); | |
548 | } | |
c28b3d6e NTND |
549 | return data; |
550 | } | |
551 | ||
f6198812 AS |
552 | /* |
553 | * Frees memory within el which was allocated for exclude patterns and | |
554 | * the file buffer. Does not free el itself. | |
555 | */ | |
556 | void clear_exclude_list(struct exclude_list *el) | |
0fd0e241 NTND |
557 | { |
558 | int i; | |
559 | ||
c62a9173 NTND |
560 | for (i = 0; i < el->nr; i++) { |
561 | string_list_clear(&el->excludes[i]->sticky_paths, 0); | |
0fd0e241 | 562 | free(el->excludes[i]); |
c62a9173 | 563 | } |
0fd0e241 | 564 | free(el->excludes); |
c082df24 | 565 | free(el->filebuf); |
0fd0e241 | 566 | |
2653a8c6 | 567 | memset(el, 0, sizeof(*el)); |
0fd0e241 NTND |
568 | } |
569 | ||
7e2e4b37 | 570 | static void trim_trailing_spaces(char *buf) |
16402b99 | 571 | { |
e61a6c1d PB |
572 | char *p, *last_space = NULL; |
573 | ||
574 | for (p = buf; *p; p++) | |
575 | switch (*p) { | |
576 | case ' ': | |
577 | if (!last_space) | |
578 | last_space = p; | |
579 | break; | |
580 | case '\\': | |
581 | p++; | |
582 | if (!*p) | |
583 | return; | |
584 | /* fallthrough */ | |
585 | default: | |
586 | last_space = NULL; | |
587 | } | |
588 | ||
589 | if (last_space) | |
590 | *last_space = '\0'; | |
16402b99 NTND |
591 | } |
592 | ||
0dcb8d7f NTND |
593 | /* |
594 | * Given a subdirectory name and "dir" of the current directory, | |
595 | * search the subdir in "dir" and return it, or create a new one if it | |
596 | * does not exist in "dir". | |
597 | * | |
598 | * If "name" has the trailing slash, it'll be excluded in the search. | |
599 | */ | |
600 | static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, | |
601 | struct untracked_cache_dir *dir, | |
602 | const char *name, int len) | |
603 | { | |
604 | int first, last; | |
605 | struct untracked_cache_dir *d; | |
606 | if (!dir) | |
607 | return NULL; | |
608 | if (len && name[len - 1] == '/') | |
609 | len--; | |
610 | first = 0; | |
611 | last = dir->dirs_nr; | |
612 | while (last > first) { | |
613 | int cmp, next = (last + first) >> 1; | |
614 | d = dir->dirs[next]; | |
615 | cmp = strncmp(name, d->name, len); | |
616 | if (!cmp && strlen(d->name) > len) | |
617 | cmp = -1; | |
618 | if (!cmp) | |
619 | return d; | |
620 | if (cmp < 0) { | |
621 | last = next; | |
622 | continue; | |
623 | } | |
624 | first = next+1; | |
625 | } | |
626 | ||
627 | uc->dir_created++; | |
96ffc06f | 628 | FLEX_ALLOC_MEM(d, name, name, len); |
0dcb8d7f NTND |
629 | |
630 | ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); | |
631 | memmove(dir->dirs + first + 1, dir->dirs + first, | |
632 | (dir->dirs_nr - first) * sizeof(*dir->dirs)); | |
633 | dir->dirs_nr++; | |
634 | dir->dirs[first] = d; | |
635 | return d; | |
636 | } | |
637 | ||
ccad261f NTND |
638 | static void do_invalidate_gitignore(struct untracked_cache_dir *dir) |
639 | { | |
640 | int i; | |
641 | dir->valid = 0; | |
642 | dir->untracked_nr = 0; | |
643 | for (i = 0; i < dir->dirs_nr; i++) | |
644 | do_invalidate_gitignore(dir->dirs[i]); | |
645 | } | |
646 | ||
647 | static void invalidate_gitignore(struct untracked_cache *uc, | |
648 | struct untracked_cache_dir *dir) | |
649 | { | |
650 | uc->gitignore_invalidated++; | |
651 | do_invalidate_gitignore(dir); | |
652 | } | |
653 | ||
91a2288b NTND |
654 | static void invalidate_directory(struct untracked_cache *uc, |
655 | struct untracked_cache_dir *dir) | |
656 | { | |
26cb0182 | 657 | int i; |
91a2288b NTND |
658 | uc->dir_invalidated++; |
659 | dir->valid = 0; | |
660 | dir->untracked_nr = 0; | |
26cb0182 NTND |
661 | for (i = 0; i < dir->dirs_nr; i++) |
662 | dir->dirs[i]->recurse = 0; | |
91a2288b NTND |
663 | } |
664 | ||
55fe6f51 NTND |
665 | /* |
666 | * Given a file with name "fname", read it (either from disk, or from | |
667 | * the index if "check_index" is non-zero), parse it and store the | |
668 | * exclude rules in "el". | |
669 | * | |
670 | * If "ss" is not NULL, compute SHA-1 of the exclude file and fill | |
671 | * stat data from disk (only valid if add_excludes returns zero). If | |
672 | * ss_valid is non-zero, "ss" must contain good value as input. | |
673 | */ | |
674 | static int add_excludes(const char *fname, const char *base, int baselen, | |
675 | struct exclude_list *el, int check_index, | |
676 | struct sha1_stat *sha1_stat) | |
453ec4bd | 677 | { |
c470701a | 678 | struct stat st; |
c04318e4 | 679 | int fd, i, lineno = 1; |
9d14017a | 680 | size_t size = 0; |
453ec4bd LT |
681 | char *buf, *entry; |
682 | ||
683 | fd = open(fname, O_RDONLY); | |
c28b3d6e | 684 | if (fd < 0 || fstat(fd, &st) < 0) { |
69660731 | 685 | if (errno != ENOENT) |
55b38a48 | 686 | warn_on_inaccessible(fname); |
c28b3d6e NTND |
687 | if (0 <= fd) |
688 | close(fd); | |
689 | if (!check_index || | |
55fe6f51 | 690 | (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) |
c28b3d6e | 691 | return -1; |
45d76f17 NTND |
692 | if (size == 0) { |
693 | free(buf); | |
694 | return 0; | |
695 | } | |
696 | if (buf[size-1] != '\n') { | |
50a6c8ef | 697 | buf = xrealloc(buf, st_add(size, 1)); |
45d76f17 NTND |
698 | buf[size++] = '\n'; |
699 | } | |
d961baa8 | 700 | } else { |
c28b3d6e NTND |
701 | size = xsize_t(st.st_size); |
702 | if (size == 0) { | |
55fe6f51 NTND |
703 | if (sha1_stat) { |
704 | fill_stat_data(&sha1_stat->stat, &st); | |
705 | hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); | |
706 | sha1_stat->valid = 1; | |
707 | } | |
c28b3d6e NTND |
708 | close(fd); |
709 | return 0; | |
710 | } | |
3733e694 | 711 | buf = xmallocz(size); |
c28b3d6e | 712 | if (read_in_full(fd, buf, size) != size) { |
45d76f17 | 713 | free(buf); |
c28b3d6e NTND |
714 | close(fd); |
715 | return -1; | |
716 | } | |
45d76f17 | 717 | buf[size++] = '\n'; |
c28b3d6e | 718 | close(fd); |
55fe6f51 NTND |
719 | if (sha1_stat) { |
720 | int pos; | |
721 | if (sha1_stat->valid && | |
ed4efab1 | 722 | !match_stat_data_racy(&the_index, &sha1_stat->stat, &st)) |
55fe6f51 NTND |
723 | ; /* no content change, ss->sha1 still good */ |
724 | else if (check_index && | |
725 | (pos = cache_name_pos(fname, strlen(fname))) >= 0 && | |
726 | !ce_stage(active_cache[pos]) && | |
727 | ce_uptodate(active_cache[pos]) && | |
728 | !would_convert_to_git(fname)) | |
729 | hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); | |
730 | else | |
731 | hash_sha1_file(buf, size, "blob", sha1_stat->sha1); | |
732 | fill_stat_data(&sha1_stat->stat, &st); | |
733 | sha1_stat->valid = 1; | |
734 | } | |
6ba78238 | 735 | } |
453ec4bd | 736 | |
c082df24 | 737 | el->filebuf = buf; |
245e1c19 | 738 | |
dde843e7 JH |
739 | if (skip_utf8_bom(&buf, size)) |
740 | size -= buf - el->filebuf; | |
741 | ||
453ec4bd | 742 | entry = buf; |
245e1c19 | 743 | |
45d76f17 NTND |
744 | for (i = 0; i < size; i++) { |
745 | if (buf[i] == '\n') { | |
453ec4bd LT |
746 | if (entry != buf + i && entry[0] != '#') { |
747 | buf[i - (i && buf[i-1] == '\r')] = 0; | |
7e2e4b37 | 748 | trim_trailing_spaces(entry); |
c04318e4 | 749 | add_exclude(entry, base, baselen, el, lineno); |
453ec4bd | 750 | } |
c04318e4 | 751 | lineno++; |
453ec4bd LT |
752 | entry = buf + i + 1; |
753 | } | |
754 | } | |
755 | return 0; | |
453ec4bd LT |
756 | } |
757 | ||
55fe6f51 NTND |
758 | int add_excludes_from_file_to_list(const char *fname, const char *base, |
759 | int baselen, struct exclude_list *el, | |
760 | int check_index) | |
761 | { | |
762 | return add_excludes(fname, base, baselen, el, check_index, NULL); | |
763 | } | |
764 | ||
c04318e4 AS |
765 | struct exclude_list *add_exclude_list(struct dir_struct *dir, |
766 | int group_type, const char *src) | |
c082df24 AS |
767 | { |
768 | struct exclude_list *el; | |
769 | struct exclude_list_group *group; | |
770 | ||
771 | group = &dir->exclude_list_group[group_type]; | |
772 | ALLOC_GROW(group->el, group->nr + 1, group->alloc); | |
773 | el = &group->el[group->nr++]; | |
774 | memset(el, 0, sizeof(*el)); | |
c04318e4 | 775 | el->src = src; |
c082df24 AS |
776 | return el; |
777 | } | |
778 | ||
779 | /* | |
780 | * Used to set up core.excludesfile and .git/info/exclude lists. | |
781 | */ | |
0dcb8d7f NTND |
782 | static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname, |
783 | struct sha1_stat *sha1_stat) | |
453ec4bd | 784 | { |
c082df24 | 785 | struct exclude_list *el; |
ccad261f NTND |
786 | /* |
787 | * catch setup_standard_excludes() that's called before | |
788 | * dir->untracked is assigned. That function behaves | |
789 | * differently when dir->untracked is non-NULL. | |
790 | */ | |
791 | if (!dir->untracked) | |
792 | dir->unmanaged_exclude_files++; | |
c04318e4 | 793 | el = add_exclude_list(dir, EXC_FILE, fname); |
0dcb8d7f | 794 | if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0) |
453ec4bd LT |
795 | die("cannot use %s as an exclude file", fname); |
796 | } | |
797 | ||
0dcb8d7f NTND |
798 | void add_excludes_from_file(struct dir_struct *dir, const char *fname) |
799 | { | |
ccad261f | 800 | dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */ |
0dcb8d7f NTND |
801 | add_excludes_from_file_1(dir, fname, NULL); |
802 | } | |
803 | ||
82dce998 NTND |
804 | int match_basename(const char *basename, int basenamelen, |
805 | const char *pattern, int prefix, int patternlen, | |
806 | int flags) | |
593cb880 NTND |
807 | { |
808 | if (prefix == patternlen) { | |
0b6e56df JH |
809 | if (patternlen == basenamelen && |
810 | !strncmp_icase(pattern, basename, basenamelen)) | |
593cb880 NTND |
811 | return 1; |
812 | } else if (flags & EXC_FLAG_ENDSWITH) { | |
0b6e56df | 813 | /* "*literal" matching against "fooliteral" */ |
593cb880 | 814 | if (patternlen - 1 <= basenamelen && |
0b6e56df JH |
815 | !strncmp_icase(pattern + 1, |
816 | basename + basenamelen - (patternlen - 1), | |
817 | patternlen - 1)) | |
593cb880 NTND |
818 | return 1; |
819 | } else { | |
0b6e56df JH |
820 | if (fnmatch_icase_mem(pattern, patternlen, |
821 | basename, basenamelen, | |
822 | 0) == 0) | |
593cb880 NTND |
823 | return 1; |
824 | } | |
825 | return 0; | |
826 | } | |
827 | ||
82dce998 NTND |
828 | int match_pathname(const char *pathname, int pathlen, |
829 | const char *base, int baselen, | |
830 | const char *pattern, int prefix, int patternlen, | |
831 | int flags) | |
b5592632 NTND |
832 | { |
833 | const char *name; | |
834 | int namelen; | |
835 | ||
836 | /* | |
837 | * match with FNM_PATHNAME; the pattern has base implicitly | |
838 | * in front of it. | |
839 | */ | |
840 | if (*pattern == '/') { | |
841 | pattern++; | |
982ac873 | 842 | patternlen--; |
b5592632 NTND |
843 | prefix--; |
844 | } | |
845 | ||
846 | /* | |
847 | * baselen does not count the trailing slash. base[] may or | |
848 | * may not end with a trailing slash though. | |
849 | */ | |
850 | if (pathlen < baselen + 1 || | |
851 | (baselen && pathname[baselen] != '/') || | |
852 | strncmp_icase(pathname, base, baselen)) | |
853 | return 0; | |
854 | ||
855 | namelen = baselen ? pathlen - baselen - 1 : pathlen; | |
856 | name = pathname + pathlen - namelen; | |
857 | ||
858 | if (prefix) { | |
859 | /* | |
860 | * if the non-wildcard part is longer than the | |
861 | * remaining pathname, surely it cannot match. | |
862 | */ | |
863 | if (prefix > namelen) | |
864 | return 0; | |
865 | ||
866 | if (strncmp_icase(pattern, name, prefix)) | |
867 | return 0; | |
868 | pattern += prefix; | |
ab3aebc1 | 869 | patternlen -= prefix; |
b5592632 NTND |
870 | name += prefix; |
871 | namelen -= prefix; | |
ab3aebc1 JK |
872 | |
873 | /* | |
874 | * If the whole pattern did not have a wildcard, | |
875 | * then our prefix match is all we need; we | |
876 | * do not need to call fnmatch at all. | |
877 | */ | |
a60ea8fb | 878 | if (!patternlen && (!namelen || *name == '/')) |
ab3aebc1 | 879 | return 1; |
b5592632 NTND |
880 | } |
881 | ||
ab3aebc1 JK |
882 | return fnmatch_icase_mem(pattern, patternlen, |
883 | name, namelen, | |
f30366b2 | 884 | WM_PATHNAME) == 0; |
b5592632 NTND |
885 | } |
886 | ||
c62a9173 NTND |
887 | static void add_sticky(struct exclude *exc, const char *pathname, int pathlen) |
888 | { | |
889 | struct strbuf sb = STRBUF_INIT; | |
890 | int i; | |
891 | ||
892 | for (i = exc->sticky_paths.nr - 1; i >= 0; i--) { | |
893 | const char *sticky = exc->sticky_paths.items[i].string; | |
894 | int len = strlen(sticky); | |
895 | ||
896 | if (pathlen < len && sticky[pathlen] == '/' && | |
897 | !strncmp(pathname, sticky, pathlen)) | |
898 | return; | |
899 | } | |
900 | ||
901 | strbuf_add(&sb, pathname, pathlen); | |
902 | string_list_append_nodup(&exc->sticky_paths, strbuf_detach(&sb, NULL)); | |
903 | } | |
904 | ||
905 | static int match_sticky(struct exclude *exc, const char *pathname, int pathlen, int dtype) | |
906 | { | |
907 | int i; | |
908 | ||
909 | for (i = exc->sticky_paths.nr - 1; i >= 0; i--) { | |
910 | const char *sticky = exc->sticky_paths.items[i].string; | |
911 | int len = strlen(sticky); | |
912 | ||
913 | if (pathlen == len && dtype == DT_DIR && | |
914 | !strncmp(pathname, sticky, len)) | |
915 | return 1; | |
916 | ||
917 | if (pathlen > len && pathname[len] == '/' && | |
918 | !strncmp(pathname, sticky, len)) | |
919 | return 1; | |
920 | } | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
d589a67e NTND |
925 | static inline int different_decisions(const struct exclude *a, |
926 | const struct exclude *b) | |
927 | { | |
928 | return (a->flags & EXC_FLAG_NEGATIVE) != (b->flags & EXC_FLAG_NEGATIVE); | |
929 | } | |
930 | ||
931 | /* | |
932 | * Return non-zero if pathname is a directory and an ancestor of the | |
933 | * literal path in a pattern. | |
934 | */ | |
935 | static int match_directory_part(const char *pathname, int pathlen, | |
936 | int *dtype, struct exclude *x) | |
937 | { | |
938 | const char *base = x->base; | |
939 | int baselen = x->baselen ? x->baselen - 1 : 0; | |
940 | const char *pattern = x->pattern; | |
941 | int prefix = x->nowildcardlen; | |
942 | int patternlen = x->patternlen; | |
943 | ||
944 | if (*dtype == DT_UNKNOWN) | |
945 | *dtype = get_dtype(NULL, pathname, pathlen); | |
946 | if (*dtype != DT_DIR) | |
947 | return 0; | |
948 | ||
949 | if (*pattern == '/') { | |
950 | pattern++; | |
951 | patternlen--; | |
952 | prefix--; | |
953 | } | |
954 | ||
955 | if (baselen) { | |
956 | if (((pathlen < baselen && base[pathlen] == '/') || | |
957 | pathlen == baselen) && | |
958 | !strncmp_icase(pathname, base, pathlen)) | |
959 | return 1; | |
960 | pathname += baselen + 1; | |
961 | pathlen -= baselen + 1; | |
962 | } | |
963 | ||
964 | ||
965 | if (prefix && | |
966 | (((pathlen < prefix && pattern[pathlen] == '/') || | |
967 | pathlen == prefix) && | |
968 | !strncmp_icase(pathname, pattern, pathlen))) | |
969 | return 1; | |
970 | ||
971 | return 0; | |
972 | } | |
973 | ||
974 | static struct exclude *should_descend(const char *pathname, int pathlen, | |
975 | int *dtype, struct exclude_list *el, | |
976 | struct exclude *exc) | |
977 | { | |
978 | int i; | |
979 | ||
980 | for (i = el->nr - 1; 0 <= i; i--) { | |
981 | struct exclude *x = el->excludes[i]; | |
982 | ||
983 | if (x == exc) | |
984 | break; | |
985 | ||
986 | if (!(x->flags & EXC_FLAG_NODIR) && | |
987 | different_decisions(x, exc) && | |
988 | match_directory_part(pathname, pathlen, dtype, x)) | |
989 | return x; | |
990 | } | |
991 | return NULL; | |
992 | } | |
993 | ||
578cd7c3 AS |
994 | /* |
995 | * Scan the given exclude list in reverse to see whether pathname | |
996 | * should be ignored. The first match (i.e. the last on the list), if | |
997 | * any, determines the fate. Returns the exclude_list element which | |
998 | * matched, or NULL for undecided. | |
453ec4bd | 999 | */ |
578cd7c3 AS |
1000 | static struct exclude *last_exclude_matching_from_list(const char *pathname, |
1001 | int pathlen, | |
1002 | const char *basename, | |
1003 | int *dtype, | |
1004 | struct exclude_list *el) | |
453ec4bd | 1005 | { |
e6efecc4 | 1006 | struct exclude *exc = NULL; /* undecided */ |
d589a67e | 1007 | int i, maybe_descend = 0; |
453ec4bd | 1008 | |
35a94d44 | 1009 | if (!el->nr) |
578cd7c3 | 1010 | return NULL; /* undefined */ |
d6b8fc30 | 1011 | |
bac65a2b NTND |
1012 | trace_printf_key(&trace_exclude, "exclude: from %s\n", el->src); |
1013 | ||
35a94d44 NTND |
1014 | for (i = el->nr - 1; 0 <= i; i--) { |
1015 | struct exclude *x = el->excludes[i]; | |
b5592632 | 1016 | const char *exclude = x->pattern; |
b5592632 | 1017 | int prefix = x->nowildcardlen; |
35a94d44 | 1018 | |
d589a67e NTND |
1019 | if (!maybe_descend && i < el->nr - 1 && |
1020 | different_decisions(x, el->excludes[i+1])) | |
1021 | maybe_descend = 1; | |
1022 | ||
c62a9173 NTND |
1023 | if (x->sticky_paths.nr) { |
1024 | if (*dtype == DT_UNKNOWN) | |
1025 | *dtype = get_dtype(NULL, pathname, pathlen); | |
1026 | if (match_sticky(x, pathname, pathlen, *dtype)) { | |
1027 | exc = x; | |
1028 | break; | |
1029 | } | |
1030 | continue; | |
1031 | } | |
1032 | ||
35a94d44 NTND |
1033 | if (x->flags & EXC_FLAG_MUSTBEDIR) { |
1034 | if (*dtype == DT_UNKNOWN) | |
1035 | *dtype = get_dtype(NULL, pathname, pathlen); | |
1036 | if (*dtype != DT_DIR) | |
1037 | continue; | |
1038 | } | |
d6b8fc30 | 1039 | |
35a94d44 | 1040 | if (x->flags & EXC_FLAG_NODIR) { |
593cb880 NTND |
1041 | if (match_basename(basename, |
1042 | pathlen - (basename - pathname), | |
1043 | exclude, prefix, x->patternlen, | |
e6efecc4 NTND |
1044 | x->flags)) { |
1045 | exc = x; | |
1046 | break; | |
1047 | } | |
35a94d44 NTND |
1048 | continue; |
1049 | } | |
1050 | ||
b5592632 NTND |
1051 | assert(x->baselen == 0 || x->base[x->baselen - 1] == '/'); |
1052 | if (match_pathname(pathname, pathlen, | |
1053 | x->base, x->baselen ? x->baselen - 1 : 0, | |
e6efecc4 NTND |
1054 | exclude, prefix, x->patternlen, x->flags)) { |
1055 | exc = x; | |
1056 | break; | |
1057 | } | |
453ec4bd | 1058 | } |
bac65a2b NTND |
1059 | |
1060 | if (!exc) { | |
1061 | trace_printf_key(&trace_exclude, "exclude: %.*s => n/a\n", | |
1062 | pathlen, pathname); | |
1063 | return NULL; | |
1064 | } | |
1065 | ||
d589a67e NTND |
1066 | /* |
1067 | * We have found a matching pattern "exc" that may exclude whole | |
1068 | * directory. We also found that there may be a pattern that matches | |
1069 | * something inside the directory and reincludes stuff. | |
1070 | * | |
1071 | * Go through the patterns again, find that pattern and double check. | |
1072 | * If it's true, return "undecided" and keep descending in. "exc" is | |
1073 | * marked sticky so that it continues to match inside the directory. | |
1074 | */ | |
1075 | if (!(exc->flags & EXC_FLAG_NEGATIVE) && maybe_descend) { | |
1076 | struct exclude *x; | |
1077 | ||
1078 | if (*dtype == DT_UNKNOWN) | |
1079 | *dtype = get_dtype(NULL, pathname, pathlen); | |
1080 | ||
1081 | if (*dtype == DT_DIR && | |
1082 | (x = should_descend(pathname, pathlen, dtype, el, exc))) { | |
1083 | add_sticky(exc, pathname, pathlen); | |
1084 | trace_printf_key(&trace_exclude, | |
1085 | "exclude: %.*s vs %s at line %d => %s," | |
1086 | " forced open by %s at line %d => n/a\n", | |
1087 | pathlen, pathname, exc->pattern, exc->srcpos, | |
1088 | exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes", | |
1089 | x->pattern, x->srcpos); | |
1090 | return NULL; | |
1091 | } | |
1092 | } | |
1093 | ||
c62a9173 | 1094 | trace_printf_key(&trace_exclude, "exclude: %.*s vs %s at line %d => %s%s\n", |
bac65a2b | 1095 | pathlen, pathname, exc->pattern, exc->srcpos, |
c62a9173 NTND |
1096 | exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes", |
1097 | exc->sticky_paths.nr ? " (stuck)" : ""); | |
e6efecc4 | 1098 | return exc; |
578cd7c3 AS |
1099 | } |
1100 | ||
1101 | /* | |
1102 | * Scan the list and let the last match determine the fate. | |
1103 | * Return 1 for exclude, 0 for include and -1 for undecided. | |
1104 | */ | |
1105 | int is_excluded_from_list(const char *pathname, | |
1106 | int pathlen, const char *basename, int *dtype, | |
1107 | struct exclude_list *el) | |
1108 | { | |
1109 | struct exclude *exclude; | |
1110 | exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); | |
1111 | if (exclude) | |
1112 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; | |
453ec4bd LT |
1113 | return -1; /* undecided */ |
1114 | } | |
1115 | ||
46aa2f95 KB |
1116 | static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, |
1117 | const char *pathname, int pathlen, const char *basename, | |
1118 | int *dtype_p) | |
1119 | { | |
1120 | int i, j; | |
1121 | struct exclude_list_group *group; | |
1122 | struct exclude *exclude; | |
1123 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { | |
1124 | group = &dir->exclude_list_group[i]; | |
1125 | for (j = group->nr - 1; j >= 0; j--) { | |
1126 | exclude = last_exclude_matching_from_list( | |
1127 | pathname, pathlen, basename, dtype_p, | |
1128 | &group->el[j]); | |
1129 | if (exclude) | |
1130 | return exclude; | |
1131 | } | |
1132 | } | |
1133 | return NULL; | |
1134 | } | |
1135 | ||
6cd5c582 KB |
1136 | /* |
1137 | * Loads the per-directory exclude list for the substring of base | |
1138 | * which has a char length of baselen. | |
1139 | */ | |
1140 | static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) | |
1141 | { | |
1142 | struct exclude_list_group *group; | |
1143 | struct exclude_list *el; | |
1144 | struct exclude_stack *stk = NULL; | |
0dcb8d7f | 1145 | struct untracked_cache_dir *untracked; |
6cd5c582 KB |
1146 | int current; |
1147 | ||
6cd5c582 KB |
1148 | group = &dir->exclude_list_group[EXC_DIRS]; |
1149 | ||
d961baa8 NTND |
1150 | /* |
1151 | * Pop the exclude lists from the EXCL_DIRS exclude_list_group | |
6cd5c582 | 1152 | * which originate from directories not in the prefix of the |
d961baa8 NTND |
1153 | * path being checked. |
1154 | */ | |
6cd5c582 KB |
1155 | while ((stk = dir->exclude_stack) != NULL) { |
1156 | if (stk->baselen <= baselen && | |
aceb9429 | 1157 | !strncmp(dir->basebuf.buf, base, stk->baselen)) |
6cd5c582 KB |
1158 | break; |
1159 | el = &group->el[dir->exclude_stack->exclude_ix]; | |
1160 | dir->exclude_stack = stk->prev; | |
95c6f271 | 1161 | dir->exclude = NULL; |
aceb9429 | 1162 | free((char *)el->src); /* see strbuf_detach() below */ |
6cd5c582 KB |
1163 | clear_exclude_list(el); |
1164 | free(stk); | |
1165 | group->nr--; | |
1166 | } | |
1167 | ||
95c6f271 KB |
1168 | /* Skip traversing into sub directories if the parent is excluded */ |
1169 | if (dir->exclude) | |
1170 | return; | |
1171 | ||
aceb9429 NTND |
1172 | /* |
1173 | * Lazy initialization. All call sites currently just | |
1174 | * memset(dir, 0, sizeof(*dir)) before use. Changing all of | |
1175 | * them seems lots of work for little benefit. | |
1176 | */ | |
1177 | if (!dir->basebuf.buf) | |
1178 | strbuf_init(&dir->basebuf, PATH_MAX); | |
1179 | ||
6cd5c582 KB |
1180 | /* Read from the parent directories and push them down. */ |
1181 | current = stk ? stk->baselen : -1; | |
aceb9429 | 1182 | strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); |
0dcb8d7f NTND |
1183 | if (dir->untracked) |
1184 | untracked = stk ? stk->ucd : dir->untracked->root; | |
1185 | else | |
1186 | untracked = NULL; | |
1187 | ||
6cd5c582 | 1188 | while (current < baselen) { |
6cd5c582 | 1189 | const char *cp; |
0dcb8d7f | 1190 | struct sha1_stat sha1_stat; |
6cd5c582 | 1191 | |
03e11a71 | 1192 | stk = xcalloc(1, sizeof(*stk)); |
6cd5c582 KB |
1193 | if (current < 0) { |
1194 | cp = base; | |
1195 | current = 0; | |
d961baa8 | 1196 | } else { |
6cd5c582 KB |
1197 | cp = strchr(base + current + 1, '/'); |
1198 | if (!cp) | |
1199 | die("oops in prep_exclude"); | |
1200 | cp++; | |
0dcb8d7f NTND |
1201 | untracked = |
1202 | lookup_untracked(dir->untracked, untracked, | |
1203 | base + current, | |
1204 | cp - base - current); | |
6cd5c582 KB |
1205 | } |
1206 | stk->prev = dir->exclude_stack; | |
1207 | stk->baselen = cp - base; | |
95c6f271 | 1208 | stk->exclude_ix = group->nr; |
0dcb8d7f | 1209 | stk->ucd = untracked; |
95c6f271 | 1210 | el = add_exclude_list(dir, EXC_DIRS, NULL); |
aceb9429 NTND |
1211 | strbuf_add(&dir->basebuf, base + current, stk->baselen - current); |
1212 | assert(stk->baselen == dir->basebuf.len); | |
95c6f271 KB |
1213 | |
1214 | /* Abort if the directory is excluded */ | |
1215 | if (stk->baselen) { | |
1216 | int dt = DT_DIR; | |
aceb9429 | 1217 | dir->basebuf.buf[stk->baselen - 1] = 0; |
95c6f271 | 1218 | dir->exclude = last_exclude_matching_from_lists(dir, |
aceb9429 NTND |
1219 | dir->basebuf.buf, stk->baselen - 1, |
1220 | dir->basebuf.buf + current, &dt); | |
1221 | dir->basebuf.buf[stk->baselen - 1] = '/'; | |
c3c327de KB |
1222 | if (dir->exclude && |
1223 | dir->exclude->flags & EXC_FLAG_NEGATIVE) | |
1224 | dir->exclude = NULL; | |
95c6f271 | 1225 | if (dir->exclude) { |
95c6f271 KB |
1226 | dir->exclude_stack = stk; |
1227 | return; | |
1228 | } | |
1229 | } | |
1230 | ||
aceb9429 | 1231 | /* Try to read per-directory file */ |
0dcb8d7f NTND |
1232 | hashclr(sha1_stat.sha1); |
1233 | sha1_stat.valid = 0; | |
27b099ae NTND |
1234 | if (dir->exclude_per_dir && |
1235 | /* | |
1236 | * If we know that no files have been added in | |
1237 | * this directory (i.e. valid_cached_dir() has | |
1238 | * been executed and set untracked->valid) .. | |
1239 | */ | |
1240 | (!untracked || !untracked->valid || | |
1241 | /* | |
1242 | * .. and .gitignore does not exist before | |
7687252f DT |
1243 | * (i.e. null exclude_sha1). Then we can skip |
1244 | * loading .gitignore, which would result in | |
1245 | * ENOENT anyway. | |
27b099ae NTND |
1246 | */ |
1247 | !is_null_sha1(untracked->exclude_sha1))) { | |
95c6f271 KB |
1248 | /* |
1249 | * dir->basebuf gets reused by the traversal, but we | |
1250 | * need fname to remain unchanged to ensure the src | |
1251 | * member of each struct exclude correctly | |
1252 | * back-references its source file. Other invocations | |
1253 | * of add_exclude_list provide stable strings, so we | |
aceb9429 | 1254 | * strbuf_detach() and free() here in the caller. |
95c6f271 | 1255 | */ |
aceb9429 NTND |
1256 | struct strbuf sb = STRBUF_INIT; |
1257 | strbuf_addbuf(&sb, &dir->basebuf); | |
1258 | strbuf_addstr(&sb, dir->exclude_per_dir); | |
1259 | el->src = strbuf_detach(&sb, NULL); | |
0dcb8d7f NTND |
1260 | add_excludes(el->src, el->src, stk->baselen, el, 1, |
1261 | untracked ? &sha1_stat : NULL); | |
1262 | } | |
5ebf79ad NTND |
1263 | /* |
1264 | * NEEDSWORK: when untracked cache is enabled, prep_exclude() | |
1265 | * will first be called in valid_cached_dir() then maybe many | |
1266 | * times more in last_exclude_matching(). When the cache is | |
1267 | * used, last_exclude_matching() will not be called and | |
1268 | * reading .gitignore content will be a waste. | |
1269 | * | |
1270 | * So when it's called by valid_cached_dir() and we can get | |
1271 | * .gitignore SHA-1 from the index (i.e. .gitignore is not | |
1272 | * modified on work tree), we could delay reading the | |
1273 | * .gitignore content until we absolutely need it in | |
1274 | * last_exclude_matching(). Be careful about ignore rule | |
1275 | * order, though, if you do that. | |
1276 | */ | |
1277 | if (untracked && | |
1278 | hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) { | |
1279 | invalidate_gitignore(dir->untracked, untracked); | |
0dcb8d7f | 1280 | hashcpy(untracked->exclude_sha1, sha1_stat.sha1); |
95c6f271 | 1281 | } |
6cd5c582 KB |
1282 | dir->exclude_stack = stk; |
1283 | current = stk->baselen; | |
1284 | } | |
aceb9429 | 1285 | strbuf_setlen(&dir->basebuf, baselen); |
6cd5c582 KB |
1286 | } |
1287 | ||
f4cd69a6 AS |
1288 | /* |
1289 | * Loads the exclude lists for the directory containing pathname, then | |
1290 | * scans all exclude lists to determine whether pathname is excluded. | |
1291 | * Returns the exclude_list element which matched, or NULL for | |
1292 | * undecided. | |
1293 | */ | |
b07bc8c8 | 1294 | struct exclude *last_exclude_matching(struct dir_struct *dir, |
f4cd69a6 AS |
1295 | const char *pathname, |
1296 | int *dtype_p) | |
453ec4bd LT |
1297 | { |
1298 | int pathlen = strlen(pathname); | |
68492fc7 LK |
1299 | const char *basename = strrchr(pathname, '/'); |
1300 | basename = (basename) ? basename+1 : pathname; | |
453ec4bd | 1301 | |
63d285c8 | 1302 | prep_exclude(dir, pathname, basename-pathname); |
c082df24 | 1303 | |
95c6f271 KB |
1304 | if (dir->exclude) |
1305 | return dir->exclude; | |
1306 | ||
46aa2f95 KB |
1307 | return last_exclude_matching_from_lists(dir, pathname, pathlen, |
1308 | basename, dtype_p); | |
f4cd69a6 AS |
1309 | } |
1310 | ||
1311 | /* | |
1312 | * Loads the exclude lists for the directory containing pathname, then | |
1313 | * scans all exclude lists to determine whether pathname is excluded. | |
1314 | * Returns 1 if true, otherwise 0. | |
1315 | */ | |
b07bc8c8 | 1316 | int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p) |
f4cd69a6 AS |
1317 | { |
1318 | struct exclude *exclude = | |
1319 | last_exclude_matching(dir, pathname, dtype_p); | |
1320 | if (exclude) | |
1321 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; | |
453ec4bd LT |
1322 | return 0; |
1323 | } | |
1324 | ||
f3fa1838 JH |
1325 | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
1326 | { | |
453ec4bd LT |
1327 | struct dir_entry *ent; |
1328 | ||
96ffc06f | 1329 | FLEX_ALLOC_MEM(ent, name, pathname, len); |
453ec4bd | 1330 | ent->len = len; |
4d06f8ac | 1331 | return ent; |
453ec4bd LT |
1332 | } |
1333 | ||
159b3212 | 1334 | static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) |
6815e569 | 1335 | { |
ebbd7439 | 1336 | if (cache_file_exists(pathname, len, ignore_case)) |
6815e569 JK |
1337 | return NULL; |
1338 | ||
25fd2f7a | 1339 | ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
6815e569 JK |
1340 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
1341 | } | |
1342 | ||
108da0db | 1343 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len) |
2abd31b0 | 1344 | { |
6e4f981f | 1345 | if (!cache_name_is_other(pathname, len)) |
2abd31b0 JK |
1346 | return NULL; |
1347 | ||
25fd2f7a | 1348 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
2abd31b0 JK |
1349 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
1350 | } | |
1351 | ||
09595258 LT |
1352 | enum exist_status { |
1353 | index_nonexistent = 0, | |
1354 | index_directory, | |
4b05548f | 1355 | index_gitdir |
09595258 LT |
1356 | }; |
1357 | ||
5102c617 | 1358 | /* |
71261027 | 1359 | * Do not use the alphabetically sorted index to look up |
5102c617 | 1360 | * the directory name; instead, use the case insensitive |
ebbd7439 | 1361 | * directory hash. |
5102c617 JJ |
1362 | */ |
1363 | static enum exist_status directory_exists_in_index_icase(const char *dirname, int len) | |
1364 | { | |
41284eb0 | 1365 | struct cache_entry *ce; |
5102c617 | 1366 | |
41284eb0 | 1367 | if (cache_dir_exists(dirname, len)) |
5102c617 JJ |
1368 | return index_directory; |
1369 | ||
41284eb0 DT |
1370 | ce = cache_file_exists(dirname, len, ignore_case); |
1371 | if (ce && S_ISGITLINK(ce->ce_mode)) | |
5102c617 JJ |
1372 | return index_gitdir; |
1373 | ||
5102c617 JJ |
1374 | return index_nonexistent; |
1375 | } | |
1376 | ||
09595258 LT |
1377 | /* |
1378 | * The index sorts alphabetically by entry name, which | |
1379 | * means that a gitlink sorts as '\0' at the end, while | |
1380 | * a directory (which is defined not as an entry, but as | |
1381 | * the files it contains) will sort with the '/' at the | |
1382 | * end. | |
1383 | */ | |
1384 | static enum exist_status directory_exists_in_index(const char *dirname, int len) | |
453ec4bd | 1385 | { |
5102c617 JJ |
1386 | int pos; |
1387 | ||
1388 | if (ignore_case) | |
1389 | return directory_exists_in_index_icase(dirname, len); | |
1390 | ||
1391 | pos = cache_name_pos(dirname, len); | |
09595258 LT |
1392 | if (pos < 0) |
1393 | pos = -pos-1; | |
1394 | while (pos < active_nr) { | |
9c5e6c80 | 1395 | const struct cache_entry *ce = active_cache[pos++]; |
09595258 LT |
1396 | unsigned char endchar; |
1397 | ||
1398 | if (strncmp(ce->name, dirname, len)) | |
1399 | break; | |
1400 | endchar = ce->name[len]; | |
1401 | if (endchar > '/') | |
1402 | break; | |
1403 | if (endchar == '/') | |
1404 | return index_directory; | |
7a51ed66 | 1405 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
09595258 LT |
1406 | return index_gitdir; |
1407 | } | |
1408 | return index_nonexistent; | |
1409 | } | |
1410 | ||
1411 | /* | |
1412 | * When we find a directory when traversing the filesystem, we | |
1413 | * have three distinct cases: | |
1414 | * | |
1415 | * - ignore it | |
1416 | * - see it as a directory | |
1417 | * - recurse into it | |
1418 | * | |
1419 | * and which one we choose depends on a combination of existing | |
1420 | * git index contents and the flags passed into the directory | |
1421 | * traversal routine. | |
1422 | * | |
1423 | * Case 1: If we *already* have entries in the index under that | |
5bd8e2d8 KB |
1424 | * directory name, we always recurse into the directory to see |
1425 | * all the files. | |
09595258 LT |
1426 | * |
1427 | * Case 2: If we *already* have that directory name as a gitlink, | |
1428 | * we always continue to see it as a gitlink, regardless of whether | |
1429 | * there is an actual git directory there or not (it might not | |
1430 | * be checked out as a subproject!) | |
1431 | * | |
1432 | * Case 3: if we didn't have it in the index previously, we | |
1433 | * have a few sub-cases: | |
1434 | * | |
1435 | * (a) if "show_other_directories" is true, we show it as | |
1436 | * just a directory, unless "hide_empty_directories" is | |
defd7c7b KB |
1437 | * also true, in which case we need to check if it contains any |
1438 | * untracked and / or ignored files. | |
09595258 | 1439 | * (b) if it looks like a git directory, and we don't have |
302b9282 | 1440 | * 'no_gitlinks' set we treat it as a gitlink, and show it |
09595258 LT |
1441 | * as a directory. |
1442 | * (c) otherwise, we recurse into it. | |
1443 | */ | |
defd7c7b | 1444 | static enum path_treatment treat_directory(struct dir_struct *dir, |
0dcb8d7f | 1445 | struct untracked_cache_dir *untracked, |
2e5910f2 | 1446 | const char *dirname, int len, int baselen, int exclude, |
09595258 LT |
1447 | const struct path_simplify *simplify) |
1448 | { | |
1449 | /* The "len-1" is to strip the final '/' */ | |
1450 | switch (directory_exists_in_index(dirname, len-1)) { | |
1451 | case index_directory: | |
defd7c7b | 1452 | return path_recurse; |
09595258 LT |
1453 | |
1454 | case index_gitdir: | |
26c986e1 | 1455 | return path_none; |
09595258 LT |
1456 | |
1457 | case index_nonexistent: | |
7c4c97c0 | 1458 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
09595258 | 1459 | break; |
7c4c97c0 | 1460 | if (!(dir->flags & DIR_NO_GITLINKS)) { |
09595258 LT |
1461 | unsigned char sha1[20]; |
1462 | if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0) | |
defd7c7b | 1463 | return path_untracked; |
09595258 | 1464 | } |
defd7c7b | 1465 | return path_recurse; |
09595258 LT |
1466 | } |
1467 | ||
1468 | /* This is the "show_other_directories" case */ | |
721ac4ed | 1469 | |
184d2a8e | 1470 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
defd7c7b KB |
1471 | return exclude ? path_excluded : path_untracked; |
1472 | ||
2e5910f2 DT |
1473 | untracked = lookup_untracked(dir->untracked, untracked, |
1474 | dirname + baselen, len - baselen); | |
0dcb8d7f NTND |
1475 | return read_directory_recursive(dir, dirname, len, |
1476 | untracked, 1, simplify); | |
453ec4bd LT |
1477 | } |
1478 | ||
9fc42d60 LT |
1479 | /* |
1480 | * This is an inexact early pruning of any recursive directory | |
1481 | * reading - if the path cannot possibly be in the pathspec, | |
1482 | * return true, and we'll skip it early. | |
1483 | */ | |
1484 | static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify) | |
1485 | { | |
1486 | if (simplify) { | |
1487 | for (;;) { | |
1488 | const char *match = simplify->path; | |
1489 | int len = simplify->len; | |
1490 | ||
1491 | if (!match) | |
1492 | break; | |
1493 | if (len > pathlen) | |
1494 | len = pathlen; | |
1495 | if (!memcmp(path, match, len)) | |
1496 | return 0; | |
1497 | simplify++; | |
1498 | } | |
1499 | return 1; | |
1500 | } | |
1501 | return 0; | |
1502 | } | |
1503 | ||
29209cbe JK |
1504 | /* |
1505 | * This function tells us whether an excluded path matches a | |
1506 | * list of "interesting" pathspecs. That is, whether a path matched | |
1507 | * by any of the pathspecs could possibly be ignored by excluding | |
1508 | * the specified path. This can happen if: | |
1509 | * | |
1510 | * 1. the path is mentioned explicitly in the pathspec | |
1511 | * | |
1512 | * 2. the path is a directory prefix of some element in the | |
1513 | * pathspec | |
1514 | */ | |
1515 | static int exclude_matches_pathspec(const char *path, int len, | |
1516 | const struct path_simplify *simplify) | |
e96980ef JK |
1517 | { |
1518 | if (simplify) { | |
1519 | for (; simplify->path; simplify++) { | |
1520 | if (len == simplify->len | |
1521 | && !memcmp(path, simplify->path, len)) | |
1522 | return 1; | |
29209cbe JK |
1523 | if (len < simplify->len |
1524 | && simplify->path[len] == '/' | |
1525 | && !memcmp(path, simplify->path, len)) | |
1526 | return 1; | |
e96980ef JK |
1527 | } |
1528 | } | |
1529 | return 0; | |
1530 | } | |
1531 | ||
443e061a LT |
1532 | static int get_index_dtype(const char *path, int len) |
1533 | { | |
1534 | int pos; | |
9c5e6c80 | 1535 | const struct cache_entry *ce; |
443e061a | 1536 | |
ebbd7439 | 1537 | ce = cache_file_exists(path, len, 0); |
443e061a LT |
1538 | if (ce) { |
1539 | if (!ce_uptodate(ce)) | |
1540 | return DT_UNKNOWN; | |
1541 | if (S_ISGITLINK(ce->ce_mode)) | |
1542 | return DT_DIR; | |
1543 | /* | |
1544 | * Nobody actually cares about the | |
1545 | * difference between DT_LNK and DT_REG | |
1546 | */ | |
1547 | return DT_REG; | |
1548 | } | |
1549 | ||
1550 | /* Try to look it up as a directory */ | |
1551 | pos = cache_name_pos(path, len); | |
1552 | if (pos >= 0) | |
1553 | return DT_UNKNOWN; | |
1554 | pos = -pos-1; | |
1555 | while (pos < active_nr) { | |
1556 | ce = active_cache[pos++]; | |
1557 | if (strncmp(ce->name, path, len)) | |
1558 | break; | |
1559 | if (ce->name[len] > '/') | |
1560 | break; | |
1561 | if (ce->name[len] < '/') | |
1562 | continue; | |
1563 | if (!ce_uptodate(ce)) | |
1564 | break; /* continue? */ | |
1565 | return DT_DIR; | |
1566 | } | |
1567 | return DT_UNKNOWN; | |
1568 | } | |
1569 | ||
caa6b782 | 1570 | static int get_dtype(struct dirent *de, const char *path, int len) |
07134421 | 1571 | { |
6831a88a | 1572 | int dtype = de ? DTYPE(de) : DT_UNKNOWN; |
07134421 LT |
1573 | struct stat st; |
1574 | ||
1575 | if (dtype != DT_UNKNOWN) | |
1576 | return dtype; | |
443e061a LT |
1577 | dtype = get_index_dtype(path, len); |
1578 | if (dtype != DT_UNKNOWN) | |
1579 | return dtype; | |
1580 | if (lstat(path, &st)) | |
07134421 LT |
1581 | return dtype; |
1582 | if (S_ISREG(st.st_mode)) | |
1583 | return DT_REG; | |
1584 | if (S_ISDIR(st.st_mode)) | |
1585 | return DT_DIR; | |
1586 | if (S_ISLNK(st.st_mode)) | |
1587 | return DT_LNK; | |
1588 | return dtype; | |
1589 | } | |
1590 | ||
16e2cfa9 | 1591 | static enum path_treatment treat_one_path(struct dir_struct *dir, |
0dcb8d7f | 1592 | struct untracked_cache_dir *untracked, |
49dc2cc2 | 1593 | struct strbuf *path, |
2e5910f2 | 1594 | int baselen, |
16e2cfa9 JH |
1595 | const struct path_simplify *simplify, |
1596 | int dtype, struct dirent *de) | |
53cc5356 | 1597 | { |
8aaf8d77 | 1598 | int exclude; |
ebbd7439 | 1599 | int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case); |
2eac2a4c | 1600 | |
8aaf8d77 KB |
1601 | if (dtype == DT_UNKNOWN) |
1602 | dtype = get_dtype(de, path->buf, path->len); | |
1603 | ||
1604 | /* Always exclude indexed files */ | |
2eac2a4c | 1605 | if (dtype != DT_DIR && has_path_in_index) |
defd7c7b | 1606 | return path_none; |
8aaf8d77 | 1607 | |
2eac2a4c JH |
1608 | /* |
1609 | * When we are looking at a directory P in the working tree, | |
1610 | * there are three cases: | |
1611 | * | |
1612 | * (1) P exists in the index. Everything inside the directory P in | |
1613 | * the working tree needs to go when P is checked out from the | |
1614 | * index. | |
1615 | * | |
1616 | * (2) P does not exist in the index, but there is P/Q in the index. | |
1617 | * We know P will stay a directory when we check out the contents | |
1618 | * of the index, but we do not know yet if there is a directory | |
1619 | * P/Q in the working tree to be killed, so we need to recurse. | |
1620 | * | |
1621 | * (3) P does not exist in the index, and there is no P/Q in the index | |
1622 | * to require P to be a directory, either. Only in this case, we | |
1623 | * know that everything inside P will not be killed without | |
1624 | * recursing. | |
1625 | */ | |
1626 | if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && | |
1627 | (dtype == DT_DIR) && | |
de372b1b ES |
1628 | !has_path_in_index && |
1629 | (directory_exists_in_index(path->buf, path->len) == index_nonexistent)) | |
1630 | return path_none; | |
8aaf8d77 KB |
1631 | |
1632 | exclude = is_excluded(dir, path->buf, &dtype); | |
53cc5356 JH |
1633 | |
1634 | /* | |
1635 | * Excluded? If we don't explicitly want to show | |
1636 | * ignored files, ignore it | |
1637 | */ | |
0aaf62b6 | 1638 | if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO))) |
defd7c7b | 1639 | return path_excluded; |
53cc5356 | 1640 | |
53cc5356 JH |
1641 | switch (dtype) { |
1642 | default: | |
defd7c7b | 1643 | return path_none; |
53cc5356 | 1644 | case DT_DIR: |
49dc2cc2 | 1645 | strbuf_addch(path, '/'); |
2e5910f2 DT |
1646 | return treat_directory(dir, untracked, path->buf, path->len, |
1647 | baselen, exclude, simplify); | |
53cc5356 JH |
1648 | case DT_REG: |
1649 | case DT_LNK: | |
defd7c7b | 1650 | return exclude ? path_excluded : path_untracked; |
53cc5356 | 1651 | } |
53cc5356 JH |
1652 | } |
1653 | ||
91a2288b NTND |
1654 | static enum path_treatment treat_path_fast(struct dir_struct *dir, |
1655 | struct untracked_cache_dir *untracked, | |
1656 | struct cached_dir *cdir, | |
1657 | struct strbuf *path, | |
1658 | int baselen, | |
1659 | const struct path_simplify *simplify) | |
1660 | { | |
1661 | strbuf_setlen(path, baselen); | |
1662 | if (!cdir->ucd) { | |
1663 | strbuf_addstr(path, cdir->file); | |
1664 | return path_untracked; | |
1665 | } | |
1666 | strbuf_addstr(path, cdir->ucd->name); | |
1667 | /* treat_one_path() does this before it calls treat_directory() */ | |
00b6c178 | 1668 | strbuf_complete(path, '/'); |
91a2288b NTND |
1669 | if (cdir->ucd->check_only) |
1670 | /* | |
1671 | * check_only is set as a result of treat_directory() getting | |
1672 | * to its bottom. Verify again the same set of directories | |
1673 | * with check_only set. | |
1674 | */ | |
1675 | return read_directory_recursive(dir, path->buf, path->len, | |
1676 | cdir->ucd, 1, simplify); | |
1677 | /* | |
1678 | * We get path_recurse in the first run when | |
1679 | * directory_exists_in_index() returns index_nonexistent. We | |
1680 | * are sure that new changes in the index does not impact the | |
1681 | * outcome. Return now. | |
1682 | */ | |
1683 | return path_recurse; | |
1684 | } | |
1685 | ||
16e2cfa9 | 1686 | static enum path_treatment treat_path(struct dir_struct *dir, |
0dcb8d7f | 1687 | struct untracked_cache_dir *untracked, |
cf7c6148 | 1688 | struct cached_dir *cdir, |
49dc2cc2 | 1689 | struct strbuf *path, |
16e2cfa9 | 1690 | int baselen, |
49dc2cc2 | 1691 | const struct path_simplify *simplify) |
16e2cfa9 JH |
1692 | { |
1693 | int dtype; | |
cf7c6148 | 1694 | struct dirent *de = cdir->de; |
16e2cfa9 | 1695 | |
91a2288b NTND |
1696 | if (!de) |
1697 | return treat_path_fast(dir, untracked, cdir, path, | |
1698 | baselen, simplify); | |
16e2cfa9 | 1699 | if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git")) |
defd7c7b | 1700 | return path_none; |
49dc2cc2 RS |
1701 | strbuf_setlen(path, baselen); |
1702 | strbuf_addstr(path, de->d_name); | |
1703 | if (simplify_away(path->buf, path->len, simplify)) | |
defd7c7b | 1704 | return path_none; |
16e2cfa9 JH |
1705 | |
1706 | dtype = DTYPE(de); | |
2e5910f2 | 1707 | return treat_one_path(dir, untracked, path, baselen, simplify, dtype, de); |
0dcb8d7f NTND |
1708 | } |
1709 | ||
1710 | static void add_untracked(struct untracked_cache_dir *dir, const char *name) | |
1711 | { | |
1712 | if (!dir) | |
1713 | return; | |
1714 | ALLOC_GROW(dir->untracked, dir->untracked_nr + 1, | |
1715 | dir->untracked_alloc); | |
1716 | dir->untracked[dir->untracked_nr++] = xstrdup(name); | |
16e2cfa9 JH |
1717 | } |
1718 | ||
91a2288b NTND |
1719 | static int valid_cached_dir(struct dir_struct *dir, |
1720 | struct untracked_cache_dir *untracked, | |
1721 | struct strbuf *path, | |
1722 | int check_only) | |
1723 | { | |
1724 | struct stat st; | |
1725 | ||
1726 | if (!untracked) | |
1727 | return 0; | |
1728 | ||
1729 | if (stat(path->len ? path->buf : ".", &st)) { | |
1730 | invalidate_directory(dir->untracked, untracked); | |
1731 | memset(&untracked->stat_data, 0, sizeof(untracked->stat_data)); | |
1732 | return 0; | |
1733 | } | |
1734 | if (!untracked->valid || | |
ed4efab1 | 1735 | match_stat_data_racy(&the_index, &untracked->stat_data, &st)) { |
91a2288b NTND |
1736 | if (untracked->valid) |
1737 | invalidate_directory(dir->untracked, untracked); | |
1738 | fill_stat_data(&untracked->stat_data, &st); | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | if (untracked->check_only != !!check_only) { | |
1743 | invalidate_directory(dir->untracked, untracked); | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | /* | |
1748 | * prep_exclude will be called eventually on this directory, | |
1749 | * but it's called much later in last_exclude_matching(). We | |
1750 | * need it now to determine the validity of the cache for this | |
1751 | * path. The next calls will be nearly no-op, the way | |
1752 | * prep_exclude() is designed. | |
1753 | */ | |
1754 | if (path->len && path->buf[path->len - 1] != '/') { | |
1755 | strbuf_addch(path, '/'); | |
1756 | prep_exclude(dir, path->buf, path->len); | |
1757 | strbuf_setlen(path, path->len - 1); | |
1758 | } else | |
1759 | prep_exclude(dir, path->buf, path->len); | |
1760 | ||
1761 | /* hopefully prep_exclude() haven't invalidated this entry... */ | |
1762 | return untracked->valid; | |
1763 | } | |
1764 | ||
cf7c6148 NTND |
1765 | static int open_cached_dir(struct cached_dir *cdir, |
1766 | struct dir_struct *dir, | |
1767 | struct untracked_cache_dir *untracked, | |
1768 | struct strbuf *path, | |
1769 | int check_only) | |
1770 | { | |
1771 | memset(cdir, 0, sizeof(*cdir)); | |
1772 | cdir->untracked = untracked; | |
91a2288b NTND |
1773 | if (valid_cached_dir(dir, untracked, path, check_only)) |
1774 | return 0; | |
cf7c6148 | 1775 | cdir->fdir = opendir(path->len ? path->buf : "."); |
91a2288b NTND |
1776 | if (dir->untracked) |
1777 | dir->untracked->dir_opened++; | |
cf7c6148 NTND |
1778 | if (!cdir->fdir) |
1779 | return -1; | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | static int read_cached_dir(struct cached_dir *cdir) | |
1784 | { | |
1785 | if (cdir->fdir) { | |
1786 | cdir->de = readdir(cdir->fdir); | |
1787 | if (!cdir->de) | |
1788 | return -1; | |
1789 | return 0; | |
1790 | } | |
91a2288b NTND |
1791 | while (cdir->nr_dirs < cdir->untracked->dirs_nr) { |
1792 | struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs]; | |
26cb0182 NTND |
1793 | if (!d->recurse) { |
1794 | cdir->nr_dirs++; | |
1795 | continue; | |
1796 | } | |
91a2288b NTND |
1797 | cdir->ucd = d; |
1798 | cdir->nr_dirs++; | |
1799 | return 0; | |
1800 | } | |
1801 | cdir->ucd = NULL; | |
1802 | if (cdir->nr_files < cdir->untracked->untracked_nr) { | |
1803 | struct untracked_cache_dir *d = cdir->untracked; | |
1804 | cdir->file = d->untracked[cdir->nr_files++]; | |
1805 | return 0; | |
1806 | } | |
cf7c6148 NTND |
1807 | return -1; |
1808 | } | |
1809 | ||
1810 | static void close_cached_dir(struct cached_dir *cdir) | |
1811 | { | |
1812 | if (cdir->fdir) | |
1813 | closedir(cdir->fdir); | |
91a2288b NTND |
1814 | /* |
1815 | * We have gone through this directory and found no untracked | |
1816 | * entries. Mark it valid. | |
1817 | */ | |
26cb0182 | 1818 | if (cdir->untracked) { |
91a2288b | 1819 | cdir->untracked->valid = 1; |
26cb0182 NTND |
1820 | cdir->untracked->recurse = 1; |
1821 | } | |
16e2cfa9 JH |
1822 | } |
1823 | ||
453ec4bd LT |
1824 | /* |
1825 | * Read a directory tree. We currently ignore anything but | |
1826 | * directories, regular files and symlinks. That's because git | |
1827 | * doesn't handle them at all yet. Maybe that will change some | |
1828 | * day. | |
1829 | * | |
1830 | * Also, we ignore the name ".git" (even if it is not a directory). | |
1831 | * That likely will not change. | |
defd7c7b KB |
1832 | * |
1833 | * Returns the most significant path_treatment value encountered in the scan. | |
453ec4bd | 1834 | */ |
defd7c7b | 1835 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
53cc5356 | 1836 | const char *base, int baselen, |
0dcb8d7f | 1837 | struct untracked_cache_dir *untracked, int check_only, |
53cc5356 | 1838 | const struct path_simplify *simplify) |
453ec4bd | 1839 | { |
cf7c6148 | 1840 | struct cached_dir cdir; |
defd7c7b | 1841 | enum path_treatment state, subdir_state, dir_state = path_none; |
bef36921 | 1842 | struct strbuf path = STRBUF_INIT; |
bac65a2b | 1843 | static int level = 0; |
453ec4bd | 1844 | |
bef36921 | 1845 | strbuf_add(&path, base, baselen); |
02cb6753 | 1846 | |
bac65a2b NTND |
1847 | trace_printf_key(&trace_exclude, "exclude: [%d] enter '%.*s'\n", |
1848 | level++, baselen, base); | |
1849 | ||
cf7c6148 | 1850 | if (open_cached_dir(&cdir, dir, untracked, &path, check_only)) |
1528d247 RS |
1851 | goto out; |
1852 | ||
0dcb8d7f NTND |
1853 | if (untracked) |
1854 | untracked->check_only = !!check_only; | |
1855 | ||
cf7c6148 | 1856 | while (!read_cached_dir(&cdir)) { |
defd7c7b | 1857 | /* check how the file or directory should be treated */ |
cf7c6148 | 1858 | state = treat_path(dir, untracked, &cdir, &path, baselen, simplify); |
0dcb8d7f | 1859 | |
defd7c7b KB |
1860 | if (state > dir_state) |
1861 | dir_state = state; | |
1862 | ||
1863 | /* recurse into subdir if instructed by treat_path */ | |
1864 | if (state == path_recurse) { | |
0dcb8d7f NTND |
1865 | struct untracked_cache_dir *ud; |
1866 | ud = lookup_untracked(dir->untracked, untracked, | |
1867 | path.buf + baselen, | |
1868 | path.len - baselen); | |
1869 | subdir_state = | |
1870 | read_directory_recursive(dir, path.buf, path.len, | |
1871 | ud, check_only, simplify); | |
defd7c7b KB |
1872 | if (subdir_state > dir_state) |
1873 | dir_state = subdir_state; | |
1874 | } | |
1875 | ||
1876 | if (check_only) { | |
1877 | /* abort early if maximum state has been reached */ | |
0dcb8d7f | 1878 | if (dir_state == path_untracked) { |
91a2288b | 1879 | if (cdir.fdir) |
0dcb8d7f | 1880 | add_untracked(untracked, path.buf + baselen); |
defd7c7b | 1881 | break; |
0dcb8d7f | 1882 | } |
defd7c7b | 1883 | /* skip the dir_add_* part */ |
02cb6753 | 1884 | continue; |
453ec4bd | 1885 | } |
defd7c7b KB |
1886 | |
1887 | /* add the path to the appropriate result list */ | |
1888 | switch (state) { | |
1889 | case path_excluded: | |
1890 | if (dir->flags & DIR_SHOW_IGNORED) | |
1891 | dir_add_name(dir, path.buf, path.len); | |
0aaf62b6 KB |
1892 | else if ((dir->flags & DIR_SHOW_IGNORED_TOO) || |
1893 | ((dir->flags & DIR_COLLECT_IGNORED) && | |
1894 | exclude_matches_pathspec(path.buf, path.len, | |
1895 | simplify))) | |
1896 | dir_add_ignored(dir, path.buf, path.len); | |
defd7c7b KB |
1897 | break; |
1898 | ||
1899 | case path_untracked: | |
0dcb8d7f NTND |
1900 | if (dir->flags & DIR_SHOW_IGNORED) |
1901 | break; | |
1902 | dir_add_name(dir, path.buf, path.len); | |
91a2288b | 1903 | if (cdir.fdir) |
0dcb8d7f | 1904 | add_untracked(untracked, path.buf + baselen); |
1528d247 | 1905 | break; |
defd7c7b KB |
1906 | |
1907 | default: | |
1908 | break; | |
1909 | } | |
453ec4bd | 1910 | } |
cf7c6148 | 1911 | close_cached_dir(&cdir); |
1528d247 | 1912 | out: |
bac65a2b NTND |
1913 | trace_printf_key(&trace_exclude, "exclude: [%d] leave '%.*s'\n", |
1914 | --level, baselen, base); | |
bef36921 | 1915 | strbuf_release(&path); |
453ec4bd | 1916 | |
defd7c7b | 1917 | return dir_state; |
453ec4bd LT |
1918 | } |
1919 | ||
1920 | static int cmp_name(const void *p1, const void *p2) | |
1921 | { | |
1922 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; | |
1923 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; | |
1924 | ||
ccdd4a0f | 1925 | return name_compare(e1->name, e1->len, e2->name, e2->len); |
453ec4bd LT |
1926 | } |
1927 | ||
9fc42d60 LT |
1928 | static struct path_simplify *create_simplify(const char **pathspec) |
1929 | { | |
1930 | int nr, alloc = 0; | |
1931 | struct path_simplify *simplify = NULL; | |
1932 | ||
1933 | if (!pathspec) | |
1934 | return NULL; | |
1935 | ||
1936 | for (nr = 0 ; ; nr++) { | |
1937 | const char *match; | |
9af49f82 | 1938 | ALLOC_GROW(simplify, nr + 1, alloc); |
9fc42d60 LT |
1939 | match = *pathspec++; |
1940 | if (!match) | |
1941 | break; | |
1942 | simplify[nr].path = match; | |
1943 | simplify[nr].len = simple_length(match); | |
1944 | } | |
1945 | simplify[nr].path = NULL; | |
1946 | simplify[nr].len = 0; | |
1947 | return simplify; | |
1948 | } | |
1949 | ||
1950 | static void free_simplify(struct path_simplify *simplify) | |
1951 | { | |
8e0f7003 | 1952 | free(simplify); |
9fc42d60 LT |
1953 | } |
1954 | ||
48ffef96 JH |
1955 | static int treat_leading_path(struct dir_struct *dir, |
1956 | const char *path, int len, | |
1957 | const struct path_simplify *simplify) | |
1958 | { | |
49dc2cc2 RS |
1959 | struct strbuf sb = STRBUF_INIT; |
1960 | int baselen, rc = 0; | |
48ffef96 | 1961 | const char *cp; |
be8a84c5 | 1962 | int old_flags = dir->flags; |
48ffef96 JH |
1963 | |
1964 | while (len && path[len - 1] == '/') | |
1965 | len--; | |
1966 | if (!len) | |
1967 | return 1; | |
1968 | baselen = 0; | |
be8a84c5 | 1969 | dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES; |
48ffef96 JH |
1970 | while (1) { |
1971 | cp = path + baselen + !!baselen; | |
1972 | cp = memchr(cp, '/', path + len - cp); | |
1973 | if (!cp) | |
1974 | baselen = len; | |
1975 | else | |
1976 | baselen = cp - path; | |
49dc2cc2 RS |
1977 | strbuf_setlen(&sb, 0); |
1978 | strbuf_add(&sb, path, baselen); | |
1979 | if (!is_directory(sb.buf)) | |
1980 | break; | |
1981 | if (simplify_away(sb.buf, sb.len, simplify)) | |
1982 | break; | |
2e5910f2 | 1983 | if (treat_one_path(dir, NULL, &sb, baselen, simplify, |
defd7c7b | 1984 | DT_DIR, NULL) == path_none) |
49dc2cc2 RS |
1985 | break; /* do not recurse into it */ |
1986 | if (len <= baselen) { | |
1987 | rc = 1; | |
1988 | break; /* finished checking */ | |
1989 | } | |
48ffef96 | 1990 | } |
49dc2cc2 | 1991 | strbuf_release(&sb); |
be8a84c5 | 1992 | dir->flags = old_flags; |
49dc2cc2 | 1993 | return rc; |
48ffef96 JH |
1994 | } |
1995 | ||
1e8fef60 NTND |
1996 | static const char *get_ident_string(void) |
1997 | { | |
1998 | static struct strbuf sb = STRBUF_INIT; | |
1999 | struct utsname uts; | |
2000 | ||
2001 | if (sb.len) | |
2002 | return sb.buf; | |
100e4337 | 2003 | if (uname(&uts) < 0) |
1e8fef60 | 2004 | die_errno(_("failed to get kernel name and information")); |
0e0f7618 CC |
2005 | strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(), |
2006 | uts.sysname); | |
1e8fef60 NTND |
2007 | return sb.buf; |
2008 | } | |
2009 | ||
2010 | static int ident_in_untracked(const struct untracked_cache *uc) | |
2011 | { | |
0e0f7618 CC |
2012 | /* |
2013 | * Previous git versions may have saved many NUL separated | |
2014 | * strings in the "ident" field, but it is insane to manage | |
2015 | * many locations, so just take care of the first one. | |
2016 | */ | |
1e8fef60 | 2017 | |
0e0f7618 | 2018 | return !strcmp(uc->ident.buf, get_ident_string()); |
1e8fef60 NTND |
2019 | } |
2020 | ||
0e0f7618 | 2021 | static void set_untracked_ident(struct untracked_cache *uc) |
1e8fef60 | 2022 | { |
0e0f7618 | 2023 | strbuf_reset(&uc->ident); |
1e8fef60 | 2024 | strbuf_addstr(&uc->ident, get_ident_string()); |
0e0f7618 CC |
2025 | |
2026 | /* | |
2027 | * This strbuf used to contain a list of NUL separated | |
2028 | * strings, so save NUL too for backward compatibility. | |
2029 | */ | |
1e8fef60 NTND |
2030 | strbuf_addch(&uc->ident, 0); |
2031 | } | |
2032 | ||
4a4ca479 CC |
2033 | static void new_untracked_cache(struct index_state *istate) |
2034 | { | |
2035 | struct untracked_cache *uc = xcalloc(1, sizeof(*uc)); | |
2036 | strbuf_init(&uc->ident, 100); | |
2037 | uc->exclude_per_dir = ".gitignore"; | |
2038 | /* should be the same flags used by git-status */ | |
2039 | uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
0e0f7618 | 2040 | set_untracked_ident(uc); |
4a4ca479 | 2041 | istate->untracked = uc; |
0e0f7618 | 2042 | istate->cache_changed |= UNTRACKED_CHANGED; |
4a4ca479 CC |
2043 | } |
2044 | ||
2045 | void add_untracked_cache(struct index_state *istate) | |
2046 | { | |
2047 | if (!istate->untracked) { | |
2048 | new_untracked_cache(istate); | |
0e0f7618 CC |
2049 | } else { |
2050 | if (!ident_in_untracked(istate->untracked)) { | |
2051 | free_untracked_cache(istate->untracked); | |
2052 | new_untracked_cache(istate); | |
2053 | } | |
2054 | } | |
4a4ca479 CC |
2055 | } |
2056 | ||
07b29bfd CC |
2057 | void remove_untracked_cache(struct index_state *istate) |
2058 | { | |
2059 | if (istate->untracked) { | |
2060 | free_untracked_cache(istate->untracked); | |
2061 | istate->untracked = NULL; | |
2062 | istate->cache_changed |= UNTRACKED_CHANGED; | |
2063 | } | |
2064 | } | |
2065 | ||
ccad261f NTND |
2066 | static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir, |
2067 | int base_len, | |
2068 | const struct pathspec *pathspec) | |
2069 | { | |
2070 | struct untracked_cache_dir *root; | |
2071 | ||
76e6b090 | 2072 | if (!dir->untracked || getenv("GIT_DISABLE_UNTRACKED_CACHE")) |
ccad261f NTND |
2073 | return NULL; |
2074 | ||
2075 | /* | |
2076 | * We only support $GIT_DIR/info/exclude and core.excludesfile | |
2077 | * as the global ignore rule files. Any other additions | |
2078 | * (e.g. from command line) invalidate the cache. This | |
2079 | * condition also catches running setup_standard_excludes() | |
2080 | * before setting dir->untracked! | |
2081 | */ | |
2082 | if (dir->unmanaged_exclude_files) | |
2083 | return NULL; | |
2084 | ||
2085 | /* | |
2086 | * Optimize for the main use case only: whole-tree git | |
2087 | * status. More work involved in treat_leading_path() if we | |
2088 | * use cache on just a subset of the worktree. pathspec | |
2089 | * support could make the matter even worse. | |
2090 | */ | |
2091 | if (base_len || (pathspec && pathspec->nr)) | |
2092 | return NULL; | |
2093 | ||
2094 | /* Different set of flags may produce different results */ | |
2095 | if (dir->flags != dir->untracked->dir_flags || | |
2096 | /* | |
2097 | * See treat_directory(), case index_nonexistent. Without | |
2098 | * this flag, we may need to also cache .git file content | |
2099 | * for the resolve_gitlink_ref() call, which we don't. | |
2100 | */ | |
2101 | !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) || | |
2102 | /* We don't support collecting ignore files */ | |
2103 | (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO | | |
2104 | DIR_COLLECT_IGNORED))) | |
2105 | return NULL; | |
2106 | ||
2107 | /* | |
2108 | * If we use .gitignore in the cache and now you change it to | |
2109 | * .gitexclude, everything will go wrong. | |
2110 | */ | |
2111 | if (dir->exclude_per_dir != dir->untracked->exclude_per_dir && | |
2112 | strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir)) | |
2113 | return NULL; | |
2114 | ||
2115 | /* | |
2116 | * EXC_CMDL is not considered in the cache. If people set it, | |
2117 | * skip the cache. | |
2118 | */ | |
2119 | if (dir->exclude_list_group[EXC_CMDL].nr) | |
2120 | return NULL; | |
2121 | ||
1e8fef60 | 2122 | if (!ident_in_untracked(dir->untracked)) { |
0e0f7618 | 2123 | warning(_("Untracked cache is disabled on this system or location.")); |
1e8fef60 NTND |
2124 | return NULL; |
2125 | } | |
2126 | ||
ccad261f NTND |
2127 | if (!dir->untracked->root) { |
2128 | const int len = sizeof(*dir->untracked->root); | |
2129 | dir->untracked->root = xmalloc(len); | |
2130 | memset(dir->untracked->root, 0, len); | |
2131 | } | |
2132 | ||
2133 | /* Validate $GIT_DIR/info/exclude and core.excludesfile */ | |
2134 | root = dir->untracked->root; | |
2135 | if (hashcmp(dir->ss_info_exclude.sha1, | |
2136 | dir->untracked->ss_info_exclude.sha1)) { | |
2137 | invalidate_gitignore(dir->untracked, root); | |
2138 | dir->untracked->ss_info_exclude = dir->ss_info_exclude; | |
2139 | } | |
2140 | if (hashcmp(dir->ss_excludes_file.sha1, | |
2141 | dir->untracked->ss_excludes_file.sha1)) { | |
2142 | invalidate_gitignore(dir->untracked, root); | |
2143 | dir->untracked->ss_excludes_file = dir->ss_excludes_file; | |
2144 | } | |
26cb0182 NTND |
2145 | |
2146 | /* Make sure this directory is not dropped out at saving phase */ | |
2147 | root->recurse = 1; | |
ccad261f NTND |
2148 | return root; |
2149 | } | |
2150 | ||
c62a9173 NTND |
2151 | static void clear_sticky(struct dir_struct *dir) |
2152 | { | |
2153 | struct exclude_list_group *g; | |
2154 | struct exclude_list *el; | |
2155 | struct exclude *x; | |
2156 | int i, j, k; | |
2157 | ||
2158 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { | |
2159 | g = &dir->exclude_list_group[i]; | |
2160 | for (j = g->nr - 1; j >= 0; j--) { | |
2161 | el = &g->el[j]; | |
2162 | for (k = el->nr - 1; 0 <= k; k--) { | |
2163 | x = el->excludes[k]; | |
2164 | string_list_clear(&x->sticky_paths, 0); | |
2165 | } | |
2166 | } | |
2167 | } | |
2168 | } | |
2169 | ||
7327d3d1 | 2170 | int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec) |
9fc42d60 | 2171 | { |
725b0605 | 2172 | struct path_simplify *simplify; |
ccad261f | 2173 | struct untracked_cache_dir *untracked; |
b4189aa8 | 2174 | |
7327d3d1 NTND |
2175 | /* |
2176 | * Check out create_simplify() | |
2177 | */ | |
2178 | if (pathspec) | |
5c6933d2 NTND |
2179 | GUARD_PATHSPEC(pathspec, |
2180 | PATHSPEC_FROMTOP | | |
2181 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 2182 | PATHSPEC_LITERAL | |
93d93537 | 2183 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
2184 | PATHSPEC_ICASE | |
2185 | PATHSPEC_EXCLUDE); | |
7327d3d1 | 2186 | |
dba2e203 | 2187 | if (has_symlink_leading_path(path, len)) |
725b0605 JH |
2188 | return dir->nr; |
2189 | ||
d589a67e NTND |
2190 | /* |
2191 | * Stay on the safe side. if read_directory() has run once on | |
2192 | * "dir", some sticky flag may have been left. Clear them all. | |
2193 | */ | |
2194 | clear_sticky(dir); | |
2195 | ||
ef79b1f8 NTND |
2196 | /* |
2197 | * exclude patterns are treated like positive ones in | |
2198 | * create_simplify. Usually exclude patterns should be a | |
2199 | * subset of positive ones, which has no impacts on | |
2200 | * create_simplify(). | |
2201 | */ | |
b3920bbd | 2202 | simplify = create_simplify(pathspec ? pathspec->_raw : NULL); |
ccad261f NTND |
2203 | untracked = validate_untracked_cache(dir, len, pathspec); |
2204 | if (!untracked) | |
2205 | /* | |
2206 | * make sure untracked cache code path is disabled, | |
2207 | * e.g. prep_exclude() | |
2208 | */ | |
2209 | dir->untracked = NULL; | |
48ffef96 | 2210 | if (!len || treat_leading_path(dir, path, len, simplify)) |
ccad261f | 2211 | read_directory_recursive(dir, path, len, untracked, 0, simplify); |
9fc42d60 | 2212 | free_simplify(simplify); |
453ec4bd | 2213 | qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); |
2abd31b0 | 2214 | qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name); |
c9ccb5d3 NTND |
2215 | if (dir->untracked) { |
2216 | static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS); | |
2217 | trace_printf_key(&trace_untracked_stats, | |
2218 | "node creation: %u\n" | |
2219 | "gitignore invalidation: %u\n" | |
2220 | "directory invalidation: %u\n" | |
2221 | "opendir: %u\n", | |
2222 | dir->untracked->dir_created, | |
2223 | dir->untracked->gitignore_invalidated, | |
2224 | dir->untracked->dir_invalidated, | |
2225 | dir->untracked->dir_opened); | |
1bbb3dba NTND |
2226 | if (dir->untracked == the_index.untracked && |
2227 | (dir->untracked->dir_opened || | |
2228 | dir->untracked->gitignore_invalidated || | |
2229 | dir->untracked->dir_invalidated)) | |
2230 | the_index.cache_changed |= UNTRACKED_CHANGED; | |
2231 | if (dir->untracked != the_index.untracked) { | |
2232 | free(dir->untracked); | |
2233 | dir->untracked = NULL; | |
2234 | } | |
c9ccb5d3 | 2235 | } |
453ec4bd LT |
2236 | return dir->nr; |
2237 | } | |
c91f0d92 | 2238 | |
686a4a06 | 2239 | int file_exists(const char *f) |
c91f0d92 | 2240 | { |
686a4a06 | 2241 | struct stat sb; |
a50f9fc5 | 2242 | return lstat(f, &sb) == 0; |
c91f0d92 | 2243 | } |
e6636747 | 2244 | |
63ec5e1f JS |
2245 | static int cmp_icase(char a, char b) |
2246 | { | |
2247 | if (a == b) | |
2248 | return 0; | |
2249 | if (ignore_case) | |
2250 | return toupper(a) - toupper(b); | |
2251 | return a - b; | |
2252 | } | |
2253 | ||
e6636747 | 2254 | /* |
9b125da4 NTND |
2255 | * Given two normalized paths (a trailing slash is ok), if subdir is |
2256 | * outside dir, return -1. Otherwise return the offset in subdir that | |
2257 | * can be used as relative path to dir. | |
e6636747 | 2258 | */ |
9b125da4 | 2259 | int dir_inside_of(const char *subdir, const char *dir) |
e6636747 | 2260 | { |
9b125da4 | 2261 | int offset = 0; |
e6636747 | 2262 | |
9b125da4 | 2263 | assert(dir && subdir && *dir && *subdir); |
e6636747 | 2264 | |
63ec5e1f | 2265 | while (*dir && *subdir && !cmp_icase(*dir, *subdir)) { |
e6636747 | 2266 | dir++; |
9b125da4 NTND |
2267 | subdir++; |
2268 | offset++; | |
490544b1 | 2269 | } |
9b125da4 NTND |
2270 | |
2271 | /* hel[p]/me vs hel[l]/yeah */ | |
2272 | if (*dir && *subdir) | |
2273 | return -1; | |
2274 | ||
2275 | if (!*subdir) | |
2276 | return !*dir ? offset : -1; /* same dir */ | |
2277 | ||
2278 | /* foo/[b]ar vs foo/[] */ | |
2279 | if (is_dir_sep(dir[-1])) | |
2280 | return is_dir_sep(subdir[-1]) ? offset : -1; | |
2281 | ||
2282 | /* foo[/]bar vs foo[] */ | |
2283 | return is_dir_sep(*subdir) ? offset + 1 : -1; | |
e6636747 JS |
2284 | } |
2285 | ||
2286 | int is_inside_dir(const char *dir) | |
2287 | { | |
56b9f6e7 RS |
2288 | char *cwd; |
2289 | int rc; | |
2290 | ||
b892913d NTND |
2291 | if (!dir) |
2292 | return 0; | |
56b9f6e7 RS |
2293 | |
2294 | cwd = xgetcwd(); | |
2295 | rc = (dir_inside_of(cwd, dir) >= 0); | |
2296 | free(cwd); | |
2297 | return rc; | |
e6636747 | 2298 | } |
7155b727 | 2299 | |
55892d23 AP |
2300 | int is_empty_dir(const char *path) |
2301 | { | |
2302 | DIR *dir = opendir(path); | |
2303 | struct dirent *e; | |
2304 | int ret = 1; | |
2305 | ||
2306 | if (!dir) | |
2307 | return 0; | |
2308 | ||
2309 | while ((e = readdir(dir)) != NULL) | |
2310 | if (!is_dot_or_dotdot(e->d_name)) { | |
2311 | ret = 0; | |
2312 | break; | |
2313 | } | |
2314 | ||
2315 | closedir(dir); | |
2316 | return ret; | |
2317 | } | |
2318 | ||
ae2f203e | 2319 | static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) |
7155b727 | 2320 | { |
a0f4afbe | 2321 | DIR *dir; |
7155b727 | 2322 | struct dirent *e; |
ae2f203e | 2323 | int ret = 0, original_len = path->len, len, kept_down = 0; |
a0f4afbe | 2324 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
c844a803 | 2325 | int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL); |
a0f4afbe | 2326 | unsigned char submodule_head[20]; |
7155b727 | 2327 | |
a0f4afbe | 2328 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
ae2f203e | 2329 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { |
a0f4afbe | 2330 | /* Do not descend and nuke a nested git work tree. */ |
ae2f203e JH |
2331 | if (kept_up) |
2332 | *kept_up = 1; | |
a0f4afbe | 2333 | return 0; |
ae2f203e | 2334 | } |
a0f4afbe | 2335 | |
ae2f203e | 2336 | flag &= ~REMOVE_DIR_KEEP_TOPLEVEL; |
a0f4afbe | 2337 | dir = opendir(path->buf); |
c844a803 | 2338 | if (!dir) { |
863808cd MH |
2339 | if (errno == ENOENT) |
2340 | return keep_toplevel ? -1 : 0; | |
2341 | else if (errno == EACCES && !keep_toplevel) | |
ecb2c282 MH |
2342 | /* |
2343 | * An empty dir could be removable even if it | |
2344 | * is unreadable: | |
2345 | */ | |
c844a803 JH |
2346 | return rmdir(path->buf); |
2347 | else | |
2348 | return -1; | |
2349 | } | |
00b6c178 | 2350 | strbuf_complete(path, '/'); |
7155b727 JS |
2351 | |
2352 | len = path->len; | |
2353 | while ((e = readdir(dir)) != NULL) { | |
2354 | struct stat st; | |
8ca12c0d AP |
2355 | if (is_dot_or_dotdot(e->d_name)) |
2356 | continue; | |
7155b727 JS |
2357 | |
2358 | strbuf_setlen(path, len); | |
2359 | strbuf_addstr(path, e->d_name); | |
863808cd MH |
2360 | if (lstat(path->buf, &st)) { |
2361 | if (errno == ENOENT) | |
2362 | /* | |
2363 | * file disappeared, which is what we | |
2364 | * wanted anyway | |
2365 | */ | |
2366 | continue; | |
2367 | /* fall thru */ | |
2368 | } else if (S_ISDIR(st.st_mode)) { | |
ae2f203e | 2369 | if (!remove_dir_recurse(path, flag, &kept_down)) |
7155b727 | 2370 | continue; /* happy */ |
863808cd MH |
2371 | } else if (!only_empty && |
2372 | (!unlink(path->buf) || errno == ENOENT)) { | |
7155b727 | 2373 | continue; /* happy, too */ |
863808cd | 2374 | } |
7155b727 JS |
2375 | |
2376 | /* path too long, stat fails, or non-directory still exists */ | |
2377 | ret = -1; | |
2378 | break; | |
2379 | } | |
2380 | closedir(dir); | |
2381 | ||
2382 | strbuf_setlen(path, original_len); | |
ae2f203e | 2383 | if (!ret && !keep_toplevel && !kept_down) |
863808cd | 2384 | ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; |
ae2f203e JH |
2385 | else if (kept_up) |
2386 | /* | |
2387 | * report the uplevel that it is not an error that we | |
2388 | * did not rmdir() our directory. | |
2389 | */ | |
2390 | *kept_up = !ret; | |
7155b727 JS |
2391 | return ret; |
2392 | } | |
039bc64e | 2393 | |
ae2f203e JH |
2394 | int remove_dir_recursively(struct strbuf *path, int flag) |
2395 | { | |
2396 | return remove_dir_recurse(path, flag, NULL); | |
2397 | } | |
2398 | ||
f932729c JK |
2399 | static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude") |
2400 | ||
039bc64e JH |
2401 | void setup_standard_excludes(struct dir_struct *dir) |
2402 | { | |
2403 | const char *path; | |
2404 | ||
2405 | dir->exclude_per_dir = ".gitignore"; | |
099d2d86 JH |
2406 | |
2407 | /* core.excludefile defaulting to $XDG_HOME/git/ignore */ | |
2845ce7f PT |
2408 | if (!excludes_file) |
2409 | excludes_file = xdg_config_home("ignore"); | |
4698c8fe | 2410 | if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) |
38ccaf93 JH |
2411 | add_excludes_from_file_1(dir, excludes_file, |
2412 | dir->untracked ? &dir->ss_excludes_file : NULL); | |
099d2d86 JH |
2413 | |
2414 | /* per repository user preference */ | |
f932729c | 2415 | path = git_path_info_exclude(); |
099d2d86 | 2416 | if (!access_or_warn(path, R_OK, 0)) |
0dcb8d7f NTND |
2417 | add_excludes_from_file_1(dir, path, |
2418 | dir->untracked ? &dir->ss_info_exclude : NULL); | |
039bc64e | 2419 | } |
4a92d1bf AR |
2420 | |
2421 | int remove_path(const char *name) | |
2422 | { | |
2423 | char *slash; | |
2424 | ||
9a6728d4 | 2425 | if (unlink(name) && errno != ENOENT && errno != ENOTDIR) |
4a92d1bf AR |
2426 | return -1; |
2427 | ||
2428 | slash = strrchr(name, '/'); | |
2429 | if (slash) { | |
2430 | char *dirs = xstrdup(name); | |
2431 | slash = dirs + (slash - name); | |
2432 | do { | |
2433 | *slash = '\0'; | |
3fc0d131 | 2434 | } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); |
4a92d1bf AR |
2435 | free(dirs); |
2436 | } | |
2437 | return 0; | |
2438 | } | |
2439 | ||
270be816 AS |
2440 | /* |
2441 | * Frees memory within dir which was allocated for exclude lists and | |
2442 | * the exclude_stack. Does not free dir itself. | |
2443 | */ | |
2444 | void clear_directory(struct dir_struct *dir) | |
2445 | { | |
2446 | int i, j; | |
2447 | struct exclude_list_group *group; | |
2448 | struct exclude_list *el; | |
2449 | struct exclude_stack *stk; | |
2450 | ||
2451 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { | |
2452 | group = &dir->exclude_list_group[i]; | |
2453 | for (j = 0; j < group->nr; j++) { | |
2454 | el = &group->el[j]; | |
2455 | if (i == EXC_DIRS) | |
2456 | free((char *)el->src); | |
2457 | clear_exclude_list(el); | |
2458 | } | |
2459 | free(group->el); | |
2460 | } | |
2461 | ||
2462 | stk = dir->exclude_stack; | |
2463 | while (stk) { | |
2464 | struct exclude_stack *prev = stk->prev; | |
2465 | free(stk); | |
2466 | stk = prev; | |
2467 | } | |
aceb9429 | 2468 | strbuf_release(&dir->basebuf); |
270be816 | 2469 | } |
83c094ad NTND |
2470 | |
2471 | struct ondisk_untracked_cache { | |
2472 | struct stat_data info_exclude_stat; | |
2473 | struct stat_data excludes_file_stat; | |
2474 | uint32_t dir_flags; | |
2475 | unsigned char info_exclude_sha1[20]; | |
2476 | unsigned char excludes_file_sha1[20]; | |
2477 | char exclude_per_dir[FLEX_ARRAY]; | |
2478 | }; | |
2479 | ||
2480 | #define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1) | |
2481 | ||
2482 | struct write_data { | |
2483 | int index; /* number of written untracked_cache_dir */ | |
2484 | struct ewah_bitmap *check_only; /* from untracked_cache_dir */ | |
2485 | struct ewah_bitmap *valid; /* from untracked_cache_dir */ | |
2486 | struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */ | |
2487 | struct strbuf out; | |
2488 | struct strbuf sb_stat; | |
2489 | struct strbuf sb_sha1; | |
2490 | }; | |
2491 | ||
2492 | static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from) | |
2493 | { | |
2494 | to->sd_ctime.sec = htonl(from->sd_ctime.sec); | |
2495 | to->sd_ctime.nsec = htonl(from->sd_ctime.nsec); | |
2496 | to->sd_mtime.sec = htonl(from->sd_mtime.sec); | |
2497 | to->sd_mtime.nsec = htonl(from->sd_mtime.nsec); | |
2498 | to->sd_dev = htonl(from->sd_dev); | |
2499 | to->sd_ino = htonl(from->sd_ino); | |
2500 | to->sd_uid = htonl(from->sd_uid); | |
2501 | to->sd_gid = htonl(from->sd_gid); | |
2502 | to->sd_size = htonl(from->sd_size); | |
2503 | } | |
2504 | ||
2505 | static void write_one_dir(struct untracked_cache_dir *untracked, | |
2506 | struct write_data *wd) | |
2507 | { | |
2508 | struct stat_data stat_data; | |
2509 | struct strbuf *out = &wd->out; | |
2510 | unsigned char intbuf[16]; | |
2511 | unsigned int intlen, value; | |
2512 | int i = wd->index++; | |
2513 | ||
2514 | /* | |
2515 | * untracked_nr should be reset whenever valid is clear, but | |
2516 | * for safety.. | |
2517 | */ | |
2518 | if (!untracked->valid) { | |
2519 | untracked->untracked_nr = 0; | |
2520 | untracked->check_only = 0; | |
2521 | } | |
2522 | ||
2523 | if (untracked->check_only) | |
2524 | ewah_set(wd->check_only, i); | |
2525 | if (untracked->valid) { | |
2526 | ewah_set(wd->valid, i); | |
2527 | stat_data_to_disk(&stat_data, &untracked->stat_data); | |
2528 | strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data)); | |
2529 | } | |
2530 | if (!is_null_sha1(untracked->exclude_sha1)) { | |
2531 | ewah_set(wd->sha1_valid, i); | |
2532 | strbuf_add(&wd->sb_sha1, untracked->exclude_sha1, 20); | |
2533 | } | |
2534 | ||
2535 | intlen = encode_varint(untracked->untracked_nr, intbuf); | |
2536 | strbuf_add(out, intbuf, intlen); | |
2537 | ||
2538 | /* skip non-recurse directories */ | |
2539 | for (i = 0, value = 0; i < untracked->dirs_nr; i++) | |
2540 | if (untracked->dirs[i]->recurse) | |
2541 | value++; | |
2542 | intlen = encode_varint(value, intbuf); | |
2543 | strbuf_add(out, intbuf, intlen); | |
2544 | ||
2545 | strbuf_add(out, untracked->name, strlen(untracked->name) + 1); | |
2546 | ||
2547 | for (i = 0; i < untracked->untracked_nr; i++) | |
2548 | strbuf_add(out, untracked->untracked[i], | |
2549 | strlen(untracked->untracked[i]) + 1); | |
2550 | ||
2551 | for (i = 0; i < untracked->dirs_nr; i++) | |
2552 | if (untracked->dirs[i]->recurse) | |
2553 | write_one_dir(untracked->dirs[i], wd); | |
2554 | } | |
2555 | ||
2556 | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked) | |
2557 | { | |
2558 | struct ondisk_untracked_cache *ouc; | |
2559 | struct write_data wd; | |
2560 | unsigned char varbuf[16]; | |
e0b83735 JK |
2561 | int varint_len; |
2562 | size_t len = strlen(untracked->exclude_per_dir); | |
2563 | ||
2564 | FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len); | |
83c094ad NTND |
2565 | stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat); |
2566 | stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat); | |
2567 | hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1); | |
2568 | hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1); | |
2569 | ouc->dir_flags = htonl(untracked->dir_flags); | |
1e8fef60 NTND |
2570 | |
2571 | varint_len = encode_varint(untracked->ident.len, varbuf); | |
2572 | strbuf_add(out, varbuf, varint_len); | |
2573 | strbuf_add(out, untracked->ident.buf, untracked->ident.len); | |
2574 | ||
83c094ad NTND |
2575 | strbuf_add(out, ouc, ouc_size(len)); |
2576 | free(ouc); | |
2577 | ouc = NULL; | |
2578 | ||
2579 | if (!untracked->root) { | |
2580 | varint_len = encode_varint(0, varbuf); | |
2581 | strbuf_add(out, varbuf, varint_len); | |
2582 | return; | |
2583 | } | |
2584 | ||
2585 | wd.index = 0; | |
2586 | wd.check_only = ewah_new(); | |
2587 | wd.valid = ewah_new(); | |
2588 | wd.sha1_valid = ewah_new(); | |
2589 | strbuf_init(&wd.out, 1024); | |
2590 | strbuf_init(&wd.sb_stat, 1024); | |
2591 | strbuf_init(&wd.sb_sha1, 1024); | |
2592 | write_one_dir(untracked->root, &wd); | |
2593 | ||
2594 | varint_len = encode_varint(wd.index, varbuf); | |
2595 | strbuf_add(out, varbuf, varint_len); | |
2596 | strbuf_addbuf(out, &wd.out); | |
2597 | ewah_serialize_strbuf(wd.valid, out); | |
2598 | ewah_serialize_strbuf(wd.check_only, out); | |
2599 | ewah_serialize_strbuf(wd.sha1_valid, out); | |
2600 | strbuf_addbuf(out, &wd.sb_stat); | |
2601 | strbuf_addbuf(out, &wd.sb_sha1); | |
2602 | strbuf_addch(out, '\0'); /* safe guard for string lists */ | |
2603 | ||
2604 | ewah_free(wd.valid); | |
2605 | ewah_free(wd.check_only); | |
2606 | ewah_free(wd.sha1_valid); | |
2607 | strbuf_release(&wd.out); | |
2608 | strbuf_release(&wd.sb_stat); | |
2609 | strbuf_release(&wd.sb_sha1); | |
2610 | } | |
f9e6c649 NTND |
2611 | |
2612 | static void free_untracked(struct untracked_cache_dir *ucd) | |
2613 | { | |
2614 | int i; | |
2615 | if (!ucd) | |
2616 | return; | |
2617 | for (i = 0; i < ucd->dirs_nr; i++) | |
2618 | free_untracked(ucd->dirs[i]); | |
2619 | for (i = 0; i < ucd->untracked_nr; i++) | |
2620 | free(ucd->untracked[i]); | |
2621 | free(ucd->untracked); | |
2622 | free(ucd->dirs); | |
2623 | free(ucd); | |
2624 | } | |
2625 | ||
2626 | void free_untracked_cache(struct untracked_cache *uc) | |
2627 | { | |
2628 | if (uc) | |
2629 | free_untracked(uc->root); | |
2630 | free(uc); | |
2631 | } | |
2632 | ||
2633 | struct read_data { | |
2634 | int index; | |
2635 | struct untracked_cache_dir **ucd; | |
2636 | struct ewah_bitmap *check_only; | |
2637 | struct ewah_bitmap *valid; | |
2638 | struct ewah_bitmap *sha1_valid; | |
2639 | const unsigned char *data; | |
2640 | const unsigned char *end; | |
2641 | }; | |
2642 | ||
2643 | static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from) | |
2644 | { | |
2645 | to->sd_ctime.sec = get_be32(&from->sd_ctime.sec); | |
2646 | to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec); | |
2647 | to->sd_mtime.sec = get_be32(&from->sd_mtime.sec); | |
2648 | to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec); | |
2649 | to->sd_dev = get_be32(&from->sd_dev); | |
2650 | to->sd_ino = get_be32(&from->sd_ino); | |
2651 | to->sd_uid = get_be32(&from->sd_uid); | |
2652 | to->sd_gid = get_be32(&from->sd_gid); | |
2653 | to->sd_size = get_be32(&from->sd_size); | |
2654 | } | |
2655 | ||
2656 | static int read_one_dir(struct untracked_cache_dir **untracked_, | |
2657 | struct read_data *rd) | |
2658 | { | |
2659 | struct untracked_cache_dir ud, *untracked; | |
2660 | const unsigned char *next, *data = rd->data, *end = rd->end; | |
2661 | unsigned int value; | |
2662 | int i, len; | |
2663 | ||
2664 | memset(&ud, 0, sizeof(ud)); | |
2665 | ||
2666 | next = data; | |
2667 | value = decode_varint(&next); | |
2668 | if (next > end) | |
2669 | return -1; | |
2670 | ud.recurse = 1; | |
2671 | ud.untracked_alloc = value; | |
2672 | ud.untracked_nr = value; | |
2673 | if (ud.untracked_nr) | |
b32fa95f | 2674 | ALLOC_ARRAY(ud.untracked, ud.untracked_nr); |
f9e6c649 NTND |
2675 | data = next; |
2676 | ||
2677 | next = data; | |
2678 | ud.dirs_alloc = ud.dirs_nr = decode_varint(&next); | |
2679 | if (next > end) | |
2680 | return -1; | |
b32fa95f | 2681 | ALLOC_ARRAY(ud.dirs, ud.dirs_nr); |
f9e6c649 NTND |
2682 | data = next; |
2683 | ||
2684 | len = strlen((const char *)data); | |
2685 | next = data + len + 1; | |
2686 | if (next > rd->end) | |
2687 | return -1; | |
50a6c8ef | 2688 | *untracked_ = untracked = xmalloc(st_add(sizeof(*untracked), len)); |
f9e6c649 NTND |
2689 | memcpy(untracked, &ud, sizeof(ud)); |
2690 | memcpy(untracked->name, data, len + 1); | |
2691 | data = next; | |
2692 | ||
2693 | for (i = 0; i < untracked->untracked_nr; i++) { | |
2694 | len = strlen((const char *)data); | |
2695 | next = data + len + 1; | |
2696 | if (next > rd->end) | |
2697 | return -1; | |
2698 | untracked->untracked[i] = xstrdup((const char*)data); | |
2699 | data = next; | |
2700 | } | |
2701 | ||
2702 | rd->ucd[rd->index++] = untracked; | |
2703 | rd->data = data; | |
2704 | ||
2705 | for (i = 0; i < untracked->dirs_nr; i++) { | |
2706 | len = read_one_dir(untracked->dirs + i, rd); | |
2707 | if (len < 0) | |
2708 | return -1; | |
2709 | } | |
2710 | return 0; | |
2711 | } | |
2712 | ||
2713 | static void set_check_only(size_t pos, void *cb) | |
2714 | { | |
2715 | struct read_data *rd = cb; | |
2716 | struct untracked_cache_dir *ud = rd->ucd[pos]; | |
2717 | ud->check_only = 1; | |
2718 | } | |
2719 | ||
2720 | static void read_stat(size_t pos, void *cb) | |
2721 | { | |
2722 | struct read_data *rd = cb; | |
2723 | struct untracked_cache_dir *ud = rd->ucd[pos]; | |
2724 | if (rd->data + sizeof(struct stat_data) > rd->end) { | |
2725 | rd->data = rd->end + 1; | |
2726 | return; | |
2727 | } | |
2728 | stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data); | |
2729 | rd->data += sizeof(struct stat_data); | |
2730 | ud->valid = 1; | |
2731 | } | |
2732 | ||
2733 | static void read_sha1(size_t pos, void *cb) | |
2734 | { | |
2735 | struct read_data *rd = cb; | |
2736 | struct untracked_cache_dir *ud = rd->ucd[pos]; | |
2737 | if (rd->data + 20 > rd->end) { | |
2738 | rd->data = rd->end + 1; | |
2739 | return; | |
2740 | } | |
2741 | hashcpy(ud->exclude_sha1, rd->data); | |
2742 | rd->data += 20; | |
2743 | } | |
2744 | ||
2745 | static void load_sha1_stat(struct sha1_stat *sha1_stat, | |
2746 | const struct stat_data *stat, | |
2747 | const unsigned char *sha1) | |
2748 | { | |
2749 | stat_data_from_disk(&sha1_stat->stat, stat); | |
2750 | hashcpy(sha1_stat->sha1, sha1); | |
2751 | sha1_stat->valid = 1; | |
2752 | } | |
2753 | ||
2754 | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz) | |
2755 | { | |
2756 | const struct ondisk_untracked_cache *ouc; | |
2757 | struct untracked_cache *uc; | |
2758 | struct read_data rd; | |
2759 | const unsigned char *next = data, *end = (const unsigned char *)data + sz; | |
1e8fef60 NTND |
2760 | const char *ident; |
2761 | int ident_len, len; | |
f9e6c649 NTND |
2762 | |
2763 | if (sz <= 1 || end[-1] != '\0') | |
2764 | return NULL; | |
2765 | end--; | |
2766 | ||
1e8fef60 NTND |
2767 | ident_len = decode_varint(&next); |
2768 | if (next + ident_len > end) | |
2769 | return NULL; | |
2770 | ident = (const char *)next; | |
2771 | next += ident_len; | |
2772 | ||
f9e6c649 NTND |
2773 | ouc = (const struct ondisk_untracked_cache *)next; |
2774 | if (next + ouc_size(0) > end) | |
2775 | return NULL; | |
2776 | ||
2777 | uc = xcalloc(1, sizeof(*uc)); | |
1e8fef60 NTND |
2778 | strbuf_init(&uc->ident, ident_len); |
2779 | strbuf_add(&uc->ident, ident, ident_len); | |
f9e6c649 NTND |
2780 | load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat, |
2781 | ouc->info_exclude_sha1); | |
2782 | load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat, | |
2783 | ouc->excludes_file_sha1); | |
2784 | uc->dir_flags = get_be32(&ouc->dir_flags); | |
2785 | uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir); | |
2786 | /* NUL after exclude_per_dir is covered by sizeof(*ouc) */ | |
2787 | next += ouc_size(strlen(ouc->exclude_per_dir)); | |
2788 | if (next >= end) | |
2789 | goto done2; | |
2790 | ||
2791 | len = decode_varint(&next); | |
2792 | if (next > end || len == 0) | |
2793 | goto done2; | |
2794 | ||
2795 | rd.valid = ewah_new(); | |
2796 | rd.check_only = ewah_new(); | |
2797 | rd.sha1_valid = ewah_new(); | |
2798 | rd.data = next; | |
2799 | rd.end = end; | |
2800 | rd.index = 0; | |
b32fa95f | 2801 | ALLOC_ARRAY(rd.ucd, len); |
f9e6c649 NTND |
2802 | |
2803 | if (read_one_dir(&uc->root, &rd) || rd.index != len) | |
2804 | goto done; | |
2805 | ||
2806 | next = rd.data; | |
2807 | len = ewah_read_mmap(rd.valid, next, end - next); | |
2808 | if (len < 0) | |
2809 | goto done; | |
2810 | ||
2811 | next += len; | |
2812 | len = ewah_read_mmap(rd.check_only, next, end - next); | |
2813 | if (len < 0) | |
2814 | goto done; | |
2815 | ||
2816 | next += len; | |
2817 | len = ewah_read_mmap(rd.sha1_valid, next, end - next); | |
2818 | if (len < 0) | |
2819 | goto done; | |
2820 | ||
2821 | ewah_each_bit(rd.check_only, set_check_only, &rd); | |
2822 | rd.data = next + len; | |
2823 | ewah_each_bit(rd.valid, read_stat, &rd); | |
2824 | ewah_each_bit(rd.sha1_valid, read_sha1, &rd); | |
2825 | next = rd.data; | |
2826 | ||
2827 | done: | |
2828 | free(rd.ucd); | |
2829 | ewah_free(rd.valid); | |
2830 | ewah_free(rd.check_only); | |
2831 | ewah_free(rd.sha1_valid); | |
2832 | done2: | |
2833 | if (next != end) { | |
2834 | free_untracked_cache(uc); | |
2835 | uc = NULL; | |
2836 | } | |
2837 | return uc; | |
2838 | } | |
e931371a | 2839 | |
73f9145f NTND |
2840 | static void invalidate_one_directory(struct untracked_cache *uc, |
2841 | struct untracked_cache_dir *ucd) | |
2842 | { | |
2843 | uc->dir_invalidated++; | |
2844 | ucd->valid = 0; | |
2845 | ucd->untracked_nr = 0; | |
2846 | } | |
2847 | ||
2848 | /* | |
2849 | * Normally when an entry is added or removed from a directory, | |
2850 | * invalidating that directory is enough. No need to touch its | |
2851 | * ancestors. When a directory is shown as "foo/bar/" in git-status | |
2852 | * however, deleting or adding an entry may have cascading effect. | |
2853 | * | |
2854 | * Say the "foo/bar/file" has become untracked, we need to tell the | |
2855 | * untracked_cache_dir of "foo" that "bar/" is not an untracked | |
2856 | * directory any more (because "bar" is managed by foo as an untracked | |
2857 | * "file"). | |
2858 | * | |
2859 | * Similarly, if "foo/bar/file" moves from untracked to tracked and it | |
2860 | * was the last untracked entry in the entire "foo", we should show | |
2861 | * "foo/" instead. Which means we have to invalidate past "bar" up to | |
2862 | * "foo". | |
2863 | * | |
2864 | * This function traverses all directories from root to leaf. If there | |
2865 | * is a chance of one of the above cases happening, we invalidate back | |
2866 | * to root. Otherwise we just invalidate the leaf. There may be a more | |
2867 | * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to | |
2868 | * detect these cases and avoid unnecessary invalidation, for example, | |
2869 | * checking for the untracked entry named "bar/" in "foo", but for now | |
2870 | * stick to something safe and simple. | |
2871 | */ | |
2872 | static int invalidate_one_component(struct untracked_cache *uc, | |
2873 | struct untracked_cache_dir *dir, | |
2874 | const char *path, int len) | |
2875 | { | |
2876 | const char *rest = strchr(path, '/'); | |
2877 | ||
2878 | if (rest) { | |
2879 | int component_len = rest - path; | |
2880 | struct untracked_cache_dir *d = | |
2881 | lookup_untracked(uc, dir, path, component_len); | |
2882 | int ret = | |
2883 | invalidate_one_component(uc, d, rest + 1, | |
2884 | len - (component_len + 1)); | |
2885 | if (ret) | |
2886 | invalidate_one_directory(uc, dir); | |
2887 | return ret; | |
2888 | } | |
2889 | ||
2890 | invalidate_one_directory(uc, dir); | |
2891 | return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES; | |
2892 | } | |
2893 | ||
e931371a NTND |
2894 | void untracked_cache_invalidate_path(struct index_state *istate, |
2895 | const char *path) | |
2896 | { | |
e931371a NTND |
2897 | if (!istate->untracked || !istate->untracked->root) |
2898 | return; | |
73f9145f NTND |
2899 | invalidate_one_component(istate->untracked, istate->untracked->root, |
2900 | path, strlen(path)); | |
e931371a NTND |
2901 | } |
2902 | ||
2903 | void untracked_cache_remove_from_index(struct index_state *istate, | |
2904 | const char *path) | |
2905 | { | |
2906 | untracked_cache_invalidate_path(istate, path); | |
2907 | } | |
2908 | ||
2909 | void untracked_cache_add_to_index(struct index_state *istate, | |
2910 | const char *path) | |
2911 | { | |
2912 | untracked_cache_invalidate_path(istate, path); | |
2913 | } |