1#include"cache.h" 2#include"tree-walk.h" 3#include"unpack-trees.h" 4#include"dir.h" 5#include"tree.h" 6#include"pathspec.h" 7 8static const char*get_mode(const char*str,unsigned int*modep) 9{ 10unsigned char c; 11unsigned int mode =0; 12 13if(*str ==' ') 14return NULL; 15 16while((c = *str++) !=' ') { 17if(c <'0'|| c >'7') 18return NULL; 19 mode = (mode <<3) + (c -'0'); 20} 21*modep = mode; 22return str; 23} 24 25static voiddecode_tree_entry(struct tree_desc *desc,const char*buf,unsigned long size) 26{ 27const char*path; 28unsigned int mode, len; 29 30if(size <24|| buf[size -21]) 31die("corrupt tree file"); 32 33 path =get_mode(buf, &mode); 34if(!path || !*path) 35die("corrupt tree file"); 36 len =strlen(path) +1; 37 38/* Initialize the descriptor entry */ 39 desc->entry.path = path; 40 desc->entry.mode = mode; 41 desc->entry.sha1 = (const unsigned char*)(path + len); 42} 43 44voidinit_tree_desc(struct tree_desc *desc,const void*buffer,unsigned long size) 45{ 46 desc->buffer = buffer; 47 desc->size = size; 48if(size) 49decode_tree_entry(desc, buffer, size); 50} 51 52void*fill_tree_descriptor(struct tree_desc *desc,const unsigned char*sha1) 53{ 54unsigned long size =0; 55void*buf = NULL; 56 57if(sha1) { 58 buf =read_object_with_reference(sha1, tree_type, &size, NULL); 59if(!buf) 60die("unable to read tree%s",sha1_to_hex(sha1)); 61} 62init_tree_desc(desc, buf, size); 63return buf; 64} 65 66static voidentry_clear(struct name_entry *a) 67{ 68memset(a,0,sizeof(*a)); 69} 70 71static voidentry_extract(struct tree_desc *t,struct name_entry *a) 72{ 73*a = t->entry; 74} 75 76voidupdate_tree_entry(struct tree_desc *desc) 77{ 78const void*buf = desc->buffer; 79const unsigned char*end = desc->entry.sha1 +20; 80unsigned long size = desc->size; 81unsigned long len = end - (const unsigned char*)buf; 82 83if(size < len) 84die("corrupt tree file"); 85 buf = end; 86 size -= len; 87 desc->buffer = buf; 88 desc->size = size; 89if(size) 90decode_tree_entry(desc, buf, size); 91} 92 93inttree_entry(struct tree_desc *desc,struct name_entry *entry) 94{ 95if(!desc->size) 96return0; 97 98*entry = desc->entry; 99update_tree_entry(desc); 100return1; 101} 102 103voidsetup_traverse_info(struct traverse_info *info,const char*base) 104{ 105int pathlen =strlen(base); 106static struct traverse_info dummy; 107 108memset(info,0,sizeof(*info)); 109if(pathlen && base[pathlen-1] =='/') 110 pathlen--; 111 info->pathlen = pathlen ? pathlen +1:0; 112 info->name.path = base; 113 info->name.sha1 = (void*)(base + pathlen +1); 114if(pathlen) 115 info->prev = &dummy; 116} 117 118char*make_traverse_path(char*path,const struct traverse_info *info,const struct name_entry *n) 119{ 120int len =tree_entry_len(n); 121int pathlen = info->pathlen; 122 123 path[pathlen + len] =0; 124for(;;) { 125memcpy(path + pathlen, n->path, len); 126if(!pathlen) 127break; 128 path[--pathlen] ='/'; 129 n = &info->name; 130 len =tree_entry_len(n); 131 info = info->prev; 132 pathlen -= len; 133} 134return path; 135} 136 137struct tree_desc_skip { 138struct tree_desc_skip *prev; 139const void*ptr; 140}; 141 142struct tree_desc_x { 143struct tree_desc d; 144struct tree_desc_skip *skip; 145}; 146 147static intname_compare(const char*a,int a_len, 148const char*b,int b_len) 149{ 150int len = (a_len < b_len) ? a_len : b_len; 151int cmp =memcmp(a, b, len); 152if(cmp) 153return cmp; 154return(a_len - b_len); 155} 156 157static intcheck_entry_match(const char*a,int a_len,const char*b,int b_len) 158{ 159/* 160 * The caller wants to pick *a* from a tree or nothing. 161 * We are looking at *b* in a tree. 162 * 163 * (0) If a and b are the same name, we are trivially happy. 164 * 165 * There are three possibilities where *a* could be hiding 166 * behind *b*. 167 * 168 * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no 169 * matter what. 170 * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree; 171 * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree. 172 * 173 * Otherwise we know *a* won't appear in the tree without 174 * scanning further. 175 */ 176 177int cmp =name_compare(a, a_len, b, b_len); 178 179/* Most common case first -- reading sync'd trees */ 180if(!cmp) 181return cmp; 182 183if(0< cmp) { 184/* a comes after b; it does not matter if it is case (3) 185 if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/') 186 return 1; 187 */ 188return1;/* keep looking */ 189} 190 191/* b comes after a; are we looking at case (2)? */ 192if(a_len < b_len && !memcmp(a, b, a_len) && b[a_len] <'/') 193return1;/* keep looking */ 194 195return-1;/* a cannot appear in the tree */ 196} 197 198/* 199 * From the extended tree_desc, extract the first name entry, while 200 * paying attention to the candidate "first" name. Most importantly, 201 * when looking for an entry, if there are entries that sorts earlier 202 * in the tree object representation than that name, skip them and 203 * process the named entry first. We will remember that we haven't 204 * processed the first entry yet, and in the later call skip the 205 * entry we processed early when update_extended_entry() is called. 206 * 207 * E.g. if the underlying tree object has these entries: 208 * 209 * blob "t-1" 210 * blob "t-2" 211 * tree "t" 212 * blob "t=1" 213 * 214 * and the "first" asks for "t", remember that we still need to 215 * process "t-1" and "t-2" but extract "t". After processing the 216 * entry "t" from this call, the caller will let us know by calling 217 * update_extended_entry() that we can remember "t" has been processed 218 * already. 219 */ 220 221static voidextended_entry_extract(struct tree_desc_x *t, 222struct name_entry *a, 223const char*first, 224int first_len) 225{ 226const char*path; 227int len; 228struct tree_desc probe; 229struct tree_desc_skip *skip; 230 231/* 232 * Extract the first entry from the tree_desc, but skip the 233 * ones that we already returned in earlier rounds. 234 */ 235while(1) { 236if(!t->d.size) { 237entry_clear(a); 238break;/* not found */ 239} 240entry_extract(&t->d, a); 241for(skip = t->skip; skip; skip = skip->prev) 242if(a->path == skip->ptr) 243break;/* found */ 244if(!skip) 245break; 246/* We have processed this entry already. */ 247update_tree_entry(&t->d); 248} 249 250if(!first || !a->path) 251return; 252 253/* 254 * The caller wants "first" from this tree, or nothing. 255 */ 256 path = a->path; 257 len =tree_entry_len(a); 258switch(check_entry_match(first, first_len, path, len)) { 259case-1: 260entry_clear(a); 261case0: 262return; 263default: 264break; 265} 266 267/* 268 * We need to look-ahead -- we suspect that a subtree whose 269 * name is "first" may be hiding behind the current entry "path". 270 */ 271 probe = t->d; 272while(probe.size) { 273entry_extract(&probe, a); 274 path = a->path; 275 len =tree_entry_len(a); 276switch(check_entry_match(first, first_len, path, len)) { 277case-1: 278entry_clear(a); 279case0: 280return; 281default: 282update_tree_entry(&probe); 283break; 284} 285/* keep looking */ 286} 287entry_clear(a); 288} 289 290static voidupdate_extended_entry(struct tree_desc_x *t,struct name_entry *a) 291{ 292if(t->d.entry.path == a->path) { 293update_tree_entry(&t->d); 294}else{ 295/* we have returned this entry early */ 296struct tree_desc_skip *skip =xmalloc(sizeof(*skip)); 297 skip->ptr = a->path; 298 skip->prev = t->skip; 299 t->skip = skip; 300} 301} 302 303static voidfree_extended_entry(struct tree_desc_x *t) 304{ 305struct tree_desc_skip *p, *s; 306 307for(s = t->skip; s; s = p) { 308 p = s->prev; 309free(s); 310} 311} 312 313staticinlineintprune_traversal(struct name_entry *e, 314struct traverse_info *info, 315struct strbuf *base, 316int still_interesting) 317{ 318if(!info->pathspec || still_interesting ==2) 319return2; 320if(still_interesting <0) 321return still_interesting; 322returntree_entry_interesting(e, base,0, info->pathspec); 323} 324 325inttraverse_trees(int n,struct tree_desc *t,struct traverse_info *info) 326{ 327int error =0; 328struct name_entry *entry =xmalloc(n*sizeof(*entry)); 329int i; 330struct tree_desc_x *tx =xcalloc(n,sizeof(*tx)); 331struct strbuf base = STRBUF_INIT; 332int interesting =1; 333 334for(i =0; i < n; i++) 335 tx[i].d = t[i]; 336 337if(info->prev) { 338strbuf_grow(&base, info->pathlen); 339make_traverse_path(base.buf, info->prev, &info->name); 340 base.buf[info->pathlen-1] ='/'; 341strbuf_setlen(&base, info->pathlen); 342} 343for(;;) { 344int trees_used; 345unsigned long mask, dirmask; 346const char*first = NULL; 347int first_len =0; 348struct name_entry *e = NULL; 349int len; 350 351for(i =0; i < n; i++) { 352 e = entry + i; 353extended_entry_extract(tx + i, e, NULL,0); 354} 355 356/* 357 * A tree may have "t-2" at the current location even 358 * though it may have "t" that is a subtree behind it, 359 * and another tree may return "t". We want to grab 360 * all "t" from all trees to match in such a case. 361 */ 362for(i =0; i < n; i++) { 363 e = entry + i; 364if(!e->path) 365continue; 366 len =tree_entry_len(e); 367if(!first) { 368 first = e->path; 369 first_len = len; 370continue; 371} 372if(name_compare(e->path, len, first, first_len) <0) { 373 first = e->path; 374 first_len = len; 375} 376} 377 378if(first) { 379for(i =0; i < n; i++) { 380 e = entry + i; 381extended_entry_extract(tx + i, e, first, first_len); 382/* Cull the ones that are not the earliest */ 383if(!e->path) 384continue; 385 len =tree_entry_len(e); 386if(name_compare(e->path, len, first, first_len)) 387entry_clear(e); 388} 389} 390 391/* Now we have in entry[i] the earliest name from the trees */ 392 mask =0; 393 dirmask =0; 394for(i =0; i < n; i++) { 395if(!entry[i].path) 396continue; 397 mask |=1ul<< i; 398if(S_ISDIR(entry[i].mode)) 399 dirmask |=1ul<< i; 400 e = &entry[i]; 401} 402if(!mask) 403break; 404 interesting =prune_traversal(e, info, &base, interesting); 405if(interesting <0) 406break; 407if(interesting) { 408 trees_used = info->fn(n, mask, dirmask, entry, info); 409if(trees_used <0) { 410 error = trees_used; 411if(!info->show_all_errors) 412break; 413} 414 mask &= trees_used; 415} 416for(i =0; i < n; i++) 417if(mask & (1ul<< i)) 418update_extended_entry(tx + i, entry + i); 419} 420free(entry); 421for(i =0; i < n; i++) 422free_extended_entry(tx + i); 423free(tx); 424strbuf_release(&base); 425return error; 426} 427 428static intfind_tree_entry(struct tree_desc *t,const char*name,unsigned char*result,unsigned*mode) 429{ 430int namelen =strlen(name); 431while(t->size) { 432const char*entry; 433const unsigned char*sha1; 434int entrylen, cmp; 435 436 sha1 =tree_entry_extract(t, &entry, mode); 437 entrylen =tree_entry_len(&t->entry); 438update_tree_entry(t); 439if(entrylen > namelen) 440continue; 441 cmp =memcmp(name, entry, entrylen); 442if(cmp >0) 443continue; 444if(cmp <0) 445break; 446if(entrylen == namelen) { 447hashcpy(result, sha1); 448return0; 449} 450if(name[entrylen] !='/') 451continue; 452if(!S_ISDIR(*mode)) 453break; 454if(++entrylen == namelen) { 455hashcpy(result, sha1); 456return0; 457} 458returnget_tree_entry(sha1, name + entrylen, result, mode); 459} 460return-1; 461} 462 463intget_tree_entry(const unsigned char*tree_sha1,const char*name,unsigned char*sha1,unsigned*mode) 464{ 465int retval; 466void*tree; 467unsigned long size; 468unsigned char root[20]; 469 470 tree =read_object_with_reference(tree_sha1, tree_type, &size, root); 471if(!tree) 472return-1; 473 474if(name[0] =='\0') { 475hashcpy(sha1, root); 476free(tree); 477return0; 478} 479 480if(!size) { 481 retval = -1; 482}else{ 483struct tree_desc t; 484init_tree_desc(&t, tree, size); 485 retval =find_tree_entry(&t, name, sha1, mode); 486} 487free(tree); 488return retval; 489} 490 491static intmatch_entry(const struct pathspec_item *item, 492const struct name_entry *entry,int pathlen, 493const char*match,int matchlen, 494enum interesting *never_interesting) 495{ 496int m = -1;/* signals that we haven't called strncmp() */ 497 498if(item->magic & PATHSPEC_ICASE) 499/* 500 * "Never interesting" trick requires exact 501 * matching. We could do something clever with inexact 502 * matching, but it's trickier (and not to forget that 503 * strcasecmp is locale-dependent, at least in 504 * glibc). Just disable it for now. It can't be worse 505 * than the wildcard's codepath of '[Tt][Hi][Is][Ss]' 506 * pattern. 507 */ 508*never_interesting = entry_not_interesting; 509else if(*never_interesting != entry_not_interesting) { 510/* 511 * We have not seen any match that sorts later 512 * than the current path. 513 */ 514 515/* 516 * Does match sort strictly earlier than path 517 * with their common parts? 518 */ 519 m =strncmp(match, entry->path, 520(matchlen < pathlen) ? matchlen : pathlen); 521if(m <0) 522return0; 523 524/* 525 * If we come here even once, that means there is at 526 * least one pathspec that would sort equal to or 527 * later than the path we are currently looking at. 528 * In other words, if we have never reached this point 529 * after iterating all pathspecs, it means all 530 * pathspecs are either outside of base, or inside the 531 * base but sorts strictly earlier than the current 532 * one. In either case, they will never match the 533 * subsequent entries. In such a case, we initialized 534 * the variable to -1 and that is what will be 535 * returned, allowing the caller to terminate early. 536 */ 537*never_interesting = entry_not_interesting; 538} 539 540if(pathlen > matchlen) 541return0; 542 543if(matchlen > pathlen) { 544if(match[pathlen] !='/') 545return0; 546if(!S_ISDIR(entry->mode)) 547return0; 548} 549 550if(m == -1) 551/* 552 * we cheated and did not do strncmp(), so we do 553 * that here. 554 */ 555 m =ps_strncmp(item, match, entry->path, pathlen); 556 557/* 558 * If common part matched earlier then it is a hit, 559 * because we rejected the case where path is not a 560 * leading directory and is shorter than match. 561 */ 562if(!m) 563/* 564 * match_entry does not check if the prefix part is 565 * matched case-sensitively. If the entry is a 566 * directory and part of prefix, it'll be rematched 567 * eventually by basecmp with special treatment for 568 * the prefix. 569 */ 570return1; 571 572return0; 573} 574 575/* :(icase)-aware string compare */ 576static intbasecmp(const struct pathspec_item *item, 577const char*base,const char*match,int len) 578{ 579if(item->magic & PATHSPEC_ICASE) { 580int ret, n = len > item->prefix ? item->prefix : len; 581 ret =strncmp(base, match, n); 582if(ret) 583return ret; 584 base += n; 585 match += n; 586 len -= n; 587} 588returnps_strncmp(item, base, match, len); 589} 590 591static intmatch_dir_prefix(const struct pathspec_item *item, 592const char*base, 593const char*match,int matchlen) 594{ 595if(basecmp(item, base, match, matchlen)) 596return0; 597 598/* 599 * If the base is a subdirectory of a path which 600 * was specified, all of them are interesting. 601 */ 602if(!matchlen || 603 base[matchlen] =='/'|| 604 match[matchlen -1] =='/') 605return1; 606 607/* Just a random prefix match */ 608return0; 609} 610 611/* 612 * Perform matching on the leading non-wildcard part of 613 * pathspec. item->nowildcard_len must be greater than zero. Return 614 * non-zero if base is matched. 615 */ 616static intmatch_wildcard_base(const struct pathspec_item *item, 617const char*base,int baselen, 618int*matched) 619{ 620const char*match = item->match; 621/* the wildcard part is not considered in this function */ 622int matchlen = item->nowildcard_len; 623 624if(baselen) { 625int dirlen; 626/* 627 * Return early if base is longer than the 628 * non-wildcard part but it does not match. 629 */ 630if(baselen >= matchlen) { 631*matched = matchlen; 632return!basecmp(item, base, match, matchlen); 633} 634 635 dirlen = matchlen; 636while(dirlen && match[dirlen -1] !='/') 637 dirlen--; 638 639/* 640 * Return early if base is shorter than the 641 * non-wildcard part but it does not match. Note that 642 * base ends with '/' so we are sure it really matches 643 * directory 644 */ 645if(basecmp(item, base, match, baselen)) 646return0; 647*matched = baselen; 648}else 649*matched =0; 650/* 651 * we could have checked entry against the non-wildcard part 652 * that is not in base and does similar never_interesting 653 * optimization as in match_entry. For now just be happy with 654 * base comparison. 655 */ 656return entry_interesting; 657} 658 659/* 660 * Is a tree entry interesting given the pathspec we have? 661 * 662 * Pre-condition: either baselen == base_offset (i.e. empty path) 663 * or base[baselen-1] == '/' (i.e. with trailing slash). 664 */ 665enum interesting tree_entry_interesting(const struct name_entry *entry, 666struct strbuf *base,int base_offset, 667const struct pathspec *ps) 668{ 669int i; 670int pathlen, baselen = base->len - base_offset; 671enum interesting never_interesting = ps->has_wildcard ? 672 entry_not_interesting : all_entries_not_interesting; 673 674GUARD_PATHSPEC(ps, 675 PATHSPEC_FROMTOP | 676 PATHSPEC_MAXDEPTH | 677 PATHSPEC_LITERAL | 678 PATHSPEC_GLOB | 679 PATHSPEC_ICASE); 680 681if(!ps->nr) { 682if(!ps->recursive || 683!(ps->magic & PATHSPEC_MAXDEPTH) || 684 ps->max_depth == -1) 685return all_entries_interesting; 686returnwithin_depth(base->buf + base_offset, baselen, 687!!S_ISDIR(entry->mode), 688 ps->max_depth) ? 689 entry_interesting : entry_not_interesting; 690} 691 692 pathlen =tree_entry_len(entry); 693 694for(i = ps->nr -1; i >=0; i--) { 695const struct pathspec_item *item = ps->items+i; 696const char*match = item->match; 697const char*base_str = base->buf + base_offset; 698int matchlen = item->len, matched =0; 699 700if(baselen >= matchlen) { 701/* If it doesn't match, move along... */ 702if(!match_dir_prefix(item, base_str, match, matchlen)) 703goto match_wildcards; 704 705if(!ps->recursive || 706!(ps->magic & PATHSPEC_MAXDEPTH) || 707 ps->max_depth == -1) 708return all_entries_interesting; 709 710returnwithin_depth(base_str + matchlen +1, 711 baselen - matchlen -1, 712!!S_ISDIR(entry->mode), 713 ps->max_depth) ? 714 entry_interesting : entry_not_interesting; 715} 716 717/* Either there must be no base, or the base must match. */ 718if(baselen ==0|| !basecmp(item, base_str, match, baselen)) { 719if(match_entry(item, entry, pathlen, 720 match + baselen, matchlen - baselen, 721&never_interesting)) 722return entry_interesting; 723 724if(item->nowildcard_len < item->len) { 725if(!git_fnmatch(item, match + baselen, entry->path, 726 item->nowildcard_len - baselen)) 727return entry_interesting; 728 729/* 730 * Match all directories. We'll try to 731 * match files later on. 732 */ 733if(ps->recursive &&S_ISDIR(entry->mode)) 734return entry_interesting; 735} 736 737continue; 738} 739 740match_wildcards: 741if(item->nowildcard_len == item->len) 742continue; 743 744if(item->nowildcard_len && 745!match_wildcard_base(item, base_str, baselen, &matched)) 746return entry_not_interesting; 747 748/* 749 * Concatenate base and entry->path into one and do 750 * fnmatch() on it. 751 * 752 * While we could avoid concatenation in certain cases 753 * [1], which saves a memcpy and potentially a 754 * realloc, it turns out not worth it. Measurement on 755 * linux-2.6 does not show any clear improvements, 756 * partly because of the nowildcard_len optimization 757 * in git_fnmatch(). Avoid micro-optimizations here. 758 * 759 * [1] if match_wildcard_base() says the base 760 * directory is already matched, we only need to match 761 * the rest, which is shorter so _in theory_ faster. 762 */ 763 764strbuf_add(base, entry->path, pathlen); 765 766if(!git_fnmatch(item, match, base->buf + base_offset, 767 item->nowildcard_len)) { 768strbuf_setlen(base, base_offset + baselen); 769return entry_interesting; 770} 771strbuf_setlen(base, base_offset + baselen); 772 773/* 774 * Match all directories. We'll try to match files 775 * later on. 776 * max_depth is ignored but we may consider support it 777 * in future, see 778 * http://thread.gmane.org/gmane.comp.version-control.git/163757/focus=163840 779 */ 780if(ps->recursive &&S_ISDIR(entry->mode)) 781return entry_interesting; 782} 783return never_interesting;/* No matches */ 784}