]>
Commit | Line | Data |
---|---|---|
453ec4bd LT |
1 | #ifndef DIR_H |
2 | #define DIR_H | |
3 | ||
95a68344 AS |
4 | /* See Documentation/technical/api-directory-listing.txt */ |
5 | ||
70c369cd | 6 | #include "cache.h" |
96cc8ab5 | 7 | #include "hashmap.h" |
eb41775e JH |
8 | #include "strbuf.h" |
9 | ||
453ec4bd | 10 | struct dir_entry { |
e96980ef | 11 | unsigned int len; |
453ec4bd LT |
12 | char name[FLEX_ARRAY]; /* more */ |
13 | }; | |
14 | ||
4ff89ee5 DS |
15 | #define PATTERN_FLAG_NODIR 1 |
16 | #define PATTERN_FLAG_ENDSWITH 4 | |
17 | #define PATTERN_FLAG_MUSTBEDIR 8 | |
18 | #define PATTERN_FLAG_NEGATIVE 16 | |
68492fc7 | 19 | |
ab8db613 | 20 | struct path_pattern { |
709359c8 | 21 | /* |
65edd96a | 22 | * This allows callers of last_matching_pattern() etc. |
709359c8 NTND |
23 | * to determine the origin of the matching pattern. |
24 | */ | |
caa3d554 | 25 | struct pattern_list *pl; |
709359c8 NTND |
26 | |
27 | const char *pattern; | |
28 | int patternlen; | |
29 | int nowildcardlen; | |
30 | const char *base; | |
31 | int baselen; | |
4ff89ee5 | 32 | unsigned flags; /* PATTERN_FLAG_* */ |
709359c8 NTND |
33 | |
34 | /* | |
35 | * Counting starts from 1 for line numbers in ignore files, | |
36 | * and from -1 decrementing for patterns from CLI args. | |
37 | */ | |
38 | int srcpos; | |
39 | }; | |
40 | ||
96cc8ab5 DS |
41 | /* used for hashmaps for cone patterns */ |
42 | struct pattern_entry { | |
43 | struct hashmap_entry ent; | |
44 | char *pattern; | |
45 | size_t patternlen; | |
46 | }; | |
47 | ||
95a68344 | 48 | /* |
c082df24 AS |
49 | * Each excludes file will be parsed into a fresh exclude_list which |
50 | * is appended to the relevant exclude_list_group (either EXC_DIRS or | |
51 | * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group | |
52 | * can also be used to represent the list of --exclude values passed | |
53 | * via CLI args. | |
95a68344 | 54 | */ |
caa3d554 | 55 | struct pattern_list { |
453ec4bd LT |
56 | int nr; |
57 | int alloc; | |
c04318e4 | 58 | |
c082df24 AS |
59 | /* remember pointer to exclude file contents so we can free() */ |
60 | char *filebuf; | |
61 | ||
c04318e4 AS |
62 | /* origin of list, e.g. path to filename, or descriptive string */ |
63 | const char *src; | |
64 | ||
ab8db613 | 65 | struct path_pattern **patterns; |
96cc8ab5 DS |
66 | |
67 | /* | |
68 | * While scanning the excludes, we attempt to match the patterns | |
69 | * with a more restricted set that allows us to use hashsets for | |
70 | * matching logic, which is faster than the linear lookup in the | |
71 | * excludes array above. If non-zero, that check succeeded. | |
72 | */ | |
73 | unsigned use_cone_patterns; | |
74 | unsigned full_cone; | |
75 | ||
76 | /* | |
77 | * Stores paths where everything starting with those paths | |
78 | * is included. | |
79 | */ | |
80 | struct hashmap recursive_hashmap; | |
81 | ||
82 | /* | |
83 | * Used to check single-level parents of blobs. | |
84 | */ | |
85 | struct hashmap parent_hashmap; | |
453ec4bd LT |
86 | }; |
87 | ||
95a68344 AS |
88 | /* |
89 | * The contents of the per-directory exclude files are lazily read on | |
90 | * demand and then cached in memory, one per exclude_stack struct, in | |
91 | * order to avoid opening and parsing each one every time that | |
92 | * directory is traversed. | |
93 | */ | |
63d285c8 | 94 | struct exclude_stack { |
95a68344 | 95 | struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */ |
63d285c8 | 96 | int baselen; |
c082df24 | 97 | int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */ |
0dcb8d7f | 98 | struct untracked_cache_dir *ucd; |
c082df24 AS |
99 | }; |
100 | ||
101 | struct exclude_list_group { | |
102 | int nr, alloc; | |
caa3d554 | 103 | struct pattern_list *pl; |
63d285c8 JH |
104 | }; |
105 | ||
4b33e602 | 106 | struct oid_stat { |
55fe6f51 | 107 | struct stat_data stat; |
4b33e602 | 108 | struct object_id oid; |
55fe6f51 NTND |
109 | int valid; |
110 | }; | |
111 | ||
0dcb8d7f NTND |
112 | /* |
113 | * Untracked cache | |
114 | * | |
115 | * The following inputs are sufficient to determine what files in a | |
116 | * directory are excluded: | |
117 | * | |
118 | * - The list of files and directories of the directory in question | |
119 | * - The $GIT_DIR/index | |
120 | * - dir_struct flags | |
121 | * - The content of $GIT_DIR/info/exclude | |
122 | * - The content of core.excludesfile | |
123 | * - The content (or the lack) of .gitignore of all parent directories | |
124 | * from $GIT_WORK_TREE | |
125 | * - The check_only flag in read_directory_recursive (for | |
126 | * DIR_HIDE_EMPTY_DIRECTORIES) | |
127 | * | |
128 | * The first input can be checked using directory mtime. In many | |
129 | * filesystems, directory mtime (stat_data field) is updated when its | |
130 | * files or direct subdirs are added or removed. | |
131 | * | |
132 | * The second one can be hooked from cache_tree_invalidate_path(). | |
133 | * Whenever a file (or a submodule) is added or removed from a | |
134 | * directory, we invalidate that directory. | |
135 | * | |
136 | * The remaining inputs are easy, their SHA-1 could be used to verify | |
137 | * their contents (exclude_sha1[], info_exclude_sha1[] and | |
138 | * excludes_file_sha1[]) | |
139 | */ | |
140 | struct untracked_cache_dir { | |
141 | struct untracked_cache_dir **dirs; | |
142 | char **untracked; | |
143 | struct stat_data stat_data; | |
144 | unsigned int untracked_alloc, dirs_nr, dirs_alloc; | |
145 | unsigned int untracked_nr; | |
146 | unsigned int check_only : 1; | |
26cb0182 | 147 | /* all data except 'dirs' in this struct are good */ |
ccad261f | 148 | unsigned int valid : 1; |
26cb0182 | 149 | unsigned int recurse : 1; |
70c369cd | 150 | /* null object ID means this directory does not have .gitignore */ |
151 | struct object_id exclude_oid; | |
0dcb8d7f NTND |
152 | char name[FLEX_ARRAY]; |
153 | }; | |
154 | ||
155 | struct untracked_cache { | |
4b33e602 PO |
156 | struct oid_stat ss_info_exclude; |
157 | struct oid_stat ss_excludes_file; | |
0dcb8d7f | 158 | const char *exclude_per_dir; |
1e8fef60 | 159 | struct strbuf ident; |
0dcb8d7f NTND |
160 | /* |
161 | * dir_struct#flags must match dir_flags or the untracked | |
162 | * cache is ignored. | |
163 | */ | |
164 | unsigned dir_flags; | |
165 | struct untracked_cache_dir *root; | |
166 | /* Statistics */ | |
167 | int dir_created; | |
ccad261f | 168 | int gitignore_invalidated; |
91a2288b NTND |
169 | int dir_invalidated; |
170 | int dir_opened; | |
883e248b BP |
171 | /* fsmonitor invalidation data */ |
172 | unsigned int use_fsmonitor : 1; | |
0dcb8d7f NTND |
173 | }; |
174 | ||
453ec4bd LT |
175 | struct dir_struct { |
176 | int nr, alloc; | |
2abd31b0 | 177 | int ignored_nr, ignored_alloc; |
7c4c97c0 JS |
178 | enum { |
179 | DIR_SHOW_IGNORED = 1<<0, | |
180 | DIR_SHOW_OTHER_DIRECTORIES = 1<<1, | |
181 | DIR_HIDE_EMPTY_DIRECTORIES = 1<<2, | |
182 | DIR_NO_GITLINKS = 1<<3, | |
0aaf62b6 | 183 | DIR_COLLECT_IGNORED = 1<<4, |
2eac2a4c | 184 | DIR_SHOW_IGNORED_TOO = 1<<5, |
fb898888 | 185 | DIR_COLLECT_KILLED_ONLY = 1<<6, |
eec0f7f2 | 186 | DIR_KEEP_UNTRACKED_CONTENTS = 1<<7, |
09487f2c EN |
187 | DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8, |
188 | DIR_SKIP_NESTED_GIT = 1<<9 | |
7c4c97c0 | 189 | } flags; |
453ec4bd | 190 | struct dir_entry **entries; |
2abd31b0 | 191 | struct dir_entry **ignored; |
453ec4bd LT |
192 | |
193 | /* Exclude info */ | |
194 | const char *exclude_per_dir; | |
c082df24 | 195 | |
63d285c8 | 196 | /* |
c082df24 AS |
197 | * We maintain three groups of exclude pattern lists: |
198 | * | |
63d285c8 JH |
199 | * EXC_CMDL lists patterns explicitly given on the command line. |
200 | * EXC_DIRS lists patterns obtained from per-directory ignore files. | |
c082df24 AS |
201 | * EXC_FILE lists patterns from fallback ignore files, e.g. |
202 | * - .git/info/exclude | |
203 | * - core.excludesfile | |
204 | * | |
205 | * Each group contains multiple exclude lists, a single list | |
206 | * per source. | |
63d285c8 JH |
207 | */ |
208 | #define EXC_CMDL 0 | |
209 | #define EXC_DIRS 1 | |
210 | #define EXC_FILE 2 | |
c082df24 | 211 | struct exclude_list_group exclude_list_group[3]; |
63d285c8 | 212 | |
95a68344 AS |
213 | /* |
214 | * Temporary variables which are used during loading of the | |
215 | * per-directory exclude lists. | |
216 | * | |
217 | * exclude_stack points to the top of the exclude_stack, and | |
218 | * basebuf contains the full path to the current | |
95c6f271 KB |
219 | * (sub)directory in the traversal. Exclude points to the |
220 | * matching exclude struct if the directory is excluded. | |
95a68344 | 221 | */ |
63d285c8 | 222 | struct exclude_stack *exclude_stack; |
ab8db613 | 223 | struct path_pattern *pattern; |
aceb9429 | 224 | struct strbuf basebuf; |
0dcb8d7f NTND |
225 | |
226 | /* Enable untracked file cache if set */ | |
227 | struct untracked_cache *untracked; | |
4b33e602 PO |
228 | struct oid_stat ss_info_exclude; |
229 | struct oid_stat ss_excludes_file; | |
ccad261f | 230 | unsigned unmanaged_exclude_files; |
453ec4bd LT |
231 | }; |
232 | ||
e0556a92 | 233 | /*Count the number of slashes for string s*/ |
55454427 | 234 | int count_slashes(const char *s); |
e0556a92 | 235 | |
52ed1894 AS |
236 | /* |
237 | * The ordering of these constants is significant, with | |
238 | * higher-numbered match types signifying "closer" (i.e. more | |
239 | * specific) matches which will override lower-numbered match types | |
240 | * when populating the seen[] array. | |
241 | */ | |
e813d50e | 242 | #define MATCHED_RECURSIVELY 1 |
89a1f4aa EN |
243 | #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2 |
244 | #define MATCHED_FNMATCH 3 | |
245 | #define MATCHED_EXACTLY 4 | |
55454427 DL |
246 | int simple_length(const char *match); |
247 | int no_wildcard(const char *string); | |
248 | char *common_prefix(const struct pathspec *pathspec); | |
249 | int match_pathspec(const struct index_state *istate, | |
ad6dad09 DL |
250 | const struct pathspec *pathspec, |
251 | const char *name, int namelen, | |
252 | int prefix, char *seen, int is_dir); | |
4aeeef37 | 253 | int report_path_error(const char *ps_matched, const struct pathspec *pathspec); |
55454427 | 254 | int within_depth(const char *name, int namelen, int depth, int max_depth); |
3c6a370b | 255 | |
55454427 | 256 | int fill_directory(struct dir_struct *dir, |
ad6dad09 DL |
257 | struct index_state *istate, |
258 | const struct pathspec *pathspec); | |
55454427 | 259 | int read_directory(struct dir_struct *, struct index_state *istate, |
ad6dad09 DL |
260 | const char *path, int len, |
261 | const struct pathspec *pathspec); | |
f8a9d428 | 262 | |
468ce99b DS |
263 | enum pattern_match_result { |
264 | UNDECIDED = -1, | |
265 | NOT_MATCHED = 0, | |
266 | MATCHED = 1, | |
267 | }; | |
268 | ||
269 | /* | |
270 | * Scan the list of patterns to determine if the ordered list | |
271 | * of patterns matches on 'pathname'. | |
272 | * | |
273 | * Return 1 for a match, 0 for not matched and -1 for undecided. | |
274 | */ | |
275 | enum pattern_match_result path_matches_pattern_list(const char *pathname, | |
276 | int pathlen, | |
277 | const char *basename, int *dtype, | |
278 | struct pattern_list *pl, | |
279 | struct index_state *istate); | |
9e58beca BW |
280 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, |
281 | struct index_state *istate, | |
282 | const char *pathname, int len); | |
eb41775e | 283 | |
82dce998 NTND |
284 | /* |
285 | * these implement the matching logic for dir.c:excluded_from_list and | |
286 | * attr.c:path_matches() | |
287 | */ | |
55454427 | 288 | int match_basename(const char *, int, |
ad6dad09 | 289 | const char *, int, int, unsigned); |
55454427 | 290 | int match_pathname(const char *, int, |
ad6dad09 DL |
291 | const char *, int, |
292 | const char *, int, int, unsigned); | |
82dce998 | 293 | |
65edd96a DS |
294 | struct path_pattern *last_matching_pattern(struct dir_struct *dir, |
295 | struct index_state *istate, | |
296 | const char *name, int *dtype); | |
782cd4c0 | 297 | |
55454427 | 298 | int is_excluded(struct dir_struct *dir, |
ad6dad09 DL |
299 | struct index_state *istate, |
300 | const char *name, int *dtype); | |
eb41775e | 301 | |
96cc8ab5 DS |
302 | int hashmap_contains_parent(struct hashmap *map, |
303 | const char *path, | |
304 | struct strbuf *buffer); | |
65edd96a | 305 | struct pattern_list *add_pattern_list(struct dir_struct *dir, |
ad6dad09 | 306 | int group_type, const char *src); |
65edd96a | 307 | int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen, |
caa3d554 | 308 | struct pattern_list *pl, struct index_state *istate); |
65edd96a DS |
309 | void add_patterns_from_file(struct dir_struct *, const char *fname); |
310 | int add_patterns_from_blob_to_list(struct object_id *oid, | |
ad6dad09 | 311 | const char *base, int baselen, |
caa3d554 | 312 | struct pattern_list *pl); |
65edd96a DS |
313 | void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen); |
314 | void add_pattern(const char *string, const char *base, | |
caa3d554 | 315 | int baselen, struct pattern_list *pl, int srcpos); |
65edd96a | 316 | void clear_pattern_list(struct pattern_list *pl); |
55454427 | 317 | void clear_directory(struct dir_struct *dir); |
0488481e NTND |
318 | |
319 | int repo_file_exists(struct repository *repo, const char *path); | |
320 | int file_exists(const char *); | |
453ec4bd | 321 | |
55454427 DL |
322 | int is_inside_dir(const char *dir); |
323 | int dir_inside_of(const char *subdir, const char *dir); | |
e6636747 | 324 | |
8ca12c0d AP |
325 | static inline int is_dot_or_dotdot(const char *name) |
326 | { | |
327 | return (name[0] == '.' && | |
328 | (name[1] == '\0' || | |
329 | (name[1] == '.' && name[2] == '\0'))); | |
330 | } | |
331 | ||
55454427 | 332 | int is_empty_dir(const char *dir); |
55892d23 | 333 | |
55454427 | 334 | void setup_standard_excludes(struct dir_struct *dir); |
a0f4afbe | 335 | |
728af283 MH |
336 | |
337 | /* Constants for remove_dir_recursively: */ | |
338 | ||
339 | /* | |
340 | * If a non-directory is found within path, stop and return an error. | |
341 | * (In this case some empty directories might already have been | |
342 | * removed.) | |
343 | */ | |
a0f4afbe | 344 | #define REMOVE_DIR_EMPTY_ONLY 01 |
728af283 MH |
345 | |
346 | /* | |
347 | * If any Git work trees are found within path, skip them without | |
348 | * considering it an error. | |
349 | */ | |
a0f4afbe | 350 | #define REMOVE_DIR_KEEP_NESTED_GIT 02 |
728af283 MH |
351 | |
352 | /* Remove the contents of path, but leave path itself. */ | |
c844a803 | 353 | #define REMOVE_DIR_KEEP_TOPLEVEL 04 |
728af283 MH |
354 | |
355 | /* | |
356 | * Remove path and its contents, recursively. flags is a combination | |
357 | * of the above REMOVE_DIR_* constants. Return 0 on success. | |
358 | * | |
359 | * This function uses path as temporary scratch space, but restores it | |
360 | * before returning. | |
361 | */ | |
55454427 | 362 | int remove_dir_recursively(struct strbuf *path, int flag); |
7155b727 | 363 | |
4a92d1bf | 364 | /* tries to remove the path with empty directories along it, ignores ENOENT */ |
55454427 | 365 | int remove_path(const char *path); |
4a92d1bf | 366 | |
55454427 DL |
367 | int fspathcmp(const char *a, const char *b); |
368 | int fspathncmp(const char *a, const char *b, size_t count); | |
8cf2a84e | 369 | |
5d74762d NTND |
370 | /* |
371 | * The prefix part of pattern must not contains wildcards. | |
372 | */ | |
bd30c2e4 | 373 | struct pathspec_item; |
55454427 | 374 | int git_fnmatch(const struct pathspec_item *item, |
ad6dad09 DL |
375 | const char *pattern, const char *string, |
376 | int prefix); | |
5d74762d | 377 | |
55454427 | 378 | int submodule_path_match(const struct index_state *istate, |
ad6dad09 DL |
379 | const struct pathspec *ps, |
380 | const char *submodule_name, | |
381 | char *seen); | |
75a6315f | 382 | |
6d2df284 NTND |
383 | static inline int ce_path_match(const struct index_state *istate, |
384 | const struct cache_entry *ce, | |
429bb40a NTND |
385 | const struct pathspec *pathspec, |
386 | char *seen) | |
387 | { | |
6d2df284 | 388 | return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen, |
ae8d0824 | 389 | S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode)); |
429bb40a NTND |
390 | } |
391 | ||
6d2df284 NTND |
392 | static inline int dir_path_match(const struct index_state *istate, |
393 | const struct dir_entry *ent, | |
ebb32893 NTND |
394 | const struct pathspec *pathspec, |
395 | int prefix, char *seen) | |
396 | { | |
ae8d0824 NTND |
397 | int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/'; |
398 | int len = has_trailing_dir ? ent->len - 1 : ent->len; | |
6d2df284 | 399 | return match_pathspec(istate, pathspec, ent->name, len, prefix, seen, |
ae8d0824 | 400 | has_trailing_dir); |
ebb32893 NTND |
401 | } |
402 | ||
bbf504a9 SL |
403 | int cmp_dir_entry(const void *p1, const void *p2); |
404 | int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in); | |
405 | ||
0cacebf0 | 406 | void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path); |
e931371a NTND |
407 | void untracked_cache_remove_from_index(struct index_state *, const char *); |
408 | void untracked_cache_add_to_index(struct index_state *, const char *); | |
409 | ||
f9e6c649 NTND |
410 | void free_untracked_cache(struct untracked_cache *); |
411 | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz); | |
83c094ad | 412 | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked); |
4a4ca479 | 413 | void add_untracked_cache(struct index_state *istate); |
07b29bfd | 414 | void remove_untracked_cache(struct index_state *istate); |
da62f786 SB |
415 | |
416 | /* | |
417 | * Connect a worktree to a git directory by creating (or overwriting) a | |
418 | * '.git' file containing the location of the git directory. In the git | |
419 | * directory set the core.worktree setting to indicate where the worktree is. | |
420 | * When `recurse_into_nested` is set, recurse into any nested submodules, | |
421 | * connecting them as well. | |
422 | */ | |
55454427 | 423 | void connect_work_tree_and_git_dir(const char *work_tree, |
ad6dad09 DL |
424 | const char *git_dir, |
425 | int recurse_into_nested); | |
55454427 | 426 | void relocate_gitdir(const char *path, |
ad6dad09 DL |
427 | const char *old_git_dir, |
428 | const char *new_git_dir); | |
453ec4bd | 429 | #endif |