blob: 8e02dfb505d163eca3c33d17e495be8f166b83e5 [file] [log] [blame]
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001#ifndef DIR_H
2#define DIR_H
3
brian m. carlson70c369c2018-05-02 00:25:48 +00004#include "cache.h"
Derrick Stolee96cc8ab2019-11-21 22:04:41 +00005#include "hashmap.h"
Junio C Hamanoeb417752012-06-01 11:28:00 -07006#include "strbuf.h"
7
Heba Waly266f03e2019-11-17 21:04:41 +00008/**
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 *
Elijah Newreneceba532020-08-18 22:58:26 +000022 * - Prepare `struct dir_struct dir` using `dir_init()` function.
Heba Waly266f03e2019-11-17 21:04:41 +000023 *
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
Elijah Newreneceba532020-08-18 22:58:26 +000028 * `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`.
Heba Waly266f03e2019-11-17 21:04:41 +000029 *
Elijah Newreneceba532020-08-18 22:58:26 +000030 * - 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.
Heba Waly266f03e2019-11-17 21:04:41 +000033 *
Elijah Newreneceba532020-08-18 22:58:26 +000034 * - Call `fill_directory()`.
Heba Waly266f03e2019-11-17 21:04:41 +000035 *
Elijah Newreneceba532020-08-18 22:58:26 +000036 * - Use `dir.entries[]` and `dir.ignored[]`.
Heba Waly266f03e2019-11-17 21:04:41 +000037 *
Elijah Newreneceba532020-08-18 22:58:26 +000038 * - Call `dir_clear()` when the contained elements are no longer in use.
Heba Waly266f03e2019-11-17 21:04:41 +000039 *
40 */
41
Linus Torvalds453ec4b2006-05-16 19:02:14 -070042struct dir_entry {
Jeff Kinge96980e2007-06-12 23:42:14 +020043 unsigned int len;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070044 char name[FLEX_ARRAY]; /* more */
45};
46
Derrick Stolee4ff89ee2019-09-03 11:04:56 -070047#define PATTERN_FLAG_NODIR 1
48#define PATTERN_FLAG_ENDSWITH 4
49#define PATTERN_FLAG_MUSTBEDIR 8
50#define PATTERN_FLAG_NEGATIVE 16
Lars Knoll68492fc2007-10-28 21:27:13 +010051
Derrick Stoleeab8db612019-09-03 11:04:55 -070052struct path_pattern {
Nguyễn Thái Ngọc Duy709359c2014-07-14 11:48:03 +020053 /*
Derrick Stolee65edd962019-09-03 11:04:57 -070054 * This allows callers of last_matching_pattern() etc.
Nguyễn Thái Ngọc Duy709359c2014-07-14 11:48:03 +020055 * to determine the origin of the matching pattern.
56 */
Derrick Stoleecaa3d552019-09-03 11:04:56 -070057 struct pattern_list *pl;
Nguyễn Thái Ngọc Duy709359c2014-07-14 11:48:03 +020058
59 const char *pattern;
60 int patternlen;
61 int nowildcardlen;
62 const char *base;
63 int baselen;
Derrick Stolee4ff89ee2019-09-03 11:04:56 -070064 unsigned flags; /* PATTERN_FLAG_* */
Nguyễn Thái Ngọc Duy709359c2014-07-14 11:48:03 +020065
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
Derrick Stolee96cc8ab2019-11-21 22:04:41 +000073/* used for hashmaps for cone patterns */
74struct pattern_entry {
75 struct hashmap_entry ent;
76 char *pattern;
77 size_t patternlen;
78};
79
Adam Spiers95a68342012-12-27 02:32:21 +000080/*
Adam Spiersc082df22013-01-06 16:58:03 +000081 * 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.
Adam Spiers95a68342012-12-27 02:32:21 +000086 */
Derrick Stoleecaa3d552019-09-03 11:04:56 -070087struct pattern_list {
Linus Torvalds453ec4b2006-05-16 19:02:14 -070088 int nr;
89 int alloc;
Adam Spiersc04318e2013-01-06 16:58:04 +000090
Adam Spiersc082df22013-01-06 16:58:03 +000091 /* remember pointer to exclude file contents so we can free() */
92 char *filebuf;
93
Adam Spiersc04318e2013-01-06 16:58:04 +000094 /* origin of list, e.g. path to filename, or descriptive string */
95 const char *src;
96
Derrick Stoleeab8db612019-09-03 11:04:55 -070097 struct path_pattern **patterns;
Derrick Stolee96cc8ab2019-11-21 22:04:41 +000098
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;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700118};
119
Adam Spiers95a68342012-12-27 02:32:21 +0000120/*
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 */
Junio C Hamano63d285c2007-11-29 02:17:44 -0800126struct exclude_stack {
Adam Spiers95a68342012-12-27 02:32:21 +0000127 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
Junio C Hamano63d285c2007-11-29 02:17:44 -0800128 int baselen;
Adam Spiersc082df22013-01-06 16:58:03 +0000129 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700130 struct untracked_cache_dir *ucd;
Adam Spiersc082df22013-01-06 16:58:03 +0000131};
132
133struct exclude_list_group {
134 int nr, alloc;
Derrick Stoleecaa3d552019-09-03 11:04:56 -0700135 struct pattern_list *pl;
Junio C Hamano63d285c2007-11-29 02:17:44 -0800136};
137
Patryk Obara4b33e602018-01-28 01:13:12 +0100138struct oid_stat {
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700139 struct stat_data stat;
Patryk Obara4b33e602018-01-28 01:13:12 +0100140 struct object_id oid;
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700141 int valid;
142};
143
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700144/*
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;
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +0700179 /* all data except 'dirs' in this struct are good */
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700180 unsigned int valid : 1;
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +0700181 unsigned int recurse : 1;
brian m. carlson70c369c2018-05-02 00:25:48 +0000182 /* null object ID means this directory does not have .gitignore */
183 struct object_id exclude_oid;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700184 char name[FLEX_ARRAY];
185};
186
187struct untracked_cache {
Patryk Obara4b33e602018-01-28 01:13:12 +0100188 struct oid_stat ss_info_exclude;
189 struct oid_stat ss_excludes_file;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700190 const char *exclude_per_dir;
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +0700191 struct strbuf ident;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700192 /*
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;
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700200 int gitignore_invalidated;
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +0700201 int dir_invalidated;
202 int dir_opened;
Ben Peart883e2482017-09-22 12:35:40 -0400203 /* fsmonitor invalidation data */
204 unsigned int use_fsmonitor : 1;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700205};
206
Heba Waly266f03e2019-11-17 21:04:41 +0000207/**
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 */
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700212struct dir_struct {
Heba Waly266f03e2019-11-17 21:04:41 +0000213
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 */
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100226 enum {
Heba Waly266f03e2019-11-17 21:04:41 +0000227
228 /**
229 * Return just ignored files in `entries[]`, not untracked files.
230 * This flag is mutually exclusive with `DIR_SHOW_IGNORED_TOO`.
231 */
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100232 DIR_SHOW_IGNORED = 1<<0,
Heba Waly266f03e2019-11-17 21:04:41 +0000233
234 /* Include a directory that is not tracked. */
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100235 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
Heba Waly266f03e2019-11-17 21:04:41 +0000236
237 /* Do not include a directory that is not tracked and is empty. */
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100238 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
Heba Waly266f03e2019-11-17 21:04:41 +0000239
240 /**
241 * If set, recurse into a directory that looks like a Git directory.
242 * Otherwise it is shown as a directory.
243 */
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100244 DIR_NO_GITLINKS = 1<<3,
Heba Waly266f03e2019-11-17 21:04:41 +0000245
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 */
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200252 DIR_COLLECT_IGNORED = 1<<4,
Heba Waly266f03e2019-11-17 21:04:41 +0000253
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 */
Junio C Hamano2eac2a42013-08-15 12:13:46 -0700259 DIR_SHOW_IGNORED_TOO = 1<<5,
Heba Waly266f03e2019-11-17 21:04:41 +0000260
Samuel Lijinfb898882017-05-18 04:21:52 -0400261 DIR_COLLECT_KILLED_ONLY = 1<<6,
Heba Waly266f03e2019-11-17 21:04:41 +0000262
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 */
Jameson Millereec0f7f2017-10-30 13:21:37 -0400268 DIR_KEEP_UNTRACKED_CONTENTS = 1<<7,
Heba Waly266f03e2019-11-17 21:04:41 +0000269
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 */
Elijah Newren09487f22019-09-17 09:35:02 -0700284 DIR_SHOW_IGNORED_TOO_MODE_MATCHING = 1<<8,
Heba Waly266f03e2019-11-17 21:04:41 +0000285
Elijah Newren09487f22019-09-17 09:35:02 -0700286 DIR_SKIP_NESTED_GIT = 1<<9
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100287 } flags;
Heba Waly266f03e2019-11-17 21:04:41 +0000288
289 /* An array of `struct dir_entry`, each element of which describes a path. */
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700290 struct dir_entry **entries;
Heba Waly266f03e2019-11-17 21:04:41 +0000291
292 /**
293 * used for ignored paths with the `DIR_SHOW_IGNORED_TOO` and
294 * `DIR_COLLECT_IGNORED` flags.
295 */
Jeff King2abd31b2007-06-11 09:39:50 -0400296 struct dir_entry **ignored;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700297
Heba Waly266f03e2019-11-17 21:04:41 +0000298 /**
299 * The name of the file to be read in each directory for excluded files
300 * (typically `.gitignore`).
301 */
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700302 const char *exclude_per_dir;
Adam Spiersc082df22013-01-06 16:58:03 +0000303
Junio C Hamano63d285c2007-11-29 02:17:44 -0800304 /*
Adam Spiersc082df22013-01-06 16:58:03 +0000305 * We maintain three groups of exclude pattern lists:
306 *
Junio C Hamano63d285c2007-11-29 02:17:44 -0800307 * EXC_CMDL lists patterns explicitly given on the command line.
308 * EXC_DIRS lists patterns obtained from per-directory ignore files.
Adam Spiersc082df22013-01-06 16:58:03 +0000309 * 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.
Junio C Hamano63d285c2007-11-29 02:17:44 -0800315 */
316#define EXC_CMDL 0
317#define EXC_DIRS 1
318#define EXC_FILE 2
Adam Spiersc082df22013-01-06 16:58:03 +0000319 struct exclude_list_group exclude_list_group[3];
Junio C Hamano63d285c2007-11-29 02:17:44 -0800320
Adam Spiers95a68342012-12-27 02:32:21 +0000321 /*
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
Karsten Blees95c6f272013-04-15 21:12:14 +0200327 * (sub)directory in the traversal. Exclude points to the
328 * matching exclude struct if the directory is excluded.
Adam Spiers95a68342012-12-27 02:32:21 +0000329 */
Junio C Hamano63d285c2007-11-29 02:17:44 -0800330 struct exclude_stack *exclude_stack;
Derrick Stoleeab8db612019-09-03 11:04:55 -0700331 struct path_pattern *pattern;
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +0200332 struct strbuf basebuf;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700333
334 /* Enable untracked file cache if set */
335 struct untracked_cache *untracked;
Patryk Obara4b33e602018-01-28 01:13:12 +0100336 struct oid_stat ss_info_exclude;
337 struct oid_stat ss_excludes_file;
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700338 unsigned unmanaged_exclude_files;
Elijah Newren7fe1ffdaf2021-05-12 17:28:15 +0000339
340 /* Stats about the traversal */
341 unsigned visited_paths;
342 unsigned visited_directories;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700343};
344
Ævar Arnfjörð Bjarmasonce93a4c2021-07-01 12:51:27 +0200345#define DIR_INIT { 0 }
346
Elijah Newrenb548f0f2021-05-12 17:28:22 +0000347struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
348
Prathamesh Chavane0556a92017-06-08 23:38:12 +0530349/*Count the number of slashes for string s*/
Denton Liu55454422019-04-29 04:28:14 -0400350int count_slashes(const char *s);
Prathamesh Chavane0556a92017-06-08 23:38:12 +0530351
Adam Spiers52ed1892013-01-06 16:58:06 +0000352/*
353 * The ordering of these constants is significant, with
354 * higher-numbered match types signifying "closer" (i.e. more
355 * specific) matches which will override lower-numbered match types
356 * when populating the seen[] array.
357 */
Junio C Hamanoe813d502006-12-25 03:09:52 -0800358#define MATCHED_RECURSIVELY 1
Elijah Newren89a1f4a2019-09-17 09:34:58 -0700359#define MATCHED_RECURSIVELY_LEADING_PATHSPEC 2
360#define MATCHED_FNMATCH 3
361#define MATCHED_EXACTLY 4
Denton Liu55454422019-04-29 04:28:14 -0400362int simple_length(const char *match);
363int no_wildcard(const char *string);
364char *common_prefix(const struct pathspec *pathspec);
Derrick Stolee847a9e52021-04-01 01:49:39 +0000365int match_pathspec(struct index_state *istate,
Denton Liuad6dad02019-04-29 04:28:23 -0400366 const struct pathspec *pathspec,
367 const char *name, int namelen,
368 int prefix, char *seen, int is_dir);
Junio C Hamano4aeeef32019-05-13 23:50:32 +0900369int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
Denton Liu55454422019-04-29 04:28:14 -0400370int within_depth(const char *name, int namelen, int depth, int max_depth);
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700371
Denton Liu55454422019-04-29 04:28:14 -0400372int fill_directory(struct dir_struct *dir,
Denton Liuad6dad02019-04-29 04:28:23 -0400373 struct index_state *istate,
374 const struct pathspec *pathspec);
Denton Liu55454422019-04-29 04:28:14 -0400375int read_directory(struct dir_struct *, struct index_state *istate,
Denton Liuad6dad02019-04-29 04:28:23 -0400376 const char *path, int len,
377 const struct pathspec *pathspec);
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800378
Derrick Stolee468ce992019-09-03 11:04:58 -0700379enum pattern_match_result {
380 UNDECIDED = -1,
381 NOT_MATCHED = 0,
382 MATCHED = 1,
Derrick Stoleeeb42fec2019-11-21 22:04:43 +0000383 MATCHED_RECURSIVE = 2,
Derrick Stolee468ce992019-09-03 11:04:58 -0700384};
385
386/*
387 * Scan the list of patterns to determine if the ordered list
388 * of patterns matches on 'pathname'.
389 *
390 * Return 1 for a match, 0 for not matched and -1 for undecided.
391 */
392enum pattern_match_result path_matches_pattern_list(const char *pathname,
393 int pathlen,
394 const char *basename, int *dtype,
395 struct pattern_list *pl,
396 struct index_state *istate);
Derrick Stolee02155c82021-09-08 01:42:30 +0000397
398int init_sparse_checkout_patterns(struct index_state *state);
399
400int path_in_sparse_checkout(const char *path,
401 struct index_state *istate);
402int path_in_cone_mode_sparse_checkout(const char *path,
403 struct index_state *istate);
404
Brandon Williams9e58bec2017-05-05 12:53:25 -0700405struct dir_entry *dir_add_ignored(struct dir_struct *dir,
406 struct index_state *istate,
407 const char *pathname, int len);
Junio C Hamanoeb417752012-06-01 11:28:00 -0700408
409/*
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700410 * these implement the matching logic for dir.c:excluded_from_list and
411 * attr.c:path_matches()
412 */
Denton Liu55454422019-04-29 04:28:14 -0400413int match_basename(const char *, int,
Denton Liuad6dad02019-04-29 04:28:23 -0400414 const char *, int, int, unsigned);
Denton Liu55454422019-04-29 04:28:14 -0400415int match_pathname(const char *, int,
Denton Liuad6dad02019-04-29 04:28:23 -0400416 const char *, int,
417 const char *, int, int, unsigned);
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700418
Derrick Stolee65edd962019-09-03 11:04:57 -0700419struct path_pattern *last_matching_pattern(struct dir_struct *dir,
420 struct index_state *istate,
421 const char *name, int *dtype);
Junio C Hamano782cd4c2012-06-05 21:17:52 -0700422
Denton Liu55454422019-04-29 04:28:14 -0400423int is_excluded(struct dir_struct *dir,
Denton Liuad6dad02019-04-29 04:28:23 -0400424 struct index_state *istate,
425 const char *name, int *dtype);
Junio C Hamanoeb417752012-06-01 11:28:00 -0700426
Derrick Stoleeaf09ce22019-11-21 22:04:42 +0000427int pl_hashmap_cmp(const void *unused_cmp_data,
428 const struct hashmap_entry *a,
429 const struct hashmap_entry *b,
430 const void *key);
Derrick Stolee96cc8ab2019-11-21 22:04:41 +0000431int hashmap_contains_parent(struct hashmap *map,
432 const char *path,
433 struct strbuf *buffer);
Derrick Stolee65edd962019-09-03 11:04:57 -0700434struct pattern_list *add_pattern_list(struct dir_struct *dir,
Denton Liuad6dad02019-04-29 04:28:23 -0400435 int group_type, const char *src);
Derrick Stolee65edd962019-09-03 11:04:57 -0700436int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
Jeff King1679d602021-02-16 09:44:28 -0500437 struct pattern_list *pl, struct index_state *istate,
438 unsigned flags);
Derrick Stolee65edd962019-09-03 11:04:57 -0700439void add_patterns_from_file(struct dir_struct *, const char *fname);
440int add_patterns_from_blob_to_list(struct object_id *oid,
Denton Liuad6dad02019-04-29 04:28:23 -0400441 const char *base, int baselen,
Derrick Stoleecaa3d552019-09-03 11:04:56 -0700442 struct pattern_list *pl);
Derrick Stolee65edd962019-09-03 11:04:57 -0700443void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
444void add_pattern(const char *string, const char *base,
Derrick Stoleecaa3d552019-09-03 11:04:56 -0700445 int baselen, struct pattern_list *pl, int srcpos);
Derrick Stolee65edd962019-09-03 11:04:57 -0700446void clear_pattern_list(struct pattern_list *pl);
Elijah Newreneceba532020-08-18 22:58:26 +0000447void dir_clear(struct dir_struct *dir);
Nguyễn Thái Ngọc Duy04884812019-04-16 16:33:34 +0700448
449int repo_file_exists(struct repository *repo, const char *path);
450int file_exists(const char *);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700451
Denton Liu55454422019-04-29 04:28:14 -0400452int is_inside_dir(const char *dir);
453int dir_inside_of(const char *subdir, const char *dir);
Johannes Schindeline6636742007-08-01 01:29:17 +0100454
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300455static inline int is_dot_or_dotdot(const char *name)
456{
457 return (name[0] == '.' &&
458 (name[1] == '\0' ||
459 (name[1] == '.' && name[2] == '\0')));
460}
461
Denton Liu55454422019-04-29 04:28:14 -0400462int is_empty_dir(const char *dir);
Alexander Potashev55892d22009-01-11 15:19:12 +0300463
Atharva Raykared863012021-08-10 17:16:36 +0530464/*
465 * Retrieve the "humanish" basename of the given Git URL.
466 *
467 * For example:
468 * /path/to/repo.git => "repo"
469 * host.xz:foo/.git => "foo"
470 * http://example.com/user/bar.baz => "bar.baz"
471 */
472char *git_url_basename(const char *repo, int is_bundle, int is_bare);
473void strip_dir_trailing_slashes(char *dir);
474
Denton Liu55454422019-04-29 04:28:14 -0400475void setup_standard_excludes(struct dir_struct *dir);
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700476
Derrick Stoleedd230222021-01-23 19:58:17 +0000477char *get_sparse_checkout_filename(void);
478int get_sparse_checkout_patterns(struct pattern_list *pl);
Michael Haggerty728af282016-04-24 07:07:59 +0200479
480/* Constants for remove_dir_recursively: */
481
482/*
483 * If a non-directory is found within path, stop and return an error.
484 * (In this case some empty directories might already have been
485 * removed.)
486 */
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700487#define REMOVE_DIR_EMPTY_ONLY 01
Michael Haggerty728af282016-04-24 07:07:59 +0200488
489/*
490 * If any Git work trees are found within path, skip them without
491 * considering it an error.
492 */
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700493#define REMOVE_DIR_KEEP_NESTED_GIT 02
Michael Haggerty728af282016-04-24 07:07:59 +0200494
495/* Remove the contents of path, but leave path itself. */
Junio C Hamanoc844a802012-03-15 15:58:54 +0100496#define REMOVE_DIR_KEEP_TOPLEVEL 04
Michael Haggerty728af282016-04-24 07:07:59 +0200497
Elijah Newren580a5d72021-12-09 05:08:34 +0000498/* Remove the_original_cwd too */
499#define REMOVE_DIR_PURGE_ORIGINAL_CWD 0x08
500
Michael Haggerty728af282016-04-24 07:07:59 +0200501/*
502 * Remove path and its contents, recursively. flags is a combination
503 * of the above REMOVE_DIR_* constants. Return 0 on success.
504 *
505 * This function uses path as temporary scratch space, but restores it
506 * before returning.
507 */
Denton Liu55454422019-04-29 04:28:14 -0400508int remove_dir_recursively(struct strbuf *path, int flag);
Johannes Schindelin7155b722007-09-28 16:28:54 +0100509
Elijah Newren63bbe8b2021-12-09 05:08:33 +0000510/*
511 * Tries to remove the path, along with leading empty directories so long as
512 * those empty directories are not startup_info->original_cwd. Ignores
513 * ENOENT.
514 */
Denton Liu55454422019-04-29 04:28:14 -0400515int remove_path(const char *path);
Alex Riesen4a92d1b2008-09-27 00:56:46 +0200516
Denton Liu55454422019-04-29 04:28:14 -0400517int fspathcmp(const char *a, const char *b);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000518int fspatheq(const char *a, const char *b);
Denton Liu55454422019-04-29 04:28:14 -0400519int fspathncmp(const char *a, const char *b, size_t count);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000520unsigned int fspathhash(const char *str);
Joshua Jensen8cf2a842010-10-03 09:56:41 +0000521
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700522/*
523 * The prefix part of pattern must not contains wildcards.
524 */
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700525struct pathspec_item;
Denton Liu55454422019-04-29 04:28:14 -0400526int git_fnmatch(const struct pathspec_item *item,
Denton Liuad6dad02019-04-29 04:28:23 -0400527 const char *pattern, const char *string,
528 int prefix);
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700529
Derrick Stolee847a9e52021-04-01 01:49:39 +0000530int submodule_path_match(struct index_state *istate,
Denton Liuad6dad02019-04-29 04:28:23 -0400531 const struct pathspec *ps,
532 const char *submodule_name,
533 char *seen);
Brandon Williams75a63152016-10-07 11:18:51 -0700534
Derrick Stolee847a9e52021-04-01 01:49:39 +0000535static inline int ce_path_match(struct index_state *istate,
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200536 const struct cache_entry *ce,
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700537 const struct pathspec *pathspec,
538 char *seen)
539{
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200540 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700541 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700542}
543
Derrick Stolee847a9e52021-04-01 01:49:39 +0000544static inline int dir_path_match(struct index_state *istate,
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200545 const struct dir_entry *ent,
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700546 const struct pathspec *pathspec,
547 int prefix, char *seen)
548{
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700549 int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
550 int len = has_trailing_dir ? ent->len - 1 : ent->len;
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200551 return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700552 has_trailing_dir);
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700553}
554
Samuel Lijinbbf504a2017-05-18 04:21:53 -0400555int cmp_dir_entry(const void *p1, const void *p2);
556int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
557
Nguyễn Thái Ngọc Duy0cacebf2018-02-07 16:21:40 +0700558void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +0700559void untracked_cache_remove_from_index(struct index_state *, const char *);
560void untracked_cache_add_to_index(struct index_state *, const char *);
561
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +0700562void free_untracked_cache(struct untracked_cache *);
563struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +0700564void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
Christian Couder4a4ca472016-01-24 16:28:19 +0100565void add_untracked_cache(struct index_state *istate);
Christian Couder07b29bf2016-01-24 16:28:20 +0100566void remove_untracked_cache(struct index_state *istate);
Stefan Bellerda62f782018-03-28 15:35:31 -0700567
568/*
569 * Connect a worktree to a git directory by creating (or overwriting) a
570 * '.git' file containing the location of the git directory. In the git
571 * directory set the core.worktree setting to indicate where the worktree is.
572 * When `recurse_into_nested` is set, recurse into any nested submodules,
573 * connecting them as well.
574 */
Denton Liu55454422019-04-29 04:28:14 -0400575void connect_work_tree_and_git_dir(const char *work_tree,
Denton Liuad6dad02019-04-29 04:28:23 -0400576 const char *git_dir,
577 int recurse_into_nested);
Denton Liu55454422019-04-29 04:28:14 -0400578void relocate_gitdir(const char *path,
Denton Liuad6dad02019-04-29 04:28:23 -0400579 const char *old_git_dir,
580 const char *new_git_dir);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700581#endif