]>
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" |
453ec4bd | 15 | |
9fc42d60 LT |
16 | struct path_simplify { |
17 | int len; | |
18 | const char *path; | |
19 | }; | |
20 | ||
defd7c7b KB |
21 | /* |
22 | * Tells read_directory_recursive how a file or directory should be treated. | |
23 | * Values are ordered by significance, e.g. if a directory contains both | |
24 | * excluded and untracked files, it is listed as untracked because | |
25 | * path_untracked > path_excluded. | |
26 | */ | |
27 | enum path_treatment { | |
28 | path_none = 0, | |
29 | path_recurse, | |
30 | path_excluded, | |
31 | path_untracked | |
32 | }; | |
33 | ||
cf7c6148 NTND |
34 | /* |
35 | * Support data structure for our opendir/readdir/closedir wrappers | |
36 | */ | |
37 | struct cached_dir { | |
38 | DIR *fdir; | |
39 | struct untracked_cache_dir *untracked; | |
91a2288b NTND |
40 | int nr_files; |
41 | int nr_dirs; | |
42 | ||
cf7c6148 | 43 | struct dirent *de; |
91a2288b NTND |
44 | const char *file; |
45 | struct untracked_cache_dir *ucd; | |
cf7c6148 NTND |
46 | }; |
47 | ||
defd7c7b | 48 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
0dcb8d7f | 49 | const char *path, int len, struct untracked_cache_dir *untracked, |
09595258 | 50 | int check_only, const struct path_simplify *simplify); |
caa6b782 | 51 | static int get_dtype(struct dirent *de, const char *path, int len); |
09595258 | 52 | |
8cf2a84e JJ |
53 | /* helper string functions with support for the ignore_case flag */ |
54 | int strcmp_icase(const char *a, const char *b) | |
55 | { | |
56 | return ignore_case ? strcasecmp(a, b) : strcmp(a, b); | |
57 | } | |
58 | ||
59 | int strncmp_icase(const char *a, const char *b, size_t count) | |
60 | { | |
61 | return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); | |
62 | } | |
63 | ||
64 | int fnmatch_icase(const char *pattern, const char *string, int flags) | |
65 | { | |
eb07894f NTND |
66 | return wildmatch(pattern, string, |
67 | flags | (ignore_case ? WM_CASEFOLD : 0), | |
68 | NULL); | |
8cf2a84e JJ |
69 | } |
70 | ||
1f26ce61 CB |
71 | int git_fnmatch(const struct pathspec_item *item, |
72 | const char *pattern, const char *string, | |
73 | int prefix) | |
5d74762d | 74 | { |
5d74762d | 75 | if (prefix > 0) { |
93d93537 | 76 | if (ps_strncmp(item, pattern, string, prefix)) |
eb07894f | 77 | return WM_NOMATCH; |
5d74762d NTND |
78 | pattern += prefix; |
79 | string += prefix; | |
80 | } | |
bd30c2e4 | 81 | if (item->flags & PATHSPEC_ONESTAR) { |
8c6abbcd NTND |
82 | int pattern_len = strlen(++pattern); |
83 | int string_len = strlen(string); | |
84 | return string_len < pattern_len || | |
93d93537 NTND |
85 | ps_strcmp(item, pattern, |
86 | string + string_len - pattern_len); | |
8c6abbcd | 87 | } |
bd30c2e4 | 88 | if (item->magic & PATHSPEC_GLOB) |
93d93537 NTND |
89 | return wildmatch(pattern, string, |
90 | WM_PATHNAME | | |
91 | (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0), | |
92 | NULL); | |
bd30c2e4 NTND |
93 | else |
94 | /* wildmatch has not learned no FNM_PATHNAME mode yet */ | |
eb07894f NTND |
95 | return wildmatch(pattern, string, |
96 | item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0, | |
97 | NULL); | |
5d74762d NTND |
98 | } |
99 | ||
0b6e56df JH |
100 | static int fnmatch_icase_mem(const char *pattern, int patternlen, |
101 | const char *string, int stringlen, | |
102 | int flags) | |
103 | { | |
104 | int match_status; | |
105 | struct strbuf pat_buf = STRBUF_INIT; | |
106 | struct strbuf str_buf = STRBUF_INIT; | |
107 | const char *use_pat = pattern; | |
108 | const char *use_str = string; | |
109 | ||
110 | if (pattern[patternlen]) { | |
111 | strbuf_add(&pat_buf, pattern, patternlen); | |
112 | use_pat = pat_buf.buf; | |
113 | } | |
114 | if (string[stringlen]) { | |
115 | strbuf_add(&str_buf, string, stringlen); | |
116 | use_str = str_buf.buf; | |
117 | } | |
118 | ||
f30366b2 JH |
119 | if (ignore_case) |
120 | flags |= WM_CASEFOLD; | |
121 | match_status = wildmatch(use_pat, use_str, flags, NULL); | |
0b6e56df JH |
122 | |
123 | strbuf_release(&pat_buf); | |
124 | strbuf_release(&str_buf); | |
125 | ||
126 | return match_status; | |
127 | } | |
128 | ||
827f4d6c | 129 | static size_t common_prefix_len(const struct pathspec *pathspec) |
3c6a370b | 130 | { |
827f4d6c | 131 | int n; |
4a085b16 | 132 | size_t max = 0; |
3c6a370b | 133 | |
93d93537 NTND |
134 | /* |
135 | * ":(icase)path" is treated as a pathspec full of | |
136 | * wildcard. In other words, only prefix is considered common | |
137 | * prefix. If the pathspec is abc/foo abc/bar, running in | |
138 | * subdir xyz, the common prefix is still xyz, not xuz/abc as | |
139 | * in non-:(icase). | |
140 | */ | |
5c6933d2 NTND |
141 | GUARD_PATHSPEC(pathspec, |
142 | PATHSPEC_FROMTOP | | |
143 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 144 | PATHSPEC_LITERAL | |
93d93537 | 145 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
146 | PATHSPEC_ICASE | |
147 | PATHSPEC_EXCLUDE); | |
4a085b16 | 148 | |
827f4d6c | 149 | for (n = 0; n < pathspec->nr; n++) { |
93d93537 | 150 | size_t i = 0, len = 0, item_len; |
ef79b1f8 NTND |
151 | if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) |
152 | continue; | |
93d93537 NTND |
153 | if (pathspec->items[n].magic & PATHSPEC_ICASE) |
154 | item_len = pathspec->items[n].prefix; | |
155 | else | |
156 | item_len = pathspec->items[n].nowildcard_len; | |
157 | while (i < item_len && (n == 0 || i < max)) { | |
827f4d6c NTND |
158 | char c = pathspec->items[n].match[i]; |
159 | if (c != pathspec->items[0].match[i]) | |
4a085b16 JH |
160 | break; |
161 | if (c == '/') | |
162 | len = i + 1; | |
827f4d6c | 163 | i++; |
4a085b16 | 164 | } |
827f4d6c | 165 | if (n == 0 || len < max) { |
4a085b16 JH |
166 | max = len; |
167 | if (!max) | |
168 | break; | |
169 | } | |
3c6a370b | 170 | } |
4a085b16 | 171 | return max; |
3c6a370b LT |
172 | } |
173 | ||
f950eb95 CB |
174 | /* |
175 | * Returns a copy of the longest leading path common among all | |
176 | * pathspecs. | |
177 | */ | |
827f4d6c | 178 | char *common_prefix(const struct pathspec *pathspec) |
f950eb95 CB |
179 | { |
180 | unsigned long len = common_prefix_len(pathspec); | |
181 | ||
827f4d6c | 182 | return len ? xmemdupz(pathspec->items[0].match, len) : NULL; |
f950eb95 CB |
183 | } |
184 | ||
7327d3d1 | 185 | int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) |
1d8842d9 | 186 | { |
4a085b16 | 187 | size_t len; |
1d8842d9 LT |
188 | |
189 | /* | |
190 | * Calculate common prefix for the pathspec, and | |
191 | * use that to optimize the directory walk | |
192 | */ | |
4a085b16 | 193 | len = common_prefix_len(pathspec); |
1d8842d9 LT |
194 | |
195 | /* Read the directory and prune it */ | |
b3920bbd | 196 | read_directory(dir, pathspec->nr ? pathspec->_raw[0] : "", len, pathspec); |
dba2e203 | 197 | return len; |
1d8842d9 LT |
198 | } |
199 | ||
bc96cc87 NTND |
200 | int within_depth(const char *name, int namelen, |
201 | int depth, int max_depth) | |
202 | { | |
203 | const char *cp = name, *cpe = name + namelen; | |
204 | ||
205 | while (cp < cpe) { | |
206 | if (*cp++ != '/') | |
207 | continue; | |
208 | depth++; | |
209 | if (depth > max_depth) | |
210 | return 0; | |
211 | } | |
212 | return 1; | |
213 | } | |
214 | ||
42b0874a | 215 | #define DO_MATCH_EXCLUDE 1 |
68690fdd | 216 | #define DO_MATCH_DIRECTORY 2 |
42b0874a | 217 | |
61cf2820 NTND |
218 | /* |
219 | * Does 'match' match the given name? | |
220 | * A match is found if | |
221 | * | |
222 | * (1) the 'match' string is leading directory of 'name', or | |
223 | * (2) the 'match' string is a wildcard and matches 'name', or | |
224 | * (3) the 'match' string is exactly the same as 'name'. | |
225 | * | |
226 | * and the return value tells which case it was. | |
227 | * | |
228 | * It returns 0 when there is no match. | |
229 | */ | |
230 | static int match_pathspec_item(const struct pathspec_item *item, int prefix, | |
42b0874a | 231 | const char *name, int namelen, unsigned flags) |
61cf2820 NTND |
232 | { |
233 | /* name/namelen has prefix cut off by caller */ | |
234 | const char *match = item->match + prefix; | |
235 | int matchlen = item->len - prefix; | |
236 | ||
93d93537 NTND |
237 | /* |
238 | * The normal call pattern is: | |
239 | * 1. prefix = common_prefix_len(ps); | |
240 | * 2. prune something, or fill_directory | |
854b0959 | 241 | * 3. match_pathspec() |
93d93537 NTND |
242 | * |
243 | * 'prefix' at #1 may be shorter than the command's prefix and | |
244 | * it's ok for #2 to match extra files. Those extras will be | |
245 | * trimmed at #3. | |
246 | * | |
247 | * Suppose the pathspec is 'foo' and '../bar' running from | |
248 | * subdir 'xyz'. The common prefix at #1 will be empty, thanks | |
249 | * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The | |
250 | * user does not want XYZ/foo, only the "foo" part should be | |
251 | * case-insensitive. We need to filter out XYZ/foo here. In | |
252 | * other words, we do not trust the caller on comparing the | |
253 | * prefix part when :(icase) is involved. We do exact | |
254 | * comparison ourselves. | |
255 | * | |
256 | * Normally the caller (common_prefix_len() in fact) does | |
257 | * _exact_ matching on name[-prefix+1..-1] and we do not need | |
258 | * to check that part. Be defensive and check it anyway, in | |
259 | * case common_prefix_len is changed, or a new caller is | |
260 | * introduced that does not use common_prefix_len. | |
261 | * | |
262 | * If the penalty turns out too high when prefix is really | |
263 | * long, maybe change it to | |
264 | * strncmp(match, name, item->prefix - prefix) | |
265 | */ | |
266 | if (item->prefix && (item->magic & PATHSPEC_ICASE) && | |
267 | strncmp(item->match, name - prefix, item->prefix)) | |
268 | return 0; | |
269 | ||
61cf2820 NTND |
270 | /* If the match was just the prefix, we matched */ |
271 | if (!*match) | |
272 | return MATCHED_RECURSIVELY; | |
273 | ||
93d93537 | 274 | if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { |
61cf2820 NTND |
275 | if (matchlen == namelen) |
276 | return MATCHED_EXACTLY; | |
277 | ||
278 | if (match[matchlen-1] == '/' || name[matchlen] == '/') | |
279 | return MATCHED_RECURSIVELY; | |
68690fdd NTND |
280 | } else if ((flags & DO_MATCH_DIRECTORY) && |
281 | match[matchlen - 1] == '/' && | |
282 | namelen == matchlen - 1 && | |
283 | !ps_strncmp(item, match, name, namelen)) | |
284 | return MATCHED_EXACTLY; | |
61cf2820 | 285 | |
5d74762d | 286 | if (item->nowildcard_len < item->len && |
bd30c2e4 | 287 | !git_fnmatch(item, match, name, |
8c6abbcd | 288 | item->nowildcard_len - prefix)) |
61cf2820 NTND |
289 | return MATCHED_FNMATCH; |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | /* | |
52ed1894 AS |
295 | * Given a name and a list of pathspecs, returns the nature of the |
296 | * closest (i.e. most specific) match of the name to any of the | |
297 | * pathspecs. | |
298 | * | |
299 | * The caller typically calls this multiple times with the same | |
300 | * pathspec and seen[] array but with different name/namelen | |
301 | * (e.g. entries from the index) and is interested in seeing if and | |
302 | * how each pathspec matches all the names it calls this function | |
303 | * with. A mark is left in the seen[] array for each pathspec element | |
304 | * indicating the closest type of match that element achieved, so if | |
305 | * seen[n] remains zero after multiple invocations, that means the nth | |
306 | * pathspec did not match any names, which could indicate that the | |
307 | * user mistyped the nth pathspec. | |
61cf2820 | 308 | */ |
854b0959 NTND |
309 | static int do_match_pathspec(const struct pathspec *ps, |
310 | const char *name, int namelen, | |
311 | int prefix, char *seen, | |
42b0874a | 312 | unsigned flags) |
61cf2820 | 313 | { |
42b0874a | 314 | int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; |
61cf2820 | 315 | |
5c6933d2 NTND |
316 | GUARD_PATHSPEC(ps, |
317 | PATHSPEC_FROMTOP | | |
318 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 319 | PATHSPEC_LITERAL | |
93d93537 | 320 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
321 | PATHSPEC_ICASE | |
322 | PATHSPEC_EXCLUDE); | |
8f4f8f45 | 323 | |
61cf2820 | 324 | if (!ps->nr) { |
6330a171 NTND |
325 | if (!ps->recursive || |
326 | !(ps->magic & PATHSPEC_MAXDEPTH) || | |
327 | ps->max_depth == -1) | |
61cf2820 NTND |
328 | return MATCHED_RECURSIVELY; |
329 | ||
330 | if (within_depth(name, namelen, 0, ps->max_depth)) | |
331 | return MATCHED_EXACTLY; | |
332 | else | |
333 | return 0; | |
334 | } | |
335 | ||
336 | name += prefix; | |
337 | namelen -= prefix; | |
338 | ||
339 | for (i = ps->nr - 1; i >= 0; i--) { | |
340 | int how; | |
ef79b1f8 NTND |
341 | |
342 | if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || | |
343 | ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) | |
344 | continue; | |
345 | ||
61cf2820 NTND |
346 | if (seen && seen[i] == MATCHED_EXACTLY) |
347 | continue; | |
ef79b1f8 NTND |
348 | /* |
349 | * Make exclude patterns optional and never report | |
350 | * "pathspec ':(exclude)foo' matches no files" | |
351 | */ | |
352 | if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) | |
353 | seen[i] = MATCHED_FNMATCH; | |
42b0874a NTND |
354 | how = match_pathspec_item(ps->items+i, prefix, name, |
355 | namelen, flags); | |
6330a171 NTND |
356 | if (ps->recursive && |
357 | (ps->magic & PATHSPEC_MAXDEPTH) && | |
358 | ps->max_depth != -1 && | |
61cf2820 NTND |
359 | how && how != MATCHED_FNMATCH) { |
360 | int len = ps->items[i].len; | |
361 | if (name[len] == '/') | |
362 | len++; | |
363 | if (within_depth(name+len, namelen-len, 0, ps->max_depth)) | |
364 | how = MATCHED_EXACTLY; | |
365 | else | |
366 | how = 0; | |
367 | } | |
368 | if (how) { | |
369 | if (retval < how) | |
370 | retval = how; | |
371 | if (seen && seen[i] < how) | |
372 | seen[i] = how; | |
373 | } | |
374 | } | |
375 | return retval; | |
376 | } | |
377 | ||
854b0959 NTND |
378 | int match_pathspec(const struct pathspec *ps, |
379 | const char *name, int namelen, | |
ae8d0824 | 380 | int prefix, char *seen, int is_dir) |
ef79b1f8 NTND |
381 | { |
382 | int positive, negative; | |
ae8d0824 | 383 | unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; |
42b0874a NTND |
384 | positive = do_match_pathspec(ps, name, namelen, |
385 | prefix, seen, flags); | |
ef79b1f8 NTND |
386 | if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) |
387 | return positive; | |
42b0874a NTND |
388 | negative = do_match_pathspec(ps, name, namelen, |
389 | prefix, seen, | |
390 | flags | DO_MATCH_EXCLUDE); | |
ef79b1f8 NTND |
391 | return negative ? 0 : positive; |
392 | } | |
393 | ||
fcd631ed NTND |
394 | /* |
395 | * Return the length of the "simple" part of a path match limiter. | |
396 | */ | |
87323bda | 397 | int simple_length(const char *match) |
fcd631ed NTND |
398 | { |
399 | int len = -1; | |
400 | ||
401 | for (;;) { | |
402 | unsigned char c = *match++; | |
403 | len++; | |
404 | if (c == '\0' || is_glob_special(c)) | |
405 | return len; | |
406 | } | |
407 | } | |
408 | ||
87323bda | 409 | int no_wildcard(const char *string) |
68492fc7 | 410 | { |
fcd631ed | 411 | return string[simple_length(string)] == '\0'; |
68492fc7 LK |
412 | } |
413 | ||
82dce998 NTND |
414 | void parse_exclude_pattern(const char **pattern, |
415 | int *patternlen, | |
416 | int *flags, | |
417 | int *nowildcardlen) | |
84460eec NTND |
418 | { |
419 | const char *p = *pattern; | |
420 | size_t i, len; | |
421 | ||
422 | *flags = 0; | |
423 | if (*p == '!') { | |
424 | *flags |= EXC_FLAG_NEGATIVE; | |
425 | p++; | |
426 | } | |
427 | len = strlen(p); | |
428 | if (len && p[len - 1] == '/') { | |
429 | len--; | |
430 | *flags |= EXC_FLAG_MUSTBEDIR; | |
431 | } | |
432 | for (i = 0; i < len; i++) { | |
433 | if (p[i] == '/') | |
434 | break; | |
435 | } | |
436 | if (i == len) | |
437 | *flags |= EXC_FLAG_NODIR; | |
438 | *nowildcardlen = simple_length(p); | |
439 | /* | |
440 | * we should have excluded the trailing slash from 'p' too, | |
441 | * but that's one more allocation. Instead just make sure | |
442 | * nowildcardlen does not exceed real patternlen | |
443 | */ | |
444 | if (*nowildcardlen > len) | |
445 | *nowildcardlen = len; | |
446 | if (*p == '*' && no_wildcard(p + 1)) | |
447 | *flags |= EXC_FLAG_ENDSWITH; | |
448 | *pattern = p; | |
449 | *patternlen = len; | |
450 | } | |
451 | ||
453ec4bd | 452 | void add_exclude(const char *string, const char *base, |
c04318e4 | 453 | int baselen, struct exclude_list *el, int srcpos) |
453ec4bd | 454 | { |
d6b8fc30 | 455 | struct exclude *x; |
84460eec NTND |
456 | int patternlen; |
457 | int flags; | |
458 | int nowildcardlen; | |
453ec4bd | 459 | |
84460eec NTND |
460 | parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); |
461 | if (flags & EXC_FLAG_MUSTBEDIR) { | |
d6b8fc30 | 462 | char *s; |
84460eec | 463 | x = xmalloc(sizeof(*x) + patternlen + 1); |
4b25d091 | 464 | s = (char *)(x+1); |
84460eec NTND |
465 | memcpy(s, string, patternlen); |
466 | s[patternlen] = '\0'; | |
d6b8fc30 | 467 | x->pattern = s; |
d6b8fc30 JH |
468 | } else { |
469 | x = xmalloc(sizeof(*x)); | |
470 | x->pattern = string; | |
471 | } | |
84460eec NTND |
472 | x->patternlen = patternlen; |
473 | x->nowildcardlen = nowildcardlen; | |
453ec4bd LT |
474 | x->base = base; |
475 | x->baselen = baselen; | |
d6b8fc30 | 476 | x->flags = flags; |
c04318e4 | 477 | x->srcpos = srcpos; |
840fc334 AS |
478 | ALLOC_GROW(el->excludes, el->nr + 1, el->alloc); |
479 | el->excludes[el->nr++] = x; | |
c04318e4 | 480 | x->el = el; |
453ec4bd LT |
481 | } |
482 | ||
55fe6f51 NTND |
483 | static void *read_skip_worktree_file_from_index(const char *path, size_t *size, |
484 | struct sha1_stat *sha1_stat) | |
c28b3d6e NTND |
485 | { |
486 | int pos, len; | |
487 | unsigned long sz; | |
488 | enum object_type type; | |
489 | void *data; | |
c28b3d6e NTND |
490 | |
491 | len = strlen(path); | |
71261027 | 492 | pos = cache_name_pos(path, len); |
c28b3d6e NTND |
493 | if (pos < 0) |
494 | return NULL; | |
71261027 | 495 | if (!ce_skip_worktree(active_cache[pos])) |
c28b3d6e | 496 | return NULL; |
71261027 | 497 | data = read_sha1_file(active_cache[pos]->sha1, &type, &sz); |
c28b3d6e NTND |
498 | if (!data || type != OBJ_BLOB) { |
499 | free(data); | |
500 | return NULL; | |
501 | } | |
502 | *size = xsize_t(sz); | |
55fe6f51 NTND |
503 | if (sha1_stat) { |
504 | memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat)); | |
505 | hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); | |
506 | } | |
c28b3d6e NTND |
507 | return data; |
508 | } | |
509 | ||
f6198812 AS |
510 | /* |
511 | * Frees memory within el which was allocated for exclude patterns and | |
512 | * the file buffer. Does not free el itself. | |
513 | */ | |
514 | void clear_exclude_list(struct exclude_list *el) | |
0fd0e241 NTND |
515 | { |
516 | int i; | |
517 | ||
518 | for (i = 0; i < el->nr; i++) | |
519 | free(el->excludes[i]); | |
520 | free(el->excludes); | |
c082df24 | 521 | free(el->filebuf); |
0fd0e241 NTND |
522 | |
523 | el->nr = 0; | |
524 | el->excludes = NULL; | |
c082df24 | 525 | el->filebuf = NULL; |
0fd0e241 NTND |
526 | } |
527 | ||
7e2e4b37 | 528 | static void trim_trailing_spaces(char *buf) |
16402b99 | 529 | { |
e61a6c1d PB |
530 | char *p, *last_space = NULL; |
531 | ||
532 | for (p = buf; *p; p++) | |
533 | switch (*p) { | |
534 | case ' ': | |
535 | if (!last_space) | |
536 | last_space = p; | |
537 | break; | |
538 | case '\\': | |
539 | p++; | |
540 | if (!*p) | |
541 | return; | |
542 | /* fallthrough */ | |
543 | default: | |
544 | last_space = NULL; | |
545 | } | |
546 | ||
547 | if (last_space) | |
548 | *last_space = '\0'; | |
16402b99 NTND |
549 | } |
550 | ||
0dcb8d7f NTND |
551 | /* |
552 | * Given a subdirectory name and "dir" of the current directory, | |
553 | * search the subdir in "dir" and return it, or create a new one if it | |
554 | * does not exist in "dir". | |
555 | * | |
556 | * If "name" has the trailing slash, it'll be excluded in the search. | |
557 | */ | |
558 | static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, | |
559 | struct untracked_cache_dir *dir, | |
560 | const char *name, int len) | |
561 | { | |
562 | int first, last; | |
563 | struct untracked_cache_dir *d; | |
564 | if (!dir) | |
565 | return NULL; | |
566 | if (len && name[len - 1] == '/') | |
567 | len--; | |
568 | first = 0; | |
569 | last = dir->dirs_nr; | |
570 | while (last > first) { | |
571 | int cmp, next = (last + first) >> 1; | |
572 | d = dir->dirs[next]; | |
573 | cmp = strncmp(name, d->name, len); | |
574 | if (!cmp && strlen(d->name) > len) | |
575 | cmp = -1; | |
576 | if (!cmp) | |
577 | return d; | |
578 | if (cmp < 0) { | |
579 | last = next; | |
580 | continue; | |
581 | } | |
582 | first = next+1; | |
583 | } | |
584 | ||
585 | uc->dir_created++; | |
586 | d = xmalloc(sizeof(*d) + len + 1); | |
587 | memset(d, 0, sizeof(*d)); | |
588 | memcpy(d->name, name, len); | |
589 | d->name[len] = '\0'; | |
590 | ||
591 | ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); | |
592 | memmove(dir->dirs + first + 1, dir->dirs + first, | |
593 | (dir->dirs_nr - first) * sizeof(*dir->dirs)); | |
594 | dir->dirs_nr++; | |
595 | dir->dirs[first] = d; | |
596 | return d; | |
597 | } | |
598 | ||
ccad261f NTND |
599 | static void do_invalidate_gitignore(struct untracked_cache_dir *dir) |
600 | { | |
601 | int i; | |
602 | dir->valid = 0; | |
603 | dir->untracked_nr = 0; | |
604 | for (i = 0; i < dir->dirs_nr; i++) | |
605 | do_invalidate_gitignore(dir->dirs[i]); | |
606 | } | |
607 | ||
608 | static void invalidate_gitignore(struct untracked_cache *uc, | |
609 | struct untracked_cache_dir *dir) | |
610 | { | |
611 | uc->gitignore_invalidated++; | |
612 | do_invalidate_gitignore(dir); | |
613 | } | |
614 | ||
91a2288b NTND |
615 | static void invalidate_directory(struct untracked_cache *uc, |
616 | struct untracked_cache_dir *dir) | |
617 | { | |
618 | uc->dir_invalidated++; | |
619 | dir->valid = 0; | |
620 | dir->untracked_nr = 0; | |
621 | } | |
622 | ||
55fe6f51 NTND |
623 | /* |
624 | * Given a file with name "fname", read it (either from disk, or from | |
625 | * the index if "check_index" is non-zero), parse it and store the | |
626 | * exclude rules in "el". | |
627 | * | |
628 | * If "ss" is not NULL, compute SHA-1 of the exclude file and fill | |
629 | * stat data from disk (only valid if add_excludes returns zero). If | |
630 | * ss_valid is non-zero, "ss" must contain good value as input. | |
631 | */ | |
632 | static int add_excludes(const char *fname, const char *base, int baselen, | |
633 | struct exclude_list *el, int check_index, | |
634 | struct sha1_stat *sha1_stat) | |
453ec4bd | 635 | { |
c470701a | 636 | struct stat st; |
c04318e4 | 637 | int fd, i, lineno = 1; |
9d14017a | 638 | size_t size = 0; |
453ec4bd LT |
639 | char *buf, *entry; |
640 | ||
641 | fd = open(fname, O_RDONLY); | |
c28b3d6e | 642 | if (fd < 0 || fstat(fd, &st) < 0) { |
69660731 | 643 | if (errno != ENOENT) |
55b38a48 | 644 | warn_on_inaccessible(fname); |
c28b3d6e NTND |
645 | if (0 <= fd) |
646 | close(fd); | |
647 | if (!check_index || | |
55fe6f51 | 648 | (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) |
c28b3d6e | 649 | return -1; |
45d76f17 NTND |
650 | if (size == 0) { |
651 | free(buf); | |
652 | return 0; | |
653 | } | |
654 | if (buf[size-1] != '\n') { | |
655 | buf = xrealloc(buf, size+1); | |
656 | buf[size++] = '\n'; | |
657 | } | |
d961baa8 | 658 | } else { |
c28b3d6e NTND |
659 | size = xsize_t(st.st_size); |
660 | if (size == 0) { | |
55fe6f51 NTND |
661 | if (sha1_stat) { |
662 | fill_stat_data(&sha1_stat->stat, &st); | |
663 | hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); | |
664 | sha1_stat->valid = 1; | |
665 | } | |
c28b3d6e NTND |
666 | close(fd); |
667 | return 0; | |
668 | } | |
45d76f17 | 669 | buf = xmalloc(size+1); |
c28b3d6e | 670 | if (read_in_full(fd, buf, size) != size) { |
45d76f17 | 671 | free(buf); |
c28b3d6e NTND |
672 | close(fd); |
673 | return -1; | |
674 | } | |
45d76f17 | 675 | buf[size++] = '\n'; |
c28b3d6e | 676 | close(fd); |
55fe6f51 NTND |
677 | if (sha1_stat) { |
678 | int pos; | |
679 | if (sha1_stat->valid && | |
680 | !match_stat_data(&sha1_stat->stat, &st)) | |
681 | ; /* no content change, ss->sha1 still good */ | |
682 | else if (check_index && | |
683 | (pos = cache_name_pos(fname, strlen(fname))) >= 0 && | |
684 | !ce_stage(active_cache[pos]) && | |
685 | ce_uptodate(active_cache[pos]) && | |
686 | !would_convert_to_git(fname)) | |
687 | hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); | |
688 | else | |
689 | hash_sha1_file(buf, size, "blob", sha1_stat->sha1); | |
690 | fill_stat_data(&sha1_stat->stat, &st); | |
691 | sha1_stat->valid = 1; | |
692 | } | |
6ba78238 | 693 | } |
453ec4bd | 694 | |
c082df24 | 695 | el->filebuf = buf; |
453ec4bd | 696 | entry = buf; |
45d76f17 NTND |
697 | for (i = 0; i < size; i++) { |
698 | if (buf[i] == '\n') { | |
453ec4bd LT |
699 | if (entry != buf + i && entry[0] != '#') { |
700 | buf[i - (i && buf[i-1] == '\r')] = 0; | |
7e2e4b37 | 701 | trim_trailing_spaces(entry); |
c04318e4 | 702 | add_exclude(entry, base, baselen, el, lineno); |
453ec4bd | 703 | } |
c04318e4 | 704 | lineno++; |
453ec4bd LT |
705 | entry = buf + i + 1; |
706 | } | |
707 | } | |
708 | return 0; | |
453ec4bd LT |
709 | } |
710 | ||
55fe6f51 NTND |
711 | int add_excludes_from_file_to_list(const char *fname, const char *base, |
712 | int baselen, struct exclude_list *el, | |
713 | int check_index) | |
714 | { | |
715 | return add_excludes(fname, base, baselen, el, check_index, NULL); | |
716 | } | |
717 | ||
c04318e4 AS |
718 | struct exclude_list *add_exclude_list(struct dir_struct *dir, |
719 | int group_type, const char *src) | |
c082df24 AS |
720 | { |
721 | struct exclude_list *el; | |
722 | struct exclude_list_group *group; | |
723 | ||
724 | group = &dir->exclude_list_group[group_type]; | |
725 | ALLOC_GROW(group->el, group->nr + 1, group->alloc); | |
726 | el = &group->el[group->nr++]; | |
727 | memset(el, 0, sizeof(*el)); | |
c04318e4 | 728 | el->src = src; |
c082df24 AS |
729 | return el; |
730 | } | |
731 | ||
732 | /* | |
733 | * Used to set up core.excludesfile and .git/info/exclude lists. | |
734 | */ | |
0dcb8d7f NTND |
735 | static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname, |
736 | struct sha1_stat *sha1_stat) | |
453ec4bd | 737 | { |
c082df24 | 738 | struct exclude_list *el; |
ccad261f NTND |
739 | /* |
740 | * catch setup_standard_excludes() that's called before | |
741 | * dir->untracked is assigned. That function behaves | |
742 | * differently when dir->untracked is non-NULL. | |
743 | */ | |
744 | if (!dir->untracked) | |
745 | dir->unmanaged_exclude_files++; | |
c04318e4 | 746 | el = add_exclude_list(dir, EXC_FILE, fname); |
0dcb8d7f | 747 | if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0) |
453ec4bd LT |
748 | die("cannot use %s as an exclude file", fname); |
749 | } | |
750 | ||
0dcb8d7f NTND |
751 | void add_excludes_from_file(struct dir_struct *dir, const char *fname) |
752 | { | |
ccad261f | 753 | dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */ |
0dcb8d7f NTND |
754 | add_excludes_from_file_1(dir, fname, NULL); |
755 | } | |
756 | ||
82dce998 NTND |
757 | int match_basename(const char *basename, int basenamelen, |
758 | const char *pattern, int prefix, int patternlen, | |
759 | int flags) | |
593cb880 NTND |
760 | { |
761 | if (prefix == patternlen) { | |
0b6e56df JH |
762 | if (patternlen == basenamelen && |
763 | !strncmp_icase(pattern, basename, basenamelen)) | |
593cb880 NTND |
764 | return 1; |
765 | } else if (flags & EXC_FLAG_ENDSWITH) { | |
0b6e56df | 766 | /* "*literal" matching against "fooliteral" */ |
593cb880 | 767 | if (patternlen - 1 <= basenamelen && |
0b6e56df JH |
768 | !strncmp_icase(pattern + 1, |
769 | basename + basenamelen - (patternlen - 1), | |
770 | patternlen - 1)) | |
593cb880 NTND |
771 | return 1; |
772 | } else { | |
0b6e56df JH |
773 | if (fnmatch_icase_mem(pattern, patternlen, |
774 | basename, basenamelen, | |
775 | 0) == 0) | |
593cb880 NTND |
776 | return 1; |
777 | } | |
778 | return 0; | |
779 | } | |
780 | ||
82dce998 NTND |
781 | int match_pathname(const char *pathname, int pathlen, |
782 | const char *base, int baselen, | |
783 | const char *pattern, int prefix, int patternlen, | |
784 | int flags) | |
b5592632 NTND |
785 | { |
786 | const char *name; | |
787 | int namelen; | |
788 | ||
789 | /* | |
790 | * match with FNM_PATHNAME; the pattern has base implicitly | |
791 | * in front of it. | |
792 | */ | |
793 | if (*pattern == '/') { | |
794 | pattern++; | |
982ac873 | 795 | patternlen--; |
b5592632 NTND |
796 | prefix--; |
797 | } | |
798 | ||
799 | /* | |
800 | * baselen does not count the trailing slash. base[] may or | |
801 | * may not end with a trailing slash though. | |
802 | */ | |
803 | if (pathlen < baselen + 1 || | |
804 | (baselen && pathname[baselen] != '/') || | |
805 | strncmp_icase(pathname, base, baselen)) | |
806 | return 0; | |
807 | ||
808 | namelen = baselen ? pathlen - baselen - 1 : pathlen; | |
809 | name = pathname + pathlen - namelen; | |
810 | ||
811 | if (prefix) { | |
812 | /* | |
813 | * if the non-wildcard part is longer than the | |
814 | * remaining pathname, surely it cannot match. | |
815 | */ | |
816 | if (prefix > namelen) | |
817 | return 0; | |
818 | ||
819 | if (strncmp_icase(pattern, name, prefix)) | |
820 | return 0; | |
821 | pattern += prefix; | |
ab3aebc1 | 822 | patternlen -= prefix; |
b5592632 NTND |
823 | name += prefix; |
824 | namelen -= prefix; | |
ab3aebc1 JK |
825 | |
826 | /* | |
827 | * If the whole pattern did not have a wildcard, | |
828 | * then our prefix match is all we need; we | |
829 | * do not need to call fnmatch at all. | |
830 | */ | |
831 | if (!patternlen && !namelen) | |
832 | return 1; | |
b5592632 NTND |
833 | } |
834 | ||
ab3aebc1 JK |
835 | return fnmatch_icase_mem(pattern, patternlen, |
836 | name, namelen, | |
f30366b2 | 837 | WM_PATHNAME) == 0; |
b5592632 NTND |
838 | } |
839 | ||
578cd7c3 AS |
840 | /* |
841 | * Scan the given exclude list in reverse to see whether pathname | |
842 | * should be ignored. The first match (i.e. the last on the list), if | |
843 | * any, determines the fate. Returns the exclude_list element which | |
844 | * matched, or NULL for undecided. | |
453ec4bd | 845 | */ |
578cd7c3 AS |
846 | static struct exclude *last_exclude_matching_from_list(const char *pathname, |
847 | int pathlen, | |
848 | const char *basename, | |
849 | int *dtype, | |
850 | struct exclude_list *el) | |
453ec4bd LT |
851 | { |
852 | int i; | |
853 | ||
35a94d44 | 854 | if (!el->nr) |
578cd7c3 | 855 | return NULL; /* undefined */ |
d6b8fc30 | 856 | |
35a94d44 NTND |
857 | for (i = el->nr - 1; 0 <= i; i--) { |
858 | struct exclude *x = el->excludes[i]; | |
b5592632 | 859 | const char *exclude = x->pattern; |
b5592632 | 860 | int prefix = x->nowildcardlen; |
35a94d44 NTND |
861 | |
862 | if (x->flags & EXC_FLAG_MUSTBEDIR) { | |
863 | if (*dtype == DT_UNKNOWN) | |
864 | *dtype = get_dtype(NULL, pathname, pathlen); | |
865 | if (*dtype != DT_DIR) | |
866 | continue; | |
867 | } | |
d6b8fc30 | 868 | |
35a94d44 | 869 | if (x->flags & EXC_FLAG_NODIR) { |
593cb880 NTND |
870 | if (match_basename(basename, |
871 | pathlen - (basename - pathname), | |
872 | exclude, prefix, x->patternlen, | |
873 | x->flags)) | |
578cd7c3 | 874 | return x; |
35a94d44 NTND |
875 | continue; |
876 | } | |
877 | ||
b5592632 NTND |
878 | assert(x->baselen == 0 || x->base[x->baselen - 1] == '/'); |
879 | if (match_pathname(pathname, pathlen, | |
880 | x->base, x->baselen ? x->baselen - 1 : 0, | |
881 | exclude, prefix, x->patternlen, x->flags)) | |
578cd7c3 | 882 | return x; |
453ec4bd | 883 | } |
578cd7c3 AS |
884 | return NULL; /* undecided */ |
885 | } | |
886 | ||
887 | /* | |
888 | * Scan the list and let the last match determine the fate. | |
889 | * Return 1 for exclude, 0 for include and -1 for undecided. | |
890 | */ | |
891 | int is_excluded_from_list(const char *pathname, | |
892 | int pathlen, const char *basename, int *dtype, | |
893 | struct exclude_list *el) | |
894 | { | |
895 | struct exclude *exclude; | |
896 | exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); | |
897 | if (exclude) | |
898 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; | |
453ec4bd LT |
899 | return -1; /* undecided */ |
900 | } | |
901 | ||
46aa2f95 KB |
902 | static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, |
903 | const char *pathname, int pathlen, const char *basename, | |
904 | int *dtype_p) | |
905 | { | |
906 | int i, j; | |
907 | struct exclude_list_group *group; | |
908 | struct exclude *exclude; | |
909 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { | |
910 | group = &dir->exclude_list_group[i]; | |
911 | for (j = group->nr - 1; j >= 0; j--) { | |
912 | exclude = last_exclude_matching_from_list( | |
913 | pathname, pathlen, basename, dtype_p, | |
914 | &group->el[j]); | |
915 | if (exclude) | |
916 | return exclude; | |
917 | } | |
918 | } | |
919 | return NULL; | |
920 | } | |
921 | ||
6cd5c582 KB |
922 | /* |
923 | * Loads the per-directory exclude list for the substring of base | |
924 | * which has a char length of baselen. | |
925 | */ | |
926 | static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) | |
927 | { | |
928 | struct exclude_list_group *group; | |
929 | struct exclude_list *el; | |
930 | struct exclude_stack *stk = NULL; | |
0dcb8d7f | 931 | struct untracked_cache_dir *untracked; |
6cd5c582 KB |
932 | int current; |
933 | ||
6cd5c582 KB |
934 | group = &dir->exclude_list_group[EXC_DIRS]; |
935 | ||
d961baa8 NTND |
936 | /* |
937 | * Pop the exclude lists from the EXCL_DIRS exclude_list_group | |
6cd5c582 | 938 | * which originate from directories not in the prefix of the |
d961baa8 NTND |
939 | * path being checked. |
940 | */ | |
6cd5c582 KB |
941 | while ((stk = dir->exclude_stack) != NULL) { |
942 | if (stk->baselen <= baselen && | |
aceb9429 | 943 | !strncmp(dir->basebuf.buf, base, stk->baselen)) |
6cd5c582 KB |
944 | break; |
945 | el = &group->el[dir->exclude_stack->exclude_ix]; | |
946 | dir->exclude_stack = stk->prev; | |
95c6f271 | 947 | dir->exclude = NULL; |
aceb9429 | 948 | free((char *)el->src); /* see strbuf_detach() below */ |
6cd5c582 KB |
949 | clear_exclude_list(el); |
950 | free(stk); | |
951 | group->nr--; | |
952 | } | |
953 | ||
95c6f271 KB |
954 | /* Skip traversing into sub directories if the parent is excluded */ |
955 | if (dir->exclude) | |
956 | return; | |
957 | ||
aceb9429 NTND |
958 | /* |
959 | * Lazy initialization. All call sites currently just | |
960 | * memset(dir, 0, sizeof(*dir)) before use. Changing all of | |
961 | * them seems lots of work for little benefit. | |
962 | */ | |
963 | if (!dir->basebuf.buf) | |
964 | strbuf_init(&dir->basebuf, PATH_MAX); | |
965 | ||
6cd5c582 KB |
966 | /* Read from the parent directories and push them down. */ |
967 | current = stk ? stk->baselen : -1; | |
aceb9429 | 968 | strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); |
0dcb8d7f NTND |
969 | if (dir->untracked) |
970 | untracked = stk ? stk->ucd : dir->untracked->root; | |
971 | else | |
972 | untracked = NULL; | |
973 | ||
6cd5c582 | 974 | while (current < baselen) { |
6cd5c582 | 975 | const char *cp; |
0dcb8d7f | 976 | struct sha1_stat sha1_stat; |
6cd5c582 | 977 | |
03e11a71 | 978 | stk = xcalloc(1, sizeof(*stk)); |
6cd5c582 KB |
979 | if (current < 0) { |
980 | cp = base; | |
981 | current = 0; | |
d961baa8 | 982 | } else { |
6cd5c582 KB |
983 | cp = strchr(base + current + 1, '/'); |
984 | if (!cp) | |
985 | die("oops in prep_exclude"); | |
986 | cp++; | |
0dcb8d7f NTND |
987 | untracked = |
988 | lookup_untracked(dir->untracked, untracked, | |
989 | base + current, | |
990 | cp - base - current); | |
6cd5c582 KB |
991 | } |
992 | stk->prev = dir->exclude_stack; | |
993 | stk->baselen = cp - base; | |
95c6f271 | 994 | stk->exclude_ix = group->nr; |
0dcb8d7f | 995 | stk->ucd = untracked; |
95c6f271 | 996 | el = add_exclude_list(dir, EXC_DIRS, NULL); |
aceb9429 NTND |
997 | strbuf_add(&dir->basebuf, base + current, stk->baselen - current); |
998 | assert(stk->baselen == dir->basebuf.len); | |
95c6f271 KB |
999 | |
1000 | /* Abort if the directory is excluded */ | |
1001 | if (stk->baselen) { | |
1002 | int dt = DT_DIR; | |
aceb9429 | 1003 | dir->basebuf.buf[stk->baselen - 1] = 0; |
95c6f271 | 1004 | dir->exclude = last_exclude_matching_from_lists(dir, |
aceb9429 NTND |
1005 | dir->basebuf.buf, stk->baselen - 1, |
1006 | dir->basebuf.buf + current, &dt); | |
1007 | dir->basebuf.buf[stk->baselen - 1] = '/'; | |
c3c327de KB |
1008 | if (dir->exclude && |
1009 | dir->exclude->flags & EXC_FLAG_NEGATIVE) | |
1010 | dir->exclude = NULL; | |
95c6f271 | 1011 | if (dir->exclude) { |
95c6f271 KB |
1012 | dir->exclude_stack = stk; |
1013 | return; | |
1014 | } | |
1015 | } | |
1016 | ||
aceb9429 | 1017 | /* Try to read per-directory file */ |
0dcb8d7f NTND |
1018 | hashclr(sha1_stat.sha1); |
1019 | sha1_stat.valid = 0; | |
aceb9429 | 1020 | if (dir->exclude_per_dir) { |
95c6f271 KB |
1021 | /* |
1022 | * dir->basebuf gets reused by the traversal, but we | |
1023 | * need fname to remain unchanged to ensure the src | |
1024 | * member of each struct exclude correctly | |
1025 | * back-references its source file. Other invocations | |
1026 | * of add_exclude_list provide stable strings, so we | |
aceb9429 | 1027 | * strbuf_detach() and free() here in the caller. |
95c6f271 | 1028 | */ |
aceb9429 NTND |
1029 | struct strbuf sb = STRBUF_INIT; |
1030 | strbuf_addbuf(&sb, &dir->basebuf); | |
1031 | strbuf_addstr(&sb, dir->exclude_per_dir); | |
1032 | el->src = strbuf_detach(&sb, NULL); | |
0dcb8d7f NTND |
1033 | add_excludes(el->src, el->src, stk->baselen, el, 1, |
1034 | untracked ? &sha1_stat : NULL); | |
1035 | } | |
5ebf79ad NTND |
1036 | /* |
1037 | * NEEDSWORK: when untracked cache is enabled, prep_exclude() | |
1038 | * will first be called in valid_cached_dir() then maybe many | |
1039 | * times more in last_exclude_matching(). When the cache is | |
1040 | * used, last_exclude_matching() will not be called and | |
1041 | * reading .gitignore content will be a waste. | |
1042 | * | |
1043 | * So when it's called by valid_cached_dir() and we can get | |
1044 | * .gitignore SHA-1 from the index (i.e. .gitignore is not | |
1045 | * modified on work tree), we could delay reading the | |
1046 | * .gitignore content until we absolutely need it in | |
1047 | * last_exclude_matching(). Be careful about ignore rule | |
1048 | * order, though, if you do that. | |
1049 | */ | |
1050 | if (untracked && | |
1051 | hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) { | |
1052 | invalidate_gitignore(dir->untracked, untracked); | |
0dcb8d7f | 1053 | hashcpy(untracked->exclude_sha1, sha1_stat.sha1); |
95c6f271 | 1054 | } |
6cd5c582 KB |
1055 | dir->exclude_stack = stk; |
1056 | current = stk->baselen; | |
1057 | } | |
aceb9429 | 1058 | strbuf_setlen(&dir->basebuf, baselen); |
6cd5c582 KB |
1059 | } |
1060 | ||
f4cd69a6 AS |
1061 | /* |
1062 | * Loads the exclude lists for the directory containing pathname, then | |
1063 | * scans all exclude lists to determine whether pathname is excluded. | |
1064 | * Returns the exclude_list element which matched, or NULL for | |
1065 | * undecided. | |
1066 | */ | |
b07bc8c8 | 1067 | struct exclude *last_exclude_matching(struct dir_struct *dir, |
f4cd69a6 AS |
1068 | const char *pathname, |
1069 | int *dtype_p) | |
453ec4bd LT |
1070 | { |
1071 | int pathlen = strlen(pathname); | |
68492fc7 LK |
1072 | const char *basename = strrchr(pathname, '/'); |
1073 | basename = (basename) ? basename+1 : pathname; | |
453ec4bd | 1074 | |
63d285c8 | 1075 | prep_exclude(dir, pathname, basename-pathname); |
c082df24 | 1076 | |
95c6f271 KB |
1077 | if (dir->exclude) |
1078 | return dir->exclude; | |
1079 | ||
46aa2f95 KB |
1080 | return last_exclude_matching_from_lists(dir, pathname, pathlen, |
1081 | basename, dtype_p); | |
f4cd69a6 AS |
1082 | } |
1083 | ||
1084 | /* | |
1085 | * Loads the exclude lists for the directory containing pathname, then | |
1086 | * scans all exclude lists to determine whether pathname is excluded. | |
1087 | * Returns 1 if true, otherwise 0. | |
1088 | */ | |
b07bc8c8 | 1089 | int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p) |
f4cd69a6 AS |
1090 | { |
1091 | struct exclude *exclude = | |
1092 | last_exclude_matching(dir, pathname, dtype_p); | |
1093 | if (exclude) | |
1094 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; | |
453ec4bd LT |
1095 | return 0; |
1096 | } | |
1097 | ||
f3fa1838 JH |
1098 | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
1099 | { | |
453ec4bd LT |
1100 | struct dir_entry *ent; |
1101 | ||
453ec4bd LT |
1102 | ent = xmalloc(sizeof(*ent) + len + 1); |
1103 | ent->len = len; | |
1104 | memcpy(ent->name, pathname, len); | |
1105 | ent->name[len] = 0; | |
4d06f8ac | 1106 | return ent; |
453ec4bd LT |
1107 | } |
1108 | ||
159b3212 | 1109 | static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) |
6815e569 | 1110 | { |
ebbd7439 | 1111 | if (cache_file_exists(pathname, len, ignore_case)) |
6815e569 JK |
1112 | return NULL; |
1113 | ||
25fd2f7a | 1114 | ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
6815e569 JK |
1115 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
1116 | } | |
1117 | ||
108da0db | 1118 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len) |
2abd31b0 | 1119 | { |
6e4f981f | 1120 | if (!cache_name_is_other(pathname, len)) |
2abd31b0 JK |
1121 | return NULL; |
1122 | ||
25fd2f7a | 1123 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
2abd31b0 JK |
1124 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
1125 | } | |
1126 | ||
09595258 LT |
1127 | enum exist_status { |
1128 | index_nonexistent = 0, | |
1129 | index_directory, | |
4b05548f | 1130 | index_gitdir |
09595258 LT |
1131 | }; |
1132 | ||
5102c617 | 1133 | /* |
71261027 | 1134 | * Do not use the alphabetically sorted index to look up |
5102c617 | 1135 | * the directory name; instead, use the case insensitive |
ebbd7439 | 1136 | * directory hash. |
5102c617 JJ |
1137 | */ |
1138 | static enum exist_status directory_exists_in_index_icase(const char *dirname, int len) | |
1139 | { | |
d28eec26 | 1140 | const struct cache_entry *ce = cache_dir_exists(dirname, len); |
5102c617 JJ |
1141 | unsigned char endchar; |
1142 | ||
1143 | if (!ce) | |
1144 | return index_nonexistent; | |
1145 | endchar = ce->name[len]; | |
1146 | ||
1147 | /* | |
1148 | * The cache_entry structure returned will contain this dirname | |
1149 | * and possibly additional path components. | |
1150 | */ | |
1151 | if (endchar == '/') | |
1152 | return index_directory; | |
1153 | ||
1154 | /* | |
1155 | * If there are no additional path components, then this cache_entry | |
1156 | * represents a submodule. Submodules, despite being directories, | |
1157 | * are stored in the cache without a closing slash. | |
1158 | */ | |
1159 | if (!endchar && S_ISGITLINK(ce->ce_mode)) | |
1160 | return index_gitdir; | |
1161 | ||
1162 | /* This should never be hit, but it exists just in case. */ | |
1163 | return index_nonexistent; | |
1164 | } | |
1165 | ||
09595258 LT |
1166 | /* |
1167 | * The index sorts alphabetically by entry name, which | |
1168 | * means that a gitlink sorts as '\0' at the end, while | |
1169 | * a directory (which is defined not as an entry, but as | |
1170 | * the files it contains) will sort with the '/' at the | |
1171 | * end. | |
1172 | */ | |
1173 | static enum exist_status directory_exists_in_index(const char *dirname, int len) | |
453ec4bd | 1174 | { |
5102c617 JJ |
1175 | int pos; |
1176 | ||
1177 | if (ignore_case) | |
1178 | return directory_exists_in_index_icase(dirname, len); | |
1179 | ||
1180 | pos = cache_name_pos(dirname, len); | |
09595258 LT |
1181 | if (pos < 0) |
1182 | pos = -pos-1; | |
1183 | while (pos < active_nr) { | |
9c5e6c80 | 1184 | const struct cache_entry *ce = active_cache[pos++]; |
09595258 LT |
1185 | unsigned char endchar; |
1186 | ||
1187 | if (strncmp(ce->name, dirname, len)) | |
1188 | break; | |
1189 | endchar = ce->name[len]; | |
1190 | if (endchar > '/') | |
1191 | break; | |
1192 | if (endchar == '/') | |
1193 | return index_directory; | |
7a51ed66 | 1194 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
09595258 LT |
1195 | return index_gitdir; |
1196 | } | |
1197 | return index_nonexistent; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * When we find a directory when traversing the filesystem, we | |
1202 | * have three distinct cases: | |
1203 | * | |
1204 | * - ignore it | |
1205 | * - see it as a directory | |
1206 | * - recurse into it | |
1207 | * | |
1208 | * and which one we choose depends on a combination of existing | |
1209 | * git index contents and the flags passed into the directory | |
1210 | * traversal routine. | |
1211 | * | |
1212 | * Case 1: If we *already* have entries in the index under that | |
5bd8e2d8 KB |
1213 | * directory name, we always recurse into the directory to see |
1214 | * all the files. | |
09595258 LT |
1215 | * |
1216 | * Case 2: If we *already* have that directory name as a gitlink, | |
1217 | * we always continue to see it as a gitlink, regardless of whether | |
1218 | * there is an actual git directory there or not (it might not | |
1219 | * be checked out as a subproject!) | |
1220 | * | |
1221 | * Case 3: if we didn't have it in the index previously, we | |
1222 | * have a few sub-cases: | |
1223 | * | |
1224 | * (a) if "show_other_directories" is true, we show it as | |
1225 | * just a directory, unless "hide_empty_directories" is | |
defd7c7b KB |
1226 | * also true, in which case we need to check if it contains any |
1227 | * untracked and / or ignored files. | |
09595258 | 1228 | * (b) if it looks like a git directory, and we don't have |
302b9282 | 1229 | * 'no_gitlinks' set we treat it as a gitlink, and show it |
09595258 LT |
1230 | * as a directory. |
1231 | * (c) otherwise, we recurse into it. | |
1232 | */ | |
defd7c7b | 1233 | static enum path_treatment treat_directory(struct dir_struct *dir, |
0dcb8d7f | 1234 | struct untracked_cache_dir *untracked, |
721ac4ed | 1235 | const char *dirname, int len, int exclude, |
09595258 LT |
1236 | const struct path_simplify *simplify) |
1237 | { | |
1238 | /* The "len-1" is to strip the final '/' */ | |
1239 | switch (directory_exists_in_index(dirname, len-1)) { | |
1240 | case index_directory: | |
defd7c7b | 1241 | return path_recurse; |
09595258 LT |
1242 | |
1243 | case index_gitdir: | |
26c986e1 | 1244 | return path_none; |
09595258 LT |
1245 | |
1246 | case index_nonexistent: | |
7c4c97c0 | 1247 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
09595258 | 1248 | break; |
7c4c97c0 | 1249 | if (!(dir->flags & DIR_NO_GITLINKS)) { |
09595258 LT |
1250 | unsigned char sha1[20]; |
1251 | if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0) | |
defd7c7b | 1252 | return path_untracked; |
09595258 | 1253 | } |
defd7c7b | 1254 | return path_recurse; |
09595258 LT |
1255 | } |
1256 | ||
1257 | /* This is the "show_other_directories" case */ | |
721ac4ed | 1258 | |
184d2a8e | 1259 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
defd7c7b KB |
1260 | return exclude ? path_excluded : path_untracked; |
1261 | ||
0dcb8d7f NTND |
1262 | untracked = lookup_untracked(dir->untracked, untracked, dirname, len); |
1263 | return read_directory_recursive(dir, dirname, len, | |
1264 | untracked, 1, simplify); | |
453ec4bd LT |
1265 | } |
1266 | ||
9fc42d60 LT |
1267 | /* |
1268 | * This is an inexact early pruning of any recursive directory | |
1269 | * reading - if the path cannot possibly be in the pathspec, | |
1270 | * return true, and we'll skip it early. | |
1271 | */ | |
1272 | static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify) | |
1273 | { | |
1274 | if (simplify) { | |
1275 | for (;;) { | |
1276 | const char *match = simplify->path; | |
1277 | int len = simplify->len; | |
1278 | ||
1279 | if (!match) | |
1280 | break; | |
1281 | if (len > pathlen) | |
1282 | len = pathlen; | |
1283 | if (!memcmp(path, match, len)) | |
1284 | return 0; | |
1285 | simplify++; | |
1286 | } | |
1287 | return 1; | |
1288 | } | |
1289 | return 0; | |
1290 | } | |
1291 | ||
29209cbe JK |
1292 | /* |
1293 | * This function tells us whether an excluded path matches a | |
1294 | * list of "interesting" pathspecs. That is, whether a path matched | |
1295 | * by any of the pathspecs could possibly be ignored by excluding | |
1296 | * the specified path. This can happen if: | |
1297 | * | |
1298 | * 1. the path is mentioned explicitly in the pathspec | |
1299 | * | |
1300 | * 2. the path is a directory prefix of some element in the | |
1301 | * pathspec | |
1302 | */ | |
1303 | static int exclude_matches_pathspec(const char *path, int len, | |
1304 | const struct path_simplify *simplify) | |
e96980ef JK |
1305 | { |
1306 | if (simplify) { | |
1307 | for (; simplify->path; simplify++) { | |
1308 | if (len == simplify->len | |
1309 | && !memcmp(path, simplify->path, len)) | |
1310 | return 1; | |
29209cbe JK |
1311 | if (len < simplify->len |
1312 | && simplify->path[len] == '/' | |
1313 | && !memcmp(path, simplify->path, len)) | |
1314 | return 1; | |
e96980ef JK |
1315 | } |
1316 | } | |
1317 | return 0; | |
1318 | } | |
1319 | ||
443e061a LT |
1320 | static int get_index_dtype(const char *path, int len) |
1321 | { | |
1322 | int pos; | |
9c5e6c80 | 1323 | const struct cache_entry *ce; |
443e061a | 1324 | |
ebbd7439 | 1325 | ce = cache_file_exists(path, len, 0); |
443e061a LT |
1326 | if (ce) { |
1327 | if (!ce_uptodate(ce)) | |
1328 | return DT_UNKNOWN; | |
1329 | if (S_ISGITLINK(ce->ce_mode)) | |
1330 | return DT_DIR; | |
1331 | /* | |
1332 | * Nobody actually cares about the | |
1333 | * difference between DT_LNK and DT_REG | |
1334 | */ | |
1335 | return DT_REG; | |
1336 | } | |
1337 | ||
1338 | /* Try to look it up as a directory */ | |
1339 | pos = cache_name_pos(path, len); | |
1340 | if (pos >= 0) | |
1341 | return DT_UNKNOWN; | |
1342 | pos = -pos-1; | |
1343 | while (pos < active_nr) { | |
1344 | ce = active_cache[pos++]; | |
1345 | if (strncmp(ce->name, path, len)) | |
1346 | break; | |
1347 | if (ce->name[len] > '/') | |
1348 | break; | |
1349 | if (ce->name[len] < '/') | |
1350 | continue; | |
1351 | if (!ce_uptodate(ce)) | |
1352 | break; /* continue? */ | |
1353 | return DT_DIR; | |
1354 | } | |
1355 | return DT_UNKNOWN; | |
1356 | } | |
1357 | ||
caa6b782 | 1358 | static int get_dtype(struct dirent *de, const char *path, int len) |
07134421 | 1359 | { |
6831a88a | 1360 | int dtype = de ? DTYPE(de) : DT_UNKNOWN; |
07134421 LT |
1361 | struct stat st; |
1362 | ||
1363 | if (dtype != DT_UNKNOWN) | |
1364 | return dtype; | |
443e061a LT |
1365 | dtype = get_index_dtype(path, len); |
1366 | if (dtype != DT_UNKNOWN) | |
1367 | return dtype; | |
1368 | if (lstat(path, &st)) | |
07134421 LT |
1369 | return dtype; |
1370 | if (S_ISREG(st.st_mode)) | |
1371 | return DT_REG; | |
1372 | if (S_ISDIR(st.st_mode)) | |
1373 | return DT_DIR; | |
1374 | if (S_ISLNK(st.st_mode)) | |
1375 | return DT_LNK; | |
1376 | return dtype; | |
1377 | } | |
1378 | ||
16e2cfa9 | 1379 | static enum path_treatment treat_one_path(struct dir_struct *dir, |
0dcb8d7f | 1380 | struct untracked_cache_dir *untracked, |
49dc2cc2 | 1381 | struct strbuf *path, |
16e2cfa9 JH |
1382 | const struct path_simplify *simplify, |
1383 | int dtype, struct dirent *de) | |
53cc5356 | 1384 | { |
8aaf8d77 | 1385 | int exclude; |
ebbd7439 | 1386 | int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case); |
2eac2a4c | 1387 | |
8aaf8d77 KB |
1388 | if (dtype == DT_UNKNOWN) |
1389 | dtype = get_dtype(de, path->buf, path->len); | |
1390 | ||
1391 | /* Always exclude indexed files */ | |
2eac2a4c | 1392 | if (dtype != DT_DIR && has_path_in_index) |
defd7c7b | 1393 | return path_none; |
8aaf8d77 | 1394 | |
2eac2a4c JH |
1395 | /* |
1396 | * When we are looking at a directory P in the working tree, | |
1397 | * there are three cases: | |
1398 | * | |
1399 | * (1) P exists in the index. Everything inside the directory P in | |
1400 | * the working tree needs to go when P is checked out from the | |
1401 | * index. | |
1402 | * | |
1403 | * (2) P does not exist in the index, but there is P/Q in the index. | |
1404 | * We know P will stay a directory when we check out the contents | |
1405 | * of the index, but we do not know yet if there is a directory | |
1406 | * P/Q in the working tree to be killed, so we need to recurse. | |
1407 | * | |
1408 | * (3) P does not exist in the index, and there is no P/Q in the index | |
1409 | * to require P to be a directory, either. Only in this case, we | |
1410 | * know that everything inside P will not be killed without | |
1411 | * recursing. | |
1412 | */ | |
1413 | if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && | |
1414 | (dtype == DT_DIR) && | |
de372b1b ES |
1415 | !has_path_in_index && |
1416 | (directory_exists_in_index(path->buf, path->len) == index_nonexistent)) | |
1417 | return path_none; | |
8aaf8d77 KB |
1418 | |
1419 | exclude = is_excluded(dir, path->buf, &dtype); | |
53cc5356 JH |
1420 | |
1421 | /* | |
1422 | * Excluded? If we don't explicitly want to show | |
1423 | * ignored files, ignore it | |
1424 | */ | |
0aaf62b6 | 1425 | if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO))) |
defd7c7b | 1426 | return path_excluded; |
53cc5356 | 1427 | |
53cc5356 JH |
1428 | switch (dtype) { |
1429 | default: | |
defd7c7b | 1430 | return path_none; |
53cc5356 | 1431 | case DT_DIR: |
49dc2cc2 | 1432 | strbuf_addch(path, '/'); |
0dcb8d7f | 1433 | return treat_directory(dir, untracked, path->buf, path->len, exclude, |
defd7c7b | 1434 | simplify); |
53cc5356 JH |
1435 | case DT_REG: |
1436 | case DT_LNK: | |
defd7c7b | 1437 | return exclude ? path_excluded : path_untracked; |
53cc5356 | 1438 | } |
53cc5356 JH |
1439 | } |
1440 | ||
91a2288b NTND |
1441 | static enum path_treatment treat_path_fast(struct dir_struct *dir, |
1442 | struct untracked_cache_dir *untracked, | |
1443 | struct cached_dir *cdir, | |
1444 | struct strbuf *path, | |
1445 | int baselen, | |
1446 | const struct path_simplify *simplify) | |
1447 | { | |
1448 | strbuf_setlen(path, baselen); | |
1449 | if (!cdir->ucd) { | |
1450 | strbuf_addstr(path, cdir->file); | |
1451 | return path_untracked; | |
1452 | } | |
1453 | strbuf_addstr(path, cdir->ucd->name); | |
1454 | /* treat_one_path() does this before it calls treat_directory() */ | |
1455 | if (path->buf[path->len - 1] != '/') | |
1456 | strbuf_addch(path, '/'); | |
1457 | if (cdir->ucd->check_only) | |
1458 | /* | |
1459 | * check_only is set as a result of treat_directory() getting | |
1460 | * to its bottom. Verify again the same set of directories | |
1461 | * with check_only set. | |
1462 | */ | |
1463 | return read_directory_recursive(dir, path->buf, path->len, | |
1464 | cdir->ucd, 1, simplify); | |
1465 | /* | |
1466 | * We get path_recurse in the first run when | |
1467 | * directory_exists_in_index() returns index_nonexistent. We | |
1468 | * are sure that new changes in the index does not impact the | |
1469 | * outcome. Return now. | |
1470 | */ | |
1471 | return path_recurse; | |
1472 | } | |
1473 | ||
16e2cfa9 | 1474 | static enum path_treatment treat_path(struct dir_struct *dir, |
0dcb8d7f | 1475 | struct untracked_cache_dir *untracked, |
cf7c6148 | 1476 | struct cached_dir *cdir, |
49dc2cc2 | 1477 | struct strbuf *path, |
16e2cfa9 | 1478 | int baselen, |
49dc2cc2 | 1479 | const struct path_simplify *simplify) |
16e2cfa9 JH |
1480 | { |
1481 | int dtype; | |
cf7c6148 | 1482 | struct dirent *de = cdir->de; |
16e2cfa9 | 1483 | |
91a2288b NTND |
1484 | if (!de) |
1485 | return treat_path_fast(dir, untracked, cdir, path, | |
1486 | baselen, simplify); | |
16e2cfa9 | 1487 | if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git")) |
defd7c7b | 1488 | return path_none; |
49dc2cc2 RS |
1489 | strbuf_setlen(path, baselen); |
1490 | strbuf_addstr(path, de->d_name); | |
1491 | if (simplify_away(path->buf, path->len, simplify)) | |
defd7c7b | 1492 | return path_none; |
16e2cfa9 JH |
1493 | |
1494 | dtype = DTYPE(de); | |
0dcb8d7f NTND |
1495 | return treat_one_path(dir, untracked, path, simplify, dtype, de); |
1496 | } | |
1497 | ||
1498 | static void add_untracked(struct untracked_cache_dir *dir, const char *name) | |
1499 | { | |
1500 | if (!dir) | |
1501 | return; | |
1502 | ALLOC_GROW(dir->untracked, dir->untracked_nr + 1, | |
1503 | dir->untracked_alloc); | |
1504 | dir->untracked[dir->untracked_nr++] = xstrdup(name); | |
16e2cfa9 JH |
1505 | } |
1506 | ||
91a2288b NTND |
1507 | static int valid_cached_dir(struct dir_struct *dir, |
1508 | struct untracked_cache_dir *untracked, | |
1509 | struct strbuf *path, | |
1510 | int check_only) | |
1511 | { | |
1512 | struct stat st; | |
1513 | ||
1514 | if (!untracked) | |
1515 | return 0; | |
1516 | ||
1517 | if (stat(path->len ? path->buf : ".", &st)) { | |
1518 | invalidate_directory(dir->untracked, untracked); | |
1519 | memset(&untracked->stat_data, 0, sizeof(untracked->stat_data)); | |
1520 | return 0; | |
1521 | } | |
1522 | if (!untracked->valid || | |
1523 | match_stat_data(&untracked->stat_data, &st)) { | |
1524 | if (untracked->valid) | |
1525 | invalidate_directory(dir->untracked, untracked); | |
1526 | fill_stat_data(&untracked->stat_data, &st); | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | if (untracked->check_only != !!check_only) { | |
1531 | invalidate_directory(dir->untracked, untracked); | |
1532 | return 0; | |
1533 | } | |
1534 | ||
1535 | /* | |
1536 | * prep_exclude will be called eventually on this directory, | |
1537 | * but it's called much later in last_exclude_matching(). We | |
1538 | * need it now to determine the validity of the cache for this | |
1539 | * path. The next calls will be nearly no-op, the way | |
1540 | * prep_exclude() is designed. | |
1541 | */ | |
1542 | if (path->len && path->buf[path->len - 1] != '/') { | |
1543 | strbuf_addch(path, '/'); | |
1544 | prep_exclude(dir, path->buf, path->len); | |
1545 | strbuf_setlen(path, path->len - 1); | |
1546 | } else | |
1547 | prep_exclude(dir, path->buf, path->len); | |
1548 | ||
1549 | /* hopefully prep_exclude() haven't invalidated this entry... */ | |
1550 | return untracked->valid; | |
1551 | } | |
1552 | ||
cf7c6148 NTND |
1553 | static int open_cached_dir(struct cached_dir *cdir, |
1554 | struct dir_struct *dir, | |
1555 | struct untracked_cache_dir *untracked, | |
1556 | struct strbuf *path, | |
1557 | int check_only) | |
1558 | { | |
1559 | memset(cdir, 0, sizeof(*cdir)); | |
1560 | cdir->untracked = untracked; | |
91a2288b NTND |
1561 | if (valid_cached_dir(dir, untracked, path, check_only)) |
1562 | return 0; | |
cf7c6148 | 1563 | cdir->fdir = opendir(path->len ? path->buf : "."); |
91a2288b NTND |
1564 | if (dir->untracked) |
1565 | dir->untracked->dir_opened++; | |
cf7c6148 NTND |
1566 | if (!cdir->fdir) |
1567 | return -1; | |
1568 | return 0; | |
1569 | } | |
1570 | ||
1571 | static int read_cached_dir(struct cached_dir *cdir) | |
1572 | { | |
1573 | if (cdir->fdir) { | |
1574 | cdir->de = readdir(cdir->fdir); | |
1575 | if (!cdir->de) | |
1576 | return -1; | |
1577 | return 0; | |
1578 | } | |
91a2288b NTND |
1579 | while (cdir->nr_dirs < cdir->untracked->dirs_nr) { |
1580 | struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs]; | |
1581 | cdir->ucd = d; | |
1582 | cdir->nr_dirs++; | |
1583 | return 0; | |
1584 | } | |
1585 | cdir->ucd = NULL; | |
1586 | if (cdir->nr_files < cdir->untracked->untracked_nr) { | |
1587 | struct untracked_cache_dir *d = cdir->untracked; | |
1588 | cdir->file = d->untracked[cdir->nr_files++]; | |
1589 | return 0; | |
1590 | } | |
cf7c6148 NTND |
1591 | return -1; |
1592 | } | |
1593 | ||
1594 | static void close_cached_dir(struct cached_dir *cdir) | |
1595 | { | |
1596 | if (cdir->fdir) | |
1597 | closedir(cdir->fdir); | |
91a2288b NTND |
1598 | /* |
1599 | * We have gone through this directory and found no untracked | |
1600 | * entries. Mark it valid. | |
1601 | */ | |
1602 | if (cdir->untracked) | |
1603 | cdir->untracked->valid = 1; | |
cf7c6148 NTND |
1604 | } |
1605 | ||
453ec4bd LT |
1606 | /* |
1607 | * Read a directory tree. We currently ignore anything but | |
1608 | * directories, regular files and symlinks. That's because git | |
1609 | * doesn't handle them at all yet. Maybe that will change some | |
1610 | * day. | |
1611 | * | |
1612 | * Also, we ignore the name ".git" (even if it is not a directory). | |
1613 | * That likely will not change. | |
defd7c7b KB |
1614 | * |
1615 | * Returns the most significant path_treatment value encountered in the scan. | |
453ec4bd | 1616 | */ |
defd7c7b | 1617 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
53cc5356 | 1618 | const char *base, int baselen, |
0dcb8d7f | 1619 | struct untracked_cache_dir *untracked, int check_only, |
53cc5356 | 1620 | const struct path_simplify *simplify) |
453ec4bd | 1621 | { |
cf7c6148 | 1622 | struct cached_dir cdir; |
defd7c7b | 1623 | enum path_treatment state, subdir_state, dir_state = path_none; |
bef36921 | 1624 | struct strbuf path = STRBUF_INIT; |
453ec4bd | 1625 | |
bef36921 | 1626 | strbuf_add(&path, base, baselen); |
02cb6753 | 1627 | |
cf7c6148 | 1628 | if (open_cached_dir(&cdir, dir, untracked, &path, check_only)) |
1528d247 RS |
1629 | goto out; |
1630 | ||
0dcb8d7f NTND |
1631 | if (untracked) |
1632 | untracked->check_only = !!check_only; | |
1633 | ||
cf7c6148 | 1634 | while (!read_cached_dir(&cdir)) { |
defd7c7b | 1635 | /* check how the file or directory should be treated */ |
cf7c6148 | 1636 | state = treat_path(dir, untracked, &cdir, &path, baselen, simplify); |
0dcb8d7f | 1637 | |
defd7c7b KB |
1638 | if (state > dir_state) |
1639 | dir_state = state; | |
1640 | ||
1641 | /* recurse into subdir if instructed by treat_path */ | |
1642 | if (state == path_recurse) { | |
0dcb8d7f NTND |
1643 | struct untracked_cache_dir *ud; |
1644 | ud = lookup_untracked(dir->untracked, untracked, | |
1645 | path.buf + baselen, | |
1646 | path.len - baselen); | |
1647 | subdir_state = | |
1648 | read_directory_recursive(dir, path.buf, path.len, | |
1649 | ud, check_only, simplify); | |
defd7c7b KB |
1650 | if (subdir_state > dir_state) |
1651 | dir_state = subdir_state; | |
1652 | } | |
1653 | ||
1654 | if (check_only) { | |
1655 | /* abort early if maximum state has been reached */ | |
0dcb8d7f | 1656 | if (dir_state == path_untracked) { |
91a2288b | 1657 | if (cdir.fdir) |
0dcb8d7f | 1658 | add_untracked(untracked, path.buf + baselen); |
defd7c7b | 1659 | break; |
0dcb8d7f | 1660 | } |
defd7c7b | 1661 | /* skip the dir_add_* part */ |
02cb6753 | 1662 | continue; |
453ec4bd | 1663 | } |
defd7c7b KB |
1664 | |
1665 | /* add the path to the appropriate result list */ | |
1666 | switch (state) { | |
1667 | case path_excluded: | |
1668 | if (dir->flags & DIR_SHOW_IGNORED) | |
1669 | dir_add_name(dir, path.buf, path.len); | |
0aaf62b6 KB |
1670 | else if ((dir->flags & DIR_SHOW_IGNORED_TOO) || |
1671 | ((dir->flags & DIR_COLLECT_IGNORED) && | |
1672 | exclude_matches_pathspec(path.buf, path.len, | |
1673 | simplify))) | |
1674 | dir_add_ignored(dir, path.buf, path.len); | |
defd7c7b KB |
1675 | break; |
1676 | ||
1677 | case path_untracked: | |
0dcb8d7f NTND |
1678 | if (dir->flags & DIR_SHOW_IGNORED) |
1679 | break; | |
1680 | dir_add_name(dir, path.buf, path.len); | |
91a2288b | 1681 | if (cdir.fdir) |
0dcb8d7f | 1682 | add_untracked(untracked, path.buf + baselen); |
1528d247 | 1683 | break; |
defd7c7b KB |
1684 | |
1685 | default: | |
1686 | break; | |
1687 | } | |
453ec4bd | 1688 | } |
cf7c6148 | 1689 | close_cached_dir(&cdir); |
1528d247 | 1690 | out: |
bef36921 | 1691 | strbuf_release(&path); |
453ec4bd | 1692 | |
defd7c7b | 1693 | return dir_state; |
453ec4bd LT |
1694 | } |
1695 | ||
1696 | static int cmp_name(const void *p1, const void *p2) | |
1697 | { | |
1698 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; | |
1699 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; | |
1700 | ||
ccdd4a0f | 1701 | return name_compare(e1->name, e1->len, e2->name, e2->len); |
453ec4bd LT |
1702 | } |
1703 | ||
9fc42d60 LT |
1704 | static struct path_simplify *create_simplify(const char **pathspec) |
1705 | { | |
1706 | int nr, alloc = 0; | |
1707 | struct path_simplify *simplify = NULL; | |
1708 | ||
1709 | if (!pathspec) | |
1710 | return NULL; | |
1711 | ||
1712 | for (nr = 0 ; ; nr++) { | |
1713 | const char *match; | |
9af49f82 | 1714 | ALLOC_GROW(simplify, nr + 1, alloc); |
9fc42d60 LT |
1715 | match = *pathspec++; |
1716 | if (!match) | |
1717 | break; | |
1718 | simplify[nr].path = match; | |
1719 | simplify[nr].len = simple_length(match); | |
1720 | } | |
1721 | simplify[nr].path = NULL; | |
1722 | simplify[nr].len = 0; | |
1723 | return simplify; | |
1724 | } | |
1725 | ||
1726 | static void free_simplify(struct path_simplify *simplify) | |
1727 | { | |
8e0f7003 | 1728 | free(simplify); |
9fc42d60 LT |
1729 | } |
1730 | ||
48ffef96 JH |
1731 | static int treat_leading_path(struct dir_struct *dir, |
1732 | const char *path, int len, | |
1733 | const struct path_simplify *simplify) | |
1734 | { | |
49dc2cc2 RS |
1735 | struct strbuf sb = STRBUF_INIT; |
1736 | int baselen, rc = 0; | |
48ffef96 | 1737 | const char *cp; |
be8a84c5 | 1738 | int old_flags = dir->flags; |
48ffef96 JH |
1739 | |
1740 | while (len && path[len - 1] == '/') | |
1741 | len--; | |
1742 | if (!len) | |
1743 | return 1; | |
1744 | baselen = 0; | |
be8a84c5 | 1745 | dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES; |
48ffef96 JH |
1746 | while (1) { |
1747 | cp = path + baselen + !!baselen; | |
1748 | cp = memchr(cp, '/', path + len - cp); | |
1749 | if (!cp) | |
1750 | baselen = len; | |
1751 | else | |
1752 | baselen = cp - path; | |
49dc2cc2 RS |
1753 | strbuf_setlen(&sb, 0); |
1754 | strbuf_add(&sb, path, baselen); | |
1755 | if (!is_directory(sb.buf)) | |
1756 | break; | |
1757 | if (simplify_away(sb.buf, sb.len, simplify)) | |
1758 | break; | |
0dcb8d7f | 1759 | if (treat_one_path(dir, NULL, &sb, simplify, |
defd7c7b | 1760 | DT_DIR, NULL) == path_none) |
49dc2cc2 RS |
1761 | break; /* do not recurse into it */ |
1762 | if (len <= baselen) { | |
1763 | rc = 1; | |
1764 | break; /* finished checking */ | |
1765 | } | |
48ffef96 | 1766 | } |
49dc2cc2 | 1767 | strbuf_release(&sb); |
be8a84c5 | 1768 | dir->flags = old_flags; |
49dc2cc2 | 1769 | return rc; |
48ffef96 JH |
1770 | } |
1771 | ||
ccad261f NTND |
1772 | static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir, |
1773 | int base_len, | |
1774 | const struct pathspec *pathspec) | |
1775 | { | |
1776 | struct untracked_cache_dir *root; | |
1777 | ||
1778 | if (!dir->untracked) | |
1779 | return NULL; | |
1780 | ||
1781 | /* | |
1782 | * We only support $GIT_DIR/info/exclude and core.excludesfile | |
1783 | * as the global ignore rule files. Any other additions | |
1784 | * (e.g. from command line) invalidate the cache. This | |
1785 | * condition also catches running setup_standard_excludes() | |
1786 | * before setting dir->untracked! | |
1787 | */ | |
1788 | if (dir->unmanaged_exclude_files) | |
1789 | return NULL; | |
1790 | ||
1791 | /* | |
1792 | * Optimize for the main use case only: whole-tree git | |
1793 | * status. More work involved in treat_leading_path() if we | |
1794 | * use cache on just a subset of the worktree. pathspec | |
1795 | * support could make the matter even worse. | |
1796 | */ | |
1797 | if (base_len || (pathspec && pathspec->nr)) | |
1798 | return NULL; | |
1799 | ||
1800 | /* Different set of flags may produce different results */ | |
1801 | if (dir->flags != dir->untracked->dir_flags || | |
1802 | /* | |
1803 | * See treat_directory(), case index_nonexistent. Without | |
1804 | * this flag, we may need to also cache .git file content | |
1805 | * for the resolve_gitlink_ref() call, which we don't. | |
1806 | */ | |
1807 | !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) || | |
1808 | /* We don't support collecting ignore files */ | |
1809 | (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO | | |
1810 | DIR_COLLECT_IGNORED))) | |
1811 | return NULL; | |
1812 | ||
1813 | /* | |
1814 | * If we use .gitignore in the cache and now you change it to | |
1815 | * .gitexclude, everything will go wrong. | |
1816 | */ | |
1817 | if (dir->exclude_per_dir != dir->untracked->exclude_per_dir && | |
1818 | strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir)) | |
1819 | return NULL; | |
1820 | ||
1821 | /* | |
1822 | * EXC_CMDL is not considered in the cache. If people set it, | |
1823 | * skip the cache. | |
1824 | */ | |
1825 | if (dir->exclude_list_group[EXC_CMDL].nr) | |
1826 | return NULL; | |
1827 | ||
1828 | if (!dir->untracked->root) { | |
1829 | const int len = sizeof(*dir->untracked->root); | |
1830 | dir->untracked->root = xmalloc(len); | |
1831 | memset(dir->untracked->root, 0, len); | |
1832 | } | |
1833 | ||
1834 | /* Validate $GIT_DIR/info/exclude and core.excludesfile */ | |
1835 | root = dir->untracked->root; | |
1836 | if (hashcmp(dir->ss_info_exclude.sha1, | |
1837 | dir->untracked->ss_info_exclude.sha1)) { | |
1838 | invalidate_gitignore(dir->untracked, root); | |
1839 | dir->untracked->ss_info_exclude = dir->ss_info_exclude; | |
1840 | } | |
1841 | if (hashcmp(dir->ss_excludes_file.sha1, | |
1842 | dir->untracked->ss_excludes_file.sha1)) { | |
1843 | invalidate_gitignore(dir->untracked, root); | |
1844 | dir->untracked->ss_excludes_file = dir->ss_excludes_file; | |
1845 | } | |
1846 | return root; | |
1847 | } | |
1848 | ||
7327d3d1 | 1849 | int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec) |
9fc42d60 | 1850 | { |
725b0605 | 1851 | struct path_simplify *simplify; |
ccad261f | 1852 | struct untracked_cache_dir *untracked; |
b4189aa8 | 1853 | |
7327d3d1 NTND |
1854 | /* |
1855 | * Check out create_simplify() | |
1856 | */ | |
1857 | if (pathspec) | |
5c6933d2 NTND |
1858 | GUARD_PATHSPEC(pathspec, |
1859 | PATHSPEC_FROMTOP | | |
1860 | PATHSPEC_MAXDEPTH | | |
bd30c2e4 | 1861 | PATHSPEC_LITERAL | |
93d93537 | 1862 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
1863 | PATHSPEC_ICASE | |
1864 | PATHSPEC_EXCLUDE); | |
7327d3d1 | 1865 | |
dba2e203 | 1866 | if (has_symlink_leading_path(path, len)) |
725b0605 JH |
1867 | return dir->nr; |
1868 | ||
ef79b1f8 NTND |
1869 | /* |
1870 | * exclude patterns are treated like positive ones in | |
1871 | * create_simplify. Usually exclude patterns should be a | |
1872 | * subset of positive ones, which has no impacts on | |
1873 | * create_simplify(). | |
1874 | */ | |
b3920bbd | 1875 | simplify = create_simplify(pathspec ? pathspec->_raw : NULL); |
ccad261f NTND |
1876 | untracked = validate_untracked_cache(dir, len, pathspec); |
1877 | if (!untracked) | |
1878 | /* | |
1879 | * make sure untracked cache code path is disabled, | |
1880 | * e.g. prep_exclude() | |
1881 | */ | |
1882 | dir->untracked = NULL; | |
48ffef96 | 1883 | if (!len || treat_leading_path(dir, path, len, simplify)) |
ccad261f | 1884 | read_directory_recursive(dir, path, len, untracked, 0, simplify); |
9fc42d60 | 1885 | free_simplify(simplify); |
453ec4bd | 1886 | qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); |
2abd31b0 | 1887 | qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name); |
453ec4bd LT |
1888 | return dir->nr; |
1889 | } | |
c91f0d92 | 1890 | |
686a4a06 | 1891 | int file_exists(const char *f) |
c91f0d92 | 1892 | { |
686a4a06 | 1893 | struct stat sb; |
a50f9fc5 | 1894 | return lstat(f, &sb) == 0; |
c91f0d92 | 1895 | } |
e6636747 JS |
1896 | |
1897 | /* | |
9b125da4 NTND |
1898 | * Given two normalized paths (a trailing slash is ok), if subdir is |
1899 | * outside dir, return -1. Otherwise return the offset in subdir that | |
1900 | * can be used as relative path to dir. | |
e6636747 | 1901 | */ |
9b125da4 | 1902 | int dir_inside_of(const char *subdir, const char *dir) |
e6636747 | 1903 | { |
9b125da4 | 1904 | int offset = 0; |
e6636747 | 1905 | |
9b125da4 | 1906 | assert(dir && subdir && *dir && *subdir); |
e6636747 | 1907 | |
9b125da4 | 1908 | while (*dir && *subdir && *dir == *subdir) { |
e6636747 | 1909 | dir++; |
9b125da4 NTND |
1910 | subdir++; |
1911 | offset++; | |
490544b1 | 1912 | } |
9b125da4 NTND |
1913 | |
1914 | /* hel[p]/me vs hel[l]/yeah */ | |
1915 | if (*dir && *subdir) | |
1916 | return -1; | |
1917 | ||
1918 | if (!*subdir) | |
1919 | return !*dir ? offset : -1; /* same dir */ | |
1920 | ||
1921 | /* foo/[b]ar vs foo/[] */ | |
1922 | if (is_dir_sep(dir[-1])) | |
1923 | return is_dir_sep(subdir[-1]) ? offset : -1; | |
1924 | ||
1925 | /* foo[/]bar vs foo[] */ | |
1926 | return is_dir_sep(*subdir) ? offset + 1 : -1; | |
e6636747 JS |
1927 | } |
1928 | ||
1929 | int is_inside_dir(const char *dir) | |
1930 | { | |
56b9f6e7 RS |
1931 | char *cwd; |
1932 | int rc; | |
1933 | ||
b892913d NTND |
1934 | if (!dir) |
1935 | return 0; | |
56b9f6e7 RS |
1936 | |
1937 | cwd = xgetcwd(); | |
1938 | rc = (dir_inside_of(cwd, dir) >= 0); | |
1939 | free(cwd); | |
1940 | return rc; | |
e6636747 | 1941 | } |
7155b727 | 1942 | |
55892d23 AP |
1943 | int is_empty_dir(const char *path) |
1944 | { | |
1945 | DIR *dir = opendir(path); | |
1946 | struct dirent *e; | |
1947 | int ret = 1; | |
1948 | ||
1949 | if (!dir) | |
1950 | return 0; | |
1951 | ||
1952 | while ((e = readdir(dir)) != NULL) | |
1953 | if (!is_dot_or_dotdot(e->d_name)) { | |
1954 | ret = 0; | |
1955 | break; | |
1956 | } | |
1957 | ||
1958 | closedir(dir); | |
1959 | return ret; | |
1960 | } | |
1961 | ||
ae2f203e | 1962 | static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) |
7155b727 | 1963 | { |
a0f4afbe | 1964 | DIR *dir; |
7155b727 | 1965 | struct dirent *e; |
ae2f203e | 1966 | int ret = 0, original_len = path->len, len, kept_down = 0; |
a0f4afbe | 1967 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
c844a803 | 1968 | int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL); |
a0f4afbe | 1969 | unsigned char submodule_head[20]; |
7155b727 | 1970 | |
a0f4afbe | 1971 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
ae2f203e | 1972 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { |
a0f4afbe | 1973 | /* Do not descend and nuke a nested git work tree. */ |
ae2f203e JH |
1974 | if (kept_up) |
1975 | *kept_up = 1; | |
a0f4afbe | 1976 | return 0; |
ae2f203e | 1977 | } |
a0f4afbe | 1978 | |
ae2f203e | 1979 | flag &= ~REMOVE_DIR_KEEP_TOPLEVEL; |
a0f4afbe | 1980 | dir = opendir(path->buf); |
c844a803 | 1981 | if (!dir) { |
863808cd MH |
1982 | if (errno == ENOENT) |
1983 | return keep_toplevel ? -1 : 0; | |
1984 | else if (errno == EACCES && !keep_toplevel) | |
ecb2c282 MH |
1985 | /* |
1986 | * An empty dir could be removable even if it | |
1987 | * is unreadable: | |
1988 | */ | |
c844a803 JH |
1989 | return rmdir(path->buf); |
1990 | else | |
1991 | return -1; | |
1992 | } | |
7155b727 JS |
1993 | if (path->buf[original_len - 1] != '/') |
1994 | strbuf_addch(path, '/'); | |
1995 | ||
1996 | len = path->len; | |
1997 | while ((e = readdir(dir)) != NULL) { | |
1998 | struct stat st; | |
8ca12c0d AP |
1999 | if (is_dot_or_dotdot(e->d_name)) |
2000 | continue; | |
7155b727 JS |
2001 | |
2002 | strbuf_setlen(path, len); | |
2003 | strbuf_addstr(path, e->d_name); | |
863808cd MH |
2004 | if (lstat(path->buf, &st)) { |
2005 | if (errno == ENOENT) | |
2006 | /* | |
2007 | * file disappeared, which is what we | |
2008 | * wanted anyway | |
2009 | */ | |
2010 | continue; | |
2011 | /* fall thru */ | |
2012 | } else if (S_ISDIR(st.st_mode)) { | |
ae2f203e | 2013 | if (!remove_dir_recurse(path, flag, &kept_down)) |
7155b727 | 2014 | continue; /* happy */ |
863808cd MH |
2015 | } else if (!only_empty && |
2016 | (!unlink(path->buf) || errno == ENOENT)) { | |
7155b727 | 2017 | continue; /* happy, too */ |
863808cd | 2018 | } |
7155b727 JS |
2019 | |
2020 | /* path too long, stat fails, or non-directory still exists */ | |
2021 | ret = -1; | |
2022 | break; | |
2023 | } | |
2024 | closedir(dir); | |
2025 | ||
2026 | strbuf_setlen(path, original_len); | |
ae2f203e | 2027 | if (!ret && !keep_toplevel && !kept_down) |
863808cd | 2028 | ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; |
ae2f203e JH |
2029 | else if (kept_up) |
2030 | /* | |
2031 | * report the uplevel that it is not an error that we | |
2032 | * did not rmdir() our directory. | |
2033 | */ | |
2034 | *kept_up = !ret; | |
7155b727 JS |
2035 | return ret; |
2036 | } | |
039bc64e | 2037 | |
ae2f203e JH |
2038 | int remove_dir_recursively(struct strbuf *path, int flag) |
2039 | { | |
2040 | return remove_dir_recurse(path, flag, NULL); | |
2041 | } | |
2042 | ||
039bc64e JH |
2043 | void setup_standard_excludes(struct dir_struct *dir) |
2044 | { | |
2045 | const char *path; | |
dc79687e | 2046 | char *xdg_path; |
039bc64e JH |
2047 | |
2048 | dir->exclude_per_dir = ".gitignore"; | |
2049 | path = git_path("info/exclude"); | |
dc79687e HKNN |
2050 | if (!excludes_file) { |
2051 | home_config_paths(NULL, &xdg_path, "ignore"); | |
2052 | excludes_file = xdg_path; | |
2053 | } | |
4698c8fe | 2054 | if (!access_or_warn(path, R_OK, 0)) |
0dcb8d7f NTND |
2055 | add_excludes_from_file_1(dir, path, |
2056 | dir->untracked ? &dir->ss_info_exclude : NULL); | |
4698c8fe | 2057 | if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) |
0dcb8d7f NTND |
2058 | add_excludes_from_file_1(dir, excludes_file, |
2059 | dir->untracked ? &dir->ss_excludes_file : NULL); | |
039bc64e | 2060 | } |
4a92d1bf AR |
2061 | |
2062 | int remove_path(const char *name) | |
2063 | { | |
2064 | char *slash; | |
2065 | ||
9a6728d4 | 2066 | if (unlink(name) && errno != ENOENT && errno != ENOTDIR) |
4a92d1bf AR |
2067 | return -1; |
2068 | ||
2069 | slash = strrchr(name, '/'); | |
2070 | if (slash) { | |
2071 | char *dirs = xstrdup(name); | |
2072 | slash = dirs + (slash - name); | |
2073 | do { | |
2074 | *slash = '\0'; | |
3fc0d131 | 2075 | } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); |
4a92d1bf AR |
2076 | free(dirs); |
2077 | } | |
2078 | return 0; | |
2079 | } | |
2080 | ||
270be816 AS |
2081 | /* |
2082 | * Frees memory within dir which was allocated for exclude lists and | |
2083 | * the exclude_stack. Does not free dir itself. | |
2084 | */ | |
2085 | void clear_directory(struct dir_struct *dir) | |
2086 | { | |
2087 | int i, j; | |
2088 | struct exclude_list_group *group; | |
2089 | struct exclude_list *el; | |
2090 | struct exclude_stack *stk; | |
2091 | ||
2092 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { | |
2093 | group = &dir->exclude_list_group[i]; | |
2094 | for (j = 0; j < group->nr; j++) { | |
2095 | el = &group->el[j]; | |
2096 | if (i == EXC_DIRS) | |
2097 | free((char *)el->src); | |
2098 | clear_exclude_list(el); | |
2099 | } | |
2100 | free(group->el); | |
2101 | } | |
2102 | ||
2103 | stk = dir->exclude_stack; | |
2104 | while (stk) { | |
2105 | struct exclude_stack *prev = stk->prev; | |
2106 | free(stk); | |
2107 | stk = prev; | |
2108 | } | |
aceb9429 | 2109 | strbuf_release(&dir->basebuf); |
270be816 | 2110 | } |