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