Merge tag 'v2.32.0-rc2' into debian-sid
[git/debian.git] / dir.h
blobe3db9b9ec658eef53b62cd8a1bd83a5a64389bac
1 #ifndef DIR_H
2 #define DIR_H
4 #include "cache.h"
5 #include "hashmap.h"
6 #include "strbuf.h"
8 /**
9 * The directory listing API is used to enumerate paths in the work tree,
10 * optionally taking `.git/info/exclude` and `.gitignore` files per directory
11 * into account.
14 /**
15 * Calling sequence
16 * ----------------
18 * Note: The index may be checked for .gitignore files that are
19 * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have
20 * loaded the index first.
22 * - Prepare `struct dir_struct dir` using `dir_init()` function.
24 * - To add single exclude pattern, call `add_pattern_list()` and then
25 * `add_pattern()`.
27 * - To add patterns from a file (e.g. `.git/info/exclude`), call
28 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
30 * - A short-hand function `setup_standard_excludes()` can be used to set
31 * up the standard set of exclude settings, instead of manually calling
32 * the add_pattern*() family of functions.
34 * - Call `fill_directory()`.
36 * - Use `dir.entries[]` and `dir.ignored[]`.
38 * - Call `dir_clear()` when the contained elements are no longer in use.
42 struct dir_entry {
43 unsigned int len;
44 char name[FLEX_ARRAY]; /* more */
47 #define PATTERN_FLAG_NODIR 1
48 #define PATTERN_FLAG_ENDSWITH 4
49 #define PATTERN_FLAG_MUSTBEDIR 8
50 #define PATTERN_FLAG_NEGATIVE 16
52 struct path_pattern {
54 * This allows callers of last_matching_pattern() etc.
55 * to determine the origin of the matching pattern.
57 struct pattern_list *pl;
59 const char *pattern;
60 int patternlen;
61 int nowildcardlen;
62 const char *base;
63 int baselen;
64 unsigned flags; /* PATTERN_FLAG_* */
67 * Counting starts from 1 for line numbers in ignore files,
68 * and from -1 decrementing for patterns from CLI args.
70 int srcpos;
73 /* used for hashmaps for cone patterns */
74 struct pattern_entry {
75 struct hashmap_entry ent;
76 char *pattern;
77 size_t patternlen;
81 * Each excludes file will be parsed into a fresh exclude_list which
82 * is appended to the relevant exclude_list_group (either EXC_DIRS or
83 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
84 * can also be used to represent the list of --exclude values passed
85 * via CLI args.
87 struct pattern_list {
88 int nr;
89 int alloc;
91 /* remember pointer to exclude file contents so we can free() */
92 char *filebuf;
94 /* origin of list, e.g. path to filename, or descriptive string */
95 const char *src;
97 struct path_pattern **patterns;
100 * While scanning the excludes, we attempt to match the patterns
101 * with a more restricted set that allows us to use hashsets for
102 * matching logic, which is faster than the linear lookup in the
103 * excludes array above. If non-zero, that check succeeded.
105 unsigned use_cone_patterns;
106 unsigned full_cone;
109 * Stores paths where everything starting with those paths
110 * is included.
112 struct hashmap recursive_hashmap;
115 * Used to check single-level parents of blobs.
117 struct hashmap parent_hashmap;
121 * The contents of the per-directory exclude files are lazily read on
122 * demand and then cached in memory, one per exclude_stack struct, in
123 * order to avoid opening and parsing each one every time that
124 * directory is traversed.
126 struct exclude_stack {
127 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
128 int baselen;
129 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
130 struct untracked_cache_dir *ucd;
133 struct exclude_list_group {
134 int nr, alloc;
135 struct pattern_list *pl;
138 struct oid_stat {
139 struct stat_data stat;
140 struct object_id oid;
141 int valid;
145 * Untracked cache
147 * The following inputs are sufficient to determine what files in a
148 * directory are excluded:
150 * - The list of files and directories of the directory in question
151 * - The $GIT_DIR/index
152 * - dir_struct flags
153 * - The content of $GIT_DIR/info/exclude
154 * - The content of core.excludesfile
155 * - The content (or the lack) of .gitignore of all parent directories
156 * from $GIT_WORK_TREE
157 * - The check_only flag in read_directory_recursive (for
158 * DIR_HIDE_EMPTY_DIRECTORIES)
160 * The first input can be checked using directory mtime. In many
161 * filesystems, directory mtime (stat_data field) is updated when its
162 * files or direct subdirs are added or removed.
164 * The second one can be hooked from cache_tree_invalidate_path().
165 * Whenever a file (or a submodule) is added or removed from a
166 * directory, we invalidate that directory.
168 * The remaining inputs are easy, their SHA-1 could be used to verify
169 * their contents (exclude_sha1[], info_exclude_sha1[] and
170 * excludes_file_sha1[])
172 struct untracked_cache_dir {
173 struct untracked_cache_dir **dirs;
174 char **untracked;
175 struct stat_data stat_data;
176 unsigned int untracked_alloc, dirs_nr, dirs_alloc;
177 unsigned int untracked_nr;
178 unsigned int check_only : 1;
179 /* all data except 'dirs' in this struct are good */
180 unsigned int valid : 1;
181 unsigned int recurse : 1;
182 /* null object ID means this directory does not have .gitignore */
183 struct object_id exclude_oid;
184 char name[FLEX_ARRAY];
187 struct untracked_cache {
188 struct oid_stat ss_info_exclude;
189 struct oid_stat ss_excludes_file;
190 const char *exclude_per_dir;
191 struct strbuf ident;
193 * dir_struct#flags must match dir_flags or the untracked
194 * cache is ignored.
196 unsigned dir_flags;
197 struct untracked_cache_dir *root;
198 /* Statistics */
199 int dir_created;
200 int gitignore_invalidated;
201 int dir_invalidated;
202 int dir_opened;
203 /* fsmonitor invalidation data */
204 unsigned int use_fsmonitor : 1;
208 * structure is used to pass directory traversal options to the library and to
209 * record the paths discovered. A single `struct dir_struct` is used regardless
210 * of whether or not the traversal recursively descends into subdirectories.
212 struct dir_struct {
214 /* The number of members in `entries[]` array. */
215 int nr;
217 /* Internal use; keeps track of allocation of `entries[]` array.*/
218 int alloc;
220 /* The number of members in `ignored[]` array. */
221 int ignored_nr;
223 int ignored_alloc;
225 /* bit-field of options */
226 enum {
229 * Return just ignored files in `entries[]`, not untracked files.
230 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
232 DIR_SHOW_IGNORED = 1<<0,
234 /* Include a directory that is not tracked. */
235 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
237 /* Do not include a directory that is not tracked and is empty. */
238 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
241 * If set, recurse into a directory that looks like a Git directory.
242 * Otherwise it is shown as a directory.
244 DIR_NO_GITLINKS = 1<<3,
247 * Special mode for git-add. Return ignored files in `ignored[]` and
248 * untracked files in `entries[]`. Only returns ignored files that match
249 * pathspec exactly (no wildcards). Does not recurse into ignored
250 * directories.
252 DIR_COLLECT_IGNORED = 1<<4,
255 * Similar to `DIR_SHOW_IGNORED`, but return ignored files in
256 * `ignored[]` in addition to untracked files in `entries[]`.
257 * This flag is mutually exclusive with `DIR_SHOW_IGNORED`.
259 DIR_SHOW_IGNORED_TOO = 1<<5,
261 DIR_COLLECT_KILLED_ONLY = 1<<6,
264 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
265 * set, the untracked contents of untracked directories are also
266 * returned in `entries[]`.
268 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
271 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
272 * set, returns ignored files and directories that match an exclude
273 * pattern. If a directory matches an exclude pattern, then the
274 * directory is returned and the contained paths are not. A directory
275 * that does not match an exclude pattern will not be returned even if
276 * all of its contents are ignored. In this case, the contents are
277 * returned as individual entries.
279 * If this is set, files and directories that explicitly match an ignore
280 * pattern are reported. Implicitly ignored directories (directories that
281 * do not match an ignore pattern, but whose contents are all ignored)
282 * are not reported, instead all of the contents are reported.
284 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
286 DIR_SKIP_NESTED_GIT = 1<<9
287 } flags;
289 /* An array of `struct dir_entry`, each element of which describes a path. */
290 struct dir_entry **entries;
293 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
294 * `DIR_COLLECT_IGNORED` flags.
296 struct dir_entry **ignored;
299 * The name of the file to be read in each directory for excluded files
300 * (typically `.gitignore`).
302 const char *exclude_per_dir;
305 * We maintain three groups of exclude pattern lists:
307 * EXC_CMDL lists patterns explicitly given on the command line.
308 * EXC_DIRS lists patterns obtained from per-directory ignore files.
309 * EXC_FILE lists patterns from fallback ignore files, e.g.
310 * - .git/info/exclude
311 * - core.excludesfile
313 * Each group contains multiple exclude lists, a single list
314 * per source.
316 #define EXC_CMDL 0
317 #define EXC_DIRS 1
318 #define EXC_FILE 2
319 struct exclude_list_group exclude_list_group[3];
322 * Temporary variables which are used during loading of the
323 * per-directory exclude lists.
325 * exclude_stack points to the top of the exclude_stack, and
326 * basebuf contains the full path to the current
327 * (sub)directory in the traversal. Exclude points to the
328 * matching exclude struct if the directory is excluded.
330 struct exclude_stack *exclude_stack;
331 struct path_pattern *pattern;
332 struct strbuf basebuf;
334 /* Enable untracked file cache if set */
335 struct untracked_cache *untracked;
336 struct oid_stat ss_info_exclude;
337 struct oid_stat ss_excludes_file;
338 unsigned unmanaged_exclude_files;
340 /* Stats about the traversal */
341 unsigned visited_paths;
342 unsigned visited_directories;
345 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
347 /*Count the number of slashes for string s*/
348 int count_slashes(const char *s);
351 * The ordering of these constants is significant, with
352 * higher-numbered match types signifying "closer" (i.e. more
353 * specific) matches which will override lower-numbered match types
354 * when populating the seen[] array.
356 #define MATCHED_RECURSIVELY 1
357 #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
358 #define MATCHED_FNMATCH 3
359 #define MATCHED_EXACTLY 4
360 int simple_length(const char *match);
361 int no_wildcard(const char *string);
362 char *common_prefix(const struct pathspec *pathspec);
363 int match_pathspec(struct index_state *istate,
364 const struct pathspec *pathspec,
365 const char *name, int namelen,
366 int prefix, char *seen, int is_dir);
367 int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
368 int within_depth(const char *name, int namelen, int depth, int max_depth);
370 void dir_init(struct dir_struct *dir);
372 int fill_directory(struct dir_struct *dir,
373 struct index_state *istate,
374 const struct pathspec *pathspec);
375 int read_directory(struct dir_struct *, struct index_state *istate,
376 const char *path, int len,
377 const struct pathspec *pathspec);
379 enum pattern_match_result {
380 UNDECIDED = -1,
381 NOT_MATCHED = 0,
382 MATCHED = 1,
383 MATCHED_RECURSIVE = 2,
387 * Scan the list of patterns to determine if the ordered list
388 * of patterns matches on 'pathname'.
390 * Return 1 for a match, 0 for not matched and -1 for undecided.
392 enum pattern_match_result path_matches_pattern_list(const char *pathname,
393 int pathlen,
394 const char *basename, int *dtype,
395 struct pattern_list *pl,
396 struct index_state *istate);
397 struct dir_entry *dir_add_ignored(struct dir_struct *dir,
398 struct index_state *istate,
399 const char *pathname, int len);
402 * these implement the matching logic for dir.c:excluded_from_list and
403 * attr.c:path_matches()
405 int match_basename(const char *, int,
406 const char *, int, int, unsigned);
407 int match_pathname(const char *, int,
408 const char *, int,
409 const char *, int, int, unsigned);
411 struct path_pattern *last_matching_pattern(struct dir_struct *dir,
412 struct index_state *istate,
413 const char *name, int *dtype);
415 int is_excluded(struct dir_struct *dir,
416 struct index_state *istate,
417 const char *name, int *dtype);
419 int pl_hashmap_cmp(const void *unused_cmp_data,
420 const struct hashmap_entry *a,
421 const struct hashmap_entry *b,
422 const void *key);
423 int hashmap_contains_parent(struct hashmap *map,
424 const char *path,
425 struct strbuf *buffer);
426 struct pattern_list *add_pattern_list(struct dir_struct *dir,
427 int group_type, const char *src);
428 int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
429 struct pattern_list *pl, struct index_state *istate,
430 unsigned flags);
431 void add_patterns_from_file(struct dir_struct *, const char *fname);
432 int add_patterns_from_blob_to_list(struct object_id *oid,
433 const char *base, int baselen,
434 struct pattern_list *pl);
435 void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
436 void add_pattern(const char *string, const char *base,
437 int baselen, struct pattern_list *pl, int srcpos);
438 void clear_pattern_list(struct pattern_list *pl);
439 void dir_clear(struct dir_struct *dir);
441 int repo_file_exists(struct repository *repo, const char *path);
442 int file_exists(const char *);
444 int is_inside_dir(const char *dir);
445 int dir_inside_of(const char *subdir, const char *dir);
447 static inline int is_dot_or_dotdot(const char *name)
449 return (name[0] == '.' &&
450 (name[1] == '\0' ||
451 (name[1] == '.' && name[2] == '\0')));
454 int is_empty_dir(const char *dir);
456 void setup_standard_excludes(struct dir_struct *dir);
458 char *get_sparse_checkout_filename(void);
459 int get_sparse_checkout_patterns(struct pattern_list *pl);
461 /* Constants for remove_dir_recursively: */
464 * If a non-directory is found within path, stop and return an error.
465 * (In this case some empty directories might already have been
466 * removed.)
468 #define REMOVE_DIR_EMPTY_ONLY 01
471 * If any Git work trees are found within path, skip them without
472 * considering it an error.
474 #define REMOVE_DIR_KEEP_NESTED_GIT 02
476 /* Remove the contents of path, but leave path itself. */
477 #define REMOVE_DIR_KEEP_TOPLEVEL 04
480 * Remove path and its contents, recursively. flags is a combination
481 * of the above REMOVE_DIR_* constants. Return 0 on success.
483 * This function uses path as temporary scratch space, but restores it
484 * before returning.
486 int remove_dir_recursively(struct strbuf *path, int flag);
488 /* tries to remove the path with empty directories along it, ignores ENOENT */
489 int remove_path(const char *path);
491 int fspathcmp(const char *a, const char *b);
492 int fspathncmp(const char *a, const char *b, size_t count);
495 * The prefix part of pattern must not contains wildcards.
497 struct pathspec_item;
498 int git_fnmatch(const struct pathspec_item *item,
499 const char *pattern, const char *string,
500 int prefix);
502 int submodule_path_match(struct index_state *istate,
503 const struct pathspec *ps,
504 const char *submodule_name,
505 char *seen);
507 static inline int ce_path_match(struct index_state *istate,
508 const struct cache_entry *ce,
509 const struct pathspec *pathspec,
510 char *seen)
512 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
513 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
516 static inline int dir_path_match(struct index_state *istate,
517 const struct dir_entry *ent,
518 const struct pathspec *pathspec,
519 int prefix, char *seen)
521 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
522 int len = has_trailing_dir ? ent->len - 1 : ent->len;
523 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
524 has_trailing_dir);
527 int cmp_dir_entry(const void *p1, const void *p2);
528 int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
530 void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
531 void untracked_cache_remove_from_index(struct index_state *, const char *);
532 void untracked_cache_add_to_index(struct index_state *, const char *);
534 void free_untracked_cache(struct untracked_cache *);
535 struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
536 void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
537 void add_untracked_cache(struct index_state *istate);
538 void remove_untracked_cache(struct index_state *istate);
541 * Connect a worktree to a git directory by creating (or overwriting) a
542 * '.git' file containing the location of the git directory. In the git
543 * directory set the core.worktree setting to indicate where the worktree is.
544 * When `recurse_into_nested` is set, recurse into any nested submodules,
545 * connecting them as well.
547 void connect_work_tree_and_git_dir(const char *work_tree,
548 const char *git_dir,
549 int recurse_into_nested);
550 void relocate_gitdir(const char *path,
551 const char *old_git_dir,
552 const char *new_git_dir);
553 #endif