]> git.ipfire.org Git - thirdparty/git.git/blame - dir.h
dir: move doc to dir.h
[thirdparty/git.git] / dir.h
CommitLineData
453ec4bd
LT
1#ifndef DIR_H
2#define DIR_H
3
70c369cd 4#include "cache.h"
eb41775e
JH
5#include "strbuf.h"
6
266f03ec
HW
7/**
8 * The directory listing API is used to enumerate paths in the work tree,
9 * optionally taking `.git/info/exclude` and `.gitignore` files per directory
10 * into account.
11 */
12
13/**
14 * Calling sequence
15 * ----------------
16 *
17 * Note: The index may be checked for .gitignore files that are
18 * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have
19 * loaded the index first.
20 *
21 * - Prepare `struct dir_struct dir` and clear it with `memset(&dir, 0,
22 * sizeof(dir))`.
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
28 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`. A
29 * short-hand function `setup_standard_excludes()` can be used to set
30 * up the standard set of exclude settings.
31 *
32 * - Set options described in the Data Structure section above.
33 *
34 * - Call `read_directory()`.
35 *
36 * - Use `dir.entries[]`.
37 *
38 * - Call `clear_directory()` when none of the contained elements are no longer in use.
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
95a68344 73/*
c082df24
AS
74 * Each excludes file will be parsed into a fresh exclude_list which
75 * is appended to the relevant exclude_list_group (either EXC_DIRS or
76 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
77 * can also be used to represent the list of --exclude values passed
78 * via CLI args.
95a68344 79 */
caa3d554 80struct pattern_list {
453ec4bd
LT
81 int nr;
82 int alloc;
c04318e4 83
c082df24
AS
84 /* remember pointer to exclude file contents so we can free() */
85 char *filebuf;
86
c04318e4
AS
87 /* origin of list, e.g. path to filename, or descriptive string */
88 const char *src;
89
ab8db613 90 struct path_pattern **patterns;
453ec4bd
LT
91};
92
95a68344
AS
93/*
94 * The contents of the per-directory exclude files are lazily read on
95 * demand and then cached in memory, one per exclude_stack struct, in
96 * order to avoid opening and parsing each one every time that
97 * directory is traversed.
98 */
63d285c8 99struct exclude_stack {
95a68344 100 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
63d285c8 101 int baselen;
c082df24 102 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
0dcb8d7f 103 struct untracked_cache_dir *ucd;
c082df24
AS
104};
105
106struct exclude_list_group {
107 int nr, alloc;
caa3d554 108 struct pattern_list *pl;
63d285c8
JH
109};
110
4b33e602 111struct oid_stat {
55fe6f51 112 struct stat_data stat;
4b33e602 113 struct object_id oid;
55fe6f51
NTND
114 int valid;
115};
116
0dcb8d7f
NTND
117/*
118 * Untracked cache
119 *
120 * The following inputs are sufficient to determine what files in a
121 * directory are excluded:
122 *
123 * - The list of files and directories of the directory in question
124 * - The $GIT_DIR/index
125 * - dir_struct flags
126 * - The content of $GIT_DIR/info/exclude
127 * - The content of core.excludesfile
128 * - The content (or the lack) of .gitignore of all parent directories
129 * from $GIT_WORK_TREE
130 * - The check_only flag in read_directory_recursive (for
131 * DIR_HIDE_EMPTY_DIRECTORIES)
132 *
133 * The first input can be checked using directory mtime. In many
134 * filesystems, directory mtime (stat_data field) is updated when its
135 * files or direct subdirs are added or removed.
136 *
137 * The second one can be hooked from cache_tree_invalidate_path().
138 * Whenever a file (or a submodule) is added or removed from a
139 * directory, we invalidate that directory.
140 *
141 * The remaining inputs are easy, their SHA-1 could be used to verify
142 * their contents (exclude_sha1[], info_exclude_sha1[] and
143 * excludes_file_sha1[])
144 */
145struct untracked_cache_dir {
146 struct untracked_cache_dir **dirs;
147 char **untracked;
148 struct stat_data stat_data;
149 unsigned int untracked_alloc, dirs_nr, dirs_alloc;
150 unsigned int untracked_nr;
151 unsigned int check_only : 1;
26cb0182 152 /* all data except 'dirs' in this struct are good */
ccad261f 153 unsigned int valid : 1;
26cb0182 154 unsigned int recurse : 1;
70c369cd 155 /* null object ID means this directory does not have .gitignore */
156 struct object_id exclude_oid;
0dcb8d7f
NTND
157 char name[FLEX_ARRAY];
158};
159
160struct untracked_cache {
4b33e602
PO
161 struct oid_stat ss_info_exclude;
162 struct oid_stat ss_excludes_file;
0dcb8d7f 163 const char *exclude_per_dir;
1e8fef60 164 struct strbuf ident;
0dcb8d7f
NTND
165 /*
166 * dir_struct#flags must match dir_flags or the untracked
167 * cache is ignored.
168 */
169 unsigned dir_flags;
170 struct untracked_cache_dir *root;
171 /* Statistics */
172 int dir_created;
ccad261f 173 int gitignore_invalidated;
91a2288b
NTND
174 int dir_invalidated;
175 int dir_opened;
883e248b
BP
176 /* fsmonitor invalidation data */
177 unsigned int use_fsmonitor : 1;
0dcb8d7f
NTND
178};
179
266f03ec
HW
180/**
181 * structure is used to pass directory traversal options to the library and to
182 * record the paths discovered. A single `struct dir_struct` is used regardless
183 * of whether or not the traversal recursively descends into subdirectories.
184 */
453ec4bd 185struct dir_struct {
266f03ec
HW
186
187 /* The number of members in `entries[]` array. */
188 int nr;
189
190 /* Internal use; keeps track of allocation of `entries[]` array.*/
191 int alloc;
192
193 /* The number of members in `ignored[]` array. */
194 int ignored_nr;
195
196 int ignored_alloc;
197
198 /* bit-field of options */
7c4c97c0 199 enum {
266f03ec
HW
200
201 /**
202 * Return just ignored files in `entries[]`, not untracked files.
203 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
204 */
7c4c97c0 205 DIR_SHOW_IGNORED = 1<<0,
266f03ec
HW
206
207 /* Include a directory that is not tracked. */
7c4c97c0 208 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
266f03ec
HW
209
210 /* Do not include a directory that is not tracked and is empty. */
7c4c97c0 211 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
266f03ec
HW
212
213 /**
214 * If set, recurse into a directory that looks like a Git directory.
215 * Otherwise it is shown as a directory.
216 */
7c4c97c0 217 DIR_NO_GITLINKS = 1<<3,
266f03ec
HW
218
219 /**
220 * Special mode for git-add. Return ignored files in `ignored[]` and
221 * untracked files in `entries[]`. Only returns ignored files that match
222 * pathspec exactly (no wildcards). Does not recurse into ignored
223 * directories.
224 */
0aaf62b6 225 DIR_COLLECT_IGNORED = 1<<4,
266f03ec
HW
226
227 /**
228 * Similar to `DIR_SHOW_IGNORED`, but return ignored files in
229 * `ignored[]` in addition to untracked files in `entries[]`.
230 * This flag is mutually exclusive with `DIR_SHOW_IGNORED`.
231 */
2eac2a4c 232 DIR_SHOW_IGNORED_TOO = 1<<5,
266f03ec 233
fb898888 234 DIR_COLLECT_KILLED_ONLY = 1<<6,
266f03ec
HW
235
236 /**
237 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
238 * set, the untracked contents of untracked directories are also
239 * returned in `entries[]`.
240 */
eec0f7f2 241 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
266f03ec
HW
242
243 /**
244 * Only has meaning if `DIR_SHOW_IGNORED_TOO` is also set; if this is
245 * set, returns ignored files and directories that match an exclude
246 * pattern. If a directory matches an exclude pattern, then the
247 * directory is returned and the contained paths are not. A directory
248 * that does not match an exclude pattern will not be returned even if
249 * all of its contents are ignored. In this case, the contents are
250 * returned as individual entries.
251 *
252 * If this is set, files and directories that explicitly match an ignore
253 * pattern are reported. Implicitly ignored directories (directories that
254 * do not match an ignore pattern, but whose contents are all ignored)
255 * are not reported, instead all of the contents are reported.
256 */
09487f2c 257 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
266f03ec 258
09487f2c 259 DIR_SKIP_NESTED_GIT = 1<<9
7c4c97c0 260 } flags;
266f03ec
HW
261
262 /* An array of `struct dir_entry`, each element of which describes a path. */
453ec4bd 263 struct dir_entry **entries;
266f03ec
HW
264
265 /**
266 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
267 * `DIR_COLLECT_IGNORED` flags.
268 */
2abd31b0 269 struct dir_entry **ignored;
453ec4bd 270
266f03ec
HW
271 /**
272 * The name of the file to be read in each directory for excluded files
273 * (typically `.gitignore`).
274 */
453ec4bd 275 const char *exclude_per_dir;
c082df24 276
63d285c8 277 /*
c082df24
AS
278 * We maintain three groups of exclude pattern lists:
279 *
63d285c8
JH
280 * EXC_CMDL lists patterns explicitly given on the command line.
281 * EXC_DIRS lists patterns obtained from per-directory ignore files.
c082df24
AS
282 * EXC_FILE lists patterns from fallback ignore files, e.g.
283 * - .git/info/exclude
284 * - core.excludesfile
285 *
286 * Each group contains multiple exclude lists, a single list
287 * per source.
63d285c8
JH
288 */
289#define EXC_CMDL 0
290#define EXC_DIRS 1
291#define EXC_FILE 2
c082df24 292 struct exclude_list_group exclude_list_group[3];
63d285c8 293
95a68344
AS
294 /*
295 * Temporary variables which are used during loading of the
296 * per-directory exclude lists.
297 *
298 * exclude_stack points to the top of the exclude_stack, and
299 * basebuf contains the full path to the current
95c6f271
KB
300 * (sub)directory in the traversal. Exclude points to the
301 * matching exclude struct if the directory is excluded.
95a68344 302 */
63d285c8 303 struct exclude_stack *exclude_stack;
ab8db613 304 struct path_pattern *pattern;
aceb9429 305 struct strbuf basebuf;
0dcb8d7f
NTND
306
307 /* Enable untracked file cache if set */
308 struct untracked_cache *untracked;
4b33e602
PO
309 struct oid_stat ss_info_exclude;
310 struct oid_stat ss_excludes_file;
ccad261f 311 unsigned unmanaged_exclude_files;
453ec4bd
LT
312};
313
e0556a92 314/*Count the number of slashes for string s*/
55454427 315int count_slashes(const char *s);
e0556a92 316
52ed1894
AS
317/*
318 * The ordering of these constants is significant, with
319 * higher-numbered match types signifying "closer" (i.e. more
320 * specific) matches which will override lower-numbered match types
321 * when populating the seen[] array.
322 */
e813d50e 323#define MATCHED_RECURSIVELY 1
89a1f4aa
EN
324#define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
325#define MATCHED_FNMATCH 3
326#define MATCHED_EXACTLY 4
55454427
DL
327int simple_length(const char *match);
328int no_wildcard(const char *string);
329char *common_prefix(const struct pathspec *pathspec);
330int match_pathspec(const struct index_state *istate,
ad6dad09
DL
331 const struct pathspec *pathspec,
332 const char *name, int namelen,
333 int prefix, char *seen, int is_dir);
4aeeef37 334int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
55454427 335int within_depth(const char *name, int namelen, int depth, int max_depth);
3c6a370b 336
55454427 337int fill_directory(struct dir_struct *dir,
ad6dad09
DL
338 struct index_state *istate,
339 const struct pathspec *pathspec);
55454427 340int read_directory(struct dir_struct *, struct index_state *istate,
ad6dad09
DL
341 const char *path, int len,
342 const struct pathspec *pathspec);
f8a9d428 343
468ce99b
DS
344enum pattern_match_result {
345 UNDECIDED = -1,
346 NOT_MATCHED = 0,
347 MATCHED = 1,
348};
349
350/*
351 * Scan the list of patterns to determine if the ordered list
352 * of patterns matches on 'pathname'.
353 *
354 * Return 1 for a match, 0 for not matched and -1 for undecided.
355 */
356enum pattern_match_result path_matches_pattern_list(const char *pathname,
357 int pathlen,
358 const char *basename, int *dtype,
359 struct pattern_list *pl,
360 struct index_state *istate);
9e58beca
BW
361struct dir_entry *dir_add_ignored(struct dir_struct *dir,
362 struct index_state *istate,
363 const char *pathname, int len);
eb41775e 364
82dce998
NTND
365/*
366 * these implement the matching logic for dir.c:excluded_from_list and
367 * attr.c:path_matches()
368 */
55454427 369int match_basename(const char *, int,
ad6dad09 370 const char *, int, int, unsigned);
55454427 371int match_pathname(const char *, int,
ad6dad09
DL
372 const char *, int,
373 const char *, int, int, unsigned);
82dce998 374
65edd96a
DS
375struct path_pattern *last_matching_pattern(struct dir_struct *dir,
376 struct index_state *istate,
377 const char *name, int *dtype);
782cd4c0 378
55454427 379int is_excluded(struct dir_struct *dir,
ad6dad09
DL
380 struct index_state *istate,
381 const char *name, int *dtype);
eb41775e 382
65edd96a 383struct pattern_list *add_pattern_list(struct dir_struct *dir,
ad6dad09 384 int group_type, const char *src);
65edd96a 385int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
caa3d554 386 struct pattern_list *pl, struct index_state *istate);
65edd96a
DS
387void add_patterns_from_file(struct dir_struct *, const char *fname);
388int add_patterns_from_blob_to_list(struct object_id *oid,
ad6dad09 389 const char *base, int baselen,
caa3d554 390 struct pattern_list *pl);
65edd96a
DS
391void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
392void add_pattern(const char *string, const char *base,
caa3d554 393 int baselen, struct pattern_list *pl, int srcpos);
65edd96a 394void clear_pattern_list(struct pattern_list *pl);
55454427 395void clear_directory(struct dir_struct *dir);
0488481e
NTND
396
397int repo_file_exists(struct repository *repo, const char *path);
398int file_exists(const char *);
453ec4bd 399
55454427
DL
400int is_inside_dir(const char *dir);
401int dir_inside_of(const char *subdir, const char *dir);
e6636747 402
8ca12c0d
AP
403static inline int is_dot_or_dotdot(const char *name)
404{
405 return (name[0] == '.' &&
406 (name[1] == '\0' ||
407 (name[1] == '.' && name[2] == '\0')));
408}
409
55454427 410int is_empty_dir(const char *dir);
55892d23 411
55454427 412void setup_standard_excludes(struct dir_struct *dir);
a0f4afbe 413
728af283
MH
414
415/* Constants for remove_dir_recursively: */
416
417/*
418 * If a non-directory is found within path, stop and return an error.
419 * (In this case some empty directories might already have been
420 * removed.)
421 */
a0f4afbe 422#define REMOVE_DIR_EMPTY_ONLY 01
728af283
MH
423
424/*
425 * If any Git work trees are found within path, skip them without
426 * considering it an error.
427 */
a0f4afbe 428#define REMOVE_DIR_KEEP_NESTED_GIT 02
728af283
MH
429
430/* Remove the contents of path, but leave path itself. */
c844a803 431#define REMOVE_DIR_KEEP_TOPLEVEL 04
728af283
MH
432
433/*
434 * Remove path and its contents, recursively. flags is a combination
435 * of the above REMOVE_DIR_* constants. Return 0 on success.
436 *
437 * This function uses path as temporary scratch space, but restores it
438 * before returning.
439 */
55454427 440int remove_dir_recursively(struct strbuf *path, int flag);
7155b727 441
4a92d1bf 442/* tries to remove the path with empty directories along it, ignores ENOENT */
55454427 443int remove_path(const char *path);
4a92d1bf 444
55454427
DL
445int fspathcmp(const char *a, const char *b);
446int fspathncmp(const char *a, const char *b, size_t count);
8cf2a84e 447
5d74762d
NTND
448/*
449 * The prefix part of pattern must not contains wildcards.
450 */
bd30c2e4 451struct pathspec_item;
55454427 452int git_fnmatch(const struct pathspec_item *item,
ad6dad09
DL
453 const char *pattern, const char *string,
454 int prefix);
5d74762d 455
55454427 456int submodule_path_match(const struct index_state *istate,
ad6dad09
DL
457 const struct pathspec *ps,
458 const char *submodule_name,
459 char *seen);
75a6315f 460
6d2df284
NTND
461static inline int ce_path_match(const struct index_state *istate,
462 const struct cache_entry *ce,
429bb40a
NTND
463 const struct pathspec *pathspec,
464 char *seen)
465{
6d2df284 466 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
ae8d0824 467 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
429bb40a
NTND
468}
469
6d2df284
NTND
470static inline int dir_path_match(const struct index_state *istate,
471 const struct dir_entry *ent,
ebb32893
NTND
472 const struct pathspec *pathspec,
473 int prefix, char *seen)
474{
ae8d0824
NTND
475 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
476 int len = has_trailing_dir ? ent->len - 1 : ent->len;
6d2df284 477 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
ae8d0824 478 has_trailing_dir);
ebb32893
NTND
479}
480
bbf504a9
SL
481int cmp_dir_entry(const void *p1, const void *p2);
482int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
483
0cacebf0 484void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
e931371a
NTND
485void untracked_cache_remove_from_index(struct index_state *, const char *);
486void untracked_cache_add_to_index(struct index_state *, const char *);
487
f9e6c649
NTND
488void free_untracked_cache(struct untracked_cache *);
489struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
83c094ad 490void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
4a4ca479 491void add_untracked_cache(struct index_state *istate);
07b29bfd 492void remove_untracked_cache(struct index_state *istate);
da62f786
SB
493
494/*
495 * Connect a worktree to a git directory by creating (or overwriting) a
496 * '.git' file containing the location of the git directory. In the git
497 * directory set the core.worktree setting to indicate where the worktree is.
498 * When `recurse_into_nested` is set, recurse into any nested submodules,
499 * connecting them as well.
500 */
55454427 501void connect_work_tree_and_git_dir(const char *work_tree,
ad6dad09
DL
502 const char *git_dir,
503 int recurse_into_nested);
55454427 504void relocate_gitdir(const char *path,
ad6dad09
DL
505 const char *old_git_dir,
506 const char *new_git_dir);
453ec4bd 507#endif