blob: 74f4d710e8f0f28410fae49a6b9c31bf1045e0ca [file] [log] [blame]
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001#include "cache.h"
2#include "tree-walk.h"
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +07003#include "dir.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02005#include "tree.h"
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +07006#include "pathspec.h"
Derrick Stoleeda8be8c2021-01-04 03:09:10 +00007#include "json-writer.h"
Junio C Hamano1b0c7172006-03-29 22:55:43 -08008
Linus Torvalds4651ece2007-03-21 10:09:56 -07009static const char *get_mode(const char *str, unsigned int *modep)
10{
11 unsigned char c;
12 unsigned int mode = 0;
13
Martin Koegler64cc1c02008-01-06 18:21:10 +010014 if (*str == ' ')
15 return NULL;
16
Linus Torvalds4651ece2007-03-21 10:09:56 -070017 while ((c = *str++) != ' ') {
18 if (c < '0' || c > '7')
19 return NULL;
20 mode = (mode << 3) + (c - '0');
21 }
22 *modep = mode;
23 return str;
24}
25
David Turner8354fa32016-09-27 16:59:51 -040026static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
Linus Torvalds4651ece2007-03-21 10:09:56 -070027{
28 const char *path;
29 unsigned int mode, len;
brian m. carlson83e4b752018-07-16 01:27:54 +000030 const unsigned hashsz = the_hash_algo->rawsz;
Linus Torvalds4651ece2007-03-21 10:09:56 -070031
brian m. carlson83e4b752018-07-16 01:27:54 +000032 if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
David Turner8354fa32016-09-27 16:59:51 -040033 strbuf_addstr(err, _("too-short tree object"));
34 return -1;
35 }
Martin Koegler64cc1c02008-01-06 18:21:10 +010036
Linus Torvalds4651ece2007-03-21 10:09:56 -070037 path = get_mode(buf, &mode);
David Turner8354fa32016-09-27 16:59:51 -040038 if (!path) {
39 strbuf_addstr(err, _("malformed mode in tree entry"));
40 return -1;
41 }
42 if (!*path) {
43 strbuf_addstr(err, _("empty filename in tree entry"));
44 return -1;
45 }
Linus Torvalds4651ece2007-03-21 10:09:56 -070046 len = strlen(path) + 1;
47
48 /* Initialize the descriptor entry */
49 desc->entry.path = path;
Jeff Kingec18b102022-08-10 17:01:17 -040050 desc->entry.mode = (desc->flags & TREE_DESC_RAW_MODES) ? mode : canon_mode(mode);
brian m. carlsonea82b2a2019-01-15 00:39:44 +000051 desc->entry.pathlen = len - 1;
brian m. carlson92e2cab2021-04-26 01:02:50 +000052 oidread(&desc->entry.oid, (const unsigned char *)path + len);
David Turner8354fa32016-09-27 16:59:51 -040053
54 return 0;
Linus Torvalds4651ece2007-03-21 10:09:56 -070055}
56
Jeff Kingec18b102022-08-10 17:01:17 -040057static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer,
58 unsigned long size, struct strbuf *err,
59 enum tree_desc_flags flags)
Linus Torvalds6fda5e52007-03-21 10:08:25 -070060{
61 desc->buffer = buffer;
62 desc->size = size;
Jeff Kingec18b102022-08-10 17:01:17 -040063 desc->flags = flags;
Linus Torvalds4651ece2007-03-21 10:09:56 -070064 if (size)
David Turner8354fa32016-09-27 16:59:51 -040065 return decode_tree_entry(desc, buffer, size, err);
66 return 0;
67}
68
69void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
70{
71 struct strbuf err = STRBUF_INIT;
Jeff Kingec18b102022-08-10 17:01:17 -040072 if (init_tree_desc_internal(desc, buffer, size, &err, 0))
David Turner8354fa32016-09-27 16:59:51 -040073 die("%s", err.buf);
74 strbuf_release(&err);
75}
76
Jeff Kingec18b102022-08-10 17:01:17 -040077int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned long size,
78 enum tree_desc_flags flags)
David Turner8354fa32016-09-27 16:59:51 -040079{
80 struct strbuf err = STRBUF_INIT;
Jeff Kingec18b102022-08-10 17:01:17 -040081 int result = init_tree_desc_internal(desc, buffer, size, &err, flags);
David Turner8354fa32016-09-27 16:59:51 -040082 if (result)
83 error("%s", err.buf);
84 strbuf_release(&err);
85 return result;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070086}
87
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +070088void *fill_tree_descriptor(struct repository *r,
89 struct tree_desc *desc,
90 const struct object_id *oid)
Junio C Hamano1b0c7172006-03-29 22:55:43 -080091{
92 unsigned long size = 0;
93 void *buf = NULL;
94
René Scharfe5c377d32017-08-12 10:32:59 +020095 if (oid) {
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +010096 buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL);
Junio C Hamano1b0c7172006-03-29 22:55:43 -080097 if (!buf)
René Scharfe5c377d32017-08-12 10:32:59 +020098 die("unable to read tree %s", oid_to_hex(oid));
Junio C Hamano1b0c7172006-03-29 22:55:43 -080099 }
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700100 init_tree_desc(desc, buf, size);
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800101 return buf;
102}
103
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800104static void entry_clear(struct name_entry *a)
105{
106 memset(a, 0, sizeof(*a));
107}
108
109static void entry_extract(struct tree_desc *t, struct name_entry *a)
110{
Linus Torvalds4651ece2007-03-21 10:09:56 -0700111 *a = t->entry;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800112}
113
David Turner8354fa32016-09-27 16:59:51 -0400114static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err)
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800115{
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700116 const void *buf = desc->buffer;
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000117 const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + the_hash_algo->rawsz;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800118 unsigned long size = desc->size;
Linus Torvalds4651ece2007-03-21 10:09:56 -0700119 unsigned long len = end - (const unsigned char *)buf;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800120
121 if (size < len)
Jeff King2edffef2016-09-27 16:59:50 -0400122 die(_("too-short tree file"));
Linus Torvalds4651ece2007-03-21 10:09:56 -0700123 buf = end;
124 size -= len;
125 desc->buffer = buf;
126 desc->size = size;
127 if (size)
David Turner8354fa32016-09-27 16:59:51 -0400128 return decode_tree_entry(desc, buf, size, err);
129 return 0;
130}
131
132void update_tree_entry(struct tree_desc *desc)
133{
134 struct strbuf err = STRBUF_INIT;
135 if (update_tree_entry_internal(desc, &err))
136 die("%s", err.buf);
137 strbuf_release(&err);
138}
139
140int update_tree_entry_gently(struct tree_desc *desc)
141{
142 struct strbuf err = STRBUF_INIT;
143 if (update_tree_entry_internal(desc, &err)) {
144 error("%s", err.buf);
145 strbuf_release(&err);
146 /* Stop processing this tree after error */
147 desc->size = 0;
148 return -1;
149 }
150 strbuf_release(&err);
151 return 0;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800152}
153
Linus Torvalds4c068a92006-05-30 09:45:45 -0700154int tree_entry(struct tree_desc *desc, struct name_entry *entry)
155{
Linus Torvalds4651ece2007-03-21 10:09:56 -0700156 if (!desc->size)
Linus Torvalds4c068a92006-05-30 09:45:45 -0700157 return 0;
158
Linus Torvalds4651ece2007-03-21 10:09:56 -0700159 *entry = desc->entry;
160 update_tree_entry(desc);
Linus Torvalds4c068a92006-05-30 09:45:45 -0700161 return 1;
162}
163
David Turner8354fa32016-09-27 16:59:51 -0400164int tree_entry_gently(struct tree_desc *desc, struct name_entry *entry)
165{
166 if (!desc->size)
167 return 0;
168
169 *entry = desc->entry;
170 if (update_tree_entry_gently(desc))
171 return 0;
172 return 1;
173}
174
Derrick Stoleeda8be8c2021-01-04 03:09:10 +0000175static int traverse_trees_atexit_registered;
176static int traverse_trees_count;
177static int traverse_trees_cur_depth;
178static int traverse_trees_max_depth;
179
180static void trace2_traverse_trees_statistics_atexit(void)
181{
182 struct json_writer jw = JSON_WRITER_INIT;
183
184 jw_object_begin(&jw, 0);
185 jw_object_intmax(&jw, "traverse_trees_count", traverse_trees_count);
186 jw_object_intmax(&jw, "traverse_trees_max_depth", traverse_trees_max_depth);
187 jw_end(&jw);
188
189 trace2_data_json("traverse_trees", the_repository, "statistics", &jw);
190
191 jw_release(&jw);
192}
193
Linus Torvalds40d934d2008-03-05 18:59:29 -0800194void setup_traverse_info(struct traverse_info *info, const char *base)
195{
Jeff King37806082019-07-31 00:38:18 -0400196 size_t pathlen = strlen(base);
Linus Torvaldsbcbe5a52008-03-06 15:44:48 -0800197 static struct traverse_info dummy;
Linus Torvalds40d934d2008-03-05 18:59:29 -0800198
199 memset(info, 0, sizeof(*info));
200 if (pathlen && base[pathlen-1] == '/')
201 pathlen--;
202 info->pathlen = pathlen ? pathlen + 1 : 0;
Jeff King90553842019-07-31 00:38:15 -0400203 info->name = base;
204 info->namelen = pathlen;
Jeff King947208b2019-07-31 00:38:11 -0400205 if (pathlen)
Linus Torvaldsbcbe5a52008-03-06 15:44:48 -0800206 info->prev = &dummy;
Derrick Stoleeda8be8c2021-01-04 03:09:10 +0000207
208 if (trace2_is_enabled() && !traverse_trees_atexit_registered) {
209 atexit(trace2_traverse_trees_statistics_atexit);
210 traverse_trees_atexit_registered = 1;
211 }
Linus Torvalds40d934d2008-03-05 18:59:29 -0800212}
213
Jeff King5aa02f92019-07-31 00:38:25 -0400214char *make_traverse_path(char *path, size_t pathlen,
215 const struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -0400216 const char *name, size_t namelen)
Linus Torvalds40d934d2008-03-05 18:59:29 -0800217{
Jeff King5aa02f92019-07-31 00:38:25 -0400218 /* Always points to the end of the name we're about to add */
219 size_t pos = st_add(info->pathlen, namelen);
Linus Torvalds40d934d2008-03-05 18:59:29 -0800220
Jeff King5aa02f92019-07-31 00:38:25 -0400221 if (pos >= pathlen)
222 BUG("too small buffer passed to make_traverse_path");
223
224 path[pos] = 0;
Linus Torvalds40d934d2008-03-05 18:59:29 -0800225 for (;;) {
Jeff King5aa02f92019-07-31 00:38:25 -0400226 if (pos < namelen)
227 BUG("traverse_info pathlen does not match strings");
228 pos -= namelen;
229 memcpy(path + pos, name, namelen);
230
231 if (!pos)
Linus Torvalds40d934d2008-03-05 18:59:29 -0800232 break;
Jeff King5aa02f92019-07-31 00:38:25 -0400233 path[--pos] = '/';
234
235 if (!info)
236 BUG("traverse_info ran out of list items");
Jeff King90553842019-07-31 00:38:15 -0400237 name = info->name;
238 namelen = info->namelen;
Linus Torvalds40d934d2008-03-05 18:59:29 -0800239 info = info->prev;
Linus Torvalds40d934d2008-03-05 18:59:29 -0800240 }
241 return path;
242}
243
Jeff Kingc43ab062019-07-31 00:38:23 -0400244void strbuf_make_traverse_path(struct strbuf *out,
245 const struct traverse_info *info,
246 const char *name, size_t namelen)
247{
248 size_t len = traverse_path_len(info, namelen);
249
250 strbuf_grow(out, len);
Jeff King5aa02f92019-07-31 00:38:25 -0400251 make_traverse_path(out->buf + out->len, out->alloc - out->len,
252 info, name, namelen);
Jeff Kingc43ab062019-07-31 00:38:23 -0400253 strbuf_setlen(out, out->len + len);
254}
255
Junio C Hamano1ee26572009-09-19 14:07:14 -0700256struct tree_desc_skip {
257 struct tree_desc_skip *prev;
258 const void *ptr;
259};
260
261struct tree_desc_x {
262 struct tree_desc d;
263 struct tree_desc_skip *skip;
264};
265
Junio C Hamano1ee26572009-09-19 14:07:14 -0700266static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
267{
268 /*
269 * The caller wants to pick *a* from a tree or nothing.
270 * We are looking at *b* in a tree.
271 *
272 * (0) If a and b are the same name, we are trivially happy.
273 *
274 * There are three possibilities where *a* could be hiding
275 * behind *b*.
276 *
277 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
278 * matter what.
279 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
280 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
281 *
282 * Otherwise we know *a* won't appear in the tree without
283 * scanning further.
284 */
285
286 int cmp = name_compare(a, a_len, b, b_len);
287
288 /* Most common case first -- reading sync'd trees */
289 if (!cmp)
290 return cmp;
291
292 if (0 < cmp) {
293 /* a comes after b; it does not matter if it is case (3)
294 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
295 return 1;
296 */
297 return 1; /* keep looking */
298 }
299
300 /* b comes after a; are we looking at case (2)? */
301 if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/')
302 return 1; /* keep looking */
303
304 return -1; /* a cannot appear in the tree */
305}
306
307/*
308 * From the extended tree_desc, extract the first name entry, while
309 * paying attention to the candidate "first" name. Most importantly,
310 * when looking for an entry, if there are entries that sorts earlier
311 * in the tree object representation than that name, skip them and
312 * process the named entry first. We will remember that we haven't
313 * processed the first entry yet, and in the later call skip the
314 * entry we processed early when update_extended_entry() is called.
315 *
316 * E.g. if the underlying tree object has these entries:
317 *
318 * blob "t-1"
319 * blob "t-2"
320 * tree "t"
321 * blob "t=1"
322 *
323 * and the "first" asks for "t", remember that we still need to
324 * process "t-1" and "t-2" but extract "t". After processing the
325 * entry "t" from this call, the caller will let us know by calling
326 * update_extended_entry() that we can remember "t" has been processed
327 * already.
328 */
329
330static void extended_entry_extract(struct tree_desc_x *t,
331 struct name_entry *a,
332 const char *first,
333 int first_len)
334{
335 const char *path;
336 int len;
337 struct tree_desc probe;
338 struct tree_desc_skip *skip;
339
340 /*
341 * Extract the first entry from the tree_desc, but skip the
342 * ones that we already returned in earlier rounds.
343 */
344 while (1) {
345 if (!t->d.size) {
346 entry_clear(a);
347 break; /* not found */
348 }
349 entry_extract(&t->d, a);
350 for (skip = t->skip; skip; skip = skip->prev)
351 if (a->path == skip->ptr)
352 break; /* found */
353 if (!skip)
354 break;
355 /* We have processed this entry already. */
356 update_tree_entry(&t->d);
357 }
358
359 if (!first || !a->path)
360 return;
361
362 /*
363 * The caller wants "first" from this tree, or nothing.
364 */
365 path = a->path;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100366 len = tree_entry_len(a);
Junio C Hamano1ee26572009-09-19 14:07:14 -0700367 switch (check_entry_match(first, first_len, path, len)) {
368 case -1:
369 entry_clear(a);
370 case 0:
371 return;
372 default:
373 break;
374 }
375
376 /*
377 * We need to look-ahead -- we suspect that a subtree whose
378 * name is "first" may be hiding behind the current entry "path".
379 */
380 probe = t->d;
381 while (probe.size) {
382 entry_extract(&probe, a);
383 path = a->path;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100384 len = tree_entry_len(a);
Junio C Hamano1ee26572009-09-19 14:07:14 -0700385 switch (check_entry_match(first, first_len, path, len)) {
386 case -1:
387 entry_clear(a);
388 case 0:
389 return;
390 default:
391 update_tree_entry(&probe);
392 break;
393 }
394 /* keep looking */
395 }
396 entry_clear(a);
397}
398
399static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a)
400{
401 if (t->d.entry.path == a->path) {
402 update_tree_entry(&t->d);
403 } else {
404 /* we have returned this entry early */
405 struct tree_desc_skip *skip = xmalloc(sizeof(*skip));
406 skip->ptr = a->path;
407 skip->prev = t->skip;
408 t->skip = skip;
409 }
410}
411
412static void free_extended_entry(struct tree_desc_x *t)
413{
414 struct tree_desc_skip *p, *s;
415
416 for (s = t->skip; s; s = p) {
417 p = s->prev;
418 free(s);
419 }
420}
421
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100422static inline int prune_traversal(struct index_state *istate,
423 struct name_entry *e,
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700424 struct traverse_info *info,
425 struct strbuf *base,
426 int still_interesting)
427{
428 if (!info->pathspec || still_interesting == 2)
429 return 2;
430 if (still_interesting < 0)
431 return still_interesting;
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100432 return tree_entry_interesting(istate, e, base,
433 0, info->pathspec);
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700434}
435
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100436int traverse_trees(struct index_state *istate,
437 int n, struct tree_desc *t,
438 struct traverse_info *info)
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800439{
Matthieu Moye6c111b2010-08-11 10:38:07 +0200440 int error = 0;
Jeff King5290d452020-02-01 06:39:22 -0500441 struct name_entry entry[MAX_TRAVERSE_TREES];
Junio C Hamano1ee26572009-09-19 14:07:14 -0700442 int i;
Jeff King8dd40c02020-01-30 04:53:38 -0500443 struct tree_desc_x tx[ARRAY_SIZE(entry)];
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700444 struct strbuf base = STRBUF_INIT;
445 int interesting = 1;
David Turnerd9c2bd52015-12-21 17:34:20 -0500446 char *traverse_path;
Junio C Hamano1ee26572009-09-19 14:07:14 -0700447
Derrick Stoleeda8be8c2021-01-04 03:09:10 +0000448 traverse_trees_count++;
449 traverse_trees_cur_depth++;
450
451 if (traverse_trees_cur_depth > traverse_trees_max_depth)
452 traverse_trees_max_depth = traverse_trees_cur_depth;
453
Jeff King8dd40c02020-01-30 04:53:38 -0500454 if (n >= ARRAY_SIZE(entry))
455 BUG("traverse_trees() called with too many trees (%d)", n);
456
457 for (i = 0; i < n; i++) {
Junio C Hamano1ee26572009-09-19 14:07:14 -0700458 tx[i].d = t[i];
Jeff King8dd40c02020-01-30 04:53:38 -0500459 tx[i].skip = NULL;
460 }
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800461
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700462 if (info->prev) {
Jeff Kingc43ab062019-07-31 00:38:23 -0400463 strbuf_make_traverse_path(&base, info->prev,
464 info->name, info->namelen);
465 strbuf_addch(&base, '/');
466 traverse_path = xstrndup(base.buf, base.len);
David Turnerd9c2bd52015-12-21 17:34:20 -0500467 } else {
Jeff King90553842019-07-31 00:38:15 -0400468 traverse_path = xstrndup(info->name, info->pathlen);
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700469 }
David Turnerd9c2bd52015-12-21 17:34:20 -0500470 info->traverse_path = traverse_path;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800471 for (;;) {
Stefan Bellera04f8192013-07-19 22:26:32 +0200472 int trees_used;
Junio C Hamano1ee26572009-09-19 14:07:14 -0700473 unsigned long mask, dirmask;
474 const char *first = NULL;
475 int first_len = 0;
Ramsay Jones27c0f762011-09-11 20:39:32 +0100476 struct name_entry *e = NULL;
Junio C Hamano1ee26572009-09-19 14:07:14 -0700477 int len;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800478
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800479 for (i = 0; i < n; i++) {
Junio C Hamano1ee26572009-09-19 14:07:14 -0700480 e = entry + i;
481 extended_entry_extract(tx + i, e, NULL, 0);
482 }
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800483
Junio C Hamano1ee26572009-09-19 14:07:14 -0700484 /*
485 * A tree may have "t-2" at the current location even
486 * though it may have "t" that is a subtree behind it,
487 * and another tree may return "t". We want to grab
488 * all "t" from all trees to match in such a case.
489 */
490 for (i = 0; i < n; i++) {
491 e = entry + i;
492 if (!e->path)
493 continue;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100494 len = tree_entry_len(e);
Junio C Hamano1ee26572009-09-19 14:07:14 -0700495 if (!first) {
496 first = e->path;
497 first_len = len;
498 continue;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800499 }
Junio C Hamano1ee26572009-09-19 14:07:14 -0700500 if (name_compare(e->path, len, first, first_len) < 0) {
501 first = e->path;
502 first_len = len;
503 }
504 }
505
506 if (first) {
507 for (i = 0; i < n; i++) {
508 e = entry + i;
509 extended_entry_extract(tx + i, e, first, first_len);
510 /* Cull the ones that are not the earliest */
511 if (!e->path)
512 continue;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100513 len = tree_entry_len(e);
Junio C Hamano1ee26572009-09-19 14:07:14 -0700514 if (name_compare(e->path, len, first, first_len))
515 entry_clear(e);
516 }
517 }
518
519 /* Now we have in entry[i] the earliest name from the trees */
520 mask = 0;
521 dirmask = 0;
522 for (i = 0; i < n; i++) {
523 if (!entry[i].path)
524 continue;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800525 mask |= 1ul << i;
Linus Torvalds91e4f032008-03-05 20:06:18 -0800526 if (S_ISDIR(entry[i].mode))
527 dirmask |= 1ul << i;
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700528 e = &entry[i];
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800529 }
530 if (!mask)
531 break;
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100532 interesting = prune_traversal(istate, e, info, &base, interesting);
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700533 if (interesting < 0)
534 break;
535 if (interesting) {
Stefan Bellera04f8192013-07-19 22:26:32 +0200536 trees_used = info->fn(n, mask, dirmask, entry, info);
537 if (trees_used < 0) {
538 error = trees_used;
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700539 if (!info->show_all_errors)
540 break;
541 }
Stefan Bellera04f8192013-07-19 22:26:32 +0200542 mask &= trees_used;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200543 }
Junio C Hamano1ee26572009-09-19 14:07:14 -0700544 for (i = 0; i < n; i++)
Linus Torvalds5803c6f2008-03-05 19:44:06 -0800545 if (mask & (1ul << i))
Junio C Hamano1ee26572009-09-19 14:07:14 -0700546 update_extended_entry(tx + i, entry + i);
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800547 }
Junio C Hamano1ee26572009-09-19 14:07:14 -0700548 for (i = 0; i < n; i++)
549 free_extended_entry(tx + i);
David Turnerd9c2bd52015-12-21 17:34:20 -0500550 free(traverse_path);
551 info->traverse_path = NULL;
Junio C Hamano2842c0f2011-08-29 12:26:05 -0700552 strbuf_release(&base);
Derrick Stoleeda8be8c2021-01-04 03:09:10 +0000553
554 traverse_trees_cur_depth--;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200555 return error;
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800556}
557
David Turner275721c2015-05-20 13:03:38 -0400558struct dir_state {
559 void *tree;
560 unsigned long size;
brian m. carlson3b683bc2018-05-02 00:25:40 +0000561 struct object_id oid;
David Turner275721c2015-05-20 13:03:38 -0400562};
563
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700564static int find_tree_entry(struct repository *r, struct tree_desc *t,
565 const char *name, struct object_id *result,
566 unsigned short *mode)
Junio C Hamano4dcff632006-04-19 14:05:47 -0700567{
568 int namelen = strlen(name);
569 while (t->size) {
570 const char *entry;
brian m. carlson0a3faa42019-01-15 00:39:41 +0000571 struct object_id oid;
Junio C Hamano4dcff632006-04-19 14:05:47 -0700572 int entrylen, cmp;
573
brian m. carlson0a3faa42019-01-15 00:39:41 +0000574 oidcpy(&oid, tree_entry_extract(t, &entry, mode));
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100575 entrylen = tree_entry_len(&t->entry);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700576 update_tree_entry(t);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700577 if (entrylen > namelen)
578 continue;
579 cmp = memcmp(name, entry, entrylen);
580 if (cmp > 0)
581 continue;
582 if (cmp < 0)
583 break;
584 if (entrylen == namelen) {
brian m. carlson0a3faa42019-01-15 00:39:41 +0000585 oidcpy(result, &oid);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700586 return 0;
587 }
588 if (name[entrylen] != '/')
589 continue;
590 if (!S_ISDIR(*mode))
591 break;
592 if (++entrylen == namelen) {
brian m. carlson0a3faa42019-01-15 00:39:41 +0000593 oidcpy(result, &oid);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700594 return 0;
595 }
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700596 return get_tree_entry(r, &oid, name + entrylen, result, mode);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700597 }
598 return -1;
599}
600
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700601int get_tree_entry(struct repository *r,
602 const struct object_id *tree_oid,
603 const char *name,
604 struct object_id *oid,
605 unsigned short *mode)
Junio C Hamano4dcff632006-04-19 14:05:47 -0700606{
607 int retval;
608 void *tree;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700609 unsigned long size;
brian m. carlson916bc352018-03-12 02:27:51 +0000610 struct object_id root;
Junio C Hamano4dcff632006-04-19 14:05:47 -0700611
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +0100612 tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root);
Junio C Hamano4dcff632006-04-19 14:05:47 -0700613 if (!tree)
614 return -1;
Jeff Kingd93b7d12007-01-09 11:11:47 -0500615
616 if (name[0] == '\0') {
brian m. carlson916bc352018-03-12 02:27:51 +0000617 oidcpy(oid, &root);
René Scharfeef006502010-02-14 10:56:46 +0100618 free(tree);
Jeff Kingd93b7d12007-01-09 11:11:47 -0500619 return 0;
620 }
621
Junio C Hamano5fb8c052011-10-27 11:18:40 -0700622 if (!size) {
623 retval = -1;
624 } else {
625 struct tree_desc t;
626 init_tree_desc(&t, tree, size);
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700627 retval = find_tree_entry(r, &t, name, oid, mode);
Junio C Hamano5fb8c052011-10-27 11:18:40 -0700628 }
Junio C Hamano4dcff632006-04-19 14:05:47 -0700629 free(tree);
630 return retval;
631}
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +0700632
David Turner275721c2015-05-20 13:03:38 -0400633/*
634 * This is Linux's built-in max for the number of symlinks to follow.
635 * That limit, of course, does not affect git, but it's a reasonable
636 * choice.
637 */
638#define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
639
640/**
641 * Find a tree entry by following symlinks in tree_sha (which is
642 * assumed to be the root of the repository). In the event that a
643 * symlink points outside the repository (e.g. a link to /foo or a
644 * root-level link to ../foo), the portion of the link which is
645 * outside the repository will be returned in result_path, and *mode
646 * will be set to 0. It is assumed that result_path is uninitialized.
647 * If there are no symlinks, or the end result of the symlink chain
648 * points to an object inside the repository, result will be filled in
649 * with the sha1 of the found object, and *mode will hold the mode of
650 * the object.
651 *
David Turnerd1dd94b2019-01-17 23:19:43 -0500652 * See the code for enum get_oid_result for a description of
David Turner275721c2015-05-20 13:03:38 -0400653 * the return values.
654 */
Nguyễn Thái Ngọc Duy0dd1f0c2019-06-27 16:28:50 +0700655enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r,
656 struct object_id *tree_oid, const char *name,
657 struct object_id *result, struct strbuf *result_path,
658 unsigned short *mode)
David Turner275721c2015-05-20 13:03:38 -0400659{
660 int retval = MISSING_OBJECT;
661 struct dir_state *parents = NULL;
662 size_t parents_alloc = 0;
Ramsay Jones071bcaa2017-09-21 17:49:38 +0100663 size_t i, parents_nr = 0;
brian m. carlson0d4a8b52018-03-12 02:27:49 +0000664 struct object_id current_tree_oid;
David Turner275721c2015-05-20 13:03:38 -0400665 struct strbuf namebuf = STRBUF_INIT;
666 struct tree_desc t;
667 int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS;
David Turner275721c2015-05-20 13:03:38 -0400668
669 init_tree_desc(&t, NULL, 0UL);
David Turner275721c2015-05-20 13:03:38 -0400670 strbuf_addstr(&namebuf, name);
brian m. carlson3b683bc2018-05-02 00:25:40 +0000671 oidcpy(&current_tree_oid, tree_oid);
David Turner275721c2015-05-20 13:03:38 -0400672
673 while (1) {
674 int find_result;
675 char *first_slash;
676 char *remainder = NULL;
677
678 if (!t.buffer) {
679 void *tree;
brian m. carlson0d4a8b52018-03-12 02:27:49 +0000680 struct object_id root;
David Turner275721c2015-05-20 13:03:38 -0400681 unsigned long size;
Nguyễn Thái Ngọc Duy0dd1f0c2019-06-27 16:28:50 +0700682 tree = read_object_with_reference(r,
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +0700683 &current_tree_oid,
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +0100684 OBJ_TREE, &size,
brian m. carlson02f05472018-03-12 02:27:52 +0000685 &root);
David Turner275721c2015-05-20 13:03:38 -0400686 if (!tree)
687 goto done;
688
689 ALLOC_GROW(parents, parents_nr + 1, parents_alloc);
690 parents[parents_nr].tree = tree;
691 parents[parents_nr].size = size;
brian m. carlson3b683bc2018-05-02 00:25:40 +0000692 oidcpy(&parents[parents_nr].oid, &root);
David Turner275721c2015-05-20 13:03:38 -0400693 parents_nr++;
694
695 if (namebuf.buf[0] == '\0') {
brian m. carlson3b683bc2018-05-02 00:25:40 +0000696 oidcpy(result, &root);
David Turner275721c2015-05-20 13:03:38 -0400697 retval = FOUND;
698 goto done;
699 }
700
701 if (!size)
702 goto done;
703
704 /* descend */
705 init_tree_desc(&t, tree, size);
706 }
707
708 /* Handle symlinks to e.g. a//b by removing leading slashes */
709 while (namebuf.buf[0] == '/') {
710 strbuf_remove(&namebuf, 0, 1);
711 }
712
713 /* Split namebuf into a first component and a remainder */
714 if ((first_slash = strchr(namebuf.buf, '/'))) {
715 *first_slash = 0;
716 remainder = first_slash + 1;
717 }
718
719 if (!strcmp(namebuf.buf, "..")) {
720 struct dir_state *parent;
721 /*
722 * We could end up with .. in the namebuf if it
723 * appears in a symlink.
724 */
725
726 if (parents_nr == 1) {
727 if (remainder)
728 *first_slash = '/';
729 strbuf_add(result_path, namebuf.buf,
730 namebuf.len);
731 *mode = 0;
732 retval = FOUND;
733 goto done;
734 }
735 parent = &parents[parents_nr - 1];
736 free(parent->tree);
737 parents_nr--;
738 parent = &parents[parents_nr - 1];
739 init_tree_desc(&t, parent->tree, parent->size);
740 strbuf_remove(&namebuf, 0, remainder ? 3 : 2);
741 continue;
742 }
743
744 /* We could end up here via a symlink to dir/.. */
745 if (namebuf.buf[0] == '\0') {
brian m. carlson3b683bc2018-05-02 00:25:40 +0000746 oidcpy(result, &parents[parents_nr - 1].oid);
David Turner275721c2015-05-20 13:03:38 -0400747 retval = FOUND;
748 goto done;
749 }
750
751 /* Look up the first (or only) path component in the tree. */
Nguyễn Thái Ngọc Duy0dd1f0c2019-06-27 16:28:50 +0700752 find_result = find_tree_entry(r, &t, namebuf.buf,
brian m. carlson916bc352018-03-12 02:27:51 +0000753 &current_tree_oid, mode);
David Turner275721c2015-05-20 13:03:38 -0400754 if (find_result) {
755 goto done;
756 }
757
758 if (S_ISDIR(*mode)) {
759 if (!remainder) {
brian m. carlson3b683bc2018-05-02 00:25:40 +0000760 oidcpy(result, &current_tree_oid);
David Turner275721c2015-05-20 13:03:38 -0400761 retval = FOUND;
762 goto done;
763 }
764 /* Descend the tree */
765 t.buffer = NULL;
766 strbuf_remove(&namebuf, 0,
767 1 + first_slash - namebuf.buf);
768 } else if (S_ISREG(*mode)) {
769 if (!remainder) {
brian m. carlson3b683bc2018-05-02 00:25:40 +0000770 oidcpy(result, &current_tree_oid);
David Turner275721c2015-05-20 13:03:38 -0400771 retval = FOUND;
772 } else {
773 retval = NOT_DIR;
774 }
775 goto done;
776 } else if (S_ISLNK(*mode)) {
777 /* Follow a symlink */
778 unsigned long link_len;
779 size_t len;
780 char *contents, *contents_start;
781 struct dir_state *parent;
782 enum object_type type;
783
784 if (follows_remaining-- == 0) {
785 /* Too many symlinks followed */
786 retval = SYMLINK_LOOP;
787 goto done;
788 }
789
790 /*
791 * At this point, we have followed at a least
792 * one symlink, so on error we need to report this.
793 */
794 retval = DANGLING_SYMLINK;
795
Nguyễn Thái Ngọc Duy0dd1f0c2019-06-27 16:28:50 +0700796 contents = repo_read_object_file(r,
797 &current_tree_oid, &type,
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000798 &link_len);
David Turner275721c2015-05-20 13:03:38 -0400799
800 if (!contents)
801 goto done;
802
803 if (contents[0] == '/') {
804 strbuf_addstr(result_path, contents);
805 free(contents);
806 *mode = 0;
807 retval = FOUND;
808 goto done;
809 }
810
811 if (remainder)
812 len = first_slash - namebuf.buf;
813 else
814 len = namebuf.len;
815
816 contents_start = contents;
817
818 parent = &parents[parents_nr - 1];
819 init_tree_desc(&t, parent->tree, parent->size);
820 strbuf_splice(&namebuf, 0, len,
821 contents_start, link_len);
822 if (remainder)
823 namebuf.buf[link_len] = '/';
824 free(contents);
825 }
826 }
827done:
828 for (i = 0; i < parents_nr; i++)
829 free(parents[i].tree);
830 free(parents);
831
832 strbuf_release(&namebuf);
833 return retval;
834}
835
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700836static int match_entry(const struct pathspec_item *item,
837 const struct name_entry *entry, int pathlen,
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700838 const char *match, int matchlen,
Nguyễn Thái Ngọc Duyc3a47ca2012-10-20 00:14:42 +0700839 enum interesting *never_interesting)
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700840{
841 int m = -1; /* signals that we haven't called strncmp() */
842
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700843 if (item->magic & PATHSPEC_ICASE)
844 /*
845 * "Never interesting" trick requires exact
846 * matching. We could do something clever with inexact
847 * matching, but it's trickier (and not to forget that
848 * strcasecmp is locale-dependent, at least in
849 * glibc). Just disable it for now. It can't be worse
850 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
851 * pattern.
852 */
853 *never_interesting = entry_not_interesting;
854 else if (*never_interesting != entry_not_interesting) {
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700855 /*
856 * We have not seen any match that sorts later
857 * than the current path.
858 */
859
860 /*
861 * Does match sort strictly earlier than path
862 * with their common parts?
863 */
864 m = strncmp(match, entry->path,
865 (matchlen < pathlen) ? matchlen : pathlen);
866 if (m < 0)
867 return 0;
868
869 /*
870 * If we come here even once, that means there is at
871 * least one pathspec that would sort equal to or
872 * later than the path we are currently looking at.
873 * In other words, if we have never reached this point
874 * after iterating all pathspecs, it means all
875 * pathspecs are either outside of base, or inside the
876 * base but sorts strictly earlier than the current
877 * one. In either case, they will never match the
878 * subsequent entries. In such a case, we initialized
879 * the variable to -1 and that is what will be
880 * returned, allowing the caller to terminate early.
881 */
Nguyễn Thái Ngọc Duyc3a47ca2012-10-20 00:14:42 +0700882 *never_interesting = entry_not_interesting;
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700883 }
884
885 if (pathlen > matchlen)
886 return 0;
887
888 if (matchlen > pathlen) {
889 if (match[pathlen] != '/')
890 return 0;
SZEDER Gábor35a9f1e2020-06-05 13:00:23 +0000891 /*
892 * Reject non-directories as partial pathnames, except
893 * when match is a submodule with a trailing slash and
894 * nothing else (to handle 'submod/' and 'submod'
895 * uniformly).
896 */
897 if (!S_ISDIR(entry->mode) &&
898 (!S_ISGITLINK(entry->mode) || matchlen > pathlen + 1))
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700899 return 0;
900 }
901
902 if (m == -1)
903 /*
904 * we cheated and did not do strncmp(), so we do
905 * that here.
906 */
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700907 m = ps_strncmp(item, match, entry->path, pathlen);
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700908
909 /*
910 * If common part matched earlier then it is a hit,
911 * because we rejected the case where path is not a
912 * leading directory and is shorter than match.
913 */
914 if (!m)
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700915 /*
916 * match_entry does not check if the prefix part is
917 * matched case-sensitively. If the entry is a
918 * directory and part of prefix, it'll be rematched
919 * eventually by basecmp with special treatment for
920 * the prefix.
921 */
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700922 return 1;
923
924 return 0;
925}
926
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700927/* :(icase)-aware string compare */
928static int basecmp(const struct pathspec_item *item,
929 const char *base, const char *match, int len)
930{
931 if (item->magic & PATHSPEC_ICASE) {
932 int ret, n = len > item->prefix ? item->prefix : len;
933 ret = strncmp(base, match, n);
934 if (ret)
935 return ret;
936 base += n;
937 match += n;
938 len -= n;
939 }
940 return ps_strncmp(item, base, match, len);
941}
942
943static int match_dir_prefix(const struct pathspec_item *item,
944 const char *base,
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700945 const char *match, int matchlen)
946{
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700947 if (basecmp(item, base, match, matchlen))
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +0700948 return 0;
949
950 /*
951 * If the base is a subdirectory of a path which
952 * was specified, all of them are interesting.
953 */
954 if (!matchlen ||
955 base[matchlen] == '/' ||
956 match[matchlen - 1] == '/')
957 return 1;
958
959 /* Just a random prefix match */
960 return 0;
961}
962
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +0700963/*
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +0700964 * Perform matching on the leading non-wildcard part of
965 * pathspec. item->nowildcard_len must be greater than zero. Return
966 * non-zero if base is matched.
967 */
968static int match_wildcard_base(const struct pathspec_item *item,
969 const char *base, int baselen,
970 int *matched)
971{
972 const char *match = item->match;
973 /* the wildcard part is not considered in this function */
974 int matchlen = item->nowildcard_len;
975
976 if (baselen) {
977 int dirlen;
978 /*
979 * Return early if base is longer than the
980 * non-wildcard part but it does not match.
981 */
982 if (baselen >= matchlen) {
983 *matched = matchlen;
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700984 return !basecmp(item, base, match, matchlen);
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +0700985 }
986
987 dirlen = matchlen;
988 while (dirlen && match[dirlen - 1] != '/')
989 dirlen--;
990
991 /*
992 * Return early if base is shorter than the
993 * non-wildcard part but it does not match. Note that
994 * base ends with '/' so we are sure it really matches
995 * directory
996 */
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700997 if (basecmp(item, base, match, baselen))
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +0700998 return 0;
999 *matched = baselen;
1000 } else
1001 *matched = 0;
1002 /*
1003 * we could have checked entry against the non-wildcard part
1004 * that is not in base and does similar never_interesting
1005 * optimization as in match_entry. For now just be happy with
1006 * base comparison.
1007 */
1008 return entry_interesting;
1009}
1010
1011/*
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001012 * Is a tree entry interesting given the pathspec we have?
1013 *
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001014 * Pre-condition: either baselen == base_offset (i.e. empty path)
1015 * or base[baselen-1] == '/' (i.e. with trailing slash).
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001016 */
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01001017static enum interesting do_match(struct index_state *istate,
1018 const struct name_entry *entry,
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001019 struct strbuf *base, int base_offset,
1020 const struct pathspec *ps,
1021 int exclude)
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001022{
1023 int i;
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001024 int pathlen, baselen = base->len - base_offset;
Nguyễn Thái Ngọc Duyc3a47ca2012-10-20 00:14:42 +07001025 enum interesting never_interesting = ps->has_wildcard ?
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +11001026 entry_not_interesting : all_entries_not_interesting;
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001027
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +07001028 GUARD_PATHSPEC(ps,
1029 PATHSPEC_FROMTOP |
1030 PATHSPEC_MAXDEPTH |
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +07001031 PATHSPEC_LITERAL |
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +07001032 PATHSPEC_GLOB |
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001033 PATHSPEC_ICASE |
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001034 PATHSPEC_EXCLUDE |
1035 PATHSPEC_ATTR);
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +07001036
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +07001037 if (!ps->nr) {
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +07001038 if (!ps->recursive ||
1039 !(ps->magic & PATHSPEC_MAXDEPTH) ||
1040 ps->max_depth == -1)
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +11001041 return all_entries_interesting;
1042 return within_depth(base->buf + base_offset, baselen,
1043 !!S_ISDIR(entry->mode),
1044 ps->max_depth) ?
1045 entry_interesting : entry_not_interesting;
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +07001046 }
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001047
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +11001048 pathlen = tree_entry_len(entry);
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001049
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001050 for (i = ps->nr - 1; i >= 0; i--) {
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001051 const struct pathspec_item *item = ps->items+i;
1052 const char *match = item->match;
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001053 const char *base_str = base->buf + base_offset;
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +07001054 int matchlen = item->len, matched = 0;
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001055
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001056 if ((!exclude && item->magic & PATHSPEC_EXCLUDE) ||
1057 ( exclude && !(item->magic & PATHSPEC_EXCLUDE)))
1058 continue;
1059
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001060 if (baselen >= matchlen) {
1061 /* If it doesn't match, move along... */
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +07001062 if (!match_dir_prefix(item, base_str, match, matchlen))
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001063 goto match_wildcards;
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +07001064
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +07001065 if (!ps->recursive ||
1066 !(ps->magic & PATHSPEC_MAXDEPTH) ||
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001067 ps->max_depth == -1) {
1068 if (!item->attr_match_nr)
1069 return all_entries_interesting;
1070 else
1071 goto interesting;
1072 }
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +07001073
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001074 if (within_depth(base_str + matchlen + 1,
1075 baselen - matchlen - 1,
1076 !!S_ISDIR(entry->mode),
1077 ps->max_depth))
1078 goto interesting;
1079 else
1080 return entry_not_interesting;
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001081 }
1082
Dan McGee1b740922011-09-08 21:02:46 -05001083 /* Either there must be no base, or the base must match. */
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +07001084 if (baselen == 0 || !basecmp(item, base_str, match, baselen)) {
1085 if (match_entry(item, entry, pathlen,
Nguyễn Thái Ngọc Duy58c4d662010-12-15 22:02:43 +07001086 match + baselen, matchlen - baselen,
1087 &never_interesting))
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001088 goto interesting;
Nguyễn Thái Ngọc Duyf1a2ddb2010-12-15 22:02:47 +07001089
Nguyễn Thái Ngọc Duy170260a2012-11-18 16:13:06 +07001090 if (item->nowildcard_len < item->len) {
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +07001091 if (!git_fnmatch(item, match + baselen, entry->path,
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +07001092 item->nowildcard_len - baselen))
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001093 goto interesting;
Nguyễn Thái Ngọc Duyf1a2ddb2010-12-15 22:02:47 +07001094
1095 /*
1096 * Match all directories. We'll try to
1097 * match files later on.
1098 */
1099 if (ps->recursive && S_ISDIR(entry->mode))
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +11001100 return entry_interesting;
Brandon Williams74ed4372016-12-16 11:03:21 -08001101
1102 /*
1103 * When matching against submodules with
1104 * wildcard characters, ensure that the entry
1105 * at least matches up to the first wild
1106 * character. More accurate matching can then
1107 * be performed in the submodule itself.
1108 */
Brandon Williamseef3df52017-12-04 16:07:34 -08001109 if (ps->recurse_submodules &&
1110 S_ISGITLINK(entry->mode) &&
Brandon Williams74ed4372016-12-16 11:03:21 -08001111 !ps_strncmp(item, match + baselen,
1112 entry->path,
1113 item->nowildcard_len - baselen))
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001114 goto interesting;
Nguyễn Thái Ngọc Duyf1a2ddb2010-12-15 22:02:47 +07001115 }
1116
1117 continue;
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001118 }
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001119
1120match_wildcards:
Nguyễn Thái Ngọc Duy170260a2012-11-18 16:13:06 +07001121 if (item->nowildcard_len == item->len)
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001122 continue;
1123
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +07001124 if (item->nowildcard_len &&
1125 !match_wildcard_base(item, base_str, baselen, &matched))
Andy Spencere4ddb052014-01-25 22:06:46 +00001126 continue;
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +07001127
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001128 /*
1129 * Concatenate base and entry->path into one and do
1130 * fnmatch() on it.
Nguyễn Thái Ngọc Duyc904cd82012-11-24 11:33:51 +07001131 *
1132 * While we could avoid concatenation in certain cases
1133 * [1], which saves a memcpy and potentially a
1134 * realloc, it turns out not worth it. Measurement on
1135 * linux-2.6 does not show any clear improvements,
1136 * partly because of the nowildcard_len optimization
1137 * in git_fnmatch(). Avoid micro-optimizations here.
1138 *
1139 * [1] if match_wildcard_base() says the base
1140 * directory is already matched, we only need to match
1141 * the rest, which is shorter so _in theory_ faster.
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001142 */
1143
1144 strbuf_add(base, entry->path, pathlen);
1145
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +07001146 if (!git_fnmatch(item, match, base->buf + base_offset,
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +07001147 item->nowildcard_len)) {
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001148 strbuf_setlen(base, base_offset + baselen);
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001149 goto interesting;
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001150 }
Brandon Williams74ed4372016-12-16 11:03:21 -08001151
1152 /*
1153 * When matching against submodules with
1154 * wildcard characters, ensure that the entry
1155 * at least matches up to the first wild
1156 * character. More accurate matching can then
1157 * be performed in the submodule itself.
1158 */
Brandon Williamseef3df52017-12-04 16:07:34 -08001159 if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&
Brandon Williams74ed4372016-12-16 11:03:21 -08001160 !ps_strncmp(item, match, base->buf + base_offset,
1161 item->nowildcard_len)) {
1162 strbuf_setlen(base, base_offset + baselen);
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001163 goto interesting;
Brandon Williams74ed4372016-12-16 11:03:21 -08001164 }
1165
Nguyễn Thái Ngọc Duy1376e502010-12-17 19:45:33 +07001166 strbuf_setlen(base, base_offset + baselen);
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001167
1168 /*
1169 * Match all directories. We'll try to match files
1170 * later on.
Nguyễn Thái Ngọc Duy8c69c1f2012-01-14 16:23:22 +07001171 * max_depth is ignored but we may consider support it
1172 * in future, see
Jeff King3eae30e2019-11-27 07:54:04 -05001173 * https://lore.kernel.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
Nguyễn Thái Ngọc Duyd38f2802010-12-15 22:02:46 +07001174 */
1175 if (ps->recursive && S_ISDIR(entry->mode))
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +11001176 return entry_interesting;
Nguyễn Thái Ngọc Duy5a0b97b2018-11-18 17:48:00 +01001177 continue;
1178interesting:
1179 if (item->attr_match_nr) {
1180 int ret;
1181
1182 /*
1183 * Must not return all_entries_not_interesting
1184 * prematurely. We do not know if all entries do not
1185 * match some attributes with current attr API.
1186 */
1187 never_interesting = entry_not_interesting;
1188
1189 /*
1190 * Consider all directories interesting (because some
1191 * of those files inside may match some attributes
1192 * even though the parent dir does not)
1193 *
1194 * FIXME: attributes _can_ match directories and we
1195 * can probably return all_entries_interesting or
1196 * all_entries_not_interesting here if matched.
1197 */
1198 if (S_ISDIR(entry->mode))
1199 return entry_interesting;
1200
1201 strbuf_add(base, entry->path, pathlen);
1202 ret = match_pathspec_attrs(istate, base->buf + base_offset,
1203 base->len - base_offset, item);
1204 strbuf_setlen(base, base_offset + baselen);
1205 if (!ret)
1206 continue;
1207 }
1208 return entry_interesting;
Nguyễn Thái Ngọc Duy2c389fc2010-12-15 22:02:40 +07001209 }
1210 return never_interesting; /* No matches */
1211}
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001212
1213/*
1214 * Is a tree entry interesting given the pathspec we have?
1215 *
1216 * Pre-condition: either baselen == base_offset (i.e. empty path)
1217 * or base[baselen-1] == '/' (i.e. with trailing slash).
1218 */
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01001219enum interesting tree_entry_interesting(struct index_state *istate,
1220 const struct name_entry *entry,
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001221 struct strbuf *base, int base_offset,
1222 const struct pathspec *ps)
1223{
1224 enum interesting positive, negative;
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01001225 positive = do_match(istate, entry, base, base_offset, ps, 0);
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001226
1227 /*
1228 * case | entry | positive | negative | result
1229 * -----+-------+----------+----------+-------
1230 * 1 | file | -1 | -1..2 | -1
1231 * 2 | file | 0 | -1..2 | 0
1232 * 3 | file | 1 | -1 | 1
1233 * 4 | file | 1 | 0 | 1
1234 * 5 | file | 1 | 1 | 0
1235 * 6 | file | 1 | 2 | 0
1236 * 7 | file | 2 | -1 | 2
Nguyễn Thái Ngọc Duyb7845ce2018-11-04 06:28:51 +01001237 * 8 | file | 2 | 0 | 1
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001238 * 9 | file | 2 | 1 | 0
1239 * 10 | file | 2 | 2 | -1
1240 * -----+-------+----------+----------+-------
1241 * 11 | dir | -1 | -1..2 | -1
1242 * 12 | dir | 0 | -1..2 | 0
1243 * 13 | dir | 1 | -1 | 1
1244 * 14 | dir | 1 | 0 | 1
1245 * 15 | dir | 1 | 1 | 1 (*)
1246 * 16 | dir | 1 | 2 | 0
1247 * 17 | dir | 2 | -1 | 2
Nguyễn Thái Ngọc Duyb7845ce2018-11-04 06:28:51 +01001248 * 18 | dir | 2 | 0 | 1
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001249 * 19 | dir | 2 | 1 | 1 (*)
1250 * 20 | dir | 2 | 2 | -1
1251 *
1252 * (*) An exclude pattern interested in a directory does not
1253 * necessarily mean it will exclude all of the directory. In
1254 * wildcard case, it can't decide until looking at individual
1255 * files inside. So don't write such directories off yet.
1256 */
1257
1258 if (!(ps->magic & PATHSPEC_EXCLUDE) ||
1259 positive <= entry_not_interesting) /* #1, #2, #11, #12 */
1260 return positive;
1261
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01001262 negative = do_match(istate, entry, base, base_offset, ps, 1);
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001263
Nguyễn Thái Ngọc Duyb7845ce2018-11-04 06:28:51 +01001264 /* #8, #18 */
1265 if (positive == all_entries_interesting &&
1266 negative == entry_not_interesting)
1267 return entry_interesting;
1268
1269 /* #3, #4, #7, #13, #14, #17 */
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +07001270 if (negative <= entry_not_interesting)
1271 return positive;
1272
1273 /* #15, #19 */
1274 if (S_ISDIR(entry->mode) &&
1275 positive >= entry_interesting &&
1276 negative == entry_interesting)
1277 return entry_interesting;
1278
1279 if ((positive == entry_interesting &&
1280 negative >= entry_interesting) || /* #5, #6, #16 */
1281 (positive == all_entries_interesting &&
1282 negative == entry_interesting)) /* #9 */
1283 return entry_not_interesting;
1284
1285 return all_entries_not_interesting; /* #10, #20 */
1286}