dir: fix simple typo in comment
[git/raj.git] / dir.h
blob5855c065a610b794b3777405bba6f343230b7ea2
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` and clear it with `memset(&dir, 0,
23 * sizeof(dir))`.
25 * - To add single exclude pattern, call `add_pattern_list()` and then
26 * `add_pattern()`.
28 * - To add patterns from a file (e.g. `.git/info/exclude`), call
29 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`. A
30 * short-hand function `setup_standard_excludes()` can be used to set
31 * up the standard set of exclude settings.
33 * - Set options described in the Data Structure section above.
35 * - Call `read_directory()`.
37 * - Use `dir.entries[]`.
39 * - Call `clear_directory()` when none of the contained elements are no longer in use.
43 struct dir_entry {
44 unsigned int len;
45 char name[FLEX_ARRAY]; /* more */
48 #define PATTERN_FLAG_NODIR 1
49 #define PATTERN_FLAG_ENDSWITH 4
50 #define PATTERN_FLAG_MUSTBEDIR 8
51 #define PATTERN_FLAG_NEGATIVE 16
53 struct path_pattern {
55 * This allows callers of last_matching_pattern() etc.
56 * to determine the origin of the matching pattern.
58 struct pattern_list *pl;
60 const char *pattern;
61 int patternlen;
62 int nowildcardlen;
63 const char *base;
64 int baselen;
65 unsigned flags; /* PATTERN_FLAG_* */
68 * Counting starts from 1 for line numbers in ignore files,
69 * and from -1 decrementing for patterns from CLI args.
71 int srcpos;
74 /* used for hashmaps for cone patterns */
75 struct pattern_entry {
76 struct hashmap_entry ent;
77 char *pattern;
78 size_t patternlen;
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.
88 struct pattern_list {
89 int nr;
90 int alloc;
92 /* remember pointer to exclude file contents so we can free() */
93 char *filebuf;
95 /* origin of list, e.g. path to filename, or descriptive string */
96 const char *src;
98 struct path_pattern **patterns;
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.
106 unsigned use_cone_patterns;
107 unsigned full_cone;
110 * Stores paths where everything starting with those paths
111 * is included.
113 struct hashmap recursive_hashmap;
116 * Used to check single-level parents of blobs.
118 struct hashmap parent_hashmap;
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.
127 struct exclude_stack {
128 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
129 int baselen;
130 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
131 struct untracked_cache_dir *ucd;
134 struct exclude_list_group {
135 int nr, alloc;
136 struct pattern_list *pl;
139 struct oid_stat {
140 struct stat_data stat;
141 struct object_id oid;
142 int valid;
146 * Untracked cache
148 * The following inputs are sufficient to determine what files in a
149 * directory are excluded:
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)
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.
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.
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[])
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;
180 /* all data except 'dirs' in this struct are good */
181 unsigned int valid : 1;
182 unsigned int recurse : 1;
183 /* null object ID means this directory does not have .gitignore */
184 struct object_id exclude_oid;
185 char name[FLEX_ARRAY];
188 struct untracked_cache {
189 struct oid_stat ss_info_exclude;
190 struct oid_stat ss_excludes_file;
191 const char *exclude_per_dir;
192 struct strbuf ident;
194 * dir_struct#flags must match dir_flags or the untracked
195 * cache is ignored.
197 unsigned dir_flags;
198 struct untracked_cache_dir *root;
199 /* Statistics */
200 int dir_created;
201 int gitignore_invalidated;
202 int dir_invalidated;
203 int dir_opened;
204 /* fsmonitor invalidation data */
205 unsigned int use_fsmonitor : 1;
209 * structure is used to pass directory traversal options to the library and to
210 * record the paths discovered. A single `struct dir_struct` is used regardless
211 * of whether or not the traversal recursively descends into subdirectories.
213 struct dir_struct {
215 /* The number of members in `entries[]` array. */
216 int nr;
218 /* Internal use; keeps track of allocation of `entries[]` array.*/
219 int alloc;
221 /* The number of members in `ignored[]` array. */
222 int ignored_nr;
224 int ignored_alloc;
226 /* bit-field of options */
227 enum {
230 * Return just ignored files in `entries[]`, not untracked files.
231 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
233 DIR_SHOW_IGNORED = 1<<0,
235 /* Include a directory that is not tracked. */
236 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
238 /* Do not include a directory that is not tracked and is empty. */
239 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
242 * If set, recurse into a directory that looks like a Git directory.
243 * Otherwise it is shown as a directory.
245 DIR_NO_GITLINKS = 1<<3,
248 * Special mode for git-add. Return ignored files in `ignored[]` and
249 * untracked files in `entries[]`. Only returns ignored files that match
250 * pathspec exactly (no wildcards). Does not recurse into ignored
251 * directories.
253 DIR_COLLECT_IGNORED = 1<<4,
256 * Similar to `DIR_SHOW_IGNORED`, but return ignored files in
257 * `ignored[]` in addition to untracked files in `entries[]`.
258 * This flag is mutually exclusive with `DIR_SHOW_IGNORED`.
260 DIR_SHOW_IGNORED_TOO = 1<<5,
262 DIR_COLLECT_KILLED_ONLY = 1<<6,
265 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
266 * set, the untracked contents of untracked directories are also
267 * returned in `entries[]`.
269 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
272 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
273 * set, returns ignored files and directories that match an exclude
274 * pattern. If a directory matches an exclude pattern, then the
275 * directory is returned and the contained paths are not. A directory
276 * that does not match an exclude pattern will not be returned even if
277 * all of its contents are ignored. In this case, the contents are
278 * returned as individual entries.
280 * If this is set, files and directories that explicitly match an ignore
281 * pattern are reported. Implicitly ignored directories (directories that
282 * do not match an ignore pattern, but whose contents are all ignored)
283 * are not reported, instead all of the contents are reported.
285 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
287 DIR_SKIP_NESTED_GIT = 1<<9
288 } flags;
290 /* An array of `struct dir_entry`, each element of which describes a path. */
291 struct dir_entry **entries;
294 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
295 * `DIR_COLLECT_IGNORED` flags.
297 struct dir_entry **ignored;
300 * The name of the file to be read in each directory for excluded files
301 * (typically `.gitignore`).
303 const char *exclude_per_dir;
306 * We maintain three groups of exclude pattern lists:
308 * EXC_CMDL lists patterns explicitly given on the command line.
309 * EXC_DIRS lists patterns obtained from per-directory ignore files.
310 * EXC_FILE lists patterns from fallback ignore files, e.g.
311 * - .git/info/exclude
312 * - core.excludesfile
314 * Each group contains multiple exclude lists, a single list
315 * per source.
317 #define EXC_CMDL 0
318 #define EXC_DIRS 1
319 #define EXC_FILE 2
320 struct exclude_list_group exclude_list_group[3];
323 * Temporary variables which are used during loading of the
324 * per-directory exclude lists.
326 * exclude_stack points to the top of the exclude_stack, and
327 * basebuf contains the full path to the current
328 * (sub)directory in the traversal. Exclude points to the
329 * matching exclude struct if the directory is excluded.
331 struct exclude_stack *exclude_stack;
332 struct path_pattern *pattern;
333 struct strbuf basebuf;
335 /* Enable untracked file cache if set */
336 struct untracked_cache *untracked;
337 struct oid_stat ss_info_exclude;
338 struct oid_stat ss_excludes_file;
339 unsigned unmanaged_exclude_files;
342 /*Count the number of slashes for string s*/
343 int count_slashes(const char *s);
346 * The ordering of these constants is significant, with
347 * higher-numbered match types signifying "closer" (i.e. more
348 * specific) matches which will override lower-numbered match types
349 * when populating the seen[] array.
351 #define MATCHED_RECURSIVELY 1
352 #define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
353 #define MATCHED_FNMATCH 3
354 #define MATCHED_EXACTLY 4
355 int simple_length(const char *match);
356 int no_wildcard(const char *string);
357 char *common_prefix(const struct pathspec *pathspec);
358 int match_pathspec(const struct index_state *istate,
359 const struct pathspec *pathspec,
360 const char *name, int namelen,
361 int prefix, char *seen, int is_dir);
362 int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
363 int within_depth(const char *name, int namelen, int depth, int max_depth);
365 int fill_directory(struct dir_struct *dir,
366 struct index_state *istate,
367 const struct pathspec *pathspec);
368 int read_directory(struct dir_struct *, struct index_state *istate,
369 const char *path, int len,
370 const struct pathspec *pathspec);
372 enum pattern_match_result {
373 UNDECIDED = -1,
374 NOT_MATCHED = 0,
375 MATCHED = 1,
376 MATCHED_RECURSIVE = 2,
380 * Scan the list of patterns to determine if the ordered list
381 * of patterns matches on 'pathname'.
383 * Return 1 for a match, 0 for not matched and -1 for undecided.
385 enum pattern_match_result path_matches_pattern_list(const char *pathname,
386 int pathlen,
387 const char *basename, int *dtype,
388 struct pattern_list *pl,
389 struct index_state *istate);
390 struct dir_entry *dir_add_ignored(struct dir_struct *dir,
391 struct index_state *istate,
392 const char *pathname, int len);
395 * these implement the matching logic for dir.c:excluded_from_list and
396 * attr.c:path_matches()
398 int match_basename(const char *, int,
399 const char *, int, int, unsigned);
400 int match_pathname(const char *, int,
401 const char *, int,
402 const char *, int, int, unsigned);
404 struct path_pattern *last_matching_pattern(struct dir_struct *dir,
405 struct index_state *istate,
406 const char *name, int *dtype);
408 int is_excluded(struct dir_struct *dir,
409 struct index_state *istate,
410 const char *name, int *dtype);
412 int pl_hashmap_cmp(const void *unused_cmp_data,
413 const struct hashmap_entry *a,
414 const struct hashmap_entry *b,
415 const void *key);
416 int hashmap_contains_parent(struct hashmap *map,
417 const char *path,
418 struct strbuf *buffer);
419 struct pattern_list *add_pattern_list(struct dir_struct *dir,
420 int group_type, const char *src);
421 int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
422 struct pattern_list *pl, struct index_state *istate);
423 void add_patterns_from_file(struct dir_struct *, const char *fname);
424 int add_patterns_from_blob_to_list(struct object_id *oid,
425 const char *base, int baselen,
426 struct pattern_list *pl);
427 void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
428 void add_pattern(const char *string, const char *base,
429 int baselen, struct pattern_list *pl, int srcpos);
430 void clear_pattern_list(struct pattern_list *pl);
431 void clear_directory(struct dir_struct *dir);
433 int repo_file_exists(struct repository *repo, const char *path);
434 int file_exists(const char *);
436 int is_inside_dir(const char *dir);
437 int dir_inside_of(const char *subdir, const char *dir);
439 static inline int is_dot_or_dotdot(const char *name)
441 return (name[0] == '.' &&
442 (name[1] == '\0' ||
443 (name[1] == '.' && name[2] == '\0')));
446 int is_empty_dir(const char *dir);
448 void setup_standard_excludes(struct dir_struct *dir);
451 /* Constants for remove_dir_recursively: */
454 * If a non-directory is found within path, stop and return an error.
455 * (In this case some empty directories might already have been
456 * removed.)
458 #define REMOVE_DIR_EMPTY_ONLY 01
461 * If any Git work trees are found within path, skip them without
462 * considering it an error.
464 #define REMOVE_DIR_KEEP_NESTED_GIT 02
466 /* Remove the contents of path, but leave path itself. */
467 #define REMOVE_DIR_KEEP_TOPLEVEL 04
470 * Remove path and its contents, recursively. flags is a combination
471 * of the above REMOVE_DIR_* constants. Return 0 on success.
473 * This function uses path as temporary scratch space, but restores it
474 * before returning.
476 int remove_dir_recursively(struct strbuf *path, int flag);
478 /* tries to remove the path with empty directories along it, ignores ENOENT */
479 int remove_path(const char *path);
481 int fspathcmp(const char *a, const char *b);
482 int fspathncmp(const char *a, const char *b, size_t count);
485 * The prefix part of pattern must not contains wildcards.
487 struct pathspec_item;
488 int git_fnmatch(const struct pathspec_item *item,
489 const char *pattern, const char *string,
490 int prefix);
492 int submodule_path_match(const struct index_state *istate,
493 const struct pathspec *ps,
494 const char *submodule_name,
495 char *seen);
497 static inline int ce_path_match(const struct index_state *istate,
498 const struct cache_entry *ce,
499 const struct pathspec *pathspec,
500 char *seen)
502 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
503 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
506 static inline int dir_path_match(const struct index_state *istate,
507 const struct dir_entry *ent,
508 const struct pathspec *pathspec,
509 int prefix, char *seen)
511 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
512 int len = has_trailing_dir ? ent->len - 1 : ent->len;
513 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
514 has_trailing_dir);
517 int cmp_dir_entry(const void *p1, const void *p2);
518 int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
520 void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
521 void untracked_cache_remove_from_index(struct index_state *, const char *);
522 void untracked_cache_add_to_index(struct index_state *, const char *);
524 void free_untracked_cache(struct untracked_cache *);
525 struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
526 void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
527 void add_untracked_cache(struct index_state *istate);
528 void remove_untracked_cache(struct index_state *istate);
531 * Connect a worktree to a git directory by creating (or overwriting) a
532 * '.git' file containing the location of the git directory. In the git
533 * directory set the core.worktree setting to indicate where the worktree is.
534 * When `recurse_into_nested` is set, recurse into any nested submodules,
535 * connecting them as well.
537 void connect_work_tree_and_git_dir(const char *work_tree,
538 const char *git_dir,
539 int recurse_into_nested);
540 void relocate_gitdir(const char *path,
541 const char *old_git_dir,
542 const char *new_git_dir);
543 #endif