]>
Commit | Line | Data |
---|---|---|
453ec4bd LT |
1 | #ifndef DIR_H |
2 | #define DIR_H | |
3 | ||
96cc8ab5 | 4 | #include "hashmap.h" |
ac48adf4 EN |
5 | #include "pathspec.h" |
6 | #include "statinfo.h" | |
eb41775e JH |
7 | #include "strbuf.h" |
8 | ||
266f03ec HW |
9 | /** |
10 | * The directory listing API is used to enumerate paths in the work tree, | |
11 | * optionally taking `.git/info/exclude` and `.gitignore` files per directory | |
12 | * into account. | |
13 | */ | |
14 | ||
15 | /** | |
16 | * Calling sequence | |
17 | * ---------------- | |
18 | * | |
19 | * Note: The index may be checked for .gitignore files that are | |
20 | * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have | |
21 | * loaded the index first. | |
22 | * | |
eceba532 | 23 | * - Prepare `struct dir_struct dir` using `dir_init()` function. |
266f03ec HW |
24 | * |
25 | * - To add single exclude pattern, call `add_pattern_list()` and then | |
26 | * `add_pattern()`. | |
27 | * | |
28 | * - To add patterns from a file (e.g. `.git/info/exclude`), call | |
eceba532 | 29 | * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`. |
266f03ec | 30 | * |
eceba532 EN |
31 | * - A short-hand function `setup_standard_excludes()` can be used to set |
32 | * up the standard set of exclude settings, instead of manually calling | |
33 | * the add_pattern*() family of functions. | |
266f03ec | 34 | * |
eceba532 | 35 | * - Call `fill_directory()`. |
266f03ec | 36 | * |
eceba532 | 37 | * - Use `dir.entries[]` and `dir.ignored[]`. |
266f03ec | 38 | * |
eceba532 | 39 | * - Call `dir_clear()` when the contained elements are no longer in use. |
266f03ec HW |
40 | * |
41 | */ | |
42 | ||
453ec4bd | 43 | struct dir_entry { |
e96980ef | 44 | unsigned int len; |
453ec4bd LT |
45 | char name[FLEX_ARRAY]; /* more */ |
46 | }; | |
47 | ||
4ff89ee5 DS |
48 | #define PATTERN_FLAG_NODIR 1 |
49 | #define PATTERN_FLAG_ENDSWITH 4 | |
50 | #define PATTERN_FLAG_MUSTBEDIR 8 | |
51 | #define PATTERN_FLAG_NEGATIVE 16 | |
68492fc7 | 52 | |
ab8db613 | 53 | struct path_pattern { |
709359c8 | 54 | /* |
65edd96a | 55 | * This allows callers of last_matching_pattern() etc. |
709359c8 NTND |
56 | * to determine the origin of the matching pattern. |
57 | */ | |
caa3d554 | 58 | struct pattern_list *pl; |
709359c8 NTND |
59 | |
60 | const char *pattern; | |
61 | int patternlen; | |
62 | int nowildcardlen; | |
63 | const char *base; | |
64 | int baselen; | |
4ff89ee5 | 65 | unsigned flags; /* PATTERN_FLAG_* */ |
709359c8 NTND |
66 | |
67 | /* | |
68 | * Counting starts from 1 for line numbers in ignore files, | |
69 | * and from -1 decrementing for patterns from CLI args. | |
70 | */ | |
71 | int srcpos; | |
72 | }; | |
73 | ||
96cc8ab5 DS |
74 | /* used for hashmaps for cone patterns */ |
75 | struct pattern_entry { | |
76 | struct hashmap_entry ent; | |
77 | char *pattern; | |
78 | size_t patternlen; | |
79 | }; | |
80 | ||
95a68344 | 81 | /* |
c082df24 AS |
82 | * Each excludes file will be parsed into a fresh exclude_list which |
83 | * is appended to the relevant exclude_list_group (either EXC_DIRS or | |
84 | * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group | |
85 | * can also be used to represent the list of --exclude values passed | |
86 | * via CLI args. | |
95a68344 | 87 | */ |
caa3d554 | 88 | struct pattern_list { |
453ec4bd LT |
89 | int nr; |
90 | int alloc; | |
c04318e4 | 91 | |
c082df24 AS |
92 | /* remember pointer to exclude file contents so we can free() */ |
93 | char *filebuf; | |
94 | ||
c04318e4 AS |
95 | /* origin of list, e.g. path to filename, or descriptive string */ |
96 | const char *src; | |
97 | ||
ab8db613 | 98 | struct path_pattern **patterns; |
96cc8ab5 DS |
99 | |
100 | /* | |
101 | * While scanning the excludes, we attempt to match the patterns | |
102 | * with a more restricted set that allows us to use hashsets for | |
103 | * matching logic, which is faster than the linear lookup in the | |
104 | * excludes array above. If non-zero, that check succeeded. | |
105 | */ | |
106 | unsigned use_cone_patterns; | |
107 | unsigned full_cone; | |
108 | ||
109 | /* | |
110 | * Stores paths where everything starting with those paths | |
111 | * is included. | |
112 | */ | |
113 | struct hashmap recursive_hashmap; | |
114 | ||
115 | /* | |
116 | * Used to check single-level parents of blobs. | |
117 | */ | |
118 | struct hashmap parent_hashmap; | |
453ec4bd LT |
119 | }; |
120 | ||
95a68344 AS |
121 | /* |
122 | * The contents of the per-directory exclude files are lazily read on | |
123 | * demand and then cached in memory, one per exclude_stack struct, in | |
124 | * order to avoid opening and parsing each one every time that | |
125 | * directory is traversed. | |
126 | */ | |
63d285c8 | 127 | struct exclude_stack { |
95a68344 | 128 | struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */ |
63d285c8 | 129 | int baselen; |
c082df24 | 130 | int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */ |
0dcb8d7f | 131 | struct untracked_cache_dir *ucd; |
c082df24 AS |
132 | }; |
133 | ||
134 | struct exclude_list_group { | |
135 | int nr, alloc; | |
caa3d554 | 136 | struct pattern_list *pl; |
63d285c8 JH |
137 | }; |
138 | ||
4b33e602 | 139 | struct oid_stat { |
55fe6f51 | 140 | struct stat_data stat; |
4b33e602 | 141 | struct object_id oid; |
55fe6f51 NTND |
142 | int valid; |
143 | }; | |
144 | ||
0dcb8d7f NTND |
145 | /* |
146 | * Untracked cache | |
147 | * | |
148 | * The following inputs are sufficient to determine what files in a | |
149 | * directory are excluded: | |
150 | * | |
151 | * - The list of files and directories of the directory in question | |
152 | * - The $GIT_DIR/index | |
153 | * - dir_struct flags | |
154 | * - The content of $GIT_DIR/info/exclude | |
155 | * - The content of core.excludesfile | |
156 | * - The content (or the lack) of .gitignore of all parent directories | |
157 | * from $GIT_WORK_TREE | |
158 | * - The check_only flag in read_directory_recursive (for | |
159 | * DIR_HIDE_EMPTY_DIRECTORIES) | |
160 | * | |
161 | * The first input can be checked using directory mtime. In many | |
162 | * filesystems, directory mtime (stat_data field) is updated when its | |
163 | * files or direct subdirs are added or removed. | |
164 | * | |
165 | * The second one can be hooked from cache_tree_invalidate_path(). | |
166 | * Whenever a file (or a submodule) is added or removed from a | |
167 | * directory, we invalidate that directory. | |
168 | * | |
169 | * The remaining inputs are easy, their SHA-1 could be used to verify | |
170 | * their contents (exclude_sha1[], info_exclude_sha1[] and | |
171 | * excludes_file_sha1[]) | |
172 | */ | |
173 | struct untracked_cache_dir { | |
174 | struct untracked_cache_dir **dirs; | |
175 | char **untracked; | |
176 | struct stat_data stat_data; | |
177 | unsigned int untracked_alloc, dirs_nr, dirs_alloc; | |
178 | unsigned int untracked_nr; | |
179 | unsigned int check_only : 1; | |
26cb0182 | 180 | /* all data except 'dirs' in this struct are good */ |
ccad261f | 181 | unsigned int valid : 1; |
26cb0182 | 182 | unsigned int recurse : 1; |
70c369cd | 183 | /* null object ID means this directory does not have .gitignore */ |
184 | struct object_id exclude_oid; | |
0dcb8d7f NTND |
185 | char name[FLEX_ARRAY]; |
186 | }; | |
187 | ||
188 | struct untracked_cache { | |
4b33e602 PO |
189 | struct oid_stat ss_info_exclude; |
190 | struct oid_stat ss_excludes_file; | |
0dcb8d7f | 191 | const char *exclude_per_dir; |
083fd1a2 | 192 | char *exclude_per_dir_to_free; |
1e8fef60 | 193 | struct strbuf ident; |
0dcb8d7f NTND |
194 | /* |
195 | * dir_struct#flags must match dir_flags or the untracked | |
196 | * cache is ignored. | |
197 | */ | |
198 | unsigned dir_flags; | |
199 | struct untracked_cache_dir *root; | |
200 | /* Statistics */ | |
201 | int dir_created; | |
ccad261f | 202 | int gitignore_invalidated; |
91a2288b NTND |
203 | int dir_invalidated; |
204 | int dir_opened; | |
883e248b BP |
205 | /* fsmonitor invalidation data */ |
206 | unsigned int use_fsmonitor : 1; | |
0dcb8d7f NTND |
207 | }; |
208 | ||
266f03ec HW |
209 | /** |
210 | * structure is used to pass directory traversal options to the library and to | |
211 | * record the paths discovered. A single `struct dir_struct` is used regardless | |
212 | * of whether or not the traversal recursively descends into subdirectories. | |
213 | */ | |
453ec4bd | 214 | struct dir_struct { |
266f03ec | 215 | |
266f03ec | 216 | /* bit-field of options */ |
7c4c97c0 | 217 | enum { |
266f03ec HW |
218 | |
219 | /** | |
220 | * Return just ignored files in `entries[]`, not untracked files. | |
221 | * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`. | |
222 | */ | |
7c4c97c0 | 223 | DIR_SHOW_IGNORED = 1<<0, |
266f03ec HW |
224 | |
225 | /* Include a directory that is not tracked. */ | |
7c4c97c0 | 226 | DIR_SHOW_OTHER_DIRECTORIES = 1<<1, |
266f03ec HW |
227 | |
228 | /* Do not include a directory that is not tracked and is empty. */ | |
7c4c97c0 | 229 | DIR_HIDE_EMPTY_DIRECTORIES = 1<<2, |
266f03ec HW |
230 | |
231 | /** | |
232 | * If set, recurse into a directory that looks like a Git directory. | |
233 | * Otherwise it is shown as a directory. | |
234 | */ | |
7c4c97c0 | 235 | DIR_NO_GITLINKS = 1<<3, |
266f03ec HW |
236 | |
237 | /** | |
238 | * Special mode for git-add. Return ignored files in `ignored[]` and | |
239 | * untracked files in `entries[]`. Only returns ignored files that match | |
240 | * pathspec exactly (no wildcards). Does not recurse into ignored | |
241 | * directories. | |
242 | */ | |
0aaf62b6 | 243 | DIR_COLLECT_IGNORED = 1<<4, |
266f03ec HW |
244 | |
245 | /** | |
246 | * Similar to `DIR_SHOW_IGNORED`, but return ignored files in | |
247 | * `ignored[]` in addition to untracked files in `entries[]`. | |
248 | * This flag is mutually exclusive with `DIR_SHOW_IGNORED`. | |
249 | */ | |
2eac2a4c | 250 | DIR_SHOW_IGNORED_TOO = 1<<5, |
266f03ec | 251 | |
fb898888 | 252 | DIR_COLLECT_KILLED_ONLY = 1<<6, |
266f03ec HW |
253 | |
254 | /** | |
255 | * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is | |
256 | * set, the untracked contents of untracked directories are also | |
257 | * returned in `entries[]`. | |
258 | */ | |
eec0f7f2 | 259 | DIR_KEEP_UNTRACKED_CONTENTS = 1<<7, |
266f03ec HW |
260 | |
261 | /** | |
262 | * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is | |
263 | * set, returns ignored files and directories that match an exclude | |
264 | * pattern. If a directory matches an exclude pattern, then the | |
265 | * directory is returned and the contained paths are not. A directory | |
266 | * that does not match an exclude pattern will not be returned even if | |
267 | * all of its contents are ignored. In this case, the contents are | |
268 | * returned as individual entries. | |
269 | * | |
270 | * If this is set, files and directories that explicitly match an ignore | |
271 | * pattern are reported. Implicitly ignored directories (directories that | |
272 | * do not match an ignore pattern, but whose contents are all ignored) | |
273 | * are not reported, instead all of the contents are reported. | |
274 | */ | |
09487f2c | 275 | DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8, |
266f03ec | 276 | |
09487f2c | 277 | DIR_SKIP_NESTED_GIT = 1<<9 |
7c4c97c0 | 278 | } flags; |
266f03ec | 279 | |
d144a9d3 EN |
280 | /* The number of members in `entries[]` array. */ |
281 | int nr; /* output only */ | |
282 | ||
283 | /* The number of members in `ignored[]` array. */ | |
284 | int ignored_nr; /* output only */ | |
285 | ||
266f03ec | 286 | /* An array of `struct dir_entry`, each element of which describes a path. */ |
d144a9d3 | 287 | struct dir_entry **entries; /* output only */ |
266f03ec HW |
288 | |
289 | /** | |
290 | * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and | |
291 | * `DIR_COLLECT_IGNORED` flags. | |
292 | */ | |
d144a9d3 | 293 | struct dir_entry **ignored; /* output only */ |
453ec4bd | 294 | |
5fdf285e EN |
295 | /* Enable/update untracked file cache if set */ |
296 | struct untracked_cache *untracked; | |
297 | ||
266f03ec | 298 | /** |
59e009bf EN |
299 | * Deprecated: ls-files is the only allowed caller; all other callers |
300 | * should leave this as NULL; it pre-dated the | |
301 | * setup_standard_excludes() mechanism that replaces this. | |
302 | * | |
303 | * This field tracks the name of the file to be read in each directory | |
304 | * for excluded files (typically `.gitignore`). | |
266f03ec | 305 | */ |
453ec4bd | 306 | const char *exclude_per_dir; |
c082df24 | 307 | |
5fdf285e EN |
308 | struct dir_struct_internal { |
309 | /* Keeps track of allocation of `entries[]` array.*/ | |
310 | int alloc; | |
311 | ||
312 | /* Keeps track of allocation of `ignored[]` array. */ | |
313 | int ignored_alloc; | |
314 | ||
315 | /* | |
316 | * We maintain three groups of exclude pattern lists: | |
317 | * | |
318 | * EXC_CMDL lists patterns explicitly given on the command line. | |
319 | * EXC_DIRS lists patterns obtained from per-directory ignore | |
320 | * files. | |
321 | * EXC_FILE lists patterns from fallback ignore files, e.g. | |
322 | * - .git/info/exclude | |
323 | * - core.excludesfile | |
324 | * | |
325 | * Each group contains multiple exclude lists, a single list | |
326 | * per source. | |
327 | */ | |
63d285c8 JH |
328 | #define EXC_CMDL 0 |
329 | #define EXC_DIRS 1 | |
330 | #define EXC_FILE 2 | |
5fdf285e | 331 | struct exclude_list_group exclude_list_group[3]; |
7fe1ffda | 332 | |
5fdf285e EN |
333 | /* |
334 | * Temporary variables which are used during loading of the | |
335 | * per-directory exclude lists. | |
336 | * | |
337 | * exclude_stack points to the top of the exclude_stack, and | |
338 | * basebuf contains the full path to the current | |
339 | * (sub)directory in the traversal. Exclude points to the | |
340 | * matching exclude struct if the directory is excluded. | |
341 | */ | |
342 | struct exclude_stack *exclude_stack; | |
343 | struct path_pattern *pattern; | |
344 | struct strbuf basebuf; | |
345 | ||
346 | /* Additional metadata related to 'untracked' */ | |
347 | struct oid_stat ss_info_exclude; | |
348 | struct oid_stat ss_excludes_file; | |
349 | unsigned unmanaged_exclude_files; | |
350 | ||
351 | /* Stats about the traversal */ | |
352 | unsigned visited_paths; | |
353 | unsigned visited_directories; | |
354 | } internal; | |
453ec4bd LT |
355 | }; |
356 | ||
ce93a4c6 ÆAB |
357 | #define DIR_INIT { 0 } |
358 | ||
b548f0f1 EN |
359 | struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp); |
360 | ||
e0556a92 | 361 | /*Count the number of slashes for string s*/ |
55454427 | 362 | int count_slashes(const char *s); |
e0556a92 | 363 | |
52ed1894 AS |
364 | /* |
365 | * The ordering of these constants is significant, with | |
366 | * higher-numbered match types signifying "closer" (i.e. more | |
367 | * specific) matches which will override lower-numbered match types | |
368 | * when populating the seen[] array. | |
369 | */ | |
e813d50e | 370 | #define MATCHED_RECURSIVELY 1 |
89a1f4aa EN |
371 | #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2 |
372 | #define MATCHED_FNMATCH 3 | |
373 | #define MATCHED_EXACTLY 4 | |
55454427 DL |
374 | int simple_length(const char *match); |
375 | int no_wildcard(const char *string); | |
376 | char *common_prefix(const struct pathspec *pathspec); | |
4aeeef37 | 377 | int report_path_error(const char *ps_matched, const struct pathspec *pathspec); |
55454427 | 378 | int within_depth(const char *name, int namelen, int depth, int max_depth); |
3c6a370b | 379 | |
55454427 | 380 | int fill_directory(struct dir_struct *dir, |
ad6dad09 DL |
381 | struct index_state *istate, |
382 | const struct pathspec *pathspec); | |
55454427 | 383 | int read_directory(struct dir_struct *, struct index_state *istate, |
ad6dad09 DL |
384 | const char *path, int len, |
385 | const struct pathspec *pathspec); | |
f8a9d428 | 386 | |
468ce99b DS |
387 | enum pattern_match_result { |
388 | UNDECIDED = -1, | |
389 | NOT_MATCHED = 0, | |
390 | MATCHED = 1, | |
eb42feca | 391 | MATCHED_RECURSIVE = 2, |
468ce99b DS |
392 | }; |
393 | ||
394 | /* | |
395 | * Scan the list of patterns to determine if the ordered list | |
396 | * of patterns matches on 'pathname'. | |
397 | * | |
398 | * Return 1 for a match, 0 for not matched and -1 for undecided. | |
399 | */ | |
400 | enum pattern_match_result path_matches_pattern_list(const char *pathname, | |
401 | int pathlen, | |
402 | const char *basename, int *dtype, | |
403 | struct pattern_list *pl, | |
404 | struct index_state *istate); | |
02155c8c DS |
405 | |
406 | int init_sparse_checkout_patterns(struct index_state *state); | |
407 | ||
408 | int path_in_sparse_checkout(const char *path, | |
409 | struct index_state *istate); | |
410 | int path_in_cone_mode_sparse_checkout(const char *path, | |
411 | struct index_state *istate); | |
412 | ||
9e58beca BW |
413 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, |
414 | struct index_state *istate, | |
415 | const char *pathname, int len); | |
eb41775e | 416 | |
82dce998 NTND |
417 | /* |
418 | * these implement the matching logic for dir.c:excluded_from_list and | |
419 | * attr.c:path_matches() | |
420 | */ | |
55454427 | 421 | int match_basename(const char *, int, |
ad6dad09 | 422 | const char *, int, int, unsigned); |
55454427 | 423 | int match_pathname(const char *, int, |
ad6dad09 | 424 | const char *, int, |
77651c03 | 425 | const char *, int, int); |
82dce998 | 426 | |
65edd96a DS |
427 | struct path_pattern *last_matching_pattern(struct dir_struct *dir, |
428 | struct index_state *istate, | |
429 | const char *name, int *dtype); | |
782cd4c0 | 430 | |
55454427 | 431 | int is_excluded(struct dir_struct *dir, |
ad6dad09 DL |
432 | struct index_state *istate, |
433 | const char *name, int *dtype); | |
eb41775e | 434 | |
af09ce24 DS |
435 | int pl_hashmap_cmp(const void *unused_cmp_data, |
436 | const struct hashmap_entry *a, | |
437 | const struct hashmap_entry *b, | |
438 | const void *key); | |
96cc8ab5 DS |
439 | int hashmap_contains_parent(struct hashmap *map, |
440 | const char *path, | |
441 | struct strbuf *buffer); | |
65edd96a | 442 | struct pattern_list *add_pattern_list(struct dir_struct *dir, |
ad6dad09 | 443 | int group_type, const char *src); |
65edd96a | 444 | int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen, |
1679d60b JK |
445 | struct pattern_list *pl, struct index_state *istate, |
446 | unsigned flags); | |
65edd96a DS |
447 | void add_patterns_from_file(struct dir_struct *, const char *fname); |
448 | int add_patterns_from_blob_to_list(struct object_id *oid, | |
ad6dad09 | 449 | const char *base, int baselen, |
caa3d554 | 450 | struct pattern_list *pl); |
65edd96a DS |
451 | void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen); |
452 | void add_pattern(const char *string, const char *base, | |
caa3d554 | 453 | int baselen, struct pattern_list *pl, int srcpos); |
65edd96a | 454 | void clear_pattern_list(struct pattern_list *pl); |
eceba532 | 455 | void dir_clear(struct dir_struct *dir); |
0488481e NTND |
456 | |
457 | int repo_file_exists(struct repository *repo, const char *path); | |
458 | int file_exists(const char *); | |
453ec4bd | 459 | |
55454427 DL |
460 | int is_inside_dir(const char *dir); |
461 | int dir_inside_of(const char *subdir, const char *dir); | |
e6636747 | 462 | |
8ca12c0d AP |
463 | static inline int is_dot_or_dotdot(const char *name) |
464 | { | |
465 | return (name[0] == '.' && | |
466 | (name[1] == '\0' || | |
467 | (name[1] == '.' && name[2] == '\0'))); | |
468 | } | |
469 | ||
55454427 | 470 | int is_empty_dir(const char *dir); |
55892d23 | 471 | |
ed86301f AR |
472 | /* |
473 | * Retrieve the "humanish" basename of the given Git URL. | |
474 | * | |
475 | * For example: | |
476 | * /path/to/repo.git => "repo" | |
477 | * host.xz:foo/.git => "foo" | |
478 | * http://example.com/user/bar.baz => "bar.baz" | |
479 | */ | |
480 | char *git_url_basename(const char *repo, int is_bundle, int is_bare); | |
481 | void strip_dir_trailing_slashes(char *dir); | |
482 | ||
55454427 | 483 | void setup_standard_excludes(struct dir_struct *dir); |
a0f4afbe | 484 | |
dd23022a DS |
485 | char *get_sparse_checkout_filename(void); |
486 | int get_sparse_checkout_patterns(struct pattern_list *pl); | |
728af283 MH |
487 | |
488 | /* Constants for remove_dir_recursively: */ | |
489 | ||
490 | /* | |
491 | * If a non-directory is found within path, stop and return an error. | |
492 | * (In this case some empty directories might already have been | |
493 | * removed.) | |
494 | */ | |
a0f4afbe | 495 | #define REMOVE_DIR_EMPTY_ONLY 01 |
728af283 MH |
496 | |
497 | /* | |
498 | * If any Git work trees are found within path, skip them without | |
499 | * considering it an error. | |
500 | */ | |
a0f4afbe | 501 | #define REMOVE_DIR_KEEP_NESTED_GIT 02 |
728af283 MH |
502 | |
503 | /* Remove the contents of path, but leave path itself. */ | |
c844a803 | 504 | #define REMOVE_DIR_KEEP_TOPLEVEL 04 |
728af283 | 505 | |
580a5d7f EN |
506 | /* Remove the_original_cwd too */ |
507 | #define REMOVE_DIR_PURGE_ORIGINAL_CWD 0x08 | |
508 | ||
728af283 MH |
509 | /* |
510 | * Remove path and its contents, recursively. flags is a combination | |
511 | * of the above REMOVE_DIR_* constants. Return 0 on success. | |
512 | * | |
513 | * This function uses path as temporary scratch space, but restores it | |
514 | * before returning. | |
515 | */ | |
55454427 | 516 | int remove_dir_recursively(struct strbuf *path, int flag); |
7155b727 | 517 | |
63bbe8be EN |
518 | /* |
519 | * Tries to remove the path, along with leading empty directories so long as | |
520 | * those empty directories are not startup_info->original_cwd. Ignores | |
521 | * ENOENT. | |
522 | */ | |
55454427 | 523 | int remove_path(const char *path); |
4a92d1bf | 524 | |
55454427 | 525 | int fspathcmp(const char *a, const char *b); |
cf2dc1c2 | 526 | int fspatheq(const char *a, const char *b); |
55454427 | 527 | int fspathncmp(const char *a, const char *b, size_t count); |
cf2dc1c2 | 528 | unsigned int fspathhash(const char *str); |
8cf2a84e | 529 | |
5d74762d NTND |
530 | /* |
531 | * The prefix part of pattern must not contains wildcards. | |
532 | */ | |
bd30c2e4 | 533 | struct pathspec_item; |
55454427 | 534 | int git_fnmatch(const struct pathspec_item *item, |
ad6dad09 DL |
535 | const char *pattern, const char *string, |
536 | int prefix); | |
5d74762d | 537 | |
847a9e5d | 538 | int submodule_path_match(struct index_state *istate, |
ad6dad09 DL |
539 | const struct pathspec *ps, |
540 | const char *submodule_name, | |
541 | char *seen); | |
75a6315f | 542 | |
847a9e5d | 543 | static inline int dir_path_match(struct index_state *istate, |
6d2df284 | 544 | const struct dir_entry *ent, |
ebb32893 NTND |
545 | const struct pathspec *pathspec, |
546 | int prefix, char *seen) | |
547 | { | |
ae8d0824 NTND |
548 | int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/'; |
549 | int len = has_trailing_dir ? ent->len - 1 : ent->len; | |
6d2df284 | 550 | return match_pathspec(istate, pathspec, ent->name, len, prefix, seen, |
ae8d0824 | 551 | has_trailing_dir); |
ebb32893 NTND |
552 | } |
553 | ||
bbf504a9 SL |
554 | int cmp_dir_entry(const void *p1, const void *p2); |
555 | int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in); | |
556 | ||
0cacebf0 | 557 | void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path); |
e931371a NTND |
558 | void untracked_cache_remove_from_index(struct index_state *, const char *); |
559 | void untracked_cache_add_to_index(struct index_state *, const char *); | |
560 | ||
f9e6c649 NTND |
561 | void free_untracked_cache(struct untracked_cache *); |
562 | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz); | |
83c094ad | 563 | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked); |
4a4ca479 | 564 | void add_untracked_cache(struct index_state *istate); |
07b29bfd | 565 | void remove_untracked_cache(struct index_state *istate); |
da62f786 SB |
566 | |
567 | /* | |
568 | * Connect a worktree to a git directory by creating (or overwriting) a | |
569 | * '.git' file containing the location of the git directory. In the git | |
570 | * directory set the core.worktree setting to indicate where the worktree is. | |
571 | * When `recurse_into_nested` is set, recurse into any nested submodules, | |
572 | * connecting them as well. | |
573 | */ | |
55454427 | 574 | void connect_work_tree_and_git_dir(const char *work_tree, |
ad6dad09 DL |
575 | const char *git_dir, |
576 | int recurse_into_nested); | |
55454427 | 577 | void relocate_gitdir(const char *path, |
ad6dad09 DL |
578 | const char *old_git_dir, |
579 | const char *new_git_dir); | |
9fd512c8 ÆAB |
580 | |
581 | /** | |
582 | * The "enum path_matches_kind" determines how path_match_flags() will | |
583 | * behave. The flags come in sets, and one (and only one) must be | |
584 | * provided out of each "set": | |
585 | * | |
586 | * PATH_MATCH_NATIVE: | |
587 | * Path separator is is_dir_sep() | |
588 | * PATH_MATCH_XPLATFORM: | |
589 | * Path separator is is_xplatform_dir_sep() | |
590 | * | |
591 | * Do we use is_dir_sep() to check for a directory separator | |
592 | * (*_NATIVE), or do we always check for '/' or '\' (*_XPLATFORM). The | |
593 | * "*_NATIVE" version on Windows is the same as "*_XPLATFORM", | |
594 | * everywhere else "*_NATIVE" means "only /". | |
595 | * | |
596 | * PATH_MATCH_STARTS_WITH_DOT_SLASH: | |
597 | * Match a path starting with "./" | |
598 | * PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH: | |
599 | * Match a path starting with "../" | |
600 | * | |
601 | * The "/" in the above is adjusted based on the "*_NATIVE" and | |
602 | * "*_XPLATFORM" flags. | |
603 | */ | |
604 | enum path_match_flags { | |
605 | PATH_MATCH_NATIVE = 1 << 0, | |
606 | PATH_MATCH_XPLATFORM = 1 << 1, | |
607 | PATH_MATCH_STARTS_WITH_DOT_SLASH = 1 << 2, | |
608 | PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH = 1 << 3, | |
609 | }; | |
610 | #define PATH_MATCH_KINDS_MASK (PATH_MATCH_STARTS_WITH_DOT_SLASH | \ | |
611 | PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH) | |
612 | #define PATH_MATCH_PLATFORM_MASK (PATH_MATCH_NATIVE | PATH_MATCH_XPLATFORM) | |
613 | ||
614 | /** | |
615 | * path_match_flags() checks if a given "path" matches a given "enum | |
616 | * path_match_flags" criteria. | |
617 | */ | |
618 | int path_match_flags(const char *const path, const enum path_match_flags f); | |
619 | ||
620 | /** | |
621 | * starts_with_dot_slash_native(): convenience wrapper for | |
622 | * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_SLASH and | |
623 | * PATH_MATCH_NATIVE. | |
624 | */ | |
625 | static inline int starts_with_dot_slash_native(const char *const path) | |
626 | { | |
627 | const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_SLASH; | |
628 | ||
629 | return path_match_flags(path, what | PATH_MATCH_NATIVE); | |
630 | } | |
631 | ||
632 | /** | |
633 | * starts_with_dot_slash_native(): convenience wrapper for | |
634 | * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH and | |
635 | * PATH_MATCH_NATIVE. | |
636 | */ | |
637 | static inline int starts_with_dot_dot_slash_native(const char *const path) | |
638 | { | |
639 | const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH; | |
640 | ||
641 | return path_match_flags(path, what | PATH_MATCH_NATIVE); | |
642 | } | |
592fc5b3 EN |
643 | |
644 | #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT) | |
645 | #define DTYPE(de) ((de)->d_type) | |
646 | #else | |
647 | #undef DT_UNKNOWN | |
648 | #undef DT_DIR | |
649 | #undef DT_REG | |
650 | #undef DT_LNK | |
651 | #define DT_UNKNOWN 0 | |
652 | #define DT_DIR 1 | |
653 | #define DT_REG 2 | |
654 | #define DT_LNK 3 | |
655 | #define DTYPE(de) DT_UNKNOWN | |
656 | #endif | |
657 | ||
453ec4bd | 658 | #endif |