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