Merge branch 'bc/more-git-var'
[git/debian.git] / dir.h
blob461f0971e4a174355fb7761f68f2e37e2729a168
1 #ifndef DIR_H
2 #define DIR_H
4 #include "hash-ll.h"
5 #include "hashmap.h"
6 #include "pathspec.h"
7 #include "statinfo.h"
8 #include "strbuf.h"
10 /**
11 * The directory listing API is used to enumerate paths in the work tree,
12 * optionally taking `.git/info/exclude` and `.gitignore` files per directory
13 * into account.
16 /**
17 * Calling sequence
18 * ----------------
20 * Note: The index may be checked for .gitignore files that are
21 * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have
22 * loaded the index first.
24 * - Prepare `struct dir_struct dir` using `dir_init()` function.
26 * - To add single exclude pattern, call `add_pattern_list()` and then
27 * `add_pattern()`.
29 * - To add patterns from a file (e.g. `.git/info/exclude`), call
30 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
32 * - A short-hand function `setup_standard_excludes()` can be used to set
33 * up the standard set of exclude settings, instead of manually calling
34 * the add_pattern*() family of functions.
36 * - Call `fill_directory()`.
38 * - Use `dir.entries[]` and `dir.ignored[]`.
40 * - Call `dir_clear()` when the contained elements are no longer in use.
44 struct dir_entry {
45 unsigned int len;
46 char name[FLEX_ARRAY]; /* more */
49 #define PATTERN_FLAG_NODIR 1
50 #define PATTERN_FLAG_ENDSWITH 4
51 #define PATTERN_FLAG_MUSTBEDIR 8
52 #define PATTERN_FLAG_NEGATIVE 16
54 struct path_pattern {
56 * This allows callers of last_matching_pattern() etc.
57 * to determine the origin of the matching pattern.
59 struct pattern_list *pl;
61 const char *pattern;
62 int patternlen;
63 int nowildcardlen;
64 const char *base;
65 int baselen;
66 unsigned flags; /* PATTERN_FLAG_* */
69 * Counting starts from 1 for line numbers in ignore files,
70 * and from -1 decrementing for patterns from CLI args.
72 int srcpos;
75 /* used for hashmaps for cone patterns */
76 struct pattern_entry {
77 struct hashmap_entry ent;
78 char *pattern;
79 size_t patternlen;
83 * Each excludes file will be parsed into a fresh exclude_list which
84 * is appended to the relevant exclude_list_group (either EXC_DIRS or
85 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
86 * can also be used to represent the list of --exclude values passed
87 * via CLI args.
89 struct pattern_list {
90 int nr;
91 int alloc;
93 /* remember pointer to exclude file contents so we can free() */
94 char *filebuf;
96 /* origin of list, e.g. path to filename, or descriptive string */
97 const char *src;
99 struct path_pattern **patterns;
102 * While scanning the excludes, we attempt to match the patterns
103 * with a more restricted set that allows us to use hashsets for
104 * matching logic, which is faster than the linear lookup in the
105 * excludes array above. If non-zero, that check succeeded.
107 unsigned use_cone_patterns;
108 unsigned full_cone;
111 * Stores paths where everything starting with those paths
112 * is included.
114 struct hashmap recursive_hashmap;
117 * Used to check single-level parents of blobs.
119 struct hashmap parent_hashmap;
123 * The contents of the per-directory exclude files are lazily read on
124 * demand and then cached in memory, one per exclude_stack struct, in
125 * order to avoid opening and parsing each one every time that
126 * directory is traversed.
128 struct exclude_stack {
129 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
130 int baselen;
131 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
132 struct untracked_cache_dir *ucd;
135 struct exclude_list_group {
136 int nr, alloc;
137 struct pattern_list *pl;
140 struct oid_stat {
141 struct stat_data stat;
142 struct object_id oid;
143 int valid;
147 * Untracked cache
149 * The following inputs are sufficient to determine what files in a
150 * directory are excluded:
152 * - The list of files and directories of the directory in question
153 * - The $GIT_DIR/index
154 * - dir_struct flags
155 * - The content of $GIT_DIR/info/exclude
156 * - The content of core.excludesfile
157 * - The content (or the lack) of .gitignore of all parent directories
158 * from $GIT_WORK_TREE
159 * - The check_only flag in read_directory_recursive (for
160 * DIR_HIDE_EMPTY_DIRECTORIES)
162 * The first input can be checked using directory mtime. In many
163 * filesystems, directory mtime (stat_data field) is updated when its
164 * files or direct subdirs are added or removed.
166 * The second one can be hooked from cache_tree_invalidate_path().
167 * Whenever a file (or a submodule) is added or removed from a
168 * directory, we invalidate that directory.
170 * The remaining inputs are easy, their SHA-1 could be used to verify
171 * their contents (exclude_sha1[], info_exclude_sha1[] and
172 * excludes_file_sha1[])
174 struct untracked_cache_dir {
175 struct untracked_cache_dir **dirs;
176 char **untracked;
177 struct stat_data stat_data;
178 unsigned int untracked_alloc, dirs_nr, dirs_alloc;
179 unsigned int untracked_nr;
180 unsigned int check_only : 1;
181 /* all data except 'dirs' in this struct are good */
182 unsigned int valid : 1;
183 unsigned int recurse : 1;
184 /* null object ID means this directory does not have .gitignore */
185 struct object_id exclude_oid;
186 char name[FLEX_ARRAY];
189 struct untracked_cache {
190 struct oid_stat ss_info_exclude;
191 struct oid_stat ss_excludes_file;
192 const char *exclude_per_dir;
193 char *exclude_per_dir_to_free;
194 struct strbuf ident;
196 * dir_struct#flags must match dir_flags or the untracked
197 * cache is ignored.
199 unsigned dir_flags;
200 struct untracked_cache_dir *root;
201 /* Statistics */
202 int dir_created;
203 int gitignore_invalidated;
204 int dir_invalidated;
205 int dir_opened;
206 /* fsmonitor invalidation data */
207 unsigned int use_fsmonitor : 1;
211 * structure is used to pass directory traversal options to the library and to
212 * record the paths discovered. A single `struct dir_struct` is used regardless
213 * of whether or not the traversal recursively descends into subdirectories.
215 struct dir_struct {
217 /* bit-field of options */
218 enum {
221 * Return just ignored files in `entries[]`, not untracked files.
222 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
224 DIR_SHOW_IGNORED = 1<<0,
226 /* Include a directory that is not tracked. */
227 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
229 /* Do not include a directory that is not tracked and is empty. */
230 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
233 * If set, recurse into a directory that looks like a Git directory.
234 * Otherwise it is shown as a directory.
236 DIR_NO_GITLINKS = 1<<3,
239 * Special mode for git-add. Return ignored files in `ignored[]` and
240 * untracked files in `entries[]`. Only returns ignored files that match
241 * pathspec exactly (no wildcards). Does not recurse into ignored
242 * directories.
244 DIR_COLLECT_IGNORED = 1<<4,
247 * Similar to `DIR_SHOW_IGNORED`, but return ignored files in
248 * `ignored[]` in addition to untracked files in `entries[]`.
249 * This flag is mutually exclusive with `DIR_SHOW_IGNORED`.
251 DIR_SHOW_IGNORED_TOO = 1<<5,
253 DIR_COLLECT_KILLED_ONLY = 1<<6,
256 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
257 * set, the untracked contents of untracked directories are also
258 * returned in `entries[]`.
260 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
263 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
264 * set, returns ignored files and directories that match an exclude
265 * pattern. If a directory matches an exclude pattern, then the
266 * directory is returned and the contained paths are not. A directory
267 * that does not match an exclude pattern will not be returned even if
268 * all of its contents are ignored. In this case, the contents are
269 * returned as individual entries.
271 * If this is set, files and directories that explicitly match an ignore
272 * pattern are reported. Implicitly ignored directories (directories that
273 * do not match an ignore pattern, but whose contents are all ignored)
274 * are not reported, instead all of the contents are reported.
276 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
278 DIR_SKIP_NESTED_GIT = 1<<9
279 } flags;
281 /* The number of members in `entries[]` array. */
282 int nr; /* output only */
284 /* The number of members in `ignored[]` array. */
285 int ignored_nr; /* output only */
287 /* An array of `struct dir_entry`, each element of which describes a path. */
288 struct dir_entry **entries; /* output only */
291 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
292 * `DIR_COLLECT_IGNORED` flags.
294 struct dir_entry **ignored; /* output only */
296 /* Enable/update untracked file cache if set */
297 struct untracked_cache *untracked;
300 * Deprecated: ls-files is the only allowed caller; all other callers
301 * should leave this as NULL; it pre-dated the
302 * setup_standard_excludes() mechanism that replaces this.
304 * This field tracks the name of the file to be read in each directory
305 * for excluded files (typically `.gitignore`).
307 const char *exclude_per_dir;
309 struct dir_struct_internal {
310 /* Keeps track of allocation of `entries[]` array.*/
311 int alloc;
313 /* Keeps track of allocation of `ignored[]` array. */
314 int ignored_alloc;
317 * We maintain three groups of exclude pattern lists:
319 * EXC_CMDL lists patterns explicitly given on the command line.
320 * EXC_DIRS lists patterns obtained from per-directory ignore
321 * files.
322 * EXC_FILE lists patterns from fallback ignore files, e.g.
323 * - .git/info/exclude
324 * - core.excludesfile
326 * Each group contains multiple exclude lists, a single list
327 * per source.
329 #define EXC_CMDL 0
330 #define EXC_DIRS 1
331 #define EXC_FILE 2
332 struct exclude_list_group exclude_list_group[3];
335 * Temporary variables which are used during loading of the
336 * per-directory exclude lists.
338 * exclude_stack points to the top of the exclude_stack, and
339 * basebuf contains the full path to the current
340 * (sub)directory in the traversal. Exclude points to the
341 * matching exclude struct if the directory is excluded.
343 struct exclude_stack *exclude_stack;
344 struct path_pattern *pattern;
345 struct strbuf basebuf;
347 /* Additional metadata related to 'untracked' */
348 struct oid_stat ss_info_exclude;
349 struct oid_stat ss_excludes_file;
350 unsigned unmanaged_exclude_files;
352 /* Stats about the traversal */
353 unsigned visited_paths;
354 unsigned visited_directories;
355 } internal;
358 #define DIR_INIT { 0 }
360 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
362 /*Count the number of slashes for string s*/
363 int count_slashes(const char *s);
366 * The ordering of these constants is significant, with
367 * higher-numbered match types signifying "closer" (i.e. more
368 * specific) matches which will override lower-numbered match types
369 * when populating the seen[] array.
371 #define MATCHED_RECURSIVELY 1
372 #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
373 #define MATCHED_FNMATCH 3
374 #define MATCHED_EXACTLY 4
375 int simple_length(const char *match);
376 int no_wildcard(const char *string);
377 char *common_prefix(const struct pathspec *pathspec);
378 int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
379 int within_depth(const char *name, int namelen, int depth, int max_depth);
381 int fill_directory(struct dir_struct *dir,
382 struct index_state *istate,
383 const struct pathspec *pathspec);
384 int read_directory(struct dir_struct *, struct index_state *istate,
385 const char *path, int len,
386 const struct pathspec *pathspec);
388 enum pattern_match_result {
389 UNDECIDED = -1,
390 NOT_MATCHED = 0,
391 MATCHED = 1,
392 MATCHED_RECURSIVE = 2,
396 * Scan the list of patterns to determine if the ordered list
397 * of patterns matches on 'pathname'.
399 * Return 1 for a match, 0 for not matched and -1 for undecided.
401 enum pattern_match_result path_matches_pattern_list(const char *pathname,
402 int pathlen,
403 const char *basename, int *dtype,
404 struct pattern_list *pl,
405 struct index_state *istate);
407 int init_sparse_checkout_patterns(struct index_state *state);
409 int path_in_sparse_checkout(const char *path,
410 struct index_state *istate);
411 int path_in_cone_mode_sparse_checkout(const char *path,
412 struct index_state *istate);
414 struct dir_entry *dir_add_ignored(struct dir_struct *dir,
415 struct index_state *istate,
416 const char *pathname, int len);
419 * these implement the matching logic for dir.c:excluded_from_list and
420 * attr.c:path_matches()
422 int match_basename(const char *, int,
423 const char *, int, int, unsigned);
424 int match_pathname(const char *, int,
425 const char *, int,
426 const char *, int, int);
428 struct path_pattern *last_matching_pattern(struct dir_struct *dir,
429 struct index_state *istate,
430 const char *name, int *dtype);
432 int is_excluded(struct dir_struct *dir,
433 struct index_state *istate,
434 const char *name, int *dtype);
436 int pl_hashmap_cmp(const void *unused_cmp_data,
437 const struct hashmap_entry *a,
438 const struct hashmap_entry *b,
439 const void *key);
440 int hashmap_contains_parent(struct hashmap *map,
441 const char *path,
442 struct strbuf *buffer);
443 struct pattern_list *add_pattern_list(struct dir_struct *dir,
444 int group_type, const char *src);
445 int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
446 struct pattern_list *pl, struct index_state *istate,
447 unsigned flags);
448 void add_patterns_from_file(struct dir_struct *, const char *fname);
449 int add_patterns_from_blob_to_list(struct object_id *oid,
450 const char *base, int baselen,
451 struct pattern_list *pl);
452 void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
453 void add_pattern(const char *string, const char *base,
454 int baselen, struct pattern_list *pl, int srcpos);
455 void clear_pattern_list(struct pattern_list *pl);
456 void dir_clear(struct dir_struct *dir);
458 int repo_file_exists(struct repository *repo, const char *path);
459 int file_exists(const char *);
461 int is_inside_dir(const char *dir);
462 int dir_inside_of(const char *subdir, const char *dir);
464 static inline int is_dot_or_dotdot(const char *name)
466 return (name[0] == '.' &&
467 (name[1] == '\0' ||
468 (name[1] == '.' && name[2] == '\0')));
471 int is_empty_dir(const char *dir);
474 * Retrieve the "humanish" basename of the given Git URL.
476 * For example:
477 * /path/to/repo.git => "repo"
478 * host.xz:foo/.git => "foo"
479 * http://example.com/user/bar.baz => "bar.baz"
481 char *git_url_basename(const char *repo, int is_bundle, int is_bare);
482 void strip_dir_trailing_slashes(char *dir);
484 void setup_standard_excludes(struct dir_struct *dir);
486 char *get_sparse_checkout_filename(void);
487 int get_sparse_checkout_patterns(struct pattern_list *pl);
489 /* Constants for remove_dir_recursively: */
492 * If a non-directory is found within path, stop and return an error.
493 * (In this case some empty directories might already have been
494 * removed.)
496 #define REMOVE_DIR_EMPTY_ONLY 01
499 * If any Git work trees are found within path, skip them without
500 * considering it an error.
502 #define REMOVE_DIR_KEEP_NESTED_GIT 02
504 /* Remove the contents of path, but leave path itself. */
505 #define REMOVE_DIR_KEEP_TOPLEVEL 04
507 /* Remove the_original_cwd too */
508 #define REMOVE_DIR_PURGE_ORIGINAL_CWD 0x08
511 * Remove path and its contents, recursively. flags is a combination
512 * of the above REMOVE_DIR_* constants. Return 0 on success.
514 * This function uses path as temporary scratch space, but restores it
515 * before returning.
517 int remove_dir_recursively(struct strbuf *path, int flag);
520 * Tries to remove the path, along with leading empty directories so long as
521 * those empty directories are not startup_info->original_cwd. Ignores
522 * ENOENT.
524 int remove_path(const char *path);
526 int fspathcmp(const char *a, const char *b);
527 int fspatheq(const char *a, const char *b);
528 int fspathncmp(const char *a, const char *b, size_t count);
529 unsigned int fspathhash(const char *str);
532 * The prefix part of pattern must not contains wildcards.
534 struct pathspec_item;
535 int git_fnmatch(const struct pathspec_item *item,
536 const char *pattern, const char *string,
537 int prefix);
539 int submodule_path_match(struct index_state *istate,
540 const struct pathspec *ps,
541 const char *submodule_name,
542 char *seen);
544 static inline int dir_path_match(struct index_state *istate,
545 const struct dir_entry *ent,
546 const struct pathspec *pathspec,
547 int prefix, char *seen)
549 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
550 int len = has_trailing_dir ? ent->len - 1 : ent->len;
551 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
552 has_trailing_dir);
555 int cmp_dir_entry(const void *p1, const void *p2);
556 int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
558 void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
559 void untracked_cache_remove_from_index(struct index_state *, const char *);
560 void untracked_cache_add_to_index(struct index_state *, const char *);
562 void free_untracked_cache(struct untracked_cache *);
563 struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
564 void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
565 void add_untracked_cache(struct index_state *istate);
566 void remove_untracked_cache(struct index_state *istate);
569 * Connect a worktree to a git directory by creating (or overwriting) a
570 * '.git' file containing the location of the git directory. In the git
571 * directory set the core.worktree setting to indicate where the worktree is.
572 * When `recurse_into_nested` is set, recurse into any nested submodules,
573 * connecting them as well.
575 void connect_work_tree_and_git_dir(const char *work_tree,
576 const char *git_dir,
577 int recurse_into_nested);
578 void relocate_gitdir(const char *path,
579 const char *old_git_dir,
580 const char *new_git_dir);
583 * The "enum path_matches_kind" determines how path_match_flags() will
584 * behave. The flags come in sets, and one (and only one) must be
585 * provided out of each "set":
587 * PATH_MATCH_NATIVE:
588 * Path separator is is_dir_sep()
589 * PATH_MATCH_XPLATFORM:
590 * Path separator is is_xplatform_dir_sep()
592 * Do we use is_dir_sep() to check for a directory separator
593 * (*_NATIVE), or do we always check for '/' or '\' (*_XPLATFORM). The
594 * "*_NATIVE" version on Windows is the same as "*_XPLATFORM",
595 * everywhere else "*_NATIVE" means "only /".
597 * PATH_MATCH_STARTS_WITH_DOT_SLASH:
598 * Match a path starting with "./"
599 * PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH:
600 * Match a path starting with "../"
602 * The "/" in the above is adjusted based on the "*_NATIVE" and
603 * "*_XPLATFORM" flags.
605 enum path_match_flags {
606 PATH_MATCH_NATIVE = 1 << 0,
607 PATH_MATCH_XPLATFORM = 1 << 1,
608 PATH_MATCH_STARTS_WITH_DOT_SLASH = 1 << 2,
609 PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH = 1 << 3,
611 #define PATH_MATCH_KINDS_MASK (PATH_MATCH_STARTS_WITH_DOT_SLASH | \
612 PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH)
613 #define PATH_MATCH_PLATFORM_MASK (PATH_MATCH_NATIVE | PATH_MATCH_XPLATFORM)
616 * path_match_flags() checks if a given "path" matches a given "enum
617 * path_match_flags" criteria.
619 int path_match_flags(const char *const path, const enum path_match_flags f);
622 * starts_with_dot_slash_native(): convenience wrapper for
623 * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_SLASH and
624 * PATH_MATCH_NATIVE.
626 static inline int starts_with_dot_slash_native(const char *const path)
628 const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_SLASH;
630 return path_match_flags(path, what | PATH_MATCH_NATIVE);
634 * starts_with_dot_slash_native(): convenience wrapper for
635 * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH and
636 * PATH_MATCH_NATIVE.
638 static inline int starts_with_dot_dot_slash_native(const char *const path)
640 const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH;
642 return path_match_flags(path, what | PATH_MATCH_NATIVE);
645 #endif