Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This handles recursive filename detection with exclude |
| 3 | * files, index knowledge etc.. |
| 4 | * |
Adam Spiers | 95a6834 | 2012-12-27 02:32:21 +0000 | [diff] [blame] | 5 | * See Documentation/technical/api-directory-listing.txt |
| 6 | * |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 7 | * Copyright (C) Linus Torvalds, 2005-2006 |
| 8 | * Junio Hamano, 2005-2006 |
| 9 | */ |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 10 | #include "cache.h" |
| 11 | #include "dir.h" |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 12 | #include "refs.h" |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 13 | #include "wildmatch.h" |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 14 | #include "pathspec.h" |
Junio C Hamano | dde843e | 2015-04-16 10:45:29 -0700 | [diff] [blame] | 15 | #include "utf8.h" |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 16 | #include "varint.h" |
| 17 | #include "ewah/ewok.h" |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 18 | |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 19 | /* |
| 20 | * Tells read_directory_recursive how a file or directory should be treated. |
| 21 | * Values are ordered by significance, e.g. if a directory contains both |
| 22 | * excluded and untracked files, it is listed as untracked because |
| 23 | * path_untracked > path_excluded. |
| 24 | */ |
| 25 | enum path_treatment { |
| 26 | path_none = 0, |
| 27 | path_recurse, |
| 28 | path_excluded, |
| 29 | path_untracked |
| 30 | }; |
| 31 | |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 32 | /* |
| 33 | * Support data structure for our opendir/readdir/closedir wrappers |
| 34 | */ |
| 35 | struct cached_dir { |
| 36 | DIR *fdir; |
| 37 | struct untracked_cache_dir *untracked; |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 38 | int nr_files; |
| 39 | int nr_dirs; |
| 40 | |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 41 | struct dirent *de; |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 42 | const char *file; |
| 43 | struct untracked_cache_dir *ucd; |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 44 | }; |
| 45 | |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 46 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 47 | const char *path, int len, struct untracked_cache_dir *untracked, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 48 | int check_only, const struct pathspec *pathspec); |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 49 | static int get_dtype(struct dirent *de, const char *path, int len); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 50 | |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 51 | int fspathcmp(const char *a, const char *b) |
Joshua Jensen | 8cf2a84 | 2010-10-03 09:56:41 +0000 | [diff] [blame] | 52 | { |
| 53 | return ignore_case ? strcasecmp(a, b) : strcmp(a, b); |
| 54 | } |
| 55 | |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 56 | int fspathncmp(const char *a, const char *b, size_t count) |
Joshua Jensen | 8cf2a84 | 2010-10-03 09:56:41 +0000 | [diff] [blame] | 57 | { |
| 58 | return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); |
| 59 | } |
| 60 | |
Charles Bailey | 1f26ce6 | 2014-03-29 15:39:00 +0000 | [diff] [blame] | 61 | int git_fnmatch(const struct pathspec_item *item, |
| 62 | const char *pattern, const char *string, |
| 63 | int prefix) |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 64 | { |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 65 | if (prefix > 0) { |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 66 | if (ps_strncmp(item, pattern, string, prefix)) |
Nguyễn Thái Ngọc Duy | eb07894 | 2014-02-15 09:01:46 +0700 | [diff] [blame] | 67 | return WM_NOMATCH; |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 68 | pattern += prefix; |
| 69 | string += prefix; |
| 70 | } |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 71 | if (item->flags & PATHSPEC_ONESTAR) { |
Nguyễn Thái Ngọc Duy | 8c6abbc | 2012-11-24 11:33:50 +0700 | [diff] [blame] | 72 | int pattern_len = strlen(++pattern); |
| 73 | int string_len = strlen(string); |
| 74 | return string_len < pattern_len || |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 75 | ps_strcmp(item, pattern, |
| 76 | string + string_len - pattern_len); |
Nguyễn Thái Ngọc Duy | 8c6abbc | 2012-11-24 11:33:50 +0700 | [diff] [blame] | 77 | } |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 78 | if (item->magic & PATHSPEC_GLOB) |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 79 | return wildmatch(pattern, string, |
| 80 | WM_PATHNAME | |
| 81 | (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0), |
| 82 | NULL); |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 83 | else |
| 84 | /* wildmatch has not learned no FNM_PATHNAME mode yet */ |
Nguyễn Thái Ngọc Duy | eb07894 | 2014-02-15 09:01:46 +0700 | [diff] [blame] | 85 | return wildmatch(pattern, string, |
| 86 | item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0, |
| 87 | NULL); |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 88 | } |
| 89 | |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 90 | static int fnmatch_icase_mem(const char *pattern, int patternlen, |
| 91 | const char *string, int stringlen, |
| 92 | int flags) |
| 93 | { |
| 94 | int match_status; |
| 95 | struct strbuf pat_buf = STRBUF_INIT; |
| 96 | struct strbuf str_buf = STRBUF_INIT; |
| 97 | const char *use_pat = pattern; |
| 98 | const char *use_str = string; |
| 99 | |
| 100 | if (pattern[patternlen]) { |
| 101 | strbuf_add(&pat_buf, pattern, patternlen); |
| 102 | use_pat = pat_buf.buf; |
| 103 | } |
| 104 | if (string[stringlen]) { |
| 105 | strbuf_add(&str_buf, string, stringlen); |
| 106 | use_str = str_buf.buf; |
| 107 | } |
| 108 | |
Junio C Hamano | f30366b | 2013-04-03 09:34:04 -0700 | [diff] [blame] | 109 | if (ignore_case) |
| 110 | flags |= WM_CASEFOLD; |
| 111 | match_status = wildmatch(use_pat, use_str, flags, NULL); |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 112 | |
| 113 | strbuf_release(&pat_buf); |
| 114 | strbuf_release(&str_buf); |
| 115 | |
| 116 | return match_status; |
| 117 | } |
| 118 | |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 119 | static size_t common_prefix_len(const struct pathspec *pathspec) |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 120 | { |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 121 | int n; |
Junio C Hamano | 4a085b1 | 2011-09-06 12:32:30 -0700 | [diff] [blame] | 122 | size_t max = 0; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 123 | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 124 | /* |
| 125 | * ":(icase)path" is treated as a pathspec full of |
| 126 | * wildcard. In other words, only prefix is considered common |
| 127 | * prefix. If the pathspec is abc/foo abc/bar, running in |
| 128 | * subdir xyz, the common prefix is still xyz, not xuz/abc as |
| 129 | * in non-:(icase). |
| 130 | */ |
Nguyễn Thái Ngọc Duy | 5c6933d | 2013-07-14 15:36:06 +0700 | [diff] [blame] | 131 | GUARD_PATHSPEC(pathspec, |
| 132 | PATHSPEC_FROMTOP | |
| 133 | PATHSPEC_MAXDEPTH | |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 134 | PATHSPEC_LITERAL | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 135 | PATHSPEC_GLOB | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 136 | PATHSPEC_ICASE | |
| 137 | PATHSPEC_EXCLUDE); |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 138 | |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 139 | for (n = 0; n < pathspec->nr; n++) { |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 140 | size_t i = 0, len = 0, item_len; |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 141 | if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) |
| 142 | continue; |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 143 | if (pathspec->items[n].magic & PATHSPEC_ICASE) |
| 144 | item_len = pathspec->items[n].prefix; |
| 145 | else |
| 146 | item_len = pathspec->items[n].nowildcard_len; |
| 147 | while (i < item_len && (n == 0 || i < max)) { |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 148 | char c = pathspec->items[n].match[i]; |
| 149 | if (c != pathspec->items[0].match[i]) |
Junio C Hamano | 4a085b1 | 2011-09-06 12:32:30 -0700 | [diff] [blame] | 150 | break; |
| 151 | if (c == '/') |
| 152 | len = i + 1; |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 153 | i++; |
Junio C Hamano | 4a085b1 | 2011-09-06 12:32:30 -0700 | [diff] [blame] | 154 | } |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 155 | if (n == 0 || len < max) { |
Junio C Hamano | 4a085b1 | 2011-09-06 12:32:30 -0700 | [diff] [blame] | 156 | max = len; |
| 157 | if (!max) |
| 158 | break; |
| 159 | } |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 160 | } |
Junio C Hamano | 4a085b1 | 2011-09-06 12:32:30 -0700 | [diff] [blame] | 161 | return max; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Clemens Buchacher | f950eb9 | 2011-09-04 12:42:01 +0200 | [diff] [blame] | 164 | /* |
| 165 | * Returns a copy of the longest leading path common among all |
| 166 | * pathspecs. |
| 167 | */ |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 168 | char *common_prefix(const struct pathspec *pathspec) |
Clemens Buchacher | f950eb9 | 2011-09-04 12:42:01 +0200 | [diff] [blame] | 169 | { |
| 170 | unsigned long len = common_prefix_len(pathspec); |
| 171 | |
Nguyễn Thái Ngọc Duy | 827f4d6 | 2013-07-14 15:35:57 +0700 | [diff] [blame] | 172 | return len ? xmemdupz(pathspec->items[0].match, len) : NULL; |
Clemens Buchacher | f950eb9 | 2011-09-04 12:42:01 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Nguyễn Thái Ngọc Duy | 7327d3d | 2013-07-14 15:35:55 +0700 | [diff] [blame] | 175 | int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 176 | { |
Brandon Williams | 966de30 | 2017-01-04 10:03:58 -0800 | [diff] [blame] | 177 | char *prefix; |
| 178 | size_t prefix_len; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Calculate common prefix for the pathspec, and |
| 182 | * use that to optimize the directory walk |
| 183 | */ |
Brandon Williams | 966de30 | 2017-01-04 10:03:58 -0800 | [diff] [blame] | 184 | prefix = common_prefix(pathspec); |
| 185 | prefix_len = prefix ? strlen(prefix) : 0; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 186 | |
| 187 | /* Read the directory and prune it */ |
Brandon Williams | 966de30 | 2017-01-04 10:03:58 -0800 | [diff] [blame] | 188 | read_directory(dir, prefix, prefix_len, pathspec); |
| 189 | |
| 190 | free(prefix); |
| 191 | return prefix_len; |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 194 | int within_depth(const char *name, int namelen, |
| 195 | int depth, int max_depth) |
| 196 | { |
| 197 | const char *cp = name, *cpe = name + namelen; |
| 198 | |
| 199 | while (cp < cpe) { |
| 200 | if (*cp++ != '/') |
| 201 | continue; |
| 202 | depth++; |
| 203 | if (depth > max_depth) |
| 204 | return 0; |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 209 | #define DO_MATCH_EXCLUDE (1<<0) |
| 210 | #define DO_MATCH_DIRECTORY (1<<1) |
| 211 | #define DO_MATCH_SUBMODULE (1<<2) |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 212 | |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 213 | /* |
Allan Caffee | 2c5b011 | 2009-05-04 13:37:30 -0400 | [diff] [blame] | 214 | * Does 'match' match the given name? |
Junio C Hamano | e813d50 | 2006-12-25 03:09:52 -0800 | [diff] [blame] | 215 | * A match is found if |
| 216 | * |
| 217 | * (1) the 'match' string is leading directory of 'name', or |
| 218 | * (2) the 'match' string is a wildcard and matches 'name', or |
| 219 | * (3) the 'match' string is exactly the same as 'name'. |
| 220 | * |
| 221 | * and the return value tells which case it was. |
| 222 | * |
| 223 | * It returns 0 when there is no match. |
| 224 | */ |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 225 | static int match_pathspec_item(const struct pathspec_item *item, int prefix, |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 226 | const char *name, int namelen, unsigned flags) |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 227 | { |
| 228 | /* name/namelen has prefix cut off by caller */ |
| 229 | const char *match = item->match + prefix; |
| 230 | int matchlen = item->len - prefix; |
| 231 | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 232 | /* |
| 233 | * The normal call pattern is: |
| 234 | * 1. prefix = common_prefix_len(ps); |
| 235 | * 2. prune something, or fill_directory |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 236 | * 3. match_pathspec() |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 237 | * |
| 238 | * 'prefix' at #1 may be shorter than the command's prefix and |
| 239 | * it's ok for #2 to match extra files. Those extras will be |
| 240 | * trimmed at #3. |
| 241 | * |
| 242 | * Suppose the pathspec is 'foo' and '../bar' running from |
| 243 | * subdir 'xyz'. The common prefix at #1 will be empty, thanks |
| 244 | * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The |
| 245 | * user does not want XYZ/foo, only the "foo" part should be |
| 246 | * case-insensitive. We need to filter out XYZ/foo here. In |
| 247 | * other words, we do not trust the caller on comparing the |
| 248 | * prefix part when :(icase) is involved. We do exact |
| 249 | * comparison ourselves. |
| 250 | * |
| 251 | * Normally the caller (common_prefix_len() in fact) does |
| 252 | * _exact_ matching on name[-prefix+1..-1] and we do not need |
| 253 | * to check that part. Be defensive and check it anyway, in |
| 254 | * case common_prefix_len is changed, or a new caller is |
| 255 | * introduced that does not use common_prefix_len. |
| 256 | * |
| 257 | * If the penalty turns out too high when prefix is really |
| 258 | * long, maybe change it to |
| 259 | * strncmp(match, name, item->prefix - prefix) |
| 260 | */ |
| 261 | if (item->prefix && (item->magic & PATHSPEC_ICASE) && |
| 262 | strncmp(item->match, name - prefix, item->prefix)) |
| 263 | return 0; |
| 264 | |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 265 | /* If the match was just the prefix, we matched */ |
| 266 | if (!*match) |
| 267 | return MATCHED_RECURSIVELY; |
| 268 | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 269 | if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 270 | if (matchlen == namelen) |
| 271 | return MATCHED_EXACTLY; |
| 272 | |
| 273 | if (match[matchlen-1] == '/' || name[matchlen] == '/') |
| 274 | return MATCHED_RECURSIVELY; |
Nguyễn Thái Ngọc Duy | 68690fd | 2014-01-24 20:40:32 +0700 | [diff] [blame] | 275 | } else if ((flags & DO_MATCH_DIRECTORY) && |
| 276 | match[matchlen - 1] == '/' && |
| 277 | namelen == matchlen - 1 && |
| 278 | !ps_strncmp(item, match, name, namelen)) |
| 279 | return MATCHED_EXACTLY; |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 280 | |
Nguyễn Thái Ngọc Duy | 5d74762 | 2012-11-24 11:33:49 +0700 | [diff] [blame] | 281 | if (item->nowildcard_len < item->len && |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 282 | !git_fnmatch(item, match, name, |
Nguyễn Thái Ngọc Duy | 8c6abbc | 2012-11-24 11:33:50 +0700 | [diff] [blame] | 283 | item->nowildcard_len - prefix)) |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 284 | return MATCHED_FNMATCH; |
| 285 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 286 | /* Perform checks to see if "name" is a super set of the pathspec */ |
| 287 | if (flags & DO_MATCH_SUBMODULE) { |
| 288 | /* name is a literal prefix of the pathspec */ |
| 289 | if ((namelen < matchlen) && |
| 290 | (match[namelen] == '/') && |
| 291 | !ps_strncmp(item, match, name, namelen)) |
| 292 | return MATCHED_RECURSIVELY; |
| 293 | |
| 294 | /* name" doesn't match up to the first wild character */ |
| 295 | if (item->nowildcard_len < item->len && |
| 296 | ps_strncmp(item, match, name, |
| 297 | item->nowildcard_len - prefix)) |
| 298 | return 0; |
| 299 | |
| 300 | /* |
| 301 | * Here is where we would perform a wildmatch to check if |
| 302 | * "name" can be matched as a directory (or a prefix) against |
| 303 | * the pathspec. Since wildmatch doesn't have this capability |
| 304 | * at the present we have to punt and say that it is a match, |
| 305 | * potentially returning a false positive |
| 306 | * The submodules themselves will be able to perform more |
| 307 | * accurate matching to determine if the pathspec matches. |
| 308 | */ |
| 309 | return MATCHED_RECURSIVELY; |
| 310 | } |
| 311 | |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
Adam Spiers | 52ed189 | 2013-01-06 16:58:06 +0000 | [diff] [blame] | 316 | * Given a name and a list of pathspecs, returns the nature of the |
| 317 | * closest (i.e. most specific) match of the name to any of the |
| 318 | * pathspecs. |
| 319 | * |
| 320 | * The caller typically calls this multiple times with the same |
| 321 | * pathspec and seen[] array but with different name/namelen |
| 322 | * (e.g. entries from the index) and is interested in seeing if and |
| 323 | * how each pathspec matches all the names it calls this function |
| 324 | * with. A mark is left in the seen[] array for each pathspec element |
| 325 | * indicating the closest type of match that element achieved, so if |
| 326 | * seen[n] remains zero after multiple invocations, that means the nth |
| 327 | * pathspec did not match any names, which could indicate that the |
| 328 | * user mistyped the nth pathspec. |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 329 | */ |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 330 | static int do_match_pathspec(const struct pathspec *ps, |
| 331 | const char *name, int namelen, |
| 332 | int prefix, char *seen, |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 333 | unsigned flags) |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 334 | { |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 335 | int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 336 | |
Nguyễn Thái Ngọc Duy | 5c6933d | 2013-07-14 15:36:06 +0700 | [diff] [blame] | 337 | GUARD_PATHSPEC(ps, |
| 338 | PATHSPEC_FROMTOP | |
| 339 | PATHSPEC_MAXDEPTH | |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 340 | PATHSPEC_LITERAL | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 341 | PATHSPEC_GLOB | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 342 | PATHSPEC_ICASE | |
| 343 | PATHSPEC_EXCLUDE); |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 344 | |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 345 | if (!ps->nr) { |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 346 | if (!ps->recursive || |
| 347 | !(ps->magic & PATHSPEC_MAXDEPTH) || |
| 348 | ps->max_depth == -1) |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 349 | return MATCHED_RECURSIVELY; |
| 350 | |
| 351 | if (within_depth(name, namelen, 0, ps->max_depth)) |
| 352 | return MATCHED_EXACTLY; |
| 353 | else |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | name += prefix; |
| 358 | namelen -= prefix; |
| 359 | |
| 360 | for (i = ps->nr - 1; i >= 0; i--) { |
| 361 | int how; |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 362 | |
| 363 | if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || |
| 364 | ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) |
| 365 | continue; |
| 366 | |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 367 | if (seen && seen[i] == MATCHED_EXACTLY) |
| 368 | continue; |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 369 | /* |
| 370 | * Make exclude patterns optional and never report |
| 371 | * "pathspec ':(exclude)foo' matches no files" |
| 372 | */ |
| 373 | if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) |
| 374 | seen[i] = MATCHED_FNMATCH; |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 375 | how = match_pathspec_item(ps->items+i, prefix, name, |
| 376 | namelen, flags); |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 377 | if (ps->recursive && |
| 378 | (ps->magic & PATHSPEC_MAXDEPTH) && |
| 379 | ps->max_depth != -1 && |
Nguyễn Thái Ngọc Duy | 61cf282 | 2010-12-15 22:02:48 +0700 | [diff] [blame] | 380 | how && how != MATCHED_FNMATCH) { |
| 381 | int len = ps->items[i].len; |
| 382 | if (name[len] == '/') |
| 383 | len++; |
| 384 | if (within_depth(name+len, namelen-len, 0, ps->max_depth)) |
| 385 | how = MATCHED_EXACTLY; |
| 386 | else |
| 387 | how = 0; |
| 388 | } |
| 389 | if (how) { |
| 390 | if (retval < how) |
| 391 | retval = how; |
| 392 | if (seen && seen[i] < how) |
| 393 | seen[i] = how; |
| 394 | } |
| 395 | } |
| 396 | return retval; |
| 397 | } |
| 398 | |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 399 | int match_pathspec(const struct pathspec *ps, |
| 400 | const char *name, int namelen, |
Nguyễn Thái Ngọc Duy | ae8d082 | 2014-01-24 20:40:33 +0700 | [diff] [blame] | 401 | int prefix, char *seen, int is_dir) |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 402 | { |
| 403 | int positive, negative; |
Nguyễn Thái Ngọc Duy | ae8d082 | 2014-01-24 20:40:33 +0700 | [diff] [blame] | 404 | unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 405 | positive = do_match_pathspec(ps, name, namelen, |
| 406 | prefix, seen, flags); |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 407 | if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) |
| 408 | return positive; |
Nguyễn Thái Ngọc Duy | 42b0874 | 2014-01-24 20:40:31 +0700 | [diff] [blame] | 409 | negative = do_match_pathspec(ps, name, namelen, |
| 410 | prefix, seen, |
| 411 | flags | DO_MATCH_EXCLUDE); |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 412 | return negative ? 0 : positive; |
| 413 | } |
| 414 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Check if a submodule is a superset of the pathspec |
| 417 | */ |
| 418 | int submodule_path_match(const struct pathspec *ps, |
| 419 | const char *submodule_name, |
| 420 | char *seen) |
| 421 | { |
| 422 | int matched = do_match_pathspec(ps, submodule_name, |
| 423 | strlen(submodule_name), |
| 424 | 0, seen, |
| 425 | DO_MATCH_DIRECTORY | |
| 426 | DO_MATCH_SUBMODULE); |
| 427 | return matched; |
| 428 | } |
| 429 | |
Junio C Hamano | 777c55a | 2015-03-24 14:12:10 -0700 | [diff] [blame] | 430 | int report_path_error(const char *ps_matched, |
| 431 | const struct pathspec *pathspec, |
| 432 | const char *prefix) |
| 433 | { |
| 434 | /* |
| 435 | * Make sure all pathspec matched; otherwise it is an error. |
| 436 | */ |
Junio C Hamano | 777c55a | 2015-03-24 14:12:10 -0700 | [diff] [blame] | 437 | int num, errors = 0; |
| 438 | for (num = 0; num < pathspec->nr; num++) { |
| 439 | int other, found_dup; |
| 440 | |
| 441 | if (ps_matched[num]) |
| 442 | continue; |
| 443 | /* |
| 444 | * The caller might have fed identical pathspec |
| 445 | * twice. Do not barf on such a mistake. |
| 446 | * FIXME: parse_pathspec should have eliminated |
| 447 | * duplicate pathspec. |
| 448 | */ |
| 449 | for (found_dup = other = 0; |
| 450 | !found_dup && other < pathspec->nr; |
| 451 | other++) { |
| 452 | if (other == num || !ps_matched[other]) |
| 453 | continue; |
| 454 | if (!strcmp(pathspec->items[other].original, |
| 455 | pathspec->items[num].original)) |
| 456 | /* |
| 457 | * Ok, we have a match already. |
| 458 | */ |
| 459 | found_dup = 1; |
| 460 | } |
| 461 | if (found_dup) |
| 462 | continue; |
| 463 | |
| 464 | error("pathspec '%s' did not match any file(s) known to git.", |
| 465 | pathspec->items[num].original); |
| 466 | errors++; |
| 467 | } |
Junio C Hamano | 777c55a | 2015-03-24 14:12:10 -0700 | [diff] [blame] | 468 | return errors; |
| 469 | } |
| 470 | |
Nguyễn Thái Ngọc Duy | fcd631e | 2012-06-07 14:53:35 +0700 | [diff] [blame] | 471 | /* |
| 472 | * Return the length of the "simple" part of a path match limiter. |
| 473 | */ |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 474 | int simple_length(const char *match) |
Nguyễn Thái Ngọc Duy | fcd631e | 2012-06-07 14:53:35 +0700 | [diff] [blame] | 475 | { |
| 476 | int len = -1; |
| 477 | |
| 478 | for (;;) { |
| 479 | unsigned char c = *match++; |
| 480 | len++; |
| 481 | if (c == '\0' || is_glob_special(c)) |
| 482 | return len; |
| 483 | } |
| 484 | } |
| 485 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 486 | int no_wildcard(const char *string) |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 487 | { |
Nguyễn Thái Ngọc Duy | fcd631e | 2012-06-07 14:53:35 +0700 | [diff] [blame] | 488 | return string[simple_length(string)] == '\0'; |
Lars Knoll | 68492fc | 2007-10-28 21:27:13 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 491 | void parse_exclude_pattern(const char **pattern, |
| 492 | int *patternlen, |
Saurav Sachidanand | f870899 | 2016-03-01 22:32:59 +0530 | [diff] [blame] | 493 | unsigned *flags, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 494 | int *nowildcardlen) |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 495 | { |
| 496 | const char *p = *pattern; |
| 497 | size_t i, len; |
| 498 | |
| 499 | *flags = 0; |
| 500 | if (*p == '!') { |
| 501 | *flags |= EXC_FLAG_NEGATIVE; |
| 502 | p++; |
| 503 | } |
| 504 | len = strlen(p); |
| 505 | if (len && p[len - 1] == '/') { |
| 506 | len--; |
| 507 | *flags |= EXC_FLAG_MUSTBEDIR; |
| 508 | } |
| 509 | for (i = 0; i < len; i++) { |
| 510 | if (p[i] == '/') |
| 511 | break; |
| 512 | } |
| 513 | if (i == len) |
| 514 | *flags |= EXC_FLAG_NODIR; |
| 515 | *nowildcardlen = simple_length(p); |
| 516 | /* |
| 517 | * we should have excluded the trailing slash from 'p' too, |
| 518 | * but that's one more allocation. Instead just make sure |
| 519 | * nowildcardlen does not exceed real patternlen |
| 520 | */ |
| 521 | if (*nowildcardlen > len) |
| 522 | *nowildcardlen = len; |
| 523 | if (*p == '*' && no_wildcard(p + 1)) |
| 524 | *flags |= EXC_FLAG_ENDSWITH; |
| 525 | *pattern = p; |
| 526 | *patternlen = len; |
| 527 | } |
| 528 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 529 | void add_exclude(const char *string, const char *base, |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 530 | int baselen, struct exclude_list *el, int srcpos) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 531 | { |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 532 | struct exclude *x; |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 533 | int patternlen; |
Saurav Sachidanand | f870899 | 2016-03-01 22:32:59 +0530 | [diff] [blame] | 534 | unsigned flags; |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 535 | int nowildcardlen; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 536 | |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 537 | parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); |
| 538 | if (flags & EXC_FLAG_MUSTBEDIR) { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 539 | FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen); |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 540 | } else { |
| 541 | x = xmalloc(sizeof(*x)); |
| 542 | x->pattern = string; |
| 543 | } |
Nguyễn Thái Ngọc Duy | 84460ee | 2012-10-15 13:24:38 +0700 | [diff] [blame] | 544 | x->patternlen = patternlen; |
| 545 | x->nowildcardlen = nowildcardlen; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 546 | x->base = base; |
| 547 | x->baselen = baselen; |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 548 | x->flags = flags; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 549 | x->srcpos = srcpos; |
Adam Spiers | 840fc33 | 2012-12-27 02:32:22 +0000 | [diff] [blame] | 550 | ALLOC_GROW(el->excludes, el->nr + 1, el->alloc); |
| 551 | el->excludes[el->nr++] = x; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 552 | x->el = el; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 555 | static void *read_skip_worktree_file_from_index(const char *path, size_t *size, |
| 556 | struct sha1_stat *sha1_stat) |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 557 | { |
| 558 | int pos, len; |
| 559 | unsigned long sz; |
| 560 | enum object_type type; |
| 561 | void *data; |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 562 | |
| 563 | len = strlen(path); |
Junio C Hamano | 7126102 | 2013-08-15 12:08:45 -0700 | [diff] [blame] | 564 | pos = cache_name_pos(path, len); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 565 | if (pos < 0) |
| 566 | return NULL; |
Junio C Hamano | 7126102 | 2013-08-15 12:08:45 -0700 | [diff] [blame] | 567 | if (!ce_skip_worktree(active_cache[pos])) |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 568 | return NULL; |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 569 | data = read_sha1_file(active_cache[pos]->oid.hash, &type, &sz); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 570 | if (!data || type != OBJ_BLOB) { |
| 571 | free(data); |
| 572 | return NULL; |
| 573 | } |
| 574 | *size = xsize_t(sz); |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 575 | if (sha1_stat) { |
| 576 | memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat)); |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 577 | hashcpy(sha1_stat->sha1, active_cache[pos]->oid.hash); |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 578 | } |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 579 | return data; |
| 580 | } |
| 581 | |
Adam Spiers | f619881 | 2012-12-27 02:32:29 +0000 | [diff] [blame] | 582 | /* |
| 583 | * Frees memory within el which was allocated for exclude patterns and |
| 584 | * the file buffer. Does not free el itself. |
| 585 | */ |
| 586 | void clear_exclude_list(struct exclude_list *el) |
Nguyễn Thái Ngọc Duy | 0fd0e24 | 2010-11-27 01:17:44 +0700 | [diff] [blame] | 587 | { |
| 588 | int i; |
| 589 | |
Junio C Hamano | 5cee349 | 2016-03-18 11:06:15 -0700 | [diff] [blame] | 590 | for (i = 0; i < el->nr; i++) |
Nguyễn Thái Ngọc Duy | 0fd0e24 | 2010-11-27 01:17:44 +0700 | [diff] [blame] | 591 | free(el->excludes[i]); |
| 592 | free(el->excludes); |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 593 | free(el->filebuf); |
Nguyễn Thái Ngọc Duy | 0fd0e24 | 2010-11-27 01:17:44 +0700 | [diff] [blame] | 594 | |
Nguyễn Thái Ngọc Duy | 2653a8c | 2015-12-27 08:54:34 +0700 | [diff] [blame] | 595 | memset(el, 0, sizeof(*el)); |
Nguyễn Thái Ngọc Duy | 0fd0e24 | 2010-11-27 01:17:44 +0700 | [diff] [blame] | 596 | } |
| 597 | |
Nguyễn Thái Ngọc Duy | 7e2e4b3 | 2014-02-09 07:26:38 +0700 | [diff] [blame] | 598 | static void trim_trailing_spaces(char *buf) |
Nguyễn Thái Ngọc Duy | 16402b9 | 2014-02-09 07:26:37 +0700 | [diff] [blame] | 599 | { |
Pasha Bolokhov | e61a6c1 | 2014-06-02 15:36:56 -0700 | [diff] [blame] | 600 | char *p, *last_space = NULL; |
Nguyễn Thái Ngọc Duy | 16402b9 | 2014-02-09 07:26:37 +0700 | [diff] [blame] | 601 | |
Pasha Bolokhov | e61a6c1 | 2014-06-02 15:36:56 -0700 | [diff] [blame] | 602 | for (p = buf; *p; p++) |
| 603 | switch (*p) { |
| 604 | case ' ': |
| 605 | if (!last_space) |
| 606 | last_space = p; |
| 607 | break; |
| 608 | case '\\': |
| 609 | p++; |
| 610 | if (!*p) |
| 611 | return; |
| 612 | /* fallthrough */ |
| 613 | default: |
| 614 | last_space = NULL; |
| 615 | } |
| 616 | |
| 617 | if (last_space) |
| 618 | *last_space = '\0'; |
Nguyễn Thái Ngọc Duy | 16402b9 | 2014-02-09 07:26:37 +0700 | [diff] [blame] | 619 | } |
| 620 | |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 621 | /* |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 622 | * Given a subdirectory name and "dir" of the current directory, |
| 623 | * search the subdir in "dir" and return it, or create a new one if it |
| 624 | * does not exist in "dir". |
| 625 | * |
| 626 | * If "name" has the trailing slash, it'll be excluded in the search. |
| 627 | */ |
| 628 | static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, |
| 629 | struct untracked_cache_dir *dir, |
| 630 | const char *name, int len) |
| 631 | { |
| 632 | int first, last; |
| 633 | struct untracked_cache_dir *d; |
| 634 | if (!dir) |
| 635 | return NULL; |
| 636 | if (len && name[len - 1] == '/') |
| 637 | len--; |
| 638 | first = 0; |
| 639 | last = dir->dirs_nr; |
| 640 | while (last > first) { |
| 641 | int cmp, next = (last + first) >> 1; |
| 642 | d = dir->dirs[next]; |
| 643 | cmp = strncmp(name, d->name, len); |
| 644 | if (!cmp && strlen(d->name) > len) |
| 645 | cmp = -1; |
| 646 | if (!cmp) |
| 647 | return d; |
| 648 | if (cmp < 0) { |
| 649 | last = next; |
| 650 | continue; |
| 651 | } |
| 652 | first = next+1; |
| 653 | } |
| 654 | |
| 655 | uc->dir_created++; |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 656 | FLEX_ALLOC_MEM(d, name, name, len); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 657 | |
| 658 | ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); |
| 659 | memmove(dir->dirs + first + 1, dir->dirs + first, |
| 660 | (dir->dirs_nr - first) * sizeof(*dir->dirs)); |
| 661 | dir->dirs_nr++; |
| 662 | dir->dirs[first] = d; |
| 663 | return d; |
| 664 | } |
| 665 | |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 666 | static void do_invalidate_gitignore(struct untracked_cache_dir *dir) |
| 667 | { |
| 668 | int i; |
| 669 | dir->valid = 0; |
| 670 | dir->untracked_nr = 0; |
| 671 | for (i = 0; i < dir->dirs_nr; i++) |
| 672 | do_invalidate_gitignore(dir->dirs[i]); |
| 673 | } |
| 674 | |
| 675 | static void invalidate_gitignore(struct untracked_cache *uc, |
| 676 | struct untracked_cache_dir *dir) |
| 677 | { |
| 678 | uc->gitignore_invalidated++; |
| 679 | do_invalidate_gitignore(dir); |
| 680 | } |
| 681 | |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 682 | static void invalidate_directory(struct untracked_cache *uc, |
| 683 | struct untracked_cache_dir *dir) |
| 684 | { |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 685 | int i; |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 686 | uc->dir_invalidated++; |
| 687 | dir->valid = 0; |
| 688 | dir->untracked_nr = 0; |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 689 | for (i = 0; i < dir->dirs_nr; i++) |
| 690 | dir->dirs[i]->recurse = 0; |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 691 | } |
| 692 | |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 693 | /* |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 694 | * Given a file with name "fname", read it (either from disk, or from |
| 695 | * the index if "check_index" is non-zero), parse it and store the |
| 696 | * exclude rules in "el". |
| 697 | * |
| 698 | * If "ss" is not NULL, compute SHA-1 of the exclude file and fill |
| 699 | * stat data from disk (only valid if add_excludes returns zero). If |
| 700 | * ss_valid is non-zero, "ss" must contain good value as input. |
| 701 | */ |
| 702 | static int add_excludes(const char *fname, const char *base, int baselen, |
| 703 | struct exclude_list *el, int check_index, |
| 704 | struct sha1_stat *sha1_stat) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 705 | { |
Jonas Fonseca | c470701 | 2006-08-28 01:55:46 +0200 | [diff] [blame] | 706 | struct stat st; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 707 | int fd, i, lineno = 1; |
Pat Notz | 9d14017 | 2010-09-16 14:53:22 -0600 | [diff] [blame] | 708 | size_t size = 0; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 709 | char *buf, *entry; |
| 710 | |
| 711 | fd = open(fname, O_RDONLY); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 712 | if (fd < 0 || fstat(fd, &st) < 0) { |
Jeff King | 6966073 | 2012-08-21 02:26:07 -0400 | [diff] [blame] | 713 | if (errno != ENOENT) |
Junio C Hamano | 55b38a4 | 2012-08-21 14:52:07 -0700 | [diff] [blame] | 714 | warn_on_inaccessible(fname); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 715 | if (0 <= fd) |
| 716 | close(fd); |
| 717 | if (!check_index || |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 718 | (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 719 | return -1; |
Nguyễn Thái Ngọc Duy | 45d76f1 | 2010-01-20 21:09:16 +0700 | [diff] [blame] | 720 | if (size == 0) { |
| 721 | free(buf); |
| 722 | return 0; |
| 723 | } |
| 724 | if (buf[size-1] != '\n') { |
Jeff King | 50a6c8e | 2016-02-22 17:44:35 -0500 | [diff] [blame] | 725 | buf = xrealloc(buf, st_add(size, 1)); |
Nguyễn Thái Ngọc Duy | 45d76f1 | 2010-01-20 21:09:16 +0700 | [diff] [blame] | 726 | buf[size++] = '\n'; |
| 727 | } |
Nguyễn Thái Ngọc Duy | d961baa | 2014-07-14 11:47:11 +0200 | [diff] [blame] | 728 | } else { |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 729 | size = xsize_t(st.st_size); |
| 730 | if (size == 0) { |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 731 | if (sha1_stat) { |
| 732 | fill_stat_data(&sha1_stat->stat, &st); |
| 733 | hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); |
| 734 | sha1_stat->valid = 1; |
| 735 | } |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 736 | close(fd); |
| 737 | return 0; |
| 738 | } |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 739 | buf = xmallocz(size); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 740 | if (read_in_full(fd, buf, size) != size) { |
Nguyễn Thái Ngọc Duy | 45d76f1 | 2010-01-20 21:09:16 +0700 | [diff] [blame] | 741 | free(buf); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 742 | close(fd); |
| 743 | return -1; |
| 744 | } |
Nguyễn Thái Ngọc Duy | 45d76f1 | 2010-01-20 21:09:16 +0700 | [diff] [blame] | 745 | buf[size++] = '\n'; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 746 | close(fd); |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 747 | if (sha1_stat) { |
| 748 | int pos; |
| 749 | if (sha1_stat->valid && |
Nguyễn Thái Ngọc Duy | ed4efab | 2015-03-08 17:12:37 +0700 | [diff] [blame] | 750 | !match_stat_data_racy(&the_index, &sha1_stat->stat, &st)) |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 751 | ; /* no content change, ss->sha1 still good */ |
| 752 | else if (check_index && |
| 753 | (pos = cache_name_pos(fname, strlen(fname))) >= 0 && |
| 754 | !ce_stage(active_cache[pos]) && |
| 755 | ce_uptodate(active_cache[pos]) && |
| 756 | !would_convert_to_git(fname)) |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 757 | hashcpy(sha1_stat->sha1, |
| 758 | active_cache[pos]->oid.hash); |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 759 | else |
| 760 | hash_sha1_file(buf, size, "blob", sha1_stat->sha1); |
| 761 | fill_stat_data(&sha1_stat->stat, &st); |
| 762 | sha1_stat->valid = 1; |
| 763 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 764 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 765 | |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 766 | el->filebuf = buf; |
Carlos Martín Nieto | 245e1c1 | 2015-04-16 16:05:12 +0200 | [diff] [blame] | 767 | |
Junio C Hamano | dde843e | 2015-04-16 10:45:29 -0700 | [diff] [blame] | 768 | if (skip_utf8_bom(&buf, size)) |
| 769 | size -= buf - el->filebuf; |
| 770 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 771 | entry = buf; |
Carlos Martín Nieto | 245e1c1 | 2015-04-16 16:05:12 +0200 | [diff] [blame] | 772 | |
Nguyễn Thái Ngọc Duy | 45d76f1 | 2010-01-20 21:09:16 +0700 | [diff] [blame] | 773 | for (i = 0; i < size; i++) { |
| 774 | if (buf[i] == '\n') { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 775 | if (entry != buf + i && entry[0] != '#') { |
| 776 | buf[i - (i && buf[i-1] == '\r')] = 0; |
Nguyễn Thái Ngọc Duy | 7e2e4b3 | 2014-02-09 07:26:38 +0700 | [diff] [blame] | 777 | trim_trailing_spaces(entry); |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 778 | add_exclude(entry, base, baselen, el, lineno); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 779 | } |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 780 | lineno++; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 781 | entry = buf + i + 1; |
| 782 | } |
| 783 | } |
| 784 | return 0; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Nguyễn Thái Ngọc Duy | 55fe6f5 | 2015-03-08 17:12:24 +0700 | [diff] [blame] | 787 | int add_excludes_from_file_to_list(const char *fname, const char *base, |
| 788 | int baselen, struct exclude_list *el, |
| 789 | int check_index) |
| 790 | { |
| 791 | return add_excludes(fname, base, baselen, el, check_index, NULL); |
| 792 | } |
| 793 | |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 794 | struct exclude_list *add_exclude_list(struct dir_struct *dir, |
| 795 | int group_type, const char *src) |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 796 | { |
| 797 | struct exclude_list *el; |
| 798 | struct exclude_list_group *group; |
| 799 | |
| 800 | group = &dir->exclude_list_group[group_type]; |
| 801 | ALLOC_GROW(group->el, group->nr + 1, group->alloc); |
| 802 | el = &group->el[group->nr++]; |
| 803 | memset(el, 0, sizeof(*el)); |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 804 | el->src = src; |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 805 | return el; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Used to set up core.excludesfile and .git/info/exclude lists. |
| 810 | */ |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 811 | static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname, |
| 812 | struct sha1_stat *sha1_stat) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 813 | { |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 814 | struct exclude_list *el; |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 815 | /* |
| 816 | * catch setup_standard_excludes() that's called before |
| 817 | * dir->untracked is assigned. That function behaves |
| 818 | * differently when dir->untracked is non-NULL. |
| 819 | */ |
| 820 | if (!dir->untracked) |
| 821 | dir->unmanaged_exclude_files++; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 822 | el = add_exclude_list(dir, EXC_FILE, fname); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 823 | if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 824 | die("cannot use %s as an exclude file", fname); |
| 825 | } |
| 826 | |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 827 | void add_excludes_from_file(struct dir_struct *dir, const char *fname) |
| 828 | { |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 829 | dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */ |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 830 | add_excludes_from_file_1(dir, fname, NULL); |
| 831 | } |
| 832 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 833 | int match_basename(const char *basename, int basenamelen, |
| 834 | const char *pattern, int prefix, int patternlen, |
Saurav Sachidanand | f870899 | 2016-03-01 22:32:59 +0530 | [diff] [blame] | 835 | unsigned flags) |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 836 | { |
| 837 | if (prefix == patternlen) { |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 838 | if (patternlen == basenamelen && |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 839 | !fspathncmp(pattern, basename, basenamelen)) |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 840 | return 1; |
| 841 | } else if (flags & EXC_FLAG_ENDSWITH) { |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 842 | /* "*literal" matching against "fooliteral" */ |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 843 | if (patternlen - 1 <= basenamelen && |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 844 | !fspathncmp(pattern + 1, |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 845 | basename + basenamelen - (patternlen - 1), |
| 846 | patternlen - 1)) |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 847 | return 1; |
| 848 | } else { |
Junio C Hamano | 0b6e56d | 2013-03-28 17:47:28 -0400 | [diff] [blame] | 849 | if (fnmatch_icase_mem(pattern, patternlen, |
| 850 | basename, basenamelen, |
| 851 | 0) == 0) |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 852 | return 1; |
| 853 | } |
| 854 | return 0; |
| 855 | } |
| 856 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 857 | int match_pathname(const char *pathname, int pathlen, |
| 858 | const char *base, int baselen, |
| 859 | const char *pattern, int prefix, int patternlen, |
Saurav Sachidanand | f870899 | 2016-03-01 22:32:59 +0530 | [diff] [blame] | 860 | unsigned flags) |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 861 | { |
| 862 | const char *name; |
| 863 | int namelen; |
| 864 | |
| 865 | /* |
| 866 | * match with FNM_PATHNAME; the pattern has base implicitly |
| 867 | * in front of it. |
| 868 | */ |
| 869 | if (*pattern == '/') { |
| 870 | pattern++; |
Jeff King | 982ac87 | 2013-03-28 17:47:47 -0400 | [diff] [blame] | 871 | patternlen--; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 872 | prefix--; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * baselen does not count the trailing slash. base[] may or |
| 877 | * may not end with a trailing slash though. |
| 878 | */ |
| 879 | if (pathlen < baselen + 1 || |
| 880 | (baselen && pathname[baselen] != '/') || |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 881 | fspathncmp(pathname, base, baselen)) |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 882 | return 0; |
| 883 | |
| 884 | namelen = baselen ? pathlen - baselen - 1 : pathlen; |
| 885 | name = pathname + pathlen - namelen; |
| 886 | |
| 887 | if (prefix) { |
| 888 | /* |
| 889 | * if the non-wildcard part is longer than the |
| 890 | * remaining pathname, surely it cannot match. |
| 891 | */ |
| 892 | if (prefix > namelen) |
| 893 | return 0; |
| 894 | |
Nguyễn Thái Ngọc Duy | ba0897e | 2016-04-22 20:01:24 +0700 | [diff] [blame] | 895 | if (fspathncmp(pattern, name, prefix)) |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 896 | return 0; |
| 897 | pattern += prefix; |
Jeff King | ab3aebc | 2013-03-28 17:48:21 -0400 | [diff] [blame] | 898 | patternlen -= prefix; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 899 | name += prefix; |
| 900 | namelen -= prefix; |
Jeff King | ab3aebc | 2013-03-28 17:48:21 -0400 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * If the whole pattern did not have a wildcard, |
| 904 | * then our prefix match is all we need; we |
| 905 | * do not need to call fnmatch at all. |
| 906 | */ |
Junio C Hamano | 5cee349 | 2016-03-18 11:06:15 -0700 | [diff] [blame] | 907 | if (!patternlen && !namelen) |
Jeff King | ab3aebc | 2013-03-28 17:48:21 -0400 | [diff] [blame] | 908 | return 1; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 909 | } |
| 910 | |
Jeff King | ab3aebc | 2013-03-28 17:48:21 -0400 | [diff] [blame] | 911 | return fnmatch_icase_mem(pattern, patternlen, |
| 912 | name, namelen, |
Junio C Hamano | f30366b | 2013-04-03 09:34:04 -0700 | [diff] [blame] | 913 | WM_PATHNAME) == 0; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 914 | } |
| 915 | |
Adam Spiers | 578cd7c | 2012-12-27 02:32:26 +0000 | [diff] [blame] | 916 | /* |
| 917 | * Scan the given exclude list in reverse to see whether pathname |
| 918 | * should be ignored. The first match (i.e. the last on the list), if |
| 919 | * any, determines the fate. Returns the exclude_list element which |
| 920 | * matched, or NULL for undecided. |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 921 | */ |
Adam Spiers | 578cd7c | 2012-12-27 02:32:26 +0000 | [diff] [blame] | 922 | static struct exclude *last_exclude_matching_from_list(const char *pathname, |
| 923 | int pathlen, |
| 924 | const char *basename, |
| 925 | int *dtype, |
| 926 | struct exclude_list *el) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 927 | { |
Nguyễn Thái Ngọc Duy | e6efecc | 2015-09-21 16:56:14 +0700 | [diff] [blame] | 928 | struct exclude *exc = NULL; /* undecided */ |
Junio C Hamano | 5cee349 | 2016-03-18 11:06:15 -0700 | [diff] [blame] | 929 | int i; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 930 | |
Nguyễn Thái Ngọc Duy | 35a94d4 | 2012-05-26 19:31:12 +0700 | [diff] [blame] | 931 | if (!el->nr) |
Adam Spiers | 578cd7c | 2012-12-27 02:32:26 +0000 | [diff] [blame] | 932 | return NULL; /* undefined */ |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 933 | |
Nguyễn Thái Ngọc Duy | 35a94d4 | 2012-05-26 19:31:12 +0700 | [diff] [blame] | 934 | for (i = el->nr - 1; 0 <= i; i--) { |
| 935 | struct exclude *x = el->excludes[i]; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 936 | const char *exclude = x->pattern; |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 937 | int prefix = x->nowildcardlen; |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 938 | |
Nguyễn Thái Ngọc Duy | 35a94d4 | 2012-05-26 19:31:12 +0700 | [diff] [blame] | 939 | if (x->flags & EXC_FLAG_MUSTBEDIR) { |
| 940 | if (*dtype == DT_UNKNOWN) |
| 941 | *dtype = get_dtype(NULL, pathname, pathlen); |
| 942 | if (*dtype != DT_DIR) |
| 943 | continue; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 944 | } |
Nguyễn Thái Ngọc Duy | 35a94d4 | 2012-05-26 19:31:12 +0700 | [diff] [blame] | 945 | |
| 946 | if (x->flags & EXC_FLAG_NODIR) { |
Nguyễn Thái Ngọc Duy | 593cb88 | 2012-10-15 13:24:35 +0700 | [diff] [blame] | 947 | if (match_basename(basename, |
| 948 | pathlen - (basename - pathname), |
| 949 | exclude, prefix, x->patternlen, |
Nguyễn Thái Ngọc Duy | e6efecc | 2015-09-21 16:56:14 +0700 | [diff] [blame] | 950 | x->flags)) { |
| 951 | exc = x; |
| 952 | break; |
| 953 | } |
Nguyễn Thái Ngọc Duy | 35a94d4 | 2012-05-26 19:31:12 +0700 | [diff] [blame] | 954 | continue; |
| 955 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 956 | |
Nguyễn Thái Ngọc Duy | b559263 | 2012-10-15 13:24:37 +0700 | [diff] [blame] | 957 | assert(x->baselen == 0 || x->base[x->baselen - 1] == '/'); |
| 958 | if (match_pathname(pathname, pathlen, |
| 959 | x->base, x->baselen ? x->baselen - 1 : 0, |
Nguyễn Thái Ngọc Duy | e6efecc | 2015-09-21 16:56:14 +0700 | [diff] [blame] | 960 | exclude, prefix, x->patternlen, x->flags)) { |
| 961 | exc = x; |
| 962 | break; |
| 963 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 964 | } |
Nguyễn Thái Ngọc Duy | e6efecc | 2015-09-21 16:56:14 +0700 | [diff] [blame] | 965 | return exc; |
Adam Spiers | 578cd7c | 2012-12-27 02:32:26 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* |
| 969 | * Scan the list and let the last match determine the fate. |
| 970 | * Return 1 for exclude, 0 for include and -1 for undecided. |
| 971 | */ |
| 972 | int is_excluded_from_list(const char *pathname, |
| 973 | int pathlen, const char *basename, int *dtype, |
| 974 | struct exclude_list *el) |
| 975 | { |
| 976 | struct exclude *exclude; |
| 977 | exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); |
| 978 | if (exclude) |
| 979 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 980 | return -1; /* undecided */ |
| 981 | } |
| 982 | |
Karsten Blees | 46aa2f9 | 2013-04-15 21:11:02 +0200 | [diff] [blame] | 983 | static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, |
| 984 | const char *pathname, int pathlen, const char *basename, |
| 985 | int *dtype_p) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 986 | { |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 987 | int i, j; |
| 988 | struct exclude_list_group *group; |
Adam Spiers | f4cd69a | 2012-12-27 02:32:27 +0000 | [diff] [blame] | 989 | struct exclude *exclude; |
Adam Spiers | c082df2 | 2013-01-06 16:58:03 +0000 | [diff] [blame] | 990 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { |
| 991 | group = &dir->exclude_list_group[i]; |
| 992 | for (j = group->nr - 1; j >= 0; j--) { |
| 993 | exclude = last_exclude_matching_from_list( |
| 994 | pathname, pathlen, basename, dtype_p, |
| 995 | &group->el[j]); |
| 996 | if (exclude) |
| 997 | return exclude; |
| 998 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 999 | } |
Adam Spiers | f4cd69a | 2012-12-27 02:32:27 +0000 | [diff] [blame] | 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1004 | * Loads the per-directory exclude list for the substring of base |
| 1005 | * which has a char length of baselen. |
| 1006 | */ |
| 1007 | static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) |
| 1008 | { |
| 1009 | struct exclude_list_group *group; |
| 1010 | struct exclude_list *el; |
| 1011 | struct exclude_stack *stk = NULL; |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1012 | struct untracked_cache_dir *untracked; |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1013 | int current; |
| 1014 | |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1015 | group = &dir->exclude_list_group[EXC_DIRS]; |
| 1016 | |
Nguyễn Thái Ngọc Duy | d961baa | 2014-07-14 11:47:11 +0200 | [diff] [blame] | 1017 | /* |
| 1018 | * Pop the exclude lists from the EXCL_DIRS exclude_list_group |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1019 | * which originate from directories not in the prefix of the |
Nguyễn Thái Ngọc Duy | d961baa | 2014-07-14 11:47:11 +0200 | [diff] [blame] | 1020 | * path being checked. |
| 1021 | */ |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1022 | while ((stk = dir->exclude_stack) != NULL) { |
| 1023 | if (stk->baselen <= baselen && |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1024 | !strncmp(dir->basebuf.buf, base, stk->baselen)) |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1025 | break; |
| 1026 | el = &group->el[dir->exclude_stack->exclude_ix]; |
| 1027 | dir->exclude_stack = stk->prev; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1028 | dir->exclude = NULL; |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1029 | free((char *)el->src); /* see strbuf_detach() below */ |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1030 | clear_exclude_list(el); |
| 1031 | free(stk); |
| 1032 | group->nr--; |
| 1033 | } |
| 1034 | |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1035 | /* Skip traversing into sub directories if the parent is excluded */ |
| 1036 | if (dir->exclude) |
| 1037 | return; |
| 1038 | |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1039 | /* |
| 1040 | * Lazy initialization. All call sites currently just |
| 1041 | * memset(dir, 0, sizeof(*dir)) before use. Changing all of |
| 1042 | * them seems lots of work for little benefit. |
| 1043 | */ |
| 1044 | if (!dir->basebuf.buf) |
| 1045 | strbuf_init(&dir->basebuf, PATH_MAX); |
| 1046 | |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1047 | /* Read from the parent directories and push them down. */ |
| 1048 | current = stk ? stk->baselen : -1; |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1049 | strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1050 | if (dir->untracked) |
| 1051 | untracked = stk ? stk->ucd : dir->untracked->root; |
| 1052 | else |
| 1053 | untracked = NULL; |
| 1054 | |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1055 | while (current < baselen) { |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1056 | const char *cp; |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1057 | struct sha1_stat sha1_stat; |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1058 | |
Nguyễn Thái Ngọc Duy | 03e11a7 | 2014-10-21 18:38:06 +0700 | [diff] [blame] | 1059 | stk = xcalloc(1, sizeof(*stk)); |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1060 | if (current < 0) { |
| 1061 | cp = base; |
| 1062 | current = 0; |
Nguyễn Thái Ngọc Duy | d961baa | 2014-07-14 11:47:11 +0200 | [diff] [blame] | 1063 | } else { |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1064 | cp = strchr(base + current + 1, '/'); |
| 1065 | if (!cp) |
| 1066 | die("oops in prep_exclude"); |
| 1067 | cp++; |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1068 | untracked = |
| 1069 | lookup_untracked(dir->untracked, untracked, |
| 1070 | base + current, |
| 1071 | cp - base - current); |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1072 | } |
| 1073 | stk->prev = dir->exclude_stack; |
| 1074 | stk->baselen = cp - base; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1075 | stk->exclude_ix = group->nr; |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1076 | stk->ucd = untracked; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1077 | el = add_exclude_list(dir, EXC_DIRS, NULL); |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1078 | strbuf_add(&dir->basebuf, base + current, stk->baselen - current); |
| 1079 | assert(stk->baselen == dir->basebuf.len); |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1080 | |
| 1081 | /* Abort if the directory is excluded */ |
| 1082 | if (stk->baselen) { |
| 1083 | int dt = DT_DIR; |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1084 | dir->basebuf.buf[stk->baselen - 1] = 0; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1085 | dir->exclude = last_exclude_matching_from_lists(dir, |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1086 | dir->basebuf.buf, stk->baselen - 1, |
| 1087 | dir->basebuf.buf + current, &dt); |
| 1088 | dir->basebuf.buf[stk->baselen - 1] = '/'; |
Karsten Blees | c3c327d | 2013-05-29 22:32:36 +0200 | [diff] [blame] | 1089 | if (dir->exclude && |
| 1090 | dir->exclude->flags & EXC_FLAG_NEGATIVE) |
| 1091 | dir->exclude = NULL; |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1092 | if (dir->exclude) { |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1093 | dir->exclude_stack = stk; |
| 1094 | return; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1098 | /* Try to read per-directory file */ |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1099 | hashclr(sha1_stat.sha1); |
| 1100 | sha1_stat.valid = 0; |
Nguyễn Thái Ngọc Duy | 27b099a | 2015-03-08 17:12:31 +0700 | [diff] [blame] | 1101 | if (dir->exclude_per_dir && |
| 1102 | /* |
| 1103 | * If we know that no files have been added in |
| 1104 | * this directory (i.e. valid_cached_dir() has |
| 1105 | * been executed and set untracked->valid) .. |
| 1106 | */ |
| 1107 | (!untracked || !untracked->valid || |
| 1108 | /* |
| 1109 | * .. and .gitignore does not exist before |
David Turner | 7687252 | 2015-07-31 13:35:01 -0400 | [diff] [blame] | 1110 | * (i.e. null exclude_sha1). Then we can skip |
| 1111 | * loading .gitignore, which would result in |
| 1112 | * ENOENT anyway. |
Nguyễn Thái Ngọc Duy | 27b099a | 2015-03-08 17:12:31 +0700 | [diff] [blame] | 1113 | */ |
| 1114 | !is_null_sha1(untracked->exclude_sha1))) { |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1115 | /* |
| 1116 | * dir->basebuf gets reused by the traversal, but we |
| 1117 | * need fname to remain unchanged to ensure the src |
| 1118 | * member of each struct exclude correctly |
| 1119 | * back-references its source file. Other invocations |
| 1120 | * of add_exclude_list provide stable strings, so we |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1121 | * strbuf_detach() and free() here in the caller. |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1122 | */ |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1123 | struct strbuf sb = STRBUF_INIT; |
| 1124 | strbuf_addbuf(&sb, &dir->basebuf); |
| 1125 | strbuf_addstr(&sb, dir->exclude_per_dir); |
| 1126 | el->src = strbuf_detach(&sb, NULL); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1127 | add_excludes(el->src, el->src, stk->baselen, el, 1, |
| 1128 | untracked ? &sha1_stat : NULL); |
| 1129 | } |
Nguyễn Thái Ngọc Duy | 5ebf79a | 2015-03-08 17:12:27 +0700 | [diff] [blame] | 1130 | /* |
| 1131 | * NEEDSWORK: when untracked cache is enabled, prep_exclude() |
| 1132 | * will first be called in valid_cached_dir() then maybe many |
| 1133 | * times more in last_exclude_matching(). When the cache is |
| 1134 | * used, last_exclude_matching() will not be called and |
| 1135 | * reading .gitignore content will be a waste. |
| 1136 | * |
| 1137 | * So when it's called by valid_cached_dir() and we can get |
| 1138 | * .gitignore SHA-1 from the index (i.e. .gitignore is not |
| 1139 | * modified on work tree), we could delay reading the |
| 1140 | * .gitignore content until we absolutely need it in |
| 1141 | * last_exclude_matching(). Be careful about ignore rule |
| 1142 | * order, though, if you do that. |
| 1143 | */ |
| 1144 | if (untracked && |
| 1145 | hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) { |
| 1146 | invalidate_gitignore(dir->untracked, untracked); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1147 | hashcpy(untracked->exclude_sha1, sha1_stat.sha1); |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1148 | } |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1149 | dir->exclude_stack = stk; |
| 1150 | current = stk->baselen; |
| 1151 | } |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 1152 | strbuf_setlen(&dir->basebuf, baselen); |
Karsten Blees | 6cd5c58 | 2013-04-15 21:11:37 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* |
Adam Spiers | f4cd69a | 2012-12-27 02:32:27 +0000 | [diff] [blame] | 1156 | * Loads the exclude lists for the directory containing pathname, then |
| 1157 | * scans all exclude lists to determine whether pathname is excluded. |
Karsten Blees | 46aa2f9 | 2013-04-15 21:11:02 +0200 | [diff] [blame] | 1158 | * Returns the exclude_list element which matched, or NULL for |
| 1159 | * undecided. |
| 1160 | */ |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 1161 | struct exclude *last_exclude_matching(struct dir_struct *dir, |
Karsten Blees | 46aa2f9 | 2013-04-15 21:11:02 +0200 | [diff] [blame] | 1162 | const char *pathname, |
| 1163 | int *dtype_p) |
| 1164 | { |
| 1165 | int pathlen = strlen(pathname); |
| 1166 | const char *basename = strrchr(pathname, '/'); |
| 1167 | basename = (basename) ? basename+1 : pathname; |
| 1168 | |
| 1169 | prep_exclude(dir, pathname, basename-pathname); |
| 1170 | |
Karsten Blees | 95c6f27 | 2013-04-15 21:12:14 +0200 | [diff] [blame] | 1171 | if (dir->exclude) |
| 1172 | return dir->exclude; |
| 1173 | |
Karsten Blees | 46aa2f9 | 2013-04-15 21:11:02 +0200 | [diff] [blame] | 1174 | return last_exclude_matching_from_lists(dir, pathname, pathlen, |
| 1175 | basename, dtype_p); |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * Loads the exclude lists for the directory containing pathname, then |
| 1180 | * scans all exclude lists to determine whether pathname is excluded. |
Adam Spiers | f4cd69a | 2012-12-27 02:32:27 +0000 | [diff] [blame] | 1181 | * Returns 1 if true, otherwise 0. |
| 1182 | */ |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 1183 | int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p) |
Adam Spiers | f4cd69a | 2012-12-27 02:32:27 +0000 | [diff] [blame] | 1184 | { |
| 1185 | struct exclude *exclude = |
| 1186 | last_exclude_matching(dir, pathname, dtype_p); |
| 1187 | if (exclude) |
| 1188 | return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1189 | return 0; |
| 1190 | } |
| 1191 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 1192 | static struct dir_entry *dir_entry_new(const char *pathname, int len) |
| 1193 | { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1194 | struct dir_entry *ent; |
| 1195 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 1196 | FLEX_ALLOC_MEM(ent, name, pathname, len); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1197 | ent->len = len; |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 1198 | return ent; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Nanako Shiraishi | 159b321 | 2008-10-02 19:14:23 +0900 | [diff] [blame] | 1201 | static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 1202 | { |
Eric Sunshine | ebbd743 | 2013-09-17 03:06:15 -0400 | [diff] [blame] | 1203 | if (cache_file_exists(pathname, len, ignore_case)) |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 1204 | return NULL; |
| 1205 | |
Jeff King | 25fd2f7 | 2007-06-16 18:43:40 -0400 | [diff] [blame] | 1206 | ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
Jeff King | 6815e56 | 2007-06-11 09:39:44 -0400 | [diff] [blame] | 1207 | return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
| 1208 | } |
| 1209 | |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 1210 | struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len) |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 1211 | { |
Jeff King | 6e4f981 | 2009-05-30 17:54:18 -0400 | [diff] [blame] | 1212 | if (!cache_name_is_other(pathname, len)) |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 1213 | return NULL; |
| 1214 | |
Jeff King | 25fd2f7 | 2007-06-16 18:43:40 -0400 | [diff] [blame] | 1215 | ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
Jeff King | 2abd31b | 2007-06-11 09:39:50 -0400 | [diff] [blame] | 1216 | return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
| 1217 | } |
| 1218 | |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1219 | enum exist_status { |
| 1220 | index_nonexistent = 0, |
| 1221 | index_directory, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 1222 | index_gitdir |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | /* |
Junio C Hamano | 7126102 | 2013-08-15 12:08:45 -0700 | [diff] [blame] | 1226 | * Do not use the alphabetically sorted index to look up |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1227 | * the directory name; instead, use the case insensitive |
Eric Sunshine | ebbd743 | 2013-09-17 03:06:15 -0400 | [diff] [blame] | 1228 | * directory hash. |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1229 | */ |
| 1230 | static enum exist_status directory_exists_in_index_icase(const char *dirname, int len) |
| 1231 | { |
David Turner | 41284eb | 2015-10-21 13:54:11 -0400 | [diff] [blame] | 1232 | struct cache_entry *ce; |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1233 | |
David Turner | 41284eb | 2015-10-21 13:54:11 -0400 | [diff] [blame] | 1234 | if (cache_dir_exists(dirname, len)) |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1235 | return index_directory; |
| 1236 | |
David Turner | 41284eb | 2015-10-21 13:54:11 -0400 | [diff] [blame] | 1237 | ce = cache_file_exists(dirname, len, ignore_case); |
| 1238 | if (ce && S_ISGITLINK(ce->ce_mode)) |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1239 | return index_gitdir; |
| 1240 | |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1241 | return index_nonexistent; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1245 | * The index sorts alphabetically by entry name, which |
| 1246 | * means that a gitlink sorts as '\0' at the end, while |
| 1247 | * a directory (which is defined not as an entry, but as |
| 1248 | * the files it contains) will sort with the '/' at the |
| 1249 | * end. |
| 1250 | */ |
| 1251 | static enum exist_status directory_exists_in_index(const char *dirname, int len) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1252 | { |
Joshua Jensen | 5102c61 | 2010-10-03 09:56:43 +0000 | [diff] [blame] | 1253 | int pos; |
| 1254 | |
| 1255 | if (ignore_case) |
| 1256 | return directory_exists_in_index_icase(dirname, len); |
| 1257 | |
| 1258 | pos = cache_name_pos(dirname, len); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1259 | if (pos < 0) |
| 1260 | pos = -pos-1; |
| 1261 | while (pos < active_nr) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1262 | const struct cache_entry *ce = active_cache[pos++]; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1263 | unsigned char endchar; |
| 1264 | |
| 1265 | if (strncmp(ce->name, dirname, len)) |
| 1266 | break; |
| 1267 | endchar = ce->name[len]; |
| 1268 | if (endchar > '/') |
| 1269 | break; |
| 1270 | if (endchar == '/') |
| 1271 | return index_directory; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1272 | if (!endchar && S_ISGITLINK(ce->ce_mode)) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1273 | return index_gitdir; |
| 1274 | } |
| 1275 | return index_nonexistent; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * When we find a directory when traversing the filesystem, we |
| 1280 | * have three distinct cases: |
| 1281 | * |
| 1282 | * - ignore it |
| 1283 | * - see it as a directory |
| 1284 | * - recurse into it |
| 1285 | * |
| 1286 | * and which one we choose depends on a combination of existing |
| 1287 | * git index contents and the flags passed into the directory |
| 1288 | * traversal routine. |
| 1289 | * |
| 1290 | * Case 1: If we *already* have entries in the index under that |
Karsten Blees | 5bd8e2d | 2013-04-15 21:10:05 +0200 | [diff] [blame] | 1291 | * directory name, we always recurse into the directory to see |
| 1292 | * all the files. |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1293 | * |
| 1294 | * Case 2: If we *already* have that directory name as a gitlink, |
| 1295 | * we always continue to see it as a gitlink, regardless of whether |
| 1296 | * there is an actual git directory there or not (it might not |
| 1297 | * be checked out as a subproject!) |
| 1298 | * |
| 1299 | * Case 3: if we didn't have it in the index previously, we |
| 1300 | * have a few sub-cases: |
| 1301 | * |
| 1302 | * (a) if "show_other_directories" is true, we show it as |
| 1303 | * just a directory, unless "hide_empty_directories" is |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1304 | * also true, in which case we need to check if it contains any |
| 1305 | * untracked and / or ignored files. |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1306 | * (b) if it looks like a git directory, and we don't have |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 1307 | * 'no_gitlinks' set we treat it as a gitlink, and show it |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1308 | * as a directory. |
| 1309 | * (c) otherwise, we recurse into it. |
| 1310 | */ |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1311 | static enum path_treatment treat_directory(struct dir_struct *dir, |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1312 | struct untracked_cache_dir *untracked, |
David Turner | 2e5910f | 2015-08-19 20:01:25 +0700 | [diff] [blame] | 1313 | const char *dirname, int len, int baselen, int exclude, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1314 | const struct pathspec *pathspec) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1315 | { |
| 1316 | /* The "len-1" is to strip the final '/' */ |
| 1317 | switch (directory_exists_in_index(dirname, len-1)) { |
| 1318 | case index_directory: |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1319 | return path_recurse; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1320 | |
| 1321 | case index_gitdir: |
Junio C Hamano | 26c986e | 2013-07-01 14:00:32 -0700 | [diff] [blame] | 1322 | return path_none; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1323 | |
| 1324 | case index_nonexistent: |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 1325 | if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1326 | break; |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 1327 | if (!(dir->flags & DIR_NO_GITLINKS)) { |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1328 | unsigned char sha1[20]; |
| 1329 | if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1330 | return path_untracked; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1331 | } |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1332 | return path_recurse; |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* This is the "show_other_directories" case */ |
Antoine Pelisse | 721ac4e | 2012-12-30 15:39:00 +0100 | [diff] [blame] | 1336 | |
Karsten Blees | 184d2a8 | 2013-04-15 21:08:02 +0200 | [diff] [blame] | 1337 | if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1338 | return exclude ? path_excluded : path_untracked; |
| 1339 | |
David Turner | 2e5910f | 2015-08-19 20:01:25 +0700 | [diff] [blame] | 1340 | untracked = lookup_untracked(dir->untracked, untracked, |
| 1341 | dirname + baselen, len - baselen); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1342 | return read_directory_recursive(dir, dirname, len, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1343 | untracked, 1, pathspec); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1347 | * This is an inexact early pruning of any recursive directory |
| 1348 | * reading - if the path cannot possibly be in the pathspec, |
| 1349 | * return true, and we'll skip it early. |
| 1350 | */ |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1351 | static int simplify_away(const char *path, int pathlen, |
| 1352 | const struct pathspec *pathspec) |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1353 | { |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1354 | int i; |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1355 | |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1356 | if (!pathspec || !pathspec->nr) |
| 1357 | return 0; |
| 1358 | |
| 1359 | GUARD_PATHSPEC(pathspec, |
| 1360 | PATHSPEC_FROMTOP | |
| 1361 | PATHSPEC_MAXDEPTH | |
| 1362 | PATHSPEC_LITERAL | |
| 1363 | PATHSPEC_GLOB | |
| 1364 | PATHSPEC_ICASE | |
| 1365 | PATHSPEC_EXCLUDE); |
| 1366 | |
| 1367 | for (i = 0; i < pathspec->nr; i++) { |
| 1368 | const struct pathspec_item *item = &pathspec->items[i]; |
| 1369 | int len = item->nowildcard_len; |
| 1370 | |
| 1371 | if (len > pathlen) |
| 1372 | len = pathlen; |
| 1373 | if (!ps_strncmp(item, item->match, path, len)) |
| 1374 | return 0; |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1375 | } |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1376 | |
| 1377 | return 1; |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Jeff King | 29209cb | 2010-03-11 02:15:43 -0500 | [diff] [blame] | 1380 | /* |
| 1381 | * This function tells us whether an excluded path matches a |
| 1382 | * list of "interesting" pathspecs. That is, whether a path matched |
| 1383 | * by any of the pathspecs could possibly be ignored by excluding |
| 1384 | * the specified path. This can happen if: |
| 1385 | * |
| 1386 | * 1. the path is mentioned explicitly in the pathspec |
| 1387 | * |
| 1388 | * 2. the path is a directory prefix of some element in the |
| 1389 | * pathspec |
| 1390 | */ |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1391 | static int exclude_matches_pathspec(const char *path, int pathlen, |
| 1392 | const struct pathspec *pathspec) |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 1393 | { |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1394 | int i; |
| 1395 | |
| 1396 | if (!pathspec || !pathspec->nr) |
| 1397 | return 0; |
| 1398 | |
| 1399 | GUARD_PATHSPEC(pathspec, |
| 1400 | PATHSPEC_FROMTOP | |
| 1401 | PATHSPEC_MAXDEPTH | |
| 1402 | PATHSPEC_LITERAL | |
| 1403 | PATHSPEC_GLOB | |
| 1404 | PATHSPEC_ICASE | |
| 1405 | PATHSPEC_EXCLUDE); |
| 1406 | |
| 1407 | for (i = 0; i < pathspec->nr; i++) { |
| 1408 | const struct pathspec_item *item = &pathspec->items[i]; |
| 1409 | int len = item->nowildcard_len; |
| 1410 | |
| 1411 | if (len == pathlen && |
| 1412 | !ps_strncmp(item, item->match, path, pathlen)) |
| 1413 | return 1; |
| 1414 | if (len > pathlen && |
| 1415 | item->match[pathlen] == '/' && |
| 1416 | !ps_strncmp(item, item->match, path, pathlen)) |
| 1417 | return 1; |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 1418 | } |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 1422 | static int get_index_dtype(const char *path, int len) |
| 1423 | { |
| 1424 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1425 | const struct cache_entry *ce; |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 1426 | |
Eric Sunshine | ebbd743 | 2013-09-17 03:06:15 -0400 | [diff] [blame] | 1427 | ce = cache_file_exists(path, len, 0); |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 1428 | if (ce) { |
| 1429 | if (!ce_uptodate(ce)) |
| 1430 | return DT_UNKNOWN; |
| 1431 | if (S_ISGITLINK(ce->ce_mode)) |
| 1432 | return DT_DIR; |
| 1433 | /* |
| 1434 | * Nobody actually cares about the |
| 1435 | * difference between DT_LNK and DT_REG |
| 1436 | */ |
| 1437 | return DT_REG; |
| 1438 | } |
| 1439 | |
| 1440 | /* Try to look it up as a directory */ |
| 1441 | pos = cache_name_pos(path, len); |
| 1442 | if (pos >= 0) |
| 1443 | return DT_UNKNOWN; |
| 1444 | pos = -pos-1; |
| 1445 | while (pos < active_nr) { |
| 1446 | ce = active_cache[pos++]; |
| 1447 | if (strncmp(ce->name, path, len)) |
| 1448 | break; |
| 1449 | if (ce->name[len] > '/') |
| 1450 | break; |
| 1451 | if (ce->name[len] < '/') |
| 1452 | continue; |
| 1453 | if (!ce_uptodate(ce)) |
| 1454 | break; /* continue? */ |
| 1455 | return DT_DIR; |
| 1456 | } |
| 1457 | return DT_UNKNOWN; |
| 1458 | } |
| 1459 | |
Linus Torvalds | caa6b78 | 2009-07-08 19:31:49 -0700 | [diff] [blame] | 1460 | static int get_dtype(struct dirent *de, const char *path, int len) |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 1461 | { |
Junio C Hamano | 6831a88 | 2008-01-31 20:23:25 -0800 | [diff] [blame] | 1462 | int dtype = de ? DTYPE(de) : DT_UNKNOWN; |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 1463 | struct stat st; |
| 1464 | |
| 1465 | if (dtype != DT_UNKNOWN) |
| 1466 | return dtype; |
Linus Torvalds | 443e061 | 2009-07-09 13:14:28 -0700 | [diff] [blame] | 1467 | dtype = get_index_dtype(path, len); |
| 1468 | if (dtype != DT_UNKNOWN) |
| 1469 | return dtype; |
| 1470 | if (lstat(path, &st)) |
Linus Torvalds | 0713442 | 2007-10-19 10:59:22 -0700 | [diff] [blame] | 1471 | return dtype; |
| 1472 | if (S_ISREG(st.st_mode)) |
| 1473 | return DT_REG; |
| 1474 | if (S_ISDIR(st.st_mode)) |
| 1475 | return DT_DIR; |
| 1476 | if (S_ISLNK(st.st_mode)) |
| 1477 | return DT_LNK; |
| 1478 | return dtype; |
| 1479 | } |
| 1480 | |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1481 | static enum path_treatment treat_one_path(struct dir_struct *dir, |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1482 | struct untracked_cache_dir *untracked, |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1483 | struct strbuf *path, |
David Turner | 2e5910f | 2015-08-19 20:01:25 +0700 | [diff] [blame] | 1484 | int baselen, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1485 | const struct pathspec *pathspec, |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1486 | int dtype, struct dirent *de) |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1487 | { |
Karsten Blees | 8aaf8d7 | 2013-04-15 21:13:35 +0200 | [diff] [blame] | 1488 | int exclude; |
Eric Sunshine | ebbd743 | 2013-09-17 03:06:15 -0400 | [diff] [blame] | 1489 | int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case); |
Junio C Hamano | 2eac2a4 | 2013-08-15 12:13:46 -0700 | [diff] [blame] | 1490 | |
Karsten Blees | 8aaf8d7 | 2013-04-15 21:13:35 +0200 | [diff] [blame] | 1491 | if (dtype == DT_UNKNOWN) |
| 1492 | dtype = get_dtype(de, path->buf, path->len); |
| 1493 | |
| 1494 | /* Always exclude indexed files */ |
Junio C Hamano | 2eac2a4 | 2013-08-15 12:13:46 -0700 | [diff] [blame] | 1495 | if (dtype != DT_DIR && has_path_in_index) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1496 | return path_none; |
Karsten Blees | 8aaf8d7 | 2013-04-15 21:13:35 +0200 | [diff] [blame] | 1497 | |
Junio C Hamano | 2eac2a4 | 2013-08-15 12:13:46 -0700 | [diff] [blame] | 1498 | /* |
| 1499 | * When we are looking at a directory P in the working tree, |
| 1500 | * there are three cases: |
| 1501 | * |
| 1502 | * (1) P exists in the index. Everything inside the directory P in |
| 1503 | * the working tree needs to go when P is checked out from the |
| 1504 | * index. |
| 1505 | * |
| 1506 | * (2) P does not exist in the index, but there is P/Q in the index. |
| 1507 | * We know P will stay a directory when we check out the contents |
| 1508 | * of the index, but we do not know yet if there is a directory |
| 1509 | * P/Q in the working tree to be killed, so we need to recurse. |
| 1510 | * |
| 1511 | * (3) P does not exist in the index, and there is no P/Q in the index |
| 1512 | * to require P to be a directory, either. Only in this case, we |
| 1513 | * know that everything inside P will not be killed without |
| 1514 | * recursing. |
| 1515 | */ |
| 1516 | if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && |
| 1517 | (dtype == DT_DIR) && |
Eric Sunshine | de372b1 | 2013-09-17 03:06:17 -0400 | [diff] [blame] | 1518 | !has_path_in_index && |
| 1519 | (directory_exists_in_index(path->buf, path->len) == index_nonexistent)) |
| 1520 | return path_none; |
Karsten Blees | 8aaf8d7 | 2013-04-15 21:13:35 +0200 | [diff] [blame] | 1521 | |
| 1522 | exclude = is_excluded(dir, path->buf, &dtype); |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1523 | |
| 1524 | /* |
| 1525 | * Excluded? If we don't explicitly want to show |
| 1526 | * ignored files, ignore it |
| 1527 | */ |
Karsten Blees | 0aaf62b | 2013-04-15 21:15:03 +0200 | [diff] [blame] | 1528 | if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO))) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1529 | return path_excluded; |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1530 | |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1531 | switch (dtype) { |
| 1532 | default: |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1533 | return path_none; |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1534 | case DT_DIR: |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1535 | strbuf_addch(path, '/'); |
David Turner | 2e5910f | 2015-08-19 20:01:25 +0700 | [diff] [blame] | 1536 | return treat_directory(dir, untracked, path->buf, path->len, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1537 | baselen, exclude, pathspec); |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1538 | case DT_REG: |
| 1539 | case DT_LNK: |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1540 | return exclude ? path_excluded : path_untracked; |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1541 | } |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1544 | static enum path_treatment treat_path_fast(struct dir_struct *dir, |
| 1545 | struct untracked_cache_dir *untracked, |
| 1546 | struct cached_dir *cdir, |
| 1547 | struct strbuf *path, |
| 1548 | int baselen, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1549 | const struct pathspec *pathspec) |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1550 | { |
| 1551 | strbuf_setlen(path, baselen); |
| 1552 | if (!cdir->ucd) { |
| 1553 | strbuf_addstr(path, cdir->file); |
| 1554 | return path_untracked; |
| 1555 | } |
| 1556 | strbuf_addstr(path, cdir->ucd->name); |
| 1557 | /* treat_one_path() does this before it calls treat_directory() */ |
Jeff King | 00b6c17 | 2015-09-24 17:08:35 -0400 | [diff] [blame] | 1558 | strbuf_complete(path, '/'); |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1559 | if (cdir->ucd->check_only) |
| 1560 | /* |
| 1561 | * check_only is set as a result of treat_directory() getting |
| 1562 | * to its bottom. Verify again the same set of directories |
| 1563 | * with check_only set. |
| 1564 | */ |
| 1565 | return read_directory_recursive(dir, path->buf, path->len, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1566 | cdir->ucd, 1, pathspec); |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1567 | /* |
| 1568 | * We get path_recurse in the first run when |
| 1569 | * directory_exists_in_index() returns index_nonexistent. We |
| 1570 | * are sure that new changes in the index does not impact the |
| 1571 | * outcome. Return now. |
| 1572 | */ |
| 1573 | return path_recurse; |
| 1574 | } |
| 1575 | |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1576 | static enum path_treatment treat_path(struct dir_struct *dir, |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1577 | struct untracked_cache_dir *untracked, |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1578 | struct cached_dir *cdir, |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1579 | struct strbuf *path, |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1580 | int baselen, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1581 | const struct pathspec *pathspec) |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1582 | { |
| 1583 | int dtype; |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1584 | struct dirent *de = cdir->de; |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1585 | |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1586 | if (!de) |
| 1587 | return treat_path_fast(dir, untracked, cdir, path, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1588 | baselen, pathspec); |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1589 | if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git")) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1590 | return path_none; |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1591 | strbuf_setlen(path, baselen); |
| 1592 | strbuf_addstr(path, de->d_name); |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1593 | if (simplify_away(path->buf, path->len, pathspec)) |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1594 | return path_none; |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1595 | |
| 1596 | dtype = DTYPE(de); |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1597 | return treat_one_path(dir, untracked, path, baselen, pathspec, dtype, de); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | static void add_untracked(struct untracked_cache_dir *dir, const char *name) |
| 1601 | { |
| 1602 | if (!dir) |
| 1603 | return; |
| 1604 | ALLOC_GROW(dir->untracked, dir->untracked_nr + 1, |
| 1605 | dir->untracked_alloc); |
| 1606 | dir->untracked[dir->untracked_nr++] = xstrdup(name); |
Junio C Hamano | 16e2cfa | 2010-01-08 20:56:16 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1609 | static int valid_cached_dir(struct dir_struct *dir, |
| 1610 | struct untracked_cache_dir *untracked, |
| 1611 | struct strbuf *path, |
| 1612 | int check_only) |
| 1613 | { |
| 1614 | struct stat st; |
| 1615 | |
| 1616 | if (!untracked) |
| 1617 | return 0; |
| 1618 | |
| 1619 | if (stat(path->len ? path->buf : ".", &st)) { |
| 1620 | invalidate_directory(dir->untracked, untracked); |
| 1621 | memset(&untracked->stat_data, 0, sizeof(untracked->stat_data)); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | if (!untracked->valid || |
Nguyễn Thái Ngọc Duy | ed4efab | 2015-03-08 17:12:37 +0700 | [diff] [blame] | 1625 | match_stat_data_racy(&the_index, &untracked->stat_data, &st)) { |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1626 | if (untracked->valid) |
| 1627 | invalidate_directory(dir->untracked, untracked); |
| 1628 | fill_stat_data(&untracked->stat_data, &st); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | if (untracked->check_only != !!check_only) { |
| 1633 | invalidate_directory(dir->untracked, untracked); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * prep_exclude will be called eventually on this directory, |
| 1639 | * but it's called much later in last_exclude_matching(). We |
| 1640 | * need it now to determine the validity of the cache for this |
| 1641 | * path. The next calls will be nearly no-op, the way |
| 1642 | * prep_exclude() is designed. |
| 1643 | */ |
| 1644 | if (path->len && path->buf[path->len - 1] != '/') { |
| 1645 | strbuf_addch(path, '/'); |
| 1646 | prep_exclude(dir, path->buf, path->len); |
| 1647 | strbuf_setlen(path, path->len - 1); |
| 1648 | } else |
| 1649 | prep_exclude(dir, path->buf, path->len); |
| 1650 | |
| 1651 | /* hopefully prep_exclude() haven't invalidated this entry... */ |
| 1652 | return untracked->valid; |
| 1653 | } |
| 1654 | |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1655 | static int open_cached_dir(struct cached_dir *cdir, |
| 1656 | struct dir_struct *dir, |
| 1657 | struct untracked_cache_dir *untracked, |
| 1658 | struct strbuf *path, |
| 1659 | int check_only) |
| 1660 | { |
| 1661 | memset(cdir, 0, sizeof(*cdir)); |
| 1662 | cdir->untracked = untracked; |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1663 | if (valid_cached_dir(dir, untracked, path, check_only)) |
| 1664 | return 0; |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1665 | cdir->fdir = opendir(path->len ? path->buf : "."); |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1666 | if (dir->untracked) |
| 1667 | dir->untracked->dir_opened++; |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1668 | if (!cdir->fdir) |
| 1669 | return -1; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static int read_cached_dir(struct cached_dir *cdir) |
| 1674 | { |
| 1675 | if (cdir->fdir) { |
| 1676 | cdir->de = readdir(cdir->fdir); |
| 1677 | if (!cdir->de) |
| 1678 | return -1; |
| 1679 | return 0; |
| 1680 | } |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1681 | while (cdir->nr_dirs < cdir->untracked->dirs_nr) { |
| 1682 | struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs]; |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 1683 | if (!d->recurse) { |
| 1684 | cdir->nr_dirs++; |
| 1685 | continue; |
| 1686 | } |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1687 | cdir->ucd = d; |
| 1688 | cdir->nr_dirs++; |
| 1689 | return 0; |
| 1690 | } |
| 1691 | cdir->ucd = NULL; |
| 1692 | if (cdir->nr_files < cdir->untracked->untracked_nr) { |
| 1693 | struct untracked_cache_dir *d = cdir->untracked; |
| 1694 | cdir->file = d->untracked[cdir->nr_files++]; |
| 1695 | return 0; |
| 1696 | } |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1697 | return -1; |
| 1698 | } |
| 1699 | |
| 1700 | static void close_cached_dir(struct cached_dir *cdir) |
| 1701 | { |
| 1702 | if (cdir->fdir) |
| 1703 | closedir(cdir->fdir); |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1704 | /* |
| 1705 | * We have gone through this directory and found no untracked |
| 1706 | * entries. Mark it valid. |
| 1707 | */ |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 1708 | if (cdir->untracked) { |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1709 | cdir->untracked->valid = 1; |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 1710 | cdir->untracked->recurse = 1; |
| 1711 | } |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | /* |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1715 | * Read a directory tree. We currently ignore anything but |
| 1716 | * directories, regular files and symlinks. That's because git |
| 1717 | * doesn't handle them at all yet. Maybe that will change some |
| 1718 | * day. |
| 1719 | * |
| 1720 | * Also, we ignore the name ".git" (even if it is not a directory). |
| 1721 | * That likely will not change. |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1722 | * |
| 1723 | * Returns the most significant path_treatment value encountered in the scan. |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1724 | */ |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1725 | static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
Junio C Hamano | 53cc535 | 2010-01-08 19:14:07 -0800 | [diff] [blame] | 1726 | const char *base, int baselen, |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1727 | struct untracked_cache_dir *untracked, int check_only, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1728 | const struct pathspec *pathspec) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1729 | { |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1730 | struct cached_dir cdir; |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1731 | enum path_treatment state, subdir_state, dir_state = path_none; |
Junio C Hamano | bef3692 | 2012-05-08 09:43:40 -0700 | [diff] [blame] | 1732 | struct strbuf path = STRBUF_INIT; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1733 | |
Junio C Hamano | bef3692 | 2012-05-08 09:43:40 -0700 | [diff] [blame] | 1734 | strbuf_add(&path, base, baselen); |
Nguyễn Thái Ngọc Duy | 02cb675 | 2011-10-24 17:36:11 +1100 | [diff] [blame] | 1735 | |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1736 | if (open_cached_dir(&cdir, dir, untracked, &path, check_only)) |
René Scharfe | 1528d24 | 2012-05-11 16:53:07 +0200 | [diff] [blame] | 1737 | goto out; |
| 1738 | |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1739 | if (untracked) |
| 1740 | untracked->check_only = !!check_only; |
| 1741 | |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1742 | while (!read_cached_dir(&cdir)) { |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1743 | /* check how the file or directory should be treated */ |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1744 | state = treat_path(dir, untracked, &cdir, &path, |
| 1745 | baselen, pathspec); |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1746 | |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1747 | if (state > dir_state) |
| 1748 | dir_state = state; |
| 1749 | |
| 1750 | /* recurse into subdir if instructed by treat_path */ |
| 1751 | if (state == path_recurse) { |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1752 | struct untracked_cache_dir *ud; |
| 1753 | ud = lookup_untracked(dir->untracked, untracked, |
| 1754 | path.buf + baselen, |
| 1755 | path.len - baselen); |
| 1756 | subdir_state = |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1757 | read_directory_recursive(dir, path.buf, |
| 1758 | path.len, ud, |
| 1759 | check_only, pathspec); |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1760 | if (subdir_state > dir_state) |
| 1761 | dir_state = subdir_state; |
| 1762 | } |
| 1763 | |
| 1764 | if (check_only) { |
| 1765 | /* abort early if maximum state has been reached */ |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1766 | if (dir_state == path_untracked) { |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1767 | if (cdir.fdir) |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1768 | add_untracked(untracked, path.buf + baselen); |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1769 | break; |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1770 | } |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1771 | /* skip the dir_add_* part */ |
Nguyễn Thái Ngọc Duy | 02cb675 | 2011-10-24 17:36:11 +1100 | [diff] [blame] | 1772 | continue; |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* add the path to the appropriate result list */ |
| 1776 | switch (state) { |
| 1777 | case path_excluded: |
| 1778 | if (dir->flags & DIR_SHOW_IGNORED) |
| 1779 | dir_add_name(dir, path.buf, path.len); |
Karsten Blees | 0aaf62b | 2013-04-15 21:15:03 +0200 | [diff] [blame] | 1780 | else if ((dir->flags & DIR_SHOW_IGNORED_TOO) || |
| 1781 | ((dir->flags & DIR_COLLECT_IGNORED) && |
| 1782 | exclude_matches_pathspec(path.buf, path.len, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1783 | pathspec))) |
Karsten Blees | 0aaf62b | 2013-04-15 21:15:03 +0200 | [diff] [blame] | 1784 | dir_add_ignored(dir, path.buf, path.len); |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1785 | break; |
| 1786 | |
| 1787 | case path_untracked: |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1788 | if (dir->flags & DIR_SHOW_IGNORED) |
| 1789 | break; |
| 1790 | dir_add_name(dir, path.buf, path.len); |
Nguyễn Thái Ngọc Duy | 91a2288 | 2015-03-08 17:12:29 +0700 | [diff] [blame] | 1791 | if (cdir.fdir) |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 1792 | add_untracked(untracked, path.buf + baselen); |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1793 | break; |
| 1794 | |
| 1795 | default: |
Nguyễn Thái Ngọc Duy | 02cb675 | 2011-10-24 17:36:11 +1100 | [diff] [blame] | 1796 | break; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1797 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1798 | } |
Nguyễn Thái Ngọc Duy | cf7c614 | 2015-03-08 17:12:28 +0700 | [diff] [blame] | 1799 | close_cached_dir(&cdir); |
René Scharfe | 1528d24 | 2012-05-11 16:53:07 +0200 | [diff] [blame] | 1800 | out: |
Junio C Hamano | bef3692 | 2012-05-08 09:43:40 -0700 | [diff] [blame] | 1801 | strbuf_release(&path); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1802 | |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1803 | return dir_state; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | static int cmp_name(const void *p1, const void *p2) |
| 1807 | { |
| 1808 | const struct dir_entry *e1 = *(const struct dir_entry **)p1; |
| 1809 | const struct dir_entry *e2 = *(const struct dir_entry **)p2; |
| 1810 | |
Jeremiah Mahler | ccdd4a0 | 2014-06-19 19:06:44 -0700 | [diff] [blame] | 1811 | return name_compare(e1->name, e1->len, e2->name, e2->len); |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1814 | static int treat_leading_path(struct dir_struct *dir, |
| 1815 | const char *path, int len, |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1816 | const struct pathspec *pathspec) |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1817 | { |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1818 | struct strbuf sb = STRBUF_INIT; |
| 1819 | int baselen, rc = 0; |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1820 | const char *cp; |
Karsten Blees | be8a84c | 2013-04-15 21:09:25 +0200 | [diff] [blame] | 1821 | int old_flags = dir->flags; |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1822 | |
| 1823 | while (len && path[len - 1] == '/') |
| 1824 | len--; |
| 1825 | if (!len) |
| 1826 | return 1; |
| 1827 | baselen = 0; |
Karsten Blees | be8a84c | 2013-04-15 21:09:25 +0200 | [diff] [blame] | 1828 | dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES; |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1829 | while (1) { |
| 1830 | cp = path + baselen + !!baselen; |
| 1831 | cp = memchr(cp, '/', path + len - cp); |
| 1832 | if (!cp) |
| 1833 | baselen = len; |
| 1834 | else |
| 1835 | baselen = cp - path; |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1836 | strbuf_setlen(&sb, 0); |
| 1837 | strbuf_add(&sb, path, baselen); |
| 1838 | if (!is_directory(sb.buf)) |
| 1839 | break; |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1840 | if (simplify_away(sb.buf, sb.len, pathspec)) |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1841 | break; |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 1842 | if (treat_one_path(dir, NULL, &sb, baselen, pathspec, |
Karsten Blees | defd7c7 | 2013-04-15 21:14:22 +0200 | [diff] [blame] | 1843 | DT_DIR, NULL) == path_none) |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1844 | break; /* do not recurse into it */ |
| 1845 | if (len <= baselen) { |
| 1846 | rc = 1; |
| 1847 | break; /* finished checking */ |
| 1848 | } |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1849 | } |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1850 | strbuf_release(&sb); |
Karsten Blees | be8a84c | 2013-04-15 21:09:25 +0200 | [diff] [blame] | 1851 | dir->flags = old_flags; |
René Scharfe | 49dc2cc | 2012-05-01 13:25:24 +0200 | [diff] [blame] | 1852 | return rc; |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1855 | static const char *get_ident_string(void) |
| 1856 | { |
| 1857 | static struct strbuf sb = STRBUF_INIT; |
| 1858 | struct utsname uts; |
| 1859 | |
| 1860 | if (sb.len) |
| 1861 | return sb.buf; |
Charles Bailey | 100e433 | 2015-07-17 18:09:41 +0100 | [diff] [blame] | 1862 | if (uname(&uts) < 0) |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1863 | die_errno(_("failed to get kernel name and information")); |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1864 | strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(), |
| 1865 | uts.sysname); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1866 | return sb.buf; |
| 1867 | } |
| 1868 | |
| 1869 | static int ident_in_untracked(const struct untracked_cache *uc) |
| 1870 | { |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1871 | /* |
| 1872 | * Previous git versions may have saved many NUL separated |
| 1873 | * strings in the "ident" field, but it is insane to manage |
| 1874 | * many locations, so just take care of the first one. |
| 1875 | */ |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1876 | |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1877 | return !strcmp(uc->ident.buf, get_ident_string()); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1880 | static void set_untracked_ident(struct untracked_cache *uc) |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1881 | { |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1882 | strbuf_reset(&uc->ident); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1883 | strbuf_addstr(&uc->ident, get_ident_string()); |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1884 | |
| 1885 | /* |
| 1886 | * This strbuf used to contain a list of NUL separated |
| 1887 | * strings, so save NUL too for backward compatibility. |
| 1888 | */ |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1889 | strbuf_addch(&uc->ident, 0); |
| 1890 | } |
| 1891 | |
Christian Couder | 4a4ca47 | 2016-01-24 16:28:19 +0100 | [diff] [blame] | 1892 | static void new_untracked_cache(struct index_state *istate) |
| 1893 | { |
| 1894 | struct untracked_cache *uc = xcalloc(1, sizeof(*uc)); |
| 1895 | strbuf_init(&uc->ident, 100); |
| 1896 | uc->exclude_per_dir = ".gitignore"; |
| 1897 | /* should be the same flags used by git-status */ |
| 1898 | uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1899 | set_untracked_ident(uc); |
Christian Couder | 4a4ca47 | 2016-01-24 16:28:19 +0100 | [diff] [blame] | 1900 | istate->untracked = uc; |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1901 | istate->cache_changed |= UNTRACKED_CHANGED; |
Christian Couder | 4a4ca47 | 2016-01-24 16:28:19 +0100 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | void add_untracked_cache(struct index_state *istate) |
| 1905 | { |
| 1906 | if (!istate->untracked) { |
| 1907 | new_untracked_cache(istate); |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1908 | } else { |
| 1909 | if (!ident_in_untracked(istate->untracked)) { |
| 1910 | free_untracked_cache(istate->untracked); |
| 1911 | new_untracked_cache(istate); |
| 1912 | } |
| 1913 | } |
Christian Couder | 4a4ca47 | 2016-01-24 16:28:19 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
Christian Couder | 07b29bf | 2016-01-24 16:28:20 +0100 | [diff] [blame] | 1916 | void remove_untracked_cache(struct index_state *istate) |
| 1917 | { |
| 1918 | if (istate->untracked) { |
| 1919 | free_untracked_cache(istate->untracked); |
| 1920 | istate->untracked = NULL; |
| 1921 | istate->cache_changed |= UNTRACKED_CHANGED; |
| 1922 | } |
| 1923 | } |
| 1924 | |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 1925 | static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir, |
| 1926 | int base_len, |
| 1927 | const struct pathspec *pathspec) |
| 1928 | { |
| 1929 | struct untracked_cache_dir *root; |
| 1930 | |
Nguyễn Thái Ngọc Duy | 76e6b09 | 2015-03-08 17:12:40 +0700 | [diff] [blame] | 1931 | if (!dir->untracked || getenv("GIT_DISABLE_UNTRACKED_CACHE")) |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 1932 | return NULL; |
| 1933 | |
| 1934 | /* |
| 1935 | * We only support $GIT_DIR/info/exclude and core.excludesfile |
| 1936 | * as the global ignore rule files. Any other additions |
| 1937 | * (e.g. from command line) invalidate the cache. This |
| 1938 | * condition also catches running setup_standard_excludes() |
| 1939 | * before setting dir->untracked! |
| 1940 | */ |
| 1941 | if (dir->unmanaged_exclude_files) |
| 1942 | return NULL; |
| 1943 | |
| 1944 | /* |
| 1945 | * Optimize for the main use case only: whole-tree git |
| 1946 | * status. More work involved in treat_leading_path() if we |
| 1947 | * use cache on just a subset of the worktree. pathspec |
| 1948 | * support could make the matter even worse. |
| 1949 | */ |
| 1950 | if (base_len || (pathspec && pathspec->nr)) |
| 1951 | return NULL; |
| 1952 | |
| 1953 | /* Different set of flags may produce different results */ |
| 1954 | if (dir->flags != dir->untracked->dir_flags || |
| 1955 | /* |
| 1956 | * See treat_directory(), case index_nonexistent. Without |
| 1957 | * this flag, we may need to also cache .git file content |
| 1958 | * for the resolve_gitlink_ref() call, which we don't. |
| 1959 | */ |
| 1960 | !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) || |
| 1961 | /* We don't support collecting ignore files */ |
| 1962 | (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO | |
| 1963 | DIR_COLLECT_IGNORED))) |
| 1964 | return NULL; |
| 1965 | |
| 1966 | /* |
| 1967 | * If we use .gitignore in the cache and now you change it to |
| 1968 | * .gitexclude, everything will go wrong. |
| 1969 | */ |
| 1970 | if (dir->exclude_per_dir != dir->untracked->exclude_per_dir && |
| 1971 | strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir)) |
| 1972 | return NULL; |
| 1973 | |
| 1974 | /* |
| 1975 | * EXC_CMDL is not considered in the cache. If people set it, |
| 1976 | * skip the cache. |
| 1977 | */ |
| 1978 | if (dir->exclude_list_group[EXC_CMDL].nr) |
| 1979 | return NULL; |
| 1980 | |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1981 | if (!ident_in_untracked(dir->untracked)) { |
Christian Couder | 0e0f761 | 2016-01-24 16:28:21 +0100 | [diff] [blame] | 1982 | warning(_("Untracked cache is disabled on this system or location.")); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 1983 | return NULL; |
| 1984 | } |
| 1985 | |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 1986 | if (!dir->untracked->root) { |
| 1987 | const int len = sizeof(*dir->untracked->root); |
| 1988 | dir->untracked->root = xmalloc(len); |
| 1989 | memset(dir->untracked->root, 0, len); |
| 1990 | } |
| 1991 | |
| 1992 | /* Validate $GIT_DIR/info/exclude and core.excludesfile */ |
| 1993 | root = dir->untracked->root; |
| 1994 | if (hashcmp(dir->ss_info_exclude.sha1, |
| 1995 | dir->untracked->ss_info_exclude.sha1)) { |
| 1996 | invalidate_gitignore(dir->untracked, root); |
| 1997 | dir->untracked->ss_info_exclude = dir->ss_info_exclude; |
| 1998 | } |
| 1999 | if (hashcmp(dir->ss_excludes_file.sha1, |
| 2000 | dir->untracked->ss_excludes_file.sha1)) { |
| 2001 | invalidate_gitignore(dir->untracked, root); |
| 2002 | dir->untracked->ss_excludes_file = dir->ss_excludes_file; |
| 2003 | } |
Nguyễn Thái Ngọc Duy | 26cb018 | 2015-03-08 17:12:30 +0700 | [diff] [blame] | 2004 | |
| 2005 | /* Make sure this directory is not dropped out at saving phase */ |
| 2006 | root->recurse = 1; |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 2007 | return root; |
| 2008 | } |
| 2009 | |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 2010 | int read_directory(struct dir_struct *dir, const char *path, |
| 2011 | int len, const struct pathspec *pathspec) |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 2012 | { |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 2013 | struct untracked_cache_dir *untracked; |
Linus Torvalds | b4189aa | 2006-05-16 19:46:16 -0700 | [diff] [blame] | 2014 | |
Linus Torvalds | dba2e20 | 2009-07-08 19:24:39 -0700 | [diff] [blame] | 2015 | if (has_symlink_leading_path(path, len)) |
Junio C Hamano | 725b060 | 2008-08-04 00:52:37 -0700 | [diff] [blame] | 2016 | return dir->nr; |
| 2017 | |
Nguyễn Thái Ngọc Duy | ccad261 | 2015-03-08 17:12:26 +0700 | [diff] [blame] | 2018 | untracked = validate_untracked_cache(dir, len, pathspec); |
| 2019 | if (!untracked) |
| 2020 | /* |
| 2021 | * make sure untracked cache code path is disabled, |
| 2022 | * e.g. prep_exclude() |
| 2023 | */ |
| 2024 | dir->untracked = NULL; |
Brandon Williams | e1b8c7b | 2017-01-04 10:03:57 -0800 | [diff] [blame] | 2025 | if (!len || treat_leading_path(dir, path, len, pathspec)) |
| 2026 | read_directory_recursive(dir, path, len, untracked, 0, pathspec); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 2027 | QSORT(dir->entries, dir->nr, cmp_name); |
| 2028 | QSORT(dir->ignored, dir->ignored_nr, cmp_name); |
Nguyễn Thái Ngọc Duy | c9ccb5d | 2015-03-08 17:12:38 +0700 | [diff] [blame] | 2029 | if (dir->untracked) { |
| 2030 | static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS); |
| 2031 | trace_printf_key(&trace_untracked_stats, |
| 2032 | "node creation: %u\n" |
| 2033 | "gitignore invalidation: %u\n" |
| 2034 | "directory invalidation: %u\n" |
| 2035 | "opendir: %u\n", |
| 2036 | dir->untracked->dir_created, |
| 2037 | dir->untracked->gitignore_invalidated, |
| 2038 | dir->untracked->dir_invalidated, |
| 2039 | dir->untracked->dir_opened); |
Nguyễn Thái Ngọc Duy | 1bbb3db | 2015-03-08 17:12:39 +0700 | [diff] [blame] | 2040 | if (dir->untracked == the_index.untracked && |
| 2041 | (dir->untracked->dir_opened || |
| 2042 | dir->untracked->gitignore_invalidated || |
| 2043 | dir->untracked->dir_invalidated)) |
| 2044 | the_index.cache_changed |= UNTRACKED_CHANGED; |
| 2045 | if (dir->untracked != the_index.untracked) { |
| 2046 | free(dir->untracked); |
| 2047 | dir->untracked = NULL; |
| 2048 | } |
Nguyễn Thái Ngọc Duy | c9ccb5d | 2015-03-08 17:12:38 +0700 | [diff] [blame] | 2049 | } |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 2050 | return dir->nr; |
| 2051 | } |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 2052 | |
Junio C Hamano | 686a4a0 | 2007-11-29 01:11:46 -0800 | [diff] [blame] | 2053 | int file_exists(const char *f) |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 2054 | { |
Junio C Hamano | 686a4a0 | 2007-11-29 01:11:46 -0800 | [diff] [blame] | 2055 | struct stat sb; |
Junio C Hamano | a50f9fc5 | 2007-11-18 01:58:16 -0800 | [diff] [blame] | 2056 | return lstat(f, &sb) == 0; |
Jeff King | c91f0d9 | 2006-09-08 04:05:34 -0400 | [diff] [blame] | 2057 | } |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2058 | |
Johannes Schindelin | 63ec5e1 | 2015-09-28 18:12:18 +0200 | [diff] [blame] | 2059 | static int cmp_icase(char a, char b) |
| 2060 | { |
| 2061 | if (a == b) |
| 2062 | return 0; |
| 2063 | if (ignore_case) |
| 2064 | return toupper(a) - toupper(b); |
| 2065 | return a - b; |
| 2066 | } |
| 2067 | |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2068 | /* |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2069 | * Given two normalized paths (a trailing slash is ok), if subdir is |
| 2070 | * outside dir, return -1. Otherwise return the offset in subdir that |
| 2071 | * can be used as relative path to dir. |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2072 | */ |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2073 | int dir_inside_of(const char *subdir, const char *dir) |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2074 | { |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2075 | int offset = 0; |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2076 | |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2077 | assert(dir && subdir && *dir && *subdir); |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2078 | |
Johannes Schindelin | 63ec5e1 | 2015-09-28 18:12:18 +0200 | [diff] [blame] | 2079 | while (*dir && *subdir && !cmp_icase(*dir, *subdir)) { |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2080 | dir++; |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2081 | subdir++; |
| 2082 | offset++; |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2083 | } |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 2084 | |
| 2085 | /* hel[p]/me vs hel[l]/yeah */ |
| 2086 | if (*dir && *subdir) |
| 2087 | return -1; |
| 2088 | |
| 2089 | if (!*subdir) |
| 2090 | return !*dir ? offset : -1; /* same dir */ |
| 2091 | |
| 2092 | /* foo/[b]ar vs foo/[] */ |
| 2093 | if (is_dir_sep(dir[-1])) |
| 2094 | return is_dir_sep(subdir[-1]) ? offset : -1; |
| 2095 | |
| 2096 | /* foo[/]bar vs foo[] */ |
| 2097 | return is_dir_sep(*subdir) ? offset + 1 : -1; |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | int is_inside_dir(const char *dir) |
| 2101 | { |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 2102 | char *cwd; |
| 2103 | int rc; |
| 2104 | |
Nguyễn Thái Ngọc Duy | b892913 | 2011-03-26 16:04:25 +0700 | [diff] [blame] | 2105 | if (!dir) |
| 2106 | return 0; |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 2107 | |
| 2108 | cwd = xgetcwd(); |
| 2109 | rc = (dir_inside_of(cwd, dir) >= 0); |
| 2110 | free(cwd); |
| 2111 | return rc; |
Johannes Schindelin | e663674 | 2007-08-01 01:29:17 +0100 | [diff] [blame] | 2112 | } |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2113 | |
Alexander Potashev | 55892d2 | 2009-01-11 15:19:12 +0300 | [diff] [blame] | 2114 | int is_empty_dir(const char *path) |
| 2115 | { |
| 2116 | DIR *dir = opendir(path); |
| 2117 | struct dirent *e; |
| 2118 | int ret = 1; |
| 2119 | |
| 2120 | if (!dir) |
| 2121 | return 0; |
| 2122 | |
| 2123 | while ((e = readdir(dir)) != NULL) |
| 2124 | if (!is_dot_or_dotdot(e->d_name)) { |
| 2125 | ret = 0; |
| 2126 | break; |
| 2127 | } |
| 2128 | |
| 2129 | closedir(dir); |
| 2130 | return ret; |
| 2131 | } |
| 2132 | |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2133 | static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2134 | { |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2135 | DIR *dir; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2136 | struct dirent *e; |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2137 | int ret = 0, original_len = path->len, len, kept_down = 0; |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2138 | int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); |
Junio C Hamano | c844a80 | 2012-03-15 15:58:54 +0100 | [diff] [blame] | 2139 | int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL); |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2140 | unsigned char submodule_head[20]; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2141 | |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2142 | if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2143 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2144 | /* Do not descend and nuke a nested git work tree. */ |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2145 | if (kept_up) |
| 2146 | *kept_up = 1; |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2147 | return 0; |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2148 | } |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2149 | |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2150 | flag &= ~REMOVE_DIR_KEEP_TOPLEVEL; |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 2151 | dir = opendir(path->buf); |
Junio C Hamano | c844a80 | 2012-03-15 15:58:54 +0100 | [diff] [blame] | 2152 | if (!dir) { |
Michael Haggerty | 863808c | 2014-01-18 23:48:57 +0100 | [diff] [blame] | 2153 | if (errno == ENOENT) |
| 2154 | return keep_toplevel ? -1 : 0; |
| 2155 | else if (errno == EACCES && !keep_toplevel) |
Michael Haggerty | ecb2c28 | 2014-01-18 23:48:56 +0100 | [diff] [blame] | 2156 | /* |
| 2157 | * An empty dir could be removable even if it |
| 2158 | * is unreadable: |
| 2159 | */ |
Junio C Hamano | c844a80 | 2012-03-15 15:58:54 +0100 | [diff] [blame] | 2160 | return rmdir(path->buf); |
| 2161 | else |
| 2162 | return -1; |
| 2163 | } |
Jeff King | 00b6c17 | 2015-09-24 17:08:35 -0400 | [diff] [blame] | 2164 | strbuf_complete(path, '/'); |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2165 | |
| 2166 | len = path->len; |
| 2167 | while ((e = readdir(dir)) != NULL) { |
| 2168 | struct stat st; |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 2169 | if (is_dot_or_dotdot(e->d_name)) |
| 2170 | continue; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2171 | |
| 2172 | strbuf_setlen(path, len); |
| 2173 | strbuf_addstr(path, e->d_name); |
Michael Haggerty | 863808c | 2014-01-18 23:48:57 +0100 | [diff] [blame] | 2174 | if (lstat(path->buf, &st)) { |
| 2175 | if (errno == ENOENT) |
| 2176 | /* |
| 2177 | * file disappeared, which is what we |
| 2178 | * wanted anyway |
| 2179 | */ |
| 2180 | continue; |
| 2181 | /* fall thru */ |
| 2182 | } else if (S_ISDIR(st.st_mode)) { |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2183 | if (!remove_dir_recurse(path, flag, &kept_down)) |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2184 | continue; /* happy */ |
Michael Haggerty | 863808c | 2014-01-18 23:48:57 +0100 | [diff] [blame] | 2185 | } else if (!only_empty && |
| 2186 | (!unlink(path->buf) || errno == ENOENT)) { |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2187 | continue; /* happy, too */ |
Michael Haggerty | 863808c | 2014-01-18 23:48:57 +0100 | [diff] [blame] | 2188 | } |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2189 | |
| 2190 | /* path too long, stat fails, or non-directory still exists */ |
| 2191 | ret = -1; |
| 2192 | break; |
| 2193 | } |
| 2194 | closedir(dir); |
| 2195 | |
| 2196 | strbuf_setlen(path, original_len); |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2197 | if (!ret && !keep_toplevel && !kept_down) |
Michael Haggerty | 863808c | 2014-01-18 23:48:57 +0100 | [diff] [blame] | 2198 | ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2199 | else if (kept_up) |
| 2200 | /* |
| 2201 | * report the uplevel that it is not an error that we |
| 2202 | * did not rmdir() our directory. |
| 2203 | */ |
| 2204 | *kept_up = !ret; |
Johannes Schindelin | 7155b72 | 2007-09-28 16:28:54 +0100 | [diff] [blame] | 2205 | return ret; |
| 2206 | } |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 2207 | |
Junio C Hamano | ae2f203 | 2012-03-15 01:04:12 -0700 | [diff] [blame] | 2208 | int remove_dir_recursively(struct strbuf *path, int flag) |
| 2209 | { |
| 2210 | return remove_dir_recurse(path, flag, NULL); |
| 2211 | } |
| 2212 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2213 | static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude") |
| 2214 | |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 2215 | void setup_standard_excludes(struct dir_struct *dir) |
| 2216 | { |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 2217 | dir->exclude_per_dir = ".gitignore"; |
Junio C Hamano | 099d2d8 | 2015-04-22 14:31:49 -0700 | [diff] [blame] | 2218 | |
| 2219 | /* core.excludefile defaulting to $XDG_HOME/git/ignore */ |
Paul Tan | 2845ce7 | 2015-05-06 16:01:00 +0800 | [diff] [blame] | 2220 | if (!excludes_file) |
| 2221 | excludes_file = xdg_config_home("ignore"); |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 2222 | if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) |
Nguyễn Thái Ngọc Duy | 0dcb8d7 | 2015-03-08 17:12:25 +0700 | [diff] [blame] | 2223 | add_excludes_from_file_1(dir, excludes_file, |
| 2224 | dir->untracked ? &dir->ss_excludes_file : NULL); |
Junio C Hamano | 099d2d8 | 2015-04-22 14:31:49 -0700 | [diff] [blame] | 2225 | |
| 2226 | /* per repository user preference */ |
Jeff King | f0056f6 | 2016-10-20 02:16:41 -0400 | [diff] [blame] | 2227 | if (startup_info->have_repository) { |
| 2228 | const char *path = git_path_info_exclude(); |
| 2229 | if (!access_or_warn(path, R_OK, 0)) |
| 2230 | add_excludes_from_file_1(dir, path, |
| 2231 | dir->untracked ? &dir->ss_info_exclude : NULL); |
| 2232 | } |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 2233 | } |
Alex Riesen | 4a92d1b | 2008-09-27 00:56:46 +0200 | [diff] [blame] | 2234 | |
| 2235 | int remove_path(const char *name) |
| 2236 | { |
| 2237 | char *slash; |
| 2238 | |
Jeff King | 9a6728d | 2013-04-04 15:03:35 -0400 | [diff] [blame] | 2239 | if (unlink(name) && errno != ENOENT && errno != ENOTDIR) |
Alex Riesen | 4a92d1b | 2008-09-27 00:56:46 +0200 | [diff] [blame] | 2240 | return -1; |
| 2241 | |
| 2242 | slash = strrchr(name, '/'); |
| 2243 | if (slash) { |
| 2244 | char *dirs = xstrdup(name); |
| 2245 | slash = dirs + (slash - name); |
| 2246 | do { |
| 2247 | *slash = '\0'; |
Jeff King | 3fc0d13 | 2010-02-19 00:57:21 -0500 | [diff] [blame] | 2248 | } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); |
Alex Riesen | 4a92d1b | 2008-09-27 00:56:46 +0200 | [diff] [blame] | 2249 | free(dirs); |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
Adam Spiers | 270be81 | 2013-01-06 16:58:05 +0000 | [diff] [blame] | 2254 | /* |
| 2255 | * Frees memory within dir which was allocated for exclude lists and |
| 2256 | * the exclude_stack. Does not free dir itself. |
| 2257 | */ |
| 2258 | void clear_directory(struct dir_struct *dir) |
| 2259 | { |
| 2260 | int i, j; |
| 2261 | struct exclude_list_group *group; |
| 2262 | struct exclude_list *el; |
| 2263 | struct exclude_stack *stk; |
| 2264 | |
| 2265 | for (i = EXC_CMDL; i <= EXC_FILE; i++) { |
| 2266 | group = &dir->exclude_list_group[i]; |
| 2267 | for (j = 0; j < group->nr; j++) { |
| 2268 | el = &group->el[j]; |
| 2269 | if (i == EXC_DIRS) |
| 2270 | free((char *)el->src); |
| 2271 | clear_exclude_list(el); |
| 2272 | } |
| 2273 | free(group->el); |
| 2274 | } |
| 2275 | |
| 2276 | stk = dir->exclude_stack; |
| 2277 | while (stk) { |
| 2278 | struct exclude_stack *prev = stk->prev; |
| 2279 | free(stk); |
| 2280 | stk = prev; |
| 2281 | } |
Nguyễn Thái Ngọc Duy | aceb942 | 2014-07-14 11:50:22 +0200 | [diff] [blame] | 2282 | strbuf_release(&dir->basebuf); |
Adam Spiers | 270be81 | 2013-01-06 16:58:05 +0000 | [diff] [blame] | 2283 | } |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 2284 | |
| 2285 | struct ondisk_untracked_cache { |
| 2286 | struct stat_data info_exclude_stat; |
| 2287 | struct stat_data excludes_file_stat; |
| 2288 | uint32_t dir_flags; |
| 2289 | unsigned char info_exclude_sha1[20]; |
| 2290 | unsigned char excludes_file_sha1[20]; |
| 2291 | char exclude_per_dir[FLEX_ARRAY]; |
| 2292 | }; |
| 2293 | |
| 2294 | #define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1) |
| 2295 | |
| 2296 | struct write_data { |
| 2297 | int index; /* number of written untracked_cache_dir */ |
| 2298 | struct ewah_bitmap *check_only; /* from untracked_cache_dir */ |
| 2299 | struct ewah_bitmap *valid; /* from untracked_cache_dir */ |
| 2300 | struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */ |
| 2301 | struct strbuf out; |
| 2302 | struct strbuf sb_stat; |
| 2303 | struct strbuf sb_sha1; |
| 2304 | }; |
| 2305 | |
| 2306 | static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from) |
| 2307 | { |
| 2308 | to->sd_ctime.sec = htonl(from->sd_ctime.sec); |
| 2309 | to->sd_ctime.nsec = htonl(from->sd_ctime.nsec); |
| 2310 | to->sd_mtime.sec = htonl(from->sd_mtime.sec); |
| 2311 | to->sd_mtime.nsec = htonl(from->sd_mtime.nsec); |
| 2312 | to->sd_dev = htonl(from->sd_dev); |
| 2313 | to->sd_ino = htonl(from->sd_ino); |
| 2314 | to->sd_uid = htonl(from->sd_uid); |
| 2315 | to->sd_gid = htonl(from->sd_gid); |
| 2316 | to->sd_size = htonl(from->sd_size); |
| 2317 | } |
| 2318 | |
| 2319 | static void write_one_dir(struct untracked_cache_dir *untracked, |
| 2320 | struct write_data *wd) |
| 2321 | { |
| 2322 | struct stat_data stat_data; |
| 2323 | struct strbuf *out = &wd->out; |
| 2324 | unsigned char intbuf[16]; |
| 2325 | unsigned int intlen, value; |
| 2326 | int i = wd->index++; |
| 2327 | |
| 2328 | /* |
| 2329 | * untracked_nr should be reset whenever valid is clear, but |
| 2330 | * for safety.. |
| 2331 | */ |
| 2332 | if (!untracked->valid) { |
| 2333 | untracked->untracked_nr = 0; |
| 2334 | untracked->check_only = 0; |
| 2335 | } |
| 2336 | |
| 2337 | if (untracked->check_only) |
| 2338 | ewah_set(wd->check_only, i); |
| 2339 | if (untracked->valid) { |
| 2340 | ewah_set(wd->valid, i); |
| 2341 | stat_data_to_disk(&stat_data, &untracked->stat_data); |
| 2342 | strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data)); |
| 2343 | } |
| 2344 | if (!is_null_sha1(untracked->exclude_sha1)) { |
| 2345 | ewah_set(wd->sha1_valid, i); |
| 2346 | strbuf_add(&wd->sb_sha1, untracked->exclude_sha1, 20); |
| 2347 | } |
| 2348 | |
| 2349 | intlen = encode_varint(untracked->untracked_nr, intbuf); |
| 2350 | strbuf_add(out, intbuf, intlen); |
| 2351 | |
| 2352 | /* skip non-recurse directories */ |
| 2353 | for (i = 0, value = 0; i < untracked->dirs_nr; i++) |
| 2354 | if (untracked->dirs[i]->recurse) |
| 2355 | value++; |
| 2356 | intlen = encode_varint(value, intbuf); |
| 2357 | strbuf_add(out, intbuf, intlen); |
| 2358 | |
| 2359 | strbuf_add(out, untracked->name, strlen(untracked->name) + 1); |
| 2360 | |
| 2361 | for (i = 0; i < untracked->untracked_nr; i++) |
| 2362 | strbuf_add(out, untracked->untracked[i], |
| 2363 | strlen(untracked->untracked[i]) + 1); |
| 2364 | |
| 2365 | for (i = 0; i < untracked->dirs_nr; i++) |
| 2366 | if (untracked->dirs[i]->recurse) |
| 2367 | write_one_dir(untracked->dirs[i], wd); |
| 2368 | } |
| 2369 | |
| 2370 | void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked) |
| 2371 | { |
| 2372 | struct ondisk_untracked_cache *ouc; |
| 2373 | struct write_data wd; |
| 2374 | unsigned char varbuf[16]; |
Jeff King | e0b8373 | 2016-02-22 17:44:42 -0500 | [diff] [blame] | 2375 | int varint_len; |
| 2376 | size_t len = strlen(untracked->exclude_per_dir); |
| 2377 | |
| 2378 | FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len); |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 2379 | stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat); |
| 2380 | stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat); |
| 2381 | hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1); |
| 2382 | hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1); |
| 2383 | ouc->dir_flags = htonl(untracked->dir_flags); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 2384 | |
| 2385 | varint_len = encode_varint(untracked->ident.len, varbuf); |
| 2386 | strbuf_add(out, varbuf, varint_len); |
René Scharfe | 8109984 | 2016-07-19 20:36:29 +0200 | [diff] [blame] | 2387 | strbuf_addbuf(out, &untracked->ident); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 2388 | |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 2389 | strbuf_add(out, ouc, ouc_size(len)); |
| 2390 | free(ouc); |
| 2391 | ouc = NULL; |
| 2392 | |
| 2393 | if (!untracked->root) { |
| 2394 | varint_len = encode_varint(0, varbuf); |
| 2395 | strbuf_add(out, varbuf, varint_len); |
| 2396 | return; |
| 2397 | } |
| 2398 | |
| 2399 | wd.index = 0; |
| 2400 | wd.check_only = ewah_new(); |
| 2401 | wd.valid = ewah_new(); |
| 2402 | wd.sha1_valid = ewah_new(); |
| 2403 | strbuf_init(&wd.out, 1024); |
| 2404 | strbuf_init(&wd.sb_stat, 1024); |
| 2405 | strbuf_init(&wd.sb_sha1, 1024); |
| 2406 | write_one_dir(untracked->root, &wd); |
| 2407 | |
| 2408 | varint_len = encode_varint(wd.index, varbuf); |
| 2409 | strbuf_add(out, varbuf, varint_len); |
| 2410 | strbuf_addbuf(out, &wd.out); |
| 2411 | ewah_serialize_strbuf(wd.valid, out); |
| 2412 | ewah_serialize_strbuf(wd.check_only, out); |
| 2413 | ewah_serialize_strbuf(wd.sha1_valid, out); |
| 2414 | strbuf_addbuf(out, &wd.sb_stat); |
| 2415 | strbuf_addbuf(out, &wd.sb_sha1); |
| 2416 | strbuf_addch(out, '\0'); /* safe guard for string lists */ |
| 2417 | |
| 2418 | ewah_free(wd.valid); |
| 2419 | ewah_free(wd.check_only); |
| 2420 | ewah_free(wd.sha1_valid); |
| 2421 | strbuf_release(&wd.out); |
| 2422 | strbuf_release(&wd.sb_stat); |
| 2423 | strbuf_release(&wd.sb_sha1); |
| 2424 | } |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2425 | |
| 2426 | static void free_untracked(struct untracked_cache_dir *ucd) |
| 2427 | { |
| 2428 | int i; |
| 2429 | if (!ucd) |
| 2430 | return; |
| 2431 | for (i = 0; i < ucd->dirs_nr; i++) |
| 2432 | free_untracked(ucd->dirs[i]); |
| 2433 | for (i = 0; i < ucd->untracked_nr; i++) |
| 2434 | free(ucd->untracked[i]); |
| 2435 | free(ucd->untracked); |
| 2436 | free(ucd->dirs); |
| 2437 | free(ucd); |
| 2438 | } |
| 2439 | |
| 2440 | void free_untracked_cache(struct untracked_cache *uc) |
| 2441 | { |
| 2442 | if (uc) |
| 2443 | free_untracked(uc->root); |
| 2444 | free(uc); |
| 2445 | } |
| 2446 | |
| 2447 | struct read_data { |
| 2448 | int index; |
| 2449 | struct untracked_cache_dir **ucd; |
| 2450 | struct ewah_bitmap *check_only; |
| 2451 | struct ewah_bitmap *valid; |
| 2452 | struct ewah_bitmap *sha1_valid; |
| 2453 | const unsigned char *data; |
| 2454 | const unsigned char *end; |
| 2455 | }; |
| 2456 | |
| 2457 | static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from) |
| 2458 | { |
| 2459 | to->sd_ctime.sec = get_be32(&from->sd_ctime.sec); |
| 2460 | to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec); |
| 2461 | to->sd_mtime.sec = get_be32(&from->sd_mtime.sec); |
| 2462 | to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec); |
| 2463 | to->sd_dev = get_be32(&from->sd_dev); |
| 2464 | to->sd_ino = get_be32(&from->sd_ino); |
| 2465 | to->sd_uid = get_be32(&from->sd_uid); |
| 2466 | to->sd_gid = get_be32(&from->sd_gid); |
| 2467 | to->sd_size = get_be32(&from->sd_size); |
| 2468 | } |
| 2469 | |
| 2470 | static int read_one_dir(struct untracked_cache_dir **untracked_, |
| 2471 | struct read_data *rd) |
| 2472 | { |
| 2473 | struct untracked_cache_dir ud, *untracked; |
| 2474 | const unsigned char *next, *data = rd->data, *end = rd->end; |
| 2475 | unsigned int value; |
| 2476 | int i, len; |
| 2477 | |
| 2478 | memset(&ud, 0, sizeof(ud)); |
| 2479 | |
| 2480 | next = data; |
| 2481 | value = decode_varint(&next); |
| 2482 | if (next > end) |
| 2483 | return -1; |
| 2484 | ud.recurse = 1; |
| 2485 | ud.untracked_alloc = value; |
| 2486 | ud.untracked_nr = value; |
| 2487 | if (ud.untracked_nr) |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 2488 | ALLOC_ARRAY(ud.untracked, ud.untracked_nr); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2489 | data = next; |
| 2490 | |
| 2491 | next = data; |
| 2492 | ud.dirs_alloc = ud.dirs_nr = decode_varint(&next); |
| 2493 | if (next > end) |
| 2494 | return -1; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 2495 | ALLOC_ARRAY(ud.dirs, ud.dirs_nr); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2496 | data = next; |
| 2497 | |
| 2498 | len = strlen((const char *)data); |
| 2499 | next = data + len + 1; |
| 2500 | if (next > rd->end) |
| 2501 | return -1; |
Jeff King | 50a6c8e | 2016-02-22 17:44:35 -0500 | [diff] [blame] | 2502 | *untracked_ = untracked = xmalloc(st_add(sizeof(*untracked), len)); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2503 | memcpy(untracked, &ud, sizeof(ud)); |
| 2504 | memcpy(untracked->name, data, len + 1); |
| 2505 | data = next; |
| 2506 | |
| 2507 | for (i = 0; i < untracked->untracked_nr; i++) { |
| 2508 | len = strlen((const char *)data); |
| 2509 | next = data + len + 1; |
| 2510 | if (next > rd->end) |
| 2511 | return -1; |
| 2512 | untracked->untracked[i] = xstrdup((const char*)data); |
| 2513 | data = next; |
| 2514 | } |
| 2515 | |
| 2516 | rd->ucd[rd->index++] = untracked; |
| 2517 | rd->data = data; |
| 2518 | |
| 2519 | for (i = 0; i < untracked->dirs_nr; i++) { |
| 2520 | len = read_one_dir(untracked->dirs + i, rd); |
| 2521 | if (len < 0) |
| 2522 | return -1; |
| 2523 | } |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
| 2527 | static void set_check_only(size_t pos, void *cb) |
| 2528 | { |
| 2529 | struct read_data *rd = cb; |
| 2530 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
| 2531 | ud->check_only = 1; |
| 2532 | } |
| 2533 | |
| 2534 | static void read_stat(size_t pos, void *cb) |
| 2535 | { |
| 2536 | struct read_data *rd = cb; |
| 2537 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
| 2538 | if (rd->data + sizeof(struct stat_data) > rd->end) { |
| 2539 | rd->data = rd->end + 1; |
| 2540 | return; |
| 2541 | } |
| 2542 | stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data); |
| 2543 | rd->data += sizeof(struct stat_data); |
| 2544 | ud->valid = 1; |
| 2545 | } |
| 2546 | |
| 2547 | static void read_sha1(size_t pos, void *cb) |
| 2548 | { |
| 2549 | struct read_data *rd = cb; |
| 2550 | struct untracked_cache_dir *ud = rd->ucd[pos]; |
| 2551 | if (rd->data + 20 > rd->end) { |
| 2552 | rd->data = rd->end + 1; |
| 2553 | return; |
| 2554 | } |
| 2555 | hashcpy(ud->exclude_sha1, rd->data); |
| 2556 | rd->data += 20; |
| 2557 | } |
| 2558 | |
| 2559 | static void load_sha1_stat(struct sha1_stat *sha1_stat, |
| 2560 | const struct stat_data *stat, |
| 2561 | const unsigned char *sha1) |
| 2562 | { |
| 2563 | stat_data_from_disk(&sha1_stat->stat, stat); |
| 2564 | hashcpy(sha1_stat->sha1, sha1); |
| 2565 | sha1_stat->valid = 1; |
| 2566 | } |
| 2567 | |
| 2568 | struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz) |
| 2569 | { |
| 2570 | const struct ondisk_untracked_cache *ouc; |
| 2571 | struct untracked_cache *uc; |
| 2572 | struct read_data rd; |
| 2573 | const unsigned char *next = data, *end = (const unsigned char *)data + sz; |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 2574 | const char *ident; |
| 2575 | int ident_len, len; |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2576 | |
| 2577 | if (sz <= 1 || end[-1] != '\0') |
| 2578 | return NULL; |
| 2579 | end--; |
| 2580 | |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 2581 | ident_len = decode_varint(&next); |
| 2582 | if (next + ident_len > end) |
| 2583 | return NULL; |
| 2584 | ident = (const char *)next; |
| 2585 | next += ident_len; |
| 2586 | |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2587 | ouc = (const struct ondisk_untracked_cache *)next; |
| 2588 | if (next + ouc_size(0) > end) |
| 2589 | return NULL; |
| 2590 | |
| 2591 | uc = xcalloc(1, sizeof(*uc)); |
Nguyễn Thái Ngọc Duy | 1e8fef6 | 2015-03-08 17:12:46 +0700 | [diff] [blame] | 2592 | strbuf_init(&uc->ident, ident_len); |
| 2593 | strbuf_add(&uc->ident, ident, ident_len); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2594 | load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat, |
| 2595 | ouc->info_exclude_sha1); |
| 2596 | load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat, |
| 2597 | ouc->excludes_file_sha1); |
| 2598 | uc->dir_flags = get_be32(&ouc->dir_flags); |
| 2599 | uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir); |
| 2600 | /* NUL after exclude_per_dir is covered by sizeof(*ouc) */ |
| 2601 | next += ouc_size(strlen(ouc->exclude_per_dir)); |
| 2602 | if (next >= end) |
| 2603 | goto done2; |
| 2604 | |
| 2605 | len = decode_varint(&next); |
| 2606 | if (next > end || len == 0) |
| 2607 | goto done2; |
| 2608 | |
| 2609 | rd.valid = ewah_new(); |
| 2610 | rd.check_only = ewah_new(); |
| 2611 | rd.sha1_valid = ewah_new(); |
| 2612 | rd.data = next; |
| 2613 | rd.end = end; |
| 2614 | rd.index = 0; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 2615 | ALLOC_ARRAY(rd.ucd, len); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2616 | |
| 2617 | if (read_one_dir(&uc->root, &rd) || rd.index != len) |
| 2618 | goto done; |
| 2619 | |
| 2620 | next = rd.data; |
| 2621 | len = ewah_read_mmap(rd.valid, next, end - next); |
| 2622 | if (len < 0) |
| 2623 | goto done; |
| 2624 | |
| 2625 | next += len; |
| 2626 | len = ewah_read_mmap(rd.check_only, next, end - next); |
| 2627 | if (len < 0) |
| 2628 | goto done; |
| 2629 | |
| 2630 | next += len; |
| 2631 | len = ewah_read_mmap(rd.sha1_valid, next, end - next); |
| 2632 | if (len < 0) |
| 2633 | goto done; |
| 2634 | |
| 2635 | ewah_each_bit(rd.check_only, set_check_only, &rd); |
| 2636 | rd.data = next + len; |
| 2637 | ewah_each_bit(rd.valid, read_stat, &rd); |
| 2638 | ewah_each_bit(rd.sha1_valid, read_sha1, &rd); |
| 2639 | next = rd.data; |
| 2640 | |
| 2641 | done: |
| 2642 | free(rd.ucd); |
| 2643 | ewah_free(rd.valid); |
| 2644 | ewah_free(rd.check_only); |
| 2645 | ewah_free(rd.sha1_valid); |
| 2646 | done2: |
| 2647 | if (next != end) { |
| 2648 | free_untracked_cache(uc); |
| 2649 | uc = NULL; |
| 2650 | } |
| 2651 | return uc; |
| 2652 | } |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 2653 | |
Nguyễn Thái Ngọc Duy | 73f9145 | 2015-08-19 20:01:26 +0700 | [diff] [blame] | 2654 | static void invalidate_one_directory(struct untracked_cache *uc, |
| 2655 | struct untracked_cache_dir *ucd) |
| 2656 | { |
| 2657 | uc->dir_invalidated++; |
| 2658 | ucd->valid = 0; |
| 2659 | ucd->untracked_nr = 0; |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | * Normally when an entry is added or removed from a directory, |
| 2664 | * invalidating that directory is enough. No need to touch its |
| 2665 | * ancestors. When a directory is shown as "foo/bar/" in git-status |
| 2666 | * however, deleting or adding an entry may have cascading effect. |
| 2667 | * |
| 2668 | * Say the "foo/bar/file" has become untracked, we need to tell the |
| 2669 | * untracked_cache_dir of "foo" that "bar/" is not an untracked |
| 2670 | * directory any more (because "bar" is managed by foo as an untracked |
| 2671 | * "file"). |
| 2672 | * |
| 2673 | * Similarly, if "foo/bar/file" moves from untracked to tracked and it |
| 2674 | * was the last untracked entry in the entire "foo", we should show |
| 2675 | * "foo/" instead. Which means we have to invalidate past "bar" up to |
| 2676 | * "foo". |
| 2677 | * |
| 2678 | * This function traverses all directories from root to leaf. If there |
| 2679 | * is a chance of one of the above cases happening, we invalidate back |
| 2680 | * to root. Otherwise we just invalidate the leaf. There may be a more |
| 2681 | * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to |
| 2682 | * detect these cases and avoid unnecessary invalidation, for example, |
| 2683 | * checking for the untracked entry named "bar/" in "foo", but for now |
| 2684 | * stick to something safe and simple. |
| 2685 | */ |
| 2686 | static int invalidate_one_component(struct untracked_cache *uc, |
| 2687 | struct untracked_cache_dir *dir, |
| 2688 | const char *path, int len) |
| 2689 | { |
| 2690 | const char *rest = strchr(path, '/'); |
| 2691 | |
| 2692 | if (rest) { |
| 2693 | int component_len = rest - path; |
| 2694 | struct untracked_cache_dir *d = |
| 2695 | lookup_untracked(uc, dir, path, component_len); |
| 2696 | int ret = |
| 2697 | invalidate_one_component(uc, d, rest + 1, |
| 2698 | len - (component_len + 1)); |
| 2699 | if (ret) |
| 2700 | invalidate_one_directory(uc, dir); |
| 2701 | return ret; |
| 2702 | } |
| 2703 | |
| 2704 | invalidate_one_directory(uc, dir); |
| 2705 | return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES; |
| 2706 | } |
| 2707 | |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 2708 | void untracked_cache_invalidate_path(struct index_state *istate, |
| 2709 | const char *path) |
| 2710 | { |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 2711 | if (!istate->untracked || !istate->untracked->root) |
| 2712 | return; |
Nguyễn Thái Ngọc Duy | 73f9145 | 2015-08-19 20:01:26 +0700 | [diff] [blame] | 2713 | invalidate_one_component(istate->untracked, istate->untracked->root, |
| 2714 | path, strlen(path)); |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | void untracked_cache_remove_from_index(struct index_state *istate, |
| 2718 | const char *path) |
| 2719 | { |
| 2720 | untracked_cache_invalidate_path(istate, path); |
| 2721 | } |
| 2722 | |
| 2723 | void untracked_cache_add_to_index(struct index_state *istate, |
| 2724 | const char *path) |
| 2725 | { |
| 2726 | untracked_cache_invalidate_path(istate, path); |
| 2727 | } |
Stefan Beller | 47e83eb | 2016-12-12 11:04:34 -0800 | [diff] [blame] | 2728 | |
| 2729 | /* Update gitfile and core.worktree setting to connect work tree and git dir */ |
| 2730 | void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_) |
| 2731 | { |
| 2732 | struct strbuf file_name = STRBUF_INIT; |
| 2733 | struct strbuf rel_path = STRBUF_INIT; |
Junio C Hamano | 1c16df2 | 2017-01-18 15:12:16 -0800 | [diff] [blame] | 2734 | char *git_dir = real_pathdup(git_dir_); |
| 2735 | char *work_tree = real_pathdup(work_tree_); |
Stefan Beller | 47e83eb | 2016-12-12 11:04:34 -0800 | [diff] [blame] | 2736 | |
| 2737 | /* Update gitfile */ |
| 2738 | strbuf_addf(&file_name, "%s/.git", work_tree); |
| 2739 | write_file(file_name.buf, "gitdir: %s", |
| 2740 | relative_path(git_dir, work_tree, &rel_path)); |
| 2741 | |
| 2742 | /* Update core.worktree setting */ |
| 2743 | strbuf_reset(&file_name); |
| 2744 | strbuf_addf(&file_name, "%s/config", git_dir); |
| 2745 | git_config_set_in_file(file_name.buf, "core.worktree", |
| 2746 | relative_path(work_tree, git_dir, &rel_path)); |
| 2747 | |
| 2748 | strbuf_release(&file_name); |
| 2749 | strbuf_release(&rel_path); |
| 2750 | free(work_tree); |
| 2751 | free(git_dir); |
| 2752 | } |
Stefan Beller | f6f8586 | 2016-12-12 11:04:35 -0800 | [diff] [blame] | 2753 | |
| 2754 | /* |
| 2755 | * Migrate the git directory of the given path from old_git_dir to new_git_dir. |
| 2756 | */ |
| 2757 | void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir) |
| 2758 | { |
| 2759 | if (rename(old_git_dir, new_git_dir) < 0) |
| 2760 | die_errno(_("could not migrate git directory from '%s' to '%s'"), |
| 2761 | old_git_dir, new_git_dir); |
| 2762 | |
| 2763 | connect_work_tree_and_git_dir(path, new_git_dir); |
| 2764 | } |