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 9static 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 26static 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 len = strlen(path) + 1; 47 48 /* Initialize the descriptor entry */ 49 desc->entry.path = path; 50 desc->entry.mode = canon_mode(mode); 51 desc->entry.pathlen = len - 1; 52 hashcpy(desc->entry.oid.hash, (const unsigned char *)path + len); 53 54 return 0; 55} 56 57static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer, unsigned long size, struct strbuf *err) 58{ 59 desc->buffer = buffer; 60 desc->size = size; 61 if (size) 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; 82} 83 84void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid) 85{ 86 unsigned long size = 0; 87 void *buf = NULL; 88 89 if (oid) { 90 buf = read_object_with_reference(oid, tree_type, &size, NULL); 91 if (!buf) 92 die("unable to read tree %s", oid_to_hex(oid)); 93 } 94 init_tree_desc(desc, buf, size); 95 return buf; 96} 97 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{ 105 *a = t->entry; 106} 107 108static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err) 109{ 110 const void *buf = desc->buffer; 111 const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + the_hash_algo->rawsz; 112 unsigned long size = desc->size; 113 unsigned long len = end - (const unsigned char *)buf; 114 115 if (size < len) 116 die(_("too-short tree file")); 117 buf = end; 118 size -= len; 119 desc->buffer = buf; 120 desc->size = size; 121 if (size) 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; 146} 147 148int tree_entry(struct tree_desc *desc, struct name_entry *entry) 149{ 150 if (!desc->size) 151 return 0; 152 153 *entry = desc->entry; 154 update_tree_entry(desc); 155 return 1; 156} 157 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 169void setup_traverse_info(struct traverse_info *info, const char *base) 170{ 171 size_t pathlen = strlen(base); 172 static struct traverse_info dummy; 173 174 memset(info, 0, sizeof(*info)); 175 if (pathlen && base[pathlen-1] == '/') 176 pathlen--; 177 info->pathlen = pathlen ? pathlen + 1 : 0; 178 info->name = base; 179 info->namelen = pathlen; 180 if (pathlen) 181 info->prev = &dummy; 182} 183 184char *make_traverse_path(char *path, const struct traverse_info *info, 185 const char *name, size_t namelen) 186{ 187 size_t pathlen = info->pathlen; 188 189 path[pathlen + namelen] = 0; 190 for (;;) { 191 memcpy(path + pathlen, name, namelen); 192 if (!pathlen) 193 break; 194 path[--pathlen] = '/'; 195 name = info->name; 196 namelen = info->namelen; 197 info = info->prev; 198 pathlen -= namelen; 199 } 200 return path; 201} 202 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 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 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; 324 len = tree_entry_len(a); 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; 342 len = tree_entry_len(a); 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 380static inline int prune_traversal(struct index_state *istate, 381 struct name_entry *e, 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; 390 return tree_entry_interesting(istate, e, base, 391 0, info->pathspec); 392} 393 394int traverse_trees(struct index_state *istate, 395 int n, struct tree_desc *t, 396 struct traverse_info *info) 397{ 398 int error = 0; 399 struct name_entry *entry = xmalloc(n*sizeof(*entry)); 400 int i; 401 struct tree_desc_x *tx = xcalloc(n, sizeof(*tx)); 402 struct strbuf base = STRBUF_INIT; 403 int interesting = 1; 404 char *traverse_path; 405 406 for (i = 0; i < n; i++) 407 tx[i].d = t[i]; 408 409 if (info->prev) { 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); 414 } else { 415 traverse_path = xstrndup(info->name, 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 505struct dir_state { 506 void *tree; 507 unsigned long size; 508 struct object_id oid; 509}; 510 511static int find_tree_entry(struct tree_desc *t, const char *name, struct object_id *result, unsigned short *mode) 512{ 513 int namelen = strlen(name); 514 while (t->size) { 515 const char *entry; 516 struct object_id oid; 517 int entrylen, cmp; 518 519 oidcpy(&oid, tree_entry_extract(t, &entry, mode)); 520 entrylen = tree_entry_len(&t->entry); 521 update_tree_entry(t); 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) { 530 oidcpy(result, &oid); 531 return 0; 532 } 533 if (name[entrylen] != '/') 534 continue; 535 if (!S_ISDIR(*mode)) 536 break; 537 if (++entrylen == namelen) { 538 oidcpy(result, &oid); 539 return 0; 540 } 541 return get_tree_entry(&oid, name + entrylen, result, mode); 542 } 543 return -1; 544} 545 546int get_tree_entry(const struct object_id *tree_oid, const char *name, struct object_id *oid, unsigned short *mode) 547{ 548 int retval; 549 void *tree; 550 unsigned long size; 551 struct object_id root; 552 553 tree = read_object_with_reference(tree_oid, tree_type, &size, &root); 554 if (!tree) 555 return -1; 556 557 if (name[0] == '\0') { 558 oidcpy(oid, &root); 559 free(tree); 560 return 0; 561 } 562 563 if (!size) { 564 retval = -1; 565 } else { 566 struct tree_desc t; 567 init_tree_desc(&t, tree, size); 568 retval = find_tree_entry(&t, name, oid, mode); 569 } 570 free(tree); 571 return retval; 572} 573 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 * 593 * See the code for enum get_oid_result for a description of 594 * the return values. 595 */ 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) 597{ 598 int retval = MISSING_OBJECT; 599 struct dir_state *parents = NULL; 600 size_t parents_alloc = 0; 601 size_t i, parents_nr = 0; 602 struct object_id current_tree_oid; 603 struct strbuf namebuf = STRBUF_INIT; 604 struct tree_desc t; 605 int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS; 606 607 init_tree_desc(&t, NULL, 0UL); 608 strbuf_addstr(&namebuf, name); 609 oidcpy(¤t_tree_oid, tree_oid); 610 611 while (1) { 612 int find_result; 613 char *first_slash; 614 char *remainder = NULL; 615 616 if (!t.buffer) { 617 void *tree; 618 struct object_id root; 619 unsigned long size; 620 tree = read_object_with_reference(¤t_tree_oid, 621 tree_type, &size, 622 &root); 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; 629 oidcpy(&parents[parents_nr].oid, &root); 630 parents_nr++; 631 632 if (namebuf.buf[0] == '\0') { 633 oidcpy(result, &root); 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') { 683 oidcpy(result, &parents[parents_nr - 1].oid); 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, 690 ¤t_tree_oid, mode); 691 if (find_result) { 692 goto done; 693 } 694 695 if (S_ISDIR(*mode)) { 696 if (!remainder) { 697 oidcpy(result, ¤t_tree_oid); 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) { 707 oidcpy(result, ¤t_tree_oid); 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 733 contents = read_object_file(¤t_tree_oid, &type, 734 &link_len); 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 772static int match_entry(const struct pathspec_item *item, 773 const struct name_entry *entry, int pathlen, 774 const char *match, int matchlen, 775 enum interesting *never_interesting) 776{ 777 int m = -1; /* signals that we haven't called strncmp() */ 778 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) { 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 */ 818 *never_interesting = entry_not_interesting; 819 } 820 821 if (pathlen > matchlen) 822 return 0; 823 824 if (matchlen > pathlen) { 825 if (match[pathlen] != '/') 826 return 0; 827 if (!S_ISDIR(entry->mode) && !S_ISGITLINK(entry->mode)) 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 */ 836 m = ps_strncmp(item, match, entry->path, pathlen); 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) 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 */ 851 return 1; 852 853 return 0; 854} 855 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, 874 const char *match, int matchlen) 875{ 876 if (basecmp(item, base, match, matchlen)) 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 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; 913 return !basecmp(item, base, match, matchlen); 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 */ 926 if (basecmp(item, base, match, baselen)) 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 940/* 941 * Is a tree entry interesting given the pathspec we have? 942 * 943 * Pre-condition: either baselen == base_offset (i.e. empty path) 944 * or base[baselen-1] == '/' (i.e. with trailing slash). 945 */ 946static enum interesting do_match(struct index_state *istate, 947 const struct name_entry *entry, 948 struct strbuf *base, int base_offset, 949 const struct pathspec *ps, 950 int exclude) 951{ 952 int i; 953 int pathlen, baselen = base->len - base_offset; 954 enum interesting never_interesting = ps->has_wildcard ? 955 entry_not_interesting : all_entries_not_interesting; 956 957 GUARD_PATHSPEC(ps, 958 PATHSPEC_FROMTOP | 959 PATHSPEC_MAXDEPTH | 960 PATHSPEC_LITERAL | 961 PATHSPEC_GLOB | 962 PATHSPEC_ICASE | 963 PATHSPEC_EXCLUDE | 964 PATHSPEC_ATTR); 965 966 if (!ps->nr) { 967 if (!ps->recursive || 968 !(ps->magic & PATHSPEC_MAXDEPTH) || 969 ps->max_depth == -1) 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; 975 } 976 977 pathlen = tree_entry_len(entry); 978 979 for (i = ps->nr - 1; i >= 0; i--) { 980 const struct pathspec_item *item = ps->items+i; 981 const char *match = item->match; 982 const char *base_str = base->buf + base_offset; 983 int matchlen = item->len, matched = 0; 984 985 if ((!exclude && item->magic & PATHSPEC_EXCLUDE) || 986 ( exclude && !(item->magic & PATHSPEC_EXCLUDE))) 987 continue; 988 989 if (baselen >= matchlen) { 990 /* If it doesn't match, move along... */ 991 if (!match_dir_prefix(item, base_str, match, matchlen)) 992 goto match_wildcards; 993 994 if (!ps->recursive || 995 !(ps->magic & PATHSPEC_MAXDEPTH) || 996 ps->max_depth == -1) { 997 if (!item->attr_match_nr) 998 return all_entries_interesting; 999 else1000 goto interesting;1001 }10021003 if (within_depth(base_str + matchlen + 1,1004 baselen - matchlen - 1,1005 !!S_ISDIR(entry->mode),1006 ps->max_depth))1007 goto interesting;1008 else1009 return entry_not_interesting;1010 }10111012 /* Either there must be no base, or the base must match. */1013 if (baselen == 0 || !basecmp(item, base_str, match, baselen)) {1014 if (match_entry(item, entry, pathlen,1015 match + baselen, matchlen - baselen,1016 &never_interesting))1017 goto interesting;10181019 if (item->nowildcard_len < item->len) {1020 if (!git_fnmatch(item, match + baselen, entry->path,1021 item->nowildcard_len - baselen))1022 goto interesting;10231024 /*1025 * Match all directories. We'll try to1026 * match files later on.1027 */1028 if (ps->recursive && S_ISDIR(entry->mode))1029 return entry_interesting;10301031 /*1032 * When matching against submodules with1033 * wildcard characters, ensure that the entry1034 * at least matches up to the first wild1035 * character. More accurate matching can then1036 * be performed in the submodule itself.1037 */1038 if (ps->recurse_submodules &&1039 S_ISGITLINK(entry->mode) &&1040 !ps_strncmp(item, match + baselen,1041 entry->path,1042 item->nowildcard_len - baselen))1043 goto interesting;1044 }10451046 continue;1047 }10481049match_wildcards:1050 if (item->nowildcard_len == item->len)1051 continue;10521053 if (item->nowildcard_len &&1054 !match_wildcard_base(item, base_str, baselen, &matched))1055 continue;10561057 /*1058 * Concatenate base and entry->path into one and do1059 * fnmatch() on it.1060 *1061 * While we could avoid concatenation in certain cases1062 * [1], which saves a memcpy and potentially a1063 * realloc, it turns out not worth it. Measurement on1064 * linux-2.6 does not show any clear improvements,1065 * partly because of the nowildcard_len optimization1066 * in git_fnmatch(). Avoid micro-optimizations here.1067 *1068 * [1] if match_wildcard_base() says the base1069 * directory is already matched, we only need to match1070 * the rest, which is shorter so _in theory_ faster.1071 */10721073 strbuf_add(base, entry->path, pathlen);10741075 if (!git_fnmatch(item, match, base->buf + base_offset,1076 item->nowildcard_len)) {1077 strbuf_setlen(base, base_offset + baselen);1078 goto interesting;1079 }10801081 /*1082 * When matching against submodules with1083 * wildcard characters, ensure that the entry1084 * at least matches up to the first wild1085 * character. More accurate matching can then1086 * be performed in the submodule itself.1087 */1088 if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&1089 !ps_strncmp(item, match, base->buf + base_offset,1090 item->nowildcard_len)) {1091 strbuf_setlen(base, base_offset + baselen);1092 goto interesting;1093 }10941095 strbuf_setlen(base, base_offset + baselen);10961097 /*1098 * Match all directories. We'll try to match files1099 * later on.1100 * max_depth is ignored but we may consider support it1101 * in future, see1102 * https://public-inbox.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/1103 */1104 if (ps->recursive && S_ISDIR(entry->mode))1105 return entry_interesting;1106 continue;1107interesting:1108 if (item->attr_match_nr) {1109 int ret;11101111 /*1112 * Must not return all_entries_not_interesting1113 * prematurely. We do not know if all entries do not1114 * match some attributes with current attr API.1115 */1116 never_interesting = entry_not_interesting;11171118 /*1119 * Consider all directories interesting (because some1120 * of those files inside may match some attributes1121 * even though the parent dir does not)1122 *1123 * FIXME: attributes _can_ match directories and we1124 * can probably return all_entries_interesting or1125 * all_entries_not_interesting here if matched.1126 */1127 if (S_ISDIR(entry->mode))1128 return entry_interesting;11291130 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;1138 }1139 return never_interesting; /* No matches */1140}11411142/*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 */1148enum interesting tree_entry_interesting(struct index_state *istate,1149 const struct name_entry *entry,1150 struct strbuf *base, int base_offset,1151 const struct pathspec *ps)1152{1153 enum interesting positive, negative;1154 positive = do_match(istate, entry, base, base_offset, ps, 0);11551156 /*1157 * case | entry | positive | negative | result1158 * -----+-------+----------+----------+-------1159 * 1 | file | -1 | -1..2 | -11160 * 2 | file | 0 | -1..2 | 01161 * 3 | file | 1 | -1 | 11162 * 4 | file | 1 | 0 | 11163 * 5 | file | 1 | 1 | 01164 * 6 | file | 1 | 2 | 01165 * 7 | file | 2 | -1 | 21166 * 8 | file | 2 | 0 | 11167 * 9 | file | 2 | 1 | 01168 * 10 | file | 2 | 2 | -11169 * -----+-------+----------+----------+-------1170 * 11 | dir | -1 | -1..2 | -11171 * 12 | dir | 0 | -1..2 | 01172 * 13 | dir | 1 | -1 | 11173 * 14 | dir | 1 | 0 | 11174 * 15 | dir | 1 | 1 | 1 (*)1175 * 16 | dir | 1 | 2 | 01176 * 17 | dir | 2 | -1 | 21177 * 18 | dir | 2 | 0 | 11178 * 19 | dir | 2 | 1 | 1 (*)1179 * 20 | dir | 2 | 2 | -11180 *1181 * (*) An exclude pattern interested in a directory does not1182 * necessarily mean it will exclude all of the directory. In1183 * wildcard case, it can't decide until looking at individual1184 * files inside. So don't write such directories off yet.1185 */11861187 if (!(ps->magic & PATHSPEC_EXCLUDE) ||1188 positive <= entry_not_interesting) /* #1, #2, #11, #12 */1189 return positive;11901191 negative = do_match(istate, entry, base, base_offset, ps, 1);11921193 /* #8, #18 */1194 if (positive == all_entries_interesting &&1195 negative == entry_not_interesting)1196 return entry_interesting;11971198 /* #3, #4, #7, #13, #14, #17 */1199 if (negative <= entry_not_interesting)1200 return positive;12011202 /* #15, #19 */1203 if (S_ISDIR(entry->mode) &&1204 positive >= entry_interesting &&1205 negative == entry_interesting)1206 return entry_interesting;12071208 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;12131214 return all_entries_not_interesting; /* #10, #20 */1215}