]>
Commit | Line | Data |
---|---|---|
453ec4bd LT |
1 | /* |
2 | * This handles recursive filename detection with exclude | |
3 | * files, index knowledge etc.. | |
4 | * | |
5 | * Copyright (C) Linus Torvalds, 2005-2006 | |
6 | * Junio Hamano, 2005-2006 | |
7 | */ | |
453ec4bd LT |
8 | #include "cache.h" |
9 | #include "dir.h" | |
09595258 | 10 | #include "refs.h" |
453ec4bd | 11 | |
9fc42d60 LT |
12 | struct path_simplify { |
13 | int len; | |
14 | const char *path; | |
15 | }; | |
16 | ||
dba2e203 | 17 | static int read_directory_recursive(struct dir_struct *dir, const char *path, int len, |
09595258 | 18 | int check_only, const struct path_simplify *simplify); |
caa6b782 | 19 | static int get_dtype(struct dirent *de, const char *path, int len); |
09595258 | 20 | |
1d8842d9 | 21 | static int common_prefix(const char **pathspec) |
3c6a370b LT |
22 | { |
23 | const char *path, *slash, *next; | |
24 | int prefix; | |
25 | ||
26 | if (!pathspec) | |
27 | return 0; | |
28 | ||
29 | path = *pathspec; | |
30 | slash = strrchr(path, '/'); | |
31 | if (!slash) | |
32 | return 0; | |
33 | ||
42f9852f JH |
34 | /* |
35 | * The first 'prefix' characters of 'path' are common leading | |
36 | * path components among the pathspecs we have seen so far, | |
37 | * including the trailing slash. | |
38 | */ | |
3c6a370b LT |
39 | prefix = slash - path + 1; |
40 | while ((next = *++pathspec) != NULL) { | |
42f9852f JH |
41 | int len, last_matching_slash = -1; |
42 | for (len = 0; len < prefix && next[len] == path[len]; len++) | |
43 | if (next[len] == '/') | |
44 | last_matching_slash = len; | |
45 | if (len == prefix) | |
3c6a370b | 46 | continue; |
42f9852f JH |
47 | if (last_matching_slash < 0) |
48 | return 0; | |
49 | prefix = last_matching_slash + 1; | |
3c6a370b LT |
50 | } |
51 | return prefix; | |
52 | } | |
53 | ||
1d8842d9 LT |
54 | int fill_directory(struct dir_struct *dir, const char **pathspec) |
55 | { | |
dba2e203 LT |
56 | const char *path; |
57 | int len; | |
1d8842d9 LT |
58 | |
59 | /* | |
60 | * Calculate common prefix for the pathspec, and | |
61 | * use that to optimize the directory walk | |
62 | */ | |
dba2e203 | 63 | len = common_prefix(pathspec); |
1d8842d9 | 64 | path = ""; |
1d8842d9 | 65 | |
dba2e203 LT |
66 | if (len) |
67 | path = xmemdupz(*pathspec, len); | |
1d8842d9 LT |
68 | |
69 | /* Read the directory and prune it */ | |
dba2e203 LT |
70 | read_directory(dir, path, len, pathspec); |
71 | return len; | |
1d8842d9 LT |
72 | } |
73 | ||
e813d50e | 74 | /* |
2c5b0115 | 75 | * Does 'match' match the given name? |
e813d50e JH |
76 | * A match is found if |
77 | * | |
78 | * (1) the 'match' string is leading directory of 'name', or | |
79 | * (2) the 'match' string is a wildcard and matches 'name', or | |
80 | * (3) the 'match' string is exactly the same as 'name'. | |
81 | * | |
82 | * and the return value tells which case it was. | |
83 | * | |
84 | * It returns 0 when there is no match. | |
85 | */ | |
3c6a370b LT |
86 | static int match_one(const char *match, const char *name, int namelen) |
87 | { | |
88 | int matchlen; | |
89 | ||
90 | /* If the match was just the prefix, we matched */ | |
88ea8112 | 91 | if (!*match) |
e813d50e | 92 | return MATCHED_RECURSIVELY; |
3c6a370b | 93 | |
88ea8112 LT |
94 | for (;;) { |
95 | unsigned char c1 = *match; | |
96 | unsigned char c2 = *name; | |
8cc32992 | 97 | if (c1 == '\0' || is_glob_special(c1)) |
88ea8112 LT |
98 | break; |
99 | if (c1 != c2) | |
100 | return 0; | |
101 | match++; | |
102 | name++; | |
103 | namelen--; | |
104 | } | |
105 | ||
106 | ||
3c6a370b LT |
107 | /* |
108 | * If we don't match the matchstring exactly, | |
109 | * we need to match by fnmatch | |
110 | */ | |
88ea8112 | 111 | matchlen = strlen(match); |
3c6a370b | 112 | if (strncmp(match, name, matchlen)) |
e813d50e | 113 | return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0; |
3c6a370b | 114 | |
f2d0df71 | 115 | if (namelen == matchlen) |
e813d50e JH |
116 | return MATCHED_EXACTLY; |
117 | if (match[matchlen-1] == '/' || name[matchlen] == '/') | |
118 | return MATCHED_RECURSIVELY; | |
119 | return 0; | |
3c6a370b LT |
120 | } |
121 | ||
e813d50e JH |
122 | /* |
123 | * Given a name and a list of pathspecs, see if the name matches | |
124 | * any of the pathspecs. The caller is also interested in seeing | |
125 | * all pathspec matches some names it calls this function with | |
126 | * (otherwise the user could have mistyped the unmatched pathspec), | |
127 | * and a mark is left in seen[] array for pathspec element that | |
128 | * actually matched anything. | |
129 | */ | |
0b50922a CB |
130 | int match_pathspec(const char **pathspec, const char *name, int namelen, |
131 | int prefix, char *seen) | |
3c6a370b | 132 | { |
0b50922a CB |
133 | int i, retval = 0; |
134 | ||
135 | if (!pathspec) | |
136 | return 1; | |
3c6a370b LT |
137 | |
138 | name += prefix; | |
139 | namelen -= prefix; | |
140 | ||
0b50922a | 141 | for (i = 0; pathspec[i] != NULL; i++) { |
e813d50e | 142 | int how; |
0b50922a CB |
143 | const char *match = pathspec[i] + prefix; |
144 | if (seen && seen[i] == MATCHED_EXACTLY) | |
3c6a370b | 145 | continue; |
e813d50e JH |
146 | how = match_one(match, name, namelen); |
147 | if (how) { | |
148 | if (retval < how) | |
149 | retval = how; | |
0b50922a CB |
150 | if (seen && seen[i] < how) |
151 | seen[i] = how; | |
3c6a370b LT |
152 | } |
153 | } | |
154 | return retval; | |
155 | } | |
156 | ||
68492fc7 LK |
157 | static int no_wildcard(const char *string) |
158 | { | |
dd482eea | 159 | return string[strcspn(string, "*?[{\\")] == '\0'; |
68492fc7 LK |
160 | } |
161 | ||
453ec4bd LT |
162 | void add_exclude(const char *string, const char *base, |
163 | int baselen, struct exclude_list *which) | |
164 | { | |
d6b8fc30 JH |
165 | struct exclude *x; |
166 | size_t len; | |
167 | int to_exclude = 1; | |
168 | int flags = 0; | |
453ec4bd | 169 | |
68492fc7 | 170 | if (*string == '!') { |
d6b8fc30 | 171 | to_exclude = 0; |
68492fc7 LK |
172 | string++; |
173 | } | |
d6b8fc30 JH |
174 | len = strlen(string); |
175 | if (len && string[len - 1] == '/') { | |
176 | char *s; | |
177 | x = xmalloc(sizeof(*x) + len); | |
4b25d091 | 178 | s = (char *)(x+1); |
d6b8fc30 JH |
179 | memcpy(s, string, len - 1); |
180 | s[len - 1] = '\0'; | |
181 | string = s; | |
182 | x->pattern = s; | |
183 | flags = EXC_FLAG_MUSTBEDIR; | |
184 | } else { | |
185 | x = xmalloc(sizeof(*x)); | |
186 | x->pattern = string; | |
187 | } | |
188 | x->to_exclude = to_exclude; | |
68492fc7 | 189 | x->patternlen = strlen(string); |
453ec4bd LT |
190 | x->base = base; |
191 | x->baselen = baselen; | |
d6b8fc30 | 192 | x->flags = flags; |
68492fc7 LK |
193 | if (!strchr(string, '/')) |
194 | x->flags |= EXC_FLAG_NODIR; | |
195 | if (no_wildcard(string)) | |
196 | x->flags |= EXC_FLAG_NOWILDCARD; | |
197 | if (*string == '*' && no_wildcard(string+1)) | |
198 | x->flags |= EXC_FLAG_ENDSWITH; | |
686a4a06 | 199 | ALLOC_GROW(which->excludes, which->nr + 1, which->alloc); |
453ec4bd LT |
200 | which->excludes[which->nr++] = x; |
201 | } | |
202 | ||
c28b3d6e NTND |
203 | static void *read_skip_worktree_file_from_index(const char *path, size_t *size) |
204 | { | |
205 | int pos, len; | |
206 | unsigned long sz; | |
207 | enum object_type type; | |
208 | void *data; | |
209 | struct index_state *istate = &the_index; | |
210 | ||
211 | len = strlen(path); | |
212 | pos = index_name_pos(istate, path, len); | |
213 | if (pos < 0) | |
214 | return NULL; | |
215 | if (!ce_skip_worktree(istate->cache[pos])) | |
216 | return NULL; | |
217 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); | |
218 | if (!data || type != OBJ_BLOB) { | |
219 | free(data); | |
220 | return NULL; | |
221 | } | |
222 | *size = xsize_t(sz); | |
223 | return data; | |
224 | } | |
225 | ||
cb097534 NTND |
226 | int add_excludes_from_file_to_list(const char *fname, |
227 | const char *base, | |
228 | int baselen, | |
229 | char **buf_p, | |
230 | struct exclude_list *which, | |
231 | int check_index) | |
453ec4bd | 232 | { |
c470701a | 233 | struct stat st; |
453ec4bd | 234 | int fd, i; |
dc49cd76 | 235 | size_t size; |
453ec4bd LT |
236 | char *buf, *entry; |
237 | ||
238 | fd = open(fname, O_RDONLY); | |
c28b3d6e NTND |
239 | if (fd < 0 || fstat(fd, &st) < 0) { |
240 | if (0 <= fd) | |
241 | close(fd); | |
242 | if (!check_index || | |
243 | (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL) | |
244 | return -1; | |
45d76f17 NTND |
245 | if (size == 0) { |
246 | free(buf); | |
247 | return 0; | |
248 | } | |
249 | if (buf[size-1] != '\n') { | |
250 | buf = xrealloc(buf, size+1); | |
251 | buf[size++] = '\n'; | |
252 | } | |
453ec4bd | 253 | } |
c28b3d6e NTND |
254 | else { |
255 | size = xsize_t(st.st_size); | |
256 | if (size == 0) { | |
257 | close(fd); | |
258 | return 0; | |
259 | } | |
45d76f17 | 260 | buf = xmalloc(size+1); |
c28b3d6e | 261 | if (read_in_full(fd, buf, size) != size) { |
45d76f17 | 262 | free(buf); |
c28b3d6e NTND |
263 | close(fd); |
264 | return -1; | |
265 | } | |
45d76f17 | 266 | buf[size++] = '\n'; |
c28b3d6e | 267 | close(fd); |
6ba78238 | 268 | } |
453ec4bd | 269 | |
63d285c8 JH |
270 | if (buf_p) |
271 | *buf_p = buf; | |
453ec4bd | 272 | entry = buf; |
45d76f17 NTND |
273 | for (i = 0; i < size; i++) { |
274 | if (buf[i] == '\n') { | |
453ec4bd LT |
275 | if (entry != buf + i && entry[0] != '#') { |
276 | buf[i - (i && buf[i-1] == '\r')] = 0; | |
277 | add_exclude(entry, base, baselen, which); | |
278 | } | |
279 | entry = buf + i + 1; | |
280 | } | |
281 | } | |
282 | return 0; | |
453ec4bd LT |
283 | } |
284 | ||
285 | void add_excludes_from_file(struct dir_struct *dir, const char *fname) | |
286 | { | |
cb097534 NTND |
287 | if (add_excludes_from_file_to_list(fname, "", 0, NULL, |
288 | &dir->exclude_list[EXC_FILE], 0) < 0) | |
453ec4bd LT |
289 | die("cannot use %s as an exclude file", fname); |
290 | } | |
291 | ||
63d285c8 | 292 | static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) |
453ec4bd | 293 | { |
63d285c8 JH |
294 | struct exclude_list *el; |
295 | struct exclude_stack *stk = NULL; | |
296 | int current; | |
297 | ||
298 | if ((!dir->exclude_per_dir) || | |
299 | (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX)) | |
300 | return; /* too long a path -- ignore */ | |
301 | ||
302 | /* Pop the ones that are not the prefix of the path being checked. */ | |
303 | el = &dir->exclude_list[EXC_DIRS]; | |
304 | while ((stk = dir->exclude_stack) != NULL) { | |
305 | if (stk->baselen <= baselen && | |
306 | !strncmp(dir->basebuf, base, stk->baselen)) | |
307 | break; | |
308 | dir->exclude_stack = stk->prev; | |
309 | while (stk->exclude_ix < el->nr) | |
310 | free(el->excludes[--el->nr]); | |
311 | free(stk->filebuf); | |
312 | free(stk); | |
453ec4bd | 313 | } |
453ec4bd | 314 | |
63d285c8 JH |
315 | /* Read from the parent directories and push them down. */ |
316 | current = stk ? stk->baselen : -1; | |
317 | while (current < baselen) { | |
318 | struct exclude_stack *stk = xcalloc(1, sizeof(*stk)); | |
319 | const char *cp; | |
453ec4bd | 320 | |
63d285c8 JH |
321 | if (current < 0) { |
322 | cp = base; | |
323 | current = 0; | |
324 | } | |
325 | else { | |
326 | cp = strchr(base + current + 1, '/'); | |
327 | if (!cp) | |
328 | die("oops in prep_exclude"); | |
329 | cp++; | |
330 | } | |
331 | stk->prev = dir->exclude_stack; | |
332 | stk->baselen = cp - base; | |
333 | stk->exclude_ix = el->nr; | |
334 | memcpy(dir->basebuf + current, base + current, | |
335 | stk->baselen - current); | |
336 | strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); | |
cb097534 NTND |
337 | add_excludes_from_file_to_list(dir->basebuf, |
338 | dir->basebuf, stk->baselen, | |
339 | &stk->filebuf, el, 1); | |
63d285c8 JH |
340 | dir->exclude_stack = stk; |
341 | current = stk->baselen; | |
342 | } | |
343 | dir->basebuf[baselen] = '\0'; | |
453ec4bd LT |
344 | } |
345 | ||
2c5b0115 | 346 | /* Scan the list and let the last match determine the fate. |
453ec4bd LT |
347 | * Return 1 for exclude, 0 for include and -1 for undecided. |
348 | */ | |
cb097534 NTND |
349 | int excluded_from_list(const char *pathname, |
350 | int pathlen, const char *basename, int *dtype, | |
351 | struct exclude_list *el) | |
453ec4bd LT |
352 | { |
353 | int i; | |
354 | ||
355 | if (el->nr) { | |
356 | for (i = el->nr - 1; 0 <= i; i--) { | |
357 | struct exclude *x = el->excludes[i]; | |
358 | const char *exclude = x->pattern; | |
68492fc7 | 359 | int to_exclude = x->to_exclude; |
453ec4bd | 360 | |
6831a88a | 361 | if (x->flags & EXC_FLAG_MUSTBEDIR) { |
c84de707 NTND |
362 | if (!dtype) { |
363 | if (!prefixcmp(pathname, exclude)) | |
364 | return to_exclude; | |
365 | else | |
366 | continue; | |
367 | } | |
6831a88a | 368 | if (*dtype == DT_UNKNOWN) |
caa6b782 | 369 | *dtype = get_dtype(NULL, pathname, pathlen); |
6831a88a JH |
370 | if (*dtype != DT_DIR) |
371 | continue; | |
372 | } | |
d6b8fc30 | 373 | |
68492fc7 | 374 | if (x->flags & EXC_FLAG_NODIR) { |
453ec4bd | 375 | /* match basename */ |
68492fc7 LK |
376 | if (x->flags & EXC_FLAG_NOWILDCARD) { |
377 | if (!strcmp(exclude, basename)) | |
378 | return to_exclude; | |
379 | } else if (x->flags & EXC_FLAG_ENDSWITH) { | |
380 | if (x->patternlen - 1 <= pathlen && | |
381 | !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1)) | |
382 | return to_exclude; | |
383 | } else { | |
384 | if (fnmatch(exclude, basename, 0) == 0) | |
385 | return to_exclude; | |
386 | } | |
453ec4bd LT |
387 | } |
388 | else { | |
389 | /* match with FNM_PATHNAME: | |
390 | * exclude has base (baselen long) implicitly | |
391 | * in front of it. | |
392 | */ | |
393 | int baselen = x->baselen; | |
394 | if (*exclude == '/') | |
395 | exclude++; | |
396 | ||
397 | if (pathlen < baselen || | |
398 | (baselen && pathname[baselen-1] != '/') || | |
399 | strncmp(pathname, x->base, baselen)) | |
400 | continue; | |
401 | ||
68492fc7 LK |
402 | if (x->flags & EXC_FLAG_NOWILDCARD) { |
403 | if (!strcmp(exclude, pathname + baselen)) | |
404 | return to_exclude; | |
405 | } else { | |
406 | if (fnmatch(exclude, pathname+baselen, | |
407 | FNM_PATHNAME) == 0) | |
408 | return to_exclude; | |
409 | } | |
453ec4bd LT |
410 | } |
411 | } | |
412 | } | |
413 | return -1; /* undecided */ | |
414 | } | |
415 | ||
6831a88a | 416 | int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p) |
453ec4bd LT |
417 | { |
418 | int pathlen = strlen(pathname); | |
419 | int st; | |
68492fc7 LK |
420 | const char *basename = strrchr(pathname, '/'); |
421 | basename = (basename) ? basename+1 : pathname; | |
453ec4bd | 422 | |
63d285c8 | 423 | prep_exclude(dir, pathname, basename-pathname); |
453ec4bd | 424 | for (st = EXC_CMDL; st <= EXC_FILE; st++) { |
cb097534 NTND |
425 | switch (excluded_from_list(pathname, pathlen, basename, |
426 | dtype_p, &dir->exclude_list[st])) { | |
453ec4bd LT |
427 | case 0: |
428 | return 0; | |
429 | case 1: | |
430 | return 1; | |
431 | } | |
432 | } | |
433 | return 0; | |
434 | } | |
435 | ||
f3fa1838 JH |
436 | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
437 | { | |
453ec4bd LT |
438 | struct dir_entry *ent; |
439 | ||
453ec4bd LT |
440 | ent = xmalloc(sizeof(*ent) + len + 1); |
441 | ent->len = len; | |
442 | memcpy(ent->name, pathname, len); | |
443 | ent->name[len] = 0; | |
4d06f8ac | 444 | return ent; |
453ec4bd LT |
445 | } |
446 | ||
159b3212 | 447 | static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) |
6815e569 | 448 | { |
0a9b88b7 | 449 | if (cache_name_exists(pathname, len, ignore_case)) |
6815e569 JK |
450 | return NULL; |
451 | ||
25fd2f7a | 452 | ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
6815e569 JK |
453 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
454 | } | |
455 | ||
108da0db | 456 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len) |
2abd31b0 | 457 | { |
6e4f981f | 458 | if (!cache_name_is_other(pathname, len)) |
2abd31b0 JK |
459 | return NULL; |
460 | ||
25fd2f7a | 461 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
2abd31b0 JK |
462 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
463 | } | |
464 | ||
09595258 LT |
465 | enum exist_status { |
466 | index_nonexistent = 0, | |
467 | index_directory, | |
4b05548f | 468 | index_gitdir |
09595258 LT |
469 | }; |
470 | ||
471 | /* | |
472 | * The index sorts alphabetically by entry name, which | |
473 | * means that a gitlink sorts as '\0' at the end, while | |
474 | * a directory (which is defined not as an entry, but as | |
475 | * the files it contains) will sort with the '/' at the | |
476 | * end. | |
477 | */ | |
478 | static enum exist_status directory_exists_in_index(const char *dirname, int len) | |
453ec4bd LT |
479 | { |
480 | int pos = cache_name_pos(dirname, len); | |
09595258 LT |
481 | if (pos < 0) |
482 | pos = -pos-1; | |
483 | while (pos < active_nr) { | |
484 | struct cache_entry *ce = active_cache[pos++]; | |
485 | unsigned char endchar; | |
486 | ||
487 | if (strncmp(ce->name, dirname, len)) | |
488 | break; | |
489 | endchar = ce->name[len]; | |
490 | if (endchar > '/') | |
491 | break; | |
492 | if (endchar == '/') | |
493 | return index_directory; | |
7a51ed66 | 494 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
09595258 LT |
495 | return index_gitdir; |
496 | } | |
497 | return index_nonexistent; | |
498 | } | |
499 | ||
500 | /* | |
501 | * When we find a directory when traversing the filesystem, we | |
502 | * have three distinct cases: | |
503 | * | |
504 | * - ignore it | |
505 | * - see it as a directory | |
506 | * - recurse into it | |
507 | * | |
508 | * and which one we choose depends on a combination of existing | |
509 | * git index contents and the flags passed into the directory | |
510 | * traversal routine. | |
511 | * | |
512 | * Case 1: If we *already* have entries in the index under that | |
513 | * directory name, we always recurse into the directory to see | |
514 | * all the files. | |
515 | * | |
516 | * Case 2: If we *already* have that directory name as a gitlink, | |
517 | * we always continue to see it as a gitlink, regardless of whether | |
518 | * there is an actual git directory there or not (it might not | |
519 | * be checked out as a subproject!) | |
520 | * | |
521 | * Case 3: if we didn't have it in the index previously, we | |
522 | * have a few sub-cases: | |
523 | * | |
524 | * (a) if "show_other_directories" is true, we show it as | |
525 | * just a directory, unless "hide_empty_directories" is | |
526 | * also true and the directory is empty, in which case | |
527 | * we just ignore it entirely. | |
528 | * (b) if it looks like a git directory, and we don't have | |
302b9282 | 529 | * 'no_gitlinks' set we treat it as a gitlink, and show it |
09595258 LT |
530 | * as a directory. |
531 | * (c) otherwise, we recurse into it. | |
532 | */ | |
533 | enum directory_treatment { | |
534 | show_directory, | |
535 | ignore_directory, | |
4b05548f | 536 | recurse_into_directory |
09595258 LT |
537 | }; |
538 | ||
539 | static enum directory_treatment treat_directory(struct dir_struct *dir, | |
540 | const char *dirname, int len, | |
541 | const struct path_simplify *simplify) | |
542 | { | |
543 | /* The "len-1" is to strip the final '/' */ | |
544 | switch (directory_exists_in_index(dirname, len-1)) { | |
545 | case index_directory: | |
546 | return recurse_into_directory; | |
547 | ||
548 | case index_gitdir: | |
7c4c97c0 | 549 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
ab22aed3 | 550 | return ignore_directory; |
09595258 LT |
551 | return show_directory; |
552 | ||
553 | case index_nonexistent: | |
7c4c97c0 | 554 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
09595258 | 555 | break; |
7c4c97c0 | 556 | if (!(dir->flags & DIR_NO_GITLINKS)) { |
09595258 LT |
557 | unsigned char sha1[20]; |
558 | if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0) | |
559 | return show_directory; | |
560 | } | |
561 | return recurse_into_directory; | |
562 | } | |
563 | ||
564 | /* This is the "show_other_directories" case */ | |
7c4c97c0 | 565 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
09595258 | 566 | return show_directory; |
dba2e203 | 567 | if (!read_directory_recursive(dir, dirname, len, 1, simplify)) |
09595258 LT |
568 | return ignore_directory; |
569 | return show_directory; | |
453ec4bd LT |
570 | } |
571 | ||
9fc42d60 LT |
572 | /* |
573 | * This is an inexact early pruning of any recursive directory | |
574 | * reading - if the path cannot possibly be in the pathspec, | |
575 | * return true, and we'll skip it early. | |
576 | */ | |
577 | static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify) | |
578 | { | |
579 | if (simplify) { | |
580 | for (;;) { | |
581 | const char *match = simplify->path; | |
582 | int len = simplify->len; | |
583 | ||
584 | if (!match) | |
585 | break; | |
586 | if (len > pathlen) | |
587 | len = pathlen; | |
588 | if (!memcmp(path, match, len)) | |
589 | return 0; | |
590 | simplify++; | |
591 | } | |
592 | return 1; | |
593 | } | |
594 | return 0; | |
595 | } | |
596 | ||
29209cbe JK |
597 | /* |
598 | * This function tells us whether an excluded path matches a | |
599 | * list of "interesting" pathspecs. That is, whether a path matched | |
600 | * by any of the pathspecs could possibly be ignored by excluding | |
601 | * the specified path. This can happen if: | |
602 | * | |
603 | * 1. the path is mentioned explicitly in the pathspec | |
604 | * | |
605 | * 2. the path is a directory prefix of some element in the | |
606 | * pathspec | |
607 | */ | |
608 | static int exclude_matches_pathspec(const char *path, int len, | |
609 | const struct path_simplify *simplify) | |
e96980ef JK |
610 | { |
611 | if (simplify) { | |
612 | for (; simplify->path; simplify++) { | |
613 | if (len == simplify->len | |
614 | && !memcmp(path, simplify->path, len)) | |
615 | return 1; | |
29209cbe JK |
616 | if (len < simplify->len |
617 | && simplify->path[len] == '/' | |
618 | && !memcmp(path, simplify->path, len)) | |
619 | return 1; | |
e96980ef JK |
620 | } |
621 | } | |
622 | return 0; | |
623 | } | |
624 | ||
443e061a LT |
625 | static int get_index_dtype(const char *path, int len) |
626 | { | |
627 | int pos; | |
628 | struct cache_entry *ce; | |
629 | ||
630 | ce = cache_name_exists(path, len, 0); | |
631 | if (ce) { | |
632 | if (!ce_uptodate(ce)) | |
633 | return DT_UNKNOWN; | |
634 | if (S_ISGITLINK(ce->ce_mode)) | |
635 | return DT_DIR; | |
636 | /* | |
637 | * Nobody actually cares about the | |
638 | * difference between DT_LNK and DT_REG | |
639 | */ | |
640 | return DT_REG; | |
641 | } | |
642 | ||
643 | /* Try to look it up as a directory */ | |
644 | pos = cache_name_pos(path, len); | |
645 | if (pos >= 0) | |
646 | return DT_UNKNOWN; | |
647 | pos = -pos-1; | |
648 | while (pos < active_nr) { | |
649 | ce = active_cache[pos++]; | |
650 | if (strncmp(ce->name, path, len)) | |
651 | break; | |
652 | if (ce->name[len] > '/') | |
653 | break; | |
654 | if (ce->name[len] < '/') | |
655 | continue; | |
656 | if (!ce_uptodate(ce)) | |
657 | break; /* continue? */ | |
658 | return DT_DIR; | |
659 | } | |
660 | return DT_UNKNOWN; | |
661 | } | |
662 | ||
caa6b782 | 663 | static int get_dtype(struct dirent *de, const char *path, int len) |
07134421 | 664 | { |
6831a88a | 665 | int dtype = de ? DTYPE(de) : DT_UNKNOWN; |
07134421 LT |
666 | struct stat st; |
667 | ||
668 | if (dtype != DT_UNKNOWN) | |
669 | return dtype; | |
443e061a LT |
670 | dtype = get_index_dtype(path, len); |
671 | if (dtype != DT_UNKNOWN) | |
672 | return dtype; | |
673 | if (lstat(path, &st)) | |
07134421 LT |
674 | return dtype; |
675 | if (S_ISREG(st.st_mode)) | |
676 | return DT_REG; | |
677 | if (S_ISDIR(st.st_mode)) | |
678 | return DT_DIR; | |
679 | if (S_ISLNK(st.st_mode)) | |
680 | return DT_LNK; | |
681 | return dtype; | |
682 | } | |
683 | ||
53cc5356 JH |
684 | enum path_treatment { |
685 | path_ignored, | |
686 | path_handled, | |
4b05548f | 687 | path_recurse |
53cc5356 JH |
688 | }; |
689 | ||
16e2cfa9 JH |
690 | static enum path_treatment treat_one_path(struct dir_struct *dir, |
691 | char *path, int *len, | |
692 | const struct path_simplify *simplify, | |
693 | int dtype, struct dirent *de) | |
53cc5356 | 694 | { |
16e2cfa9 | 695 | int exclude = excluded(dir, path, &dtype); |
53cc5356 | 696 | if (exclude && (dir->flags & DIR_COLLECT_IGNORED) |
29209cbe | 697 | && exclude_matches_pathspec(path, *len, simplify)) |
53cc5356 JH |
698 | dir_add_ignored(dir, path, *len); |
699 | ||
700 | /* | |
701 | * Excluded? If we don't explicitly want to show | |
702 | * ignored files, ignore it | |
703 | */ | |
704 | if (exclude && !(dir->flags & DIR_SHOW_IGNORED)) | |
705 | return path_ignored; | |
706 | ||
707 | if (dtype == DT_UNKNOWN) | |
708 | dtype = get_dtype(de, path, *len); | |
709 | ||
710 | /* | |
711 | * Do we want to see just the ignored files? | |
712 | * We still need to recurse into directories, | |
713 | * even if we don't ignore them, since the | |
714 | * directory may contain files that we do.. | |
715 | */ | |
716 | if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) { | |
717 | if (dtype != DT_DIR) | |
718 | return path_ignored; | |
719 | } | |
720 | ||
721 | switch (dtype) { | |
722 | default: | |
723 | return path_ignored; | |
724 | case DT_DIR: | |
725 | memcpy(path + *len, "/", 2); | |
726 | (*len)++; | |
727 | switch (treat_directory(dir, path, *len, simplify)) { | |
728 | case show_directory: | |
729 | if (exclude != !!(dir->flags | |
730 | & DIR_SHOW_IGNORED)) | |
731 | return path_ignored; | |
732 | break; | |
733 | case recurse_into_directory: | |
734 | return path_recurse; | |
735 | case ignore_directory: | |
736 | return path_ignored; | |
737 | } | |
738 | break; | |
739 | case DT_REG: | |
740 | case DT_LNK: | |
741 | break; | |
742 | } | |
743 | return path_handled; | |
744 | } | |
745 | ||
16e2cfa9 JH |
746 | static enum path_treatment treat_path(struct dir_struct *dir, |
747 | struct dirent *de, | |
748 | char *path, int path_max, | |
749 | int baselen, | |
750 | const struct path_simplify *simplify, | |
751 | int *len) | |
752 | { | |
753 | int dtype; | |
754 | ||
755 | if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git")) | |
756 | return path_ignored; | |
757 | *len = strlen(de->d_name); | |
758 | /* Ignore overly long pathnames! */ | |
759 | if (*len + baselen + 8 > path_max) | |
760 | return path_ignored; | |
761 | memcpy(path + baselen, de->d_name, *len + 1); | |
762 | *len += baselen; | |
763 | if (simplify_away(path, *len, simplify)) | |
764 | return path_ignored; | |
765 | ||
766 | dtype = DTYPE(de); | |
767 | return treat_one_path(dir, path, len, simplify, dtype, de); | |
768 | } | |
769 | ||
453ec4bd LT |
770 | /* |
771 | * Read a directory tree. We currently ignore anything but | |
772 | * directories, regular files and symlinks. That's because git | |
773 | * doesn't handle them at all yet. Maybe that will change some | |
774 | * day. | |
775 | * | |
776 | * Also, we ignore the name ".git" (even if it is not a directory). | |
777 | * That likely will not change. | |
778 | */ | |
53cc5356 JH |
779 | static int read_directory_recursive(struct dir_struct *dir, |
780 | const char *base, int baselen, | |
781 | int check_only, | |
782 | const struct path_simplify *simplify) | |
453ec4bd | 783 | { |
dba2e203 | 784 | DIR *fdir = opendir(*base ? base : "."); |
453ec4bd LT |
785 | int contents = 0; |
786 | ||
787 | if (fdir) { | |
453ec4bd | 788 | struct dirent *de; |
dba2e203 LT |
789 | char path[PATH_MAX + 1]; |
790 | memcpy(path, base, baselen); | |
453ec4bd | 791 | |
453ec4bd | 792 | while ((de = readdir(fdir)) != NULL) { |
53cc5356 JH |
793 | int len; |
794 | switch (treat_path(dir, de, path, sizeof(path), | |
795 | baselen, simplify, &len)) { | |
796 | case path_recurse: | |
797 | contents += read_directory_recursive | |
798 | (dir, path, len, 0, simplify); | |
453ec4bd | 799 | continue; |
53cc5356 | 800 | case path_ignored: |
5d5cea67 | 801 | continue; |
53cc5356 | 802 | case path_handled: |
453ec4bd LT |
803 | break; |
804 | } | |
453ec4bd | 805 | contents++; |
07ccbff8 JS |
806 | if (check_only) |
807 | goto exit_early; | |
808 | else | |
dba2e203 | 809 | dir_add_name(dir, path, len); |
453ec4bd | 810 | } |
07ccbff8 | 811 | exit_early: |
453ec4bd | 812 | closedir(fdir); |
453ec4bd LT |
813 | } |
814 | ||
815 | return contents; | |
816 | } | |
817 | ||
818 | static int cmp_name(const void *p1, const void *p2) | |
819 | { | |
820 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; | |
821 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; | |
822 | ||
823 | return cache_name_compare(e1->name, e1->len, | |
824 | e2->name, e2->len); | |
825 | } | |
826 | ||
9fc42d60 LT |
827 | /* |
828 | * Return the length of the "simple" part of a path match limiter. | |
829 | */ | |
830 | static int simple_length(const char *match) | |
453ec4bd | 831 | { |
9fc42d60 LT |
832 | int len = -1; |
833 | ||
834 | for (;;) { | |
835 | unsigned char c = *match++; | |
836 | len++; | |
8cc32992 | 837 | if (c == '\0' || is_glob_special(c)) |
9fc42d60 LT |
838 | return len; |
839 | } | |
840 | } | |
841 | ||
842 | static struct path_simplify *create_simplify(const char **pathspec) | |
843 | { | |
844 | int nr, alloc = 0; | |
845 | struct path_simplify *simplify = NULL; | |
846 | ||
847 | if (!pathspec) | |
848 | return NULL; | |
849 | ||
850 | for (nr = 0 ; ; nr++) { | |
851 | const char *match; | |
852 | if (nr >= alloc) { | |
853 | alloc = alloc_nr(alloc); | |
854 | simplify = xrealloc(simplify, alloc * sizeof(*simplify)); | |
855 | } | |
856 | match = *pathspec++; | |
857 | if (!match) | |
858 | break; | |
859 | simplify[nr].path = match; | |
860 | simplify[nr].len = simple_length(match); | |
861 | } | |
862 | simplify[nr].path = NULL; | |
863 | simplify[nr].len = 0; | |
864 | return simplify; | |
865 | } | |
866 | ||
867 | static void free_simplify(struct path_simplify *simplify) | |
868 | { | |
8e0f7003 | 869 | free(simplify); |
9fc42d60 LT |
870 | } |
871 | ||
48ffef96 JH |
872 | static int treat_leading_path(struct dir_struct *dir, |
873 | const char *path, int len, | |
874 | const struct path_simplify *simplify) | |
875 | { | |
876 | char pathbuf[PATH_MAX]; | |
877 | int baselen, blen; | |
878 | const char *cp; | |
879 | ||
880 | while (len && path[len - 1] == '/') | |
881 | len--; | |
882 | if (!len) | |
883 | return 1; | |
884 | baselen = 0; | |
885 | while (1) { | |
886 | cp = path + baselen + !!baselen; | |
887 | cp = memchr(cp, '/', path + len - cp); | |
888 | if (!cp) | |
889 | baselen = len; | |
890 | else | |
891 | baselen = cp - path; | |
892 | memcpy(pathbuf, path, baselen); | |
893 | pathbuf[baselen] = '\0'; | |
894 | if (!is_directory(pathbuf)) | |
895 | return 0; | |
896 | if (simplify_away(pathbuf, baselen, simplify)) | |
897 | return 0; | |
898 | blen = baselen; | |
899 | if (treat_one_path(dir, pathbuf, &blen, simplify, | |
900 | DT_DIR, NULL) == path_ignored) | |
901 | return 0; /* do not recurse into it */ | |
902 | if (len <= baselen) | |
903 | return 1; /* finished checking */ | |
904 | } | |
905 | } | |
906 | ||
dba2e203 | 907 | int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec) |
9fc42d60 | 908 | { |
725b0605 | 909 | struct path_simplify *simplify; |
b4189aa8 | 910 | |
dba2e203 | 911 | if (has_symlink_leading_path(path, len)) |
725b0605 JH |
912 | return dir->nr; |
913 | ||
914 | simplify = create_simplify(pathspec); | |
48ffef96 JH |
915 | if (!len || treat_leading_path(dir, path, len, simplify)) |
916 | read_directory_recursive(dir, path, len, 0, simplify); | |
9fc42d60 | 917 | free_simplify(simplify); |
453ec4bd | 918 | qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name); |
2abd31b0 | 919 | qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name); |
453ec4bd LT |
920 | return dir->nr; |
921 | } | |
c91f0d92 | 922 | |
686a4a06 | 923 | int file_exists(const char *f) |
c91f0d92 | 924 | { |
686a4a06 | 925 | struct stat sb; |
a50f9fc5 | 926 | return lstat(f, &sb) == 0; |
c91f0d92 | 927 | } |
e6636747 JS |
928 | |
929 | /* | |
930 | * get_relative_cwd() gets the prefix of the current working directory | |
931 | * relative to 'dir'. If we are not inside 'dir', it returns NULL. | |
420acb31 JS |
932 | * |
933 | * As a convenience, it also returns NULL if 'dir' is already NULL. The | |
934 | * reason for this behaviour is that it is natural for functions returning | |
935 | * directory names to return NULL to say "this directory does not exist" | |
936 | * or "this directory is invalid". These cases are usually handled the | |
937 | * same as if the cwd is not inside 'dir' at all, so get_relative_cwd() | |
938 | * returns NULL for both of them. | |
939 | * | |
940 | * Most notably, get_relative_cwd(buffer, size, get_git_work_tree()) | |
941 | * unifies the handling of "outside work tree" with "no work tree at all". | |
e6636747 JS |
942 | */ |
943 | char *get_relative_cwd(char *buffer, int size, const char *dir) | |
944 | { | |
945 | char *cwd = buffer; | |
946 | ||
e6636747 JS |
947 | if (!dir) |
948 | return NULL; | |
949 | if (!getcwd(buffer, size)) | |
d824cbba | 950 | die_errno("can't find the current directory"); |
e6636747 JS |
951 | |
952 | if (!is_absolute_path(dir)) | |
953 | dir = make_absolute_path(dir); | |
954 | ||
955 | while (*dir && *dir == *cwd) { | |
956 | dir++; | |
957 | cwd++; | |
958 | } | |
959 | if (*dir) | |
960 | return NULL; | |
490544b1 CB |
961 | switch (*cwd) { |
962 | case '\0': | |
963 | return cwd; | |
964 | case '/': | |
e6636747 | 965 | return cwd + 1; |
490544b1 CB |
966 | default: |
967 | return NULL; | |
968 | } | |
e6636747 JS |
969 | } |
970 | ||
971 | int is_inside_dir(const char *dir) | |
972 | { | |
973 | char buffer[PATH_MAX]; | |
974 | return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL; | |
975 | } | |
7155b727 | 976 | |
55892d23 AP |
977 | int is_empty_dir(const char *path) |
978 | { | |
979 | DIR *dir = opendir(path); | |
980 | struct dirent *e; | |
981 | int ret = 1; | |
982 | ||
983 | if (!dir) | |
984 | return 0; | |
985 | ||
986 | while ((e = readdir(dir)) != NULL) | |
987 | if (!is_dot_or_dotdot(e->d_name)) { | |
988 | ret = 0; | |
989 | break; | |
990 | } | |
991 | ||
992 | closedir(dir); | |
993 | return ret; | |
994 | } | |
995 | ||
a0f4afbe | 996 | int remove_dir_recursively(struct strbuf *path, int flag) |
7155b727 | 997 | { |
a0f4afbe | 998 | DIR *dir; |
7155b727 JS |
999 | struct dirent *e; |
1000 | int ret = 0, original_len = path->len, len; | |
a0f4afbe JH |
1001 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
1002 | unsigned char submodule_head[20]; | |
7155b727 | 1003 | |
a0f4afbe JH |
1004 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
1005 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) | |
1006 | /* Do not descend and nuke a nested git work tree. */ | |
1007 | return 0; | |
1008 | ||
1009 | dir = opendir(path->buf); | |
7155b727 JS |
1010 | if (!dir) |
1011 | return -1; | |
1012 | if (path->buf[original_len - 1] != '/') | |
1013 | strbuf_addch(path, '/'); | |
1014 | ||
1015 | len = path->len; | |
1016 | while ((e = readdir(dir)) != NULL) { | |
1017 | struct stat st; | |
8ca12c0d AP |
1018 | if (is_dot_or_dotdot(e->d_name)) |
1019 | continue; | |
7155b727 JS |
1020 | |
1021 | strbuf_setlen(path, len); | |
1022 | strbuf_addstr(path, e->d_name); | |
1023 | if (lstat(path->buf, &st)) | |
1024 | ; /* fall thru */ | |
1025 | else if (S_ISDIR(st.st_mode)) { | |
1026 | if (!remove_dir_recursively(path, only_empty)) | |
1027 | continue; /* happy */ | |
1028 | } else if (!only_empty && !unlink(path->buf)) | |
1029 | continue; /* happy, too */ | |
1030 | ||
1031 | /* path too long, stat fails, or non-directory still exists */ | |
1032 | ret = -1; | |
1033 | break; | |
1034 | } | |
1035 | closedir(dir); | |
1036 | ||
1037 | strbuf_setlen(path, original_len); | |
1038 | if (!ret) | |
1039 | ret = rmdir(path->buf); | |
1040 | return ret; | |
1041 | } | |
039bc64e JH |
1042 | |
1043 | void setup_standard_excludes(struct dir_struct *dir) | |
1044 | { | |
1045 | const char *path; | |
1046 | ||
1047 | dir->exclude_per_dir = ".gitignore"; | |
1048 | path = git_path("info/exclude"); | |
1049 | if (!access(path, R_OK)) | |
1050 | add_excludes_from_file(dir, path); | |
1051 | if (excludes_file && !access(excludes_file, R_OK)) | |
1052 | add_excludes_from_file(dir, excludes_file); | |
1053 | } | |
4a92d1bf AR |
1054 | |
1055 | int remove_path(const char *name) | |
1056 | { | |
1057 | char *slash; | |
1058 | ||
1059 | if (unlink(name) && errno != ENOENT) | |
1060 | return -1; | |
1061 | ||
1062 | slash = strrchr(name, '/'); | |
1063 | if (slash) { | |
1064 | char *dirs = xstrdup(name); | |
1065 | slash = dirs + (slash - name); | |
1066 | do { | |
1067 | *slash = '\0'; | |
3fc0d131 | 1068 | } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); |
4a92d1bf AR |
1069 | free(dirs); |
1070 | } | |
1071 | return 0; | |
1072 | } | |
1073 |