]> git.ipfire.org Git - thirdparty/git.git/blame - dir.h
sparse-checkout: init and set in cone mode
[thirdparty/git.git] / dir.h
CommitLineData
453ec4bd
LT
1#ifndef DIR_H
2#define DIR_H
3
95a68344
AS
4/* See Documentation/technical/api-directory-listing.txt */
5
70c369cd 6#include "cache.h"
96cc8ab5 7#include "hashmap.h"
eb41775e
JH
8#include "strbuf.h"
9
453ec4bd 10struct dir_entry {
e96980ef 11 unsigned int len;
453ec4bd
LT
12 char name[FLEX_ARRAY]; /* more */
13};
14
4ff89ee5
DS
15#define PATTERN_FLAG_NODIR 1
16#define PATTERN_FLAG_ENDSWITH 4
17#define PATTERN_FLAG_MUSTBEDIR 8
18#define PATTERN_FLAG_NEGATIVE 16
68492fc7 19
ab8db613 20struct path_pattern {
709359c8 21 /*
65edd96a 22 * This allows callers of last_matching_pattern() etc.
709359c8
NTND
23 * to determine the origin of the matching pattern.
24 */
caa3d554 25 struct pattern_list *pl;
709359c8
NTND
26
27 const char *pattern;
28 int patternlen;
29 int nowildcardlen;
30 const char *base;
31 int baselen;
4ff89ee5 32 unsigned flags; /* PATTERN_FLAG_* */
709359c8
NTND
33
34 /*
35 * Counting starts from 1 for line numbers in ignore files,
36 * and from -1 decrementing for patterns from CLI args.
37 */
38 int srcpos;
39};
40
96cc8ab5
DS
41/* used for hashmaps for cone patterns */
42struct pattern_entry {
43 struct hashmap_entry ent;
44 char *pattern;
45 size_t patternlen;
46};
47
95a68344 48/*
c082df24
AS
49 * Each excludes file will be parsed into a fresh exclude_list which
50 * is appended to the relevant exclude_list_group (either EXC_DIRS or
51 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
52 * can also be used to represent the list of --exclude values passed
53 * via CLI args.
95a68344 54 */
caa3d554 55struct pattern_list {
453ec4bd
LT
56 int nr;
57 int alloc;
c04318e4 58
c082df24
AS
59 /* remember pointer to exclude file contents so we can free() */
60 char *filebuf;
61
c04318e4
AS
62 /* origin of list, e.g. path to filename, or descriptive string */
63 const char *src;
64
ab8db613 65 struct path_pattern **patterns;
96cc8ab5
DS
66
67 /*
68 * While scanning the excludes, we attempt to match the patterns
69 * with a more restricted set that allows us to use hashsets for
70 * matching logic, which is faster than the linear lookup in the
71 * excludes array above. If non-zero, that check succeeded.
72 */
73 unsigned use_cone_patterns;
74 unsigned full_cone;
75
76 /*
77 * Stores paths where everything starting with those paths
78 * is included.
79 */
80 struct hashmap recursive_hashmap;
81
82 /*
83 * Used to check single-level parents of blobs.
84 */
85 struct hashmap parent_hashmap;
453ec4bd
LT
86};
87
95a68344
AS
88/*
89 * The contents of the per-directory exclude files are lazily read on
90 * demand and then cached in memory, one per exclude_stack struct, in
91 * order to avoid opening and parsing each one every time that
92 * directory is traversed.
93 */
63d285c8 94struct exclude_stack {
95a68344 95 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
63d285c8 96 int baselen;
c082df24 97 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
0dcb8d7f 98 struct untracked_cache_dir *ucd;
c082df24
AS
99};
100
101struct exclude_list_group {
102 int nr, alloc;
caa3d554 103 struct pattern_list *pl;
63d285c8
JH
104};
105
4b33e602 106struct oid_stat {
55fe6f51 107 struct stat_data stat;
4b33e602 108 struct object_id oid;
55fe6f51
NTND
109 int valid;
110};
111
0dcb8d7f
NTND
112/*
113 * Untracked cache
114 *
115 * The following inputs are sufficient to determine what files in a
116 * directory are excluded:
117 *
118 * - The list of files and directories of the directory in question
119 * - The $GIT_DIR/index
120 * - dir_struct flags
121 * - The content of $GIT_DIR/info/exclude
122 * - The content of core.excludesfile
123 * - The content (or the lack) of .gitignore of all parent directories
124 * from $GIT_WORK_TREE
125 * - The check_only flag in read_directory_recursive (for
126 * DIR_HIDE_EMPTY_DIRECTORIES)
127 *
128 * The first input can be checked using directory mtime. In many
129 * filesystems, directory mtime (stat_data field) is updated when its
130 * files or direct subdirs are added or removed.
131 *
132 * The second one can be hooked from cache_tree_invalidate_path().
133 * Whenever a file (or a submodule) is added or removed from a
134 * directory, we invalidate that directory.
135 *
136 * The remaining inputs are easy, their SHA-1 could be used to verify
137 * their contents (exclude_sha1[], info_exclude_sha1[] and
138 * excludes_file_sha1[])
139 */
140struct untracked_cache_dir {
141 struct untracked_cache_dir **dirs;
142 char **untracked;
143 struct stat_data stat_data;
144 unsigned int untracked_alloc, dirs_nr, dirs_alloc;
145 unsigned int untracked_nr;
146 unsigned int check_only : 1;
26cb0182 147 /* all data except 'dirs' in this struct are good */
ccad261f 148 unsigned int valid : 1;
26cb0182 149 unsigned int recurse : 1;
70c369cd 150 /* null object ID means this directory does not have .gitignore */
151 struct object_id exclude_oid;
0dcb8d7f
NTND
152 char name[FLEX_ARRAY];
153};
154
155struct untracked_cache {
4b33e602
PO
156 struct oid_stat ss_info_exclude;
157 struct oid_stat ss_excludes_file;
0dcb8d7f 158 const char *exclude_per_dir;
1e8fef60 159 struct strbuf ident;
0dcb8d7f
NTND
160 /*
161 * dir_struct#flags must match dir_flags or the untracked
162 * cache is ignored.
163 */
164 unsigned dir_flags;
165 struct untracked_cache_dir *root;
166 /* Statistics */
167 int dir_created;
ccad261f 168 int gitignore_invalidated;
91a2288b
NTND
169 int dir_invalidated;
170 int dir_opened;
883e248b
BP
171 /* fsmonitor invalidation data */
172 unsigned int use_fsmonitor : 1;
0dcb8d7f
NTND
173};
174
453ec4bd
LT
175struct dir_struct {
176 int nr, alloc;
2abd31b0 177 int ignored_nr, ignored_alloc;
7c4c97c0
JS
178 enum {
179 DIR_SHOW_IGNORED = 1<<0,
180 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
181 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
182 DIR_NO_GITLINKS = 1<<3,
0aaf62b6 183 DIR_COLLECT_IGNORED = 1<<4,
2eac2a4c 184 DIR_SHOW_IGNORED_TOO = 1<<5,
fb898888 185 DIR_COLLECT_KILLED_ONLY = 1<<6,
eec0f7f2 186 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
09487f2c
EN
187 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
188 DIR_SKIP_NESTED_GIT = 1<<9
7c4c97c0 189 } flags;
453ec4bd 190 struct dir_entry **entries;
2abd31b0 191 struct dir_entry **ignored;
453ec4bd
LT
192
193 /* Exclude info */
194 const char *exclude_per_dir;
c082df24 195
63d285c8 196 /*
c082df24
AS
197 * We maintain three groups of exclude pattern lists:
198 *
63d285c8
JH
199 * EXC_CMDL lists patterns explicitly given on the command line.
200 * EXC_DIRS lists patterns obtained from per-directory ignore files.
c082df24
AS
201 * EXC_FILE lists patterns from fallback ignore files, e.g.
202 * - .git/info/exclude
203 * - core.excludesfile
204 *
205 * Each group contains multiple exclude lists, a single list
206 * per source.
63d285c8
JH
207 */
208#define EXC_CMDL 0
209#define EXC_DIRS 1
210#define EXC_FILE 2
c082df24 211 struct exclude_list_group exclude_list_group[3];
63d285c8 212
95a68344
AS
213 /*
214 * Temporary variables which are used during loading of the
215 * per-directory exclude lists.
216 *
217 * exclude_stack points to the top of the exclude_stack, and
218 * basebuf contains the full path to the current
95c6f271
KB
219 * (sub)directory in the traversal. Exclude points to the
220 * matching exclude struct if the directory is excluded.
95a68344 221 */
63d285c8 222 struct exclude_stack *exclude_stack;
ab8db613 223 struct path_pattern *pattern;
aceb9429 224 struct strbuf basebuf;
0dcb8d7f
NTND
225
226 /* Enable untracked file cache if set */
227 struct untracked_cache *untracked;
4b33e602
PO
228 struct oid_stat ss_info_exclude;
229 struct oid_stat ss_excludes_file;
ccad261f 230 unsigned unmanaged_exclude_files;
453ec4bd
LT
231};
232
e0556a92 233/*Count the number of slashes for string s*/
55454427 234int count_slashes(const char *s);
e0556a92 235
52ed1894
AS
236/*
237 * The ordering of these constants is significant, with
238 * higher-numbered match types signifying "closer" (i.e. more
239 * specific) matches which will override lower-numbered match types
240 * when populating the seen[] array.
241 */
e813d50e 242#define MATCHED_RECURSIVELY 1
89a1f4aa
EN
243#define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
244#define MATCHED_FNMATCH 3
245#define MATCHED_EXACTLY 4
55454427
DL
246int simple_length(const char *match);
247int no_wildcard(const char *string);
248char *common_prefix(const struct pathspec *pathspec);
249int match_pathspec(const struct index_state *istate,
ad6dad09
DL
250 const struct pathspec *pathspec,
251 const char *name, int namelen,
252 int prefix, char *seen, int is_dir);
4aeeef37 253int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
55454427 254int within_depth(const char *name, int namelen, int depth, int max_depth);
3c6a370b 255
55454427 256int fill_directory(struct dir_struct *dir,
ad6dad09
DL
257 struct index_state *istate,
258 const struct pathspec *pathspec);
55454427 259int read_directory(struct dir_struct *, struct index_state *istate,
ad6dad09
DL
260 const char *path, int len,
261 const struct pathspec *pathspec);
f8a9d428 262
468ce99b
DS
263enum pattern_match_result {
264 UNDECIDED = -1,
265 NOT_MATCHED = 0,
266 MATCHED = 1,
267};
268
269/*
270 * Scan the list of patterns to determine if the ordered list
271 * of patterns matches on 'pathname'.
272 *
273 * Return 1 for a match, 0 for not matched and -1 for undecided.
274 */
275enum pattern_match_result path_matches_pattern_list(const char *pathname,
276 int pathlen,
277 const char *basename, int *dtype,
278 struct pattern_list *pl,
279 struct index_state *istate);
9e58beca
BW
280struct dir_entry *dir_add_ignored(struct dir_struct *dir,
281 struct index_state *istate,
282 const char *pathname, int len);
eb41775e 283
82dce998
NTND
284/*
285 * these implement the matching logic for dir.c:excluded_from_list and
286 * attr.c:path_matches()
287 */
55454427 288int match_basename(const char *, int,
ad6dad09 289 const char *, int, int, unsigned);
55454427 290int match_pathname(const char *, int,
ad6dad09
DL
291 const char *, int,
292 const char *, int, int, unsigned);
82dce998 293
65edd96a
DS
294struct path_pattern *last_matching_pattern(struct dir_struct *dir,
295 struct index_state *istate,
296 const char *name, int *dtype);
782cd4c0 297
55454427 298int is_excluded(struct dir_struct *dir,
ad6dad09
DL
299 struct index_state *istate,
300 const char *name, int *dtype);
eb41775e 301
af09ce24
DS
302int pl_hashmap_cmp(const void *unused_cmp_data,
303 const struct hashmap_entry *a,
304 const struct hashmap_entry *b,
305 const void *key);
96cc8ab5
DS
306int hashmap_contains_parent(struct hashmap *map,
307 const char *path,
308 struct strbuf *buffer);
65edd96a 309struct pattern_list *add_pattern_list(struct dir_struct *dir,
ad6dad09 310 int group_type, const char *src);
65edd96a 311int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
caa3d554 312 struct pattern_list *pl, struct index_state *istate);
65edd96a
DS
313void add_patterns_from_file(struct dir_struct *, const char *fname);
314int add_patterns_from_blob_to_list(struct object_id *oid,
ad6dad09 315 const char *base, int baselen,
caa3d554 316 struct pattern_list *pl);
65edd96a
DS
317void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
318void add_pattern(const char *string, const char *base,
caa3d554 319 int baselen, struct pattern_list *pl, int srcpos);
65edd96a 320void clear_pattern_list(struct pattern_list *pl);
55454427 321void clear_directory(struct dir_struct *dir);
0488481e
NTND
322
323int repo_file_exists(struct repository *repo, const char *path);
324int file_exists(const char *);
453ec4bd 325
55454427
DL
326int is_inside_dir(const char *dir);
327int dir_inside_of(const char *subdir, const char *dir);
e6636747 328
8ca12c0d
AP
329static inline int is_dot_or_dotdot(const char *name)
330{
331 return (name[0] == '.' &&
332 (name[1] == '\0' ||
333 (name[1] == '.' && name[2] == '\0')));
334}
335
55454427 336int is_empty_dir(const char *dir);
55892d23 337
55454427 338void setup_standard_excludes(struct dir_struct *dir);
a0f4afbe 339
728af283
MH
340
341/* Constants for remove_dir_recursively: */
342
343/*
344 * If a non-directory is found within path, stop and return an error.
345 * (In this case some empty directories might already have been
346 * removed.)
347 */
a0f4afbe 348#define REMOVE_DIR_EMPTY_ONLY 01
728af283
MH
349
350/*
351 * If any Git work trees are found within path, skip them without
352 * considering it an error.
353 */
a0f4afbe 354#define REMOVE_DIR_KEEP_NESTED_GIT 02
728af283
MH
355
356/* Remove the contents of path, but leave path itself. */
c844a803 357#define REMOVE_DIR_KEEP_TOPLEVEL 04
728af283
MH
358
359/*
360 * Remove path and its contents, recursively. flags is a combination
361 * of the above REMOVE_DIR_* constants. Return 0 on success.
362 *
363 * This function uses path as temporary scratch space, but restores it
364 * before returning.
365 */
55454427 366int remove_dir_recursively(struct strbuf *path, int flag);
7155b727 367
4a92d1bf 368/* tries to remove the path with empty directories along it, ignores ENOENT */
55454427 369int remove_path(const char *path);
4a92d1bf 370
55454427
DL
371int fspathcmp(const char *a, const char *b);
372int fspathncmp(const char *a, const char *b, size_t count);
8cf2a84e 373
5d74762d
NTND
374/*
375 * The prefix part of pattern must not contains wildcards.
376 */
bd30c2e4 377struct pathspec_item;
55454427 378int git_fnmatch(const struct pathspec_item *item,
ad6dad09
DL
379 const char *pattern, const char *string,
380 int prefix);
5d74762d 381
55454427 382int submodule_path_match(const struct index_state *istate,
ad6dad09
DL
383 const struct pathspec *ps,
384 const char *submodule_name,
385 char *seen);
75a6315f 386
6d2df284
NTND
387static inline int ce_path_match(const struct index_state *istate,
388 const struct cache_entry *ce,
429bb40a
NTND
389 const struct pathspec *pathspec,
390 char *seen)
391{
6d2df284 392 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
ae8d0824 393 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
429bb40a
NTND
394}
395
6d2df284
NTND
396static inline int dir_path_match(const struct index_state *istate,
397 const struct dir_entry *ent,
ebb32893
NTND
398 const struct pathspec *pathspec,
399 int prefix, char *seen)
400{
ae8d0824
NTND
401 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
402 int len = has_trailing_dir ? ent->len - 1 : ent->len;
6d2df284 403 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
ae8d0824 404 has_trailing_dir);
ebb32893
NTND
405}
406
bbf504a9
SL
407int cmp_dir_entry(const void *p1, const void *p2);
408int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
409
0cacebf0 410void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
e931371a
NTND
411void untracked_cache_remove_from_index(struct index_state *, const char *);
412void untracked_cache_add_to_index(struct index_state *, const char *);
413
f9e6c649
NTND
414void free_untracked_cache(struct untracked_cache *);
415struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
83c094ad 416void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
4a4ca479 417void add_untracked_cache(struct index_state *istate);
07b29bfd 418void remove_untracked_cache(struct index_state *istate);
da62f786
SB
419
420/*
421 * Connect a worktree to a git directory by creating (or overwriting) a
422 * '.git' file containing the location of the git directory. In the git
423 * directory set the core.worktree setting to indicate where the worktree is.
424 * When `recurse_into_nested` is set, recurse into any nested submodules,
425 * connecting them as well.
426 */
55454427 427void connect_work_tree_and_git_dir(const char *work_tree,
ad6dad09
DL
428 const char *git_dir,
429 int recurse_into_nested);
55454427 430void relocate_gitdir(const char *path,
ad6dad09
DL
431 const char *old_git_dir,
432 const char *new_git_dir);
453ec4bd 433#endif