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