Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1 | #ifndef DIR_H |
| 2 | #define DIR_H |
| 3 | |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 4 | /* See Documentation/technical/api-directory-listing.txt */ |
| 5 | |
Junio C Hamano | eb41775 | 2012-06-01 11:28:00 -0700 | [diff] [blame] | 6 | #include "strbuf.h" |
| 7 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 8 | struct dir_entry { |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 9 | unsigned int len; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 10 | char name[FLEX_ARRAY]; /* more */ |
| 11 | }; |
| 12 | |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 13 | #define EXC_FLAG_NODIR 1 |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 14 | #define EXC_FLAG_ENDSWITH 4 |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 15 | #define EXC_FLAG_MUSTBEDIR 8 |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 16 | #define EXC_FLAG_NEGATIVE 16 |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 17 | |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 18 | /* |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 19 | * Each excludes file will be parsed into a fresh exclude_list which |
| 20 | * is appended to the relevant exclude_list_group (either EXC_DIRS or |
| 21 | * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group |
| 22 | * can also be used to represent the list of --exclude values passed |
| 23 | * via CLI args. |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 24 | */ |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 25 | struct exclude_list { |
| 26 | int nr; |
| 27 | int alloc; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 28 | |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 29 | /* remember pointer to exclude file contents so we can free() */ |
| 30 | char *filebuf; |
| 31 | |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 32 | /* origin of list, e.g. path to filename, or descriptive string */ |
| 33 | const char *src; |
| 34 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 35 | struct exclude { |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 36 | /* |
| 37 | * This allows callers of last_exclude_matching() etc. |
| 38 | * to determine the origin of the matching pattern. |
| 39 | */ |
| 40 | struct exclude_list *el; |
| 41 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 42 | const char *pattern; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 43 | int patternlen; |
Nguyễn Thái Ngọc Duy | f9f6e2c | 2012-06-07 14:53:36 +0700 | [diff] [blame] | 44 | int nowildcardlen; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 45 | const char *base; |
| 46 | int baselen; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 47 | int flags; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Counting starts from 1 for line numbers in ignore files, |
| 51 | * and from -1 decrementing for patterns from CLI args. |
| 52 | */ |
| 53 | int srcpos; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 54 | } **excludes; |
| 55 | }; |
| 56 | |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 57 | /* |
| 58 | * The contents of the per-directory exclude files are lazily read on |
| 59 | * demand and then cached in memory, one per exclude_stack struct, in |
| 60 | * order to avoid opening and parsing each one every time that |
| 61 | * directory is traversed. |
| 62 | */ |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 63 | struct exclude_stack { |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 64 | struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */ |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 65 | int baselen; |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 66 | int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */ |
| 67 | }; |
| 68 | |
| 69 | struct exclude_list_group { |
| 70 | int nr, alloc; |
| 71 | struct exclude_list *el; |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 74 | struct dir_struct { |
| 75 | int nr, alloc; |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 76 | int ignored_nr, ignored_alloc; |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 77 | enum { |
| 78 | DIR_SHOW_IGNORED = 1<<0, |
| 79 | DIR_SHOW_OTHER_DIRECTORIES = 1<<1, |
| 80 | DIR_HIDE_EMPTY_DIRECTORIES = 1<<2, |
| 81 | DIR_NO_GITLINKS = 1<<3, |
Karsten Blees | 0aaf62b | 2013-04-15 21:15:03 +0200 | [diff] [blame] | 82 | DIR_COLLECT_IGNORED = 1<<4, |
Junio C Hamano | 2eac2a4 | 2013-08-15 12:13:46 -0700 | [diff] [blame] | 83 | DIR_SHOW_IGNORED_TOO = 1<<5, |
| 84 | DIR_COLLECT_KILLED_ONLY = 1<<6 |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 85 | } flags; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 86 | struct dir_entry **entries; |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 87 | struct dir_entry **ignored; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 88 | |
| 89 | /* Exclude info */ |
| 90 | const char *exclude_per_dir; |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 91 | |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 92 | /* |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 93 | * We maintain three groups of exclude pattern lists: |
| 94 | * |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 95 | * EXC_CMDL lists patterns explicitly given on the command line. |
| 96 | * EXC_DIRS lists patterns obtained from per-directory ignore files. |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 97 | * EXC_FILE lists patterns from fallback ignore files, e.g. |
| 98 | * - .git/info/exclude |
| 99 | * - core.excludesfile |
| 100 | * |
| 101 | * Each group contains multiple exclude lists, a single list |
| 102 | * per source. |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 103 | */ |
| 104 | #define EXC_CMDL 0 |
| 105 | #define EXC_DIRS 1 |
| 106 | #define EXC_FILE 2 |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 107 | struct exclude_list_group exclude_list_group[3]; |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 108 | |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 109 | /* |
| 110 | * Temporary variables which are used during loading of the |
| 111 | * per-directory exclude lists. |
| 112 | * |
| 113 | * exclude_stack points to the top of the exclude_stack, and |
| 114 | * basebuf contains the full path to the current |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 115 | * (sub)directory in the traversal. Exclude points to the |
| 116 | * matching exclude struct if the directory is excluded. |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 117 | */ |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 118 | struct exclude_stack *exclude_stack; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 119 | struct exclude *exclude; |
Junio C Hamano | 63d285c | 2007-11-29 02:17:44 -0800 | [diff] [blame] | 120 | char basebuf[PATH_MAX]; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Adam Spiers | 52ed189 | 2013-01-06 16:58:06 +0000 | [diff] [blame] | 123 | /* |
| 124 | * The ordering of these constants is significant, with |
| 125 | * higher-numbered match types signifying "closer" (i.e. more |
| 126 | * specific) matches which will override lower-numbered match types |
| 127 | * when populating the seen[] array. |
| 128 | */ |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 129 | #define MATCHED_RECURSIVELY 1 |
| 130 | #define MATCHED_FNMATCH 2 |
| 131 | #define MATCHED_EXACTLY 3 |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 132 | extern int simple_length(const char *match); |
| 133 | extern int no_wildcard(const char *string); |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 134 | extern char *common_prefix(const struct pathspec *pathspec); |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 135 | extern int match_pathspec(const struct pathspec *pathspec, |
| 136 | const char *name, int namelen, |
| 137 | int prefix, char *seen); |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 138 | extern int within_depth(const char *name, int namelen, int depth, int max_depth); |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 139 | |
Nguyễn Thái Ngọc Duy | 7327d3d | 2013-07-14 15:35:55 +0700 | [diff] [blame] | 140 | extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec); |
| 141 | extern int read_directory(struct dir_struct *, const char *path, int len, const struct pathspec *pathspec); |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 142 | |
Adam Spiers | 0795805 | 2012-12-27 02:32:24 +0000 | [diff] [blame] | 143 | extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename, |
| 144 | int *dtype, struct exclude_list *el); |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 145 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len); |
Junio C Hamano | eb41775 | 2012-06-01 11:28:00 -0700 | [diff] [blame] | 146 | |
| 147 | /* |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 148 | * these implement the matching logic for dir.c:excluded_from_list and |
| 149 | * attr.c:path_matches() |
| 150 | */ |
| 151 | extern int match_basename(const char *, int, |
| 152 | const char *, int, int, int); |
| 153 | extern int match_pathname(const char *, int, |
| 154 | const char *, int, |
| 155 | const char *, int, int, int); |
| 156 | |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 157 | extern struct exclude *last_exclude_matching(struct dir_struct *dir, |
| 158 | const char *name, int *dtype); |
Junio C Hamano | 782cd4c | 2012-06-05 21:17:52 -0700 | [diff] [blame] | 159 | |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 160 | extern int is_excluded(struct dir_struct *dir, const char *name, int *dtype); |
Junio C Hamano | eb41775 | 2012-06-01 11:28:00 -0700 | [diff] [blame] | 161 | |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 162 | extern struct exclude_list *add_exclude_list(struct dir_struct *dir, |
| 163 | int group_type, const char *src); |
Nguyễn Thái Ngọc Duy | cb09753 | 2009-08-20 20:47:04 +0700 | [diff] [blame] | 164 | extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen, |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 165 | struct exclude_list *el, int check_index); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 166 | extern void add_excludes_from_file(struct dir_struct *, const char *fname); |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 167 | extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 168 | extern void add_exclude(const char *string, const char *base, |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 169 | int baselen, struct exclude_list *el, int srcpos); |
Adam Spiers | f619881 | 2012-12-27 02:32:29 +0000 | [diff] [blame] | 170 | extern void clear_exclude_list(struct exclude_list *el); |
Adam Spiers | 270be81 | 2013-01-06 16:58:05 +0000 | [diff] [blame] | 171 | extern void clear_directory(struct dir_struct *dir); |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 172 | extern int file_exists(const char *); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 173 | |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 174 | extern int is_inside_dir(const char *dir); |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 175 | extern int dir_inside_of(const char *subdir, const char *dir); |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 176 | |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 177 | static inline int is_dot_or_dotdot(const char *name) |
| 178 | { |
| 179 | return (name[0] == '.' && |
| 180 | (name[1] == '\0' || |
| 181 | (name[1] == '.' && name[2] == '\0'))); |
| 182 | } |
| 183 | |
Alexander Potashev | 55892d2 | 2009-01-11 15:19:12 +0300 | [diff] [blame] | 184 | extern int is_empty_dir(const char *dir); |
| 185 | |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 186 | extern void setup_standard_excludes(struct dir_struct *dir); |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 187 | |
| 188 | #define REMOVE_DIR_EMPTY_ONLY 01 |
| 189 | #define REMOVE_DIR_KEEP_NESTED_GIT 02 |
Junio C Hamano | c844a80 | 2012-03-15 15:58:54 +0100 | [diff] [blame] | 190 | #define REMOVE_DIR_KEEP_TOPLEVEL 04 |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 191 | extern int remove_dir_recursively(struct strbuf *path, int flag); |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 192 | |
Alex Riesen | 4a92d1b | 2008-09-27 00:56:46 +0200 | [diff] [blame] | 193 | /* tries to remove the path with empty directories along it, ignores ENOENT */ |
| 194 | extern int remove_path(const char *path); |
| 195 | |
Joshua Jensen | 8cf2a84 | 2010-10-03 09:56:41 +0000 | [diff] [blame] | 196 | extern int strcmp_icase(const char *a, const char *b); |
| 197 | extern int strncmp_icase(const char *a, const char *b, size_t count); |
| 198 | extern int fnmatch_icase(const char *pattern, const char *string, int flags); |
| 199 | |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 200 | /* |
| 201 | * The prefix part of pattern must not contains wildcards. |
| 202 | */ |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 203 | struct pathspec_item; |
| 204 | extern int git_fnmatch(const struct pathspec_item *item, |
| 205 | const char *pattern, const char *string, |
| 206 | int prefix); |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 207 | |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 208 | static inline int ce_path_match(const struct cache_entry *ce, |
| 209 | const struct pathspec *pathspec, |
| 210 | char *seen) |
| 211 | { |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 212 | return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen); |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 213 | } |
| 214 | |
Nguyễn Thái Ngọc Duy | ebb3289 | 2014-01-24 20:40:29 +0700 | [diff] [blame] | 215 | static inline int dir_path_match(const struct dir_entry *ent, |
| 216 | const struct pathspec *pathspec, |
| 217 | int prefix, char *seen) |
| 218 | { |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 219 | return match_pathspec(pathspec, ent->name, ent->len, prefix, seen); |
Nguyễn Thái Ngọc Duy | ebb3289 | 2014-01-24 20:40:29 +0700 | [diff] [blame] | 220 | } |
| 221 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 222 | #endif |