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