13 * The directory listing API is used to enumerate paths in the work tree,
14 * optionally taking `.git/info/exclude` and `.gitignore` files per directory
22 * Note: The index may be checked for .gitignore files that are
23 * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have
24 * loaded the index first.
26 * - Prepare `struct dir_struct dir` using `dir_init()` function.
28 * - To add single exclude pattern, call `add_pattern_list()` and then
31 * - To add patterns from a file (e.g. `.git/info/exclude`), call
32 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
34 * - A short-hand function `setup_standard_excludes()` can be used to set
35 * up the standard set of exclude settings, instead of manually calling
36 * the add_pattern*() family of functions.
38 * - Call `fill_directory()`.
40 * - Use `dir.entries[]` and `dir.ignored[]`.
42 * - Call `dir_clear()` when the contained elements are no longer in use.
50 char name
[FLEX_ARRAY
]; /* more */
53 #define PATTERN_FLAG_NODIR 1
54 #define PATTERN_FLAG_ENDSWITH 4
55 #define PATTERN_FLAG_MUSTBEDIR 8
56 #define PATTERN_FLAG_NEGATIVE 16
60 * This allows callers of last_matching_pattern() etc.
61 * to determine the origin of the matching pattern.
63 struct pattern_list
*pl
;
70 unsigned flags
; /* PATTERN_FLAG_* */
73 * Counting starts from 1 for line numbers in ignore files,
74 * and from -1 decrementing for patterns from CLI args.
79 /* used for hashmaps for cone patterns */
80 struct pattern_entry
{
81 struct hashmap_entry ent
;
87 * Each excludes file will be parsed into a fresh exclude_list which
88 * is appended to the relevant exclude_list_group (either EXC_DIRS or
89 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
90 * can also be used to represent the list of --exclude values passed
97 /* remember pointer to exclude file contents so we can free() */
100 /* origin of list, e.g. path to filename, or descriptive string */
103 struct path_pattern
**patterns
;
106 * While scanning the excludes, we attempt to match the patterns
107 * with a more restricted set that allows us to use hashsets for
108 * matching logic, which is faster than the linear lookup in the
109 * excludes array above. If non-zero, that check succeeded.
111 unsigned use_cone_patterns
;
115 * Stores paths where everything starting with those paths
118 struct hashmap recursive_hashmap
;
121 * Used to check single-level parents of blobs.
123 struct hashmap parent_hashmap
;
127 * The contents of the per-directory exclude files are lazily read on
128 * demand and then cached in memory, one per exclude_stack struct, in
129 * order to avoid opening and parsing each one every time that
130 * directory is traversed.
132 struct exclude_stack
{
133 struct exclude_stack
*prev
; /* the struct exclude_stack for the parent directory */
135 int exclude_ix
; /* index of exclude_list within EXC_DIRS exclude_list_group */
136 struct untracked_cache_dir
*ucd
;
139 struct exclude_list_group
{
141 struct pattern_list
*pl
;
145 struct stat_data stat
;
146 struct object_id oid
;
153 * The following inputs are sufficient to determine what files in a
154 * directory are excluded:
156 * - The list of files and directories of the directory in question
157 * - The $GIT_DIR/index
159 * - The content of $GIT_DIR/info/exclude
160 * - The content of core.excludesfile
161 * - The content (or the lack) of .gitignore of all parent directories
162 * from $GIT_WORK_TREE
163 * - The check_only flag in read_directory_recursive (for
164 * DIR_HIDE_EMPTY_DIRECTORIES)
166 * The first input can be checked using directory mtime. In many
167 * filesystems, directory mtime (stat_data field) is updated when its
168 * files or direct subdirs are added or removed.
170 * The second one can be hooked from cache_tree_invalidate_path().
171 * Whenever a file (or a submodule) is added or removed from a
172 * directory, we invalidate that directory.
174 * The remaining inputs are easy, their SHA-1 could be used to verify
175 * their contents (exclude_sha1[], info_exclude_sha1[] and
176 * excludes_file_sha1[])
178 struct untracked_cache_dir
{
179 struct untracked_cache_dir
**dirs
;
181 struct stat_data stat_data
;
182 unsigned int untracked_alloc
, dirs_nr
, dirs_alloc
;
183 unsigned int untracked_nr
;
184 unsigned int check_only
: 1;
185 /* all data except 'dirs' in this struct are good */
186 unsigned int valid
: 1;
187 unsigned int recurse
: 1;
188 /* null object ID means this directory does not have .gitignore */
189 struct object_id exclude_oid
;
190 char name
[FLEX_ARRAY
];
193 struct untracked_cache
{
194 struct oid_stat ss_info_exclude
;
195 struct oid_stat ss_excludes_file
;
196 const char *exclude_per_dir
;
197 char *exclude_per_dir_to_free
;
200 * dir_struct#flags must match dir_flags or the untracked
204 struct untracked_cache_dir
*root
;
207 int gitignore_invalidated
;
210 /* fsmonitor invalidation data */
211 unsigned int use_fsmonitor
: 1;
215 * structure is used to pass directory traversal options to the library and to
216 * record the paths discovered. A single `struct dir_struct` is used regardless
217 * of whether or not the traversal recursively descends into subdirectories.
221 /* bit-field of options */
225 * Return just ignored files in `entries[]`, not untracked files.
226 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
228 DIR_SHOW_IGNORED
= 1<<0,
230 /* Include a directory that is not tracked. */
231 DIR_SHOW_OTHER_DIRECTORIES
= 1<<1,
233 /* Do not include a directory that is not tracked and is empty. */
234 DIR_HIDE_EMPTY_DIRECTORIES
= 1<<2,
237 * If set, recurse into a directory that looks like a Git directory.
238 * Otherwise it is shown as a directory.
240 DIR_NO_GITLINKS
= 1<<3,
243 * Special mode for git-add. Return ignored files in `ignored[]` and
244 * untracked files in `entries[]`. Only returns ignored files that match
245 * pathspec exactly (no wildcards). Does not recurse into ignored
248 DIR_COLLECT_IGNORED
= 1<<4,
251 * Similar to `DIR_SHOW_IGNORED`, but return ignored files in
252 * `ignored[]` in addition to untracked files in `entries[]`.
253 * This flag is mutually exclusive with `DIR_SHOW_IGNORED`.
255 DIR_SHOW_IGNORED_TOO
= 1<<5,
257 DIR_COLLECT_KILLED_ONLY
= 1<<6,
260 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
261 * set, the untracked contents of untracked directories are also
262 * returned in `entries[]`.
264 DIR_KEEP_UNTRACKED_CONTENTS
= 1<<7,
267 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
268 * set, returns ignored files and directories that match an exclude
269 * pattern. If a directory matches an exclude pattern, then the
270 * directory is returned and the contained paths are not. A directory
271 * that does not match an exclude pattern will not be returned even if
272 * all of its contents are ignored. In this case, the contents are
273 * returned as individual entries.
275 * If this is set, files and directories that explicitly match an ignore
276 * pattern are reported. Implicitly ignored directories (directories that
277 * do not match an ignore pattern, but whose contents are all ignored)
278 * are not reported, instead all of the contents are reported.
280 DIR_SHOW_IGNORED_TOO_MODE_MATCHING
= 1<<8,
282 DIR_SKIP_NESTED_GIT
= 1<<9
285 /* The number of members in `entries[]` array. */
286 int nr
; /* output only */
288 /* The number of members in `ignored[]` array. */
289 int ignored_nr
; /* output only */
291 /* An array of `struct dir_entry`, each element of which describes a path. */
292 struct dir_entry
**entries
; /* output only */
295 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
296 * `DIR_COLLECT_IGNORED` flags.
298 struct dir_entry
**ignored
; /* output only */
300 /* Enable/update untracked file cache if set */
301 struct untracked_cache
*untracked
;
304 * Deprecated: ls-files is the only allowed caller; all other callers
305 * should leave this as NULL; it pre-dated the
306 * setup_standard_excludes() mechanism that replaces this.
308 * This field tracks the name of the file to be read in each directory
309 * for excluded files (typically `.gitignore`).
311 const char *exclude_per_dir
;
313 struct dir_struct_internal
{
314 /* Keeps track of allocation of `entries[]` array.*/
317 /* Keeps track of allocation of `ignored[]` array. */
321 * We maintain three groups of exclude pattern lists:
323 * EXC_CMDL lists patterns explicitly given on the command line.
324 * EXC_DIRS lists patterns obtained from per-directory ignore
326 * EXC_FILE lists patterns from fallback ignore files, e.g.
327 * - .git/info/exclude
328 * - core.excludesfile
330 * Each group contains multiple exclude lists, a single list
336 struct exclude_list_group exclude_list_group
[3];
339 * Temporary variables which are used during loading of the
340 * per-directory exclude lists.
342 * exclude_stack points to the top of the exclude_stack, and
343 * basebuf contains the full path to the current
344 * (sub)directory in the traversal. Exclude points to the
345 * matching exclude struct if the directory is excluded.
347 struct exclude_stack
*exclude_stack
;
348 struct path_pattern
*pattern
;
349 struct strbuf basebuf
;
351 /* Additional metadata related to 'untracked' */
352 struct oid_stat ss_info_exclude
;
353 struct oid_stat ss_excludes_file
;
354 unsigned unmanaged_exclude_files
;
356 /* Stats about the traversal */
357 unsigned visited_paths
;
358 unsigned visited_directories
;
362 #define DIR_INIT { 0 }
364 struct dirent
*readdir_skip_dot_and_dotdot(DIR *dirp
);
367 * Get the d_type of a dirent. If the d_type is unknown, derive it from
368 * stat.st_mode using the path to the dirent's containing directory (path) and
369 * the name of the dirent itself.
371 * Note that 'path' is assumed to have a trailing slash. It is also modified
372 * in-place during the execution of the function, but is then reverted to its
373 * original value before returning.
375 unsigned char get_dtype(struct dirent
*e
, struct strbuf
*path
);
377 /*Count the number of slashes for string s*/
378 int count_slashes(const char *s
);
381 * The ordering of these constants is significant, with
382 * higher-numbered match types signifying "closer" (i.e. more
383 * specific) matches which will override lower-numbered match types
384 * when populating the seen[] array.
386 #define MATCHED_RECURSIVELY 1
387 #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
388 #define MATCHED_FNMATCH 3
389 #define MATCHED_EXACTLY 4
390 int simple_length(const char *match
);
391 int no_wildcard(const char *string
);
392 char *common_prefix(const struct pathspec
*pathspec
);
393 int report_path_error(const char *ps_matched
, const struct pathspec
*pathspec
);
394 int within_depth(const char *name
, int namelen
, int depth
, int max_depth
);
396 int fill_directory(struct dir_struct
*dir
,
397 struct index_state
*istate
,
398 const struct pathspec
*pathspec
);
399 int read_directory(struct dir_struct
*, struct index_state
*istate
,
400 const char *path
, int len
,
401 const struct pathspec
*pathspec
);
403 enum pattern_match_result
{
407 MATCHED_RECURSIVE
= 2,
411 * Scan the list of patterns to determine if the ordered list
412 * of patterns matches on 'pathname'.
414 * Return 1 for a match, 0 for not matched and -1 for undecided.
416 enum pattern_match_result
path_matches_pattern_list(const char *pathname
,
418 const char *basename
, int *dtype
,
419 struct pattern_list
*pl
,
420 struct index_state
*istate
);
422 int init_sparse_checkout_patterns(struct index_state
*state
);
424 int path_in_sparse_checkout(const char *path
,
425 struct index_state
*istate
);
426 int path_in_cone_mode_sparse_checkout(const char *path
,
427 struct index_state
*istate
);
429 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
,
430 struct index_state
*istate
,
431 const char *pathname
, int len
);
434 * these implement the matching logic for dir.c:excluded_from_list and
435 * attr.c:path_matches()
437 int match_basename(const char *, int,
438 const char *, int, int, unsigned);
439 int match_pathname(const char *, int,
441 const char *, int, int);
443 struct path_pattern
*last_matching_pattern(struct dir_struct
*dir
,
444 struct index_state
*istate
,
445 const char *name
, int *dtype
);
447 int is_excluded(struct dir_struct
*dir
,
448 struct index_state
*istate
,
449 const char *name
, int *dtype
);
451 int pl_hashmap_cmp(const void *unused_cmp_data
,
452 const struct hashmap_entry
*a
,
453 const struct hashmap_entry
*b
,
455 int hashmap_contains_parent(struct hashmap
*map
,
457 struct strbuf
*buffer
);
458 struct pattern_list
*add_pattern_list(struct dir_struct
*dir
,
459 int group_type
, const char *src
);
460 int add_patterns_from_file_to_list(const char *fname
, const char *base
, int baselen
,
461 struct pattern_list
*pl
, struct index_state
*istate
,
463 void add_patterns_from_file(struct dir_struct
*, const char *fname
);
464 int add_patterns_from_blob_to_list(struct object_id
*oid
,
465 const char *base
, int baselen
,
466 struct pattern_list
*pl
);
467 void parse_path_pattern(const char **string
, int *patternlen
, unsigned *flags
, int *nowildcardlen
);
468 void add_pattern(const char *string
, const char *base
,
469 int baselen
, struct pattern_list
*pl
, int srcpos
);
470 void clear_pattern_list(struct pattern_list
*pl
);
471 void dir_clear(struct dir_struct
*dir
);
473 int repo_file_exists(struct repository
*repo
, const char *path
);
474 int file_exists(const char *);
476 int is_inside_dir(const char *dir
);
477 int dir_inside_of(const char *subdir
, const char *dir
);
479 static inline int is_dot_or_dotdot(const char *name
)
481 return (name
[0] == '.' &&
483 (name
[1] == '.' && name
[2] == '\0')));
486 int is_empty_dir(const char *dir
);
489 * Retrieve the "humanish" basename of the given Git URL.
492 * /path/to/repo.git => "repo"
493 * host.xz:foo/.git => "foo"
494 * http://example.com/user/bar.baz => "bar.baz"
496 char *git_url_basename(const char *repo
, int is_bundle
, int is_bare
);
497 void strip_dir_trailing_slashes(char *dir
);
499 void setup_standard_excludes(struct dir_struct
*dir
);
501 char *get_sparse_checkout_filename(void);
502 int get_sparse_checkout_patterns(struct pattern_list
*pl
);
504 /* Constants for remove_dir_recursively: */
507 * If a non-directory is found within path, stop and return an error.
508 * (In this case some empty directories might already have been
511 #define REMOVE_DIR_EMPTY_ONLY 01
514 * If any Git work trees are found within path, skip them without
515 * considering it an error.
517 #define REMOVE_DIR_KEEP_NESTED_GIT 02
519 /* Remove the contents of path, but leave path itself. */
520 #define REMOVE_DIR_KEEP_TOPLEVEL 04
522 /* Remove the_original_cwd too */
523 #define REMOVE_DIR_PURGE_ORIGINAL_CWD 0x08
526 * Remove path and its contents, recursively. flags is a combination
527 * of the above REMOVE_DIR_* constants. Return 0 on success.
529 * This function uses path as temporary scratch space, but restores it
532 int remove_dir_recursively(struct strbuf
*path
, int flag
);
535 * Tries to remove the path, along with leading empty directories so long as
536 * those empty directories are not startup_info->original_cwd. Ignores
539 int remove_path(const char *path
);
541 int fspathcmp(const char *a
, const char *b
);
542 int fspatheq(const char *a
, const char *b
);
543 int fspathncmp(const char *a
, const char *b
, size_t count
);
544 unsigned int fspathhash(const char *str
);
547 * The prefix part of pattern must not contains wildcards.
549 struct pathspec_item
;
550 int git_fnmatch(const struct pathspec_item
*item
,
551 const char *pattern
, const char *string
,
554 int submodule_path_match(struct index_state
*istate
,
555 const struct pathspec
*ps
,
556 const char *submodule_name
,
559 static inline int dir_path_match(struct index_state
*istate
,
560 const struct dir_entry
*ent
,
561 const struct pathspec
*pathspec
,
562 int prefix
, char *seen
)
564 int has_trailing_dir
= ent
->len
&& ent
->name
[ent
->len
- 1] == '/';
565 int len
= has_trailing_dir
? ent
->len
- 1 : ent
->len
;
566 return match_pathspec(istate
, pathspec
, ent
->name
, len
, prefix
, seen
,
570 int cmp_dir_entry(const void *p1
, const void *p2
);
571 int check_dir_entry_contains(const struct dir_entry
*out
, const struct dir_entry
*in
);
573 void untracked_cache_invalidate_path(struct index_state
*, const char *, int safe_path
);
574 void untracked_cache_remove_from_index(struct index_state
*, const char *);
575 void untracked_cache_add_to_index(struct index_state
*, const char *);
577 void free_untracked_cache(struct untracked_cache
*);
578 struct untracked_cache
*read_untracked_extension(const void *data
, unsigned long sz
);
579 void write_untracked_extension(struct strbuf
*out
, struct untracked_cache
*untracked
);
580 void add_untracked_cache(struct index_state
*istate
);
581 void remove_untracked_cache(struct index_state
*istate
);
584 * Connect a worktree to a git directory by creating (or overwriting) a
585 * '.git' file containing the location of the git directory. In the git
586 * directory set the core.worktree setting to indicate where the worktree is.
587 * When `recurse_into_nested` is set, recurse into any nested submodules,
588 * connecting them as well.
590 void connect_work_tree_and_git_dir(const char *work_tree
,
592 int recurse_into_nested
);
593 void relocate_gitdir(const char *path
,
594 const char *old_git_dir
,
595 const char *new_git_dir
);
598 * The "enum path_matches_kind" determines how path_match_flags() will
599 * behave. The flags come in sets, and one (and only one) must be
600 * provided out of each "set":
603 * Path separator is is_dir_sep()
604 * PATH_MATCH_XPLATFORM:
605 * Path separator is is_xplatform_dir_sep()
607 * Do we use is_dir_sep() to check for a directory separator
608 * (*_NATIVE), or do we always check for '/' or '\' (*_XPLATFORM). The
609 * "*_NATIVE" version on Windows is the same as "*_XPLATFORM",
610 * everywhere else "*_NATIVE" means "only /".
612 * PATH_MATCH_STARTS_WITH_DOT_SLASH:
613 * Match a path starting with "./"
614 * PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH:
615 * Match a path starting with "../"
617 * The "/" in the above is adjusted based on the "*_NATIVE" and
618 * "*_XPLATFORM" flags.
620 enum path_match_flags
{
621 PATH_MATCH_NATIVE
= 1 << 0,
622 PATH_MATCH_XPLATFORM
= 1 << 1,
623 PATH_MATCH_STARTS_WITH_DOT_SLASH
= 1 << 2,
624 PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
= 1 << 3,
626 #define PATH_MATCH_KINDS_MASK (PATH_MATCH_STARTS_WITH_DOT_SLASH | \
627 PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH)
628 #define PATH_MATCH_PLATFORM_MASK (PATH_MATCH_NATIVE | PATH_MATCH_XPLATFORM)
631 * path_match_flags() checks if a given "path" matches a given "enum
632 * path_match_flags" criteria.
634 int path_match_flags(const char *const path
, const enum path_match_flags f
);
637 * starts_with_dot_slash_native(): convenience wrapper for
638 * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_SLASH and
641 static inline int starts_with_dot_slash_native(const char *const path
)
643 const enum path_match_flags what
= PATH_MATCH_STARTS_WITH_DOT_SLASH
;
645 return path_match_flags(path
, what
| PATH_MATCH_NATIVE
);
649 * starts_with_dot_slash_native(): convenience wrapper for
650 * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH and
653 static inline int starts_with_dot_dot_slash_native(const char *const path
)
655 const enum path_match_flags what
= PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
;
657 return path_match_flags(path
, what
| PATH_MATCH_NATIVE
);