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