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