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