Line | Count | Source (jump to first uncovered line) |
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 | | */ |
8 | | |
9 | | #define USE_THE_REPOSITORY_VARIABLE |
10 | | |
11 | | #include "git-compat-util.h" |
12 | | #include "abspath.h" |
13 | | #include "config.h" |
14 | | #include "convert.h" |
15 | | #include "dir.h" |
16 | | #include "environment.h" |
17 | | #include "gettext.h" |
18 | | #include "name-hash.h" |
19 | | #include "object-file.h" |
20 | | #include "object-store-ll.h" |
21 | | #include "path.h" |
22 | | #include "refs.h" |
23 | | #include "wildmatch.h" |
24 | | #include "pathspec.h" |
25 | | #include "utf8.h" |
26 | | #include "varint.h" |
27 | | #include "ewah/ewok.h" |
28 | | #include "fsmonitor-ll.h" |
29 | | #include "read-cache-ll.h" |
30 | | #include "setup.h" |
31 | | #include "sparse-index.h" |
32 | | #include "submodule-config.h" |
33 | | #include "symlinks.h" |
34 | | #include "trace2.h" |
35 | | #include "tree.h" |
36 | | #include "hex.h" |
37 | | |
38 | | /* |
39 | | * The maximum size of a pattern/exclude file. If the file exceeds this size |
40 | | * we will ignore it. |
41 | | */ |
42 | 0 | #define PATTERN_MAX_FILE_SIZE (100 * 1024 * 1024) |
43 | | |
44 | | /* |
45 | | * Tells read_directory_recursive how a file or directory should be treated. |
46 | | * Values are ordered by significance, e.g. if a directory contains both |
47 | | * excluded and untracked files, it is listed as untracked because |
48 | | * path_untracked > path_excluded. |
49 | | */ |
50 | | enum path_treatment { |
51 | | path_none = 0, |
52 | | path_recurse, |
53 | | path_excluded, |
54 | | path_untracked |
55 | | }; |
56 | | |
57 | | /* |
58 | | * Support data structure for our opendir/readdir/closedir wrappers |
59 | | */ |
60 | | struct cached_dir { |
61 | | DIR *fdir; |
62 | | struct untracked_cache_dir *untracked; |
63 | | int nr_files; |
64 | | int nr_dirs; |
65 | | |
66 | | const char *d_name; |
67 | | int d_type; |
68 | | const char *file; |
69 | | struct untracked_cache_dir *ucd; |
70 | | }; |
71 | | |
72 | | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
73 | | struct index_state *istate, const char *path, int len, |
74 | | struct untracked_cache_dir *untracked, |
75 | | int check_only, int stop_at_first_file, const struct pathspec *pathspec); |
76 | | static int resolve_dtype(int dtype, struct index_state *istate, |
77 | | const char *path, int len); |
78 | | struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp) |
79 | 0 | { |
80 | 0 | struct dirent *e; |
81 | |
|
82 | 0 | while ((e = readdir(dirp)) != NULL) { |
83 | 0 | if (!is_dot_or_dotdot(e->d_name)) |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | return e; |
87 | 0 | } |
88 | | |
89 | | int count_slashes(const char *s) |
90 | 0 | { |
91 | 0 | int cnt = 0; |
92 | 0 | while (*s) |
93 | 0 | if (*s++ == '/') |
94 | 0 | cnt++; |
95 | 0 | return cnt; |
96 | 0 | } |
97 | | |
98 | | int git_fspathcmp(const char *a, const char *b) |
99 | 0 | { |
100 | 0 | return ignore_case ? strcasecmp(a, b) : strcmp(a, b); |
101 | 0 | } |
102 | | |
103 | | int fspatheq(const char *a, const char *b) |
104 | 0 | { |
105 | 0 | return !fspathcmp(a, b); |
106 | 0 | } |
107 | | |
108 | | int git_fspathncmp(const char *a, const char *b, size_t count) |
109 | 0 | { |
110 | 0 | return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); |
111 | 0 | } |
112 | | |
113 | | int paths_collide(const char *a, const char *b) |
114 | 0 | { |
115 | 0 | size_t len_a = strlen(a), len_b = strlen(b); |
116 | |
|
117 | 0 | if (len_a == len_b) |
118 | 0 | return fspatheq(a, b); |
119 | | |
120 | 0 | if (len_a < len_b) |
121 | 0 | return is_dir_sep(b[len_a]) && !fspathncmp(a, b, len_a); |
122 | 0 | return is_dir_sep(a[len_b]) && !fspathncmp(a, b, len_b); |
123 | 0 | } |
124 | | |
125 | | unsigned int fspathhash(const char *str) |
126 | 0 | { |
127 | 0 | return ignore_case ? strihash(str) : strhash(str); |
128 | 0 | } |
129 | | |
130 | | int git_fnmatch(const struct pathspec_item *item, |
131 | | const char *pattern, const char *string, |
132 | | int prefix) |
133 | 0 | { |
134 | 0 | if (prefix > 0) { |
135 | 0 | if (ps_strncmp(item, pattern, string, prefix)) |
136 | 0 | return WM_NOMATCH; |
137 | 0 | pattern += prefix; |
138 | 0 | string += prefix; |
139 | 0 | } |
140 | 0 | if (item->flags & PATHSPEC_ONESTAR) { |
141 | 0 | int pattern_len = strlen(++pattern); |
142 | 0 | int string_len = strlen(string); |
143 | 0 | return string_len < pattern_len || |
144 | 0 | ps_strcmp(item, pattern, |
145 | 0 | string + string_len - pattern_len); |
146 | 0 | } |
147 | 0 | if (item->magic & PATHSPEC_GLOB) |
148 | 0 | return wildmatch(pattern, string, |
149 | 0 | WM_PATHNAME | |
150 | 0 | (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0)); |
151 | 0 | else |
152 | | /* wildmatch has not learned no FNM_PATHNAME mode yet */ |
153 | 0 | return wildmatch(pattern, string, |
154 | 0 | item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0); |
155 | 0 | } |
156 | | |
157 | | static int fnmatch_icase_mem(const char *pattern, int patternlen, |
158 | | const char *string, int stringlen, |
159 | | int flags) |
160 | 0 | { |
161 | 0 | int match_status; |
162 | 0 | struct strbuf pat_buf = STRBUF_INIT; |
163 | 0 | struct strbuf str_buf = STRBUF_INIT; |
164 | 0 | const char *use_pat = pattern; |
165 | 0 | const char *use_str = string; |
166 | |
|
167 | 0 | if (pattern[patternlen]) { |
168 | 0 | strbuf_add(&pat_buf, pattern, patternlen); |
169 | 0 | use_pat = pat_buf.buf; |
170 | 0 | } |
171 | 0 | if (string[stringlen]) { |
172 | 0 | strbuf_add(&str_buf, string, stringlen); |
173 | 0 | use_str = str_buf.buf; |
174 | 0 | } |
175 | |
|
176 | 0 | if (ignore_case) |
177 | 0 | flags |= WM_CASEFOLD; |
178 | 0 | match_status = wildmatch(use_pat, use_str, flags); |
179 | |
|
180 | 0 | strbuf_release(&pat_buf); |
181 | 0 | strbuf_release(&str_buf); |
182 | |
|
183 | 0 | return match_status; |
184 | 0 | } |
185 | | |
186 | | static size_t common_prefix_len(const struct pathspec *pathspec) |
187 | 0 | { |
188 | 0 | int n; |
189 | 0 | size_t max = 0; |
190 | | |
191 | | /* |
192 | | * ":(icase)path" is treated as a pathspec full of |
193 | | * wildcard. In other words, only prefix is considered common |
194 | | * prefix. If the pathspec is abc/foo abc/bar, running in |
195 | | * subdir xyz, the common prefix is still xyz, not xyz/abc as |
196 | | * in non-:(icase). |
197 | | */ |
198 | 0 | GUARD_PATHSPEC(pathspec, |
199 | 0 | PATHSPEC_FROMTOP | |
200 | 0 | PATHSPEC_MAXDEPTH | |
201 | 0 | PATHSPEC_LITERAL | |
202 | 0 | PATHSPEC_GLOB | |
203 | 0 | PATHSPEC_ICASE | |
204 | 0 | PATHSPEC_EXCLUDE | |
205 | 0 | PATHSPEC_ATTR); |
206 | | |
207 | 0 | for (n = 0; n < pathspec->nr; n++) { |
208 | 0 | size_t i = 0, len = 0, item_len; |
209 | 0 | if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) |
210 | 0 | continue; |
211 | 0 | if (pathspec->items[n].magic & PATHSPEC_ICASE) |
212 | 0 | item_len = pathspec->items[n].prefix; |
213 | 0 | else |
214 | 0 | item_len = pathspec->items[n].nowildcard_len; |
215 | 0 | while (i < item_len && (n == 0 || i < max)) { |
216 | 0 | char c = pathspec->items[n].match[i]; |
217 | 0 | if (c != pathspec->items[0].match[i]) |
218 | 0 | break; |
219 | 0 | if (c == '/') |
220 | 0 | len = i + 1; |
221 | 0 | i++; |
222 | 0 | } |
223 | 0 | if (n == 0 || len < max) { |
224 | 0 | max = len; |
225 | 0 | if (!max) |
226 | 0 | break; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | return max; |
230 | 0 | } |
231 | | |
232 | | /* |
233 | | * Returns a copy of the longest leading path common among all |
234 | | * pathspecs. |
235 | | */ |
236 | | char *common_prefix(const struct pathspec *pathspec) |
237 | 0 | { |
238 | 0 | unsigned long len = common_prefix_len(pathspec); |
239 | |
|
240 | 0 | return len ? xmemdupz(pathspec->items[0].match, len) : NULL; |
241 | 0 | } |
242 | | |
243 | | int fill_directory(struct dir_struct *dir, |
244 | | struct index_state *istate, |
245 | | const struct pathspec *pathspec) |
246 | 0 | { |
247 | 0 | const char *prefix; |
248 | 0 | size_t prefix_len; |
249 | |
|
250 | 0 | unsigned exclusive_flags = DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO; |
251 | 0 | if ((dir->flags & exclusive_flags) == exclusive_flags) |
252 | 0 | BUG("DIR_SHOW_IGNORED and DIR_SHOW_IGNORED_TOO are exclusive"); |
253 | | |
254 | | /* |
255 | | * Calculate common prefix for the pathspec, and |
256 | | * use that to optimize the directory walk |
257 | | */ |
258 | 0 | prefix_len = common_prefix_len(pathspec); |
259 | 0 | prefix = prefix_len ? pathspec->items[0].match : ""; |
260 | | |
261 | | /* Read the directory and prune it */ |
262 | 0 | read_directory(dir, istate, prefix, prefix_len, pathspec); |
263 | |
|
264 | 0 | return prefix_len; |
265 | 0 | } |
266 | | |
267 | | int within_depth(const char *name, int namelen, |
268 | | int depth, int max_depth) |
269 | 0 | { |
270 | 0 | const char *cp = name, *cpe = name + namelen; |
271 | |
|
272 | 0 | while (cp < cpe) { |
273 | 0 | if (*cp++ != '/') |
274 | 0 | continue; |
275 | 0 | depth++; |
276 | 0 | if (depth > max_depth) |
277 | 0 | return 0; |
278 | 0 | } |
279 | 0 | return 1; |
280 | 0 | } |
281 | | |
282 | | /* |
283 | | * Read the contents of the blob with the given OID into a buffer. |
284 | | * Append a trailing LF to the end if the last line doesn't have one. |
285 | | * |
286 | | * Returns: |
287 | | * -1 when the OID is invalid or unknown or does not refer to a blob. |
288 | | * 0 when the blob is empty. |
289 | | * 1 along with { data, size } of the (possibly augmented) buffer |
290 | | * when successful. |
291 | | * |
292 | | * Optionally updates the given oid_stat with the given OID (when valid). |
293 | | */ |
294 | | static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat, |
295 | | size_t *size_out, char **data_out) |
296 | 0 | { |
297 | 0 | enum object_type type; |
298 | 0 | unsigned long sz; |
299 | 0 | char *data; |
300 | |
|
301 | 0 | *size_out = 0; |
302 | 0 | *data_out = NULL; |
303 | |
|
304 | 0 | data = repo_read_object_file(the_repository, oid, &type, &sz); |
305 | 0 | if (!data || type != OBJ_BLOB) { |
306 | 0 | free(data); |
307 | 0 | return -1; |
308 | 0 | } |
309 | | |
310 | 0 | if (oid_stat) { |
311 | 0 | memset(&oid_stat->stat, 0, sizeof(oid_stat->stat)); |
312 | 0 | oidcpy(&oid_stat->oid, oid); |
313 | 0 | } |
314 | |
|
315 | 0 | if (sz == 0) { |
316 | 0 | free(data); |
317 | 0 | return 0; |
318 | 0 | } |
319 | | |
320 | 0 | if (data[sz - 1] != '\n') { |
321 | 0 | data = xrealloc(data, st_add(sz, 1)); |
322 | 0 | data[sz++] = '\n'; |
323 | 0 | } |
324 | |
|
325 | 0 | *size_out = xsize_t(sz); |
326 | 0 | *data_out = data; |
327 | |
|
328 | 0 | return 1; |
329 | 0 | } |
330 | | |
331 | 0 | #define DO_MATCH_EXCLUDE (1<<0) |
332 | 0 | #define DO_MATCH_DIRECTORY (1<<1) |
333 | 0 | #define DO_MATCH_LEADING_PATHSPEC (1<<2) |
334 | | |
335 | | /* |
336 | | * Does the given pathspec match the given name? A match is found if |
337 | | * |
338 | | * (1) the pathspec string is leading directory of 'name' ("RECURSIVELY"), or |
339 | | * (2) the pathspec string has a leading part matching 'name' ("LEADING"), or |
340 | | * (3) the pathspec string is a wildcard and matches 'name' ("WILDCARD"), or |
341 | | * (4) the pathspec string is exactly the same as 'name' ("EXACT"). |
342 | | * |
343 | | * Return value tells which case it was (1-4), or 0 when there is no match. |
344 | | * |
345 | | * It may be instructive to look at a small table of concrete examples |
346 | | * to understand the differences between 1, 2, and 4: |
347 | | * |
348 | | * Pathspecs |
349 | | * | a/b | a/b/ | a/b/c |
350 | | * ------+-----------+-----------+------------ |
351 | | * a/b | EXACT | EXACT[1] | LEADING[2] |
352 | | * Names a/b/ | RECURSIVE | EXACT | LEADING[2] |
353 | | * a/b/c | RECURSIVE | RECURSIVE | EXACT |
354 | | * |
355 | | * [1] Only if DO_MATCH_DIRECTORY is passed; otherwise, this is NOT a match. |
356 | | * [2] Only if DO_MATCH_LEADING_PATHSPEC is passed; otherwise, not a match. |
357 | | */ |
358 | | static int match_pathspec_item(struct index_state *istate, |
359 | | const struct pathspec_item *item, int prefix, |
360 | | const char *name, int namelen, unsigned flags) |
361 | 0 | { |
362 | | /* name/namelen has prefix cut off by caller */ |
363 | 0 | const char *match = item->match + prefix; |
364 | 0 | int matchlen = item->len - prefix; |
365 | | |
366 | | /* |
367 | | * The normal call pattern is: |
368 | | * 1. prefix = common_prefix_len(ps); |
369 | | * 2. prune something, or fill_directory |
370 | | * 3. match_pathspec() |
371 | | * |
372 | | * 'prefix' at #1 may be shorter than the command's prefix and |
373 | | * it's ok for #2 to match extra files. Those extras will be |
374 | | * trimmed at #3. |
375 | | * |
376 | | * Suppose the pathspec is 'foo' and '../bar' running from |
377 | | * subdir 'xyz'. The common prefix at #1 will be empty, thanks |
378 | | * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The |
379 | | * user does not want XYZ/foo, only the "foo" part should be |
380 | | * case-insensitive. We need to filter out XYZ/foo here. In |
381 | | * other words, we do not trust the caller on comparing the |
382 | | * prefix part when :(icase) is involved. We do exact |
383 | | * comparison ourselves. |
384 | | * |
385 | | * Normally the caller (common_prefix_len() in fact) does |
386 | | * _exact_ matching on name[-prefix+1..-1] and we do not need |
387 | | * to check that part. Be defensive and check it anyway, in |
388 | | * case common_prefix_len is changed, or a new caller is |
389 | | * introduced that does not use common_prefix_len. |
390 | | * |
391 | | * If the penalty turns out too high when prefix is really |
392 | | * long, maybe change it to |
393 | | * strncmp(match, name, item->prefix - prefix) |
394 | | */ |
395 | 0 | if (item->prefix && (item->magic & PATHSPEC_ICASE) && |
396 | 0 | strncmp(item->match, name - prefix, item->prefix)) |
397 | 0 | return 0; |
398 | | |
399 | 0 | if (item->attr_match_nr && |
400 | 0 | !match_pathspec_attrs(istate, name - prefix, namelen + prefix, item)) |
401 | 0 | return 0; |
402 | | |
403 | | /* If the match was just the prefix, we matched */ |
404 | 0 | if (!*match) |
405 | 0 | return MATCHED_RECURSIVELY; |
406 | | |
407 | 0 | if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { |
408 | 0 | if (matchlen == namelen) |
409 | 0 | return MATCHED_EXACTLY; |
410 | | |
411 | 0 | if (match[matchlen-1] == '/' || name[matchlen] == '/') |
412 | 0 | return MATCHED_RECURSIVELY; |
413 | 0 | } else if ((flags & DO_MATCH_DIRECTORY) && |
414 | 0 | match[matchlen - 1] == '/' && |
415 | 0 | namelen == matchlen - 1 && |
416 | 0 | !ps_strncmp(item, match, name, namelen)) |
417 | 0 | return MATCHED_EXACTLY; |
418 | | |
419 | 0 | if (item->nowildcard_len < item->len && |
420 | 0 | !git_fnmatch(item, match, name, |
421 | 0 | item->nowildcard_len - prefix)) |
422 | 0 | return MATCHED_FNMATCH; |
423 | | |
424 | | /* Perform checks to see if "name" is a leading string of the pathspec */ |
425 | 0 | if ( (flags & DO_MATCH_LEADING_PATHSPEC) && |
426 | 0 | !(flags & DO_MATCH_EXCLUDE)) { |
427 | | /* name is a literal prefix of the pathspec */ |
428 | 0 | int offset = name[namelen-1] == '/' ? 1 : 0; |
429 | 0 | if ((namelen < matchlen) && |
430 | 0 | (match[namelen-offset] == '/') && |
431 | 0 | !ps_strncmp(item, match, name, namelen)) |
432 | 0 | return MATCHED_RECURSIVELY_LEADING_PATHSPEC; |
433 | | |
434 | | /* name doesn't match up to the first wild character */ |
435 | 0 | if (item->nowildcard_len < item->len && |
436 | 0 | ps_strncmp(item, match, name, |
437 | 0 | item->nowildcard_len - prefix)) |
438 | 0 | return 0; |
439 | | |
440 | | /* |
441 | | * name has no wildcard, and it didn't match as a leading |
442 | | * pathspec so return. |
443 | | */ |
444 | 0 | if (item->nowildcard_len == item->len) |
445 | 0 | return 0; |
446 | | |
447 | | /* |
448 | | * Here is where we would perform a wildmatch to check if |
449 | | * "name" can be matched as a directory (or a prefix) against |
450 | | * the pathspec. Since wildmatch doesn't have this capability |
451 | | * at the present we have to punt and say that it is a match, |
452 | | * potentially returning a false positive |
453 | | * The submodules themselves will be able to perform more |
454 | | * accurate matching to determine if the pathspec matches. |
455 | | */ |
456 | 0 | return MATCHED_RECURSIVELY_LEADING_PATHSPEC; |
457 | 0 | } |
458 | | |
459 | 0 | return 0; |
460 | 0 | } |
461 | | |
462 | | /* |
463 | | * do_match_pathspec() is meant to ONLY be called by |
464 | | * match_pathspec_with_flags(); calling it directly risks pathspecs |
465 | | * like ':!unwanted_path' being ignored. |
466 | | * |
467 | | * Given a name and a list of pathspecs, returns the nature of the |
468 | | * closest (i.e. most specific) match of the name to any of the |
469 | | * pathspecs. |
470 | | * |
471 | | * The caller typically calls this multiple times with the same |
472 | | * pathspec and seen[] array but with different name/namelen |
473 | | * (e.g. entries from the index) and is interested in seeing if and |
474 | | * how each pathspec matches all the names it calls this function |
475 | | * with. A mark is left in the seen[] array for each pathspec element |
476 | | * indicating the closest type of match that element achieved, so if |
477 | | * seen[n] remains zero after multiple invocations, that means the nth |
478 | | * pathspec did not match any names, which could indicate that the |
479 | | * user mistyped the nth pathspec. |
480 | | */ |
481 | | static int do_match_pathspec(struct index_state *istate, |
482 | | const struct pathspec *ps, |
483 | | const char *name, int namelen, |
484 | | int prefix, char *seen, |
485 | | unsigned flags) |
486 | 0 | { |
487 | 0 | int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; |
488 | |
|
489 | 0 | GUARD_PATHSPEC(ps, |
490 | 0 | PATHSPEC_FROMTOP | |
491 | 0 | PATHSPEC_MAXDEPTH | |
492 | 0 | PATHSPEC_LITERAL | |
493 | 0 | PATHSPEC_GLOB | |
494 | 0 | PATHSPEC_ICASE | |
495 | 0 | PATHSPEC_EXCLUDE | |
496 | 0 | PATHSPEC_ATTR); |
497 | | |
498 | 0 | if (!ps->nr) { |
499 | 0 | if (!ps->recursive || |
500 | 0 | !(ps->magic & PATHSPEC_MAXDEPTH) || |
501 | 0 | ps->max_depth == -1) |
502 | 0 | return MATCHED_RECURSIVELY; |
503 | | |
504 | 0 | if (within_depth(name, namelen, 0, ps->max_depth)) |
505 | 0 | return MATCHED_EXACTLY; |
506 | 0 | else |
507 | 0 | return 0; |
508 | 0 | } |
509 | | |
510 | 0 | name += prefix; |
511 | 0 | namelen -= prefix; |
512 | |
|
513 | 0 | for (i = ps->nr - 1; i >= 0; i--) { |
514 | 0 | int how; |
515 | |
|
516 | 0 | if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || |
517 | 0 | ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) |
518 | 0 | continue; |
519 | | |
520 | 0 | if (seen && seen[i] == MATCHED_EXACTLY) |
521 | 0 | continue; |
522 | | /* |
523 | | * Make exclude patterns optional and never report |
524 | | * "pathspec ':(exclude)foo' matches no files" |
525 | | */ |
526 | 0 | if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) |
527 | 0 | seen[i] = MATCHED_FNMATCH; |
528 | 0 | how = match_pathspec_item(istate, ps->items+i, prefix, name, |
529 | 0 | namelen, flags); |
530 | 0 | if (ps->recursive && |
531 | 0 | (ps->magic & PATHSPEC_MAXDEPTH) && |
532 | 0 | ps->max_depth != -1 && |
533 | 0 | how && how != MATCHED_FNMATCH) { |
534 | 0 | int len = ps->items[i].len; |
535 | 0 | if (name[len] == '/') |
536 | 0 | len++; |
537 | 0 | if (within_depth(name+len, namelen-len, 0, ps->max_depth)) |
538 | 0 | how = MATCHED_EXACTLY; |
539 | 0 | else |
540 | 0 | how = 0; |
541 | 0 | } |
542 | 0 | if (how) { |
543 | 0 | if (retval < how) |
544 | 0 | retval = how; |
545 | 0 | if (seen && seen[i] < how) |
546 | 0 | seen[i] = how; |
547 | 0 | } |
548 | 0 | } |
549 | 0 | return retval; |
550 | 0 | } |
551 | | |
552 | | static int match_pathspec_with_flags(struct index_state *istate, |
553 | | const struct pathspec *ps, |
554 | | const char *name, int namelen, |
555 | | int prefix, char *seen, unsigned flags) |
556 | 0 | { |
557 | 0 | int positive, negative; |
558 | 0 | positive = do_match_pathspec(istate, ps, name, namelen, |
559 | 0 | prefix, seen, flags); |
560 | 0 | if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) |
561 | 0 | return positive; |
562 | 0 | negative = do_match_pathspec(istate, ps, name, namelen, |
563 | 0 | prefix, seen, |
564 | 0 | flags | DO_MATCH_EXCLUDE); |
565 | 0 | return negative ? 0 : positive; |
566 | 0 | } |
567 | | |
568 | | int match_pathspec(struct index_state *istate, |
569 | | const struct pathspec *ps, |
570 | | const char *name, int namelen, |
571 | | int prefix, char *seen, int is_dir) |
572 | 0 | { |
573 | 0 | unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; |
574 | 0 | return match_pathspec_with_flags(istate, ps, name, namelen, |
575 | 0 | prefix, seen, flags); |
576 | 0 | } |
577 | | |
578 | | /** |
579 | | * Check if a submodule is a superset of the pathspec |
580 | | */ |
581 | | int submodule_path_match(struct index_state *istate, |
582 | | const struct pathspec *ps, |
583 | | const char *submodule_name, |
584 | | char *seen) |
585 | 0 | { |
586 | 0 | int matched = match_pathspec_with_flags(istate, ps, submodule_name, |
587 | 0 | strlen(submodule_name), |
588 | 0 | 0, seen, |
589 | 0 | DO_MATCH_DIRECTORY | |
590 | 0 | DO_MATCH_LEADING_PATHSPEC); |
591 | 0 | return matched; |
592 | 0 | } |
593 | | |
594 | | int report_path_error(const char *ps_matched, |
595 | | const struct pathspec *pathspec) |
596 | 0 | { |
597 | | /* |
598 | | * Make sure all pathspec matched; otherwise it is an error. |
599 | | */ |
600 | 0 | int num, errors = 0; |
601 | 0 | for (num = 0; num < pathspec->nr; num++) { |
602 | 0 | int other, found_dup; |
603 | |
|
604 | 0 | if (ps_matched[num]) |
605 | 0 | continue; |
606 | | /* |
607 | | * The caller might have fed identical pathspec |
608 | | * twice. Do not barf on such a mistake. |
609 | | * FIXME: parse_pathspec should have eliminated |
610 | | * duplicate pathspec. |
611 | | */ |
612 | 0 | for (found_dup = other = 0; |
613 | 0 | !found_dup && other < pathspec->nr; |
614 | 0 | other++) { |
615 | 0 | if (other == num || !ps_matched[other]) |
616 | 0 | continue; |
617 | 0 | if (!strcmp(pathspec->items[other].original, |
618 | 0 | pathspec->items[num].original)) |
619 | | /* |
620 | | * Ok, we have a match already. |
621 | | */ |
622 | 0 | found_dup = 1; |
623 | 0 | } |
624 | 0 | if (found_dup) |
625 | 0 | continue; |
626 | | |
627 | 0 | error(_("pathspec '%s' did not match any file(s) known to git"), |
628 | 0 | pathspec->items[num].original); |
629 | 0 | errors++; |
630 | 0 | } |
631 | 0 | return errors; |
632 | 0 | } |
633 | | |
634 | | /* |
635 | | * Return the length of the "simple" part of a path match limiter. |
636 | | */ |
637 | | int simple_length(const char *match) |
638 | 0 | { |
639 | 0 | int len = -1; |
640 | |
|
641 | 0 | for (;;) { |
642 | 0 | unsigned char c = *match++; |
643 | 0 | len++; |
644 | 0 | if (c == '\0' || is_glob_special(c)) |
645 | 0 | return len; |
646 | 0 | } |
647 | 0 | } |
648 | | |
649 | | int no_wildcard(const char *string) |
650 | 0 | { |
651 | 0 | return string[simple_length(string)] == '\0'; |
652 | 0 | } |
653 | | |
654 | | void parse_path_pattern(const char **pattern, |
655 | | int *patternlen, |
656 | | unsigned *flags, |
657 | | int *nowildcardlen) |
658 | 0 | { |
659 | 0 | const char *p = *pattern; |
660 | 0 | size_t i, len; |
661 | |
|
662 | 0 | *flags = 0; |
663 | 0 | if (*p == '!') { |
664 | 0 | *flags |= PATTERN_FLAG_NEGATIVE; |
665 | 0 | p++; |
666 | 0 | } |
667 | 0 | len = strlen(p); |
668 | 0 | if (len && p[len - 1] == '/') { |
669 | 0 | len--; |
670 | 0 | *flags |= PATTERN_FLAG_MUSTBEDIR; |
671 | 0 | } |
672 | 0 | for (i = 0; i < len; i++) { |
673 | 0 | if (p[i] == '/') |
674 | 0 | break; |
675 | 0 | } |
676 | 0 | if (i == len) |
677 | 0 | *flags |= PATTERN_FLAG_NODIR; |
678 | 0 | *nowildcardlen = simple_length(p); |
679 | | /* |
680 | | * we should have excluded the trailing slash from 'p' too, |
681 | | * but that's one more allocation. Instead just make sure |
682 | | * nowildcardlen does not exceed real patternlen |
683 | | */ |
684 | 0 | if (*nowildcardlen > len) |
685 | 0 | *nowildcardlen = len; |
686 | 0 | if (*p == '*' && no_wildcard(p + 1)) |
687 | 0 | *flags |= PATTERN_FLAG_ENDSWITH; |
688 | 0 | *pattern = p; |
689 | 0 | *patternlen = len; |
690 | 0 | } |
691 | | |
692 | | int pl_hashmap_cmp(const void *cmp_data UNUSED, |
693 | | const struct hashmap_entry *a, |
694 | | const struct hashmap_entry *b, |
695 | | const void *key UNUSED) |
696 | 0 | { |
697 | 0 | const struct pattern_entry *ee1 = |
698 | 0 | container_of(a, struct pattern_entry, ent); |
699 | 0 | const struct pattern_entry *ee2 = |
700 | 0 | container_of(b, struct pattern_entry, ent); |
701 | |
|
702 | 0 | size_t min_len = ee1->patternlen <= ee2->patternlen |
703 | 0 | ? ee1->patternlen |
704 | 0 | : ee2->patternlen; |
705 | |
|
706 | 0 | return fspathncmp(ee1->pattern, ee2->pattern, min_len); |
707 | 0 | } |
708 | | |
709 | | static char *dup_and_filter_pattern(const char *pattern) |
710 | 0 | { |
711 | 0 | char *set, *read; |
712 | 0 | size_t count = 0; |
713 | 0 | char *result = xstrdup(pattern); |
714 | |
|
715 | 0 | set = result; |
716 | 0 | read = result; |
717 | |
|
718 | 0 | while (*read) { |
719 | | /* skip escape characters (once) */ |
720 | 0 | if (*read == '\\') |
721 | 0 | read++; |
722 | |
|
723 | 0 | *set = *read; |
724 | |
|
725 | 0 | set++; |
726 | 0 | read++; |
727 | 0 | count++; |
728 | 0 | } |
729 | 0 | *set = 0; |
730 | |
|
731 | 0 | if (count > 2 && |
732 | 0 | *(set - 1) == '*' && |
733 | 0 | *(set - 2) == '/') |
734 | 0 | *(set - 2) = 0; |
735 | |
|
736 | 0 | return result; |
737 | 0 | } |
738 | | |
739 | | static void clear_pattern_entry_hashmap(struct hashmap *map) |
740 | 0 | { |
741 | 0 | struct hashmap_iter iter; |
742 | 0 | struct pattern_entry *entry; |
743 | |
|
744 | 0 | hashmap_for_each_entry(map, &iter, entry, ent) { |
745 | 0 | free(entry->pattern); |
746 | 0 | } |
747 | 0 | hashmap_clear_and_free(map, struct pattern_entry, ent); |
748 | 0 | } |
749 | | |
750 | | static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given) |
751 | 0 | { |
752 | 0 | struct pattern_entry *translated; |
753 | 0 | char *truncated; |
754 | 0 | char *data = NULL; |
755 | 0 | const char *prev, *cur, *next; |
756 | |
|
757 | 0 | if (!pl->use_cone_patterns) |
758 | 0 | return; |
759 | | |
760 | 0 | if (given->flags & PATTERN_FLAG_NEGATIVE && |
761 | 0 | given->flags & PATTERN_FLAG_MUSTBEDIR && |
762 | 0 | !strcmp(given->pattern, "/*")) { |
763 | 0 | pl->full_cone = 0; |
764 | 0 | return; |
765 | 0 | } |
766 | | |
767 | 0 | if (!given->flags && !strcmp(given->pattern, "/*")) { |
768 | 0 | pl->full_cone = 1; |
769 | 0 | return; |
770 | 0 | } |
771 | | |
772 | 0 | if (given->patternlen < 2 || |
773 | 0 | *given->pattern != '/' || |
774 | 0 | strstr(given->pattern, "**")) { |
775 | | /* Not a cone pattern. */ |
776 | 0 | warning(_("unrecognized pattern: '%s'"), given->pattern); |
777 | 0 | goto clear_hashmaps; |
778 | 0 | } |
779 | | |
780 | 0 | if (!(given->flags & PATTERN_FLAG_MUSTBEDIR) && |
781 | 0 | strcmp(given->pattern, "/*")) { |
782 | | /* Not a cone pattern. */ |
783 | 0 | warning(_("unrecognized pattern: '%s'"), given->pattern); |
784 | 0 | goto clear_hashmaps; |
785 | 0 | } |
786 | | |
787 | 0 | prev = given->pattern; |
788 | 0 | cur = given->pattern + 1; |
789 | 0 | next = given->pattern + 2; |
790 | |
|
791 | 0 | while (*cur) { |
792 | | /* Watch for glob characters '*', '\', '[', '?' */ |
793 | 0 | if (!is_glob_special(*cur)) |
794 | 0 | goto increment; |
795 | | |
796 | | /* But only if *prev != '\\' */ |
797 | 0 | if (*prev == '\\') |
798 | 0 | goto increment; |
799 | | |
800 | | /* But allow the initial '\' */ |
801 | 0 | if (*cur == '\\' && |
802 | 0 | is_glob_special(*next)) |
803 | 0 | goto increment; |
804 | | |
805 | | /* But a trailing '/' then '*' is fine */ |
806 | 0 | if (*prev == '/' && |
807 | 0 | *cur == '*' && |
808 | 0 | *next == 0) |
809 | 0 | goto increment; |
810 | | |
811 | | /* Not a cone pattern. */ |
812 | 0 | warning(_("unrecognized pattern: '%s'"), given->pattern); |
813 | 0 | goto clear_hashmaps; |
814 | | |
815 | 0 | increment: |
816 | 0 | prev++; |
817 | 0 | cur++; |
818 | 0 | next++; |
819 | 0 | } |
820 | | |
821 | 0 | if (given->patternlen > 2 && |
822 | 0 | !strcmp(given->pattern + given->patternlen - 2, "/*")) { |
823 | 0 | struct pattern_entry *old; |
824 | |
|
825 | 0 | if (!(given->flags & PATTERN_FLAG_NEGATIVE)) { |
826 | | /* Not a cone pattern. */ |
827 | 0 | warning(_("unrecognized pattern: '%s'"), given->pattern); |
828 | 0 | goto clear_hashmaps; |
829 | 0 | } |
830 | | |
831 | 0 | truncated = dup_and_filter_pattern(given->pattern); |
832 | |
|
833 | 0 | translated = xmalloc(sizeof(struct pattern_entry)); |
834 | 0 | translated->pattern = truncated; |
835 | 0 | translated->patternlen = given->patternlen - 2; |
836 | 0 | hashmap_entry_init(&translated->ent, |
837 | 0 | fspathhash(translated->pattern)); |
838 | |
|
839 | 0 | if (!hashmap_get_entry(&pl->recursive_hashmap, |
840 | 0 | translated, ent, NULL)) { |
841 | | /* We did not see the "parent" included */ |
842 | 0 | warning(_("unrecognized negative pattern: '%s'"), |
843 | 0 | given->pattern); |
844 | 0 | free(truncated); |
845 | 0 | free(translated); |
846 | 0 | goto clear_hashmaps; |
847 | 0 | } |
848 | | |
849 | 0 | hashmap_add(&pl->parent_hashmap, &translated->ent); |
850 | 0 | old = hashmap_remove_entry(&pl->recursive_hashmap, translated, ent, &data); |
851 | 0 | if (old) { |
852 | 0 | free(old->pattern); |
853 | 0 | free(old); |
854 | 0 | } |
855 | 0 | free(data); |
856 | 0 | return; |
857 | 0 | } |
858 | | |
859 | 0 | if (given->flags & PATTERN_FLAG_NEGATIVE) { |
860 | 0 | warning(_("unrecognized negative pattern: '%s'"), |
861 | 0 | given->pattern); |
862 | 0 | goto clear_hashmaps; |
863 | 0 | } |
864 | | |
865 | 0 | translated = xmalloc(sizeof(struct pattern_entry)); |
866 | |
|
867 | 0 | translated->pattern = dup_and_filter_pattern(given->pattern); |
868 | 0 | translated->patternlen = given->patternlen; |
869 | 0 | hashmap_entry_init(&translated->ent, |
870 | 0 | fspathhash(translated->pattern)); |
871 | |
|
872 | 0 | hashmap_add(&pl->recursive_hashmap, &translated->ent); |
873 | |
|
874 | 0 | if (hashmap_get_entry(&pl->parent_hashmap, translated, ent, NULL)) { |
875 | | /* we already included this at the parent level */ |
876 | 0 | warning(_("your sparse-checkout file may have issues: pattern '%s' is repeated"), |
877 | 0 | given->pattern); |
878 | 0 | goto clear_hashmaps; |
879 | 0 | } |
880 | | |
881 | 0 | return; |
882 | | |
883 | 0 | clear_hashmaps: |
884 | 0 | warning(_("disabling cone pattern matching")); |
885 | 0 | clear_pattern_entry_hashmap(&pl->recursive_hashmap); |
886 | 0 | clear_pattern_entry_hashmap(&pl->parent_hashmap); |
887 | 0 | pl->use_cone_patterns = 0; |
888 | 0 | } |
889 | | |
890 | | static int hashmap_contains_path(struct hashmap *map, |
891 | | struct strbuf *pattern) |
892 | 0 | { |
893 | 0 | struct pattern_entry p; |
894 | | |
895 | | /* Check straight mapping */ |
896 | 0 | p.pattern = pattern->buf; |
897 | 0 | p.patternlen = pattern->len; |
898 | 0 | hashmap_entry_init(&p.ent, fspathhash(p.pattern)); |
899 | 0 | return !!hashmap_get_entry(map, &p, ent, NULL); |
900 | 0 | } |
901 | | |
902 | | int hashmap_contains_parent(struct hashmap *map, |
903 | | const char *path, |
904 | | struct strbuf *buffer) |
905 | 0 | { |
906 | 0 | char *slash_pos; |
907 | |
|
908 | 0 | strbuf_setlen(buffer, 0); |
909 | |
|
910 | 0 | if (path[0] != '/') |
911 | 0 | strbuf_addch(buffer, '/'); |
912 | |
|
913 | 0 | strbuf_addstr(buffer, path); |
914 | |
|
915 | 0 | slash_pos = strrchr(buffer->buf, '/'); |
916 | |
|
917 | 0 | while (slash_pos > buffer->buf) { |
918 | 0 | strbuf_setlen(buffer, slash_pos - buffer->buf); |
919 | |
|
920 | 0 | if (hashmap_contains_path(map, buffer)) |
921 | 0 | return 1; |
922 | | |
923 | 0 | slash_pos = strrchr(buffer->buf, '/'); |
924 | 0 | } |
925 | | |
926 | 0 | return 0; |
927 | 0 | } |
928 | | |
929 | | void add_pattern(const char *string, const char *base, |
930 | | int baselen, struct pattern_list *pl, int srcpos) |
931 | 0 | { |
932 | 0 | struct path_pattern *pattern; |
933 | 0 | int patternlen; |
934 | 0 | unsigned flags; |
935 | 0 | int nowildcardlen; |
936 | |
|
937 | 0 | parse_path_pattern(&string, &patternlen, &flags, &nowildcardlen); |
938 | 0 | FLEX_ALLOC_MEM(pattern, pattern, string, patternlen); |
939 | 0 | pattern->patternlen = patternlen; |
940 | 0 | pattern->nowildcardlen = nowildcardlen; |
941 | 0 | pattern->base = base; |
942 | 0 | pattern->baselen = baselen; |
943 | 0 | pattern->flags = flags; |
944 | 0 | pattern->srcpos = srcpos; |
945 | 0 | ALLOC_GROW(pl->patterns, pl->nr + 1, pl->alloc); |
946 | 0 | pl->patterns[pl->nr++] = pattern; |
947 | 0 | pattern->pl = pl; |
948 | |
|
949 | 0 | add_pattern_to_hashsets(pl, pattern); |
950 | 0 | } |
951 | | |
952 | | static int read_skip_worktree_file_from_index(struct index_state *istate, |
953 | | const char *path, |
954 | | size_t *size_out, char **data_out, |
955 | | struct oid_stat *oid_stat) |
956 | 0 | { |
957 | 0 | int pos, len; |
958 | |
|
959 | 0 | len = strlen(path); |
960 | 0 | pos = index_name_pos(istate, path, len); |
961 | 0 | if (pos < 0) |
962 | 0 | return -1; |
963 | 0 | if (!ce_skip_worktree(istate->cache[pos])) |
964 | 0 | return -1; |
965 | | |
966 | 0 | return do_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); |
967 | 0 | } |
968 | | |
969 | | /* |
970 | | * Frees memory within pl which was allocated for exclude patterns and |
971 | | * the file buffer. Does not free pl itself. |
972 | | */ |
973 | | void clear_pattern_list(struct pattern_list *pl) |
974 | 0 | { |
975 | 0 | int i; |
976 | |
|
977 | 0 | for (i = 0; i < pl->nr; i++) |
978 | 0 | free(pl->patterns[i]); |
979 | 0 | free(pl->patterns); |
980 | 0 | clear_pattern_entry_hashmap(&pl->recursive_hashmap); |
981 | 0 | clear_pattern_entry_hashmap(&pl->parent_hashmap); |
982 | |
|
983 | 0 | memset(pl, 0, sizeof(*pl)); |
984 | 0 | } |
985 | | |
986 | | static void trim_trailing_spaces(char *buf) |
987 | 0 | { |
988 | 0 | char *p, *last_space = NULL; |
989 | |
|
990 | 0 | for (p = buf; *p; p++) |
991 | 0 | switch (*p) { |
992 | 0 | case ' ': |
993 | 0 | if (!last_space) |
994 | 0 | last_space = p; |
995 | 0 | break; |
996 | 0 | case '\\': |
997 | 0 | p++; |
998 | 0 | if (!*p) |
999 | 0 | return; |
1000 | | /* fallthrough */ |
1001 | 0 | default: |
1002 | 0 | last_space = NULL; |
1003 | 0 | } |
1004 | | |
1005 | 0 | if (last_space) |
1006 | 0 | *last_space = '\0'; |
1007 | 0 | } |
1008 | | |
1009 | | /* |
1010 | | * Given a subdirectory name and "dir" of the current directory, |
1011 | | * search the subdir in "dir" and return it, or create a new one if it |
1012 | | * does not exist in "dir". |
1013 | | * |
1014 | | * If "name" has the trailing slash, it'll be excluded in the search. |
1015 | | */ |
1016 | | static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, |
1017 | | struct untracked_cache_dir *dir, |
1018 | | const char *name, int len) |
1019 | 0 | { |
1020 | 0 | int first, last; |
1021 | 0 | struct untracked_cache_dir *d; |
1022 | 0 | if (!dir) |
1023 | 0 | return NULL; |
1024 | 0 | if (len && name[len - 1] == '/') |
1025 | 0 | len--; |
1026 | 0 | first = 0; |
1027 | 0 | last = dir->dirs_nr; |
1028 | 0 | while (last > first) { |
1029 | 0 | int cmp, next = first + ((last - first) >> 1); |
1030 | 0 | d = dir->dirs[next]; |
1031 | 0 | cmp = strncmp(name, d->name, len); |
1032 | 0 | if (!cmp && strlen(d->name) > len) |
1033 | 0 | cmp = -1; |
1034 | 0 | if (!cmp) |
1035 | 0 | return d; |
1036 | 0 | if (cmp < 0) { |
1037 | 0 | last = next; |
1038 | 0 | continue; |
1039 | 0 | } |
1040 | 0 | first = next+1; |
1041 | 0 | } |
1042 | | |
1043 | 0 | uc->dir_created++; |
1044 | 0 | FLEX_ALLOC_MEM(d, name, name, len); |
1045 | |
|
1046 | 0 | ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); |
1047 | 0 | MOVE_ARRAY(dir->dirs + first + 1, dir->dirs + first, |
1048 | 0 | dir->dirs_nr - first); |
1049 | 0 | dir->dirs_nr++; |
1050 | 0 | dir->dirs[first] = d; |
1051 | 0 | return d; |
1052 | 0 | } |
1053 | | |
1054 | | static void do_invalidate_gitignore(struct untracked_cache_dir *dir) |
1055 | 0 | { |
1056 | 0 | int i; |
1057 | 0 | dir->valid = 0; |
1058 | 0 | dir->untracked_nr = 0; |
1059 | 0 | for (i = 0; i < dir->dirs_nr; i++) |
1060 | 0 | do_invalidate_gitignore(dir->dirs[i]); |
1061 | 0 | } |
1062 | | |
1063 | | static void invalidate_gitignore(struct untracked_cache *uc, |
1064 | | struct untracked_cache_dir *dir) |
1065 | 0 | { |
1066 | 0 | uc->gitignore_invalidated++; |
1067 | 0 | do_invalidate_gitignore(dir); |
1068 | 0 | } |
1069 | | |
1070 | | static void invalidate_directory(struct untracked_cache *uc, |
1071 | | struct untracked_cache_dir *dir) |
1072 | 0 | { |
1073 | 0 | int i; |
1074 | | |
1075 | | /* |
1076 | | * Invalidation increment here is just roughly correct. If |
1077 | | * untracked_nr or any of dirs[].recurse is non-zero, we |
1078 | | * should increment dir_invalidated too. But that's more |
1079 | | * expensive to do. |
1080 | | */ |
1081 | 0 | if (dir->valid) |
1082 | 0 | uc->dir_invalidated++; |
1083 | |
|
1084 | 0 | dir->valid = 0; |
1085 | 0 | dir->untracked_nr = 0; |
1086 | 0 | for (i = 0; i < dir->dirs_nr; i++) |
1087 | 0 | dir->dirs[i]->recurse = 0; |
1088 | 0 | } |
1089 | | |
1090 | | static int add_patterns_from_buffer(char *buf, size_t size, |
1091 | | const char *base, int baselen, |
1092 | | struct pattern_list *pl); |
1093 | | |
1094 | | /* Flags for add_patterns() */ |
1095 | 0 | #define PATTERN_NOFOLLOW (1<<0) |
1096 | | |
1097 | | /* |
1098 | | * Given a file with name "fname", read it (either from disk, or from |
1099 | | * an index if 'istate' is non-null), parse it and store the |
1100 | | * exclude rules in "pl". |
1101 | | * |
1102 | | * If "oid_stat" is not NULL, compute oid of the exclude file and fill |
1103 | | * stat data from disk (only valid if add_patterns returns zero). If |
1104 | | * oid_stat.valid is non-zero, "oid_stat" must contain good value as input. |
1105 | | */ |
1106 | | static int add_patterns(const char *fname, const char *base, int baselen, |
1107 | | struct pattern_list *pl, struct index_state *istate, |
1108 | | unsigned flags, struct oid_stat *oid_stat) |
1109 | 0 | { |
1110 | 0 | struct stat st; |
1111 | 0 | int r; |
1112 | 0 | int fd; |
1113 | 0 | size_t size = 0; |
1114 | 0 | char *buf; |
1115 | |
|
1116 | 0 | if (flags & PATTERN_NOFOLLOW) |
1117 | 0 | fd = open_nofollow(fname, O_RDONLY); |
1118 | 0 | else |
1119 | 0 | fd = open(fname, O_RDONLY); |
1120 | |
|
1121 | 0 | if (fd < 0 || fstat(fd, &st) < 0) { |
1122 | 0 | if (fd < 0) |
1123 | 0 | warn_on_fopen_errors(fname); |
1124 | 0 | else |
1125 | 0 | close(fd); |
1126 | 0 | if (!istate) |
1127 | 0 | return -1; |
1128 | 0 | r = read_skip_worktree_file_from_index(istate, fname, |
1129 | 0 | &size, &buf, |
1130 | 0 | oid_stat); |
1131 | 0 | if (r != 1) |
1132 | 0 | return r; |
1133 | 0 | } else { |
1134 | 0 | size = xsize_t(st.st_size); |
1135 | 0 | if (size == 0) { |
1136 | 0 | if (oid_stat) { |
1137 | 0 | fill_stat_data(&oid_stat->stat, &st); |
1138 | 0 | oidcpy(&oid_stat->oid, the_hash_algo->empty_blob); |
1139 | 0 | oid_stat->valid = 1; |
1140 | 0 | } |
1141 | 0 | close(fd); |
1142 | 0 | return 0; |
1143 | 0 | } |
1144 | 0 | buf = xmallocz(size); |
1145 | 0 | if (read_in_full(fd, buf, size) != size) { |
1146 | 0 | free(buf); |
1147 | 0 | close(fd); |
1148 | 0 | return -1; |
1149 | 0 | } |
1150 | 0 | buf[size++] = '\n'; |
1151 | 0 | close(fd); |
1152 | 0 | if (oid_stat) { |
1153 | 0 | int pos; |
1154 | 0 | if (oid_stat->valid && |
1155 | 0 | !match_stat_data_racy(istate, &oid_stat->stat, &st)) |
1156 | 0 | ; /* no content change, oid_stat->oid still good */ |
1157 | 0 | else if (istate && |
1158 | 0 | (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 && |
1159 | 0 | !ce_stage(istate->cache[pos]) && |
1160 | 0 | ce_uptodate(istate->cache[pos]) && |
1161 | 0 | !would_convert_to_git(istate, fname)) |
1162 | 0 | oidcpy(&oid_stat->oid, |
1163 | 0 | &istate->cache[pos]->oid); |
1164 | 0 | else |
1165 | 0 | hash_object_file(the_hash_algo, buf, size, |
1166 | 0 | OBJ_BLOB, &oid_stat->oid); |
1167 | 0 | fill_stat_data(&oid_stat->stat, &st); |
1168 | 0 | oid_stat->valid = 1; |
1169 | 0 | } |
1170 | 0 | } |
1171 | | |
1172 | 0 | if (size > PATTERN_MAX_FILE_SIZE) { |
1173 | 0 | warning("ignoring excessively large pattern file: %s", fname); |
1174 | 0 | free(buf); |
1175 | 0 | return -1; |
1176 | 0 | } |
1177 | | |
1178 | 0 | add_patterns_from_buffer(buf, size, base, baselen, pl); |
1179 | 0 | free(buf); |
1180 | 0 | return 0; |
1181 | 0 | } |
1182 | | |
1183 | | static int add_patterns_from_buffer(char *buf, size_t size, |
1184 | | const char *base, int baselen, |
1185 | | struct pattern_list *pl) |
1186 | 0 | { |
1187 | 0 | char *orig = buf; |
1188 | 0 | int i, lineno = 1; |
1189 | 0 | char *entry; |
1190 | |
|
1191 | 0 | hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
1192 | 0 | hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); |
1193 | |
|
1194 | 0 | if (skip_utf8_bom(&buf, size)) |
1195 | 0 | size -= buf - orig; |
1196 | |
|
1197 | 0 | entry = buf; |
1198 | |
|
1199 | 0 | for (i = 0; i < size; i++) { |
1200 | 0 | if (buf[i] == '\n') { |
1201 | 0 | if (entry != buf + i && entry[0] != '#') { |
1202 | 0 | buf[i - (i && buf[i-1] == '\r')] = 0; |
1203 | 0 | trim_trailing_spaces(entry); |
1204 | 0 | add_pattern(entry, base, baselen, pl, lineno); |
1205 | 0 | } |
1206 | 0 | lineno++; |
1207 | 0 | entry = buf + i + 1; |
1208 | 0 | } |
1209 | 0 | } |
1210 | 0 | return 0; |
1211 | 0 | } |
1212 | | |
1213 | | int add_patterns_from_file_to_list(const char *fname, const char *base, |
1214 | | int baselen, struct pattern_list *pl, |
1215 | | struct index_state *istate, |
1216 | | unsigned flags) |
1217 | 0 | { |
1218 | 0 | return add_patterns(fname, base, baselen, pl, istate, flags, NULL); |
1219 | 0 | } |
1220 | | |
1221 | | int add_patterns_from_blob_to_list( |
1222 | | struct object_id *oid, |
1223 | | const char *base, int baselen, |
1224 | | struct pattern_list *pl) |
1225 | 0 | { |
1226 | 0 | char *buf; |
1227 | 0 | size_t size; |
1228 | 0 | int r; |
1229 | |
|
1230 | 0 | r = do_read_blob(oid, NULL, &size, &buf); |
1231 | 0 | if (r != 1) |
1232 | 0 | return r; |
1233 | | |
1234 | 0 | if (size > PATTERN_MAX_FILE_SIZE) { |
1235 | 0 | warning("ignoring excessively large pattern blob: %s", |
1236 | 0 | oid_to_hex(oid)); |
1237 | 0 | free(buf); |
1238 | 0 | return -1; |
1239 | 0 | } |
1240 | | |
1241 | 0 | add_patterns_from_buffer(buf, size, base, baselen, pl); |
1242 | 0 | free(buf); |
1243 | 0 | return 0; |
1244 | 0 | } |
1245 | | |
1246 | | struct pattern_list *add_pattern_list(struct dir_struct *dir, |
1247 | | int group_type, const char *src) |
1248 | 0 | { |
1249 | 0 | struct pattern_list *pl; |
1250 | 0 | struct exclude_list_group *group; |
1251 | |
|
1252 | 0 | group = &dir->internal.exclude_list_group[group_type]; |
1253 | 0 | ALLOC_GROW(group->pl, group->nr + 1, group->alloc); |
1254 | 0 | pl = &group->pl[group->nr++]; |
1255 | 0 | memset(pl, 0, sizeof(*pl)); |
1256 | 0 | pl->src = src; |
1257 | 0 | return pl; |
1258 | 0 | } |
1259 | | |
1260 | | /* |
1261 | | * Used to set up core.excludesfile and .git/info/exclude lists. |
1262 | | */ |
1263 | | static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname, |
1264 | | struct oid_stat *oid_stat) |
1265 | 0 | { |
1266 | 0 | struct pattern_list *pl; |
1267 | | /* |
1268 | | * catch setup_standard_excludes() that's called before |
1269 | | * dir->untracked is assigned. That function behaves |
1270 | | * differently when dir->untracked is non-NULL. |
1271 | | */ |
1272 | 0 | if (!dir->untracked) |
1273 | 0 | dir->internal.unmanaged_exclude_files++; |
1274 | 0 | pl = add_pattern_list(dir, EXC_FILE, fname); |
1275 | 0 | if (add_patterns(fname, "", 0, pl, NULL, 0, oid_stat) < 0) |
1276 | 0 | die(_("cannot use %s as an exclude file"), fname); |
1277 | 0 | } |
1278 | | |
1279 | | void add_patterns_from_file(struct dir_struct *dir, const char *fname) |
1280 | 0 | { |
1281 | 0 | dir->internal.unmanaged_exclude_files++; /* see validate_untracked_cache() */ |
1282 | 0 | add_patterns_from_file_1(dir, fname, NULL); |
1283 | 0 | } |
1284 | | |
1285 | | int match_basename(const char *basename, int basenamelen, |
1286 | | const char *pattern, int prefix, int patternlen, |
1287 | | unsigned flags) |
1288 | 0 | { |
1289 | 0 | if (prefix == patternlen) { |
1290 | 0 | if (patternlen == basenamelen && |
1291 | 0 | !fspathncmp(pattern, basename, basenamelen)) |
1292 | 0 | return 1; |
1293 | 0 | } else if (flags & PATTERN_FLAG_ENDSWITH) { |
1294 | | /* "*literal" matching against "fooliteral" */ |
1295 | 0 | if (patternlen - 1 <= basenamelen && |
1296 | 0 | !fspathncmp(pattern + 1, |
1297 | 0 | basename + basenamelen - (patternlen - 1), |
1298 | 0 | patternlen - 1)) |
1299 | 0 | return 1; |
1300 | 0 | } else { |
1301 | 0 | if (fnmatch_icase_mem(pattern, patternlen, |
1302 | 0 | basename, basenamelen, |
1303 | 0 | 0) == 0) |
1304 | 0 | return 1; |
1305 | 0 | } |
1306 | 0 | return 0; |
1307 | 0 | } |
1308 | | |
1309 | | int match_pathname(const char *pathname, int pathlen, |
1310 | | const char *base, int baselen, |
1311 | | const char *pattern, int prefix, int patternlen) |
1312 | 0 | { |
1313 | 0 | const char *name; |
1314 | 0 | int namelen; |
1315 | | |
1316 | | /* |
1317 | | * match with FNM_PATHNAME; the pattern has base implicitly |
1318 | | * in front of it. |
1319 | | */ |
1320 | 0 | if (*pattern == '/') { |
1321 | 0 | pattern++; |
1322 | 0 | patternlen--; |
1323 | 0 | prefix--; |
1324 | 0 | } |
1325 | | |
1326 | | /* |
1327 | | * baselen does not count the trailing slash. base[] may or |
1328 | | * may not end with a trailing slash though. |
1329 | | */ |
1330 | 0 | if (pathlen < baselen + 1 || |
1331 | 0 | (baselen && pathname[baselen] != '/') || |
1332 | 0 | fspathncmp(pathname, base, baselen)) |
1333 | 0 | return 0; |
1334 | | |
1335 | 0 | namelen = baselen ? pathlen - baselen - 1 : pathlen; |
1336 | 0 | name = pathname + pathlen - namelen; |
1337 | |
|
1338 | 0 | if (prefix) { |
1339 | | /* |
1340 | | * if the non-wildcard part is longer than the |
1341 | | * remaining pathname, surely it cannot match. |
1342 | | */ |
1343 | 0 | if (prefix > namelen) |
1344 | 0 | return 0; |
1345 | | |
1346 | 0 | if (fspathncmp(pattern, name, prefix)) |
1347 | 0 | return 0; |
1348 | 0 | pattern += prefix; |
1349 | 0 | patternlen -= prefix; |
1350 | 0 | name += prefix; |
1351 | 0 | namelen -= prefix; |
1352 | | |
1353 | | /* |
1354 | | * If the whole pattern did not have a wildcard, |
1355 | | * then our prefix match is all we need; we |
1356 | | * do not need to call fnmatch at all. |
1357 | | */ |
1358 | 0 | if (!patternlen && !namelen) |
1359 | 0 | return 1; |
1360 | 0 | } |
1361 | | |
1362 | 0 | return fnmatch_icase_mem(pattern, patternlen, |
1363 | 0 | name, namelen, |
1364 | 0 | WM_PATHNAME) == 0; |
1365 | 0 | } |
1366 | | |
1367 | | /* |
1368 | | * Scan the given exclude list in reverse to see whether pathname |
1369 | | * should be ignored. The first match (i.e. the last on the list), if |
1370 | | * any, determines the fate. Returns the exclude_list element which |
1371 | | * matched, or NULL for undecided. |
1372 | | */ |
1373 | | static struct path_pattern *last_matching_pattern_from_list(const char *pathname, |
1374 | | int pathlen, |
1375 | | const char *basename, |
1376 | | int *dtype, |
1377 | | struct pattern_list *pl, |
1378 | | struct index_state *istate) |
1379 | 0 | { |
1380 | 0 | struct path_pattern *res = NULL; /* undecided */ |
1381 | 0 | int i; |
1382 | |
|
1383 | 0 | if (!pl->nr) |
1384 | 0 | return NULL; /* undefined */ |
1385 | | |
1386 | 0 | for (i = pl->nr - 1; 0 <= i; i--) { |
1387 | 0 | struct path_pattern *pattern = pl->patterns[i]; |
1388 | 0 | const char *exclude = pattern->pattern; |
1389 | 0 | int prefix = pattern->nowildcardlen; |
1390 | |
|
1391 | 0 | if (pattern->flags & PATTERN_FLAG_MUSTBEDIR) { |
1392 | 0 | *dtype = resolve_dtype(*dtype, istate, pathname, pathlen); |
1393 | 0 | if (*dtype != DT_DIR) |
1394 | 0 | continue; |
1395 | 0 | } |
1396 | | |
1397 | 0 | if (pattern->flags & PATTERN_FLAG_NODIR) { |
1398 | 0 | if (match_basename(basename, |
1399 | 0 | pathlen - (basename - pathname), |
1400 | 0 | exclude, prefix, pattern->patternlen, |
1401 | 0 | pattern->flags)) { |
1402 | 0 | res = pattern; |
1403 | 0 | break; |
1404 | 0 | } |
1405 | 0 | continue; |
1406 | 0 | } |
1407 | | |
1408 | 0 | assert(pattern->baselen == 0 || |
1409 | 0 | pattern->base[pattern->baselen - 1] == '/'); |
1410 | 0 | if (match_pathname(pathname, pathlen, |
1411 | 0 | pattern->base, |
1412 | 0 | pattern->baselen ? pattern->baselen - 1 : 0, |
1413 | 0 | exclude, prefix, pattern->patternlen)) { |
1414 | 0 | res = pattern; |
1415 | 0 | break; |
1416 | 0 | } |
1417 | 0 | } |
1418 | 0 | return res; |
1419 | 0 | } |
1420 | | |
1421 | | /* |
1422 | | * Scan the list of patterns to determine if the ordered list |
1423 | | * of patterns matches on 'pathname'. |
1424 | | * |
1425 | | * Return 1 for a match, 0 for not matched and -1 for undecided. |
1426 | | */ |
1427 | | enum pattern_match_result path_matches_pattern_list( |
1428 | | const char *pathname, int pathlen, |
1429 | | const char *basename, int *dtype, |
1430 | | struct pattern_list *pl, |
1431 | | struct index_state *istate) |
1432 | 0 | { |
1433 | 0 | struct path_pattern *pattern; |
1434 | 0 | struct strbuf parent_pathname = STRBUF_INIT; |
1435 | 0 | int result = NOT_MATCHED; |
1436 | 0 | size_t slash_pos; |
1437 | |
|
1438 | 0 | if (!pl->use_cone_patterns) { |
1439 | 0 | pattern = last_matching_pattern_from_list(pathname, pathlen, basename, |
1440 | 0 | dtype, pl, istate); |
1441 | 0 | if (pattern) { |
1442 | 0 | if (pattern->flags & PATTERN_FLAG_NEGATIVE) |
1443 | 0 | return NOT_MATCHED; |
1444 | 0 | else |
1445 | 0 | return MATCHED; |
1446 | 0 | } |
1447 | | |
1448 | 0 | return UNDECIDED; |
1449 | 0 | } |
1450 | | |
1451 | 0 | if (pl->full_cone) |
1452 | 0 | return MATCHED; |
1453 | | |
1454 | 0 | strbuf_addch(&parent_pathname, '/'); |
1455 | 0 | strbuf_add(&parent_pathname, pathname, pathlen); |
1456 | | |
1457 | | /* |
1458 | | * Directory entries are matched if and only if a file |
1459 | | * contained immediately within them is matched. For the |
1460 | | * case of a directory entry, modify the path to create |
1461 | | * a fake filename within this directory, allowing us to |
1462 | | * use the file-base matching logic in an equivalent way. |
1463 | | */ |
1464 | 0 | if (parent_pathname.len > 0 && |
1465 | 0 | parent_pathname.buf[parent_pathname.len - 1] == '/') { |
1466 | 0 | slash_pos = parent_pathname.len - 1; |
1467 | 0 | strbuf_add(&parent_pathname, "-", 1); |
1468 | 0 | } else { |
1469 | 0 | const char *slash_ptr = strrchr(parent_pathname.buf, '/'); |
1470 | 0 | slash_pos = slash_ptr ? slash_ptr - parent_pathname.buf : 0; |
1471 | 0 | } |
1472 | |
|
1473 | 0 | if (hashmap_contains_path(&pl->recursive_hashmap, |
1474 | 0 | &parent_pathname)) { |
1475 | 0 | result = MATCHED_RECURSIVE; |
1476 | 0 | goto done; |
1477 | 0 | } |
1478 | | |
1479 | 0 | if (!slash_pos) { |
1480 | | /* include every file in root */ |
1481 | 0 | result = MATCHED; |
1482 | 0 | goto done; |
1483 | 0 | } |
1484 | | |
1485 | 0 | strbuf_setlen(&parent_pathname, slash_pos); |
1486 | |
|
1487 | 0 | if (hashmap_contains_path(&pl->parent_hashmap, &parent_pathname)) { |
1488 | 0 | result = MATCHED; |
1489 | 0 | goto done; |
1490 | 0 | } |
1491 | | |
1492 | 0 | if (hashmap_contains_parent(&pl->recursive_hashmap, |
1493 | 0 | pathname, |
1494 | 0 | &parent_pathname)) |
1495 | 0 | result = MATCHED_RECURSIVE; |
1496 | |
|
1497 | 0 | done: |
1498 | 0 | strbuf_release(&parent_pathname); |
1499 | 0 | return result; |
1500 | 0 | } |
1501 | | |
1502 | | int init_sparse_checkout_patterns(struct index_state *istate) |
1503 | 0 | { |
1504 | 0 | if (!core_apply_sparse_checkout) |
1505 | 0 | return 1; |
1506 | 0 | if (istate->sparse_checkout_patterns) |
1507 | 0 | return 0; |
1508 | | |
1509 | 0 | CALLOC_ARRAY(istate->sparse_checkout_patterns, 1); |
1510 | |
|
1511 | 0 | if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0) { |
1512 | 0 | FREE_AND_NULL(istate->sparse_checkout_patterns); |
1513 | 0 | return -1; |
1514 | 0 | } |
1515 | | |
1516 | 0 | return 0; |
1517 | 0 | } |
1518 | | |
1519 | | static int path_in_sparse_checkout_1(const char *path, |
1520 | | struct index_state *istate, |
1521 | | int require_cone_mode) |
1522 | 0 | { |
1523 | 0 | int dtype = DT_REG; |
1524 | 0 | enum pattern_match_result match = UNDECIDED; |
1525 | 0 | const char *end, *slash; |
1526 | | |
1527 | | /* |
1528 | | * We default to accepting a path if the path is empty, there are no |
1529 | | * patterns, or the patterns are of the wrong type. |
1530 | | */ |
1531 | 0 | if (!*path || |
1532 | 0 | init_sparse_checkout_patterns(istate) || |
1533 | 0 | (require_cone_mode && |
1534 | 0 | !istate->sparse_checkout_patterns->use_cone_patterns)) |
1535 | 0 | return 1; |
1536 | | |
1537 | | /* |
1538 | | * If UNDECIDED, use the match from the parent dir (recursively), or |
1539 | | * fall back to NOT_MATCHED at the topmost level. Note that cone mode |
1540 | | * never returns UNDECIDED, so we will execute only one iteration in |
1541 | | * this case. |
1542 | | */ |
1543 | 0 | for (end = path + strlen(path); |
1544 | 0 | end > path && match == UNDECIDED; |
1545 | 0 | end = slash) { |
1546 | |
|
1547 | 0 | for (slash = end - 1; slash > path && *slash != '/'; slash--) |
1548 | 0 | ; /* do nothing */ |
1549 | |
|
1550 | 0 | match = path_matches_pattern_list(path, end - path, |
1551 | 0 | slash > path ? slash + 1 : path, &dtype, |
1552 | 0 | istate->sparse_checkout_patterns, istate); |
1553 | | |
1554 | | /* We are going to match the parent dir now */ |
1555 | 0 | dtype = DT_DIR; |
1556 | 0 | } |
1557 | 0 | return match > 0; |
1558 | 0 | } |
1559 | | |
1560 | | int path_in_sparse_checkout(const char *path, |
1561 | | struct index_state *istate) |
1562 | 0 | { |
1563 | 0 | return path_in_sparse_checkout_1(path, istate, 0); |
1564 | 0 | } |
1565 | | |
1566 | | int path_in_cone_mode_sparse_checkout(const char *path, |
1567 | | struct index_state *istate) |
1568 | 0 | { |
1569 | 0 | return path_in_sparse_checkout_1(path, istate, 1); |
1570 | 0 | } |
1571 | | |
1572 | | static struct path_pattern *last_matching_pattern_from_lists( |
1573 | | struct dir_struct *dir, struct index_state *istate, |
1574 | | const char *pathname, int pathlen, |
1575 | | const char *basename, int *dtype_p) |
1576 | 0 | { |
1577 | 0 | int i, j; |
1578 | 0 | struct exclude_list_group *group; |
1579 | 0 | struct path_pattern *pattern; |
1580 | 0 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { |
1581 | 0 | group = &dir->internal.exclude_list_group[i]; |
1582 | 0 | for (j = group->nr - 1; j >= 0; j--) { |
1583 | 0 | pattern = last_matching_pattern_from_list( |
1584 | 0 | pathname, pathlen, basename, dtype_p, |
1585 | 0 | &group->pl[j], istate); |
1586 | 0 | if (pattern) |
1587 | 0 | return pattern; |
1588 | 0 | } |
1589 | 0 | } |
1590 | 0 | return NULL; |
1591 | 0 | } |
1592 | | |
1593 | | /* |
1594 | | * Loads the per-directory exclude list for the substring of base |
1595 | | * which has a char length of baselen. |
1596 | | */ |
1597 | | static void prep_exclude(struct dir_struct *dir, |
1598 | | struct index_state *istate, |
1599 | | const char *base, int baselen) |
1600 | 0 | { |
1601 | 0 | struct exclude_list_group *group; |
1602 | 0 | struct pattern_list *pl; |
1603 | 0 | struct exclude_stack *stk = NULL; |
1604 | 0 | struct untracked_cache_dir *untracked; |
1605 | 0 | int current; |
1606 | |
|
1607 | 0 | group = &dir->internal.exclude_list_group[EXC_DIRS]; |
1608 | | |
1609 | | /* |
1610 | | * Pop the exclude lists from the EXCL_DIRS exclude_list_group |
1611 | | * which originate from directories not in the prefix of the |
1612 | | * path being checked. |
1613 | | */ |
1614 | 0 | while ((stk = dir->internal.exclude_stack) != NULL) { |
1615 | 0 | if (stk->baselen <= baselen && |
1616 | 0 | !strncmp(dir->internal.basebuf.buf, base, stk->baselen)) |
1617 | 0 | break; |
1618 | 0 | pl = &group->pl[dir->internal.exclude_stack->exclude_ix]; |
1619 | 0 | dir->internal.exclude_stack = stk->prev; |
1620 | 0 | dir->internal.pattern = NULL; |
1621 | 0 | free((char *)pl->src); /* see strbuf_detach() below */ |
1622 | 0 | clear_pattern_list(pl); |
1623 | 0 | free(stk); |
1624 | 0 | group->nr--; |
1625 | 0 | } |
1626 | | |
1627 | | /* Skip traversing into sub directories if the parent is excluded */ |
1628 | 0 | if (dir->internal.pattern) |
1629 | 0 | return; |
1630 | | |
1631 | | /* |
1632 | | * Lazy initialization. All call sites currently just |
1633 | | * memset(dir, 0, sizeof(*dir)) before use. Changing all of |
1634 | | * them seems lots of work for little benefit. |
1635 | | */ |
1636 | 0 | if (!dir->internal.basebuf.buf) |
1637 | 0 | strbuf_init(&dir->internal.basebuf, PATH_MAX); |
1638 | | |
1639 | | /* Read from the parent directories and push them down. */ |
1640 | 0 | current = stk ? stk->baselen : -1; |
1641 | 0 | strbuf_setlen(&dir->internal.basebuf, current < 0 ? 0 : current); |
1642 | 0 | if (dir->untracked) |
1643 | 0 | untracked = stk ? stk->ucd : dir->untracked->root; |
1644 | 0 | else |
1645 | 0 | untracked = NULL; |
1646 | |
|
1647 | 0 | while (current < baselen) { |
1648 | 0 | const char *cp; |
1649 | 0 | struct oid_stat oid_stat; |
1650 | |
|
1651 | 0 | CALLOC_ARRAY(stk, 1); |
1652 | 0 | if (current < 0) { |
1653 | 0 | cp = base; |
1654 | 0 | current = 0; |
1655 | 0 | } else { |
1656 | 0 | cp = strchr(base + current + 1, '/'); |
1657 | 0 | if (!cp) |
1658 | 0 | die("oops in prep_exclude"); |
1659 | 0 | cp++; |
1660 | 0 | untracked = |
1661 | 0 | lookup_untracked(dir->untracked, |
1662 | 0 | untracked, |
1663 | 0 | base + current, |
1664 | 0 | cp - base - current); |
1665 | 0 | } |
1666 | 0 | stk->prev = dir->internal.exclude_stack; |
1667 | 0 | stk->baselen = cp - base; |
1668 | 0 | stk->exclude_ix = group->nr; |
1669 | 0 | stk->ucd = untracked; |
1670 | 0 | pl = add_pattern_list(dir, EXC_DIRS, NULL); |
1671 | 0 | strbuf_add(&dir->internal.basebuf, base + current, stk->baselen - current); |
1672 | 0 | assert(stk->baselen == dir->internal.basebuf.len); |
1673 | | |
1674 | | /* Abort if the directory is excluded */ |
1675 | 0 | if (stk->baselen) { |
1676 | 0 | int dt = DT_DIR; |
1677 | 0 | dir->internal.basebuf.buf[stk->baselen - 1] = 0; |
1678 | 0 | dir->internal.pattern = last_matching_pattern_from_lists(dir, |
1679 | 0 | istate, |
1680 | 0 | dir->internal.basebuf.buf, stk->baselen - 1, |
1681 | 0 | dir->internal.basebuf.buf + current, &dt); |
1682 | 0 | dir->internal.basebuf.buf[stk->baselen - 1] = '/'; |
1683 | 0 | if (dir->internal.pattern && |
1684 | 0 | dir->internal.pattern->flags & PATTERN_FLAG_NEGATIVE) |
1685 | 0 | dir->internal.pattern = NULL; |
1686 | 0 | if (dir->internal.pattern) { |
1687 | 0 | dir->internal.exclude_stack = stk; |
1688 | 0 | return; |
1689 | 0 | } |
1690 | 0 | } |
1691 | | |
1692 | | /* Try to read per-directory file */ |
1693 | 0 | oidclr(&oid_stat.oid, the_repository->hash_algo); |
1694 | 0 | oid_stat.valid = 0; |
1695 | 0 | if (dir->exclude_per_dir && |
1696 | | /* |
1697 | | * If we know that no files have been added in |
1698 | | * this directory (i.e. valid_cached_dir() has |
1699 | | * been executed and set untracked->valid) .. |
1700 | | */ |
1701 | 0 | (!untracked || !untracked->valid || |
1702 | | /* |
1703 | | * .. and .gitignore does not exist before |
1704 | | * (i.e. null exclude_oid). Then we can skip |
1705 | | * loading .gitignore, which would result in |
1706 | | * ENOENT anyway. |
1707 | | */ |
1708 | 0 | !is_null_oid(&untracked->exclude_oid))) { |
1709 | | /* |
1710 | | * dir->internal.basebuf gets reused by the traversal, |
1711 | | * but we need fname to remain unchanged to ensure the |
1712 | | * src member of each struct path_pattern correctly |
1713 | | * back-references its source file. Other invocations |
1714 | | * of add_pattern_list provide stable strings, so we |
1715 | | * strbuf_detach() and free() here in the caller. |
1716 | | */ |
1717 | 0 | struct strbuf sb = STRBUF_INIT; |
1718 | 0 | strbuf_addbuf(&sb, &dir->internal.basebuf); |
1719 | 0 | strbuf_addstr(&sb, dir->exclude_per_dir); |
1720 | 0 | pl->src = strbuf_detach(&sb, NULL); |
1721 | 0 | add_patterns(pl->src, pl->src, stk->baselen, pl, istate, |
1722 | 0 | PATTERN_NOFOLLOW, |
1723 | 0 | untracked ? &oid_stat : NULL); |
1724 | 0 | } |
1725 | | /* |
1726 | | * NEEDSWORK: when untracked cache is enabled, prep_exclude() |
1727 | | * will first be called in valid_cached_dir() then maybe many |
1728 | | * times more in last_matching_pattern(). When the cache is |
1729 | | * used, last_matching_pattern() will not be called and |
1730 | | * reading .gitignore content will be a waste. |
1731 | | * |
1732 | | * So when it's called by valid_cached_dir() and we can get |
1733 | | * .gitignore SHA-1 from the index (i.e. .gitignore is not |
1734 | | * modified on work tree), we could delay reading the |
1735 | | * .gitignore content until we absolutely need it in |
1736 | | * last_matching_pattern(). Be careful about ignore rule |
1737 | | * order, though, if you do that. |
1738 | | */ |
1739 | 0 | if (untracked && |
1740 | 0 | !oideq(&oid_stat.oid, &untracked->exclude_oid)) { |
1741 | 0 | invalidate_gitignore(dir->untracked, untracked); |
1742 | 0 | oidcpy(&untracked->exclude_oid, &oid_stat.oid); |
1743 | 0 | } |
1744 | 0 | dir->internal.exclude_stack = stk; |
1745 | 0 | current = stk->baselen; |
1746 | 0 | } |
1747 | 0 | strbuf_setlen(&dir->internal.basebuf, baselen); |
1748 | 0 | } |
1749 | | |
1750 | | /* |
1751 | | * Loads the exclude lists for the directory containing pathname, then |
1752 | | * scans all exclude lists to determine whether pathname is excluded. |
1753 | | * Returns the exclude_list element which matched, or NULL for |
1754 | | * undecided. |
1755 | | */ |
1756 | | struct path_pattern *last_matching_pattern(struct dir_struct *dir, |
1757 | | struct index_state *istate, |
1758 | | const char *pathname, |
1759 | | int *dtype_p) |
1760 | 0 | { |
1761 | 0 | int pathlen = strlen(pathname); |
1762 | 0 | const char *basename = strrchr(pathname, '/'); |
1763 | 0 | basename = (basename) ? basename+1 : pathname; |
1764 | |
|
1765 | 0 | prep_exclude(dir, istate, pathname, basename-pathname); |
1766 | |
|
1767 | 0 | if (dir->internal.pattern) |
1768 | 0 | return dir->internal.pattern; |
1769 | | |
1770 | 0 | return last_matching_pattern_from_lists(dir, istate, pathname, pathlen, |
1771 | 0 | basename, dtype_p); |
1772 | 0 | } |
1773 | | |
1774 | | /* |
1775 | | * Loads the exclude lists for the directory containing pathname, then |
1776 | | * scans all exclude lists to determine whether pathname is excluded. |
1777 | | * Returns 1 if true, otherwise 0. |
1778 | | */ |
1779 | | int is_excluded(struct dir_struct *dir, struct index_state *istate, |
1780 | | const char *pathname, int *dtype_p) |
1781 | 0 | { |
1782 | 0 | struct path_pattern *pattern = |
1783 | 0 | last_matching_pattern(dir, istate, pathname, dtype_p); |
1784 | 0 | if (pattern) |
1785 | 0 | return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1; |
1786 | 0 | return 0; |
1787 | 0 | } |
1788 | | |
1789 | | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
1790 | 0 | { |
1791 | 0 | struct dir_entry *ent; |
1792 | |
|
1793 | 0 | FLEX_ALLOC_MEM(ent, name, pathname, len); |
1794 | 0 | ent->len = len; |
1795 | 0 | return ent; |
1796 | 0 | } |
1797 | | |
1798 | | static struct dir_entry *dir_add_name(struct dir_struct *dir, |
1799 | | struct index_state *istate, |
1800 | | const char *pathname, int len) |
1801 | 0 | { |
1802 | 0 | if (index_file_exists(istate, pathname, len, ignore_case)) |
1803 | 0 | return NULL; |
1804 | | |
1805 | 0 | ALLOC_GROW(dir->entries, dir->nr+1, dir->internal.alloc); |
1806 | 0 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
1807 | 0 | } |
1808 | | |
1809 | | struct dir_entry *dir_add_ignored(struct dir_struct *dir, |
1810 | | struct index_state *istate, |
1811 | | const char *pathname, int len) |
1812 | 0 | { |
1813 | 0 | if (!index_name_is_other(istate, pathname, len)) |
1814 | 0 | return NULL; |
1815 | | |
1816 | 0 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->internal.ignored_alloc); |
1817 | 0 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
1818 | 0 | } |
1819 | | |
1820 | | enum exist_status { |
1821 | | index_nonexistent = 0, |
1822 | | index_directory, |
1823 | | index_gitdir |
1824 | | }; |
1825 | | |
1826 | | /* |
1827 | | * Do not use the alphabetically sorted index to look up |
1828 | | * the directory name; instead, use the case insensitive |
1829 | | * directory hash. |
1830 | | */ |
1831 | | static enum exist_status directory_exists_in_index_icase(struct index_state *istate, |
1832 | | const char *dirname, int len) |
1833 | 0 | { |
1834 | 0 | struct cache_entry *ce; |
1835 | |
|
1836 | 0 | if (index_dir_exists(istate, dirname, len)) |
1837 | 0 | return index_directory; |
1838 | | |
1839 | 0 | ce = index_file_exists(istate, dirname, len, ignore_case); |
1840 | 0 | if (ce && S_ISGITLINK(ce->ce_mode)) |
1841 | 0 | return index_gitdir; |
1842 | | |
1843 | 0 | return index_nonexistent; |
1844 | 0 | } |
1845 | | |
1846 | | /* |
1847 | | * The index sorts alphabetically by entry name, which |
1848 | | * means that a gitlink sorts as '\0' at the end, while |
1849 | | * a directory (which is defined not as an entry, but as |
1850 | | * the files it contains) will sort with the '/' at the |
1851 | | * end. |
1852 | | */ |
1853 | | static enum exist_status directory_exists_in_index(struct index_state *istate, |
1854 | | const char *dirname, int len) |
1855 | 0 | { |
1856 | 0 | int pos; |
1857 | |
|
1858 | 0 | if (ignore_case) |
1859 | 0 | return directory_exists_in_index_icase(istate, dirname, len); |
1860 | | |
1861 | 0 | pos = index_name_pos(istate, dirname, len); |
1862 | 0 | if (pos < 0) |
1863 | 0 | pos = -pos-1; |
1864 | 0 | while (pos < istate->cache_nr) { |
1865 | 0 | const struct cache_entry *ce = istate->cache[pos++]; |
1866 | 0 | unsigned char endchar; |
1867 | |
|
1868 | 0 | if (strncmp(ce->name, dirname, len)) |
1869 | 0 | break; |
1870 | 0 | endchar = ce->name[len]; |
1871 | 0 | if (endchar > '/') |
1872 | 0 | break; |
1873 | 0 | if (endchar == '/') |
1874 | 0 | return index_directory; |
1875 | 0 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
1876 | 0 | return index_gitdir; |
1877 | 0 | } |
1878 | 0 | return index_nonexistent; |
1879 | 0 | } |
1880 | | |
1881 | | /* |
1882 | | * When we find a directory when traversing the filesystem, we |
1883 | | * have three distinct cases: |
1884 | | * |
1885 | | * - ignore it |
1886 | | * - see it as a directory |
1887 | | * - recurse into it |
1888 | | * |
1889 | | * and which one we choose depends on a combination of existing |
1890 | | * git index contents and the flags passed into the directory |
1891 | | * traversal routine. |
1892 | | * |
1893 | | * Case 1: If we *already* have entries in the index under that |
1894 | | * directory name, we always recurse into the directory to see |
1895 | | * all the files. |
1896 | | * |
1897 | | * Case 2: If we *already* have that directory name as a gitlink, |
1898 | | * we always continue to see it as a gitlink, regardless of whether |
1899 | | * there is an actual git directory there or not (it might not |
1900 | | * be checked out as a subproject!) |
1901 | | * |
1902 | | * Case 3: if we didn't have it in the index previously, we |
1903 | | * have a few sub-cases: |
1904 | | * |
1905 | | * (a) if DIR_SHOW_OTHER_DIRECTORIES flag is set, we show it as |
1906 | | * just a directory, unless DIR_HIDE_EMPTY_DIRECTORIES is |
1907 | | * also true, in which case we need to check if it contains any |
1908 | | * untracked and / or ignored files. |
1909 | | * (b) if it looks like a git directory and we don't have the |
1910 | | * DIR_NO_GITLINKS flag, then we treat it as a gitlink, and |
1911 | | * show it as a directory. |
1912 | | * (c) otherwise, we recurse into it. |
1913 | | */ |
1914 | | static enum path_treatment treat_directory(struct dir_struct *dir, |
1915 | | struct index_state *istate, |
1916 | | struct untracked_cache_dir *untracked, |
1917 | | const char *dirname, int len, int baselen, int excluded, |
1918 | | const struct pathspec *pathspec) |
1919 | 0 | { |
1920 | | /* |
1921 | | * WARNING: From this function, you can return path_recurse or you |
1922 | | * can call read_directory_recursive() (or neither), but |
1923 | | * you CAN'T DO BOTH. |
1924 | | */ |
1925 | 0 | enum path_treatment state; |
1926 | 0 | int matches_how = 0; |
1927 | 0 | int check_only, stop_early; |
1928 | 0 | int old_ignored_nr, old_untracked_nr; |
1929 | | /* The "len-1" is to strip the final '/' */ |
1930 | 0 | enum exist_status status = directory_exists_in_index(istate, dirname, len-1); |
1931 | |
|
1932 | 0 | if (status == index_directory) |
1933 | 0 | return path_recurse; |
1934 | 0 | if (status == index_gitdir) |
1935 | 0 | return path_none; |
1936 | 0 | if (status != index_nonexistent) |
1937 | 0 | BUG("Unhandled value for directory_exists_in_index: %d\n", status); |
1938 | | |
1939 | | /* |
1940 | | * We don't want to descend into paths that don't match the necessary |
1941 | | * patterns. Clearly, if we don't have a pathspec, then we can't check |
1942 | | * for matching patterns. Also, if (excluded) then we know we matched |
1943 | | * the exclusion patterns so as an optimization we can skip checking |
1944 | | * for matching patterns. |
1945 | | */ |
1946 | 0 | if (pathspec && !excluded) { |
1947 | 0 | matches_how = match_pathspec_with_flags(istate, pathspec, |
1948 | 0 | dirname, len, |
1949 | 0 | 0 /* prefix */, |
1950 | 0 | NULL /* seen */, |
1951 | 0 | DO_MATCH_LEADING_PATHSPEC); |
1952 | 0 | if (!matches_how) |
1953 | 0 | return path_none; |
1954 | 0 | } |
1955 | | |
1956 | | |
1957 | 0 | if ((dir->flags & DIR_SKIP_NESTED_GIT) || |
1958 | 0 | !(dir->flags & DIR_NO_GITLINKS)) { |
1959 | | /* |
1960 | | * Determine if `dirname` is a nested repo by confirming that: |
1961 | | * 1) we are in a nonbare repository, and |
1962 | | * 2) `dirname` is not an immediate parent of `the_repository->gitdir`, |
1963 | | * which could occur if the git_dir or worktree location was |
1964 | | * manually configured by the user; see t2205 testcases 1-3 for |
1965 | | * examples where this matters |
1966 | | */ |
1967 | 0 | int nested_repo; |
1968 | 0 | struct strbuf sb = STRBUF_INIT; |
1969 | 0 | strbuf_addstr(&sb, dirname); |
1970 | 0 | nested_repo = is_nonbare_repository_dir(&sb); |
1971 | |
|
1972 | 0 | if (nested_repo) { |
1973 | 0 | char *real_dirname, *real_gitdir; |
1974 | 0 | strbuf_addstr(&sb, ".git"); |
1975 | 0 | real_dirname = real_pathdup(sb.buf, 1); |
1976 | 0 | real_gitdir = real_pathdup(the_repository->gitdir, 1); |
1977 | |
|
1978 | 0 | nested_repo = !!strcmp(real_dirname, real_gitdir); |
1979 | 0 | free(real_gitdir); |
1980 | 0 | free(real_dirname); |
1981 | 0 | } |
1982 | 0 | strbuf_release(&sb); |
1983 | |
|
1984 | 0 | if (nested_repo) { |
1985 | 0 | if ((dir->flags & DIR_SKIP_NESTED_GIT) || |
1986 | 0 | (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC)) |
1987 | 0 | return path_none; |
1988 | 0 | return excluded ? path_excluded : path_untracked; |
1989 | 0 | } |
1990 | 0 | } |
1991 | | |
1992 | 0 | if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) { |
1993 | 0 | if (excluded && |
1994 | 0 | (dir->flags & DIR_SHOW_IGNORED_TOO) && |
1995 | 0 | (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) { |
1996 | | |
1997 | | /* |
1998 | | * This is an excluded directory and we are |
1999 | | * showing ignored paths that match an exclude |
2000 | | * pattern. (e.g. show directory as ignored |
2001 | | * only if it matches an exclude pattern). |
2002 | | * This path will either be 'path_excluded` |
2003 | | * (if we are showing empty directories or if |
2004 | | * the directory is not empty), or will be |
2005 | | * 'path_none' (empty directory, and we are |
2006 | | * not showing empty directories). |
2007 | | */ |
2008 | 0 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
2009 | 0 | return path_excluded; |
2010 | | |
2011 | 0 | if (read_directory_recursive(dir, istate, dirname, len, |
2012 | 0 | untracked, 1, 1, pathspec) == path_excluded) |
2013 | 0 | return path_excluded; |
2014 | | |
2015 | 0 | return path_none; |
2016 | 0 | } |
2017 | 0 | return path_recurse; |
2018 | 0 | } |
2019 | | |
2020 | 0 | assert(dir->flags & DIR_SHOW_OTHER_DIRECTORIES); |
2021 | | |
2022 | | /* |
2023 | | * If we have a pathspec which could match something _below_ this |
2024 | | * directory (e.g. when checking 'subdir/' having a pathspec like |
2025 | | * 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we |
2026 | | * need to recurse. |
2027 | | */ |
2028 | 0 | if (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC) |
2029 | 0 | return path_recurse; |
2030 | | |
2031 | | /* Special cases for where this directory is excluded/ignored */ |
2032 | 0 | if (excluded) { |
2033 | | /* |
2034 | | * If DIR_SHOW_OTHER_DIRECTORIES is set and we're not |
2035 | | * hiding empty directories, there is no need to |
2036 | | * recurse into an ignored directory. |
2037 | | */ |
2038 | 0 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
2039 | 0 | return path_excluded; |
2040 | | |
2041 | | /* |
2042 | | * Even if we are hiding empty directories, we can still avoid |
2043 | | * recursing into ignored directories for DIR_SHOW_IGNORED_TOO |
2044 | | * if DIR_SHOW_IGNORED_TOO_MODE_MATCHING is also set. |
2045 | | */ |
2046 | 0 | if ((dir->flags & DIR_SHOW_IGNORED_TOO) && |
2047 | 0 | (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) |
2048 | 0 | return path_excluded; |
2049 | 0 | } |
2050 | | |
2051 | | /* |
2052 | | * Other than the path_recurse case above, we only need to |
2053 | | * recurse into untracked directories if any of the following |
2054 | | * bits is set: |
2055 | | * - DIR_SHOW_IGNORED (because then we need to determine if |
2056 | | * there are ignored entries below) |
2057 | | * - DIR_SHOW_IGNORED_TOO (same as above) |
2058 | | * - DIR_HIDE_EMPTY_DIRECTORIES (because we have to determine if |
2059 | | * the directory is empty) |
2060 | | */ |
2061 | 0 | if (!excluded && |
2062 | 0 | !(dir->flags & (DIR_SHOW_IGNORED | |
2063 | 0 | DIR_SHOW_IGNORED_TOO | |
2064 | 0 | DIR_HIDE_EMPTY_DIRECTORIES))) { |
2065 | 0 | return path_untracked; |
2066 | 0 | } |
2067 | | |
2068 | | /* |
2069 | | * Even if we don't want to know all the paths under an untracked or |
2070 | | * ignored directory, we may still need to go into the directory to |
2071 | | * determine if it is empty (because with DIR_HIDE_EMPTY_DIRECTORIES, |
2072 | | * an empty directory should be path_none instead of path_excluded or |
2073 | | * path_untracked). |
2074 | | */ |
2075 | 0 | check_only = ((dir->flags & DIR_HIDE_EMPTY_DIRECTORIES) && |
2076 | 0 | !(dir->flags & DIR_SHOW_IGNORED_TOO)); |
2077 | | |
2078 | | /* |
2079 | | * However, there's another optimization possible as a subset of |
2080 | | * check_only, based on the cases we have to consider: |
2081 | | * A) Directory matches no exclude patterns: |
2082 | | * * Directory is empty => path_none |
2083 | | * * Directory has an untracked file under it => path_untracked |
2084 | | * * Directory has only ignored files under it => path_excluded |
2085 | | * B) Directory matches an exclude pattern: |
2086 | | * * Directory is empty => path_none |
2087 | | * * Directory has an untracked file under it => path_excluded |
2088 | | * * Directory has only ignored files under it => path_excluded |
2089 | | * In case A, we can exit as soon as we've found an untracked |
2090 | | * file but otherwise have to walk all files. In case B, though, |
2091 | | * we can stop at the first file we find under the directory. |
2092 | | */ |
2093 | 0 | stop_early = check_only && excluded; |
2094 | | |
2095 | | /* |
2096 | | * If /every/ file within an untracked directory is ignored, then |
2097 | | * we want to treat the directory as ignored (for e.g. status |
2098 | | * --porcelain), without listing the individual ignored files |
2099 | | * underneath. To do so, we'll save the current ignored_nr, and |
2100 | | * pop all the ones added after it if it turns out the entire |
2101 | | * directory is ignored. Also, when DIR_SHOW_IGNORED_TOO and |
2102 | | * !DIR_KEEP_UNTRACKED_CONTENTS then we don't want to show |
2103 | | * untracked paths so will need to pop all those off the last |
2104 | | * after we traverse. |
2105 | | */ |
2106 | 0 | old_ignored_nr = dir->ignored_nr; |
2107 | 0 | old_untracked_nr = dir->nr; |
2108 | | |
2109 | | /* Actually recurse into dirname now, we'll fixup the state later. */ |
2110 | 0 | untracked = lookup_untracked(dir->untracked, untracked, |
2111 | 0 | dirname + baselen, len - baselen); |
2112 | 0 | state = read_directory_recursive(dir, istate, dirname, len, untracked, |
2113 | 0 | check_only, stop_early, pathspec); |
2114 | | |
2115 | | /* There are a variety of reasons we may need to fixup the state... */ |
2116 | 0 | if (state == path_excluded) { |
2117 | | /* state == path_excluded implies all paths under |
2118 | | * dirname were ignored... |
2119 | | * |
2120 | | * if running e.g. `git status --porcelain --ignored=matching`, |
2121 | | * then we want to see the subpaths that are ignored. |
2122 | | * |
2123 | | * if running e.g. just `git status --porcelain`, then |
2124 | | * we just want the directory itself to be listed as ignored |
2125 | | * and not the individual paths underneath. |
2126 | | */ |
2127 | 0 | int want_ignored_subpaths = |
2128 | 0 | ((dir->flags & DIR_SHOW_IGNORED_TOO) && |
2129 | 0 | (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)); |
2130 | |
|
2131 | 0 | if (want_ignored_subpaths) { |
2132 | | /* |
2133 | | * with --ignored=matching, we want the subpaths |
2134 | | * INSTEAD of the directory itself. |
2135 | | */ |
2136 | 0 | state = path_none; |
2137 | 0 | } else { |
2138 | 0 | int i; |
2139 | 0 | for (i = old_ignored_nr + 1; i<dir->ignored_nr; ++i) |
2140 | 0 | FREE_AND_NULL(dir->ignored[i]); |
2141 | 0 | dir->ignored_nr = old_ignored_nr; |
2142 | 0 | } |
2143 | 0 | } |
2144 | | |
2145 | | /* |
2146 | | * We may need to ignore some of the untracked paths we found while |
2147 | | * traversing subdirectories. |
2148 | | */ |
2149 | 0 | if ((dir->flags & DIR_SHOW_IGNORED_TOO) && |
2150 | 0 | !(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) { |
2151 | 0 | int i; |
2152 | 0 | for (i = old_untracked_nr + 1; i<dir->nr; ++i) |
2153 | 0 | FREE_AND_NULL(dir->entries[i]); |
2154 | 0 | dir->nr = old_untracked_nr; |
2155 | 0 | } |
2156 | | |
2157 | | /* |
2158 | | * If there is nothing under the current directory and we are not |
2159 | | * hiding empty directories, then we need to report on the |
2160 | | * untracked or ignored status of the directory itself. |
2161 | | */ |
2162 | 0 | if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
2163 | 0 | state = excluded ? path_excluded : path_untracked; |
2164 | |
|
2165 | 0 | return state; |
2166 | 0 | } |
2167 | | |
2168 | | /* |
2169 | | * This is an inexact early pruning of any recursive directory |
2170 | | * reading - if the path cannot possibly be in the pathspec, |
2171 | | * return true, and we'll skip it early. |
2172 | | */ |
2173 | | static int simplify_away(const char *path, int pathlen, |
2174 | | const struct pathspec *pathspec) |
2175 | 0 | { |
2176 | 0 | int i; |
2177 | |
|
2178 | 0 | if (!pathspec || !pathspec->nr) |
2179 | 0 | return 0; |
2180 | | |
2181 | 0 | GUARD_PATHSPEC(pathspec, |
2182 | 0 | PATHSPEC_FROMTOP | |
2183 | 0 | PATHSPEC_MAXDEPTH | |
2184 | 0 | PATHSPEC_LITERAL | |
2185 | 0 | PATHSPEC_GLOB | |
2186 | 0 | PATHSPEC_ICASE | |
2187 | 0 | PATHSPEC_EXCLUDE | |
2188 | 0 | PATHSPEC_ATTR); |
2189 | | |
2190 | 0 | for (i = 0; i < pathspec->nr; i++) { |
2191 | 0 | const struct pathspec_item *item = &pathspec->items[i]; |
2192 | 0 | int len = item->nowildcard_len; |
2193 | |
|
2194 | 0 | if (len > pathlen) |
2195 | 0 | len = pathlen; |
2196 | 0 | if (!ps_strncmp(item, item->match, path, len)) |
2197 | 0 | return 0; |
2198 | 0 | } |
2199 | | |
2200 | 0 | return 1; |
2201 | 0 | } |
2202 | | |
2203 | | /* |
2204 | | * This function tells us whether an excluded path matches a |
2205 | | * list of "interesting" pathspecs. That is, whether a path matched |
2206 | | * by any of the pathspecs could possibly be ignored by excluding |
2207 | | * the specified path. This can happen if: |
2208 | | * |
2209 | | * 1. the path is mentioned explicitly in the pathspec |
2210 | | * |
2211 | | * 2. the path is a directory prefix of some element in the |
2212 | | * pathspec |
2213 | | */ |
2214 | | static int exclude_matches_pathspec(const char *path, int pathlen, |
2215 | | const struct pathspec *pathspec) |
2216 | 0 | { |
2217 | 0 | int i; |
2218 | |
|
2219 | 0 | if (!pathspec || !pathspec->nr) |
2220 | 0 | return 0; |
2221 | | |
2222 | 0 | GUARD_PATHSPEC(pathspec, |
2223 | 0 | PATHSPEC_FROMTOP | |
2224 | 0 | PATHSPEC_MAXDEPTH | |
2225 | 0 | PATHSPEC_LITERAL | |
2226 | 0 | PATHSPEC_GLOB | |
2227 | 0 | PATHSPEC_ICASE | |
2228 | 0 | PATHSPEC_EXCLUDE | |
2229 | 0 | PATHSPEC_ATTR); |
2230 | | |
2231 | 0 | for (i = 0; i < pathspec->nr; i++) { |
2232 | 0 | const struct pathspec_item *item = &pathspec->items[i]; |
2233 | 0 | int len = item->nowildcard_len; |
2234 | |
|
2235 | 0 | if (len == pathlen && |
2236 | 0 | !ps_strncmp(item, item->match, path, pathlen)) |
2237 | 0 | return 1; |
2238 | 0 | if (len > pathlen && |
2239 | 0 | item->match[pathlen] == '/' && |
2240 | 0 | !ps_strncmp(item, item->match, path, pathlen)) |
2241 | 0 | return 1; |
2242 | 0 | } |
2243 | 0 | return 0; |
2244 | 0 | } |
2245 | | |
2246 | | static int get_index_dtype(struct index_state *istate, |
2247 | | const char *path, int len) |
2248 | 0 | { |
2249 | 0 | int pos; |
2250 | 0 | const struct cache_entry *ce; |
2251 | |
|
2252 | 0 | ce = index_file_exists(istate, path, len, 0); |
2253 | 0 | if (ce) { |
2254 | 0 | if (!ce_uptodate(ce)) |
2255 | 0 | return DT_UNKNOWN; |
2256 | 0 | if (S_ISGITLINK(ce->ce_mode)) |
2257 | 0 | return DT_DIR; |
2258 | | /* |
2259 | | * Nobody actually cares about the |
2260 | | * difference between DT_LNK and DT_REG |
2261 | | */ |
2262 | 0 | return DT_REG; |
2263 | 0 | } |
2264 | | |
2265 | | /* Try to look it up as a directory */ |
2266 | 0 | pos = index_name_pos(istate, path, len); |
2267 | 0 | if (pos >= 0) |
2268 | 0 | return DT_UNKNOWN; |
2269 | 0 | pos = -pos-1; |
2270 | 0 | while (pos < istate->cache_nr) { |
2271 | 0 | ce = istate->cache[pos++]; |
2272 | 0 | if (strncmp(ce->name, path, len)) |
2273 | 0 | break; |
2274 | 0 | if (ce->name[len] > '/') |
2275 | 0 | break; |
2276 | 0 | if (ce->name[len] < '/') |
2277 | 0 | continue; |
2278 | 0 | if (!ce_uptodate(ce)) |
2279 | 0 | break; /* continue? */ |
2280 | 0 | return DT_DIR; |
2281 | 0 | } |
2282 | 0 | return DT_UNKNOWN; |
2283 | 0 | } |
2284 | | |
2285 | | unsigned char get_dtype(struct dirent *e, struct strbuf *path, |
2286 | | int follow_symlink) |
2287 | 0 | { |
2288 | 0 | struct stat st; |
2289 | 0 | unsigned char dtype = DTYPE(e); |
2290 | 0 | size_t base_path_len; |
2291 | |
|
2292 | 0 | if (dtype != DT_UNKNOWN && !(follow_symlink && dtype == DT_LNK)) |
2293 | 0 | return dtype; |
2294 | | |
2295 | | /* |
2296 | | * d_type unknown or unfollowed symlink, try to fall back on [l]stat |
2297 | | * results. If [l]stat fails, explicitly set DT_UNKNOWN. |
2298 | | */ |
2299 | 0 | base_path_len = path->len; |
2300 | 0 | strbuf_addstr(path, e->d_name); |
2301 | 0 | if ((follow_symlink && stat(path->buf, &st)) || |
2302 | 0 | (!follow_symlink && lstat(path->buf, &st))) |
2303 | 0 | goto cleanup; |
2304 | | |
2305 | | /* determine d_type from st_mode */ |
2306 | 0 | if (S_ISREG(st.st_mode)) |
2307 | 0 | dtype = DT_REG; |
2308 | 0 | else if (S_ISDIR(st.st_mode)) |
2309 | 0 | dtype = DT_DIR; |
2310 | 0 | else if (S_ISLNK(st.st_mode)) |
2311 | 0 | dtype = DT_LNK; |
2312 | |
|
2313 | 0 | cleanup: |
2314 | 0 | strbuf_setlen(path, base_path_len); |
2315 | 0 | return dtype; |
2316 | 0 | } |
2317 | | |
2318 | | static int resolve_dtype(int dtype, struct index_state *istate, |
2319 | | const char *path, int len) |
2320 | 0 | { |
2321 | 0 | struct stat st; |
2322 | |
|
2323 | 0 | if (dtype != DT_UNKNOWN) |
2324 | 0 | return dtype; |
2325 | 0 | dtype = get_index_dtype(istate, path, len); |
2326 | 0 | if (dtype != DT_UNKNOWN) |
2327 | 0 | return dtype; |
2328 | 0 | if (lstat(path, &st)) |
2329 | 0 | return dtype; |
2330 | 0 | if (S_ISREG(st.st_mode)) |
2331 | 0 | return DT_REG; |
2332 | 0 | if (S_ISDIR(st.st_mode)) |
2333 | 0 | return DT_DIR; |
2334 | 0 | if (S_ISLNK(st.st_mode)) |
2335 | 0 | return DT_LNK; |
2336 | 0 | return dtype; |
2337 | 0 | } |
2338 | | |
2339 | | static enum path_treatment treat_path_fast(struct dir_struct *dir, |
2340 | | struct cached_dir *cdir, |
2341 | | struct index_state *istate, |
2342 | | struct strbuf *path, |
2343 | | int baselen, |
2344 | | const struct pathspec *pathspec) |
2345 | 0 | { |
2346 | | /* |
2347 | | * WARNING: From this function, you can return path_recurse or you |
2348 | | * can call read_directory_recursive() (or neither), but |
2349 | | * you CAN'T DO BOTH. |
2350 | | */ |
2351 | 0 | strbuf_setlen(path, baselen); |
2352 | 0 | if (!cdir->ucd) { |
2353 | 0 | strbuf_addstr(path, cdir->file); |
2354 | 0 | return path_untracked; |
2355 | 0 | } |
2356 | 0 | strbuf_addstr(path, cdir->ucd->name); |
2357 | | /* treat_one_path() does this before it calls treat_directory() */ |
2358 | 0 | strbuf_complete(path, '/'); |
2359 | 0 | if (cdir->ucd->check_only) |
2360 | | /* |
2361 | | * check_only is set as a result of treat_directory() getting |
2362 | | * to its bottom. Verify again the same set of directories |
2363 | | * with check_only set. |
2364 | | */ |
2365 | 0 | return read_directory_recursive(dir, istate, path->buf, path->len, |
2366 | 0 | cdir->ucd, 1, 0, pathspec); |
2367 | | /* |
2368 | | * We get path_recurse in the first run when |
2369 | | * directory_exists_in_index() returns index_nonexistent. We |
2370 | | * are sure that new changes in the index does not impact the |
2371 | | * outcome. Return now. |
2372 | | */ |
2373 | 0 | return path_recurse; |
2374 | 0 | } |
2375 | | |
2376 | | static enum path_treatment treat_path(struct dir_struct *dir, |
2377 | | struct untracked_cache_dir *untracked, |
2378 | | struct cached_dir *cdir, |
2379 | | struct index_state *istate, |
2380 | | struct strbuf *path, |
2381 | | int baselen, |
2382 | | const struct pathspec *pathspec) |
2383 | 0 | { |
2384 | 0 | int has_path_in_index, dtype, excluded; |
2385 | |
|
2386 | 0 | if (!cdir->d_name) |
2387 | 0 | return treat_path_fast(dir, cdir, istate, path, |
2388 | 0 | baselen, pathspec); |
2389 | 0 | if (is_dot_or_dotdot(cdir->d_name) || !fspathcmp(cdir->d_name, ".git")) |
2390 | 0 | return path_none; |
2391 | 0 | strbuf_setlen(path, baselen); |
2392 | 0 | strbuf_addstr(path, cdir->d_name); |
2393 | 0 | if (simplify_away(path->buf, path->len, pathspec)) |
2394 | 0 | return path_none; |
2395 | | |
2396 | 0 | dtype = resolve_dtype(cdir->d_type, istate, path->buf, path->len); |
2397 | | |
2398 | | /* Always exclude indexed files */ |
2399 | 0 | has_path_in_index = !!index_file_exists(istate, path->buf, path->len, |
2400 | 0 | ignore_case); |
2401 | 0 | if (dtype != DT_DIR && has_path_in_index) |
2402 | 0 | return path_none; |
2403 | | |
2404 | | /* |
2405 | | * When we are looking at a directory P in the working tree, |
2406 | | * there are three cases: |
2407 | | * |
2408 | | * (1) P exists in the index. Everything inside the directory P in |
2409 | | * the working tree needs to go when P is checked out from the |
2410 | | * index. |
2411 | | * |
2412 | | * (2) P does not exist in the index, but there is P/Q in the index. |
2413 | | * We know P will stay a directory when we check out the contents |
2414 | | * of the index, but we do not know yet if there is a directory |
2415 | | * P/Q in the working tree to be killed, so we need to recurse. |
2416 | | * |
2417 | | * (3) P does not exist in the index, and there is no P/Q in the index |
2418 | | * to require P to be a directory, either. Only in this case, we |
2419 | | * know that everything inside P will not be killed without |
2420 | | * recursing. |
2421 | | */ |
2422 | 0 | if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && |
2423 | 0 | (dtype == DT_DIR) && |
2424 | 0 | !has_path_in_index && |
2425 | 0 | (directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent)) |
2426 | 0 | return path_none; |
2427 | | |
2428 | 0 | excluded = is_excluded(dir, istate, path->buf, &dtype); |
2429 | | |
2430 | | /* |
2431 | | * Excluded? If we don't explicitly want to show |
2432 | | * ignored files, ignore it |
2433 | | */ |
2434 | 0 | if (excluded && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO))) |
2435 | 0 | return path_excluded; |
2436 | | |
2437 | 0 | switch (dtype) { |
2438 | 0 | default: |
2439 | 0 | return path_none; |
2440 | 0 | case DT_DIR: |
2441 | | /* |
2442 | | * WARNING: Do not ignore/amend the return value from |
2443 | | * treat_directory(), and especially do not change it to return |
2444 | | * path_recurse as that can cause exponential slowdown. |
2445 | | * Instead, modify treat_directory() to return the right value. |
2446 | | */ |
2447 | 0 | strbuf_addch(path, '/'); |
2448 | 0 | return treat_directory(dir, istate, untracked, |
2449 | 0 | path->buf, path->len, |
2450 | 0 | baselen, excluded, pathspec); |
2451 | 0 | case DT_REG: |
2452 | 0 | case DT_LNK: |
2453 | 0 | if (pathspec && |
2454 | 0 | !match_pathspec(istate, pathspec, path->buf, path->len, |
2455 | 0 | 0 /* prefix */, NULL /* seen */, |
2456 | 0 | 0 /* is_dir */)) |
2457 | 0 | return path_none; |
2458 | 0 | if (excluded) |
2459 | 0 | return path_excluded; |
2460 | 0 | return path_untracked; |
2461 | 0 | } |
2462 | 0 | } |
2463 | | |
2464 | | static void add_untracked(struct untracked_cache_dir *dir, const char *name) |
2465 | 0 | { |
2466 | 0 | if (!dir) |
2467 | 0 | return; |
2468 | 0 | ALLOC_GROW(dir->untracked, dir->untracked_nr + 1, |
2469 | 0 | dir->untracked_alloc); |
2470 | 0 | dir->untracked[dir->untracked_nr++] = xstrdup(name); |
2471 | 0 | } |
2472 | | |
2473 | | static int valid_cached_dir(struct dir_struct *dir, |
2474 | | struct untracked_cache_dir *untracked, |
2475 | | struct index_state *istate, |
2476 | | struct strbuf *path, |
2477 | | int check_only) |
2478 | 0 | { |
2479 | 0 | struct stat st; |
2480 | |
|
2481 | 0 | if (!untracked) |
2482 | 0 | return 0; |
2483 | | |
2484 | | /* |
2485 | | * With fsmonitor, we can trust the untracked cache's valid field. |
2486 | | */ |
2487 | 0 | refresh_fsmonitor(istate); |
2488 | 0 | if (!(dir->untracked->use_fsmonitor && untracked->valid)) { |
2489 | 0 | if (lstat(path->len ? path->buf : ".", &st)) { |
2490 | 0 | memset(&untracked->stat_data, 0, sizeof(untracked->stat_data)); |
2491 | 0 | return 0; |
2492 | 0 | } |
2493 | 0 | if (!untracked->valid || |
2494 | 0 | match_stat_data_racy(istate, &untracked->stat_data, &st)) { |
2495 | 0 | fill_stat_data(&untracked->stat_data, &st); |
2496 | 0 | return 0; |
2497 | 0 | } |
2498 | 0 | } |
2499 | | |
2500 | 0 | if (untracked->check_only != !!check_only) |
2501 | 0 | return 0; |
2502 | | |
2503 | | /* |
2504 | | * prep_exclude will be called eventually on this directory, |
2505 | | * but it's called much later in last_matching_pattern(). We |
2506 | | * need it now to determine the validity of the cache for this |
2507 | | * path. The next calls will be nearly no-op, the way |
2508 | | * prep_exclude() is designed. |
2509 | | */ |
2510 | 0 | if (path->len && path->buf[path->len - 1] != '/') { |
2511 | 0 | strbuf_addch(path, '/'); |
2512 | 0 | prep_exclude(dir, istate, path->buf, path->len); |
2513 | 0 | strbuf_setlen(path, path->len - 1); |
2514 | 0 | } else |
2515 | 0 | prep_exclude(dir, istate, path->buf, path->len); |
2516 | | |
2517 | | /* hopefully prep_exclude() haven't invalidated this entry... */ |
2518 | 0 | return untracked->valid; |
2519 | 0 | } |
2520 | | |
2521 | | static int open_cached_dir(struct cached_dir *cdir, |
2522 | | struct dir_struct *dir, |
2523 | | struct untracked_cache_dir *untracked, |
2524 | | struct index_state *istate, |
2525 | | struct strbuf *path, |
2526 | | int check_only) |
2527 | 0 | { |
2528 | 0 | const char *c_path; |
2529 | |
|
2530 | 0 | memset(cdir, 0, sizeof(*cdir)); |
2531 | 0 | cdir->untracked = untracked; |
2532 | 0 | if (valid_cached_dir(dir, untracked, istate, path, check_only)) |
2533 | 0 | return 0; |
2534 | 0 | c_path = path->len ? path->buf : "."; |
2535 | 0 | cdir->fdir = opendir(c_path); |
2536 | 0 | if (!cdir->fdir) |
2537 | 0 | warning_errno(_("could not open directory '%s'"), c_path); |
2538 | 0 | if (dir->untracked) { |
2539 | 0 | invalidate_directory(dir->untracked, untracked); |
2540 | 0 | dir->untracked->dir_opened++; |
2541 | 0 | } |
2542 | 0 | if (!cdir->fdir) |
2543 | 0 | return -1; |
2544 | 0 | return 0; |
2545 | 0 | } |
2546 | | |
2547 | | static int read_cached_dir(struct cached_dir *cdir) |
2548 | 0 | { |
2549 | 0 | struct dirent *de; |
2550 | |
|
2551 | 0 | if (cdir->fdir) { |
2552 | 0 | de = readdir_skip_dot_and_dotdot(cdir->fdir); |
2553 | 0 | if (!de) { |
2554 | 0 | cdir->d_name = NULL; |
2555 | 0 | cdir->d_type = DT_UNKNOWN; |
2556 | 0 | return -1; |
2557 | 0 | } |
2558 | 0 | cdir->d_name = de->d_name; |
2559 | 0 | cdir->d_type = DTYPE(de); |
2560 | 0 | return 0; |
2561 | 0 | } |
2562 | 0 | while (cdir->nr_dirs < cdir->untracked->dirs_nr) { |
2563 | 0 | struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs]; |
2564 | 0 | if (!d->recurse) { |
2565 | 0 | cdir->nr_dirs++; |
2566 | 0 | continue; |
2567 | 0 | } |
2568 | 0 | cdir->ucd = d; |
2569 | 0 | cdir->nr_dirs++; |
2570 | 0 | return 0; |
2571 | 0 | } |
2572 | 0 | cdir->ucd = NULL; |
2573 | 0 | if (cdir->nr_files < cdir->untracked->untracked_nr) { |
2574 | 0 | struct untracked_cache_dir *d = cdir->untracked; |
2575 | 0 | cdir->file = d->untracked[cdir->nr_files++]; |
2576 | 0 | return 0; |
2577 | 0 | } |
2578 | 0 | return -1; |
2579 | 0 | } |
2580 | | |
2581 | | static void close_cached_dir(struct cached_dir *cdir) |
2582 | 0 | { |
2583 | 0 | if (cdir->fdir) |
2584 | 0 | closedir(cdir->fdir); |
2585 | | /* |
2586 | | * We have gone through this directory and found no untracked |
2587 | | * entries. Mark it valid. |
2588 | | */ |
2589 | 0 | if (cdir->untracked) { |
2590 | 0 | cdir->untracked->valid = 1; |
2591 | 0 | cdir->untracked->recurse = 1; |
2592 | 0 | } |
2593 | 0 | } |
2594 | | |
2595 | | static void add_path_to_appropriate_result_list(struct dir_struct *dir, |
2596 | | struct untracked_cache_dir *untracked, |
2597 | | struct cached_dir *cdir, |
2598 | | struct index_state *istate, |
2599 | | struct strbuf *path, |
2600 | | int baselen, |
2601 | | const struct pathspec *pathspec, |
2602 | | enum path_treatment state) |
2603 | 0 | { |
2604 | | /* add the path to the appropriate result list */ |
2605 | 0 | switch (state) { |
2606 | 0 | case path_excluded: |
2607 | 0 | if (dir->flags & DIR_SHOW_IGNORED) |
2608 | 0 | dir_add_name(dir, istate, path->buf, path->len); |
2609 | 0 | else if ((dir->flags & DIR_SHOW_IGNORED_TOO) || |
2610 | 0 | ((dir->flags & DIR_COLLECT_IGNORED) && |
2611 | 0 | exclude_matches_pathspec(path->buf, path->len, |
2612 | 0 | pathspec))) |
2613 | 0 | dir_add_ignored(dir, istate, path->buf, path->len); |
2614 | 0 | break; |
2615 | | |
2616 | 0 | case path_untracked: |
2617 | 0 | if (dir->flags & DIR_SHOW_IGNORED) |
2618 | 0 | break; |
2619 | 0 | dir_add_name(dir, istate, path->buf, path->len); |
2620 | 0 | if (cdir->fdir) |
2621 | 0 | add_untracked(untracked, path->buf + baselen); |
2622 | 0 | break; |
2623 | | |
2624 | 0 | default: |
2625 | 0 | break; |
2626 | 0 | } |
2627 | 0 | } |
2628 | | |
2629 | | /* |
2630 | | * Read a directory tree. We currently ignore anything but |
2631 | | * directories, regular files and symlinks. That's because git |
2632 | | * doesn't handle them at all yet. Maybe that will change some |
2633 | | * day. |
2634 | | * |
2635 | | * Also, we ignore the name ".git" (even if it is not a directory). |
2636 | | * That likely will not change. |
2637 | | * |
2638 | | * If 'stop_at_first_file' is specified, 'path_excluded' is returned |
2639 | | * to signal that a file was found. This is the least significant value that |
2640 | | * indicates that a file was encountered that does not depend on the order of |
2641 | | * whether an untracked or excluded path was encountered first. |
2642 | | * |
2643 | | * Returns the most significant path_treatment value encountered in the scan. |
2644 | | * If 'stop_at_first_file' is specified, `path_excluded` is the most |
2645 | | * significant path_treatment value that will be returned. |
2646 | | */ |
2647 | | |
2648 | | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
2649 | | struct index_state *istate, const char *base, int baselen, |
2650 | | struct untracked_cache_dir *untracked, int check_only, |
2651 | | int stop_at_first_file, const struct pathspec *pathspec) |
2652 | 0 | { |
2653 | | /* |
2654 | | * WARNING: Do NOT recurse unless path_recurse is returned from |
2655 | | * treat_path(). Recursing on any other return value |
2656 | | * can result in exponential slowdown. |
2657 | | */ |
2658 | 0 | struct cached_dir cdir; |
2659 | 0 | enum path_treatment state, subdir_state, dir_state = path_none; |
2660 | 0 | struct strbuf path = STRBUF_INIT; |
2661 | |
|
2662 | 0 | strbuf_add(&path, base, baselen); |
2663 | |
|
2664 | 0 | if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only)) |
2665 | 0 | goto out; |
2666 | 0 | dir->internal.visited_directories++; |
2667 | |
|
2668 | 0 | if (untracked) |
2669 | 0 | untracked->check_only = !!check_only; |
2670 | |
|
2671 | 0 | while (!read_cached_dir(&cdir)) { |
2672 | | /* check how the file or directory should be treated */ |
2673 | 0 | state = treat_path(dir, untracked, &cdir, istate, &path, |
2674 | 0 | baselen, pathspec); |
2675 | 0 | dir->internal.visited_paths++; |
2676 | |
|
2677 | 0 | if (state > dir_state) |
2678 | 0 | dir_state = state; |
2679 | | |
2680 | | /* recurse into subdir if instructed by treat_path */ |
2681 | 0 | if (state == path_recurse) { |
2682 | 0 | struct untracked_cache_dir *ud; |
2683 | 0 | ud = lookup_untracked(dir->untracked, |
2684 | 0 | untracked, |
2685 | 0 | path.buf + baselen, |
2686 | 0 | path.len - baselen); |
2687 | 0 | subdir_state = |
2688 | 0 | read_directory_recursive(dir, istate, path.buf, |
2689 | 0 | path.len, ud, |
2690 | 0 | check_only, stop_at_first_file, pathspec); |
2691 | 0 | if (subdir_state > dir_state) |
2692 | 0 | dir_state = subdir_state; |
2693 | |
|
2694 | 0 | if (pathspec && |
2695 | 0 | !match_pathspec(istate, pathspec, path.buf, path.len, |
2696 | 0 | 0 /* prefix */, NULL, |
2697 | 0 | 0 /* do NOT special case dirs */)) |
2698 | 0 | state = path_none; |
2699 | 0 | } |
2700 | |
|
2701 | 0 | if (check_only) { |
2702 | 0 | if (stop_at_first_file) { |
2703 | | /* |
2704 | | * If stopping at first file, then |
2705 | | * signal that a file was found by |
2706 | | * returning `path_excluded`. This is |
2707 | | * to return a consistent value |
2708 | | * regardless of whether an ignored or |
2709 | | * excluded file happened to be |
2710 | | * encountered 1st. |
2711 | | * |
2712 | | * In current usage, the |
2713 | | * `stop_at_first_file` is passed when |
2714 | | * an ancestor directory has matched |
2715 | | * an exclude pattern, so any found |
2716 | | * files will be excluded. |
2717 | | */ |
2718 | 0 | if (dir_state >= path_excluded) { |
2719 | 0 | dir_state = path_excluded; |
2720 | 0 | break; |
2721 | 0 | } |
2722 | 0 | } |
2723 | | |
2724 | | /* abort early if maximum state has been reached */ |
2725 | 0 | if (dir_state == path_untracked) { |
2726 | 0 | if (cdir.fdir) |
2727 | 0 | add_untracked(untracked, path.buf + baselen); |
2728 | 0 | break; |
2729 | 0 | } |
2730 | | /* skip the add_path_to_appropriate_result_list() */ |
2731 | 0 | continue; |
2732 | 0 | } |
2733 | | |
2734 | 0 | add_path_to_appropriate_result_list(dir, untracked, &cdir, |
2735 | 0 | istate, &path, baselen, |
2736 | 0 | pathspec, state); |
2737 | 0 | } |
2738 | 0 | close_cached_dir(&cdir); |
2739 | 0 | out: |
2740 | 0 | strbuf_release(&path); |
2741 | |
|
2742 | 0 | return dir_state; |
2743 | 0 | } |
2744 | | |
2745 | | int cmp_dir_entry(const void *p1, const void *p2) |
2746 | 0 | { |
2747 | 0 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; |
2748 | 0 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; |
2749 | |
|
2750 | 0 | return name_compare(e1->name, e1->len, e2->name, e2->len); |
2751 | 0 | } |
2752 | | |
2753 | | /* check if *out lexically strictly contains *in */ |
2754 | | int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in) |
2755 | 0 | { |
2756 | 0 | return (out->len < in->len) && |
2757 | 0 | (out->name[out->len - 1] == '/') && |
2758 | 0 | !memcmp(out->name, in->name, out->len); |
2759 | 0 | } |
2760 | | |
2761 | | static int treat_leading_path(struct dir_struct *dir, |
2762 | | struct index_state *istate, |
2763 | | const char *path, int len, |
2764 | | const struct pathspec *pathspec) |
2765 | 0 | { |
2766 | 0 | struct strbuf sb = STRBUF_INIT; |
2767 | 0 | struct strbuf subdir = STRBUF_INIT; |
2768 | 0 | int prevlen, baselen; |
2769 | 0 | const char *cp; |
2770 | 0 | struct cached_dir cdir; |
2771 | 0 | enum path_treatment state = path_none; |
2772 | | |
2773 | | /* |
2774 | | * For each directory component of path, we are going to check whether |
2775 | | * that path is relevant given the pathspec. For example, if path is |
2776 | | * foo/bar/baz/ |
2777 | | * then we will ask treat_path() whether we should go into foo, then |
2778 | | * whether we should go into bar, then whether baz is relevant. |
2779 | | * Checking each is important because e.g. if path is |
2780 | | * .git/info/ |
2781 | | * then we need to check .git to know we shouldn't traverse it. |
2782 | | * If the return from treat_path() is: |
2783 | | * * path_none, for any path, we return false. |
2784 | | * * path_recurse, for all path components, we return true |
2785 | | * * <anything else> for some intermediate component, we make sure |
2786 | | * to add that path to the relevant list but return false |
2787 | | * signifying that we shouldn't recurse into it. |
2788 | | */ |
2789 | |
|
2790 | 0 | while (len && path[len - 1] == '/') |
2791 | 0 | len--; |
2792 | 0 | if (!len) |
2793 | 0 | return 1; |
2794 | | |
2795 | 0 | memset(&cdir, 0, sizeof(cdir)); |
2796 | 0 | cdir.d_type = DT_DIR; |
2797 | 0 | baselen = 0; |
2798 | 0 | prevlen = 0; |
2799 | 0 | while (1) { |
2800 | 0 | prevlen = baselen + !!baselen; |
2801 | 0 | cp = path + prevlen; |
2802 | 0 | cp = memchr(cp, '/', path + len - cp); |
2803 | 0 | if (!cp) |
2804 | 0 | baselen = len; |
2805 | 0 | else |
2806 | 0 | baselen = cp - path; |
2807 | 0 | strbuf_reset(&sb); |
2808 | 0 | strbuf_add(&sb, path, baselen); |
2809 | 0 | if (!is_directory(sb.buf)) |
2810 | 0 | break; |
2811 | 0 | strbuf_reset(&sb); |
2812 | 0 | strbuf_add(&sb, path, prevlen); |
2813 | 0 | strbuf_reset(&subdir); |
2814 | 0 | strbuf_add(&subdir, path+prevlen, baselen-prevlen); |
2815 | 0 | cdir.d_name = subdir.buf; |
2816 | 0 | state = treat_path(dir, NULL, &cdir, istate, &sb, prevlen, pathspec); |
2817 | |
|
2818 | 0 | if (state != path_recurse) |
2819 | 0 | break; /* do not recurse into it */ |
2820 | 0 | if (len <= baselen) |
2821 | 0 | break; /* finished checking */ |
2822 | 0 | } |
2823 | 0 | add_path_to_appropriate_result_list(dir, NULL, &cdir, istate, |
2824 | 0 | &sb, baselen, pathspec, |
2825 | 0 | state); |
2826 | |
|
2827 | 0 | strbuf_release(&subdir); |
2828 | 0 | strbuf_release(&sb); |
2829 | 0 | return state == path_recurse; |
2830 | 0 | } |
2831 | | |
2832 | | static const char *get_ident_string(void) |
2833 | 0 | { |
2834 | 0 | static struct strbuf sb = STRBUF_INIT; |
2835 | 0 | struct utsname uts; |
2836 | |
|
2837 | 0 | if (sb.len) |
2838 | 0 | return sb.buf; |
2839 | 0 | if (uname(&uts) < 0) |
2840 | 0 | die_errno(_("failed to get kernel name and information")); |
2841 | 0 | strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(), |
2842 | 0 | uts.sysname); |
2843 | 0 | return sb.buf; |
2844 | 0 | } |
2845 | | |
2846 | | static int ident_in_untracked(const struct untracked_cache *uc) |
2847 | 0 | { |
2848 | | /* |
2849 | | * Previous git versions may have saved many NUL separated |
2850 | | * strings in the "ident" field, but it is insane to manage |
2851 | | * many locations, so just take care of the first one. |
2852 | | */ |
2853 | |
|
2854 | 0 | return !strcmp(uc->ident.buf, get_ident_string()); |
2855 | 0 | } |
2856 | | |
2857 | | static void set_untracked_ident(struct untracked_cache *uc) |
2858 | 0 | { |
2859 | 0 | strbuf_reset(&uc->ident); |
2860 | 0 | strbuf_addstr(&uc->ident, get_ident_string()); |
2861 | | |
2862 | | /* |
2863 | | * This strbuf used to contain a list of NUL separated |
2864 | | * strings, so save NUL too for backward compatibility. |
2865 | | */ |
2866 | 0 | strbuf_addch(&uc->ident, 0); |
2867 | 0 | } |
2868 | | |
2869 | | static unsigned new_untracked_cache_flags(struct index_state *istate) |
2870 | 0 | { |
2871 | 0 | struct repository *repo = istate->repo; |
2872 | 0 | char *val; |
2873 | | |
2874 | | /* |
2875 | | * This logic is coordinated with the setting of these flags in |
2876 | | * wt-status.c#wt_status_collect_untracked(), and the evaluation |
2877 | | * of the config setting in commit.c#git_status_config() |
2878 | | */ |
2879 | 0 | if (!repo_config_get_string(repo, "status.showuntrackedfiles", &val) && |
2880 | 0 | !strcmp(val, "all")) |
2881 | 0 | return 0; |
2882 | | |
2883 | | /* |
2884 | | * The default, if "all" is not set, is "normal" - leading us here. |
2885 | | * If the value is "none" then it really doesn't matter. |
2886 | | */ |
2887 | 0 | return DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; |
2888 | 0 | } |
2889 | | |
2890 | | static void new_untracked_cache(struct index_state *istate, int flags) |
2891 | 0 | { |
2892 | 0 | struct untracked_cache *uc = xcalloc(1, sizeof(*uc)); |
2893 | 0 | strbuf_init(&uc->ident, 100); |
2894 | 0 | uc->exclude_per_dir = ".gitignore"; |
2895 | 0 | uc->dir_flags = flags >= 0 ? flags : new_untracked_cache_flags(istate); |
2896 | 0 | set_untracked_ident(uc); |
2897 | 0 | istate->untracked = uc; |
2898 | 0 | istate->cache_changed |= UNTRACKED_CHANGED; |
2899 | 0 | } |
2900 | | |
2901 | | void add_untracked_cache(struct index_state *istate) |
2902 | 0 | { |
2903 | 0 | if (!istate->untracked) { |
2904 | 0 | new_untracked_cache(istate, -1); |
2905 | 0 | } else { |
2906 | 0 | if (!ident_in_untracked(istate->untracked)) { |
2907 | 0 | free_untracked_cache(istate->untracked); |
2908 | 0 | new_untracked_cache(istate, -1); |
2909 | 0 | } |
2910 | 0 | } |
2911 | 0 | } |
2912 | | |
2913 | | void remove_untracked_cache(struct index_state *istate) |
2914 | 0 | { |
2915 | 0 | if (istate->untracked) { |
2916 | 0 | free_untracked_cache(istate->untracked); |
2917 | 0 | istate->untracked = NULL; |
2918 | 0 | istate->cache_changed |= UNTRACKED_CHANGED; |
2919 | 0 | } |
2920 | 0 | } |
2921 | | |
2922 | | static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir, |
2923 | | int base_len, |
2924 | | const struct pathspec *pathspec, |
2925 | | struct index_state *istate) |
2926 | 0 | { |
2927 | 0 | struct untracked_cache_dir *root; |
2928 | 0 | static int untracked_cache_disabled = -1; |
2929 | |
|
2930 | 0 | if (!dir->untracked) |
2931 | 0 | return NULL; |
2932 | 0 | if (untracked_cache_disabled < 0) |
2933 | 0 | untracked_cache_disabled = git_env_bool("GIT_DISABLE_UNTRACKED_CACHE", 0); |
2934 | 0 | if (untracked_cache_disabled) |
2935 | 0 | return NULL; |
2936 | | |
2937 | | /* |
2938 | | * We only support $GIT_DIR/info/exclude and core.excludesfile |
2939 | | * as the global ignore rule files. Any other additions |
2940 | | * (e.g. from command line) invalidate the cache. This |
2941 | | * condition also catches running setup_standard_excludes() |
2942 | | * before setting dir->untracked! |
2943 | | */ |
2944 | 0 | if (dir->internal.unmanaged_exclude_files) |
2945 | 0 | return NULL; |
2946 | | |
2947 | | /* |
2948 | | * Optimize for the main use case only: whole-tree git |
2949 | | * status. More work involved in treat_leading_path() if we |
2950 | | * use cache on just a subset of the worktree. pathspec |
2951 | | * support could make the matter even worse. |
2952 | | */ |
2953 | 0 | if (base_len || (pathspec && pathspec->nr)) |
2954 | 0 | return NULL; |
2955 | | |
2956 | | /* We don't support collecting ignore files */ |
2957 | 0 | if (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO | |
2958 | 0 | DIR_COLLECT_IGNORED)) |
2959 | 0 | return NULL; |
2960 | | |
2961 | | /* |
2962 | | * If we use .gitignore in the cache and now you change it to |
2963 | | * .gitexclude, everything will go wrong. |
2964 | | */ |
2965 | 0 | if (dir->exclude_per_dir != dir->untracked->exclude_per_dir && |
2966 | 0 | strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir)) |
2967 | 0 | return NULL; |
2968 | | |
2969 | | /* |
2970 | | * EXC_CMDL is not considered in the cache. If people set it, |
2971 | | * skip the cache. |
2972 | | */ |
2973 | 0 | if (dir->internal.exclude_list_group[EXC_CMDL].nr) |
2974 | 0 | return NULL; |
2975 | | |
2976 | 0 | if (!ident_in_untracked(dir->untracked)) { |
2977 | 0 | warning(_("untracked cache is disabled on this system or location")); |
2978 | 0 | return NULL; |
2979 | 0 | } |
2980 | | |
2981 | | /* |
2982 | | * If the untracked structure we received does not have the same flags |
2983 | | * as requested in this run, we're going to need to either discard the |
2984 | | * existing structure (and potentially later recreate), or bypass the |
2985 | | * untracked cache mechanism for this run. |
2986 | | */ |
2987 | 0 | if (dir->flags != dir->untracked->dir_flags) { |
2988 | | /* |
2989 | | * If the untracked structure we received does not have the same flags |
2990 | | * as configured, then we need to reset / create a new "untracked" |
2991 | | * structure to match the new config. |
2992 | | * |
2993 | | * Keeping the saved and used untracked cache consistent with the |
2994 | | * configuration provides an opportunity for frequent users of |
2995 | | * "git status -uall" to leverage the untracked cache by aligning their |
2996 | | * configuration - setting "status.showuntrackedfiles" to "all" or |
2997 | | * "normal" as appropriate. |
2998 | | * |
2999 | | * Previously using -uall (or setting "status.showuntrackedfiles" to |
3000 | | * "all") was incompatible with untracked cache and *consistently* |
3001 | | * caused surprisingly bad performance (with fscache and fsmonitor |
3002 | | * enabled) on Windows. |
3003 | | * |
3004 | | * IMPROVEMENT OPPORTUNITY: If we reworked the untracked cache storage |
3005 | | * to not be as bound up with the desired output in a given run, |
3006 | | * and instead iterated through and stored enough information to |
3007 | | * correctly serve both "modes", then users could get peak performance |
3008 | | * with or without '-uall' regardless of their |
3009 | | * "status.showuntrackedfiles" config. |
3010 | | */ |
3011 | 0 | if (dir->untracked->dir_flags != new_untracked_cache_flags(istate)) { |
3012 | 0 | free_untracked_cache(istate->untracked); |
3013 | 0 | new_untracked_cache(istate, dir->flags); |
3014 | 0 | dir->untracked = istate->untracked; |
3015 | 0 | } |
3016 | 0 | else { |
3017 | | /* |
3018 | | * Current untracked cache data is consistent with config, but not |
3019 | | * usable in this request/run; just bypass untracked cache. |
3020 | | */ |
3021 | 0 | return NULL; |
3022 | 0 | } |
3023 | 0 | } |
3024 | | |
3025 | 0 | if (!dir->untracked->root) { |
3026 | | /* Untracked cache existed but is not initialized; fix that */ |
3027 | 0 | FLEX_ALLOC_STR(dir->untracked->root, name, ""); |
3028 | 0 | istate->cache_changed |= UNTRACKED_CHANGED; |
3029 | 0 | } |
3030 | | |
3031 | | /* Validate $GIT_DIR/info/exclude and core.excludesfile */ |
3032 | 0 | root = dir->untracked->root; |
3033 | 0 | if (!oideq(&dir->internal.ss_info_exclude.oid, |
3034 | 0 | &dir->untracked->ss_info_exclude.oid)) { |
3035 | 0 | invalidate_gitignore(dir->untracked, root); |
3036 | 0 | dir->untracked->ss_info_exclude = dir->internal.ss_info_exclude; |
3037 | 0 | } |
3038 | 0 | if (!oideq(&dir->internal.ss_excludes_file.oid, |
3039 | 0 | &dir->untracked->ss_excludes_file.oid)) { |
3040 | 0 | invalidate_gitignore(dir->untracked, root); |
3041 | 0 | dir->untracked->ss_excludes_file = dir->internal.ss_excludes_file; |
3042 | 0 | } |
3043 | | |
3044 | | /* Make sure this directory is not dropped out at saving phase */ |
3045 | 0 | root->recurse = 1; |
3046 | 0 | return root; |
3047 | 0 | } |
3048 | | |
3049 | | static void emit_traversal_statistics(struct dir_struct *dir, |
3050 | | struct repository *repo, |
3051 | | const char *path, |
3052 | | int path_len) |
3053 | 0 | { |
3054 | 0 | if (!trace2_is_enabled()) |
3055 | 0 | return; |
3056 | | |
3057 | 0 | if (!path_len) { |
3058 | 0 | trace2_data_string("read_directory", repo, "path", ""); |
3059 | 0 | } else { |
3060 | 0 | struct strbuf tmp = STRBUF_INIT; |
3061 | 0 | strbuf_add(&tmp, path, path_len); |
3062 | 0 | trace2_data_string("read_directory", repo, "path", tmp.buf); |
3063 | 0 | strbuf_release(&tmp); |
3064 | 0 | } |
3065 | |
|
3066 | 0 | trace2_data_intmax("read_directory", repo, |
3067 | 0 | "directories-visited", dir->internal.visited_directories); |
3068 | 0 | trace2_data_intmax("read_directory", repo, |
3069 | 0 | "paths-visited", dir->internal.visited_paths); |
3070 | |
|
3071 | 0 | if (!dir->untracked) |
3072 | 0 | return; |
3073 | 0 | trace2_data_intmax("read_directory", repo, |
3074 | 0 | "node-creation", dir->untracked->dir_created); |
3075 | 0 | trace2_data_intmax("read_directory", repo, |
3076 | 0 | "gitignore-invalidation", |
3077 | 0 | dir->untracked->gitignore_invalidated); |
3078 | 0 | trace2_data_intmax("read_directory", repo, |
3079 | 0 | "directory-invalidation", |
3080 | 0 | dir->untracked->dir_invalidated); |
3081 | 0 | trace2_data_intmax("read_directory", repo, |
3082 | 0 | "opendir", dir->untracked->dir_opened); |
3083 | 0 | } |
3084 | | |
3085 | | int read_directory(struct dir_struct *dir, struct index_state *istate, |
3086 | | const char *path, int len, const struct pathspec *pathspec) |
3087 | 0 | { |
3088 | 0 | struct untracked_cache_dir *untracked; |
3089 | |
|
3090 | 0 | trace2_region_enter("dir", "read_directory", istate->repo); |
3091 | 0 | dir->internal.visited_paths = 0; |
3092 | 0 | dir->internal.visited_directories = 0; |
3093 | |
|
3094 | 0 | if (has_symlink_leading_path(path, len)) { |
3095 | 0 | trace2_region_leave("dir", "read_directory", istate->repo); |
3096 | 0 | return dir->nr; |
3097 | 0 | } |
3098 | | |
3099 | 0 | untracked = validate_untracked_cache(dir, len, pathspec, istate); |
3100 | 0 | if (!untracked) |
3101 | | /* |
3102 | | * make sure untracked cache code path is disabled, |
3103 | | * e.g. prep_exclude() |
3104 | | */ |
3105 | 0 | dir->untracked = NULL; |
3106 | 0 | if (!len || treat_leading_path(dir, istate, path, len, pathspec)) |
3107 | 0 | read_directory_recursive(dir, istate, path, len, untracked, 0, 0, pathspec); |
3108 | 0 | QSORT(dir->entries, dir->nr, cmp_dir_entry); |
3109 | 0 | QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry); |
3110 | |
|
3111 | 0 | emit_traversal_statistics(dir, istate->repo, path, len); |
3112 | |
|
3113 | 0 | trace2_region_leave("dir", "read_directory", istate->repo); |
3114 | 0 | if (dir->untracked) { |
3115 | 0 | static int force_untracked_cache = -1; |
3116 | |
|
3117 | 0 | if (force_untracked_cache < 0) |
3118 | 0 | force_untracked_cache = |
3119 | 0 | git_env_bool("GIT_FORCE_UNTRACKED_CACHE", -1); |
3120 | 0 | if (force_untracked_cache < 0) |
3121 | 0 | force_untracked_cache = (istate->repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE); |
3122 | 0 | if (force_untracked_cache && |
3123 | 0 | dir->untracked == istate->untracked && |
3124 | 0 | (dir->untracked->dir_opened || |
3125 | 0 | dir->untracked->gitignore_invalidated || |
3126 | 0 | dir->untracked->dir_invalidated)) |
3127 | 0 | istate->cache_changed |= UNTRACKED_CHANGED; |
3128 | 0 | if (dir->untracked != istate->untracked) { |
3129 | 0 | FREE_AND_NULL(dir->untracked); |
3130 | 0 | } |
3131 | 0 | } |
3132 | |
|
3133 | 0 | return dir->nr; |
3134 | 0 | } |
3135 | | |
3136 | | int file_exists(const char *f) |
3137 | 0 | { |
3138 | 0 | struct stat sb; |
3139 | 0 | return lstat(f, &sb) == 0; |
3140 | 0 | } |
3141 | | |
3142 | | int repo_file_exists(struct repository *repo, const char *path) |
3143 | 0 | { |
3144 | 0 | if (repo != the_repository) |
3145 | 0 | BUG("do not know how to check file existence in arbitrary repo"); |
3146 | | |
3147 | 0 | return file_exists(path); |
3148 | 0 | } |
3149 | | |
3150 | | static int cmp_icase(char a, char b) |
3151 | 0 | { |
3152 | 0 | if (a == b) |
3153 | 0 | return 0; |
3154 | 0 | if (ignore_case) |
3155 | 0 | return toupper(a) - toupper(b); |
3156 | 0 | return a - b; |
3157 | 0 | } |
3158 | | |
3159 | | /* |
3160 | | * Given two normalized paths (a trailing slash is ok), if subdir is |
3161 | | * outside dir, return -1. Otherwise return the offset in subdir that |
3162 | | * can be used as relative path to dir. |
3163 | | */ |
3164 | | int dir_inside_of(const char *subdir, const char *dir) |
3165 | 0 | { |
3166 | 0 | int offset = 0; |
3167 | |
|
3168 | 0 | assert(dir && subdir && *dir && *subdir); |
3169 | | |
3170 | 0 | while (*dir && *subdir && !cmp_icase(*dir, *subdir)) { |
3171 | 0 | dir++; |
3172 | 0 | subdir++; |
3173 | 0 | offset++; |
3174 | 0 | } |
3175 | | |
3176 | | /* hel[p]/me vs hel[l]/yeah */ |
3177 | 0 | if (*dir && *subdir) |
3178 | 0 | return -1; |
3179 | | |
3180 | 0 | if (!*subdir) |
3181 | 0 | return !*dir ? offset : -1; /* same dir */ |
3182 | | |
3183 | | /* foo/[b]ar vs foo/[] */ |
3184 | 0 | if (is_dir_sep(dir[-1])) |
3185 | 0 | return is_dir_sep(subdir[-1]) ? offset : -1; |
3186 | | |
3187 | | /* foo[/]bar vs foo[] */ |
3188 | 0 | return is_dir_sep(*subdir) ? offset + 1 : -1; |
3189 | 0 | } |
3190 | | |
3191 | | int is_inside_dir(const char *dir) |
3192 | 0 | { |
3193 | 0 | char *cwd; |
3194 | 0 | int rc; |
3195 | |
|
3196 | 0 | if (!dir) |
3197 | 0 | return 0; |
3198 | | |
3199 | 0 | cwd = xgetcwd(); |
3200 | 0 | rc = (dir_inside_of(cwd, dir) >= 0); |
3201 | 0 | free(cwd); |
3202 | 0 | return rc; |
3203 | 0 | } |
3204 | | |
3205 | | int is_empty_dir(const char *path) |
3206 | 0 | { |
3207 | 0 | DIR *dir = opendir(path); |
3208 | 0 | struct dirent *e; |
3209 | 0 | int ret = 1; |
3210 | |
|
3211 | 0 | if (!dir) |
3212 | 0 | return 0; |
3213 | | |
3214 | 0 | e = readdir_skip_dot_and_dotdot(dir); |
3215 | 0 | if (e) |
3216 | 0 | ret = 0; |
3217 | |
|
3218 | 0 | closedir(dir); |
3219 | 0 | return ret; |
3220 | 0 | } |
3221 | | |
3222 | | char *git_url_basename(const char *repo, int is_bundle, int is_bare) |
3223 | 0 | { |
3224 | 0 | const char *end = repo + strlen(repo), *start, *ptr; |
3225 | 0 | size_t len; |
3226 | 0 | char *dir; |
3227 | | |
3228 | | /* |
3229 | | * Skip scheme. |
3230 | | */ |
3231 | 0 | start = strstr(repo, "://"); |
3232 | 0 | if (!start) |
3233 | 0 | start = repo; |
3234 | 0 | else |
3235 | 0 | start += 3; |
3236 | | |
3237 | | /* |
3238 | | * Skip authentication data. The stripping does happen |
3239 | | * greedily, such that we strip up to the last '@' inside |
3240 | | * the host part. |
3241 | | */ |
3242 | 0 | for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) { |
3243 | 0 | if (*ptr == '@') |
3244 | 0 | start = ptr + 1; |
3245 | 0 | } |
3246 | | |
3247 | | /* |
3248 | | * Strip trailing spaces, slashes and /.git |
3249 | | */ |
3250 | 0 | while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1]))) |
3251 | 0 | end--; |
3252 | 0 | if (end - start > 5 && is_dir_sep(end[-5]) && |
3253 | 0 | !strncmp(end - 4, ".git", 4)) { |
3254 | 0 | end -= 5; |
3255 | 0 | while (start < end && is_dir_sep(end[-1])) |
3256 | 0 | end--; |
3257 | 0 | } |
3258 | | |
3259 | | /* |
3260 | | * It should not be possible to overflow `ptrdiff_t` by passing in an |
3261 | | * insanely long URL, but GCC does not know that and will complain |
3262 | | * without this check. |
3263 | | */ |
3264 | 0 | if (end - start < 0) |
3265 | 0 | die(_("No directory name could be guessed.\n" |
3266 | 0 | "Please specify a directory on the command line")); |
3267 | | |
3268 | | /* |
3269 | | * Strip trailing port number if we've got only a |
3270 | | * hostname (that is, there is no dir separator but a |
3271 | | * colon). This check is required such that we do not |
3272 | | * strip URI's like '/foo/bar:2222.git', which should |
3273 | | * result in a dir '2222' being guessed due to backwards |
3274 | | * compatibility. |
3275 | | */ |
3276 | 0 | if (memchr(start, '/', end - start) == NULL |
3277 | 0 | && memchr(start, ':', end - start) != NULL) { |
3278 | 0 | ptr = end; |
3279 | 0 | while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':') |
3280 | 0 | ptr--; |
3281 | 0 | if (start < ptr && ptr[-1] == ':') |
3282 | 0 | end = ptr - 1; |
3283 | 0 | } |
3284 | | |
3285 | | /* |
3286 | | * Find last component. To remain backwards compatible we |
3287 | | * also regard colons as path separators, such that |
3288 | | * cloning a repository 'foo:bar.git' would result in a |
3289 | | * directory 'bar' being guessed. |
3290 | | */ |
3291 | 0 | ptr = end; |
3292 | 0 | while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':') |
3293 | 0 | ptr--; |
3294 | 0 | start = ptr; |
3295 | | |
3296 | | /* |
3297 | | * Strip .{bundle,git}. |
3298 | | */ |
3299 | 0 | len = end - start; |
3300 | 0 | strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git"); |
3301 | |
|
3302 | 0 | if (!len || (len == 1 && *start == '/')) |
3303 | 0 | die(_("No directory name could be guessed.\n" |
3304 | 0 | "Please specify a directory on the command line")); |
3305 | | |
3306 | 0 | if (is_bare) |
3307 | 0 | dir = xstrfmt("%.*s.git", (int)len, start); |
3308 | 0 | else |
3309 | 0 | dir = xstrndup(start, len); |
3310 | | /* |
3311 | | * Replace sequences of 'control' characters and whitespace |
3312 | | * with one ascii space, remove leading and trailing spaces. |
3313 | | */ |
3314 | 0 | if (*dir) { |
3315 | 0 | char *out = dir; |
3316 | 0 | int prev_space = 1 /* strip leading whitespace */; |
3317 | 0 | for (end = dir; *end; ++end) { |
3318 | 0 | char ch = *end; |
3319 | 0 | if ((unsigned char)ch < '\x20') |
3320 | 0 | ch = '\x20'; |
3321 | 0 | if (isspace(ch)) { |
3322 | 0 | if (prev_space) |
3323 | 0 | continue; |
3324 | 0 | prev_space = 1; |
3325 | 0 | } else |
3326 | 0 | prev_space = 0; |
3327 | 0 | *out++ = ch; |
3328 | 0 | } |
3329 | 0 | *out = '\0'; |
3330 | 0 | if (out > dir && prev_space) |
3331 | 0 | out[-1] = '\0'; |
3332 | 0 | } |
3333 | 0 | return dir; |
3334 | 0 | } |
3335 | | |
3336 | | void strip_dir_trailing_slashes(char *dir) |
3337 | 0 | { |
3338 | 0 | char *end = dir + strlen(dir); |
3339 | |
|
3340 | 0 | while (dir < end - 1 && is_dir_sep(end[-1])) |
3341 | 0 | end--; |
3342 | 0 | *end = '\0'; |
3343 | 0 | } |
3344 | | |
3345 | | static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) |
3346 | 0 | { |
3347 | 0 | DIR *dir; |
3348 | 0 | struct dirent *e; |
3349 | 0 | int ret = 0, original_len = path->len, len, kept_down = 0; |
3350 | 0 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
3351 | 0 | int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL); |
3352 | 0 | int purge_original_cwd = (flag & REMOVE_DIR_PURGE_ORIGINAL_CWD); |
3353 | 0 | struct object_id submodule_head; |
3354 | |
|
3355 | 0 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
3356 | 0 | !repo_resolve_gitlink_ref(the_repository, path->buf, |
3357 | 0 | "HEAD", &submodule_head)) { |
3358 | | /* Do not descend and nuke a nested git work tree. */ |
3359 | 0 | if (kept_up) |
3360 | 0 | *kept_up = 1; |
3361 | 0 | return 0; |
3362 | 0 | } |
3363 | | |
3364 | 0 | flag &= ~REMOVE_DIR_KEEP_TOPLEVEL; |
3365 | 0 | dir = opendir(path->buf); |
3366 | 0 | if (!dir) { |
3367 | 0 | if (errno == ENOENT) |
3368 | 0 | return keep_toplevel ? -1 : 0; |
3369 | 0 | else if (errno == EACCES && !keep_toplevel) |
3370 | | /* |
3371 | | * An empty dir could be removable even if it |
3372 | | * is unreadable: |
3373 | | */ |
3374 | 0 | return rmdir(path->buf); |
3375 | 0 | else |
3376 | 0 | return -1; |
3377 | 0 | } |
3378 | 0 | strbuf_complete(path, '/'); |
3379 | |
|
3380 | 0 | len = path->len; |
3381 | 0 | while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
3382 | 0 | struct stat st; |
3383 | |
|
3384 | 0 | strbuf_setlen(path, len); |
3385 | 0 | strbuf_addstr(path, e->d_name); |
3386 | 0 | if (lstat(path->buf, &st)) { |
3387 | 0 | if (errno == ENOENT) |
3388 | | /* |
3389 | | * file disappeared, which is what we |
3390 | | * wanted anyway |
3391 | | */ |
3392 | 0 | continue; |
3393 | | /* fall through */ |
3394 | 0 | } else if (S_ISDIR(st.st_mode)) { |
3395 | 0 | if (!remove_dir_recurse(path, flag, &kept_down)) |
3396 | 0 | continue; /* happy */ |
3397 | 0 | } else if (!only_empty && |
3398 | 0 | (!unlink(path->buf) || errno == ENOENT)) { |
3399 | 0 | continue; /* happy, too */ |
3400 | 0 | } |
3401 | | |
3402 | | /* path too long, stat fails, or non-directory still exists */ |
3403 | 0 | ret = -1; |
3404 | 0 | break; |
3405 | 0 | } |
3406 | 0 | closedir(dir); |
3407 | |
|
3408 | 0 | strbuf_setlen(path, original_len); |
3409 | 0 | if (!ret && !keep_toplevel && !kept_down) { |
3410 | 0 | if (!purge_original_cwd && |
3411 | 0 | startup_info->original_cwd && |
3412 | 0 | !strcmp(startup_info->original_cwd, path->buf)) |
3413 | 0 | ret = -1; /* Do not remove current working directory */ |
3414 | 0 | else |
3415 | 0 | ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; |
3416 | 0 | } else if (kept_up) |
3417 | | /* |
3418 | | * report the uplevel that it is not an error that we |
3419 | | * did not rmdir() our directory. |
3420 | | */ |
3421 | 0 | *kept_up = !ret; |
3422 | 0 | return ret; |
3423 | 0 | } |
3424 | | |
3425 | | int remove_dir_recursively(struct strbuf *path, int flag) |
3426 | 0 | { |
3427 | 0 | return remove_dir_recurse(path, flag, NULL); |
3428 | 0 | } |
3429 | | |
3430 | | static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude") |
3431 | | |
3432 | | void setup_standard_excludes(struct dir_struct *dir) |
3433 | 0 | { |
3434 | 0 | dir->exclude_per_dir = ".gitignore"; |
3435 | | |
3436 | | /* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */ |
3437 | 0 | if (!excludes_file) |
3438 | 0 | excludes_file = xdg_config_home("ignore"); |
3439 | 0 | if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) |
3440 | 0 | add_patterns_from_file_1(dir, excludes_file, |
3441 | 0 | dir->untracked ? &dir->internal.ss_excludes_file : NULL); |
3442 | | |
3443 | | /* per repository user preference */ |
3444 | 0 | if (startup_info->have_repository) { |
3445 | 0 | const char *path = git_path_info_exclude(); |
3446 | 0 | if (!access_or_warn(path, R_OK, 0)) |
3447 | 0 | add_patterns_from_file_1(dir, path, |
3448 | 0 | dir->untracked ? &dir->internal.ss_info_exclude : NULL); |
3449 | 0 | } |
3450 | 0 | } |
3451 | | |
3452 | | char *get_sparse_checkout_filename(void) |
3453 | 0 | { |
3454 | 0 | return git_pathdup("info/sparse-checkout"); |
3455 | 0 | } |
3456 | | |
3457 | | int get_sparse_checkout_patterns(struct pattern_list *pl) |
3458 | 0 | { |
3459 | 0 | int res; |
3460 | 0 | char *sparse_filename = get_sparse_checkout_filename(); |
3461 | |
|
3462 | 0 | pl->use_cone_patterns = core_sparse_checkout_cone; |
3463 | 0 | res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL, 0); |
3464 | |
|
3465 | 0 | free(sparse_filename); |
3466 | 0 | return res; |
3467 | 0 | } |
3468 | | |
3469 | | int remove_path(const char *name) |
3470 | 0 | { |
3471 | 0 | char *slash; |
3472 | |
|
3473 | 0 | if (unlink(name) && !is_missing_file_error(errno)) |
3474 | 0 | return -1; |
3475 | | |
3476 | 0 | slash = strrchr(name, '/'); |
3477 | 0 | if (slash) { |
3478 | 0 | char *dirs = xstrdup(name); |
3479 | 0 | slash = dirs + (slash - name); |
3480 | 0 | do { |
3481 | 0 | *slash = '\0'; |
3482 | 0 | if (startup_info->original_cwd && |
3483 | 0 | !strcmp(startup_info->original_cwd, dirs)) |
3484 | 0 | break; |
3485 | 0 | } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); |
3486 | 0 | free(dirs); |
3487 | 0 | } |
3488 | 0 | return 0; |
3489 | 0 | } |
3490 | | |
3491 | | /* |
3492 | | * Frees memory within dir which was allocated, and resets fields for further |
3493 | | * use. Does not free dir itself. |
3494 | | */ |
3495 | | void dir_clear(struct dir_struct *dir) |
3496 | 0 | { |
3497 | 0 | int i, j; |
3498 | 0 | struct exclude_list_group *group; |
3499 | 0 | struct pattern_list *pl; |
3500 | 0 | struct exclude_stack *stk; |
3501 | 0 | struct dir_struct new = DIR_INIT; |
3502 | |
|
3503 | 0 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { |
3504 | 0 | group = &dir->internal.exclude_list_group[i]; |
3505 | 0 | for (j = 0; j < group->nr; j++) { |
3506 | 0 | pl = &group->pl[j]; |
3507 | 0 | if (i == EXC_DIRS) |
3508 | 0 | free((char *)pl->src); |
3509 | 0 | clear_pattern_list(pl); |
3510 | 0 | } |
3511 | 0 | free(group->pl); |
3512 | 0 | } |
3513 | |
|
3514 | 0 | for (i = 0; i < dir->ignored_nr; i++) |
3515 | 0 | free(dir->ignored[i]); |
3516 | 0 | for (i = 0; i < dir->nr; i++) |
3517 | 0 | free(dir->entries[i]); |
3518 | 0 | free(dir->ignored); |
3519 | 0 | free(dir->entries); |
3520 | |
|
3521 | 0 | stk = dir->internal.exclude_stack; |
3522 | 0 | while (stk) { |
3523 | 0 | struct exclude_stack *prev = stk->prev; |
3524 | 0 | free(stk); |
3525 | 0 | stk = prev; |
3526 | 0 | } |
3527 | 0 | strbuf_release(&dir->internal.basebuf); |
3528 | |
|
3529 | 0 | memcpy(dir, &new, sizeof(*dir)); |
3530 | 0 | } |
3531 | | |
3532 | | struct ondisk_untracked_cache { |
3533 | | struct stat_data info_exclude_stat; |
3534 | | struct stat_data excludes_file_stat; |
3535 | | uint32_t dir_flags; |
3536 | | }; |
3537 | | |
3538 | 0 | #define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x) |
3539 | | |
3540 | | struct write_data { |
3541 | | int index; /* number of written untracked_cache_dir */ |
3542 | | struct ewah_bitmap *check_only; /* from untracked_cache_dir */ |
3543 | | struct ewah_bitmap *valid; /* from untracked_cache_dir */ |
3544 | | struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */ |
3545 | | struct strbuf out; |
3546 | | struct strbuf sb_stat; |
3547 | | struct strbuf sb_sha1; |
3548 | | }; |
3549 | | |
3550 | | static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from) |
3551 | 0 | { |
3552 | 0 | to->sd_ctime.sec = htonl(from->sd_ctime.sec); |
3553 | 0 | to->sd_ctime.nsec = htonl(from->sd_ctime.nsec); |
3554 | 0 | to->sd_mtime.sec = htonl(from->sd_mtime.sec); |
3555 | 0 | to->sd_mtime.nsec = htonl(from->sd_mtime.nsec); |
3556 | 0 | to->sd_dev = htonl(from->sd_dev); |
3557 | 0 | to->sd_ino = htonl(from->sd_ino); |
3558 | 0 | to->sd_uid = htonl(from->sd_uid); |
3559 | 0 | to->sd_gid = htonl(from->sd_gid); |
3560 | 0 | to->sd_size = htonl(from->sd_size); |
3561 | 0 | } |
3562 | | |
3563 | | static void write_one_dir(struct untracked_cache_dir *untracked, |
3564 | | struct write_data *wd) |
3565 | 0 | { |
3566 | 0 | struct stat_data stat_data; |
3567 | 0 | struct strbuf *out = &wd->out; |
3568 | 0 | unsigned char intbuf[16]; |
3569 | 0 | unsigned int intlen, value; |
3570 | 0 | int i = wd->index++; |
3571 | | |
3572 | | /* |
3573 | | * untracked_nr should be reset whenever valid is clear, but |
3574 | | * for safety.. |
3575 | | */ |
3576 | 0 | if (!untracked->valid) { |
3577 | 0 | untracked->untracked_nr = 0; |
3578 | 0 | untracked->check_only = 0; |
3579 | 0 | } |
3580 | |
|
3581 | 0 | if (untracked->check_only) |
3582 | 0 | ewah_set(wd->check_only, i); |
3583 | 0 | if (untracked->valid) { |
3584 | 0 | ewah_set(wd->valid, i); |
3585 | 0 | stat_data_to_disk(&stat_data, &untracked->stat_data); |
3586 | 0 | strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data)); |
3587 | 0 | } |
3588 | 0 | if (!is_null_oid(&untracked->exclude_oid)) { |
3589 | 0 | ewah_set(wd->sha1_valid, i); |
3590 | 0 | strbuf_add(&wd->sb_sha1, untracked->exclude_oid.hash, |
3591 | 0 | the_hash_algo->rawsz); |
3592 | 0 | } |
3593 | |
|
3594 | 0 | intlen = encode_varint(untracked->untracked_nr, intbuf); |
3595 | 0 | strbuf_add(out, intbuf, intlen); |
3596 | | |
3597 | | /* skip non-recurse directories */ |
3598 | 0 | for (i = 0, value = 0; i < untracked->dirs_nr; i++) |
3599 | 0 | if (untracked->dirs[i]->recurse) |
3600 | 0 | value++; |
3601 | 0 | intlen = encode_varint(value, intbuf); |
3602 | 0 | strbuf_add(out, intbuf, intlen); |
3603 | |
|
3604 | 0 | strbuf_add(out, untracked->name, strlen(untracked->name) + 1); |
3605 | |
|
3606 | 0 | for (i = 0; i < untracked->untracked_nr; i++) |
3607 | 0 | strbuf_add(out, untracked->untracked[i], |
3608 | 0 | strlen(untracked->untracked[i]) + 1); |
3609 | |
|
3610 | 0 | for (i = 0; i < untracked->dirs_nr; i++) |
3611 | 0 | if (untracked->dirs[i]->recurse) |
3612 | 0 | write_one_dir(untracked->dirs[i], wd); |
3613 | 0 | } |
3614 | | |
3615 | | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked) |
3616 | 0 | { |
3617 | 0 | struct ondisk_untracked_cache *ouc; |
3618 | 0 | struct write_data wd; |
3619 | 0 | unsigned char varbuf[16]; |
3620 | 0 | int varint_len; |
3621 | 0 | const unsigned hashsz = the_hash_algo->rawsz; |
3622 | |
|
3623 | 0 | CALLOC_ARRAY(ouc, 1); |
3624 | 0 | stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat); |
3625 | 0 | stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat); |
3626 | 0 | ouc->dir_flags = htonl(untracked->dir_flags); |
3627 | |
|
3628 | 0 | varint_len = encode_varint(untracked->ident.len, varbuf); |
3629 | 0 | strbuf_add(out, varbuf, varint_len); |
3630 | 0 | strbuf_addbuf(out, &untracked->ident); |
3631 | |
|
3632 | 0 | strbuf_add(out, ouc, sizeof(*ouc)); |
3633 | 0 | strbuf_add(out, untracked->ss_info_exclude.oid.hash, hashsz); |
3634 | 0 | strbuf_add(out, untracked->ss_excludes_file.oid.hash, hashsz); |
3635 | 0 | strbuf_add(out, untracked->exclude_per_dir, strlen(untracked->exclude_per_dir) + 1); |
3636 | 0 | FREE_AND_NULL(ouc); |
3637 | |
|
3638 | 0 | if (!untracked->root) { |
3639 | 0 | varint_len = encode_varint(0, varbuf); |
3640 | 0 | strbuf_add(out, varbuf, varint_len); |
3641 | 0 | return; |
3642 | 0 | } |
3643 | | |
3644 | 0 | wd.index = 0; |
3645 | 0 | wd.check_only = ewah_new(); |
3646 | 0 | wd.valid = ewah_new(); |
3647 | 0 | wd.sha1_valid = ewah_new(); |
3648 | 0 | strbuf_init(&wd.out, 1024); |
3649 | 0 | strbuf_init(&wd.sb_stat, 1024); |
3650 | 0 | strbuf_init(&wd.sb_sha1, 1024); |
3651 | 0 | write_one_dir(untracked->root, &wd); |
3652 | |
|
3653 | 0 | varint_len = encode_varint(wd.index, varbuf); |
3654 | 0 | strbuf_add(out, varbuf, varint_len); |
3655 | 0 | strbuf_addbuf(out, &wd.out); |
3656 | 0 | ewah_serialize_strbuf(wd.valid, out); |
3657 | 0 | ewah_serialize_strbuf(wd.check_only, out); |
3658 | 0 | ewah_serialize_strbuf(wd.sha1_valid, out); |
3659 | 0 | strbuf_addbuf(out, &wd.sb_stat); |
3660 | 0 | strbuf_addbuf(out, &wd.sb_sha1); |
3661 | 0 | strbuf_addch(out, '\0'); /* safe guard for string lists */ |
3662 | |
|
3663 | 0 | ewah_free(wd.valid); |
3664 | 0 | ewah_free(wd.check_only); |
3665 | 0 | ewah_free(wd.sha1_valid); |
3666 | 0 | strbuf_release(&wd.out); |
3667 | 0 | strbuf_release(&wd.sb_stat); |
3668 | 0 | strbuf_release(&wd.sb_sha1); |
3669 | 0 | } |
3670 | | |
3671 | | static void free_untracked(struct untracked_cache_dir *ucd) |
3672 | 0 | { |
3673 | 0 | int i; |
3674 | 0 | if (!ucd) |
3675 | 0 | return; |
3676 | 0 | for (i = 0; i < ucd->dirs_nr; i++) |
3677 | 0 | free_untracked(ucd->dirs[i]); |
3678 | 0 | for (i = 0; i < ucd->untracked_nr; i++) |
3679 | 0 | free(ucd->untracked[i]); |
3680 | 0 | free(ucd->untracked); |
3681 | 0 | free(ucd->dirs); |
3682 | 0 | free(ucd); |
3683 | 0 | } |
3684 | | |
3685 | | void free_untracked_cache(struct untracked_cache *uc) |
3686 | 0 | { |
3687 | 0 | if (!uc) |
3688 | 0 | return; |
3689 | | |
3690 | 0 | free(uc->exclude_per_dir_to_free); |
3691 | 0 | strbuf_release(&uc->ident); |
3692 | 0 | free_untracked(uc->root); |
3693 | 0 | free(uc); |
3694 | 0 | } |
3695 | | |
3696 | | struct read_data { |
3697 | | int index; |
3698 | | struct untracked_cache_dir **ucd; |
3699 | | struct ewah_bitmap *check_only; |
3700 | | struct ewah_bitmap *valid; |
3701 | | struct ewah_bitmap *sha1_valid; |
3702 | | const unsigned char *data; |
3703 | | const unsigned char *end; |
3704 | | }; |
3705 | | |
3706 | | static void stat_data_from_disk(struct stat_data *to, const unsigned char *data) |
3707 | 0 | { |
3708 | 0 | memcpy(to, data, sizeof(*to)); |
3709 | 0 | to->sd_ctime.sec = ntohl(to->sd_ctime.sec); |
3710 | 0 | to->sd_ctime.nsec = ntohl(to->sd_ctime.nsec); |
3711 | 0 | to->sd_mtime.sec = ntohl(to->sd_mtime.sec); |
3712 | 0 | to->sd_mtime.nsec = ntohl(to->sd_mtime.nsec); |
3713 | 0 | to->sd_dev = ntohl(to->sd_dev); |
3714 | 0 | to->sd_ino = ntohl(to->sd_ino); |
3715 | 0 | to->sd_uid = ntohl(to->sd_uid); |
3716 | 0 | to->sd_gid = ntohl(to->sd_gid); |
3717 | 0 | to->sd_size = ntohl(to->sd_size); |
3718 | 0 | } |
3719 | | |
3720 | | static int read_one_dir(struct untracked_cache_dir **untracked_, |
3721 | | struct read_data *rd) |
3722 | 0 | { |
3723 | 0 | struct untracked_cache_dir ud, *untracked; |
3724 | 0 | const unsigned char *data = rd->data, *end = rd->end; |
3725 | 0 | const unsigned char *eos; |
3726 | 0 | unsigned int value; |
3727 | 0 | int i; |
3728 | |
|
3729 | 0 | memset(&ud, 0, sizeof(ud)); |
3730 | |
|
3731 | 0 | value = decode_varint(&data); |
3732 | 0 | if (data > end) |
3733 | 0 | return -1; |
3734 | 0 | ud.recurse = 1; |
3735 | 0 | ud.untracked_alloc = value; |
3736 | 0 | ud.untracked_nr = value; |
3737 | 0 | if (ud.untracked_nr) |
3738 | 0 | ALLOC_ARRAY(ud.untracked, ud.untracked_nr); |
3739 | |
|
3740 | 0 | ud.dirs_alloc = ud.dirs_nr = decode_varint(&data); |
3741 | 0 | if (data > end) |
3742 | 0 | return -1; |
3743 | 0 | ALLOC_ARRAY(ud.dirs, ud.dirs_nr); |
3744 | |
|
3745 | 0 | eos = memchr(data, '\0', end - data); |
3746 | 0 | if (!eos || eos == end) |
3747 | 0 | return -1; |
3748 | | |
3749 | 0 | *untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1)); |
3750 | 0 | memcpy(untracked, &ud, sizeof(ud)); |
3751 | 0 | memcpy(untracked->name, data, eos - data + 1); |
3752 | 0 | data = eos + 1; |
3753 | |
|
3754 | 0 | for (i = 0; i < untracked->untracked_nr; i++) { |
3755 | 0 | eos = memchr(data, '\0', end - data); |
3756 | 0 | if (!eos || eos == end) |
3757 | 0 | return -1; |
3758 | 0 | untracked->untracked[i] = xmemdupz(data, eos - data); |
3759 | 0 | data = eos + 1; |
3760 | 0 | } |
3761 | | |
3762 | 0 | rd->ucd[rd->index++] = untracked; |
3763 | 0 | rd->data = data; |
3764 | |
|
3765 | 0 | for (i = 0; i < untracked->dirs_nr; i++) { |
3766 | 0 | if (read_one_dir(untracked->dirs + i, rd) < 0) |
3767 | 0 | return -1; |
3768 | 0 | } |
3769 | 0 | return 0; |
3770 | 0 | } |
3771 | | |
3772 | | static void set_check_only(size_t pos, void *cb) |
3773 | 0 | { |
3774 | 0 | struct read_data *rd = cb; |
3775 | 0 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
3776 | 0 | ud->check_only = 1; |
3777 | 0 | } |
3778 | | |
3779 | | static void read_stat(size_t pos, void *cb) |
3780 | 0 | { |
3781 | 0 | struct read_data *rd = cb; |
3782 | 0 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
3783 | 0 | if (rd->data + sizeof(struct stat_data) > rd->end) { |
3784 | 0 | rd->data = rd->end + 1; |
3785 | 0 | return; |
3786 | 0 | } |
3787 | 0 | stat_data_from_disk(&ud->stat_data, rd->data); |
3788 | 0 | rd->data += sizeof(struct stat_data); |
3789 | 0 | ud->valid = 1; |
3790 | 0 | } |
3791 | | |
3792 | | static void read_oid(size_t pos, void *cb) |
3793 | 0 | { |
3794 | 0 | struct read_data *rd = cb; |
3795 | 0 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
3796 | 0 | if (rd->data + the_hash_algo->rawsz > rd->end) { |
3797 | 0 | rd->data = rd->end + 1; |
3798 | 0 | return; |
3799 | 0 | } |
3800 | 0 | oidread(&ud->exclude_oid, rd->data, the_repository->hash_algo); |
3801 | 0 | rd->data += the_hash_algo->rawsz; |
3802 | 0 | } |
3803 | | |
3804 | | static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data, |
3805 | | const unsigned char *sha1) |
3806 | 0 | { |
3807 | 0 | stat_data_from_disk(&oid_stat->stat, data); |
3808 | 0 | oidread(&oid_stat->oid, sha1, the_repository->hash_algo); |
3809 | 0 | oid_stat->valid = 1; |
3810 | 0 | } |
3811 | | |
3812 | | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz) |
3813 | 0 | { |
3814 | 0 | struct untracked_cache *uc; |
3815 | 0 | struct read_data rd; |
3816 | 0 | const unsigned char *next = data, *end = (const unsigned char *)data + sz; |
3817 | 0 | const char *ident; |
3818 | 0 | int ident_len; |
3819 | 0 | ssize_t len; |
3820 | 0 | const char *exclude_per_dir; |
3821 | 0 | const unsigned hashsz = the_hash_algo->rawsz; |
3822 | 0 | const unsigned offset = sizeof(struct ondisk_untracked_cache); |
3823 | 0 | const unsigned exclude_per_dir_offset = offset + 2 * hashsz; |
3824 | |
|
3825 | 0 | if (sz <= 1 || end[-1] != '\0') |
3826 | 0 | return NULL; |
3827 | 0 | end--; |
3828 | |
|
3829 | 0 | ident_len = decode_varint(&next); |
3830 | 0 | if (next + ident_len > end) |
3831 | 0 | return NULL; |
3832 | 0 | ident = (const char *)next; |
3833 | 0 | next += ident_len; |
3834 | |
|
3835 | 0 | if (next + exclude_per_dir_offset + 1 > end) |
3836 | 0 | return NULL; |
3837 | | |
3838 | 0 | CALLOC_ARRAY(uc, 1); |
3839 | 0 | strbuf_init(&uc->ident, ident_len); |
3840 | 0 | strbuf_add(&uc->ident, ident, ident_len); |
3841 | 0 | load_oid_stat(&uc->ss_info_exclude, |
3842 | 0 | next + ouc_offset(info_exclude_stat), |
3843 | 0 | next + offset); |
3844 | 0 | load_oid_stat(&uc->ss_excludes_file, |
3845 | 0 | next + ouc_offset(excludes_file_stat), |
3846 | 0 | next + offset + hashsz); |
3847 | 0 | uc->dir_flags = get_be32(next + ouc_offset(dir_flags)); |
3848 | 0 | exclude_per_dir = (const char *)next + exclude_per_dir_offset; |
3849 | 0 | uc->exclude_per_dir = uc->exclude_per_dir_to_free = xstrdup(exclude_per_dir); |
3850 | | /* NUL after exclude_per_dir is covered by sizeof(*ouc) */ |
3851 | 0 | next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1; |
3852 | 0 | if (next >= end) |
3853 | 0 | goto done2; |
3854 | | |
3855 | 0 | len = decode_varint(&next); |
3856 | 0 | if (next > end || len == 0) |
3857 | 0 | goto done2; |
3858 | | |
3859 | 0 | rd.valid = ewah_new(); |
3860 | 0 | rd.check_only = ewah_new(); |
3861 | 0 | rd.sha1_valid = ewah_new(); |
3862 | 0 | rd.data = next; |
3863 | 0 | rd.end = end; |
3864 | 0 | rd.index = 0; |
3865 | 0 | ALLOC_ARRAY(rd.ucd, len); |
3866 | |
|
3867 | 0 | if (read_one_dir(&uc->root, &rd) || rd.index != len) |
3868 | 0 | goto done; |
3869 | | |
3870 | 0 | next = rd.data; |
3871 | 0 | len = ewah_read_mmap(rd.valid, next, end - next); |
3872 | 0 | if (len < 0) |
3873 | 0 | goto done; |
3874 | | |
3875 | 0 | next += len; |
3876 | 0 | len = ewah_read_mmap(rd.check_only, next, end - next); |
3877 | 0 | if (len < 0) |
3878 | 0 | goto done; |
3879 | | |
3880 | 0 | next += len; |
3881 | 0 | len = ewah_read_mmap(rd.sha1_valid, next, end - next); |
3882 | 0 | if (len < 0) |
3883 | 0 | goto done; |
3884 | | |
3885 | 0 | ewah_each_bit(rd.check_only, set_check_only, &rd); |
3886 | 0 | rd.data = next + len; |
3887 | 0 | ewah_each_bit(rd.valid, read_stat, &rd); |
3888 | 0 | ewah_each_bit(rd.sha1_valid, read_oid, &rd); |
3889 | 0 | next = rd.data; |
3890 | |
|
3891 | 0 | done: |
3892 | 0 | free(rd.ucd); |
3893 | 0 | ewah_free(rd.valid); |
3894 | 0 | ewah_free(rd.check_only); |
3895 | 0 | ewah_free(rd.sha1_valid); |
3896 | 0 | done2: |
3897 | 0 | if (next != end) { |
3898 | 0 | free_untracked_cache(uc); |
3899 | 0 | uc = NULL; |
3900 | 0 | } |
3901 | 0 | return uc; |
3902 | 0 | } |
3903 | | |
3904 | | static void invalidate_one_directory(struct untracked_cache *uc, |
3905 | | struct untracked_cache_dir *ucd) |
3906 | 0 | { |
3907 | 0 | uc->dir_invalidated++; |
3908 | 0 | ucd->valid = 0; |
3909 | 0 | ucd->untracked_nr = 0; |
3910 | 0 | } |
3911 | | |
3912 | | /* |
3913 | | * Normally when an entry is added or removed from a directory, |
3914 | | * invalidating that directory is enough. No need to touch its |
3915 | | * ancestors. When a directory is shown as "foo/bar/" in git-status |
3916 | | * however, deleting or adding an entry may have cascading effect. |
3917 | | * |
3918 | | * Say the "foo/bar/file" has become untracked, we need to tell the |
3919 | | * untracked_cache_dir of "foo" that "bar/" is not an untracked |
3920 | | * directory any more (because "bar" is managed by foo as an untracked |
3921 | | * "file"). |
3922 | | * |
3923 | | * Similarly, if "foo/bar/file" moves from untracked to tracked and it |
3924 | | * was the last untracked entry in the entire "foo", we should show |
3925 | | * "foo/" instead. Which means we have to invalidate past "bar" up to |
3926 | | * "foo". |
3927 | | * |
3928 | | * This function traverses all directories from root to leaf. If there |
3929 | | * is a chance of one of the above cases happening, we invalidate back |
3930 | | * to root. Otherwise we just invalidate the leaf. There may be a more |
3931 | | * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to |
3932 | | * detect these cases and avoid unnecessary invalidation, for example, |
3933 | | * checking for the untracked entry named "bar/" in "foo", but for now |
3934 | | * stick to something safe and simple. |
3935 | | */ |
3936 | | static int invalidate_one_component(struct untracked_cache *uc, |
3937 | | struct untracked_cache_dir *dir, |
3938 | | const char *path, int len) |
3939 | 0 | { |
3940 | 0 | const char *rest = strchr(path, '/'); |
3941 | |
|
3942 | 0 | if (rest) { |
3943 | 0 | int component_len = rest - path; |
3944 | 0 | struct untracked_cache_dir *d = |
3945 | 0 | lookup_untracked(uc, dir, path, component_len); |
3946 | 0 | int ret = |
3947 | 0 | invalidate_one_component(uc, d, rest + 1, |
3948 | 0 | len - (component_len + 1)); |
3949 | 0 | if (ret) |
3950 | 0 | invalidate_one_directory(uc, dir); |
3951 | 0 | return ret; |
3952 | 0 | } |
3953 | | |
3954 | 0 | invalidate_one_directory(uc, dir); |
3955 | 0 | return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES; |
3956 | 0 | } |
3957 | | |
3958 | | void untracked_cache_invalidate_path(struct index_state *istate, |
3959 | | const char *path, int safe_path) |
3960 | 0 | { |
3961 | 0 | if (!istate->untracked || !istate->untracked->root) |
3962 | 0 | return; |
3963 | 0 | if (!safe_path && !verify_path(path, 0)) |
3964 | 0 | return; |
3965 | 0 | invalidate_one_component(istate->untracked, istate->untracked->root, |
3966 | 0 | path, strlen(path)); |
3967 | 0 | } |
3968 | | |
3969 | | void untracked_cache_invalidate_trimmed_path(struct index_state *istate, |
3970 | | const char *path, |
3971 | | int safe_path) |
3972 | 0 | { |
3973 | 0 | size_t len = strlen(path); |
3974 | |
|
3975 | 0 | if (!len) |
3976 | 0 | BUG("untracked_cache_invalidate_trimmed_path given zero length path"); |
3977 | | |
3978 | 0 | if (path[len - 1] != '/') { |
3979 | 0 | untracked_cache_invalidate_path(istate, path, safe_path); |
3980 | 0 | } else { |
3981 | 0 | struct strbuf tmp = STRBUF_INIT; |
3982 | |
|
3983 | 0 | strbuf_add(&tmp, path, len - 1); |
3984 | 0 | untracked_cache_invalidate_path(istate, tmp.buf, safe_path); |
3985 | 0 | strbuf_release(&tmp); |
3986 | 0 | } |
3987 | 0 | } |
3988 | | |
3989 | | void untracked_cache_remove_from_index(struct index_state *istate, |
3990 | | const char *path) |
3991 | 0 | { |
3992 | 0 | untracked_cache_invalidate_path(istate, path, 1); |
3993 | 0 | } |
3994 | | |
3995 | | void untracked_cache_add_to_index(struct index_state *istate, |
3996 | | const char *path) |
3997 | 0 | { |
3998 | 0 | untracked_cache_invalidate_path(istate, path, 1); |
3999 | 0 | } |
4000 | | |
4001 | | static void connect_wt_gitdir_in_nested(const char *sub_worktree, |
4002 | | const char *sub_gitdir) |
4003 | 0 | { |
4004 | 0 | int i; |
4005 | 0 | struct repository subrepo; |
4006 | 0 | struct strbuf sub_wt = STRBUF_INIT; |
4007 | 0 | struct strbuf sub_gd = STRBUF_INIT; |
4008 | |
|
4009 | 0 | const struct submodule *sub; |
4010 | | |
4011 | | /* If the submodule has no working tree, we can ignore it. */ |
4012 | 0 | if (repo_init(&subrepo, sub_gitdir, sub_worktree)) |
4013 | 0 | return; |
4014 | | |
4015 | 0 | if (repo_read_index(&subrepo) < 0) |
4016 | 0 | die(_("index file corrupt in repo %s"), subrepo.gitdir); |
4017 | | |
4018 | | /* TODO: audit for interaction with sparse-index. */ |
4019 | 0 | ensure_full_index(subrepo.index); |
4020 | 0 | for (i = 0; i < subrepo.index->cache_nr; i++) { |
4021 | 0 | const struct cache_entry *ce = subrepo.index->cache[i]; |
4022 | |
|
4023 | 0 | if (!S_ISGITLINK(ce->ce_mode)) |
4024 | 0 | continue; |
4025 | | |
4026 | 0 | while (i + 1 < subrepo.index->cache_nr && |
4027 | 0 | !strcmp(ce->name, subrepo.index->cache[i + 1]->name)) |
4028 | | /* |
4029 | | * Skip entries with the same name in different stages |
4030 | | * to make sure an entry is returned only once. |
4031 | | */ |
4032 | 0 | i++; |
4033 | |
|
4034 | 0 | sub = submodule_from_path(&subrepo, null_oid(), ce->name); |
4035 | 0 | if (!sub || !is_submodule_active(&subrepo, ce->name)) |
4036 | | /* .gitmodules broken or inactive sub */ |
4037 | 0 | continue; |
4038 | | |
4039 | 0 | strbuf_reset(&sub_wt); |
4040 | 0 | strbuf_reset(&sub_gd); |
4041 | 0 | strbuf_addf(&sub_wt, "%s/%s", sub_worktree, sub->path); |
4042 | 0 | submodule_name_to_gitdir(&sub_gd, &subrepo, sub->name); |
4043 | |
|
4044 | 0 | connect_work_tree_and_git_dir(sub_wt.buf, sub_gd.buf, 1); |
4045 | 0 | } |
4046 | 0 | strbuf_release(&sub_wt); |
4047 | 0 | strbuf_release(&sub_gd); |
4048 | 0 | repo_clear(&subrepo); |
4049 | 0 | } |
4050 | | |
4051 | | void connect_work_tree_and_git_dir(const char *work_tree_, |
4052 | | const char *git_dir_, |
4053 | | int recurse_into_nested) |
4054 | 0 | { |
4055 | 0 | struct strbuf gitfile_sb = STRBUF_INIT; |
4056 | 0 | struct strbuf cfg_sb = STRBUF_INIT; |
4057 | 0 | struct strbuf rel_path = STRBUF_INIT; |
4058 | 0 | char *git_dir, *work_tree; |
4059 | | |
4060 | | /* Prepare .git file */ |
4061 | 0 | strbuf_addf(&gitfile_sb, "%s/.git", work_tree_); |
4062 | 0 | if (safe_create_leading_directories_const(gitfile_sb.buf)) |
4063 | 0 | die(_("could not create directories for %s"), gitfile_sb.buf); |
4064 | | |
4065 | | /* Prepare config file */ |
4066 | 0 | strbuf_addf(&cfg_sb, "%s/config", git_dir_); |
4067 | 0 | if (safe_create_leading_directories_const(cfg_sb.buf)) |
4068 | 0 | die(_("could not create directories for %s"), cfg_sb.buf); |
4069 | | |
4070 | 0 | git_dir = real_pathdup(git_dir_, 1); |
4071 | 0 | work_tree = real_pathdup(work_tree_, 1); |
4072 | | |
4073 | | /* Write .git file */ |
4074 | 0 | write_file(gitfile_sb.buf, "gitdir: %s", |
4075 | 0 | relative_path(git_dir, work_tree, &rel_path)); |
4076 | | /* Update core.worktree setting */ |
4077 | 0 | git_config_set_in_file(cfg_sb.buf, "core.worktree", |
4078 | 0 | relative_path(work_tree, git_dir, &rel_path)); |
4079 | |
|
4080 | 0 | strbuf_release(&gitfile_sb); |
4081 | 0 | strbuf_release(&cfg_sb); |
4082 | 0 | strbuf_release(&rel_path); |
4083 | |
|
4084 | 0 | if (recurse_into_nested) |
4085 | 0 | connect_wt_gitdir_in_nested(work_tree, git_dir); |
4086 | |
|
4087 | 0 | free(work_tree); |
4088 | 0 | free(git_dir); |
4089 | 0 | } |
4090 | | |
4091 | | /* |
4092 | | * Migrate the git directory of the given path from old_git_dir to new_git_dir. |
4093 | | */ |
4094 | | void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir) |
4095 | 0 | { |
4096 | 0 | if (rename(old_git_dir, new_git_dir) < 0) |
4097 | 0 | die_errno(_("could not migrate git directory from '%s' to '%s'"), |
4098 | 0 | old_git_dir, new_git_dir); |
4099 | | |
4100 | 0 | connect_work_tree_and_git_dir(path, new_git_dir, 0); |
4101 | 0 | } |
4102 | | |
4103 | | int path_match_flags(const char *const str, const enum path_match_flags flags) |
4104 | 0 | { |
4105 | 0 | const char *p = str; |
4106 | |
|
4107 | 0 | if (flags & PATH_MATCH_NATIVE && |
4108 | 0 | flags & PATH_MATCH_XPLATFORM) |
4109 | 0 | BUG("path_match_flags() must get one match kind, not multiple!"); |
4110 | 0 | else if (!(flags & PATH_MATCH_KINDS_MASK)) |
4111 | 0 | BUG("path_match_flags() must get at least one match kind!"); |
4112 | | |
4113 | 0 | if (flags & PATH_MATCH_STARTS_WITH_DOT_SLASH && |
4114 | 0 | flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH) |
4115 | 0 | BUG("path_match_flags() must get one platform kind, not multiple!"); |
4116 | 0 | else if (!(flags & PATH_MATCH_PLATFORM_MASK)) |
4117 | 0 | BUG("path_match_flags() must get at least one platform kind!"); |
4118 | | |
4119 | 0 | if (*p++ != '.') |
4120 | 0 | return 0; |
4121 | 0 | if (flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH && |
4122 | 0 | *p++ != '.') |
4123 | 0 | return 0; |
4124 | | |
4125 | 0 | if (flags & PATH_MATCH_NATIVE) |
4126 | 0 | return is_dir_sep(*p); |
4127 | 0 | else if (flags & PATH_MATCH_XPLATFORM) |
4128 | 0 | return is_xplatform_dir_sep(*p); |
4129 | 0 | BUG("unreachable"); |
4130 | 0 | } |