]>
Commit | Line | Data |
---|---|---|
453ec4bd LT |
1 | #ifndef DIR_H |
2 | #define DIR_H | |
3 | ||
95a68344 AS |
4 | /* See Documentation/technical/api-directory-listing.txt */ |
5 | ||
eb41775e | 6 | #include "strbuf.h" |
c62a9173 | 7 | #include "string-list.h" |
eb41775e | 8 | |
453ec4bd | 9 | struct dir_entry { |
e96980ef | 10 | unsigned int len; |
453ec4bd LT |
11 | char name[FLEX_ARRAY]; /* more */ |
12 | }; | |
13 | ||
68492fc7 | 14 | #define EXC_FLAG_NODIR 1 |
68492fc7 | 15 | #define EXC_FLAG_ENDSWITH 4 |
d6b8fc30 | 16 | #define EXC_FLAG_MUSTBEDIR 8 |
84460eec | 17 | #define EXC_FLAG_NEGATIVE 16 |
68492fc7 | 18 | |
709359c8 NTND |
19 | struct 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; | |
c62a9173 NTND |
38 | |
39 | struct string_list sticky_paths; | |
709359c8 NTND |
40 | }; |
41 | ||
95a68344 | 42 | /* |
c082df24 AS |
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. | |
95a68344 | 48 | */ |
453ec4bd LT |
49 | struct exclude_list { |
50 | int nr; | |
51 | int alloc; | |
c04318e4 | 52 | |
c082df24 AS |
53 | /* remember pointer to exclude file contents so we can free() */ |
54 | char *filebuf; | |
55 | ||
c04318e4 AS |
56 | /* origin of list, e.g. path to filename, or descriptive string */ |
57 | const char *src; | |
58 | ||
709359c8 | 59 | struct exclude **excludes; |
453ec4bd LT |
60 | }; |
61 | ||
95a68344 AS |
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 | */ | |
63d285c8 | 68 | struct exclude_stack { |
95a68344 | 69 | struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */ |
63d285c8 | 70 | int baselen; |
c082df24 | 71 | int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */ |
0dcb8d7f | 72 | struct untracked_cache_dir *ucd; |
c082df24 AS |
73 | }; |
74 | ||
75 | struct exclude_list_group { | |
76 | int nr, alloc; | |
77 | struct exclude_list *el; | |
63d285c8 JH |
78 | }; |
79 | ||
55fe6f51 NTND |
80 | struct sha1_stat { |
81 | struct stat_data stat; | |
82 | unsigned char sha1[20]; | |
83 | int valid; | |
84 | }; | |
85 | ||
0dcb8d7f NTND |
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 | */ | |
114 | struct 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; | |
26cb0182 | 121 | /* all data except 'dirs' in this struct are good */ |
ccad261f | 122 | unsigned int valid : 1; |
26cb0182 | 123 | unsigned int recurse : 1; |
0dcb8d7f NTND |
124 | /* null SHA-1 means this directory does not have .gitignore */ |
125 | unsigned char exclude_sha1[20]; | |
126 | char name[FLEX_ARRAY]; | |
127 | }; | |
128 | ||
129 | struct untracked_cache { | |
130 | struct sha1_stat ss_info_exclude; | |
131 | struct sha1_stat ss_excludes_file; | |
132 | const char *exclude_per_dir; | |
1e8fef60 | 133 | struct strbuf ident; |
0dcb8d7f NTND |
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; | |
ccad261f | 142 | int gitignore_invalidated; |
91a2288b NTND |
143 | int dir_invalidated; |
144 | int dir_opened; | |
0dcb8d7f NTND |
145 | }; |
146 | ||
453ec4bd LT |
147 | struct dir_struct { |
148 | int nr, alloc; | |
2abd31b0 | 149 | int ignored_nr, ignored_alloc; |
7c4c97c0 JS |
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, | |
0aaf62b6 | 155 | DIR_COLLECT_IGNORED = 1<<4, |
2eac2a4c JH |
156 | DIR_SHOW_IGNORED_TOO = 1<<5, |
157 | DIR_COLLECT_KILLED_ONLY = 1<<6 | |
7c4c97c0 | 158 | } flags; |
453ec4bd | 159 | struct dir_entry **entries; |
2abd31b0 | 160 | struct dir_entry **ignored; |
453ec4bd LT |
161 | |
162 | /* Exclude info */ | |
163 | const char *exclude_per_dir; | |
c082df24 | 164 | |
63d285c8 | 165 | /* |
c082df24 AS |
166 | * We maintain three groups of exclude pattern lists: |
167 | * | |
63d285c8 JH |
168 | * EXC_CMDL lists patterns explicitly given on the command line. |
169 | * EXC_DIRS lists patterns obtained from per-directory ignore files. | |
c082df24 AS |
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. | |
63d285c8 JH |
176 | */ |
177 | #define EXC_CMDL 0 | |
178 | #define EXC_DIRS 1 | |
179 | #define EXC_FILE 2 | |
c082df24 | 180 | struct exclude_list_group exclude_list_group[3]; |
63d285c8 | 181 | |
95a68344 AS |
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 | |
95c6f271 KB |
188 | * (sub)directory in the traversal. Exclude points to the |
189 | * matching exclude struct if the directory is excluded. | |
95a68344 | 190 | */ |
63d285c8 | 191 | struct exclude_stack *exclude_stack; |
95c6f271 | 192 | struct exclude *exclude; |
aceb9429 | 193 | struct strbuf basebuf; |
0dcb8d7f NTND |
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; | |
ccad261f | 199 | unsigned unmanaged_exclude_files; |
453ec4bd LT |
200 | }; |
201 | ||
52ed1894 AS |
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 | */ | |
e813d50e JH |
208 | #define MATCHED_RECURSIVELY 1 |
209 | #define MATCHED_FNMATCH 2 | |
210 | #define MATCHED_EXACTLY 3 | |
87323bda NTND |
211 | extern int simple_length(const char *match); |
212 | extern int no_wildcard(const char *string); | |
827f4d6c | 213 | extern char *common_prefix(const struct pathspec *pathspec); |
854b0959 NTND |
214 | extern int match_pathspec(const struct pathspec *pathspec, |
215 | const char *name, int namelen, | |
ae8d0824 | 216 | int prefix, char *seen, int is_dir); |
777c55a6 | 217 | extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix); |
bc96cc87 | 218 | extern int within_depth(const char *name, int namelen, int depth, int max_depth); |
3c6a370b | 219 | |
7327d3d1 NTND |
220 | extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec); |
221 | extern int read_directory(struct dir_struct *, const char *path, int len, const struct pathspec *pathspec); | |
f8a9d428 | 222 | |
07958050 AS |
223 | extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename, |
224 | int *dtype, struct exclude_list *el); | |
108da0db | 225 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len); |
eb41775e | 226 | |
82dce998 NTND |
227 | /* |
228 | * these implement the matching logic for dir.c:excluded_from_list and | |
229 | * attr.c:path_matches() | |
230 | */ | |
231 | extern int match_basename(const char *, int, | |
232 | const char *, int, int, int); | |
233 | extern int match_pathname(const char *, int, | |
234 | const char *, int, | |
235 | const char *, int, int, int); | |
236 | ||
b07bc8c8 KB |
237 | extern struct exclude *last_exclude_matching(struct dir_struct *dir, |
238 | const char *name, int *dtype); | |
782cd4c0 | 239 | |
b07bc8c8 | 240 | extern int is_excluded(struct dir_struct *dir, const char *name, int *dtype); |
eb41775e | 241 | |
c04318e4 AS |
242 | extern struct exclude_list *add_exclude_list(struct dir_struct *dir, |
243 | int group_type, const char *src); | |
cb097534 | 244 | extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen, |
c082df24 | 245 | struct exclude_list *el, int check_index); |
453ec4bd | 246 | extern void add_excludes_from_file(struct dir_struct *, const char *fname); |
82dce998 | 247 | extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen); |
453ec4bd | 248 | extern void add_exclude(const char *string, const char *base, |
c04318e4 | 249 | int baselen, struct exclude_list *el, int srcpos); |
f6198812 | 250 | extern void clear_exclude_list(struct exclude_list *el); |
270be816 | 251 | extern void clear_directory(struct dir_struct *dir); |
c91f0d92 | 252 | extern int file_exists(const char *); |
453ec4bd | 253 | |
e6636747 | 254 | extern int is_inside_dir(const char *dir); |
9b125da4 | 255 | extern int dir_inside_of(const char *subdir, const char *dir); |
e6636747 | 256 | |
8ca12c0d AP |
257 | static 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 | ||
55892d23 AP |
264 | extern int is_empty_dir(const char *dir); |
265 | ||
039bc64e | 266 | extern void setup_standard_excludes(struct dir_struct *dir); |
a0f4afbe JH |
267 | |
268 | #define REMOVE_DIR_EMPTY_ONLY 01 | |
269 | #define REMOVE_DIR_KEEP_NESTED_GIT 02 | |
c844a803 | 270 | #define REMOVE_DIR_KEEP_TOPLEVEL 04 |
a0f4afbe | 271 | extern int remove_dir_recursively(struct strbuf *path, int flag); |
7155b727 | 272 | |
4a92d1bf AR |
273 | /* tries to remove the path with empty directories along it, ignores ENOENT */ |
274 | extern int remove_path(const char *path); | |
275 | ||
8cf2a84e JJ |
276 | extern int strcmp_icase(const char *a, const char *b); |
277 | extern int strncmp_icase(const char *a, const char *b, size_t count); | |
278 | extern int fnmatch_icase(const char *pattern, const char *string, int flags); | |
279 | ||
5d74762d NTND |
280 | /* |
281 | * The prefix part of pattern must not contains wildcards. | |
282 | */ | |
bd30c2e4 NTND |
283 | struct pathspec_item; |
284 | extern int git_fnmatch(const struct pathspec_item *item, | |
285 | const char *pattern, const char *string, | |
286 | int prefix); | |
5d74762d | 287 | |
429bb40a NTND |
288 | static inline int ce_path_match(const struct cache_entry *ce, |
289 | const struct pathspec *pathspec, | |
290 | char *seen) | |
291 | { | |
ae8d0824 NTND |
292 | return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen, |
293 | S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode)); | |
429bb40a NTND |
294 | } |
295 | ||
ebb32893 NTND |
296 | static inline int dir_path_match(const struct dir_entry *ent, |
297 | const struct pathspec *pathspec, | |
298 | int prefix, char *seen) | |
299 | { | |
ae8d0824 NTND |
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); | |
ebb32893 NTND |
304 | } |
305 | ||
e931371a NTND |
306 | void untracked_cache_invalidate_path(struct index_state *, const char *); |
307 | void untracked_cache_remove_from_index(struct index_state *, const char *); | |
308 | void untracked_cache_add_to_index(struct index_state *, const char *); | |
309 | ||
f9e6c649 NTND |
310 | void free_untracked_cache(struct untracked_cache *); |
311 | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz); | |
83c094ad | 312 | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked); |
4a4ca479 | 313 | void add_untracked_cache(struct index_state *istate); |
07b29bfd | 314 | void remove_untracked_cache(struct index_state *istate); |
453ec4bd | 315 | #endif |