1#ifndef DIR_H
2#define DIR_H
3
4/* See Documentation/technical/api-directory-listing.txt */
5
6#include "strbuf.h"
7#include "string-list.h"
8
9struct dir_entry {
10 unsigned int len;
11 char name[FLEX_ARRAY]; /* more */
12};
13
14#define EXC_FLAG_NODIR 1
15#define EXC_FLAG_ENDSWITH 4
16#define EXC_FLAG_MUSTBEDIR 8
17#define EXC_FLAG_NEGATIVE 16
18
19struct exclude {
20 /*
21 * This allows callers of last_exclude_matching() etc.
22 * to determine the origin of the matching pattern.
23 */
24 struct exclude_list *el;
25
26 const char *pattern;
27 int patternlen;
28 int nowildcardlen;
29 const char *base;
30 int baselen;
31 int flags;
32
33 /*
34 * Counting starts from 1 for line numbers in ignore files,
35 * and from -1 decrementing for patterns from CLI args.
36 */
37 int srcpos;
38
39 struct string_list sticky_paths;
40};
41
42/*
43 * Each excludes file will be parsed into a fresh exclude_list which
44 * is appended to the relevant exclude_list_group (either EXC_DIRS or
45 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
46 * can also be used to represent the list of --exclude values passed
47 * via CLI args.
48 */
49struct exclude_list {
50 int nr;
51 int alloc;
52
53 /* remember pointer to exclude file contents so we can free() */
54 char *filebuf;
55
56 /* origin of list, e.g. path to filename, or descriptive string */
57 const char *src;
58
59 struct exclude **excludes;
60};
61
62/*
63 * The contents of the per-directory exclude files are lazily read on
64 * demand and then cached in memory, one per exclude_stack struct, in
65 * order to avoid opening and parsing each one every time that
66 * directory is traversed.
67 */
68struct exclude_stack {
69 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
70 int baselen;
71 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
72 struct untracked_cache_dir *ucd;
73};
74
75struct exclude_list_group {
76 int nr, alloc;
77 struct exclude_list *el;
78};
79
80struct sha1_stat {
81 struct stat_data stat;
82 unsigned char sha1[20];
83 int valid;
84};
85
86/*
87 * Untracked cache
88 *
89 * The following inputs are sufficient to determine what files in a
90 * directory are excluded:
91 *
92 * - The list of files and directories of the directory in question
93 * - The $GIT_DIR/index
94 * - dir_struct flags
95 * - The content of $GIT_DIR/info/exclude
96 * - The content of core.excludesfile
97 * - The content (or the lack) of .gitignore of all parent directories
98 * from $GIT_WORK_TREE
99 * - The check_only flag in read_directory_recursive (for
100 * DIR_HIDE_EMPTY_DIRECTORIES)
101 *
102 * The first input can be checked using directory mtime. In many
103 * filesystems, directory mtime (stat_data field) is updated when its
104 * files or direct subdirs are added or removed.
105 *
106 * The second one can be hooked from cache_tree_invalidate_path().
107 * Whenever a file (or a submodule) is added or removed from a
108 * directory, we invalidate that directory.
109 *
110 * The remaining inputs are easy, their SHA-1 could be used to verify
111 * their contents (exclude_sha1[], info_exclude_sha1[] and
112 * excludes_file_sha1[])
113 */
114struct untracked_cache_dir {
115 struct untracked_cache_dir **dirs;
116 char **untracked;
117 struct stat_data stat_data;
118 unsigned int untracked_alloc, dirs_nr, dirs_alloc;
119 unsigned int untracked_nr;
120 unsigned int check_only : 1;
121 /* all data except 'dirs' in this struct are good */
122 unsigned int valid : 1;
123 unsigned int recurse : 1;
124 /* null SHA-1 means this directory does not have .gitignore */
125 unsigned char exclude_sha1[20];
126 char name[FLEX_ARRAY];
127};
128
129struct untracked_cache {
130 struct sha1_stat ss_info_exclude;
131 struct sha1_stat ss_excludes_file;
132 const char *exclude_per_dir;
133 struct strbuf ident;
134 /*
135 * dir_struct#flags must match dir_flags or the untracked
136 * cache is ignored.
137 */
138 unsigned dir_flags;
139 struct untracked_cache_dir *root;
140 /* Statistics */
141 int dir_created;
142 int gitignore_invalidated;
143 int dir_invalidated;
144 int dir_opened;
145};
146
147struct dir_struct {
148 int nr, alloc;
149 int ignored_nr, ignored_alloc;
150 enum {
151 DIR_SHOW_IGNORED = 1<<0,
152 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
153 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
154 DIR_NO_GITLINKS = 1<<3,
155 DIR_COLLECT_IGNORED = 1<<4,
156 DIR_SHOW_IGNORED_TOO = 1<<5,
157 DIR_COLLECT_KILLED_ONLY = 1<<6
158 } flags;
159 struct dir_entry **entries;
160 struct dir_entry **ignored;
161
162 /* Exclude info */
163 const char *exclude_per_dir;
164
165 /*
166 * We maintain three groups of exclude pattern lists:
167 *
168 * EXC_CMDL lists patterns explicitly given on the command line.
169 * EXC_DIRS lists patterns obtained from per-directory ignore files.
170 * EXC_FILE lists patterns from fallback ignore files, e.g.
171 * - .git/info/exclude
172 * - core.excludesfile
173 *
174 * Each group contains multiple exclude lists, a single list
175 * per source.
176 */
177#define EXC_CMDL 0
178#define EXC_DIRS 1
179#define EXC_FILE 2
180 struct exclude_list_group exclude_list_group[3];
181
182 /*
183 * Temporary variables which are used during loading of the
184 * per-directory exclude lists.
185 *
186 * exclude_stack points to the top of the exclude_stack, and
187 * basebuf contains the full path to the current
188 * (sub)directory in the traversal. Exclude points to the
189 * matching exclude struct if the directory is excluded.
190 */
191 struct exclude_stack *exclude_stack;
192 struct exclude *exclude;
193 struct strbuf basebuf;
194
195 /* Enable untracked file cache if set */
196 struct untracked_cache *untracked;
197 struct sha1_stat ss_info_exclude;
198 struct sha1_stat ss_excludes_file;
199 unsigned unmanaged_exclude_files;
200};
201
202/*
203 * The ordering of these constants is significant, with
204 * higher-numbered match types signifying "closer" (i.e. more
205 * specific) matches which will override lower-numbered match types
206 * when populating the seen[] array.
207 */
208#define MATCHED_RECURSIVELY 1
209#define MATCHED_FNMATCH 2
210#define MATCHED_EXACTLY 3
211extern int simple_length(const char *match);
212extern int no_wildcard(const char *string);
213extern char *common_prefix(const struct pathspec *pathspec);
214extern int match_pathspec(const struct pathspec *pathspec,
215 const char *name, int namelen,
216 int prefix, char *seen, int is_dir);
217extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
218extern int within_depth(const char *name, int namelen, int depth, int max_depth);
219
220extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);
221extern int read_directory(struct dir_struct *, const char *path, int len, const struct pathspec *pathspec);
222
223extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename,
224 int *dtype, struct exclude_list *el);
225struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len);
226
227/*
228 * these implement the matching logic for dir.c:excluded_from_list and
229 * attr.c:path_matches()
230 */
231extern int match_basename(const char *, int,
232 const char *, int, int, int);
233extern int match_pathname(const char *, int,
234 const char *, int,
235 const char *, int, int, int);
236
237extern struct exclude *last_exclude_matching(struct dir_struct *dir,
238 const char *name, int *dtype);
239
240extern int is_excluded(struct dir_struct *dir, const char *name, int *dtype);
241
242extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
243 int group_type, const char *src);
244extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
245 struct exclude_list *el, int check_index);
246extern void add_excludes_from_file(struct dir_struct *, const char *fname);
247extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
248extern void add_exclude(const char *string, const char *base,
249 int baselen, struct exclude_list *el, int srcpos);
250extern void clear_exclude_list(struct exclude_list *el);
251extern void clear_directory(struct dir_struct *dir);
252extern int file_exists(const char *);
253
254extern int is_inside_dir(const char *dir);
255extern int dir_inside_of(const char *subdir, const char *dir);
256
257static inline int is_dot_or_dotdot(const char *name)
258{
259 return (name[0] == '.' &&
260 (name[1] == '\0' ||
261 (name[1] == '.' && name[2] == '\0')));
262}
263
264extern int is_empty_dir(const char *dir);
265
266extern void setup_standard_excludes(struct dir_struct *dir);
267
268#define REMOVE_DIR_EMPTY_ONLY 01
269#define REMOVE_DIR_KEEP_NESTED_GIT 02
270#define REMOVE_DIR_KEEP_TOPLEVEL 04
271extern int remove_dir_recursively(struct strbuf *path, int flag);
272
273/* tries to remove the path with empty directories along it, ignores ENOENT */
274extern int remove_path(const char *path);
275
276extern int strcmp_icase(const char *a, const char *b);
277extern int strncmp_icase(const char *a, const char *b, size_t count);
278extern int fnmatch_icase(const char *pattern, const char *string, int flags);
279
280/*
281 * The prefix part of pattern must not contains wildcards.
282 */
283struct pathspec_item;
284extern int git_fnmatch(const struct pathspec_item *item,
285 const char *pattern, const char *string,
286 int prefix);
287
288static inline int ce_path_match(const struct cache_entry *ce,
289 const struct pathspec *pathspec,
290 char *seen)
291{
292 return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen,
293 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
294}
295
296static inline int dir_path_match(const struct dir_entry *ent,
297 const struct pathspec *pathspec,
298 int prefix, char *seen)
299{
300 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
301 int len = has_trailing_dir ? ent->len - 1 : ent->len;
302 return match_pathspec(pathspec, ent->name, len, prefix, seen,
303 has_trailing_dir);
304}
305
306void untracked_cache_invalidate_path(struct index_state *, const char *);
307void untracked_cache_remove_from_index(struct index_state *, const char *);
308void untracked_cache_add_to_index(struct index_state *, const char *);
309
310void free_untracked_cache(struct untracked_cache *);
311struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
312void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
313void add_untracked_cache(struct index_state *istate);
314void remove_untracked_cache(struct index_state *istate);
315#endif