tree-walk: add a strbuf wrapper for make_traverse_path()
[git/git.git] / tree-walk.c
CommitLineData
1b0c7174
JH
1#include "cache.h"
2#include "tree-walk.h"
e6c111b4 3#include "unpack-trees.h"
bc96cc87 4#include "dir.h"
cbd53a21 5#include "object-store.h"
8e440259 6#include "tree.h"
64acde94 7#include "pathspec.h"
1b0c7174 8
4651ece8
LT
9static const char *get_mode(const char *str, unsigned int *modep)
10{
11 unsigned char c;
12 unsigned int mode = 0;
13
64cc1c09
MK
14 if (*str == ' ')
15 return NULL;
16
4651ece8
LT
17 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
8354fa3d 26static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
4651ece8
LT
27{
28 const char *path;
29 unsigned int mode, len;
83e4b757 30 const unsigned hashsz = the_hash_algo->rawsz;
4651ece8 31
83e4b757 32 if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
8354fa3d
DT
33 strbuf_addstr(err, _("too-short tree object"));
34 return -1;
35 }
64cc1c09 36
4651ece8 37 path = get_mode(buf, &mode);
8354fa3d
DT
38 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 }
4651ece8
LT
46 len = strlen(path) + 1;
47
48 /* Initialize the descriptor entry */
49 desc->entry.path = path;
7146e66f 50 desc->entry.mode = canon_mode(mode);
ea82b2a0 51 desc->entry.pathlen = len - 1;
52 hashcpy(desc->entry.oid.hash, (const unsigned char *)path + len);
8354fa3d
DT
53
54 return 0;
4651ece8
LT
55}
56
8354fa3d 57static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer, unsigned long size, struct strbuf *err)
6fda5e51
LT
58{
59 desc->buffer = buffer;
60 desc->size = size;
4651ece8 61 if (size)
8354fa3d
DT
62 return decode_tree_entry(desc, buffer, size, err);
63 return 0;
64}
65
66void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
67{
68 struct strbuf err = STRBUF_INIT;
69 if (init_tree_desc_internal(desc, buffer, size, &err))
70 die("%s", err.buf);
71 strbuf_release(&err);
72}
73
74int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned long size)
75{
76 struct strbuf err = STRBUF_INIT;
77 int result = init_tree_desc_internal(desc, buffer, size, &err);
78 if (result)
79 error("%s", err.buf);
80 strbuf_release(&err);
81 return result;
6fda5e51
LT
82}
83
5c377d3d 84void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid)
1b0c7174
JH
85{
86 unsigned long size = 0;
87 void *buf = NULL;
88
5c377d3d 89 if (oid) {
02f0547e 90 buf = read_object_with_reference(oid, tree_type, &size, NULL);
1b0c7174 91 if (!buf)
5c377d3d 92 die("unable to read tree %s", oid_to_hex(oid));
1b0c7174 93 }
6fda5e51 94 init_tree_desc(desc, buf, size);
1b0c7174
JH
95 return buf;
96}
97
1b0c7174
JH
98static void entry_clear(struct name_entry *a)
99{
100 memset(a, 0, sizeof(*a));
101}
102
103static void entry_extract(struct tree_desc *t, struct name_entry *a)
104{
4651ece8 105 *a = t->entry;
1b0c7174
JH
106}
107
8354fa3d 108static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err)
1b0c7174 109{
6fda5e51 110 const void *buf = desc->buffer;
ea82b2a0 111 const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + the_hash_algo->rawsz;
1b0c7174 112 unsigned long size = desc->size;
4651ece8 113 unsigned long len = end - (const unsigned char *)buf;
1b0c7174
JH
114
115 if (size < len)
2edffef2 116 die(_("too-short tree file"));
4651ece8
LT
117 buf = end;
118 size -= len;
119 desc->buffer = buf;
120 desc->size = size;
121 if (size)
8354fa3d
DT
122 return decode_tree_entry(desc, buf, size, err);
123 return 0;
124}
125
126void update_tree_entry(struct tree_desc *desc)
127{
128 struct strbuf err = STRBUF_INIT;
129 if (update_tree_entry_internal(desc, &err))
130 die("%s", err.buf);
131 strbuf_release(&err);
132}
133
134int update_tree_entry_gently(struct tree_desc *desc)
135{
136 struct strbuf err = STRBUF_INIT;
137 if (update_tree_entry_internal(desc, &err)) {
138 error("%s", err.buf);
139 strbuf_release(&err);
140 /* Stop processing this tree after error */
141 desc->size = 0;
142 return -1;
143 }
144 strbuf_release(&err);
145 return 0;
1b0c7174
JH
146}
147
4c068a98
LT
148int tree_entry(struct tree_desc *desc, struct name_entry *entry)
149{
4651ece8 150 if (!desc->size)
4c068a98
LT
151 return 0;
152
4651ece8
LT
153 *entry = desc->entry;
154 update_tree_entry(desc);
4c068a98
LT
155 return 1;
156}
157
8354fa3d
DT
158int tree_entry_gently(struct tree_desc *desc, struct name_entry *entry)
159{
160 if (!desc->size)
161 return 0;
162
163 *entry = desc->entry;
164 if (update_tree_entry_gently(desc))
165 return 0;
166 return 1;
167}
168
40d934df
LT
169void setup_traverse_info(struct traverse_info *info, const char *base)
170{
37806080 171 size_t pathlen = strlen(base);
bcbe5a51 172 static struct traverse_info dummy;
40d934df
LT
173
174 memset(info, 0, sizeof(*info));
175 if (pathlen && base[pathlen-1] == '/')
176 pathlen--;
177 info->pathlen = pathlen ? pathlen + 1 : 0;
90553847
JK
178 info->name = base;
179 info->namelen = pathlen;
947208b7 180 if (pathlen)
bcbe5a51 181 info->prev = &dummy;
40d934df
LT
182}
183
90553847
JK
184char *make_traverse_path(char *path, const struct traverse_info *info,
185 const char *name, size_t namelen)
40d934df 186{
37806080 187 size_t pathlen = info->pathlen;
40d934df 188
90553847 189 path[pathlen + namelen] = 0;
40d934df 190 for (;;) {
90553847 191 memcpy(path + pathlen, name, namelen);
40d934df
LT
192 if (!pathlen)
193 break;
194 path[--pathlen] = '/';
90553847
JK
195 name = info->name;
196 namelen = info->namelen;
40d934df 197 info = info->prev;
90553847 198 pathlen -= namelen;
40d934df
LT
199 }
200 return path;
201}
202
c43ab062
JK
203void strbuf_make_traverse_path(struct strbuf *out,
204 const struct traverse_info *info,
205 const char *name, size_t namelen)
206{
207 size_t len = traverse_path_len(info, namelen);
208
209 strbuf_grow(out, len);
210 make_traverse_path(out->buf + out->len, info, name, namelen);
211 strbuf_setlen(out, out->len + len);
212}
213
1ee26571
JH
214struct tree_desc_skip {
215 struct tree_desc_skip *prev;
216 const void *ptr;
217};
218
219struct tree_desc_x {
220 struct tree_desc d;
221 struct tree_desc_skip *skip;
222};
223
1ee26571
JH
224static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
225{
226 /*
227 * The caller wants to pick *a* from a tree or nothing.
228 * We are looking at *b* in a tree.
229 *
230 * (0) If a and b are the same name, we are trivially happy.
231 *
232 * There are three possibilities where *a* could be hiding
233 * behind *b*.
234 *
235 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
236 * matter what.
237 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
238 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
239 *
240 * Otherwise we know *a* won't appear in the tree without
241 * scanning further.
242 */
243
244 int cmp = name_compare(a, a_len, b, b_len);
245
246 /* Most common case first -- reading sync'd trees */
247 if (!cmp)
248 return cmp;
249
250 if (0 < cmp) {
251 /* a comes after b; it does not matter if it is case (3)
252 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
253 return 1;
254 */
255 return 1; /* keep looking */
256 }
257
258 /* b comes after a; are we looking at case (2)? */
259 if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/')
260 return 1; /* keep looking */
261
262 return -1; /* a cannot appear in the tree */
263}
264
265/*
266 * From the extended tree_desc, extract the first name entry, while
267 * paying attention to the candidate "first" name. Most importantly,
268 * when looking for an entry, if there are entries that sorts earlier
269 * in the tree object representation than that name, skip them and
270 * process the named entry first. We will remember that we haven't
271 * processed the first entry yet, and in the later call skip the
272 * entry we processed early when update_extended_entry() is called.
273 *
274 * E.g. if the underlying tree object has these entries:
275 *
276 * blob "t-1"
277 * blob "t-2"
278 * tree "t"
279 * blob "t=1"
280 *
281 * and the "first" asks for "t", remember that we still need to
282 * process "t-1" and "t-2" but extract "t". After processing the
283 * entry "t" from this call, the caller will let us know by calling
284 * update_extended_entry() that we can remember "t" has been processed
285 * already.
286 */
287
288static void extended_entry_extract(struct tree_desc_x *t,
289 struct name_entry *a,
290 const char *first,
291 int first_len)
292{
293 const char *path;
294 int len;
295 struct tree_desc probe;
296 struct tree_desc_skip *skip;
297
298 /*
299 * Extract the first entry from the tree_desc, but skip the
300 * ones that we already returned in earlier rounds.
301 */
302 while (1) {
303 if (!t->d.size) {
304 entry_clear(a);
305 break; /* not found */
306 }
307 entry_extract(&t->d, a);
308 for (skip = t->skip; skip; skip = skip->prev)
309 if (a->path == skip->ptr)
310 break; /* found */
311 if (!skip)
312 break;
313 /* We have processed this entry already. */
314 update_tree_entry(&t->d);
315 }
316
317 if (!first || !a->path)
318 return;
319
320 /*
321 * The caller wants "first" from this tree, or nothing.
322 */
323 path = a->path;
0de16337 324 len = tree_entry_len(a);
1ee26571
JH
325 switch (check_entry_match(first, first_len, path, len)) {
326 case -1:
327 entry_clear(a);
328 case 0:
329 return;
330 default:
331 break;
332 }
333
334 /*
335 * We need to look-ahead -- we suspect that a subtree whose
336 * name is "first" may be hiding behind the current entry "path".
337 */
338 probe = t->d;
339 while (probe.size) {
340 entry_extract(&probe, a);
341 path = a->path;
0de16337 342 len = tree_entry_len(a);
1ee26571
JH
343 switch (check_entry_match(first, first_len, path, len)) {
344 case -1:
345 entry_clear(a);
346 case 0:
347 return;
348 default:
349 update_tree_entry(&probe);
350 break;
351 }
352 /* keep looking */
353 }
354 entry_clear(a);
355}
356
357static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a)
358{
359 if (t->d.entry.path == a->path) {
360 update_tree_entry(&t->d);
361 } else {
362 /* we have returned this entry early */
363 struct tree_desc_skip *skip = xmalloc(sizeof(*skip));
364 skip->ptr = a->path;
365 skip->prev = t->skip;
366 t->skip = skip;
367 }
368}
369
370static void free_extended_entry(struct tree_desc_x *t)
371{
372 struct tree_desc_skip *p, *s;
373
374 for (s = t->skip; s; s = p) {
375 p = s->prev;
376 free(s);
377 }
378}
379
67022e02
NTND
380static inline int prune_traversal(struct index_state *istate,
381 struct name_entry *e,
2842c0f9
JH
382 struct traverse_info *info,
383 struct strbuf *base,
384 int still_interesting)
385{
386 if (!info->pathspec || still_interesting == 2)
387 return 2;
388 if (still_interesting < 0)
389 return still_interesting;
67022e02
NTND
390 return tree_entry_interesting(istate, e, base,
391 0, info->pathspec);
2842c0f9
JH
392}
393
67022e02
NTND
394int traverse_trees(struct index_state *istate,
395 int n, struct tree_desc *t,
396 struct traverse_info *info)
1b0c7174 397{
e6c111b4 398 int error = 0;
1b0c7174 399 struct name_entry *entry = xmalloc(n*sizeof(*entry));
1ee26571
JH
400 int i;
401 struct tree_desc_x *tx = xcalloc(n, sizeof(*tx));
2842c0f9
JH
402 struct strbuf base = STRBUF_INIT;
403 int interesting = 1;
d9c2bd56 404 char *traverse_path;
1ee26571
JH
405
406 for (i = 0; i < n; i++)
407 tx[i].d = t[i];
1b0c7174 408
2842c0f9 409 if (info->prev) {
c43ab062
JK
410 strbuf_make_traverse_path(&base, info->prev,
411 info->name, info->namelen);
412 strbuf_addch(&base, '/');
413 traverse_path = xstrndup(base.buf, base.len);
d9c2bd56 414 } else {
90553847 415 traverse_path = xstrndup(info->name, info->pathlen);
2842c0f9 416 }
d9c2bd56 417 info->traverse_path = traverse_path;
1b0c7174 418 for (;;) {
a04f8196 419 int trees_used;
1ee26571
JH
420 unsigned long mask, dirmask;
421 const char *first = NULL;
422 int first_len = 0;
27c0f768 423 struct name_entry *e = NULL;
1ee26571
JH
424 int len;
425
426 for (i = 0; i < n; i++) {
427 e = entry + i;
428 extended_entry_extract(tx + i, e, NULL, 0);
429 }
1b0c7174 430
1ee26571
JH
431 /*
432 * A tree may have "t-2" at the current location even
433 * though it may have "t" that is a subtree behind it,
434 * and another tree may return "t". We want to grab
435 * all "t" from all trees to match in such a case.
436 */
1b0c7174 437 for (i = 0; i < n; i++) {
1ee26571
JH
438 e = entry + i;
439 if (!e->path)
1b0c7174 440 continue;
0de16337 441 len = tree_entry_len(e);
1ee26571
JH
442 if (!first) {
443 first = e->path;
444 first_len = len;
445 continue;
446 }
447 if (name_compare(e->path, len, first, first_len) < 0) {
448 first = e->path;
449 first_len = len;
450 }
451 }
452
453 if (first) {
454 for (i = 0; i < n; i++) {
455 e = entry + i;
456 extended_entry_extract(tx + i, e, first, first_len);
457 /* Cull the ones that are not the earliest */
458 if (!e->path)
1b0c7174 459 continue;
0de16337 460 len = tree_entry_len(e);
1ee26571
JH
461 if (name_compare(e->path, len, first, first_len))
462 entry_clear(e);
1b0c7174 463 }
1ee26571
JH
464 }
465
466 /* Now we have in entry[i] the earliest name from the trees */
467 mask = 0;
468 dirmask = 0;
469 for (i = 0; i < n; i++) {
470 if (!entry[i].path)
471 continue;
1b0c7174 472 mask |= 1ul << i;
91e4f036
LT
473 if (S_ISDIR(entry[i].mode))
474 dirmask |= 1ul << i;
2842c0f9 475 e = &entry[i];
1b0c7174
JH
476 }
477 if (!mask)
478 break;
67022e02 479 interesting = prune_traversal(istate, e, info, &base, interesting);
2842c0f9
JH
480 if (interesting < 0)
481 break;
482 if (interesting) {
a04f8196
SB
483 trees_used = info->fn(n, mask, dirmask, entry, info);
484 if (trees_used < 0) {
485 error = trees_used;
2842c0f9
JH
486 if (!info->show_all_errors)
487 break;
488 }
a04f8196 489 mask &= trees_used;
e6c111b4 490 }
1ee26571 491 for (i = 0; i < n; i++)
5803c6f8 492 if (mask & (1ul << i))
1ee26571 493 update_extended_entry(tx + i, entry + i);
1b0c7174
JH
494 }
495 free(entry);
1ee26571
JH
496 for (i = 0; i < n; i++)
497 free_extended_entry(tx + i);
498 free(tx);
d9c2bd56
DT
499 free(traverse_path);
500 info->traverse_path = NULL;
2842c0f9 501 strbuf_release(&base);
e6c111b4 502 return error;
1b0c7174
JH
503}
504
275721c2
DT
505struct dir_state {
506 void *tree;
507 unsigned long size;
3b683bcf 508 struct object_id oid;
275721c2
DT
509};
510
5ec1e728 511static int find_tree_entry(struct tree_desc *t, const char *name, struct object_id *result, unsigned short *mode)
4dcff634
JH
512{
513 int namelen = strlen(name);
514 while (t->size) {
515 const char *entry;
0a3faa45 516 struct object_id oid;
4dcff634
JH
517 int entrylen, cmp;
518
0a3faa45 519 oidcpy(&oid, tree_entry_extract(t, &entry, mode));
0de16337 520 entrylen = tree_entry_len(&t->entry);
4dcff634 521 update_tree_entry(t);
4dcff634
JH
522 if (entrylen > namelen)
523 continue;
524 cmp = memcmp(name, entry, entrylen);
525 if (cmp > 0)
526 continue;
527 if (cmp < 0)
528 break;
529 if (entrylen == namelen) {
0a3faa45 530 oidcpy(result, &oid);
4dcff634
JH
531 return 0;
532 }
533 if (name[entrylen] != '/')
534 continue;
535 if (!S_ISDIR(*mode))
536 break;
537 if (++entrylen == namelen) {
0a3faa45 538 oidcpy(result, &oid);
4dcff634
JH
539 return 0;
540 }
0a3faa45 541 return get_tree_entry(&oid, name + entrylen, result, mode);
4dcff634
JH
542 }
543 return -1;
544}
545
5ec1e728 546int get_tree_entry(const struct object_id *tree_oid, const char *name, struct object_id *oid, unsigned short *mode)
4dcff634
JH
547{
548 int retval;
549 void *tree;
6fda5e51 550 unsigned long size;
916bc35b 551 struct object_id root;
4dcff634 552
02f0547e 553 tree = read_object_with_reference(tree_oid, tree_type, &size, &root);
4dcff634
JH
554 if (!tree)
555 return -1;
d93b7d1c
JK
556
557 if (name[0] == '\0') {
916bc35b 558 oidcpy(oid, &root);
ef006503 559 free(tree);
d93b7d1c
JK
560 return 0;
561 }
562
5fb8c05f
JH
563 if (!size) {
564 retval = -1;
565 } else {
566 struct tree_desc t;
567 init_tree_desc(&t, tree, size);
916bc35b 568 retval = find_tree_entry(&t, name, oid, mode);
5fb8c05f 569 }
4dcff634
JH
570 free(tree);
571 return retval;
572}
2c389fc8 573
275721c2
DT
574/*
575 * This is Linux's built-in max for the number of symlinks to follow.
576 * That limit, of course, does not affect git, but it's a reasonable
577 * choice.
578 */
579#define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
580
581/**
582 * Find a tree entry by following symlinks in tree_sha (which is
583 * assumed to be the root of the repository). In the event that a
584 * symlink points outside the repository (e.g. a link to /foo or a
585 * root-level link to ../foo), the portion of the link which is
586 * outside the repository will be returned in result_path, and *mode
587 * will be set to 0. It is assumed that result_path is uninitialized.
588 * If there are no symlinks, or the end result of the symlink chain
589 * points to an object inside the repository, result will be filled in
590 * with the sha1 of the found object, and *mode will hold the mode of
591 * the object.
592 *
d1dd94b3 593 * See the code for enum get_oid_result for a description of
275721c2
DT
594 * the return values.
595 */
5ec1e728 596enum get_oid_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned short *mode)
275721c2
DT
597{
598 int retval = MISSING_OBJECT;
599 struct dir_state *parents = NULL;
600 size_t parents_alloc = 0;
071bcaab 601 size_t i, parents_nr = 0;
0d4a8b5b 602 struct object_id current_tree_oid;
275721c2
DT
603 struct strbuf namebuf = STRBUF_INIT;
604 struct tree_desc t;
605 int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS;
275721c2
DT
606
607 init_tree_desc(&t, NULL, 0UL);
275721c2 608 strbuf_addstr(&namebuf, name);
3b683bcf 609 oidcpy(&current_tree_oid, tree_oid);
275721c2
DT
610
611 while (1) {
612 int find_result;
613 char *first_slash;
614 char *remainder = NULL;
615
616 if (!t.buffer) {
617 void *tree;
0d4a8b5b 618 struct object_id root;
275721c2 619 unsigned long size;
02f0547e 620 tree = read_object_with_reference(&current_tree_oid,
275721c2 621 tree_type, &size,
02f0547e 622 &root);
275721c2
DT
623 if (!tree)
624 goto done;
625
626 ALLOC_GROW(parents, parents_nr + 1, parents_alloc);
627 parents[parents_nr].tree = tree;
628 parents[parents_nr].size = size;
3b683bcf 629 oidcpy(&parents[parents_nr].oid, &root);
275721c2
DT
630 parents_nr++;
631
632 if (namebuf.buf[0] == '\0') {
3b683bcf 633 oidcpy(result, &root);
275721c2
DT
634 retval = FOUND;
635 goto done;
636 }
637
638 if (!size)
639 goto done;
640
641 /* descend */
642 init_tree_desc(&t, tree, size);
643 }
644
645 /* Handle symlinks to e.g. a//b by removing leading slashes */
646 while (namebuf.buf[0] == '/') {
647 strbuf_remove(&namebuf, 0, 1);
648 }
649
650 /* Split namebuf into a first component and a remainder */
651 if ((first_slash = strchr(namebuf.buf, '/'))) {
652 *first_slash = 0;
653 remainder = first_slash + 1;
654 }
655
656 if (!strcmp(namebuf.buf, "..")) {
657 struct dir_state *parent;
658 /*
659 * We could end up with .. in the namebuf if it
660 * appears in a symlink.
661 */
662
663 if (parents_nr == 1) {
664 if (remainder)
665 *first_slash = '/';
666 strbuf_add(result_path, namebuf.buf,
667 namebuf.len);
668 *mode = 0;
669 retval = FOUND;
670 goto done;
671 }
672 parent = &parents[parents_nr - 1];
673 free(parent->tree);
674 parents_nr--;
675 parent = &parents[parents_nr - 1];
676 init_tree_desc(&t, parent->tree, parent->size);
677 strbuf_remove(&namebuf, 0, remainder ? 3 : 2);
678 continue;
679 }
680
681 /* We could end up here via a symlink to dir/.. */
682 if (namebuf.buf[0] == '\0') {
3b683bcf 683 oidcpy(result, &parents[parents_nr - 1].oid);
275721c2
DT
684 retval = FOUND;
685 goto done;
686 }
687
688 /* Look up the first (or only) path component in the tree. */
689 find_result = find_tree_entry(&t, namebuf.buf,
916bc35b 690 &current_tree_oid, mode);
275721c2
DT
691 if (find_result) {
692 goto done;
693 }
694
695 if (S_ISDIR(*mode)) {
696 if (!remainder) {
3b683bcf 697 oidcpy(result, &current_tree_oid);
275721c2
DT
698 retval = FOUND;
699 goto done;
700 }
701 /* Descend the tree */
702 t.buffer = NULL;
703 strbuf_remove(&namebuf, 0,
704 1 + first_slash - namebuf.buf);
705 } else if (S_ISREG(*mode)) {
706 if (!remainder) {
3b683bcf 707 oidcpy(result, &current_tree_oid);
275721c2
DT
708 retval = FOUND;
709 } else {
710 retval = NOT_DIR;
711 }
712 goto done;
713 } else if (S_ISLNK(*mode)) {
714 /* Follow a symlink */
715 unsigned long link_len;
716 size_t len;
717 char *contents, *contents_start;
718 struct dir_state *parent;
719 enum object_type type;
720
721 if (follows_remaining-- == 0) {
722 /* Too many symlinks followed */
723 retval = SYMLINK_LOOP;
724 goto done;
725 }
726
727 /*
728 * At this point, we have followed at a least
729 * one symlink, so on error we need to report this.
730 */
731 retval = DANGLING_SYMLINK;
732
b4f5aca4 733 contents = read_object_file(&current_tree_oid, &type,
734 &link_len);
275721c2
DT
735
736 if (!contents)
737 goto done;
738
739 if (contents[0] == '/') {
740 strbuf_addstr(result_path, contents);
741 free(contents);
742 *mode = 0;
743 retval = FOUND;
744 goto done;
745 }
746
747 if (remainder)
748 len = first_slash - namebuf.buf;
749 else
750 len = namebuf.len;
751
752 contents_start = contents;
753
754 parent = &parents[parents_nr - 1];
755 init_tree_desc(&t, parent->tree, parent->size);
756 strbuf_splice(&namebuf, 0, len,
757 contents_start, link_len);
758 if (remainder)
759 namebuf.buf[link_len] = '/';
760 free(contents);
761 }
762 }
763done:
764 for (i = 0; i < parents_nr; i++)
765 free(parents[i].tree);
766 free(parents);
767
768 strbuf_release(&namebuf);
769 return retval;
770}
771
93d93537
NTND
772static int match_entry(const struct pathspec_item *item,
773 const struct name_entry *entry, int pathlen,
58c4d666 774 const char *match, int matchlen,
c3a47ca9 775 enum interesting *never_interesting)
58c4d666
NTND
776{
777 int m = -1; /* signals that we haven't called strncmp() */
778
93d93537
NTND
779 if (item->magic & PATHSPEC_ICASE)
780 /*
781 * "Never interesting" trick requires exact
782 * matching. We could do something clever with inexact
783 * matching, but it's trickier (and not to forget that
784 * strcasecmp is locale-dependent, at least in
785 * glibc). Just disable it for now. It can't be worse
786 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
787 * pattern.
788 */
789 *never_interesting = entry_not_interesting;
790 else if (*never_interesting != entry_not_interesting) {
58c4d666
NTND
791 /*
792 * We have not seen any match that sorts later
793 * than the current path.
794 */
795
796 /*
797 * Does match sort strictly earlier than path
798 * with their common parts?
799 */
800 m = strncmp(match, entry->path,
801 (matchlen < pathlen) ? matchlen : pathlen);
802 if (m < 0)
803 return 0;
804
805 /*
806 * If we come here even once, that means there is at
807 * least one pathspec that would sort equal to or
808 * later than the path we are currently looking at.
809 * In other words, if we have never reached this point
810 * after iterating all pathspecs, it means all
811 * pathspecs are either outside of base, or inside the
812 * base but sorts strictly earlier than the current
813 * one. In either case, they will never match the
814 * subsequent entries. In such a case, we initialized
815 * the variable to -1 and that is what will be
816 * returned, allowing the caller to terminate early.
817 */
c3a47ca9 818 *never_interesting = entry_not_interesting;
58c4d666
NTND
819 }
820
821 if (pathlen > matchlen)
822 return 0;
823
824 if (matchlen > pathlen) {
825 if (match[pathlen] != '/')
826 return 0;
74b4f7f2 827 if (!S_ISDIR(entry->mode) && !S_ISGITLINK(entry->mode))
58c4d666
NTND
828 return 0;
829 }
830
831 if (m == -1)
832 /*
833 * we cheated and did not do strncmp(), so we do
834 * that here.
835 */
93d93537 836 m = ps_strncmp(item, match, entry->path, pathlen);
58c4d666
NTND
837
838 /*
839 * If common part matched earlier then it is a hit,
840 * because we rejected the case where path is not a
841 * leading directory and is shorter than match.
842 */
843 if (!m)
93d93537
NTND
844 /*
845 * match_entry does not check if the prefix part is
846 * matched case-sensitively. If the entry is a
847 * directory and part of prefix, it'll be rematched
848 * eventually by basecmp with special treatment for
849 * the prefix.
850 */
58c4d666
NTND
851 return 1;
852
853 return 0;
854}
855
93d93537
NTND
856/* :(icase)-aware string compare */
857static int basecmp(const struct pathspec_item *item,
858 const char *base, const char *match, int len)
859{
860 if (item->magic & PATHSPEC_ICASE) {
861 int ret, n = len > item->prefix ? item->prefix : len;
862 ret = strncmp(base, match, n);
863 if (ret)
864 return ret;
865 base += n;
866 match += n;
867 len -= n;
868 }
869 return ps_strncmp(item, base, match, len);
870}
871
872static int match_dir_prefix(const struct pathspec_item *item,
873 const char *base,
58c4d666
NTND
874 const char *match, int matchlen)
875{
93d93537 876 if (basecmp(item, base, match, matchlen))
58c4d666
NTND
877 return 0;
878
879 /*
880 * If the base is a subdirectory of a path which
881 * was specified, all of them are interesting.
882 */
883 if (!matchlen ||
884 base[matchlen] == '/' ||
885 match[matchlen - 1] == '/')
886 return 1;
887
888 /* Just a random prefix match */
889 return 0;
890}
891
c904cd89
NTND
892/*
893 * Perform matching on the leading non-wildcard part of
894 * pathspec. item->nowildcard_len must be greater than zero. Return
895 * non-zero if base is matched.
896 */
897static int match_wildcard_base(const struct pathspec_item *item,
898 const char *base, int baselen,
899 int *matched)
900{
901 const char *match = item->match;
902 /* the wildcard part is not considered in this function */
903 int matchlen = item->nowildcard_len;
904
905 if (baselen) {
906 int dirlen;
907 /*
908 * Return early if base is longer than the
909 * non-wildcard part but it does not match.
910 */
911 if (baselen >= matchlen) {
912 *matched = matchlen;
93d93537 913 return !basecmp(item, base, match, matchlen);
c904cd89
NTND
914 }
915
916 dirlen = matchlen;
917 while (dirlen && match[dirlen - 1] != '/')
918 dirlen--;
919
920 /*
921 * Return early if base is shorter than the
922 * non-wildcard part but it does not match. Note that
923 * base ends with '/' so we are sure it really matches
924 * directory
925 */
93d93537 926 if (basecmp(item, base, match, baselen))
c904cd89
NTND
927 return 0;
928 *matched = baselen;
929 } else
930 *matched = 0;
931 /*
932 * we could have checked entry against the non-wildcard part
933 * that is not in base and does similar never_interesting
934 * optimization as in match_entry. For now just be happy with
935 * base comparison.
936 */
937 return entry_interesting;
938}
939
2c389fc8
NTND
940/*
941 * Is a tree entry interesting given the pathspec we have?
942 *
1376e507
NTND
943 * Pre-condition: either baselen == base_offset (i.e. empty path)
944 * or base[baselen-1] == '/' (i.e. with trailing slash).
2c389fc8 945 */
67022e02
NTND
946static enum interesting do_match(struct index_state *istate,
947 const struct name_entry *entry,
ef79b1f8
NTND
948 struct strbuf *base, int base_offset,
949 const struct pathspec *ps,
950 int exclude)
2c389fc8
NTND
951{
952 int i;
1376e507 953 int pathlen, baselen = base->len - base_offset;
c3a47ca9 954 enum interesting never_interesting = ps->has_wildcard ?
d688cf07 955 entry_not_interesting : all_entries_not_interesting;
2c389fc8 956
5c6933d2
NTND
957 GUARD_PATHSPEC(ps,
958 PATHSPEC_FROMTOP |
959 PATHSPEC_MAXDEPTH |
bd30c2e4 960 PATHSPEC_LITERAL |
93d93537 961 PATHSPEC_GLOB |
ef79b1f8 962 PATHSPEC_ICASE |
5a0b97b3
NTND
963 PATHSPEC_EXCLUDE |
964 PATHSPEC_ATTR);
8f4f8f45 965
bc96cc87 966 if (!ps->nr) {
6330a171
NTND
967 if (!ps->recursive ||
968 !(ps->magic & PATHSPEC_MAXDEPTH) ||
969 ps->max_depth == -1)
d688cf07
NTND
970 return all_entries_interesting;
971 return within_depth(base->buf + base_offset, baselen,
972 !!S_ISDIR(entry->mode),
973 ps->max_depth) ?
974 entry_interesting : entry_not_interesting;
bc96cc87 975 }
2c389fc8 976
0de16337 977 pathlen = tree_entry_len(entry);
2c389fc8 978
1376e507 979 for (i = ps->nr - 1; i >= 0; i--) {
2c389fc8
NTND
980 const struct pathspec_item *item = ps->items+i;
981 const char *match = item->match;
1376e507 982 const char *base_str = base->buf + base_offset;
c904cd89 983 int matchlen = item->len, matched = 0;
2c389fc8 984
ef79b1f8
NTND
985 if ((!exclude && item->magic & PATHSPEC_EXCLUDE) ||
986 ( exclude && !(item->magic & PATHSPEC_EXCLUDE)))
987 continue;
988
2c389fc8
NTND
989 if (baselen >= matchlen) {
990 /* If it doesn't match, move along... */
93d93537 991 if (!match_dir_prefix(item, base_str, match, matchlen))
d38f2809 992 goto match_wildcards;
bc96cc87 993
6330a171
NTND
994 if (!ps->recursive ||
995 !(ps->magic & PATHSPEC_MAXDEPTH) ||
5a0b97b3
NTND
996 ps->max_depth == -1) {
997 if (!item->attr_match_nr)
998 return all_entries_interesting;
999 else
1000 goto interesting;
1001 }
1002
1003 if (within_depth(base_str + matchlen + 1,
1004 baselen - matchlen - 1,
1005 !!S_ISDIR(entry->mode),
1006 ps->max_depth))
1007 goto interesting;
1008 else
1009 return entry_not_interesting;
2c389fc8
NTND
1010 }
1011
1b740923 1012 /* Either there must be no base, or the base must match. */
93d93537
NTND
1013 if (baselen == 0 || !basecmp(item, base_str, match, baselen)) {
1014 if (match_entry(item, entry, pathlen,
58c4d666
NTND
1015 match + baselen, matchlen - baselen,
1016 &never_interesting))
5a0b97b3 1017 goto interesting;
f1a2ddbb 1018
170260ae 1019 if (item->nowildcard_len < item->len) {
bd30c2e4 1020 if (!git_fnmatch(item, match + baselen, entry->path,
8c6abbcd 1021 item->nowildcard_len - baselen))
5a0b97b3 1022 goto interesting;
f1a2ddbb
NTND
1023
1024 /*
1025 * Match all directories. We'll try to
1026 * match files later on.
1027 */
1028 if (ps->recursive && S_ISDIR(entry->mode))
d688cf07 1029 return entry_interesting;
74ed4371
BW
1030
1031 /*
1032 * When matching against submodules with
1033 * wildcard characters, ensure that the entry
1034 * at least matches up to the first wild
1035 * character. More accurate matching can then
1036 * be performed in the submodule itself.
1037 */
eef3df5a
BW
1038 if (ps->recurse_submodules &&
1039 S_ISGITLINK(entry->mode) &&
74ed4371
BW
1040 !ps_strncmp(item, match + baselen,
1041 entry->path,
1042 item->nowildcard_len - baselen))
5a0b97b3 1043 goto interesting;
f1a2ddbb
NTND
1044 }
1045
1046 continue;
2c389fc8 1047 }
d38f2809
NTND
1048
1049match_wildcards:
170260ae 1050 if (item->nowildcard_len == item->len)
d38f2809
NTND
1051 continue;
1052
c904cd89
NTND
1053 if (item->nowildcard_len &&
1054 !match_wildcard_base(item, base_str, baselen, &matched))
e4ddb057 1055 continue;
c904cd89 1056
d38f2809
NTND
1057 /*
1058 * Concatenate base and entry->path into one and do
1059 * fnmatch() on it.
c904cd89
NTND
1060 *
1061 * While we could avoid concatenation in certain cases
1062 * [1], which saves a memcpy and potentially a
1063 * realloc, it turns out not worth it. Measurement on
1064 * linux-2.6 does not show any clear improvements,
1065 * partly because of the nowildcard_len optimization
1066 * in git_fnmatch(). Avoid micro-optimizations here.
1067 *
1068 * [1] if match_wildcard_base() says the base
1069 * directory is already matched, we only need to match
1070 * the rest, which is shorter so _in theory_ faster.
d38f2809
NTND
1071 */
1072
1073 strbuf_add(base, entry->path, pathlen);
1074
bd30c2e4 1075 if (!git_fnmatch(item, match, base->buf + base_offset,
8c6abbcd 1076 item->nowildcard_len)) {
1376e507 1077 strbuf_setlen(base, base_offset + baselen);
5a0b97b3 1078 goto interesting;
d38f2809 1079 }
74ed4371
BW
1080
1081 /*
1082 * When matching against submodules with
1083 * wildcard characters, ensure that the entry
1084 * at least matches up to the first wild
1085 * character. More accurate matching can then
1086 * be performed in the submodule itself.
1087 */
eef3df5a 1088 if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&
74ed4371
BW
1089 !ps_strncmp(item, match, base->buf + base_offset,
1090 item->nowildcard_len)) {
1091 strbuf_setlen(base, base_offset + baselen);
5a0b97b3 1092 goto interesting;
74ed4371
BW
1093 }
1094
1376e507 1095 strbuf_setlen(base, base_offset + baselen);
d38f2809
NTND
1096
1097 /*
1098 * Match all directories. We'll try to match files
1099 * later on.
8c69c1f9
NTND
1100 * max_depth is ignored but we may consider support it
1101 * in future, see
5840eb9d 1102 * https://public-inbox.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/
d38f2809
NTND
1103 */
1104 if (ps->recursive && S_ISDIR(entry->mode))
d688cf07 1105 return entry_interesting;
5a0b97b3
NTND
1106 continue;
1107interesting:
1108 if (item->attr_match_nr) {
1109 int ret;
1110
1111 /*
1112 * Must not return all_entries_not_interesting
1113 * prematurely. We do not know if all entries do not
1114 * match some attributes with current attr API.
1115 */
1116 never_interesting = entry_not_interesting;
1117
1118 /*
1119 * Consider all directories interesting (because some
1120 * of those files inside may match some attributes
1121 * even though the parent dir does not)
1122 *
1123 * FIXME: attributes _can_ match directories and we
1124 * can probably return all_entries_interesting or
1125 * all_entries_not_interesting here if matched.
1126 */
1127 if (S_ISDIR(entry->mode))
1128 return entry_interesting;
1129
1130 strbuf_add(base, entry->path, pathlen);
1131 ret = match_pathspec_attrs(istate, base->buf + base_offset,
1132 base->len - base_offset, item);
1133 strbuf_setlen(base, base_offset + baselen);
1134 if (!ret)
1135 continue;
1136 }
1137 return entry_interesting;
2c389fc8
NTND
1138 }
1139 return never_interesting; /* No matches */
1140}
ef79b1f8
NTND
1141
1142/*
1143 * Is a tree entry interesting given the pathspec we have?
1144 *
1145 * Pre-condition: either baselen == base_offset (i.e. empty path)
1146 * or base[baselen-1] == '/' (i.e. with trailing slash).
1147 */
67022e02
NTND
1148enum interesting tree_entry_interesting(struct index_state *istate,
1149 const struct name_entry *entry,
ef79b1f8
NTND
1150 struct strbuf *base, int base_offset,
1151 const struct pathspec *ps)
1152{
1153 enum interesting positive, negative;
67022e02 1154 positive = do_match(istate, entry, base, base_offset, ps, 0);
ef79b1f8
NTND
1155
1156 /*
1157 * case | entry | positive | negative | result
1158 * -----+-------+----------+----------+-------
1159 * 1 | file | -1 | -1..2 | -1
1160 * 2 | file | 0 | -1..2 | 0
1161 * 3 | file | 1 | -1 | 1
1162 * 4 | file | 1 | 0 | 1
1163 * 5 | file | 1 | 1 | 0
1164 * 6 | file | 1 | 2 | 0
1165 * 7 | file | 2 | -1 | 2
b7845ceb 1166 * 8 | file | 2 | 0 | 1
ef79b1f8
NTND
1167 * 9 | file | 2 | 1 | 0
1168 * 10 | file | 2 | 2 | -1
1169 * -----+-------+----------+----------+-------
1170 * 11 | dir | -1 | -1..2 | -1
1171 * 12 | dir | 0 | -1..2 | 0
1172 * 13 | dir | 1 | -1 | 1
1173 * 14 | dir | 1 | 0 | 1
1174 * 15 | dir | 1 | 1 | 1 (*)
1175 * 16 | dir | 1 | 2 | 0
1176 * 17 | dir | 2 | -1 | 2
b7845ceb 1177 * 18 | dir | 2 | 0 | 1
ef79b1f8
NTND
1178 * 19 | dir | 2 | 1 | 1 (*)
1179 * 20 | dir | 2 | 2 | -1
1180 *
1181 * (*) An exclude pattern interested in a directory does not
1182 * necessarily mean it will exclude all of the directory. In
1183 * wildcard case, it can't decide until looking at individual
1184 * files inside. So don't write such directories off yet.
1185 */
1186
1187 if (!(ps->magic & PATHSPEC_EXCLUDE) ||
1188 positive <= entry_not_interesting) /* #1, #2, #11, #12 */
1189 return positive;
1190
67022e02 1191 negative = do_match(istate, entry, base, base_offset, ps, 1);
ef79b1f8 1192
b7845ceb
NTND
1193 /* #8, #18 */
1194 if (positive == all_entries_interesting &&
1195 negative == entry_not_interesting)
1196 return entry_interesting;
1197
1198 /* #3, #4, #7, #13, #14, #17 */
ef79b1f8
NTND
1199 if (negative <= entry_not_interesting)
1200 return positive;
1201
1202 /* #15, #19 */
1203 if (S_ISDIR(entry->mode) &&
1204 positive >= entry_interesting &&
1205 negative == entry_interesting)
1206 return entry_interesting;
1207
1208 if ((positive == entry_interesting &&
1209 negative >= entry_interesting) || /* #5, #6, #16 */
1210 (positive == all_entries_interesting &&
1211 negative == entry_interesting)) /* #9 */
1212 return entry_not_interesting;
1213
1214 return all_entries_not_interesting; /* #10, #20 */
1215}