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