]> git.ipfire.org Git - thirdparty/git.git/blame - dir.h
Merge branch 'en/merge-ort-api-null-impl'
[thirdparty/git.git] / dir.h
CommitLineData
453ec4bd
LT
1#ifndef DIR_H
2#define DIR_H
3
70c369cd 4#include "cache.h"
96cc8ab5 5#include "hashmap.h"
eb41775e
JH
6#include "strbuf.h"
7
266f03ec
HW
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.
12 */
13
14/**
15 * Calling sequence
16 * ----------------
17 *
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.
21 *
eceba532 22 * - Prepare `struct dir_struct dir` using `dir_init()` function.
266f03ec
HW
23 *
24 * - To add single exclude pattern, call `add_pattern_list()` and then
25 * `add_pattern()`.
26 *
27 * - To add patterns from a file (e.g. `.git/info/exclude`), call
eceba532 28 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
266f03ec 29 *
eceba532
EN
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.
266f03ec 33 *
eceba532 34 * - Call `fill_directory()`.
266f03ec 35 *
eceba532 36 * - Use `dir.entries[]` and `dir.ignored[]`.
266f03ec 37 *
eceba532 38 * - Call `dir_clear()` when the contained elements are no longer in use.
266f03ec
HW
39 *
40 */
41
453ec4bd 42struct dir_entry {
e96980ef 43 unsigned int len;
453ec4bd
LT
44 char name[FLEX_ARRAY]; /* more */
45};
46
4ff89ee5
DS
47#define PATTERN_FLAG_NODIR 1
48#define PATTERN_FLAG_ENDSWITH 4
49#define PATTERN_FLAG_MUSTBEDIR 8
50#define PATTERN_FLAG_NEGATIVE 16
68492fc7 51
ab8db613 52struct path_pattern {
709359c8 53 /*
65edd96a 54 * This allows callers of last_matching_pattern() etc.
709359c8
NTND
55 * to determine the origin of the matching pattern.
56 */
caa3d554 57 struct pattern_list *pl;
709359c8
NTND
58
59 const char *pattern;
60 int patternlen;
61 int nowildcardlen;
62 const char *base;
63 int baselen;
4ff89ee5 64 unsigned flags; /* PATTERN_FLAG_* */
709359c8
NTND
65
66 /*
67 * Counting starts from 1 for line numbers in ignore files,
68 * and from -1 decrementing for patterns from CLI args.
69 */
70 int srcpos;
71};
72
96cc8ab5
DS
73/* used for hashmaps for cone patterns */
74struct pattern_entry {
75 struct hashmap_entry ent;
76 char *pattern;
77 size_t patternlen;
78};
79
95a68344 80/*
c082df24
AS
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.
95a68344 86 */
caa3d554 87struct pattern_list {
453ec4bd
LT
88 int nr;
89 int alloc;
c04318e4 90
c082df24
AS
91 /* remember pointer to exclude file contents so we can free() */
92 char *filebuf;
93
c04318e4
AS
94 /* origin of list, e.g. path to filename, or descriptive string */
95 const char *src;
96
ab8db613 97 struct path_pattern **patterns;
96cc8ab5
DS
98
99 /*
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.
104 */
105 unsigned use_cone_patterns;
106 unsigned full_cone;
107
108 /*
109 * Stores paths where everything starting with those paths
110 * is included.
111 */
112 struct hashmap recursive_hashmap;
113
114 /*
115 * Used to check single-level parents of blobs.
116 */
117 struct hashmap parent_hashmap;
453ec4bd
LT
118};
119
95a68344
AS
120/*
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.
125 */
63d285c8 126struct exclude_stack {
95a68344 127 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
63d285c8 128 int baselen;
c082df24 129 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
0dcb8d7f 130 struct untracked_cache_dir *ucd;
c082df24
AS
131};
132
133struct exclude_list_group {
134 int nr, alloc;
caa3d554 135 struct pattern_list *pl;
63d285c8
JH
136};
137
4b33e602 138struct oid_stat {
55fe6f51 139 struct stat_data stat;
4b33e602 140 struct object_id oid;
55fe6f51
NTND
141 int valid;
142};
143
0dcb8d7f
NTND
144/*
145 * Untracked cache
146 *
147 * The following inputs are sufficient to determine what files in a
148 * directory are excluded:
149 *
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)
159 *
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.
163 *
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.
167 *
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[])
171 */
172struct 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;
26cb0182 179 /* all data except 'dirs' in this struct are good */
ccad261f 180 unsigned int valid : 1;
26cb0182 181 unsigned int recurse : 1;
70c369cd 182 /* null object ID means this directory does not have .gitignore */
183 struct object_id exclude_oid;
0dcb8d7f
NTND
184 char name[FLEX_ARRAY];
185};
186
187struct untracked_cache {
4b33e602
PO
188 struct oid_stat ss_info_exclude;
189 struct oid_stat ss_excludes_file;
0dcb8d7f 190 const char *exclude_per_dir;
1e8fef60 191 struct strbuf ident;
0dcb8d7f
NTND
192 /*
193 * dir_struct#flags must match dir_flags or the untracked
194 * cache is ignored.
195 */
196 unsigned dir_flags;
197 struct untracked_cache_dir *root;
198 /* Statistics */
199 int dir_created;
ccad261f 200 int gitignore_invalidated;
91a2288b
NTND
201 int dir_invalidated;
202 int dir_opened;
883e248b
BP
203 /* fsmonitor invalidation data */
204 unsigned int use_fsmonitor : 1;
0dcb8d7f
NTND
205};
206
266f03ec
HW
207/**
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.
211 */
453ec4bd 212struct dir_struct {
266f03ec
HW
213
214 /* The number of members in `entries[]` array. */
215 int nr;
216
217 /* Internal use; keeps track of allocation of `entries[]` array.*/
218 int alloc;
219
220 /* The number of members in `ignored[]` array. */
221 int ignored_nr;
222
223 int ignored_alloc;
224
225 /* bit-field of options */
7c4c97c0 226 enum {
266f03ec
HW
227
228 /**
229 * Return just ignored files in `entries[]`, not untracked files.
230 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
231 */
7c4c97c0 232 DIR_SHOW_IGNORED = 1<<0,
266f03ec
HW
233
234 /* Include a directory that is not tracked. */
7c4c97c0 235 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
266f03ec
HW
236
237 /* Do not include a directory that is not tracked and is empty. */
7c4c97c0 238 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
266f03ec
HW
239
240 /**
241 * If set, recurse into a directory that looks like a Git directory.
242 * Otherwise it is shown as a directory.
243 */
7c4c97c0 244 DIR_NO_GITLINKS = 1<<3,
266f03ec
HW
245
246 /**
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.
251 */
0aaf62b6 252 DIR_COLLECT_IGNORED = 1<<4,
266f03ec
HW
253
254 /**
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`.
258 */
2eac2a4c 259 DIR_SHOW_IGNORED_TOO = 1<<5,
266f03ec 260
fb898888 261 DIR_COLLECT_KILLED_ONLY = 1<<6,
266f03ec
HW
262
263 /**
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[]`.
267 */
eec0f7f2 268 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
266f03ec
HW
269
270 /**
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.
278 *
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.
283 */
09487f2c 284 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
266f03ec 285
09487f2c 286 DIR_SKIP_NESTED_GIT = 1<<9
7c4c97c0 287 } flags;
266f03ec
HW
288
289 /* An array of `struct dir_entry`, each element of which describes a path. */
453ec4bd 290 struct dir_entry **entries;
266f03ec
HW
291
292 /**
293 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
294 * `DIR_COLLECT_IGNORED` flags.
295 */
2abd31b0 296 struct dir_entry **ignored;
453ec4bd 297
266f03ec
HW
298 /**
299 * The name of the file to be read in each directory for excluded files
300 * (typically `.gitignore`).
301 */
453ec4bd 302 const char *exclude_per_dir;
c082df24 303
63d285c8 304 /*
c082df24
AS
305 * We maintain three groups of exclude pattern lists:
306 *
63d285c8
JH
307 * EXC_CMDL lists patterns explicitly given on the command line.
308 * EXC_DIRS lists patterns obtained from per-directory ignore files.
c082df24
AS
309 * EXC_FILE lists patterns from fallback ignore files, e.g.
310 * - .git/info/exclude
311 * - core.excludesfile
312 *
313 * Each group contains multiple exclude lists, a single list
314 * per source.
63d285c8
JH
315 */
316#define EXC_CMDL 0
317#define EXC_DIRS 1
318#define EXC_FILE 2
c082df24 319 struct exclude_list_group exclude_list_group[3];
63d285c8 320
95a68344
AS
321 /*
322 * Temporary variables which are used during loading of the
323 * per-directory exclude lists.
324 *
325 * exclude_stack points to the top of the exclude_stack, and
326 * basebuf contains the full path to the current
95c6f271
KB
327 * (sub)directory in the traversal. Exclude points to the
328 * matching exclude struct if the directory is excluded.
95a68344 329 */
63d285c8 330 struct exclude_stack *exclude_stack;
ab8db613 331 struct path_pattern *pattern;
aceb9429 332 struct strbuf basebuf;
0dcb8d7f
NTND
333
334 /* Enable untracked file cache if set */
335 struct untracked_cache *untracked;
4b33e602
PO
336 struct oid_stat ss_info_exclude;
337 struct oid_stat ss_excludes_file;
ccad261f 338 unsigned unmanaged_exclude_files;
453ec4bd
LT
339};
340
e0556a92 341/*Count the number of slashes for string s*/
55454427 342int count_slashes(const char *s);
e0556a92 343
52ed1894
AS
344/*
345 * The ordering of these constants is significant, with
346 * higher-numbered match types signifying "closer" (i.e. more
347 * specific) matches which will override lower-numbered match types
348 * when populating the seen[] array.
349 */
e813d50e 350#define MATCHED_RECURSIVELY 1
89a1f4aa
EN
351#define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
352#define MATCHED_FNMATCH 3
353#define MATCHED_EXACTLY 4
55454427
DL
354int simple_length(const char *match);
355int no_wildcard(const char *string);
356char *common_prefix(const struct pathspec *pathspec);
357int match_pathspec(const struct index_state *istate,
ad6dad09
DL
358 const struct pathspec *pathspec,
359 const char *name, int namelen,
360 int prefix, char *seen, int is_dir);
4aeeef37 361int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
55454427 362int within_depth(const char *name, int namelen, int depth, int max_depth);
3c6a370b 363
eceba532
EN
364void dir_init(struct dir_struct *dir);
365
55454427 366int fill_directory(struct dir_struct *dir,
ad6dad09
DL
367 struct index_state *istate,
368 const struct pathspec *pathspec);
55454427 369int read_directory(struct dir_struct *, struct index_state *istate,
ad6dad09
DL
370 const char *path, int len,
371 const struct pathspec *pathspec);
f8a9d428 372
468ce99b
DS
373enum pattern_match_result {
374 UNDECIDED = -1,
375 NOT_MATCHED = 0,
376 MATCHED = 1,
eb42feca 377 MATCHED_RECURSIVE = 2,
468ce99b
DS
378};
379
380/*
381 * Scan the list of patterns to determine if the ordered list
382 * of patterns matches on 'pathname'.
383 *
384 * Return 1 for a match, 0 for not matched and -1 for undecided.
385 */
386enum pattern_match_result path_matches_pattern_list(const char *pathname,
387 int pathlen,
388 const char *basename, int *dtype,
389 struct pattern_list *pl,
390 struct index_state *istate);
9e58beca
BW
391struct dir_entry *dir_add_ignored(struct dir_struct *dir,
392 struct index_state *istate,
393 const char *pathname, int len);
eb41775e 394
82dce998
NTND
395/*
396 * these implement the matching logic for dir.c:excluded_from_list and
397 * attr.c:path_matches()
398 */
55454427 399int match_basename(const char *, int,
ad6dad09 400 const char *, int, int, unsigned);
55454427 401int match_pathname(const char *, int,
ad6dad09
DL
402 const char *, int,
403 const char *, int, int, unsigned);
82dce998 404
65edd96a
DS
405struct path_pattern *last_matching_pattern(struct dir_struct *dir,
406 struct index_state *istate,
407 const char *name, int *dtype);
782cd4c0 408
55454427 409int is_excluded(struct dir_struct *dir,
ad6dad09
DL
410 struct index_state *istate,
411 const char *name, int *dtype);
eb41775e 412
af09ce24
DS
413int pl_hashmap_cmp(const void *unused_cmp_data,
414 const struct hashmap_entry *a,
415 const struct hashmap_entry *b,
416 const void *key);
96cc8ab5
DS
417int hashmap_contains_parent(struct hashmap *map,
418 const char *path,
419 struct strbuf *buffer);
65edd96a 420struct pattern_list *add_pattern_list(struct dir_struct *dir,
ad6dad09 421 int group_type, const char *src);
65edd96a 422int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
caa3d554 423 struct pattern_list *pl, struct index_state *istate);
65edd96a
DS
424void add_patterns_from_file(struct dir_struct *, const char *fname);
425int add_patterns_from_blob_to_list(struct object_id *oid,
ad6dad09 426 const char *base, int baselen,
caa3d554 427 struct pattern_list *pl);
65edd96a
DS
428void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
429void add_pattern(const char *string, const char *base,
caa3d554 430 int baselen, struct pattern_list *pl, int srcpos);
65edd96a 431void clear_pattern_list(struct pattern_list *pl);
eceba532 432void dir_clear(struct dir_struct *dir);
0488481e
NTND
433
434int repo_file_exists(struct repository *repo, const char *path);
435int file_exists(const char *);
453ec4bd 436
55454427
DL
437int is_inside_dir(const char *dir);
438int dir_inside_of(const char *subdir, const char *dir);
e6636747 439
8ca12c0d
AP
440static inline int is_dot_or_dotdot(const char *name)
441{
442 return (name[0] == '.' &&
443 (name[1] == '\0' ||
444 (name[1] == '.' && name[2] == '\0')));
445}
446
55454427 447int is_empty_dir(const char *dir);
55892d23 448
55454427 449void setup_standard_excludes(struct dir_struct *dir);
a0f4afbe 450
728af283
MH
451
452/* Constants for remove_dir_recursively: */
453
454/*
455 * If a non-directory is found within path, stop and return an error.
456 * (In this case some empty directories might already have been
457 * removed.)
458 */
a0f4afbe 459#define REMOVE_DIR_EMPTY_ONLY 01
728af283
MH
460
461/*
462 * If any Git work trees are found within path, skip them without
463 * considering it an error.
464 */
a0f4afbe 465#define REMOVE_DIR_KEEP_NESTED_GIT 02
728af283
MH
466
467/* Remove the contents of path, but leave path itself. */
c844a803 468#define REMOVE_DIR_KEEP_TOPLEVEL 04
728af283
MH
469
470/*
471 * Remove path and its contents, recursively. flags is a combination
472 * of the above REMOVE_DIR_* constants. Return 0 on success.
473 *
474 * This function uses path as temporary scratch space, but restores it
475 * before returning.
476 */
55454427 477int remove_dir_recursively(struct strbuf *path, int flag);
7155b727 478
4a92d1bf 479/* tries to remove the path with empty directories along it, ignores ENOENT */
55454427 480int remove_path(const char *path);
4a92d1bf 481
55454427
DL
482int fspathcmp(const char *a, const char *b);
483int fspathncmp(const char *a, const char *b, size_t count);
8cf2a84e 484
5d74762d
NTND
485/*
486 * The prefix part of pattern must not contains wildcards.
487 */
bd30c2e4 488struct pathspec_item;
55454427 489int git_fnmatch(const struct pathspec_item *item,
ad6dad09
DL
490 const char *pattern, const char *string,
491 int prefix);
5d74762d 492
55454427 493int submodule_path_match(const struct index_state *istate,
ad6dad09
DL
494 const struct pathspec *ps,
495 const char *submodule_name,
496 char *seen);
75a6315f 497
6d2df284
NTND
498static inline int ce_path_match(const struct index_state *istate,
499 const struct cache_entry *ce,
429bb40a
NTND
500 const struct pathspec *pathspec,
501 char *seen)
502{
6d2df284 503 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
ae8d0824 504 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
429bb40a
NTND
505}
506
6d2df284
NTND
507static inline int dir_path_match(const struct index_state *istate,
508 const struct dir_entry *ent,
ebb32893
NTND
509 const struct pathspec *pathspec,
510 int prefix, char *seen)
511{
ae8d0824
NTND
512 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
513 int len = has_trailing_dir ? ent->len - 1 : ent->len;
6d2df284 514 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
ae8d0824 515 has_trailing_dir);
ebb32893
NTND
516}
517
bbf504a9
SL
518int cmp_dir_entry(const void *p1, const void *p2);
519int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
520
0cacebf0 521void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
e931371a
NTND
522void untracked_cache_remove_from_index(struct index_state *, const char *);
523void untracked_cache_add_to_index(struct index_state *, const char *);
524
f9e6c649
NTND
525void free_untracked_cache(struct untracked_cache *);
526struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
83c094ad 527void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
4a4ca479 528void add_untracked_cache(struct index_state *istate);
07b29bfd 529void remove_untracked_cache(struct index_state *istate);
da62f786
SB
530
531/*
532 * Connect a worktree to a git directory by creating (or overwriting) a
533 * '.git' file containing the location of the git directory. In the git
534 * directory set the core.worktree setting to indicate where the worktree is.
535 * When `recurse_into_nested` is set, recurse into any nested submodules,
536 * connecting them as well.
537 */
55454427 538void connect_work_tree_and_git_dir(const char *work_tree,
ad6dad09
DL
539 const char *git_dir,
540 int recurse_into_nested);
55454427 541void relocate_gitdir(const char *path,
ad6dad09
DL
542 const char *old_git_dir,
543 const char *new_git_dir);
453ec4bd 544#endif