blob: 332f9d8095faf0ec4549cab41811167aecea31d3 [file] [log] [blame]
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001/*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
4 *
Adam Spiers95a68342012-12-27 02:32:21 +00005 * See Documentation/technical/api-directory-listing.txt
6 *
Linus Torvalds453ec4b2006-05-16 19:02:14 -07007 * Copyright (C) Linus Torvalds, 2005-2006
8 * Junio Hamano, 2005-2006
9 */
Brandon Williams12cd0bf2017-05-05 12:53:21 -070010#define NO_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvalds453ec4b2006-05-16 19:02:14 -070011#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070012#include "config.h"
Linus Torvalds453ec4b2006-05-16 19:02:14 -070013#include "dir.h"
Brandon Williamsb0db7042017-03-13 11:23:21 -070014#include "attr.h"
Linus Torvalds09595252007-04-11 14:49:44 -070015#include "refs.h"
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +070016#include "wildmatch.h"
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070017#include "pathspec.h"
Junio C Hamanodde843e2015-04-16 10:45:29 -070018#include "utf8.h"
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +070019#include "varint.h"
20#include "ewah/ewok.h"
Linus Torvalds453ec4b2006-05-16 19:02:14 -070021
Karsten Bleesdefd7c72013-04-15 21:14:22 +020022/*
23 * Tells read_directory_recursive how a file or directory should be treated.
24 * Values are ordered by significance, e.g. if a directory contains both
25 * excluded and untracked files, it is listed as untracked because
26 * path_untracked > path_excluded.
27 */
28enum path_treatment {
29 path_none = 0,
30 path_recurse,
31 path_excluded,
32 path_untracked
33};
34
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +070035/*
36 * Support data structure for our opendir/readdir/closedir wrappers
37 */
38struct cached_dir {
39 DIR *fdir;
40 struct untracked_cache_dir *untracked;
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +070041 int nr_files;
42 int nr_dirs;
43
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +070044 struct dirent *de;
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +070045 const char *file;
46 struct untracked_cache_dir *ucd;
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +070047};
48
Karsten Bleesdefd7c72013-04-15 21:14:22 +020049static enum path_treatment read_directory_recursive(struct dir_struct *dir,
Brandon Williams0ef8e162017-05-05 12:53:32 -070050 struct index_state *istate, const char *path, int len,
51 struct untracked_cache_dir *untracked,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -080052 int check_only, const struct pathspec *pathspec);
Brandon Williams98f2a682017-05-05 12:53:24 -070053static int get_dtype(struct dirent *de, struct index_state *istate,
54 const char *path, int len);
Linus Torvalds09595252007-04-11 14:49:44 -070055
Prathamesh Chavane0556a92017-06-08 23:38:12 +053056int count_slashes(const char *s)
57{
58 int cnt = 0;
59 while (*s)
60 if (*s++ == '/')
61 cnt++;
62 return cnt;
63}
64
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +070065int fspathcmp(const char *a, const char *b)
Joshua Jensen8cf2a842010-10-03 09:56:41 +000066{
67 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
68}
69
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +070070int fspathncmp(const char *a, const char *b, size_t count)
Joshua Jensen8cf2a842010-10-03 09:56:41 +000071{
72 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
73}
74
Charles Bailey1f26ce62014-03-29 15:39:00 +000075int git_fnmatch(const struct pathspec_item *item,
76 const char *pattern, const char *string,
77 int prefix)
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +070078{
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +070079 if (prefix > 0) {
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +070080 if (ps_strncmp(item, pattern, string, prefix))
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +070081 return WM_NOMATCH;
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +070082 pattern += prefix;
83 string += prefix;
84 }
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +070085 if (item->flags & PATHSPEC_ONESTAR) {
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +070086 int pattern_len = strlen(++pattern);
87 int string_len = strlen(string);
88 return string_len < pattern_len ||
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +070089 ps_strcmp(item, pattern,
90 string + string_len - pattern_len);
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +070091 }
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +070092 if (item->magic & PATHSPEC_GLOB)
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +070093 return wildmatch(pattern, string,
94 WM_PATHNAME |
95 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
96 NULL);
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +070097 else
98 /* wildmatch has not learned no FNM_PATHNAME mode yet */
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +070099 return wildmatch(pattern, string,
100 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0,
101 NULL);
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700102}
103
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400104static int fnmatch_icase_mem(const char *pattern, int patternlen,
105 const char *string, int stringlen,
106 int flags)
107{
108 int match_status;
109 struct strbuf pat_buf = STRBUF_INIT;
110 struct strbuf str_buf = STRBUF_INIT;
111 const char *use_pat = pattern;
112 const char *use_str = string;
113
114 if (pattern[patternlen]) {
115 strbuf_add(&pat_buf, pattern, patternlen);
116 use_pat = pat_buf.buf;
117 }
118 if (string[stringlen]) {
119 strbuf_add(&str_buf, string, stringlen);
120 use_str = str_buf.buf;
121 }
122
Junio C Hamanof30366b2013-04-03 09:34:04 -0700123 if (ignore_case)
124 flags |= WM_CASEFOLD;
125 match_status = wildmatch(use_pat, use_str, flags, NULL);
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400126
127 strbuf_release(&pat_buf);
128 strbuf_release(&str_buf);
129
130 return match_status;
131}
132
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700133static size_t common_prefix_len(const struct pathspec *pathspec)
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700134{
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700135 int n;
Junio C Hamano4a085b12011-09-06 12:32:30 -0700136 size_t max = 0;
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700137
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700138 /*
139 * ":(icase)path" is treated as a pathspec full of
140 * wildcard. In other words, only prefix is considered common
141 * prefix. If the pathspec is abc/foo abc/bar, running in
142 * subdir xyz, the common prefix is still xyz, not xuz/abc as
143 * in non-:(icase).
144 */
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +0700145 GUARD_PATHSPEC(pathspec,
146 PATHSPEC_FROMTOP |
147 PATHSPEC_MAXDEPTH |
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700148 PATHSPEC_LITERAL |
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700149 PATHSPEC_GLOB |
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700150 PATHSPEC_ICASE |
Brandon Williamsb0db7042017-03-13 11:23:21 -0700151 PATHSPEC_EXCLUDE |
152 PATHSPEC_ATTR);
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700153
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700154 for (n = 0; n < pathspec->nr; n++) {
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700155 size_t i = 0, len = 0, item_len;
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700156 if (pathspec->items[n].magic & PATHSPEC_EXCLUDE)
157 continue;
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700158 if (pathspec->items[n].magic & PATHSPEC_ICASE)
159 item_len = pathspec->items[n].prefix;
160 else
161 item_len = pathspec->items[n].nowildcard_len;
162 while (i < item_len && (n == 0 || i < max)) {
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700163 char c = pathspec->items[n].match[i];
164 if (c != pathspec->items[0].match[i])
Junio C Hamano4a085b12011-09-06 12:32:30 -0700165 break;
166 if (c == '/')
167 len = i + 1;
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700168 i++;
Junio C Hamano4a085b12011-09-06 12:32:30 -0700169 }
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700170 if (n == 0 || len < max) {
Junio C Hamano4a085b12011-09-06 12:32:30 -0700171 max = len;
172 if (!max)
173 break;
174 }
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700175 }
Junio C Hamano4a085b12011-09-06 12:32:30 -0700176 return max;
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700177}
178
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200179/*
180 * Returns a copy of the longest leading path common among all
181 * pathspecs.
182 */
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700183char *common_prefix(const struct pathspec *pathspec)
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200184{
185 unsigned long len = common_prefix_len(pathspec);
186
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700187 return len ? xmemdupz(pathspec->items[0].match, len) : NULL;
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200188}
189
Brandon Williams0d32c182017-05-05 12:53:34 -0700190int fill_directory(struct dir_struct *dir,
191 struct index_state *istate,
192 const struct pathspec *pathspec)
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700193{
René Scharfebec5ab82017-02-07 23:04:25 +0100194 const char *prefix;
Brandon Williams966de302017-01-04 10:03:58 -0800195 size_t prefix_len;
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700196
197 /*
198 * Calculate common prefix for the pathspec, and
199 * use that to optimize the directory walk
200 */
René Scharfebec5ab82017-02-07 23:04:25 +0100201 prefix_len = common_prefix_len(pathspec);
202 prefix = prefix_len ? pathspec->items[0].match : "";
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700203
204 /* Read the directory and prune it */
Brandon Williams0d32c182017-05-05 12:53:34 -0700205 read_directory(dir, istate, prefix, prefix_len, pathspec);
Brandon Williams966de302017-01-04 10:03:58 -0800206
Brandon Williams966de302017-01-04 10:03:58 -0800207 return prefix_len;
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700208}
209
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700210int within_depth(const char *name, int namelen,
211 int depth, int max_depth)
212{
213 const char *cp = name, *cpe = name + namelen;
214
215 while (cp < cpe) {
216 if (*cp++ != '/')
217 continue;
218 depth++;
219 if (depth > max_depth)
220 return 0;
221 }
222 return 1;
223}
224
Brandon Williams75a63152016-10-07 11:18:51 -0700225#define DO_MATCH_EXCLUDE (1<<0)
226#define DO_MATCH_DIRECTORY (1<<1)
227#define DO_MATCH_SUBMODULE (1<<2)
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700228
Brandon Williamsb0db7042017-03-13 11:23:21 -0700229static int match_attrs(const char *name, int namelen,
230 const struct pathspec_item *item)
231{
232 int i;
233
234 git_check_attr(name, item->attr_check);
235 for (i = 0; i < item->attr_match_nr; i++) {
236 const char *value;
237 int matched;
238 enum attr_match_mode match_mode;
239
240 value = item->attr_check->items[i].value;
241 match_mode = item->attr_match[i].match_mode;
242
243 if (ATTR_TRUE(value))
244 matched = (match_mode == MATCH_SET);
245 else if (ATTR_FALSE(value))
246 matched = (match_mode == MATCH_UNSET);
247 else if (ATTR_UNSET(value))
248 matched = (match_mode == MATCH_UNSPECIFIED);
249 else
250 matched = (match_mode == MATCH_VALUE &&
251 !strcmp(item->attr_match[i].value, value));
252 if (!matched)
253 return 0;
254 }
255
256 return 1;
257}
258
Junio C Hamanoe813d502006-12-25 03:09:52 -0800259/*
Allan Caffee2c5b0112009-05-04 13:37:30 -0400260 * Does 'match' match the given name?
Junio C Hamanoe813d502006-12-25 03:09:52 -0800261 * A match is found if
262 *
263 * (1) the 'match' string is leading directory of 'name', or
264 * (2) the 'match' string is a wildcard and matches 'name', or
265 * (3) the 'match' string is exactly the same as 'name'.
266 *
267 * and the return value tells which case it was.
268 *
269 * It returns 0 when there is no match.
270 */
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700271static int match_pathspec_item(const struct pathspec_item *item, int prefix,
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700272 const char *name, int namelen, unsigned flags)
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700273{
274 /* name/namelen has prefix cut off by caller */
275 const char *match = item->match + prefix;
276 int matchlen = item->len - prefix;
277
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700278 /*
279 * The normal call pattern is:
280 * 1. prefix = common_prefix_len(ps);
281 * 2. prune something, or fill_directory
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700282 * 3. match_pathspec()
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700283 *
284 * 'prefix' at #1 may be shorter than the command's prefix and
285 * it's ok for #2 to match extra files. Those extras will be
286 * trimmed at #3.
287 *
288 * Suppose the pathspec is 'foo' and '../bar' running from
289 * subdir 'xyz'. The common prefix at #1 will be empty, thanks
290 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
291 * user does not want XYZ/foo, only the "foo" part should be
292 * case-insensitive. We need to filter out XYZ/foo here. In
293 * other words, we do not trust the caller on comparing the
294 * prefix part when :(icase) is involved. We do exact
295 * comparison ourselves.
296 *
297 * Normally the caller (common_prefix_len() in fact) does
298 * _exact_ matching on name[-prefix+1..-1] and we do not need
299 * to check that part. Be defensive and check it anyway, in
300 * case common_prefix_len is changed, or a new caller is
301 * introduced that does not use common_prefix_len.
302 *
303 * If the penalty turns out too high when prefix is really
304 * long, maybe change it to
305 * strncmp(match, name, item->prefix - prefix)
306 */
307 if (item->prefix && (item->magic & PATHSPEC_ICASE) &&
308 strncmp(item->match, name - prefix, item->prefix))
309 return 0;
310
Brandon Williamsb0db7042017-03-13 11:23:21 -0700311 if (item->attr_match_nr && !match_attrs(name, namelen, item))
312 return 0;
313
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700314 /* If the match was just the prefix, we matched */
315 if (!*match)
316 return MATCHED_RECURSIVELY;
317
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700318 if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) {
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700319 if (matchlen == namelen)
320 return MATCHED_EXACTLY;
321
322 if (match[matchlen-1] == '/' || name[matchlen] == '/')
323 return MATCHED_RECURSIVELY;
Nguyễn Thái Ngọc Duy68690fd2014-01-24 20:40:32 +0700324 } else if ((flags & DO_MATCH_DIRECTORY) &&
325 match[matchlen - 1] == '/' &&
326 namelen == matchlen - 1 &&
327 !ps_strncmp(item, match, name, namelen))
328 return MATCHED_EXACTLY;
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700329
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700330 if (item->nowildcard_len < item->len &&
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700331 !git_fnmatch(item, match, name,
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +0700332 item->nowildcard_len - prefix))
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700333 return MATCHED_FNMATCH;
334
Brandon Williams75a63152016-10-07 11:18:51 -0700335 /* Perform checks to see if "name" is a super set of the pathspec */
336 if (flags & DO_MATCH_SUBMODULE) {
337 /* name is a literal prefix of the pathspec */
338 if ((namelen < matchlen) &&
339 (match[namelen] == '/') &&
340 !ps_strncmp(item, match, name, namelen))
341 return MATCHED_RECURSIVELY;
342
343 /* name" doesn't match up to the first wild character */
344 if (item->nowildcard_len < item->len &&
345 ps_strncmp(item, match, name,
346 item->nowildcard_len - prefix))
347 return 0;
348
349 /*
350 * Here is where we would perform a wildmatch to check if
351 * "name" can be matched as a directory (or a prefix) against
352 * the pathspec. Since wildmatch doesn't have this capability
353 * at the present we have to punt and say that it is a match,
354 * potentially returning a false positive
355 * The submodules themselves will be able to perform more
356 * accurate matching to determine if the pathspec matches.
357 */
358 return MATCHED_RECURSIVELY;
359 }
360
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700361 return 0;
362}
363
364/*
Adam Spiers52ed1892013-01-06 16:58:06 +0000365 * Given a name and a list of pathspecs, returns the nature of the
366 * closest (i.e. most specific) match of the name to any of the
367 * pathspecs.
368 *
369 * The caller typically calls this multiple times with the same
370 * pathspec and seen[] array but with different name/namelen
371 * (e.g. entries from the index) and is interested in seeing if and
372 * how each pathspec matches all the names it calls this function
373 * with. A mark is left in the seen[] array for each pathspec element
374 * indicating the closest type of match that element achieved, so if
375 * seen[n] remains zero after multiple invocations, that means the nth
376 * pathspec did not match any names, which could indicate that the
377 * user mistyped the nth pathspec.
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700378 */
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700379static int do_match_pathspec(const struct pathspec *ps,
380 const char *name, int namelen,
381 int prefix, char *seen,
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700382 unsigned flags)
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700383{
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700384 int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE;
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700385
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +0700386 GUARD_PATHSPEC(ps,
387 PATHSPEC_FROMTOP |
388 PATHSPEC_MAXDEPTH |
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700389 PATHSPEC_LITERAL |
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700390 PATHSPEC_GLOB |
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700391 PATHSPEC_ICASE |
Brandon Williamsb0db7042017-03-13 11:23:21 -0700392 PATHSPEC_EXCLUDE |
393 PATHSPEC_ATTR);
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +0700394
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700395 if (!ps->nr) {
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +0700396 if (!ps->recursive ||
397 !(ps->magic & PATHSPEC_MAXDEPTH) ||
398 ps->max_depth == -1)
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700399 return MATCHED_RECURSIVELY;
400
401 if (within_depth(name, namelen, 0, ps->max_depth))
402 return MATCHED_EXACTLY;
403 else
404 return 0;
405 }
406
407 name += prefix;
408 namelen -= prefix;
409
410 for (i = ps->nr - 1; i >= 0; i--) {
411 int how;
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700412
413 if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) ||
414 ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE)))
415 continue;
416
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700417 if (seen && seen[i] == MATCHED_EXACTLY)
418 continue;
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700419 /*
420 * Make exclude patterns optional and never report
421 * "pathspec ':(exclude)foo' matches no files"
422 */
423 if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE)
424 seen[i] = MATCHED_FNMATCH;
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700425 how = match_pathspec_item(ps->items+i, prefix, name,
426 namelen, flags);
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +0700427 if (ps->recursive &&
428 (ps->magic & PATHSPEC_MAXDEPTH) &&
429 ps->max_depth != -1 &&
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +0700430 how && how != MATCHED_FNMATCH) {
431 int len = ps->items[i].len;
432 if (name[len] == '/')
433 len++;
434 if (within_depth(name+len, namelen-len, 0, ps->max_depth))
435 how = MATCHED_EXACTLY;
436 else
437 how = 0;
438 }
439 if (how) {
440 if (retval < how)
441 retval = how;
442 if (seen && seen[i] < how)
443 seen[i] = how;
444 }
445 }
446 return retval;
447}
448
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700449int match_pathspec(const struct pathspec *ps,
450 const char *name, int namelen,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700451 int prefix, char *seen, int is_dir)
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700452{
453 int positive, negative;
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +0700454 unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700455 positive = do_match_pathspec(ps, name, namelen,
456 prefix, seen, flags);
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700457 if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
458 return positive;
Nguyễn Thái Ngọc Duy42b08742014-01-24 20:40:31 +0700459 negative = do_match_pathspec(ps, name, namelen,
460 prefix, seen,
461 flags | DO_MATCH_EXCLUDE);
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +0700462 return negative ? 0 : positive;
463}
464
Brandon Williams75a63152016-10-07 11:18:51 -0700465/**
466 * Check if a submodule is a superset of the pathspec
467 */
468int submodule_path_match(const struct pathspec *ps,
469 const char *submodule_name,
470 char *seen)
471{
472 int matched = do_match_pathspec(ps, submodule_name,
473 strlen(submodule_name),
474 0, seen,
475 DO_MATCH_DIRECTORY |
476 DO_MATCH_SUBMODULE);
477 return matched;
478}
479
Junio C Hamano777c55a2015-03-24 14:12:10 -0700480int report_path_error(const char *ps_matched,
481 const struct pathspec *pathspec,
482 const char *prefix)
483{
484 /*
485 * Make sure all pathspec matched; otherwise it is an error.
486 */
Junio C Hamano777c55a2015-03-24 14:12:10 -0700487 int num, errors = 0;
488 for (num = 0; num < pathspec->nr; num++) {
489 int other, found_dup;
490
491 if (ps_matched[num])
492 continue;
493 /*
494 * The caller might have fed identical pathspec
495 * twice. Do not barf on such a mistake.
496 * FIXME: parse_pathspec should have eliminated
497 * duplicate pathspec.
498 */
499 for (found_dup = other = 0;
500 !found_dup && other < pathspec->nr;
501 other++) {
502 if (other == num || !ps_matched[other])
503 continue;
504 if (!strcmp(pathspec->items[other].original,
505 pathspec->items[num].original))
506 /*
507 * Ok, we have a match already.
508 */
509 found_dup = 1;
510 }
511 if (found_dup)
512 continue;
513
514 error("pathspec '%s' did not match any file(s) known to git.",
515 pathspec->items[num].original);
516 errors++;
517 }
Junio C Hamano777c55a2015-03-24 14:12:10 -0700518 return errors;
519}
520
Nguyễn Thái Ngọc Duyfcd631e2012-06-07 14:53:35 +0700521/*
522 * Return the length of the "simple" part of a path match limiter.
523 */
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +0700524int simple_length(const char *match)
Nguyễn Thái Ngọc Duyfcd631e2012-06-07 14:53:35 +0700525{
526 int len = -1;
527
528 for (;;) {
529 unsigned char c = *match++;
530 len++;
531 if (c == '\0' || is_glob_special(c))
532 return len;
533 }
534}
535
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +0700536int no_wildcard(const char *string)
Lars Knoll68492fc2007-10-28 21:27:13 +0100537{
Nguyễn Thái Ngọc Duyfcd631e2012-06-07 14:53:35 +0700538 return string[simple_length(string)] == '\0';
Lars Knoll68492fc2007-10-28 21:27:13 +0100539}
540
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700541void parse_exclude_pattern(const char **pattern,
542 int *patternlen,
Saurav Sachidanandf8708992016-03-01 22:32:59 +0530543 unsigned *flags,
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700544 int *nowildcardlen)
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +0700545{
546 const char *p = *pattern;
547 size_t i, len;
548
549 *flags = 0;
550 if (*p == '!') {
551 *flags |= EXC_FLAG_NEGATIVE;
552 p++;
553 }
554 len = strlen(p);
555 if (len && p[len - 1] == '/') {
556 len--;
557 *flags |= EXC_FLAG_MUSTBEDIR;
558 }
559 for (i = 0; i < len; i++) {
560 if (p[i] == '/')
561 break;
562 }
563 if (i == len)
564 *flags |= EXC_FLAG_NODIR;
565 *nowildcardlen = simple_length(p);
566 /*
567 * we should have excluded the trailing slash from 'p' too,
568 * but that's one more allocation. Instead just make sure
569 * nowildcardlen does not exceed real patternlen
570 */
571 if (*nowildcardlen > len)
572 *nowildcardlen = len;
573 if (*p == '*' && no_wildcard(p + 1))
574 *flags |= EXC_FLAG_ENDSWITH;
575 *pattern = p;
576 *patternlen = len;
577}
578
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700579void add_exclude(const char *string, const char *base,
Adam Spiersc04318e2013-01-06 16:58:04 +0000580 int baselen, struct exclude_list *el, int srcpos)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700581{
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800582 struct exclude *x;
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +0700583 int patternlen;
Saurav Sachidanandf8708992016-03-01 22:32:59 +0530584 unsigned flags;
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +0700585 int nowildcardlen;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700586
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +0700587 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
588 if (flags & EXC_FLAG_MUSTBEDIR) {
Jeff King96ffc062016-02-22 17:44:32 -0500589 FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800590 } else {
591 x = xmalloc(sizeof(*x));
592 x->pattern = string;
593 }
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +0700594 x->patternlen = patternlen;
595 x->nowildcardlen = nowildcardlen;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700596 x->base = base;
597 x->baselen = baselen;
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800598 x->flags = flags;
Adam Spiersc04318e2013-01-06 16:58:04 +0000599 x->srcpos = srcpos;
Adam Spiers840fc332012-12-27 02:32:22 +0000600 ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
601 el->excludes[el->nr++] = x;
Adam Spiersc04318e2013-01-06 16:58:04 +0000602 x->el = el;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700603}
604
Brandon Williams6f52b742017-05-05 12:53:22 -0700605static void *read_skip_worktree_file_from_index(const struct index_state *istate,
606 const char *path, size_t *size,
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700607 struct sha1_stat *sha1_stat)
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700608{
609 int pos, len;
610 unsigned long sz;
611 enum object_type type;
612 void *data;
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700613
614 len = strlen(path);
Brandon Williams6f52b742017-05-05 12:53:22 -0700615 pos = index_name_pos(istate, path, len);
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700616 if (pos < 0)
617 return NULL;
Brandon Williams6f52b742017-05-05 12:53:22 -0700618 if (!ce_skip_worktree(istate->cache[pos]))
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700619 return NULL;
Brandon Williams6f52b742017-05-05 12:53:22 -0700620 data = read_sha1_file(istate->cache[pos]->oid.hash, &type, &sz);
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700621 if (!data || type != OBJ_BLOB) {
622 free(data);
623 return NULL;
624 }
625 *size = xsize_t(sz);
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700626 if (sha1_stat) {
627 memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
Brandon Williams6f52b742017-05-05 12:53:22 -0700628 hashcpy(sha1_stat->sha1, istate->cache[pos]->oid.hash);
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700629 }
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700630 return data;
631}
632
Adam Spiersf6198812012-12-27 02:32:29 +0000633/*
634 * Frees memory within el which was allocated for exclude patterns and
635 * the file buffer. Does not free el itself.
636 */
637void clear_exclude_list(struct exclude_list *el)
Nguyễn Thái Ngọc Duy0fd0e242010-11-27 01:17:44 +0700638{
639 int i;
640
Junio C Hamano5cee3492016-03-18 11:06:15 -0700641 for (i = 0; i < el->nr; i++)
Nguyễn Thái Ngọc Duy0fd0e242010-11-27 01:17:44 +0700642 free(el->excludes[i]);
643 free(el->excludes);
Adam Spiersc082df22013-01-06 16:58:03 +0000644 free(el->filebuf);
Nguyễn Thái Ngọc Duy0fd0e242010-11-27 01:17:44 +0700645
Nguyễn Thái Ngọc Duy2653a8c2015-12-27 08:54:34 +0700646 memset(el, 0, sizeof(*el));
Nguyễn Thái Ngọc Duy0fd0e242010-11-27 01:17:44 +0700647}
648
Nguyễn Thái Ngọc Duy7e2e4b32014-02-09 07:26:38 +0700649static void trim_trailing_spaces(char *buf)
Nguyễn Thái Ngọc Duy16402b92014-02-09 07:26:37 +0700650{
Pasha Bolokhove61a6c12014-06-02 15:36:56 -0700651 char *p, *last_space = NULL;
Nguyễn Thái Ngọc Duy16402b92014-02-09 07:26:37 +0700652
Pasha Bolokhove61a6c12014-06-02 15:36:56 -0700653 for (p = buf; *p; p++)
654 switch (*p) {
655 case ' ':
656 if (!last_space)
657 last_space = p;
658 break;
659 case '\\':
660 p++;
661 if (!*p)
662 return;
663 /* fallthrough */
664 default:
665 last_space = NULL;
666 }
667
668 if (last_space)
669 *last_space = '\0';
Nguyễn Thái Ngọc Duy16402b92014-02-09 07:26:37 +0700670}
671
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700672/*
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700673 * Given a subdirectory name and "dir" of the current directory,
674 * search the subdir in "dir" and return it, or create a new one if it
675 * does not exist in "dir".
676 *
677 * If "name" has the trailing slash, it'll be excluded in the search.
678 */
679static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
680 struct untracked_cache_dir *dir,
681 const char *name, int len)
682{
683 int first, last;
684 struct untracked_cache_dir *d;
685 if (!dir)
686 return NULL;
687 if (len && name[len - 1] == '/')
688 len--;
689 first = 0;
690 last = dir->dirs_nr;
691 while (last > first) {
692 int cmp, next = (last + first) >> 1;
693 d = dir->dirs[next];
694 cmp = strncmp(name, d->name, len);
695 if (!cmp && strlen(d->name) > len)
696 cmp = -1;
697 if (!cmp)
698 return d;
699 if (cmp < 0) {
700 last = next;
701 continue;
702 }
703 first = next+1;
704 }
705
706 uc->dir_created++;
Jeff King96ffc062016-02-22 17:44:32 -0500707 FLEX_ALLOC_MEM(d, name, name, len);
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700708
709 ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
710 memmove(dir->dirs + first + 1, dir->dirs + first,
711 (dir->dirs_nr - first) * sizeof(*dir->dirs));
712 dir->dirs_nr++;
713 dir->dirs[first] = d;
714 return d;
715}
716
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700717static void do_invalidate_gitignore(struct untracked_cache_dir *dir)
718{
719 int i;
720 dir->valid = 0;
721 dir->untracked_nr = 0;
722 for (i = 0; i < dir->dirs_nr; i++)
723 do_invalidate_gitignore(dir->dirs[i]);
724}
725
726static void invalidate_gitignore(struct untracked_cache *uc,
727 struct untracked_cache_dir *dir)
728{
729 uc->gitignore_invalidated++;
730 do_invalidate_gitignore(dir);
731}
732
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +0700733static void invalidate_directory(struct untracked_cache *uc,
734 struct untracked_cache_dir *dir)
735{
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +0700736 int i;
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +0700737 uc->dir_invalidated++;
738 dir->valid = 0;
739 dir->untracked_nr = 0;
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +0700740 for (i = 0; i < dir->dirs_nr; i++)
741 dir->dirs[i]->recurse = 0;
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +0700742}
743
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700744/*
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700745 * Given a file with name "fname", read it (either from disk, or from
Brandon Williams473e3932017-05-05 12:53:28 -0700746 * an index if 'istate' is non-null), parse it and store the
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700747 * exclude rules in "el".
748 *
749 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
750 * stat data from disk (only valid if add_excludes returns zero). If
751 * ss_valid is non-zero, "ss" must contain good value as input.
752 */
753static int add_excludes(const char *fname, const char *base, int baselen,
Brandon Williams473e3932017-05-05 12:53:28 -0700754 struct exclude_list *el,
755 struct index_state *istate,
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700756 struct sha1_stat *sha1_stat)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700757{
Jonas Fonsecac4707012006-08-28 01:55:46 +0200758 struct stat st;
Adam Spiersc04318e2013-01-06 16:58:04 +0000759 int fd, i, lineno = 1;
Pat Notz9d140172010-09-16 14:53:22 -0600760 size_t size = 0;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700761 char *buf, *entry;
762
763 fd = open(fname, O_RDONLY);
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700764 if (fd < 0 || fstat(fd, &st) < 0) {
Nguyễn Thái Ngọc Duy11dc1fc2017-05-03 17:16:49 +0700765 if (fd < 0)
766 warn_on_fopen_errors(fname);
767 else
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700768 close(fd);
Brandon Williams473e3932017-05-05 12:53:28 -0700769 if (!istate ||
770 (buf = read_skip_worktree_file_from_index(istate, fname, &size, sha1_stat)) == NULL)
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700771 return -1;
Nguyễn Thái Ngọc Duy45d76f12010-01-20 21:09:16 +0700772 if (size == 0) {
773 free(buf);
774 return 0;
775 }
776 if (buf[size-1] != '\n') {
Jeff King50a6c8e2016-02-22 17:44:35 -0500777 buf = xrealloc(buf, st_add(size, 1));
Nguyễn Thái Ngọc Duy45d76f12010-01-20 21:09:16 +0700778 buf[size++] = '\n';
779 }
Nguyễn Thái Ngọc Duyd961baa2014-07-14 11:47:11 +0200780 } else {
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700781 size = xsize_t(st.st_size);
782 if (size == 0) {
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700783 if (sha1_stat) {
784 fill_stat_data(&sha1_stat->stat, &st);
785 hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
786 sha1_stat->valid = 1;
787 }
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700788 close(fd);
789 return 0;
790 }
Jeff King3733e692016-02-22 17:44:28 -0500791 buf = xmallocz(size);
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700792 if (read_in_full(fd, buf, size) != size) {
Nguyễn Thái Ngọc Duy45d76f12010-01-20 21:09:16 +0700793 free(buf);
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700794 close(fd);
795 return -1;
796 }
Nguyễn Thái Ngọc Duy45d76f12010-01-20 21:09:16 +0700797 buf[size++] = '\n';
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700798 close(fd);
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700799 if (sha1_stat) {
800 int pos;
801 if (sha1_stat->valid &&
Brandon Williams473e3932017-05-05 12:53:28 -0700802 !match_stat_data_racy(istate, &sha1_stat->stat, &st))
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700803 ; /* no content change, ss->sha1 still good */
Brandon Williams473e3932017-05-05 12:53:28 -0700804 else if (istate &&
805 (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
806 !ce_stage(istate->cache[pos]) &&
807 ce_uptodate(istate->cache[pos]) &&
Brandon Williams82b474e2017-06-12 15:13:55 -0700808 !would_convert_to_git(istate, fname))
brian m. carlson99d1a982016-09-05 20:07:52 +0000809 hashcpy(sha1_stat->sha1,
Brandon Williams473e3932017-05-05 12:53:28 -0700810 istate->cache[pos]->oid.hash);
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700811 else
812 hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
813 fill_stat_data(&sha1_stat->stat, &st);
814 sha1_stat->valid = 1;
815 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700816 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700817
Adam Spiersc082df22013-01-06 16:58:03 +0000818 el->filebuf = buf;
Carlos Martín Nieto245e1c12015-04-16 16:05:12 +0200819
Junio C Hamanodde843e2015-04-16 10:45:29 -0700820 if (skip_utf8_bom(&buf, size))
821 size -= buf - el->filebuf;
822
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700823 entry = buf;
Carlos Martín Nieto245e1c12015-04-16 16:05:12 +0200824
Nguyễn Thái Ngọc Duy45d76f12010-01-20 21:09:16 +0700825 for (i = 0; i < size; i++) {
826 if (buf[i] == '\n') {
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700827 if (entry != buf + i && entry[0] != '#') {
828 buf[i - (i && buf[i-1] == '\r')] = 0;
Nguyễn Thái Ngọc Duy7e2e4b32014-02-09 07:26:38 +0700829 trim_trailing_spaces(entry);
Adam Spiersc04318e2013-01-06 16:58:04 +0000830 add_exclude(entry, base, baselen, el, lineno);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700831 }
Adam Spiersc04318e2013-01-06 16:58:04 +0000832 lineno++;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700833 entry = buf + i + 1;
834 }
835 }
836 return 0;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700837}
838
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700839int add_excludes_from_file_to_list(const char *fname, const char *base,
840 int baselen, struct exclude_list *el,
Brandon Williams473e3932017-05-05 12:53:28 -0700841 struct index_state *istate)
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700842{
Brandon Williams473e3932017-05-05 12:53:28 -0700843 return add_excludes(fname, base, baselen, el, istate, NULL);
Nguyễn Thái Ngọc Duy55fe6f52015-03-08 17:12:24 +0700844}
845
Adam Spiersc04318e2013-01-06 16:58:04 +0000846struct exclude_list *add_exclude_list(struct dir_struct *dir,
847 int group_type, const char *src)
Adam Spiersc082df22013-01-06 16:58:03 +0000848{
849 struct exclude_list *el;
850 struct exclude_list_group *group;
851
852 group = &dir->exclude_list_group[group_type];
853 ALLOC_GROW(group->el, group->nr + 1, group->alloc);
854 el = &group->el[group->nr++];
855 memset(el, 0, sizeof(*el));
Adam Spiersc04318e2013-01-06 16:58:04 +0000856 el->src = src;
Adam Spiersc082df22013-01-06 16:58:03 +0000857 return el;
858}
859
860/*
861 * Used to set up core.excludesfile and .git/info/exclude lists.
862 */
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700863static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
864 struct sha1_stat *sha1_stat)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700865{
Adam Spiersc082df22013-01-06 16:58:03 +0000866 struct exclude_list *el;
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700867 /*
868 * catch setup_standard_excludes() that's called before
869 * dir->untracked is assigned. That function behaves
870 * differently when dir->untracked is non-NULL.
871 */
872 if (!dir->untracked)
873 dir->unmanaged_exclude_files++;
Adam Spiersc04318e2013-01-06 16:58:04 +0000874 el = add_exclude_list(dir, EXC_FILE, fname);
Brandon Williams473e3932017-05-05 12:53:28 -0700875 if (add_excludes(fname, "", 0, el, NULL, sha1_stat) < 0)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700876 die("cannot use %s as an exclude file", fname);
877}
878
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700879void add_excludes_from_file(struct dir_struct *dir, const char *fname)
880{
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +0700881 dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +0700882 add_excludes_from_file_1(dir, fname, NULL);
883}
884
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700885int match_basename(const char *basename, int basenamelen,
886 const char *pattern, int prefix, int patternlen,
Saurav Sachidanandf8708992016-03-01 22:32:59 +0530887 unsigned flags)
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +0700888{
889 if (prefix == patternlen) {
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400890 if (patternlen == basenamelen &&
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +0700891 !fspathncmp(pattern, basename, basenamelen))
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +0700892 return 1;
893 } else if (flags & EXC_FLAG_ENDSWITH) {
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400894 /* "*literal" matching against "fooliteral" */
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +0700895 if (patternlen - 1 <= basenamelen &&
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +0700896 !fspathncmp(pattern + 1,
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400897 basename + basenamelen - (patternlen - 1),
898 patternlen - 1))
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +0700899 return 1;
900 } else {
Junio C Hamano0b6e56d2013-03-28 17:47:28 -0400901 if (fnmatch_icase_mem(pattern, patternlen,
902 basename, basenamelen,
903 0) == 0)
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +0700904 return 1;
905 }
906 return 0;
907}
908
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700909int match_pathname(const char *pathname, int pathlen,
910 const char *base, int baselen,
911 const char *pattern, int prefix, int patternlen,
Saurav Sachidanandf8708992016-03-01 22:32:59 +0530912 unsigned flags)
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700913{
914 const char *name;
915 int namelen;
916
917 /*
918 * match with FNM_PATHNAME; the pattern has base implicitly
919 * in front of it.
920 */
921 if (*pattern == '/') {
922 pattern++;
Jeff King982ac872013-03-28 17:47:47 -0400923 patternlen--;
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700924 prefix--;
925 }
926
927 /*
928 * baselen does not count the trailing slash. base[] may or
929 * may not end with a trailing slash though.
930 */
931 if (pathlen < baselen + 1 ||
932 (baselen && pathname[baselen] != '/') ||
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +0700933 fspathncmp(pathname, base, baselen))
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700934 return 0;
935
936 namelen = baselen ? pathlen - baselen - 1 : pathlen;
937 name = pathname + pathlen - namelen;
938
939 if (prefix) {
940 /*
941 * if the non-wildcard part is longer than the
942 * remaining pathname, surely it cannot match.
943 */
944 if (prefix > namelen)
945 return 0;
946
Nguyễn Thái Ngọc Duyba0897e2016-04-22 20:01:24 +0700947 if (fspathncmp(pattern, name, prefix))
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700948 return 0;
949 pattern += prefix;
Jeff Kingab3aebc2013-03-28 17:48:21 -0400950 patternlen -= prefix;
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700951 name += prefix;
952 namelen -= prefix;
Jeff Kingab3aebc2013-03-28 17:48:21 -0400953
954 /*
955 * If the whole pattern did not have a wildcard,
956 * then our prefix match is all we need; we
957 * do not need to call fnmatch at all.
958 */
Junio C Hamano5cee3492016-03-18 11:06:15 -0700959 if (!patternlen && !namelen)
Jeff Kingab3aebc2013-03-28 17:48:21 -0400960 return 1;
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700961 }
962
Jeff Kingab3aebc2013-03-28 17:48:21 -0400963 return fnmatch_icase_mem(pattern, patternlen,
964 name, namelen,
Junio C Hamanof30366b2013-04-03 09:34:04 -0700965 WM_PATHNAME) == 0;
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700966}
967
Adam Spiers578cd7c2012-12-27 02:32:26 +0000968/*
969 * Scan the given exclude list in reverse to see whether pathname
970 * should be ignored. The first match (i.e. the last on the list), if
971 * any, determines the fate. Returns the exclude_list element which
972 * matched, or NULL for undecided.
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700973 */
Adam Spiers578cd7c2012-12-27 02:32:26 +0000974static struct exclude *last_exclude_matching_from_list(const char *pathname,
975 int pathlen,
976 const char *basename,
977 int *dtype,
Brandon Williams2b70e882017-05-05 12:53:26 -0700978 struct exclude_list *el,
979 struct index_state *istate)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700980{
Nguyễn Thái Ngọc Duye6efecc2015-09-21 16:56:14 +0700981 struct exclude *exc = NULL; /* undecided */
Junio C Hamano5cee3492016-03-18 11:06:15 -0700982 int i;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700983
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +0700984 if (!el->nr)
Adam Spiers578cd7c2012-12-27 02:32:26 +0000985 return NULL; /* undefined */
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700986
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +0700987 for (i = el->nr - 1; 0 <= i; i--) {
988 struct exclude *x = el->excludes[i];
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700989 const char *exclude = x->pattern;
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +0700990 int prefix = x->nowildcardlen;
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800991
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +0700992 if (x->flags & EXC_FLAG_MUSTBEDIR) {
993 if (*dtype == DT_UNKNOWN)
Brandon Williams2b70e882017-05-05 12:53:26 -0700994 *dtype = get_dtype(NULL, istate, pathname, pathlen);
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +0700995 if (*dtype != DT_DIR)
996 continue;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700997 }
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +0700998
999 if (x->flags & EXC_FLAG_NODIR) {
Nguyễn Thái Ngọc Duy593cb882012-10-15 13:24:35 +07001000 if (match_basename(basename,
1001 pathlen - (basename - pathname),
1002 exclude, prefix, x->patternlen,
Nguyễn Thái Ngọc Duye6efecc2015-09-21 16:56:14 +07001003 x->flags)) {
1004 exc = x;
1005 break;
1006 }
Nguyễn Thái Ngọc Duy35a94d42012-05-26 19:31:12 +07001007 continue;
1008 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001009
Nguyễn Thái Ngọc Duyb5592632012-10-15 13:24:37 +07001010 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
1011 if (match_pathname(pathname, pathlen,
1012 x->base, x->baselen ? x->baselen - 1 : 0,
Nguyễn Thái Ngọc Duye6efecc2015-09-21 16:56:14 +07001013 exclude, prefix, x->patternlen, x->flags)) {
1014 exc = x;
1015 break;
1016 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001017 }
Nguyễn Thái Ngọc Duye6efecc2015-09-21 16:56:14 +07001018 return exc;
Adam Spiers578cd7c2012-12-27 02:32:26 +00001019}
1020
1021/*
1022 * Scan the list and let the last match determine the fate.
1023 * Return 1 for exclude, 0 for include and -1 for undecided.
1024 */
1025int is_excluded_from_list(const char *pathname,
1026 int pathlen, const char *basename, int *dtype,
Brandon Williamsfba92be2017-05-05 12:53:27 -07001027 struct exclude_list *el, struct index_state *istate)
Adam Spiers578cd7c2012-12-27 02:32:26 +00001028{
1029 struct exclude *exclude;
Brandon Williamsfba92be2017-05-05 12:53:27 -07001030 exclude = last_exclude_matching_from_list(pathname, pathlen, basename,
1031 dtype, el, istate);
Adam Spiers578cd7c2012-12-27 02:32:26 +00001032 if (exclude)
1033 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001034 return -1; /* undecided */
1035}
1036
Karsten Blees46aa2f92013-04-15 21:11:02 +02001037static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
Brandon Williams2b70e882017-05-05 12:53:26 -07001038 struct index_state *istate,
Karsten Blees46aa2f92013-04-15 21:11:02 +02001039 const char *pathname, int pathlen, const char *basename,
1040 int *dtype_p)
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001041{
Adam Spiersc082df22013-01-06 16:58:03 +00001042 int i, j;
1043 struct exclude_list_group *group;
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001044 struct exclude *exclude;
Adam Spiersc082df22013-01-06 16:58:03 +00001045 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1046 group = &dir->exclude_list_group[i];
1047 for (j = group->nr - 1; j >= 0; j--) {
1048 exclude = last_exclude_matching_from_list(
1049 pathname, pathlen, basename, dtype_p,
Brandon Williams2b70e882017-05-05 12:53:26 -07001050 &group->el[j], istate);
Adam Spiersc082df22013-01-06 16:58:03 +00001051 if (exclude)
1052 return exclude;
1053 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001054 }
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001055 return NULL;
1056}
1057
1058/*
Karsten Blees6cd5c582013-04-15 21:11:37 +02001059 * Loads the per-directory exclude list for the substring of base
1060 * which has a char length of baselen.
1061 */
Brandon Williamse799ed42017-05-05 12:53:29 -07001062static void prep_exclude(struct dir_struct *dir,
1063 struct index_state *istate,
1064 const char *base, int baselen)
Karsten Blees6cd5c582013-04-15 21:11:37 +02001065{
1066 struct exclude_list_group *group;
1067 struct exclude_list *el;
1068 struct exclude_stack *stk = NULL;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001069 struct untracked_cache_dir *untracked;
Karsten Blees6cd5c582013-04-15 21:11:37 +02001070 int current;
1071
Karsten Blees6cd5c582013-04-15 21:11:37 +02001072 group = &dir->exclude_list_group[EXC_DIRS];
1073
Nguyễn Thái Ngọc Duyd961baa2014-07-14 11:47:11 +02001074 /*
1075 * Pop the exclude lists from the EXCL_DIRS exclude_list_group
Karsten Blees6cd5c582013-04-15 21:11:37 +02001076 * which originate from directories not in the prefix of the
Nguyễn Thái Ngọc Duyd961baa2014-07-14 11:47:11 +02001077 * path being checked.
1078 */
Karsten Blees6cd5c582013-04-15 21:11:37 +02001079 while ((stk = dir->exclude_stack) != NULL) {
1080 if (stk->baselen <= baselen &&
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001081 !strncmp(dir->basebuf.buf, base, stk->baselen))
Karsten Blees6cd5c582013-04-15 21:11:37 +02001082 break;
1083 el = &group->el[dir->exclude_stack->exclude_ix];
1084 dir->exclude_stack = stk->prev;
Karsten Blees95c6f272013-04-15 21:12:14 +02001085 dir->exclude = NULL;
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001086 free((char *)el->src); /* see strbuf_detach() below */
Karsten Blees6cd5c582013-04-15 21:11:37 +02001087 clear_exclude_list(el);
1088 free(stk);
1089 group->nr--;
1090 }
1091
Karsten Blees95c6f272013-04-15 21:12:14 +02001092 /* Skip traversing into sub directories if the parent is excluded */
1093 if (dir->exclude)
1094 return;
1095
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001096 /*
1097 * Lazy initialization. All call sites currently just
1098 * memset(dir, 0, sizeof(*dir)) before use. Changing all of
1099 * them seems lots of work for little benefit.
1100 */
1101 if (!dir->basebuf.buf)
1102 strbuf_init(&dir->basebuf, PATH_MAX);
1103
Karsten Blees6cd5c582013-04-15 21:11:37 +02001104 /* Read from the parent directories and push them down. */
1105 current = stk ? stk->baselen : -1;
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001106 strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001107 if (dir->untracked)
1108 untracked = stk ? stk->ucd : dir->untracked->root;
1109 else
1110 untracked = NULL;
1111
Karsten Blees6cd5c582013-04-15 21:11:37 +02001112 while (current < baselen) {
Karsten Blees6cd5c582013-04-15 21:11:37 +02001113 const char *cp;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001114 struct sha1_stat sha1_stat;
Karsten Blees6cd5c582013-04-15 21:11:37 +02001115
Nguyễn Thái Ngọc Duy03e11a72014-10-21 18:38:06 +07001116 stk = xcalloc(1, sizeof(*stk));
Karsten Blees6cd5c582013-04-15 21:11:37 +02001117 if (current < 0) {
1118 cp = base;
1119 current = 0;
Nguyễn Thái Ngọc Duyd961baa2014-07-14 11:47:11 +02001120 } else {
Karsten Blees6cd5c582013-04-15 21:11:37 +02001121 cp = strchr(base + current + 1, '/');
1122 if (!cp)
1123 die("oops in prep_exclude");
1124 cp++;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001125 untracked =
1126 lookup_untracked(dir->untracked, untracked,
1127 base + current,
1128 cp - base - current);
Karsten Blees6cd5c582013-04-15 21:11:37 +02001129 }
1130 stk->prev = dir->exclude_stack;
1131 stk->baselen = cp - base;
Karsten Blees95c6f272013-04-15 21:12:14 +02001132 stk->exclude_ix = group->nr;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001133 stk->ucd = untracked;
Karsten Blees95c6f272013-04-15 21:12:14 +02001134 el = add_exclude_list(dir, EXC_DIRS, NULL);
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001135 strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
1136 assert(stk->baselen == dir->basebuf.len);
Karsten Blees95c6f272013-04-15 21:12:14 +02001137
1138 /* Abort if the directory is excluded */
1139 if (stk->baselen) {
1140 int dt = DT_DIR;
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001141 dir->basebuf.buf[stk->baselen - 1] = 0;
Karsten Blees95c6f272013-04-15 21:12:14 +02001142 dir->exclude = last_exclude_matching_from_lists(dir,
Brandon Williamse799ed42017-05-05 12:53:29 -07001143 istate,
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001144 dir->basebuf.buf, stk->baselen - 1,
1145 dir->basebuf.buf + current, &dt);
1146 dir->basebuf.buf[stk->baselen - 1] = '/';
Karsten Bleesc3c327d2013-05-29 22:32:36 +02001147 if (dir->exclude &&
1148 dir->exclude->flags & EXC_FLAG_NEGATIVE)
1149 dir->exclude = NULL;
Karsten Blees95c6f272013-04-15 21:12:14 +02001150 if (dir->exclude) {
Karsten Blees95c6f272013-04-15 21:12:14 +02001151 dir->exclude_stack = stk;
1152 return;
1153 }
1154 }
1155
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001156 /* Try to read per-directory file */
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001157 hashclr(sha1_stat.sha1);
1158 sha1_stat.valid = 0;
Nguyễn Thái Ngọc Duy27b099a2015-03-08 17:12:31 +07001159 if (dir->exclude_per_dir &&
1160 /*
1161 * If we know that no files have been added in
1162 * this directory (i.e. valid_cached_dir() has
1163 * been executed and set untracked->valid) ..
1164 */
1165 (!untracked || !untracked->valid ||
1166 /*
1167 * .. and .gitignore does not exist before
David Turner76872522015-07-31 13:35:01 -04001168 * (i.e. null exclude_sha1). Then we can skip
1169 * loading .gitignore, which would result in
1170 * ENOENT anyway.
Nguyễn Thái Ngọc Duy27b099a2015-03-08 17:12:31 +07001171 */
1172 !is_null_sha1(untracked->exclude_sha1))) {
Karsten Blees95c6f272013-04-15 21:12:14 +02001173 /*
1174 * dir->basebuf gets reused by the traversal, but we
1175 * need fname to remain unchanged to ensure the src
1176 * member of each struct exclude correctly
1177 * back-references its source file. Other invocations
1178 * of add_exclude_list provide stable strings, so we
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001179 * strbuf_detach() and free() here in the caller.
Karsten Blees95c6f272013-04-15 21:12:14 +02001180 */
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001181 struct strbuf sb = STRBUF_INIT;
1182 strbuf_addbuf(&sb, &dir->basebuf);
1183 strbuf_addstr(&sb, dir->exclude_per_dir);
1184 el->src = strbuf_detach(&sb, NULL);
Brandon Williamse799ed42017-05-05 12:53:29 -07001185 add_excludes(el->src, el->src, stk->baselen, el, istate,
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001186 untracked ? &sha1_stat : NULL);
1187 }
Nguyễn Thái Ngọc Duy5ebf79a2015-03-08 17:12:27 +07001188 /*
1189 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1190 * will first be called in valid_cached_dir() then maybe many
1191 * times more in last_exclude_matching(). When the cache is
1192 * used, last_exclude_matching() will not be called and
1193 * reading .gitignore content will be a waste.
1194 *
1195 * So when it's called by valid_cached_dir() and we can get
1196 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1197 * modified on work tree), we could delay reading the
1198 * .gitignore content until we absolutely need it in
1199 * last_exclude_matching(). Be careful about ignore rule
1200 * order, though, if you do that.
1201 */
1202 if (untracked &&
1203 hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1204 invalidate_gitignore(dir->untracked, untracked);
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001205 hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
Karsten Blees95c6f272013-04-15 21:12:14 +02001206 }
Karsten Blees6cd5c582013-04-15 21:11:37 +02001207 dir->exclude_stack = stk;
1208 current = stk->baselen;
1209 }
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02001210 strbuf_setlen(&dir->basebuf, baselen);
Karsten Blees6cd5c582013-04-15 21:11:37 +02001211}
1212
1213/*
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001214 * Loads the exclude lists for the directory containing pathname, then
1215 * scans all exclude lists to determine whether pathname is excluded.
Karsten Blees46aa2f92013-04-15 21:11:02 +02001216 * Returns the exclude_list element which matched, or NULL for
1217 * undecided.
1218 */
Karsten Bleesb07bc8c2013-04-15 21:12:57 +02001219struct exclude *last_exclude_matching(struct dir_struct *dir,
Brandon Williamsa0bba652017-05-05 12:53:30 -07001220 struct index_state *istate,
1221 const char *pathname,
1222 int *dtype_p)
Karsten Blees46aa2f92013-04-15 21:11:02 +02001223{
1224 int pathlen = strlen(pathname);
1225 const char *basename = strrchr(pathname, '/');
1226 basename = (basename) ? basename+1 : pathname;
1227
Brandon Williamsa0bba652017-05-05 12:53:30 -07001228 prep_exclude(dir, istate, pathname, basename-pathname);
Karsten Blees46aa2f92013-04-15 21:11:02 +02001229
Karsten Blees95c6f272013-04-15 21:12:14 +02001230 if (dir->exclude)
1231 return dir->exclude;
1232
Brandon Williamsa0bba652017-05-05 12:53:30 -07001233 return last_exclude_matching_from_lists(dir, istate, pathname, pathlen,
Karsten Blees46aa2f92013-04-15 21:11:02 +02001234 basename, dtype_p);
1235}
1236
1237/*
1238 * Loads the exclude lists for the directory containing pathname, then
1239 * scans all exclude lists to determine whether pathname is excluded.
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001240 * Returns 1 if true, otherwise 0.
1241 */
Brandon Williamsa0bba652017-05-05 12:53:30 -07001242int is_excluded(struct dir_struct *dir, struct index_state *istate,
1243 const char *pathname, int *dtype_p)
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001244{
1245 struct exclude *exclude =
Brandon Williamsa0bba652017-05-05 12:53:30 -07001246 last_exclude_matching(dir, istate, pathname, dtype_p);
Adam Spiersf4cd69a2012-12-27 02:32:27 +00001247 if (exclude)
1248 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001249 return 0;
1250}
1251
Junio C Hamanof3fa1832007-11-08 15:35:32 -08001252static struct dir_entry *dir_entry_new(const char *pathname, int len)
1253{
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001254 struct dir_entry *ent;
1255
Jeff King96ffc062016-02-22 17:44:32 -05001256 FLEX_ALLOC_MEM(ent, name, pathname, len);
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001257 ent->len = len;
Junio C Hamano4d06f8a2006-12-29 11:01:31 -08001258 return ent;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001259}
1260
Brandon Williams9e58bec2017-05-05 12:53:25 -07001261static struct dir_entry *dir_add_name(struct dir_struct *dir,
1262 struct index_state *istate,
1263 const char *pathname, int len)
Jeff King6815e562007-06-11 09:39:44 -04001264{
Brandon Williams9e58bec2017-05-05 12:53:25 -07001265 if (index_file_exists(istate, pathname, len, ignore_case))
Jeff King6815e562007-06-11 09:39:44 -04001266 return NULL;
1267
Jeff King25fd2f72007-06-16 18:43:40 -04001268 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
Jeff King6815e562007-06-11 09:39:44 -04001269 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1270}
1271
Brandon Williams9e58bec2017-05-05 12:53:25 -07001272struct dir_entry *dir_add_ignored(struct dir_struct *dir,
1273 struct index_state *istate,
1274 const char *pathname, int len)
Jeff King2abd31b2007-06-11 09:39:50 -04001275{
Brandon Williams9e58bec2017-05-05 12:53:25 -07001276 if (!index_name_is_other(istate, pathname, len))
Jeff King2abd31b2007-06-11 09:39:50 -04001277 return NULL;
1278
Jeff King25fd2f72007-06-16 18:43:40 -04001279 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
Jeff King2abd31b2007-06-11 09:39:50 -04001280 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1281}
1282
Linus Torvalds09595252007-04-11 14:49:44 -07001283enum exist_status {
1284 index_nonexistent = 0,
1285 index_directory,
Gary V. Vaughan4b055482010-05-14 09:31:35 +00001286 index_gitdir
Linus Torvalds09595252007-04-11 14:49:44 -07001287};
1288
1289/*
Junio C Hamano71261022013-08-15 12:08:45 -07001290 * Do not use the alphabetically sorted index to look up
Joshua Jensen5102c612010-10-03 09:56:43 +00001291 * the directory name; instead, use the case insensitive
Eric Sunshineebbd7432013-09-17 03:06:15 -04001292 * directory hash.
Joshua Jensen5102c612010-10-03 09:56:43 +00001293 */
Brandon Williamsae520e32017-05-05 12:53:23 -07001294static enum exist_status directory_exists_in_index_icase(struct index_state *istate,
1295 const char *dirname, int len)
Joshua Jensen5102c612010-10-03 09:56:43 +00001296{
David Turner41284eb2015-10-21 13:54:11 -04001297 struct cache_entry *ce;
Joshua Jensen5102c612010-10-03 09:56:43 +00001298
Brandon Williamsae520e32017-05-05 12:53:23 -07001299 if (index_dir_exists(istate, dirname, len))
Joshua Jensen5102c612010-10-03 09:56:43 +00001300 return index_directory;
1301
Brandon Williamsae520e32017-05-05 12:53:23 -07001302 ce = index_file_exists(istate, dirname, len, ignore_case);
David Turner41284eb2015-10-21 13:54:11 -04001303 if (ce && S_ISGITLINK(ce->ce_mode))
Joshua Jensen5102c612010-10-03 09:56:43 +00001304 return index_gitdir;
1305
Joshua Jensen5102c612010-10-03 09:56:43 +00001306 return index_nonexistent;
1307}
1308
1309/*
Linus Torvalds09595252007-04-11 14:49:44 -07001310 * The index sorts alphabetically by entry name, which
1311 * means that a gitlink sorts as '\0' at the end, while
1312 * a directory (which is defined not as an entry, but as
1313 * the files it contains) will sort with the '/' at the
1314 * end.
1315 */
Brandon Williamsae520e32017-05-05 12:53:23 -07001316static enum exist_status directory_exists_in_index(struct index_state *istate,
1317 const char *dirname, int len)
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001318{
Joshua Jensen5102c612010-10-03 09:56:43 +00001319 int pos;
1320
1321 if (ignore_case)
Brandon Williamsae520e32017-05-05 12:53:23 -07001322 return directory_exists_in_index_icase(istate, dirname, len);
Joshua Jensen5102c612010-10-03 09:56:43 +00001323
Brandon Williamsae520e32017-05-05 12:53:23 -07001324 pos = index_name_pos(istate, dirname, len);
Linus Torvalds09595252007-04-11 14:49:44 -07001325 if (pos < 0)
1326 pos = -pos-1;
Brandon Williamsae520e32017-05-05 12:53:23 -07001327 while (pos < istate->cache_nr) {
1328 const struct cache_entry *ce = istate->cache[pos++];
Linus Torvalds09595252007-04-11 14:49:44 -07001329 unsigned char endchar;
1330
1331 if (strncmp(ce->name, dirname, len))
1332 break;
1333 endchar = ce->name[len];
1334 if (endchar > '/')
1335 break;
1336 if (endchar == '/')
1337 return index_directory;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001338 if (!endchar && S_ISGITLINK(ce->ce_mode))
Linus Torvalds09595252007-04-11 14:49:44 -07001339 return index_gitdir;
1340 }
1341 return index_nonexistent;
1342}
1343
1344/*
1345 * When we find a directory when traversing the filesystem, we
1346 * have three distinct cases:
1347 *
1348 * - ignore it
1349 * - see it as a directory
1350 * - recurse into it
1351 *
1352 * and which one we choose depends on a combination of existing
1353 * git index contents and the flags passed into the directory
1354 * traversal routine.
1355 *
1356 * Case 1: If we *already* have entries in the index under that
Karsten Blees5bd8e2d2013-04-15 21:10:05 +02001357 * directory name, we always recurse into the directory to see
1358 * all the files.
Linus Torvalds09595252007-04-11 14:49:44 -07001359 *
1360 * Case 2: If we *already* have that directory name as a gitlink,
1361 * we always continue to see it as a gitlink, regardless of whether
1362 * there is an actual git directory there or not (it might not
1363 * be checked out as a subproject!)
1364 *
1365 * Case 3: if we didn't have it in the index previously, we
1366 * have a few sub-cases:
1367 *
1368 * (a) if "show_other_directories" is true, we show it as
1369 * just a directory, unless "hide_empty_directories" is
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001370 * also true, in which case we need to check if it contains any
1371 * untracked and / or ignored files.
Linus Torvalds09595252007-04-11 14:49:44 -07001372 * (b) if it looks like a git directory, and we don't have
Martin Waitz302b9282007-05-21 22:08:28 +02001373 * 'no_gitlinks' set we treat it as a gitlink, and show it
Linus Torvalds09595252007-04-11 14:49:44 -07001374 * as a directory.
1375 * (c) otherwise, we recurse into it.
1376 */
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001377static enum path_treatment treat_directory(struct dir_struct *dir,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001378 struct index_state *istate,
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001379 struct untracked_cache_dir *untracked,
David Turner2e5910f2015-08-19 20:01:25 +07001380 const char *dirname, int len, int baselen, int exclude,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001381 const struct pathspec *pathspec)
Linus Torvalds09595252007-04-11 14:49:44 -07001382{
1383 /* The "len-1" is to strip the final '/' */
Brandon Williams0ef8e162017-05-05 12:53:32 -07001384 switch (directory_exists_in_index(istate, dirname, len-1)) {
Linus Torvalds09595252007-04-11 14:49:44 -07001385 case index_directory:
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001386 return path_recurse;
Linus Torvalds09595252007-04-11 14:49:44 -07001387
1388 case index_gitdir:
Junio C Hamano26c986e2013-07-01 14:00:32 -07001389 return path_none;
Linus Torvalds09595252007-04-11 14:49:44 -07001390
1391 case index_nonexistent:
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +01001392 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
Linus Torvalds09595252007-04-11 14:49:44 -07001393 break;
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +01001394 if (!(dir->flags & DIR_NO_GITLINKS)) {
Linus Torvalds09595252007-04-11 14:49:44 -07001395 unsigned char sha1[20];
1396 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001397 return path_untracked;
Linus Torvalds09595252007-04-11 14:49:44 -07001398 }
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001399 return path_recurse;
Linus Torvalds09595252007-04-11 14:49:44 -07001400 }
1401
1402 /* This is the "show_other_directories" case */
Antoine Pelisse721ac4e2012-12-30 15:39:00 +01001403
Karsten Blees184d2a82013-04-15 21:08:02 +02001404 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001405 return exclude ? path_excluded : path_untracked;
1406
David Turner2e5910f2015-08-19 20:01:25 +07001407 untracked = lookup_untracked(dir->untracked, untracked,
1408 dirname + baselen, len - baselen);
Brandon Williams0ef8e162017-05-05 12:53:32 -07001409 return read_directory_recursive(dir, istate, dirname, len,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001410 untracked, 1, pathspec);
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001411}
1412
1413/*
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001414 * This is an inexact early pruning of any recursive directory
1415 * reading - if the path cannot possibly be in the pathspec,
1416 * return true, and we'll skip it early.
1417 */
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001418static int simplify_away(const char *path, int pathlen,
1419 const struct pathspec *pathspec)
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001420{
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001421 int i;
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001422
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001423 if (!pathspec || !pathspec->nr)
1424 return 0;
1425
1426 GUARD_PATHSPEC(pathspec,
1427 PATHSPEC_FROMTOP |
1428 PATHSPEC_MAXDEPTH |
1429 PATHSPEC_LITERAL |
1430 PATHSPEC_GLOB |
1431 PATHSPEC_ICASE |
Brandon Williamsb0db7042017-03-13 11:23:21 -07001432 PATHSPEC_EXCLUDE |
1433 PATHSPEC_ATTR);
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001434
1435 for (i = 0; i < pathspec->nr; i++) {
1436 const struct pathspec_item *item = &pathspec->items[i];
1437 int len = item->nowildcard_len;
1438
1439 if (len > pathlen)
1440 len = pathlen;
1441 if (!ps_strncmp(item, item->match, path, len))
1442 return 0;
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001443 }
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001444
1445 return 1;
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001446}
1447
Jeff King29209cb2010-03-11 02:15:43 -05001448/*
1449 * This function tells us whether an excluded path matches a
1450 * list of "interesting" pathspecs. That is, whether a path matched
1451 * by any of the pathspecs could possibly be ignored by excluding
1452 * the specified path. This can happen if:
1453 *
1454 * 1. the path is mentioned explicitly in the pathspec
1455 *
1456 * 2. the path is a directory prefix of some element in the
1457 * pathspec
1458 */
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001459static int exclude_matches_pathspec(const char *path, int pathlen,
1460 const struct pathspec *pathspec)
Jeff Kinge96980e2007-06-12 23:42:14 +02001461{
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001462 int i;
1463
1464 if (!pathspec || !pathspec->nr)
1465 return 0;
1466
1467 GUARD_PATHSPEC(pathspec,
1468 PATHSPEC_FROMTOP |
1469 PATHSPEC_MAXDEPTH |
1470 PATHSPEC_LITERAL |
1471 PATHSPEC_GLOB |
1472 PATHSPEC_ICASE |
1473 PATHSPEC_EXCLUDE);
1474
1475 for (i = 0; i < pathspec->nr; i++) {
1476 const struct pathspec_item *item = &pathspec->items[i];
1477 int len = item->nowildcard_len;
1478
1479 if (len == pathlen &&
1480 !ps_strncmp(item, item->match, path, pathlen))
1481 return 1;
1482 if (len > pathlen &&
1483 item->match[pathlen] == '/' &&
1484 !ps_strncmp(item, item->match, path, pathlen))
1485 return 1;
Jeff Kinge96980e2007-06-12 23:42:14 +02001486 }
1487 return 0;
1488}
1489
Brandon Williams98f2a682017-05-05 12:53:24 -07001490static int get_index_dtype(struct index_state *istate,
1491 const char *path, int len)
Linus Torvalds443e0612009-07-09 13:14:28 -07001492{
1493 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001494 const struct cache_entry *ce;
Linus Torvalds443e0612009-07-09 13:14:28 -07001495
Brandon Williams98f2a682017-05-05 12:53:24 -07001496 ce = index_file_exists(istate, path, len, 0);
Linus Torvalds443e0612009-07-09 13:14:28 -07001497 if (ce) {
1498 if (!ce_uptodate(ce))
1499 return DT_UNKNOWN;
1500 if (S_ISGITLINK(ce->ce_mode))
1501 return DT_DIR;
1502 /*
1503 * Nobody actually cares about the
1504 * difference between DT_LNK and DT_REG
1505 */
1506 return DT_REG;
1507 }
1508
1509 /* Try to look it up as a directory */
Brandon Williams98f2a682017-05-05 12:53:24 -07001510 pos = index_name_pos(istate, path, len);
Linus Torvalds443e0612009-07-09 13:14:28 -07001511 if (pos >= 0)
1512 return DT_UNKNOWN;
1513 pos = -pos-1;
Brandon Williams98f2a682017-05-05 12:53:24 -07001514 while (pos < istate->cache_nr) {
1515 ce = istate->cache[pos++];
Linus Torvalds443e0612009-07-09 13:14:28 -07001516 if (strncmp(ce->name, path, len))
1517 break;
1518 if (ce->name[len] > '/')
1519 break;
1520 if (ce->name[len] < '/')
1521 continue;
1522 if (!ce_uptodate(ce))
1523 break; /* continue? */
1524 return DT_DIR;
1525 }
1526 return DT_UNKNOWN;
1527}
1528
Brandon Williams98f2a682017-05-05 12:53:24 -07001529static int get_dtype(struct dirent *de, struct index_state *istate,
1530 const char *path, int len)
Linus Torvalds07134422007-10-19 10:59:22 -07001531{
Junio C Hamano6831a882008-01-31 20:23:25 -08001532 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
Linus Torvalds07134422007-10-19 10:59:22 -07001533 struct stat st;
1534
1535 if (dtype != DT_UNKNOWN)
1536 return dtype;
Brandon Williams98f2a682017-05-05 12:53:24 -07001537 dtype = get_index_dtype(istate, path, len);
Linus Torvalds443e0612009-07-09 13:14:28 -07001538 if (dtype != DT_UNKNOWN)
1539 return dtype;
1540 if (lstat(path, &st))
Linus Torvalds07134422007-10-19 10:59:22 -07001541 return dtype;
1542 if (S_ISREG(st.st_mode))
1543 return DT_REG;
1544 if (S_ISDIR(st.st_mode))
1545 return DT_DIR;
1546 if (S_ISLNK(st.st_mode))
1547 return DT_LNK;
1548 return dtype;
1549}
1550
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001551static enum path_treatment treat_one_path(struct dir_struct *dir,
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001552 struct untracked_cache_dir *untracked,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001553 struct index_state *istate,
René Scharfe49dc2cc2012-05-01 13:25:24 +02001554 struct strbuf *path,
David Turner2e5910f2015-08-19 20:01:25 +07001555 int baselen,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001556 const struct pathspec *pathspec,
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001557 int dtype, struct dirent *de)
Junio C Hamano53cc5352010-01-08 19:14:07 -08001558{
Karsten Blees8aaf8d72013-04-15 21:13:35 +02001559 int exclude;
Brandon Williams0ef8e162017-05-05 12:53:32 -07001560 int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);
Junio C Hamano2eac2a42013-08-15 12:13:46 -07001561
Karsten Blees8aaf8d72013-04-15 21:13:35 +02001562 if (dtype == DT_UNKNOWN)
Brandon Williams0ef8e162017-05-05 12:53:32 -07001563 dtype = get_dtype(de, istate, path->buf, path->len);
Karsten Blees8aaf8d72013-04-15 21:13:35 +02001564
1565 /* Always exclude indexed files */
Junio C Hamano2eac2a42013-08-15 12:13:46 -07001566 if (dtype != DT_DIR && has_path_in_index)
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001567 return path_none;
Karsten Blees8aaf8d72013-04-15 21:13:35 +02001568
Junio C Hamano2eac2a42013-08-15 12:13:46 -07001569 /*
1570 * When we are looking at a directory P in the working tree,
1571 * there are three cases:
1572 *
1573 * (1) P exists in the index. Everything inside the directory P in
1574 * the working tree needs to go when P is checked out from the
1575 * index.
1576 *
1577 * (2) P does not exist in the index, but there is P/Q in the index.
1578 * We know P will stay a directory when we check out the contents
1579 * of the index, but we do not know yet if there is a directory
1580 * P/Q in the working tree to be killed, so we need to recurse.
1581 *
1582 * (3) P does not exist in the index, and there is no P/Q in the index
1583 * to require P to be a directory, either. Only in this case, we
1584 * know that everything inside P will not be killed without
1585 * recursing.
1586 */
1587 if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
1588 (dtype == DT_DIR) &&
Eric Sunshinede372b12013-09-17 03:06:17 -04001589 !has_path_in_index &&
Brandon Williams0ef8e162017-05-05 12:53:32 -07001590 (directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))
Eric Sunshinede372b12013-09-17 03:06:17 -04001591 return path_none;
Karsten Blees8aaf8d72013-04-15 21:13:35 +02001592
Brandon Williams0ef8e162017-05-05 12:53:32 -07001593 exclude = is_excluded(dir, istate, path->buf, &dtype);
Junio C Hamano53cc5352010-01-08 19:14:07 -08001594
1595 /*
1596 * Excluded? If we don't explicitly want to show
1597 * ignored files, ignore it
1598 */
Karsten Blees0aaf62b2013-04-15 21:15:03 +02001599 if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001600 return path_excluded;
Junio C Hamano53cc5352010-01-08 19:14:07 -08001601
Junio C Hamano53cc5352010-01-08 19:14:07 -08001602 switch (dtype) {
1603 default:
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001604 return path_none;
Junio C Hamano53cc5352010-01-08 19:14:07 -08001605 case DT_DIR:
René Scharfe49dc2cc2012-05-01 13:25:24 +02001606 strbuf_addch(path, '/');
Brandon Williams0ef8e162017-05-05 12:53:32 -07001607 return treat_directory(dir, istate, untracked, path->buf, path->len,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001608 baselen, exclude, pathspec);
Junio C Hamano53cc5352010-01-08 19:14:07 -08001609 case DT_REG:
1610 case DT_LNK:
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001611 return exclude ? path_excluded : path_untracked;
Junio C Hamano53cc5352010-01-08 19:14:07 -08001612 }
Junio C Hamano53cc5352010-01-08 19:14:07 -08001613}
1614
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001615static enum path_treatment treat_path_fast(struct dir_struct *dir,
1616 struct untracked_cache_dir *untracked,
1617 struct cached_dir *cdir,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001618 struct index_state *istate,
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001619 struct strbuf *path,
1620 int baselen,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001621 const struct pathspec *pathspec)
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001622{
1623 strbuf_setlen(path, baselen);
1624 if (!cdir->ucd) {
1625 strbuf_addstr(path, cdir->file);
1626 return path_untracked;
1627 }
1628 strbuf_addstr(path, cdir->ucd->name);
1629 /* treat_one_path() does this before it calls treat_directory() */
Jeff King00b6c172015-09-24 17:08:35 -04001630 strbuf_complete(path, '/');
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001631 if (cdir->ucd->check_only)
1632 /*
1633 * check_only is set as a result of treat_directory() getting
1634 * to its bottom. Verify again the same set of directories
1635 * with check_only set.
1636 */
Brandon Williams0ef8e162017-05-05 12:53:32 -07001637 return read_directory_recursive(dir, istate, path->buf, path->len,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001638 cdir->ucd, 1, pathspec);
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001639 /*
1640 * We get path_recurse in the first run when
1641 * directory_exists_in_index() returns index_nonexistent. We
1642 * are sure that new changes in the index does not impact the
1643 * outcome. Return now.
1644 */
1645 return path_recurse;
1646}
1647
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001648static enum path_treatment treat_path(struct dir_struct *dir,
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001649 struct untracked_cache_dir *untracked,
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001650 struct cached_dir *cdir,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001651 struct index_state *istate,
René Scharfe49dc2cc2012-05-01 13:25:24 +02001652 struct strbuf *path,
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001653 int baselen,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001654 const struct pathspec *pathspec)
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001655{
1656 int dtype;
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001657 struct dirent *de = cdir->de;
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001658
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001659 if (!de)
Brandon Williams0ef8e162017-05-05 12:53:32 -07001660 return treat_path_fast(dir, untracked, cdir, istate, path,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001661 baselen, pathspec);
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001662 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001663 return path_none;
René Scharfe49dc2cc2012-05-01 13:25:24 +02001664 strbuf_setlen(path, baselen);
1665 strbuf_addstr(path, de->d_name);
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001666 if (simplify_away(path->buf, path->len, pathspec))
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001667 return path_none;
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001668
1669 dtype = DTYPE(de);
Brandon Williams0ef8e162017-05-05 12:53:32 -07001670 return treat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001671}
1672
1673static void add_untracked(struct untracked_cache_dir *dir, const char *name)
1674{
1675 if (!dir)
1676 return;
1677 ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,
1678 dir->untracked_alloc);
1679 dir->untracked[dir->untracked_nr++] = xstrdup(name);
Junio C Hamano16e2cfa2010-01-08 20:56:16 -08001680}
1681
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001682static int valid_cached_dir(struct dir_struct *dir,
1683 struct untracked_cache_dir *untracked,
Brandon Williams207a06c2017-05-05 12:53:31 -07001684 struct index_state *istate,
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001685 struct strbuf *path,
1686 int check_only)
1687{
1688 struct stat st;
1689
1690 if (!untracked)
1691 return 0;
1692
1693 if (stat(path->len ? path->buf : ".", &st)) {
1694 invalidate_directory(dir->untracked, untracked);
1695 memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
1696 return 0;
1697 }
1698 if (!untracked->valid ||
Brandon Williams207a06c2017-05-05 12:53:31 -07001699 match_stat_data_racy(istate, &untracked->stat_data, &st)) {
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001700 if (untracked->valid)
1701 invalidate_directory(dir->untracked, untracked);
1702 fill_stat_data(&untracked->stat_data, &st);
1703 return 0;
1704 }
1705
1706 if (untracked->check_only != !!check_only) {
1707 invalidate_directory(dir->untracked, untracked);
1708 return 0;
1709 }
1710
1711 /*
1712 * prep_exclude will be called eventually on this directory,
1713 * but it's called much later in last_exclude_matching(). We
1714 * need it now to determine the validity of the cache for this
1715 * path. The next calls will be nearly no-op, the way
1716 * prep_exclude() is designed.
1717 */
1718 if (path->len && path->buf[path->len - 1] != '/') {
1719 strbuf_addch(path, '/');
Brandon Williams207a06c2017-05-05 12:53:31 -07001720 prep_exclude(dir, istate, path->buf, path->len);
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001721 strbuf_setlen(path, path->len - 1);
1722 } else
Brandon Williams207a06c2017-05-05 12:53:31 -07001723 prep_exclude(dir, istate, path->buf, path->len);
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001724
1725 /* hopefully prep_exclude() haven't invalidated this entry... */
1726 return untracked->valid;
1727}
1728
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001729static int open_cached_dir(struct cached_dir *cdir,
1730 struct dir_struct *dir,
1731 struct untracked_cache_dir *untracked,
Brandon Williams207a06c2017-05-05 12:53:31 -07001732 struct index_state *istate,
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001733 struct strbuf *path,
1734 int check_only)
1735{
1736 memset(cdir, 0, sizeof(*cdir));
1737 cdir->untracked = untracked;
Brandon Williams207a06c2017-05-05 12:53:31 -07001738 if (valid_cached_dir(dir, untracked, istate, path, check_only))
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001739 return 0;
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001740 cdir->fdir = opendir(path->len ? path->buf : ".");
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001741 if (dir->untracked)
1742 dir->untracked->dir_opened++;
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001743 if (!cdir->fdir)
1744 return -1;
1745 return 0;
1746}
1747
1748static int read_cached_dir(struct cached_dir *cdir)
1749{
1750 if (cdir->fdir) {
1751 cdir->de = readdir(cdir->fdir);
1752 if (!cdir->de)
1753 return -1;
1754 return 0;
1755 }
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001756 while (cdir->nr_dirs < cdir->untracked->dirs_nr) {
1757 struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +07001758 if (!d->recurse) {
1759 cdir->nr_dirs++;
1760 continue;
1761 }
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001762 cdir->ucd = d;
1763 cdir->nr_dirs++;
1764 return 0;
1765 }
1766 cdir->ucd = NULL;
1767 if (cdir->nr_files < cdir->untracked->untracked_nr) {
1768 struct untracked_cache_dir *d = cdir->untracked;
1769 cdir->file = d->untracked[cdir->nr_files++];
1770 return 0;
1771 }
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001772 return -1;
1773}
1774
1775static void close_cached_dir(struct cached_dir *cdir)
1776{
1777 if (cdir->fdir)
1778 closedir(cdir->fdir);
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001779 /*
1780 * We have gone through this directory and found no untracked
1781 * entries. Mark it valid.
1782 */
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +07001783 if (cdir->untracked) {
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001784 cdir->untracked->valid = 1;
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +07001785 cdir->untracked->recurse = 1;
1786 }
Linus Torvalds9fc42d62007-03-30 20:39:30 -07001787}
1788
1789/*
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001790 * Read a directory tree. We currently ignore anything but
1791 * directories, regular files and symlinks. That's because git
1792 * doesn't handle them at all yet. Maybe that will change some
1793 * day.
1794 *
1795 * Also, we ignore the name ".git" (even if it is not a directory).
1796 * That likely will not change.
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001797 *
1798 * Returns the most significant path_treatment value encountered in the scan.
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001799 */
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001800static enum path_treatment read_directory_recursive(struct dir_struct *dir,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001801 struct index_state *istate, const char *base, int baselen,
1802 struct untracked_cache_dir *untracked, int check_only,
1803 const struct pathspec *pathspec)
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001804{
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001805 struct cached_dir cdir;
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001806 enum path_treatment state, subdir_state, dir_state = path_none;
Junio C Hamanobef36922012-05-08 09:43:40 -07001807 struct strbuf path = STRBUF_INIT;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001808
Junio C Hamanobef36922012-05-08 09:43:40 -07001809 strbuf_add(&path, base, baselen);
Nguyễn Thái Ngọc Duy02cb6752011-10-24 17:36:11 +11001810
Brandon Williams0ef8e162017-05-05 12:53:32 -07001811 if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))
René Scharfe1528d242012-05-11 16:53:07 +02001812 goto out;
1813
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001814 if (untracked)
1815 untracked->check_only = !!check_only;
1816
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001817 while (!read_cached_dir(&cdir)) {
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001818 /* check how the file or directory should be treated */
Brandon Williams0ef8e162017-05-05 12:53:32 -07001819 state = treat_path(dir, untracked, &cdir, istate, &path,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001820 baselen, pathspec);
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001821
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001822 if (state > dir_state)
1823 dir_state = state;
1824
1825 /* recurse into subdir if instructed by treat_path */
Samuel Lijindf5bcdf2017-05-18 04:21:51 -04001826 if ((state == path_recurse) ||
1827 ((state == path_untracked) &&
1828 (dir->flags & DIR_SHOW_IGNORED_TOO) &&
Junio C Hamanof4fd99b2017-06-02 15:06:04 +09001829 (get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001830 struct untracked_cache_dir *ud;
1831 ud = lookup_untracked(dir->untracked, untracked,
1832 path.buf + baselen,
1833 path.len - baselen);
1834 subdir_state =
Brandon Williams0ef8e162017-05-05 12:53:32 -07001835 read_directory_recursive(dir, istate, path.buf,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001836 path.len, ud,
1837 check_only, pathspec);
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001838 if (subdir_state > dir_state)
1839 dir_state = subdir_state;
1840 }
1841
1842 if (check_only) {
1843 /* abort early if maximum state has been reached */
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001844 if (dir_state == path_untracked) {
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001845 if (cdir.fdir)
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001846 add_untracked(untracked, path.buf + baselen);
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001847 break;
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001848 }
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001849 /* skip the dir_add_* part */
Nguyễn Thái Ngọc Duy02cb6752011-10-24 17:36:11 +11001850 continue;
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001851 }
1852
1853 /* add the path to the appropriate result list */
1854 switch (state) {
1855 case path_excluded:
1856 if (dir->flags & DIR_SHOW_IGNORED)
Brandon Williams0ef8e162017-05-05 12:53:32 -07001857 dir_add_name(dir, istate, path.buf, path.len);
Karsten Blees0aaf62b2013-04-15 21:15:03 +02001858 else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||
1859 ((dir->flags & DIR_COLLECT_IGNORED) &&
1860 exclude_matches_pathspec(path.buf, path.len,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001861 pathspec)))
Brandon Williams0ef8e162017-05-05 12:53:32 -07001862 dir_add_ignored(dir, istate, path.buf, path.len);
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001863 break;
1864
1865 case path_untracked:
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001866 if (dir->flags & DIR_SHOW_IGNORED)
1867 break;
Brandon Williams0ef8e162017-05-05 12:53:32 -07001868 dir_add_name(dir, istate, path.buf, path.len);
Nguyễn Thái Ngọc Duy91a22882015-03-08 17:12:29 +07001869 if (cdir.fdir)
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07001870 add_untracked(untracked, path.buf + baselen);
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001871 break;
1872
1873 default:
Nguyễn Thái Ngọc Duy02cb6752011-10-24 17:36:11 +11001874 break;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001875 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001876 }
Nguyễn Thái Ngọc Duycf7c6142015-03-08 17:12:28 +07001877 close_cached_dir(&cdir);
René Scharfe1528d242012-05-11 16:53:07 +02001878 out:
Junio C Hamanobef36922012-05-08 09:43:40 -07001879 strbuf_release(&path);
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001880
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001881 return dir_state;
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001882}
1883
Samuel Lijinbbf504a2017-05-18 04:21:53 -04001884int cmp_dir_entry(const void *p1, const void *p2)
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001885{
1886 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1887 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1888
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -07001889 return name_compare(e1->name, e1->len, e2->name, e2->len);
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001890}
1891
Samuel Lijinfb898882017-05-18 04:21:52 -04001892/* check if *out lexically strictly contains *in */
Samuel Lijinbbf504a2017-05-18 04:21:53 -04001893int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in)
Samuel Lijinfb898882017-05-18 04:21:52 -04001894{
1895 return (out->len < in->len) &&
1896 (out->name[out->len - 1] == '/') &&
1897 !memcmp(out->name, in->name, out->len);
1898}
1899
Junio C Hamano48ffef92010-01-08 23:05:41 -08001900static int treat_leading_path(struct dir_struct *dir,
Brandon Williams0ef8e162017-05-05 12:53:32 -07001901 struct index_state *istate,
Junio C Hamano48ffef92010-01-08 23:05:41 -08001902 const char *path, int len,
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001903 const struct pathspec *pathspec)
Junio C Hamano48ffef92010-01-08 23:05:41 -08001904{
René Scharfe49dc2cc2012-05-01 13:25:24 +02001905 struct strbuf sb = STRBUF_INIT;
1906 int baselen, rc = 0;
Junio C Hamano48ffef92010-01-08 23:05:41 -08001907 const char *cp;
Karsten Bleesbe8a84c2013-04-15 21:09:25 +02001908 int old_flags = dir->flags;
Junio C Hamano48ffef92010-01-08 23:05:41 -08001909
1910 while (len && path[len - 1] == '/')
1911 len--;
1912 if (!len)
1913 return 1;
1914 baselen = 0;
Karsten Bleesbe8a84c2013-04-15 21:09:25 +02001915 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
Junio C Hamano48ffef92010-01-08 23:05:41 -08001916 while (1) {
1917 cp = path + baselen + !!baselen;
1918 cp = memchr(cp, '/', path + len - cp);
1919 if (!cp)
1920 baselen = len;
1921 else
1922 baselen = cp - path;
René Scharfe49dc2cc2012-05-01 13:25:24 +02001923 strbuf_setlen(&sb, 0);
1924 strbuf_add(&sb, path, baselen);
1925 if (!is_directory(sb.buf))
1926 break;
Brandon Williamse1b8c7b2017-01-04 10:03:57 -08001927 if (simplify_away(sb.buf, sb.len, pathspec))
René Scharfe49dc2cc2012-05-01 13:25:24 +02001928 break;
Brandon Williams0ef8e162017-05-05 12:53:32 -07001929 if (treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,
Karsten Bleesdefd7c72013-04-15 21:14:22 +02001930 DT_DIR, NULL) == path_none)
René Scharfe49dc2cc2012-05-01 13:25:24 +02001931 break; /* do not recurse into it */
1932 if (len <= baselen) {
1933 rc = 1;
1934 break; /* finished checking */
1935 }
Junio C Hamano48ffef92010-01-08 23:05:41 -08001936 }
René Scharfe49dc2cc2012-05-01 13:25:24 +02001937 strbuf_release(&sb);
Karsten Bleesbe8a84c2013-04-15 21:09:25 +02001938 dir->flags = old_flags;
René Scharfe49dc2cc2012-05-01 13:25:24 +02001939 return rc;
Junio C Hamano48ffef92010-01-08 23:05:41 -08001940}
1941
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001942static const char *get_ident_string(void)
1943{
1944 static struct strbuf sb = STRBUF_INIT;
1945 struct utsname uts;
1946
1947 if (sb.len)
1948 return sb.buf;
Charles Bailey100e4332015-07-17 18:09:41 +01001949 if (uname(&uts) < 0)
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001950 die_errno(_("failed to get kernel name and information"));
Christian Couder0e0f7612016-01-24 16:28:21 +01001951 strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(),
1952 uts.sysname);
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001953 return sb.buf;
1954}
1955
1956static int ident_in_untracked(const struct untracked_cache *uc)
1957{
Christian Couder0e0f7612016-01-24 16:28:21 +01001958 /*
1959 * Previous git versions may have saved many NUL separated
1960 * strings in the "ident" field, but it is insane to manage
1961 * many locations, so just take care of the first one.
1962 */
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001963
Christian Couder0e0f7612016-01-24 16:28:21 +01001964 return !strcmp(uc->ident.buf, get_ident_string());
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001965}
1966
Christian Couder0e0f7612016-01-24 16:28:21 +01001967static void set_untracked_ident(struct untracked_cache *uc)
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001968{
Christian Couder0e0f7612016-01-24 16:28:21 +01001969 strbuf_reset(&uc->ident);
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001970 strbuf_addstr(&uc->ident, get_ident_string());
Christian Couder0e0f7612016-01-24 16:28:21 +01001971
1972 /*
1973 * This strbuf used to contain a list of NUL separated
1974 * strings, so save NUL too for backward compatibility.
1975 */
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07001976 strbuf_addch(&uc->ident, 0);
1977}
1978
Christian Couder4a4ca472016-01-24 16:28:19 +01001979static void new_untracked_cache(struct index_state *istate)
1980{
1981 struct untracked_cache *uc = xcalloc(1, sizeof(*uc));
1982 strbuf_init(&uc->ident, 100);
1983 uc->exclude_per_dir = ".gitignore";
1984 /* should be the same flags used by git-status */
1985 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Christian Couder0e0f7612016-01-24 16:28:21 +01001986 set_untracked_ident(uc);
Christian Couder4a4ca472016-01-24 16:28:19 +01001987 istate->untracked = uc;
Christian Couder0e0f7612016-01-24 16:28:21 +01001988 istate->cache_changed |= UNTRACKED_CHANGED;
Christian Couder4a4ca472016-01-24 16:28:19 +01001989}
1990
1991void add_untracked_cache(struct index_state *istate)
1992{
1993 if (!istate->untracked) {
1994 new_untracked_cache(istate);
Christian Couder0e0f7612016-01-24 16:28:21 +01001995 } else {
1996 if (!ident_in_untracked(istate->untracked)) {
1997 free_untracked_cache(istate->untracked);
1998 new_untracked_cache(istate);
1999 }
2000 }
Christian Couder4a4ca472016-01-24 16:28:19 +01002001}
2002
Christian Couder07b29bf2016-01-24 16:28:20 +01002003void remove_untracked_cache(struct index_state *istate)
2004{
2005 if (istate->untracked) {
2006 free_untracked_cache(istate->untracked);
2007 istate->untracked = NULL;
2008 istate->cache_changed |= UNTRACKED_CHANGED;
2009 }
2010}
2011
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002012static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,
2013 int base_len,
2014 const struct pathspec *pathspec)
2015{
2016 struct untracked_cache_dir *root;
2017
Nguyễn Thái Ngọc Duy76e6b092015-03-08 17:12:40 +07002018 if (!dir->untracked || getenv("GIT_DISABLE_UNTRACKED_CACHE"))
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002019 return NULL;
2020
2021 /*
2022 * We only support $GIT_DIR/info/exclude and core.excludesfile
2023 * as the global ignore rule files. Any other additions
2024 * (e.g. from command line) invalidate the cache. This
2025 * condition also catches running setup_standard_excludes()
2026 * before setting dir->untracked!
2027 */
2028 if (dir->unmanaged_exclude_files)
2029 return NULL;
2030
2031 /*
2032 * Optimize for the main use case only: whole-tree git
2033 * status. More work involved in treat_leading_path() if we
2034 * use cache on just a subset of the worktree. pathspec
2035 * support could make the matter even worse.
2036 */
2037 if (base_len || (pathspec && pathspec->nr))
2038 return NULL;
2039
2040 /* Different set of flags may produce different results */
2041 if (dir->flags != dir->untracked->dir_flags ||
2042 /*
2043 * See treat_directory(), case index_nonexistent. Without
2044 * this flag, we may need to also cache .git file content
2045 * for the resolve_gitlink_ref() call, which we don't.
2046 */
2047 !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||
2048 /* We don't support collecting ignore files */
2049 (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |
2050 DIR_COLLECT_IGNORED)))
2051 return NULL;
2052
2053 /*
2054 * If we use .gitignore in the cache and now you change it to
2055 * .gitexclude, everything will go wrong.
2056 */
2057 if (dir->exclude_per_dir != dir->untracked->exclude_per_dir &&
2058 strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))
2059 return NULL;
2060
2061 /*
2062 * EXC_CMDL is not considered in the cache. If people set it,
2063 * skip the cache.
2064 */
2065 if (dir->exclude_list_group[EXC_CMDL].nr)
2066 return NULL;
2067
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002068 if (!ident_in_untracked(dir->untracked)) {
Christian Couder0e0f7612016-01-24 16:28:21 +01002069 warning(_("Untracked cache is disabled on this system or location."));
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002070 return NULL;
2071 }
2072
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002073 if (!dir->untracked->root) {
2074 const int len = sizeof(*dir->untracked->root);
2075 dir->untracked->root = xmalloc(len);
2076 memset(dir->untracked->root, 0, len);
2077 }
2078
2079 /* Validate $GIT_DIR/info/exclude and core.excludesfile */
2080 root = dir->untracked->root;
2081 if (hashcmp(dir->ss_info_exclude.sha1,
2082 dir->untracked->ss_info_exclude.sha1)) {
2083 invalidate_gitignore(dir->untracked, root);
2084 dir->untracked->ss_info_exclude = dir->ss_info_exclude;
2085 }
2086 if (hashcmp(dir->ss_excludes_file.sha1,
2087 dir->untracked->ss_excludes_file.sha1)) {
2088 invalidate_gitignore(dir->untracked, root);
2089 dir->untracked->ss_excludes_file = dir->ss_excludes_file;
2090 }
Nguyễn Thái Ngọc Duy26cb0182015-03-08 17:12:30 +07002091
2092 /* Make sure this directory is not dropped out at saving phase */
2093 root->recurse = 1;
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002094 return root;
2095}
2096
Brandon Williams2c1eb102017-05-05 12:53:33 -07002097int read_directory(struct dir_struct *dir, struct index_state *istate,
2098 const char *path, int len, const struct pathspec *pathspec)
Linus Torvalds9fc42d62007-03-30 20:39:30 -07002099{
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002100 struct untracked_cache_dir *untracked;
Linus Torvaldsb4189aa2006-05-16 19:46:16 -07002101
Linus Torvaldsdba2e202009-07-08 19:24:39 -07002102 if (has_symlink_leading_path(path, len))
Junio C Hamano725b0602008-08-04 00:52:37 -07002103 return dir->nr;
2104
Nguyễn Thái Ngọc Duyccad2612015-03-08 17:12:26 +07002105 untracked = validate_untracked_cache(dir, len, pathspec);
2106 if (!untracked)
2107 /*
2108 * make sure untracked cache code path is disabled,
2109 * e.g. prep_exclude()
2110 */
2111 dir->untracked = NULL;
Brandon Williams2c1eb102017-05-05 12:53:33 -07002112 if (!len || treat_leading_path(dir, istate, path, len, pathspec))
2113 read_directory_recursive(dir, istate, path, len, untracked, 0, pathspec);
Samuel Lijinbbf504a2017-05-18 04:21:53 -04002114 QSORT(dir->entries, dir->nr, cmp_dir_entry);
2115 QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);
Samuel Lijinfb898882017-05-18 04:21:52 -04002116
2117 /*
2118 * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will
2119 * also pick up untracked contents of untracked dirs; by default
2120 * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.
2121 */
2122 if ((dir->flags & DIR_SHOW_IGNORED_TOO) &&
2123 !(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {
2124 int i, j;
2125
2126 /* remove from dir->entries untracked contents of untracked dirs */
2127 for (i = j = 0; j < dir->nr; j++) {
Samuel Lijinbbf504a2017-05-18 04:21:53 -04002128 if (i &&
2129 check_dir_entry_contains(dir->entries[i - 1], dir->entries[j])) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002130 FREE_AND_NULL(dir->entries[j]);
Samuel Lijinfb898882017-05-18 04:21:52 -04002131 } else {
2132 dir->entries[i++] = dir->entries[j];
2133 }
2134 }
2135
2136 dir->nr = i;
2137 }
2138
Nguyễn Thái Ngọc Duyc9ccb5d2015-03-08 17:12:38 +07002139 if (dir->untracked) {
2140 static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
2141 trace_printf_key(&trace_untracked_stats,
2142 "node creation: %u\n"
2143 "gitignore invalidation: %u\n"
2144 "directory invalidation: %u\n"
2145 "opendir: %u\n",
2146 dir->untracked->dir_created,
2147 dir->untracked->gitignore_invalidated,
2148 dir->untracked->dir_invalidated,
2149 dir->untracked->dir_opened);
Brandon Williams2c1eb102017-05-05 12:53:33 -07002150 if (dir->untracked == istate->untracked &&
Nguyễn Thái Ngọc Duy1bbb3db2015-03-08 17:12:39 +07002151 (dir->untracked->dir_opened ||
2152 dir->untracked->gitignore_invalidated ||
2153 dir->untracked->dir_invalidated))
Brandon Williams2c1eb102017-05-05 12:53:33 -07002154 istate->cache_changed |= UNTRACKED_CHANGED;
2155 if (dir->untracked != istate->untracked) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002156 FREE_AND_NULL(dir->untracked);
Nguyễn Thái Ngọc Duy1bbb3db2015-03-08 17:12:39 +07002157 }
Nguyễn Thái Ngọc Duyc9ccb5d2015-03-08 17:12:38 +07002158 }
Linus Torvalds453ec4b2006-05-16 19:02:14 -07002159 return dir->nr;
2160}
Jeff Kingc91f0d92006-09-08 04:05:34 -04002161
Junio C Hamano686a4a02007-11-29 01:11:46 -08002162int file_exists(const char *f)
Jeff Kingc91f0d92006-09-08 04:05:34 -04002163{
Junio C Hamano686a4a02007-11-29 01:11:46 -08002164 struct stat sb;
Junio C Hamanoa50f9fc52007-11-18 01:58:16 -08002165 return lstat(f, &sb) == 0;
Jeff Kingc91f0d92006-09-08 04:05:34 -04002166}
Johannes Schindeline6636742007-08-01 01:29:17 +01002167
Johannes Schindelin63ec5e12015-09-28 18:12:18 +02002168static int cmp_icase(char a, char b)
2169{
2170 if (a == b)
2171 return 0;
2172 if (ignore_case)
2173 return toupper(a) - toupper(b);
2174 return a - b;
2175}
2176
Johannes Schindeline6636742007-08-01 01:29:17 +01002177/*
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002178 * Given two normalized paths (a trailing slash is ok), if subdir is
2179 * outside dir, return -1. Otherwise return the offset in subdir that
2180 * can be used as relative path to dir.
Johannes Schindeline6636742007-08-01 01:29:17 +01002181 */
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002182int dir_inside_of(const char *subdir, const char *dir)
Johannes Schindeline6636742007-08-01 01:29:17 +01002183{
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002184 int offset = 0;
Johannes Schindeline6636742007-08-01 01:29:17 +01002185
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002186 assert(dir && subdir && *dir && *subdir);
Johannes Schindeline6636742007-08-01 01:29:17 +01002187
Johannes Schindelin63ec5e12015-09-28 18:12:18 +02002188 while (*dir && *subdir && !cmp_icase(*dir, *subdir)) {
Johannes Schindeline6636742007-08-01 01:29:17 +01002189 dir++;
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002190 subdir++;
2191 offset++;
Johannes Schindeline6636742007-08-01 01:29:17 +01002192 }
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +07002193
2194 /* hel[p]/me vs hel[l]/yeah */
2195 if (*dir && *subdir)
2196 return -1;
2197
2198 if (!*subdir)
2199 return !*dir ? offset : -1; /* same dir */
2200
2201 /* foo/[b]ar vs foo/[] */
2202 if (is_dir_sep(dir[-1]))
2203 return is_dir_sep(subdir[-1]) ? offset : -1;
2204
2205 /* foo[/]bar vs foo[] */
2206 return is_dir_sep(*subdir) ? offset + 1 : -1;
Johannes Schindeline6636742007-08-01 01:29:17 +01002207}
2208
2209int is_inside_dir(const char *dir)
2210{
René Scharfe56b9f6e2014-07-28 20:30:39 +02002211 char *cwd;
2212 int rc;
2213
Nguyễn Thái Ngọc Duyb8929132011-03-26 16:04:25 +07002214 if (!dir)
2215 return 0;
René Scharfe56b9f6e2014-07-28 20:30:39 +02002216
2217 cwd = xgetcwd();
2218 rc = (dir_inside_of(cwd, dir) >= 0);
2219 free(cwd);
2220 return rc;
Johannes Schindeline6636742007-08-01 01:29:17 +01002221}
Johannes Schindelin7155b722007-09-28 16:28:54 +01002222
Alexander Potashev55892d22009-01-11 15:19:12 +03002223int is_empty_dir(const char *path)
2224{
2225 DIR *dir = opendir(path);
2226 struct dirent *e;
2227 int ret = 1;
2228
2229 if (!dir)
2230 return 0;
2231
2232 while ((e = readdir(dir)) != NULL)
2233 if (!is_dot_or_dotdot(e->d_name)) {
2234 ret = 0;
2235 break;
2236 }
2237
2238 closedir(dir);
2239 return ret;
2240}
2241
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002242static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
Johannes Schindelin7155b722007-09-28 16:28:54 +01002243{
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002244 DIR *dir;
Johannes Schindelin7155b722007-09-28 16:28:54 +01002245 struct dirent *e;
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002246 int ret = 0, original_len = path->len, len, kept_down = 0;
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002247 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
Junio C Hamanoc844a802012-03-15 15:58:54 +01002248 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002249 unsigned char submodule_head[20];
Johannes Schindelin7155b722007-09-28 16:28:54 +01002250
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002251 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002252 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002253 /* Do not descend and nuke a nested git work tree. */
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002254 if (kept_up)
2255 *kept_up = 1;
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002256 return 0;
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002257 }
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002258
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002259 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07002260 dir = opendir(path->buf);
Junio C Hamanoc844a802012-03-15 15:58:54 +01002261 if (!dir) {
Michael Haggerty863808c2014-01-18 23:48:57 +01002262 if (errno == ENOENT)
2263 return keep_toplevel ? -1 : 0;
2264 else if (errno == EACCES && !keep_toplevel)
Michael Haggertyecb2c282014-01-18 23:48:56 +01002265 /*
2266 * An empty dir could be removable even if it
2267 * is unreadable:
2268 */
Junio C Hamanoc844a802012-03-15 15:58:54 +01002269 return rmdir(path->buf);
2270 else
2271 return -1;
2272 }
Jeff King00b6c172015-09-24 17:08:35 -04002273 strbuf_complete(path, '/');
Johannes Schindelin7155b722007-09-28 16:28:54 +01002274
2275 len = path->len;
2276 while ((e = readdir(dir)) != NULL) {
2277 struct stat st;
Alexander Potashev8ca12c02009-01-10 15:07:50 +03002278 if (is_dot_or_dotdot(e->d_name))
2279 continue;
Johannes Schindelin7155b722007-09-28 16:28:54 +01002280
2281 strbuf_setlen(path, len);
2282 strbuf_addstr(path, e->d_name);
Michael Haggerty863808c2014-01-18 23:48:57 +01002283 if (lstat(path->buf, &st)) {
2284 if (errno == ENOENT)
2285 /*
2286 * file disappeared, which is what we
2287 * wanted anyway
2288 */
2289 continue;
2290 /* fall thru */
2291 } else if (S_ISDIR(st.st_mode)) {
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002292 if (!remove_dir_recurse(path, flag, &kept_down))
Johannes Schindelin7155b722007-09-28 16:28:54 +01002293 continue; /* happy */
Michael Haggerty863808c2014-01-18 23:48:57 +01002294 } else if (!only_empty &&
2295 (!unlink(path->buf) || errno == ENOENT)) {
Johannes Schindelin7155b722007-09-28 16:28:54 +01002296 continue; /* happy, too */
Michael Haggerty863808c2014-01-18 23:48:57 +01002297 }
Johannes Schindelin7155b722007-09-28 16:28:54 +01002298
2299 /* path too long, stat fails, or non-directory still exists */
2300 ret = -1;
2301 break;
2302 }
2303 closedir(dir);
2304
2305 strbuf_setlen(path, original_len);
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002306 if (!ret && !keep_toplevel && !kept_down)
Michael Haggerty863808c2014-01-18 23:48:57 +01002307 ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002308 else if (kept_up)
2309 /*
2310 * report the uplevel that it is not an error that we
2311 * did not rmdir() our directory.
2312 */
2313 *kept_up = !ret;
Johannes Schindelin7155b722007-09-28 16:28:54 +01002314 return ret;
2315}
Junio C Hamano039bc642007-11-14 00:05:00 -08002316
Junio C Hamanoae2f2032012-03-15 01:04:12 -07002317int remove_dir_recursively(struct strbuf *path, int flag)
2318{
2319 return remove_dir_recurse(path, flag, NULL);
2320}
2321
Jeff Kingf9327292015-08-10 05:38:57 -04002322static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
2323
Junio C Hamano039bc642007-11-14 00:05:00 -08002324void setup_standard_excludes(struct dir_struct *dir)
2325{
Junio C Hamano039bc642007-11-14 00:05:00 -08002326 dir->exclude_per_dir = ".gitignore";
Junio C Hamano099d2d82015-04-22 14:31:49 -07002327
2328 /* core.excludefile defaulting to $XDG_HOME/git/ignore */
Paul Tan2845ce72015-05-06 16:01:00 +08002329 if (!excludes_file)
2330 excludes_file = xdg_config_home("ignore");
Jonathan Nieder4698c8f2013-04-12 14:03:18 -07002331 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
Nguyễn Thái Ngọc Duy0dcb8d72015-03-08 17:12:25 +07002332 add_excludes_from_file_1(dir, excludes_file,
2333 dir->untracked ? &dir->ss_excludes_file : NULL);
Junio C Hamano099d2d82015-04-22 14:31:49 -07002334
2335 /* per repository user preference */
Jeff Kingf0056f62016-10-20 02:16:41 -04002336 if (startup_info->have_repository) {
2337 const char *path = git_path_info_exclude();
2338 if (!access_or_warn(path, R_OK, 0))
2339 add_excludes_from_file_1(dir, path,
2340 dir->untracked ? &dir->ss_info_exclude : NULL);
2341 }
Junio C Hamano039bc642007-11-14 00:05:00 -08002342}
Alex Riesen4a92d1b2008-09-27 00:56:46 +02002343
2344int remove_path(const char *name)
2345{
2346 char *slash;
2347
Junio C Hamanoc7054202017-05-30 09:23:33 +09002348 if (unlink(name) && !is_missing_file_error(errno))
Alex Riesen4a92d1b2008-09-27 00:56:46 +02002349 return -1;
2350
2351 slash = strrchr(name, '/');
2352 if (slash) {
2353 char *dirs = xstrdup(name);
2354 slash = dirs + (slash - name);
2355 do {
2356 *slash = '\0';
Jeff King3fc0d132010-02-19 00:57:21 -05002357 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
Alex Riesen4a92d1b2008-09-27 00:56:46 +02002358 free(dirs);
2359 }
2360 return 0;
2361}
2362
Adam Spiers270be812013-01-06 16:58:05 +00002363/*
2364 * Frees memory within dir which was allocated for exclude lists and
2365 * the exclude_stack. Does not free dir itself.
2366 */
2367void clear_directory(struct dir_struct *dir)
2368{
2369 int i, j;
2370 struct exclude_list_group *group;
2371 struct exclude_list *el;
2372 struct exclude_stack *stk;
2373
2374 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
2375 group = &dir->exclude_list_group[i];
2376 for (j = 0; j < group->nr; j++) {
2377 el = &group->el[j];
2378 if (i == EXC_DIRS)
2379 free((char *)el->src);
2380 clear_exclude_list(el);
2381 }
2382 free(group->el);
2383 }
2384
2385 stk = dir->exclude_stack;
2386 while (stk) {
2387 struct exclude_stack *prev = stk->prev;
2388 free(stk);
2389 stk = prev;
2390 }
Nguyễn Thái Ngọc Duyaceb9422014-07-14 11:50:22 +02002391 strbuf_release(&dir->basebuf);
Adam Spiers270be812013-01-06 16:58:05 +00002392}
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002393
2394struct ondisk_untracked_cache {
2395 struct stat_data info_exclude_stat;
2396 struct stat_data excludes_file_stat;
2397 uint32_t dir_flags;
2398 unsigned char info_exclude_sha1[20];
2399 unsigned char excludes_file_sha1[20];
2400 char exclude_per_dir[FLEX_ARRAY];
2401};
2402
2403#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)
2404
2405struct write_data {
2406 int index; /* number of written untracked_cache_dir */
2407 struct ewah_bitmap *check_only; /* from untracked_cache_dir */
2408 struct ewah_bitmap *valid; /* from untracked_cache_dir */
2409 struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */
2410 struct strbuf out;
2411 struct strbuf sb_stat;
2412 struct strbuf sb_sha1;
2413};
2414
2415static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from)
2416{
2417 to->sd_ctime.sec = htonl(from->sd_ctime.sec);
2418 to->sd_ctime.nsec = htonl(from->sd_ctime.nsec);
2419 to->sd_mtime.sec = htonl(from->sd_mtime.sec);
2420 to->sd_mtime.nsec = htonl(from->sd_mtime.nsec);
2421 to->sd_dev = htonl(from->sd_dev);
2422 to->sd_ino = htonl(from->sd_ino);
2423 to->sd_uid = htonl(from->sd_uid);
2424 to->sd_gid = htonl(from->sd_gid);
2425 to->sd_size = htonl(from->sd_size);
2426}
2427
2428static void write_one_dir(struct untracked_cache_dir *untracked,
2429 struct write_data *wd)
2430{
2431 struct stat_data stat_data;
2432 struct strbuf *out = &wd->out;
2433 unsigned char intbuf[16];
2434 unsigned int intlen, value;
2435 int i = wd->index++;
2436
2437 /*
2438 * untracked_nr should be reset whenever valid is clear, but
2439 * for safety..
2440 */
2441 if (!untracked->valid) {
2442 untracked->untracked_nr = 0;
2443 untracked->check_only = 0;
2444 }
2445
2446 if (untracked->check_only)
2447 ewah_set(wd->check_only, i);
2448 if (untracked->valid) {
2449 ewah_set(wd->valid, i);
2450 stat_data_to_disk(&stat_data, &untracked->stat_data);
2451 strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data));
2452 }
2453 if (!is_null_sha1(untracked->exclude_sha1)) {
2454 ewah_set(wd->sha1_valid, i);
2455 strbuf_add(&wd->sb_sha1, untracked->exclude_sha1, 20);
2456 }
2457
2458 intlen = encode_varint(untracked->untracked_nr, intbuf);
2459 strbuf_add(out, intbuf, intlen);
2460
2461 /* skip non-recurse directories */
2462 for (i = 0, value = 0; i < untracked->dirs_nr; i++)
2463 if (untracked->dirs[i]->recurse)
2464 value++;
2465 intlen = encode_varint(value, intbuf);
2466 strbuf_add(out, intbuf, intlen);
2467
2468 strbuf_add(out, untracked->name, strlen(untracked->name) + 1);
2469
2470 for (i = 0; i < untracked->untracked_nr; i++)
2471 strbuf_add(out, untracked->untracked[i],
2472 strlen(untracked->untracked[i]) + 1);
2473
2474 for (i = 0; i < untracked->dirs_nr; i++)
2475 if (untracked->dirs[i]->recurse)
2476 write_one_dir(untracked->dirs[i], wd);
2477}
2478
2479void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked)
2480{
2481 struct ondisk_untracked_cache *ouc;
2482 struct write_data wd;
2483 unsigned char varbuf[16];
Jeff Kinge0b83732016-02-22 17:44:42 -05002484 int varint_len;
2485 size_t len = strlen(untracked->exclude_per_dir);
2486
2487 FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002488 stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
2489 stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
2490 hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);
2491 hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);
2492 ouc->dir_flags = htonl(untracked->dir_flags);
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002493
2494 varint_len = encode_varint(untracked->ident.len, varbuf);
2495 strbuf_add(out, varbuf, varint_len);
René Scharfe81099842016-07-19 20:36:29 +02002496 strbuf_addbuf(out, &untracked->ident);
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002497
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002498 strbuf_add(out, ouc, ouc_size(len));
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002499 FREE_AND_NULL(ouc);
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002500
2501 if (!untracked->root) {
2502 varint_len = encode_varint(0, varbuf);
2503 strbuf_add(out, varbuf, varint_len);
2504 return;
2505 }
2506
2507 wd.index = 0;
2508 wd.check_only = ewah_new();
2509 wd.valid = ewah_new();
2510 wd.sha1_valid = ewah_new();
2511 strbuf_init(&wd.out, 1024);
2512 strbuf_init(&wd.sb_stat, 1024);
2513 strbuf_init(&wd.sb_sha1, 1024);
2514 write_one_dir(untracked->root, &wd);
2515
2516 varint_len = encode_varint(wd.index, varbuf);
2517 strbuf_add(out, varbuf, varint_len);
2518 strbuf_addbuf(out, &wd.out);
2519 ewah_serialize_strbuf(wd.valid, out);
2520 ewah_serialize_strbuf(wd.check_only, out);
2521 ewah_serialize_strbuf(wd.sha1_valid, out);
2522 strbuf_addbuf(out, &wd.sb_stat);
2523 strbuf_addbuf(out, &wd.sb_sha1);
2524 strbuf_addch(out, '\0'); /* safe guard for string lists */
2525
2526 ewah_free(wd.valid);
2527 ewah_free(wd.check_only);
2528 ewah_free(wd.sha1_valid);
2529 strbuf_release(&wd.out);
2530 strbuf_release(&wd.sb_stat);
2531 strbuf_release(&wd.sb_sha1);
2532}
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002533
2534static void free_untracked(struct untracked_cache_dir *ucd)
2535{
2536 int i;
2537 if (!ucd)
2538 return;
2539 for (i = 0; i < ucd->dirs_nr; i++)
2540 free_untracked(ucd->dirs[i]);
2541 for (i = 0; i < ucd->untracked_nr; i++)
2542 free(ucd->untracked[i]);
2543 free(ucd->untracked);
2544 free(ucd->dirs);
2545 free(ucd);
2546}
2547
2548void free_untracked_cache(struct untracked_cache *uc)
2549{
2550 if (uc)
2551 free_untracked(uc->root);
2552 free(uc);
2553}
2554
2555struct read_data {
2556 int index;
2557 struct untracked_cache_dir **ucd;
2558 struct ewah_bitmap *check_only;
2559 struct ewah_bitmap *valid;
2560 struct ewah_bitmap *sha1_valid;
2561 const unsigned char *data;
2562 const unsigned char *end;
2563};
2564
2565static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from)
2566{
2567 to->sd_ctime.sec = get_be32(&from->sd_ctime.sec);
2568 to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec);
2569 to->sd_mtime.sec = get_be32(&from->sd_mtime.sec);
2570 to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec);
2571 to->sd_dev = get_be32(&from->sd_dev);
2572 to->sd_ino = get_be32(&from->sd_ino);
2573 to->sd_uid = get_be32(&from->sd_uid);
2574 to->sd_gid = get_be32(&from->sd_gid);
2575 to->sd_size = get_be32(&from->sd_size);
2576}
2577
2578static int read_one_dir(struct untracked_cache_dir **untracked_,
2579 struct read_data *rd)
2580{
2581 struct untracked_cache_dir ud, *untracked;
2582 const unsigned char *next, *data = rd->data, *end = rd->end;
2583 unsigned int value;
2584 int i, len;
2585
2586 memset(&ud, 0, sizeof(ud));
2587
2588 next = data;
2589 value = decode_varint(&next);
2590 if (next > end)
2591 return -1;
2592 ud.recurse = 1;
2593 ud.untracked_alloc = value;
2594 ud.untracked_nr = value;
2595 if (ud.untracked_nr)
Jeff Kingb32fa952016-02-22 17:44:25 -05002596 ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002597 data = next;
2598
2599 next = data;
2600 ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
2601 if (next > end)
2602 return -1;
Jeff Kingb32fa952016-02-22 17:44:25 -05002603 ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002604 data = next;
2605
2606 len = strlen((const char *)data);
2607 next = data + len + 1;
2608 if (next > rd->end)
2609 return -1;
Jeff King50a6c8e2016-02-22 17:44:35 -05002610 *untracked_ = untracked = xmalloc(st_add(sizeof(*untracked), len));
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002611 memcpy(untracked, &ud, sizeof(ud));
2612 memcpy(untracked->name, data, len + 1);
2613 data = next;
2614
2615 for (i = 0; i < untracked->untracked_nr; i++) {
2616 len = strlen((const char *)data);
2617 next = data + len + 1;
2618 if (next > rd->end)
2619 return -1;
2620 untracked->untracked[i] = xstrdup((const char*)data);
2621 data = next;
2622 }
2623
2624 rd->ucd[rd->index++] = untracked;
2625 rd->data = data;
2626
2627 for (i = 0; i < untracked->dirs_nr; i++) {
2628 len = read_one_dir(untracked->dirs + i, rd);
2629 if (len < 0)
2630 return -1;
2631 }
2632 return 0;
2633}
2634
2635static void set_check_only(size_t pos, void *cb)
2636{
2637 struct read_data *rd = cb;
2638 struct untracked_cache_dir *ud = rd->ucd[pos];
2639 ud->check_only = 1;
2640}
2641
2642static void read_stat(size_t pos, void *cb)
2643{
2644 struct read_data *rd = cb;
2645 struct untracked_cache_dir *ud = rd->ucd[pos];
2646 if (rd->data + sizeof(struct stat_data) > rd->end) {
2647 rd->data = rd->end + 1;
2648 return;
2649 }
2650 stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);
2651 rd->data += sizeof(struct stat_data);
2652 ud->valid = 1;
2653}
2654
2655static void read_sha1(size_t pos, void *cb)
2656{
2657 struct read_data *rd = cb;
2658 struct untracked_cache_dir *ud = rd->ucd[pos];
2659 if (rd->data + 20 > rd->end) {
2660 rd->data = rd->end + 1;
2661 return;
2662 }
2663 hashcpy(ud->exclude_sha1, rd->data);
2664 rd->data += 20;
2665}
2666
2667static void load_sha1_stat(struct sha1_stat *sha1_stat,
2668 const struct stat_data *stat,
2669 const unsigned char *sha1)
2670{
2671 stat_data_from_disk(&sha1_stat->stat, stat);
2672 hashcpy(sha1_stat->sha1, sha1);
2673 sha1_stat->valid = 1;
2674}
2675
2676struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
2677{
2678 const struct ondisk_untracked_cache *ouc;
2679 struct untracked_cache *uc;
2680 struct read_data rd;
2681 const unsigned char *next = data, *end = (const unsigned char *)data + sz;
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002682 const char *ident;
2683 int ident_len, len;
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002684
2685 if (sz <= 1 || end[-1] != '\0')
2686 return NULL;
2687 end--;
2688
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002689 ident_len = decode_varint(&next);
2690 if (next + ident_len > end)
2691 return NULL;
2692 ident = (const char *)next;
2693 next += ident_len;
2694
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002695 ouc = (const struct ondisk_untracked_cache *)next;
2696 if (next + ouc_size(0) > end)
2697 return NULL;
2698
2699 uc = xcalloc(1, sizeof(*uc));
Nguyễn Thái Ngọc Duy1e8fef62015-03-08 17:12:46 +07002700 strbuf_init(&uc->ident, ident_len);
2701 strbuf_add(&uc->ident, ident, ident_len);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002702 load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,
2703 ouc->info_exclude_sha1);
2704 load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,
2705 ouc->excludes_file_sha1);
2706 uc->dir_flags = get_be32(&ouc->dir_flags);
2707 uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir);
2708 /* NUL after exclude_per_dir is covered by sizeof(*ouc) */
2709 next += ouc_size(strlen(ouc->exclude_per_dir));
2710 if (next >= end)
2711 goto done2;
2712
2713 len = decode_varint(&next);
2714 if (next > end || len == 0)
2715 goto done2;
2716
2717 rd.valid = ewah_new();
2718 rd.check_only = ewah_new();
2719 rd.sha1_valid = ewah_new();
2720 rd.data = next;
2721 rd.end = end;
2722 rd.index = 0;
Jeff Kingb32fa952016-02-22 17:44:25 -05002723 ALLOC_ARRAY(rd.ucd, len);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002724
2725 if (read_one_dir(&uc->root, &rd) || rd.index != len)
2726 goto done;
2727
2728 next = rd.data;
2729 len = ewah_read_mmap(rd.valid, next, end - next);
2730 if (len < 0)
2731 goto done;
2732
2733 next += len;
2734 len = ewah_read_mmap(rd.check_only, next, end - next);
2735 if (len < 0)
2736 goto done;
2737
2738 next += len;
2739 len = ewah_read_mmap(rd.sha1_valid, next, end - next);
2740 if (len < 0)
2741 goto done;
2742
2743 ewah_each_bit(rd.check_only, set_check_only, &rd);
2744 rd.data = next + len;
2745 ewah_each_bit(rd.valid, read_stat, &rd);
2746 ewah_each_bit(rd.sha1_valid, read_sha1, &rd);
2747 next = rd.data;
2748
2749done:
2750 free(rd.ucd);
2751 ewah_free(rd.valid);
2752 ewah_free(rd.check_only);
2753 ewah_free(rd.sha1_valid);
2754done2:
2755 if (next != end) {
2756 free_untracked_cache(uc);
2757 uc = NULL;
2758 }
2759 return uc;
2760}
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002761
Nguyễn Thái Ngọc Duy73f91452015-08-19 20:01:26 +07002762static void invalidate_one_directory(struct untracked_cache *uc,
2763 struct untracked_cache_dir *ucd)
2764{
2765 uc->dir_invalidated++;
2766 ucd->valid = 0;
2767 ucd->untracked_nr = 0;
2768}
2769
2770/*
2771 * Normally when an entry is added or removed from a directory,
2772 * invalidating that directory is enough. No need to touch its
2773 * ancestors. When a directory is shown as "foo/bar/" in git-status
2774 * however, deleting or adding an entry may have cascading effect.
2775 *
2776 * Say the "foo/bar/file" has become untracked, we need to tell the
2777 * untracked_cache_dir of "foo" that "bar/" is not an untracked
2778 * directory any more (because "bar" is managed by foo as an untracked
2779 * "file").
2780 *
2781 * Similarly, if "foo/bar/file" moves from untracked to tracked and it
2782 * was the last untracked entry in the entire "foo", we should show
2783 * "foo/" instead. Which means we have to invalidate past "bar" up to
2784 * "foo".
2785 *
2786 * This function traverses all directories from root to leaf. If there
2787 * is a chance of one of the above cases happening, we invalidate back
2788 * to root. Otherwise we just invalidate the leaf. There may be a more
2789 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to
2790 * detect these cases and avoid unnecessary invalidation, for example,
2791 * checking for the untracked entry named "bar/" in "foo", but for now
2792 * stick to something safe and simple.
2793 */
2794static int invalidate_one_component(struct untracked_cache *uc,
2795 struct untracked_cache_dir *dir,
2796 const char *path, int len)
2797{
2798 const char *rest = strchr(path, '/');
2799
2800 if (rest) {
2801 int component_len = rest - path;
2802 struct untracked_cache_dir *d =
2803 lookup_untracked(uc, dir, path, component_len);
2804 int ret =
2805 invalidate_one_component(uc, d, rest + 1,
2806 len - (component_len + 1));
2807 if (ret)
2808 invalidate_one_directory(uc, dir);
2809 return ret;
2810 }
2811
2812 invalidate_one_directory(uc, dir);
2813 return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;
2814}
2815
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002816void untracked_cache_invalidate_path(struct index_state *istate,
2817 const char *path)
2818{
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002819 if (!istate->untracked || !istate->untracked->root)
2820 return;
Nguyễn Thái Ngọc Duy73f91452015-08-19 20:01:26 +07002821 invalidate_one_component(istate->untracked, istate->untracked->root,
2822 path, strlen(path));
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002823}
2824
2825void untracked_cache_remove_from_index(struct index_state *istate,
2826 const char *path)
2827{
2828 untracked_cache_invalidate_path(istate, path);
2829}
2830
2831void untracked_cache_add_to_index(struct index_state *istate,
2832 const char *path)
2833{
2834 untracked_cache_invalidate_path(istate, path);
2835}
Stefan Beller47e83eb2016-12-12 11:04:34 -08002836
2837/* Update gitfile and core.worktree setting to connect work tree and git dir */
2838void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)
2839{
Stefan Beller365444a2017-03-14 14:46:24 -07002840 struct strbuf gitfile_sb = STRBUF_INIT;
2841 struct strbuf cfg_sb = STRBUF_INIT;
Stefan Beller47e83eb2016-12-12 11:04:34 -08002842 struct strbuf rel_path = STRBUF_INIT;
Stefan Beller365444a2017-03-14 14:46:24 -07002843 char *git_dir, *work_tree;
Stefan Beller47e83eb2016-12-12 11:04:34 -08002844
Stefan Beller365444a2017-03-14 14:46:24 -07002845 /* Prepare .git file */
2846 strbuf_addf(&gitfile_sb, "%s/.git", work_tree_);
2847 if (safe_create_leading_directories_const(gitfile_sb.buf))
2848 die(_("could not create directories for %s"), gitfile_sb.buf);
2849
2850 /* Prepare config file */
2851 strbuf_addf(&cfg_sb, "%s/config", git_dir_);
2852 if (safe_create_leading_directories_const(cfg_sb.buf))
2853 die(_("could not create directories for %s"), cfg_sb.buf);
2854
Junio C Hamanoe394fa02017-03-28 14:05:58 -07002855 git_dir = real_pathdup(git_dir_, 1);
2856 work_tree = real_pathdup(work_tree_, 1);
Stefan Beller365444a2017-03-14 14:46:24 -07002857
2858 /* Write .git file */
2859 write_file(gitfile_sb.buf, "gitdir: %s",
Stefan Beller47e83eb2016-12-12 11:04:34 -08002860 relative_path(git_dir, work_tree, &rel_path));
Stefan Beller47e83eb2016-12-12 11:04:34 -08002861 /* Update core.worktree setting */
Stefan Beller365444a2017-03-14 14:46:24 -07002862 git_config_set_in_file(cfg_sb.buf, "core.worktree",
Stefan Beller47e83eb2016-12-12 11:04:34 -08002863 relative_path(work_tree, git_dir, &rel_path));
2864
Stefan Beller365444a2017-03-14 14:46:24 -07002865 strbuf_release(&gitfile_sb);
2866 strbuf_release(&cfg_sb);
Stefan Beller47e83eb2016-12-12 11:04:34 -08002867 strbuf_release(&rel_path);
2868 free(work_tree);
2869 free(git_dir);
2870}
Stefan Bellerf6f85862016-12-12 11:04:35 -08002871
2872/*
2873 * Migrate the git directory of the given path from old_git_dir to new_git_dir.
2874 */
2875void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir)
2876{
2877 if (rename(old_git_dir, new_git_dir) < 0)
2878 die_errno(_("could not migrate git directory from '%s' to '%s'"),
2879 old_git_dir, new_git_dir);
2880
2881 connect_work_tree_and_git_dir(path, new_git_dir);
2882}