dir.h 16 KB

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