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