1/* 2 * This handles recursive filename detection with exclude 3 * files, index knowledge etc.. 4 * 5 * See Documentation/technical/api-directory-listing.txt 6 * 7 * Copyright (C) Linus Torvalds, 2005-2006 8 * Junio Hamano, 2005-2006 9 */ 10#include"cache.h" 11#include"dir.h" 12#include"refs.h" 13#include"wildmatch.h" 14 15struct path_simplify { 16int len; 17const char*path; 18}; 19 20static intread_directory_recursive(struct dir_struct *dir,const char*path,int len, 21int check_only,const struct path_simplify *simplify); 22static intget_dtype(struct dirent *de,const char*path,int len); 23 24/* helper string functions with support for the ignore_case flag */ 25intstrcmp_icase(const char*a,const char*b) 26{ 27return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 28} 29 30intstrncmp_icase(const char*a,const char*b,size_t count) 31{ 32return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 33} 34 35intfnmatch_icase(const char*pattern,const char*string,int flags) 36{ 37returnfnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD :0)); 38} 39 40inlineintgit_fnmatch(const char*pattern,const char*string, 41int flags,int prefix) 42{ 43int fnm_flags =0; 44if(flags & GFNM_PATHNAME) 45 fnm_flags |= FNM_PATHNAME; 46if(prefix >0) { 47if(strncmp(pattern, string, prefix)) 48return FNM_NOMATCH; 49 pattern += prefix; 50 string += prefix; 51} 52if(flags & GFNM_ONESTAR) { 53int pattern_len =strlen(++pattern); 54int string_len =strlen(string); 55return string_len < pattern_len || 56strcmp(pattern, 57 string + string_len - pattern_len); 58} 59returnfnmatch(pattern, string, fnm_flags); 60} 61 62static intfnmatch_icase_mem(const char*pattern,int patternlen, 63const char*string,int stringlen, 64int flags) 65{ 66int match_status; 67struct strbuf pat_buf = STRBUF_INIT; 68struct strbuf str_buf = STRBUF_INIT; 69const char*use_pat = pattern; 70const char*use_str = string; 71 72if(pattern[patternlen]) { 73strbuf_add(&pat_buf, pattern, patternlen); 74 use_pat = pat_buf.buf; 75} 76if(string[stringlen]) { 77strbuf_add(&str_buf, string, stringlen); 78 use_str = str_buf.buf; 79} 80 81if(ignore_case) 82 flags |= WM_CASEFOLD; 83 match_status =wildmatch(use_pat, use_str, flags, NULL); 84 85strbuf_release(&pat_buf); 86strbuf_release(&str_buf); 87 88return match_status; 89} 90 91static size_tcommon_prefix_len(const char**pathspec) 92{ 93const char*n, *first; 94size_t max =0; 95int literal =limit_pathspec_to_literal(); 96 97if(!pathspec) 98return max; 99 100 first = *pathspec; 101while((n = *pathspec++)) { 102size_t i, len =0; 103for(i =0; first == n || i < max; i++) { 104char c = n[i]; 105if(!c || c != first[i] || (!literal &&is_glob_special(c))) 106break; 107if(c =='/') 108 len = i +1; 109} 110if(first == n || len < max) { 111 max = len; 112if(!max) 113break; 114} 115} 116return max; 117} 118 119/* 120 * Returns a copy of the longest leading path common among all 121 * pathspecs. 122 */ 123char*common_prefix(const char**pathspec) 124{ 125unsigned long len =common_prefix_len(pathspec); 126 127return len ?xmemdupz(*pathspec, len) : NULL; 128} 129 130intfill_directory(struct dir_struct *dir,const char**pathspec) 131{ 132size_t len; 133 134/* 135 * Calculate common prefix for the pathspec, and 136 * use that to optimize the directory walk 137 */ 138 len =common_prefix_len(pathspec); 139 140/* Read the directory and prune it */ 141read_directory(dir, pathspec ? *pathspec :"", len, pathspec); 142return len; 143} 144 145intwithin_depth(const char*name,int namelen, 146int depth,int max_depth) 147{ 148const char*cp = name, *cpe = name + namelen; 149 150while(cp < cpe) { 151if(*cp++ !='/') 152continue; 153 depth++; 154if(depth > max_depth) 155return0; 156} 157return1; 158} 159 160/* 161 * Does 'match' match the given name? 162 * A match is found if 163 * 164 * (1) the 'match' string is leading directory of 'name', or 165 * (2) the 'match' string is a wildcard and matches 'name', or 166 * (3) the 'match' string is exactly the same as 'name'. 167 * 168 * and the return value tells which case it was. 169 * 170 * It returns 0 when there is no match. 171 */ 172static intmatch_one(const char*match,const char*name,int namelen) 173{ 174int matchlen; 175int literal =limit_pathspec_to_literal(); 176 177/* If the match was just the prefix, we matched */ 178if(!*match) 179return MATCHED_RECURSIVELY; 180 181if(ignore_case) { 182for(;;) { 183unsigned char c1 =tolower(*match); 184unsigned char c2 =tolower(*name); 185if(c1 =='\0'|| (!literal &&is_glob_special(c1))) 186break; 187if(c1 != c2) 188return0; 189 match++; 190 name++; 191 namelen--; 192} 193}else{ 194for(;;) { 195unsigned char c1 = *match; 196unsigned char c2 = *name; 197if(c1 =='\0'|| (!literal &&is_glob_special(c1))) 198break; 199if(c1 != c2) 200return0; 201 match++; 202 name++; 203 namelen--; 204} 205} 206 207/* 208 * If we don't match the matchstring exactly, 209 * we need to match by fnmatch 210 */ 211 matchlen =strlen(match); 212if(strncmp_icase(match, name, matchlen)) { 213if(literal) 214return0; 215return!fnmatch_icase(match, name,0) ? MATCHED_FNMATCH :0; 216} 217 218if(namelen == matchlen) 219return MATCHED_EXACTLY; 220if(match[matchlen-1] =='/'|| name[matchlen] =='/') 221return MATCHED_RECURSIVELY; 222return0; 223} 224 225/* 226 * Given a name and a list of pathspecs, returns the nature of the 227 * closest (i.e. most specific) match of the name to any of the 228 * pathspecs. 229 * 230 * The caller typically calls this multiple times with the same 231 * pathspec and seen[] array but with different name/namelen 232 * (e.g. entries from the index) and is interested in seeing if and 233 * how each pathspec matches all the names it calls this function 234 * with. A mark is left in the seen[] array for each pathspec element 235 * indicating the closest type of match that element achieved, so if 236 * seen[n] remains zero after multiple invocations, that means the nth 237 * pathspec did not match any names, which could indicate that the 238 * user mistyped the nth pathspec. 239 */ 240intmatch_pathspec(const char**pathspec,const char*name,int namelen, 241int prefix,char*seen) 242{ 243int i, retval =0; 244 245if(!pathspec) 246return1; 247 248 name += prefix; 249 namelen -= prefix; 250 251for(i =0; pathspec[i] != NULL; i++) { 252int how; 253const char*match = pathspec[i] + prefix; 254if(seen && seen[i] == MATCHED_EXACTLY) 255continue; 256 how =match_one(match, name, namelen); 257if(how) { 258if(retval < how) 259 retval = how; 260if(seen && seen[i] < how) 261 seen[i] = how; 262} 263} 264return retval; 265} 266 267/* 268 * Does 'match' match the given name? 269 * A match is found if 270 * 271 * (1) the 'match' string is leading directory of 'name', or 272 * (2) the 'match' string is a wildcard and matches 'name', or 273 * (3) the 'match' string is exactly the same as 'name'. 274 * 275 * and the return value tells which case it was. 276 * 277 * It returns 0 when there is no match. 278 */ 279static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 280const char*name,int namelen) 281{ 282/* name/namelen has prefix cut off by caller */ 283const char*match = item->match + prefix; 284int matchlen = item->len - prefix; 285 286/* If the match was just the prefix, we matched */ 287if(!*match) 288return MATCHED_RECURSIVELY; 289 290if(matchlen <= namelen && !strncmp(match, name, matchlen)) { 291if(matchlen == namelen) 292return MATCHED_EXACTLY; 293 294if(match[matchlen-1] =='/'|| name[matchlen] =='/') 295return MATCHED_RECURSIVELY; 296} 297 298if(item->nowildcard_len < item->len && 299!git_fnmatch(match, name, 300 item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR :0, 301 item->nowildcard_len - prefix)) 302return MATCHED_FNMATCH; 303 304return0; 305} 306 307/* 308 * Given a name and a list of pathspecs, returns the nature of the 309 * closest (i.e. most specific) match of the name to any of the 310 * pathspecs. 311 * 312 * The caller typically calls this multiple times with the same 313 * pathspec and seen[] array but with different name/namelen 314 * (e.g. entries from the index) and is interested in seeing if and 315 * how each pathspec matches all the names it calls this function 316 * with. A mark is left in the seen[] array for each pathspec element 317 * indicating the closest type of match that element achieved, so if 318 * seen[n] remains zero after multiple invocations, that means the nth 319 * pathspec did not match any names, which could indicate that the 320 * user mistyped the nth pathspec. 321 */ 322intmatch_pathspec_depth(const struct pathspec *ps, 323const char*name,int namelen, 324int prefix,char*seen) 325{ 326int i, retval =0; 327 328if(!ps->nr) { 329if(!ps->recursive || ps->max_depth == -1) 330return MATCHED_RECURSIVELY; 331 332if(within_depth(name, namelen,0, ps->max_depth)) 333return MATCHED_EXACTLY; 334else 335return0; 336} 337 338 name += prefix; 339 namelen -= prefix; 340 341for(i = ps->nr -1; i >=0; i--) { 342int how; 343if(seen && seen[i] == MATCHED_EXACTLY) 344continue; 345 how =match_pathspec_item(ps->items+i, prefix, name, namelen); 346if(ps->recursive && ps->max_depth != -1&& 347 how && how != MATCHED_FNMATCH) { 348int len = ps->items[i].len; 349if(name[len] =='/') 350 len++; 351if(within_depth(name+len, namelen-len,0, ps->max_depth)) 352 how = MATCHED_EXACTLY; 353else 354 how =0; 355} 356if(how) { 357if(retval < how) 358 retval = how; 359if(seen && seen[i] < how) 360 seen[i] = how; 361} 362} 363return retval; 364} 365 366/* 367 * Return the length of the "simple" part of a path match limiter. 368 */ 369static intsimple_length(const char*match) 370{ 371int len = -1; 372 373for(;;) { 374unsigned char c = *match++; 375 len++; 376if(c =='\0'||is_glob_special(c)) 377return len; 378} 379} 380 381static intno_wildcard(const char*string) 382{ 383return string[simple_length(string)] =='\0'; 384} 385 386voidparse_exclude_pattern(const char**pattern, 387int*patternlen, 388int*flags, 389int*nowildcardlen) 390{ 391const char*p = *pattern; 392size_t i, len; 393 394*flags =0; 395if(*p =='!') { 396*flags |= EXC_FLAG_NEGATIVE; 397 p++; 398} 399 len =strlen(p); 400if(len && p[len -1] =='/') { 401 len--; 402*flags |= EXC_FLAG_MUSTBEDIR; 403} 404for(i =0; i < len; i++) { 405if(p[i] =='/') 406break; 407} 408if(i == len) 409*flags |= EXC_FLAG_NODIR; 410*nowildcardlen =simple_length(p); 411/* 412 * we should have excluded the trailing slash from 'p' too, 413 * but that's one more allocation. Instead just make sure 414 * nowildcardlen does not exceed real patternlen 415 */ 416if(*nowildcardlen > len) 417*nowildcardlen = len; 418if(*p =='*'&&no_wildcard(p +1)) 419*flags |= EXC_FLAG_ENDSWITH; 420*pattern = p; 421*patternlen = len; 422} 423 424voidadd_exclude(const char*string,const char*base, 425int baselen,struct exclude_list *el,int srcpos) 426{ 427struct exclude *x; 428int patternlen; 429int flags; 430int nowildcardlen; 431 432parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 433if(flags & EXC_FLAG_MUSTBEDIR) { 434char*s; 435 x =xmalloc(sizeof(*x) + patternlen +1); 436 s = (char*)(x+1); 437memcpy(s, string, patternlen); 438 s[patternlen] ='\0'; 439 x->pattern = s; 440}else{ 441 x =xmalloc(sizeof(*x)); 442 x->pattern = string; 443} 444 x->patternlen = patternlen; 445 x->nowildcardlen = nowildcardlen; 446 x->base = base; 447 x->baselen = baselen; 448 x->flags = flags; 449 x->srcpos = srcpos; 450ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 451 el->excludes[el->nr++] = x; 452 x->el = el; 453} 454 455static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 456{ 457int pos, len; 458unsigned long sz; 459enum object_type type; 460void*data; 461struct index_state *istate = &the_index; 462 463 len =strlen(path); 464 pos =index_name_pos(istate, path, len); 465if(pos <0) 466return NULL; 467if(!ce_skip_worktree(istate->cache[pos])) 468return NULL; 469 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz); 470if(!data || type != OBJ_BLOB) { 471free(data); 472return NULL; 473} 474*size =xsize_t(sz); 475return data; 476} 477 478/* 479 * Frees memory within el which was allocated for exclude patterns and 480 * the file buffer. Does not free el itself. 481 */ 482voidclear_exclude_list(struct exclude_list *el) 483{ 484int i; 485 486for(i =0; i < el->nr; i++) 487free(el->excludes[i]); 488free(el->excludes); 489free(el->filebuf); 490 491 el->nr =0; 492 el->excludes = NULL; 493 el->filebuf = NULL; 494} 495 496intadd_excludes_from_file_to_list(const char*fname, 497const char*base, 498int baselen, 499struct exclude_list *el, 500int check_index) 501{ 502struct stat st; 503int fd, i, lineno =1; 504size_t size =0; 505char*buf, *entry; 506 507 fd =open(fname, O_RDONLY); 508if(fd <0||fstat(fd, &st) <0) { 509if(errno != ENOENT) 510warn_on_inaccessible(fname); 511if(0<= fd) 512close(fd); 513if(!check_index || 514(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 515return-1; 516if(size ==0) { 517free(buf); 518return0; 519} 520if(buf[size-1] !='\n') { 521 buf =xrealloc(buf, size+1); 522 buf[size++] ='\n'; 523} 524} 525else{ 526 size =xsize_t(st.st_size); 527if(size ==0) { 528close(fd); 529return0; 530} 531 buf =xmalloc(size+1); 532if(read_in_full(fd, buf, size) != size) { 533free(buf); 534close(fd); 535return-1; 536} 537 buf[size++] ='\n'; 538close(fd); 539} 540 541 el->filebuf = buf; 542 entry = buf; 543for(i =0; i < size; i++) { 544if(buf[i] =='\n') { 545if(entry != buf + i && entry[0] !='#') { 546 buf[i - (i && buf[i-1] =='\r')] =0; 547add_exclude(entry, base, baselen, el, lineno); 548} 549 lineno++; 550 entry = buf + i +1; 551} 552} 553return0; 554} 555 556struct exclude_list *add_exclude_list(struct dir_struct *dir, 557int group_type,const char*src) 558{ 559struct exclude_list *el; 560struct exclude_list_group *group; 561 562 group = &dir->exclude_list_group[group_type]; 563ALLOC_GROW(group->el, group->nr +1, group->alloc); 564 el = &group->el[group->nr++]; 565memset(el,0,sizeof(*el)); 566 el->src = src; 567return el; 568} 569 570/* 571 * Used to set up core.excludesfile and .git/info/exclude lists. 572 */ 573voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 574{ 575struct exclude_list *el; 576 el =add_exclude_list(dir, EXC_FILE, fname); 577if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 578die("cannot use%sas an exclude file", fname); 579} 580 581intmatch_basename(const char*basename,int basenamelen, 582const char*pattern,int prefix,int patternlen, 583int flags) 584{ 585if(prefix == patternlen) { 586if(patternlen == basenamelen && 587!strncmp_icase(pattern, basename, basenamelen)) 588return1; 589}else if(flags & EXC_FLAG_ENDSWITH) { 590/* "*literal" matching against "fooliteral" */ 591if(patternlen -1<= basenamelen && 592!strncmp_icase(pattern +1, 593 basename + basenamelen - (patternlen -1), 594 patternlen -1)) 595return1; 596}else{ 597if(fnmatch_icase_mem(pattern, patternlen, 598 basename, basenamelen, 5990) ==0) 600return1; 601} 602return0; 603} 604 605intmatch_pathname(const char*pathname,int pathlen, 606const char*base,int baselen, 607const char*pattern,int prefix,int patternlen, 608int flags) 609{ 610const char*name; 611int namelen; 612 613/* 614 * match with FNM_PATHNAME; the pattern has base implicitly 615 * in front of it. 616 */ 617if(*pattern =='/') { 618 pattern++; 619 patternlen--; 620 prefix--; 621} 622 623/* 624 * baselen does not count the trailing slash. base[] may or 625 * may not end with a trailing slash though. 626 */ 627if(pathlen < baselen +1|| 628(baselen && pathname[baselen] !='/') || 629strncmp_icase(pathname, base, baselen)) 630return0; 631 632 namelen = baselen ? pathlen - baselen -1: pathlen; 633 name = pathname + pathlen - namelen; 634 635if(prefix) { 636/* 637 * if the non-wildcard part is longer than the 638 * remaining pathname, surely it cannot match. 639 */ 640if(prefix > namelen) 641return0; 642 643if(strncmp_icase(pattern, name, prefix)) 644return0; 645 pattern += prefix; 646 patternlen -= prefix; 647 name += prefix; 648 namelen -= prefix; 649 650/* 651 * If the whole pattern did not have a wildcard, 652 * then our prefix match is all we need; we 653 * do not need to call fnmatch at all. 654 */ 655if(!patternlen && !namelen) 656return1; 657} 658 659returnfnmatch_icase_mem(pattern, patternlen, 660 name, namelen, 661 WM_PATHNAME) ==0; 662} 663 664/* 665 * Scan the given exclude list in reverse to see whether pathname 666 * should be ignored. The first match (i.e. the last on the list), if 667 * any, determines the fate. Returns the exclude_list element which 668 * matched, or NULL for undecided. 669 */ 670static struct exclude *last_exclude_matching_from_list(const char*pathname, 671int pathlen, 672const char*basename, 673int*dtype, 674struct exclude_list *el) 675{ 676int i; 677 678if(!el->nr) 679return NULL;/* undefined */ 680 681for(i = el->nr -1;0<= i; i--) { 682struct exclude *x = el->excludes[i]; 683const char*exclude = x->pattern; 684int prefix = x->nowildcardlen; 685 686if(x->flags & EXC_FLAG_MUSTBEDIR) { 687if(*dtype == DT_UNKNOWN) 688*dtype =get_dtype(NULL, pathname, pathlen); 689if(*dtype != DT_DIR) 690continue; 691} 692 693if(x->flags & EXC_FLAG_NODIR) { 694if(match_basename(basename, 695 pathlen - (basename - pathname), 696 exclude, prefix, x->patternlen, 697 x->flags)) 698return x; 699continue; 700} 701 702assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 703if(match_pathname(pathname, pathlen, 704 x->base, x->baselen ? x->baselen -1:0, 705 exclude, prefix, x->patternlen, x->flags)) 706return x; 707} 708return NULL;/* undecided */ 709} 710 711/* 712 * Scan the list and let the last match determine the fate. 713 * Return 1 for exclude, 0 for include and -1 for undecided. 714 */ 715intis_excluded_from_list(const char*pathname, 716int pathlen,const char*basename,int*dtype, 717struct exclude_list *el) 718{ 719struct exclude *exclude; 720 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 721if(exclude) 722return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 723return-1;/* undecided */ 724} 725 726static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 727const char*pathname,int pathlen,const char*basename, 728int*dtype_p) 729{ 730int i, j; 731struct exclude_list_group *group; 732struct exclude *exclude; 733for(i = EXC_CMDL; i <= EXC_FILE; i++) { 734 group = &dir->exclude_list_group[i]; 735for(j = group->nr -1; j >=0; j--) { 736 exclude =last_exclude_matching_from_list( 737 pathname, pathlen, basename, dtype_p, 738&group->el[j]); 739if(exclude) 740return exclude; 741} 742} 743return NULL; 744} 745 746/* 747 * Loads the per-directory exclude list for the substring of base 748 * which has a char length of baselen. 749 */ 750static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 751{ 752struct exclude_list_group *group; 753struct exclude_list *el; 754struct exclude_stack *stk = NULL; 755int current; 756 757if((!dir->exclude_per_dir) || 758(baselen +strlen(dir->exclude_per_dir) >= PATH_MAX)) 759return;/* too long a path -- ignore */ 760 761 group = &dir->exclude_list_group[EXC_DIRS]; 762 763/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 764 * which originate from directories not in the prefix of the 765 * path being checked. */ 766while((stk = dir->exclude_stack) != NULL) { 767if(stk->baselen <= baselen && 768!strncmp(dir->basebuf, base, stk->baselen)) 769break; 770 el = &group->el[dir->exclude_stack->exclude_ix]; 771 dir->exclude_stack = stk->prev; 772free((char*)el->src);/* see strdup() below */ 773clear_exclude_list(el); 774free(stk); 775 group->nr--; 776} 777 778/* Read from the parent directories and push them down. */ 779 current = stk ? stk->baselen : -1; 780while(current < baselen) { 781struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 782const char*cp; 783 784if(current <0) { 785 cp = base; 786 current =0; 787} 788else{ 789 cp =strchr(base + current +1,'/'); 790if(!cp) 791die("oops in prep_exclude"); 792 cp++; 793} 794 stk->prev = dir->exclude_stack; 795 stk->baselen = cp - base; 796memcpy(dir->basebuf + current, base + current, 797 stk->baselen - current); 798strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); 799/* 800 * dir->basebuf gets reused by the traversal, but we 801 * need fname to remain unchanged to ensure the src 802 * member of each struct exclude correctly 803 * back-references its source file. Other invocations 804 * of add_exclude_list provide stable strings, so we 805 * strdup() and free() here in the caller. 806 */ 807 el =add_exclude_list(dir, EXC_DIRS,strdup(dir->basebuf)); 808 stk->exclude_ix = group->nr -1; 809add_excludes_from_file_to_list(dir->basebuf, 810 dir->basebuf, stk->baselen, 811 el,1); 812 dir->exclude_stack = stk; 813 current = stk->baselen; 814} 815 dir->basebuf[baselen] ='\0'; 816} 817 818/* 819 * Loads the exclude lists for the directory containing pathname, then 820 * scans all exclude lists to determine whether pathname is excluded. 821 * Returns the exclude_list element which matched, or NULL for 822 * undecided. 823 */ 824static struct exclude *last_exclude_matching(struct dir_struct *dir, 825const char*pathname, 826int*dtype_p) 827{ 828int pathlen =strlen(pathname); 829const char*basename =strrchr(pathname,'/'); 830 basename = (basename) ? basename+1: pathname; 831 832prep_exclude(dir, pathname, basename-pathname); 833 834returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 835 basename, dtype_p); 836} 837 838/* 839 * Loads the exclude lists for the directory containing pathname, then 840 * scans all exclude lists to determine whether pathname is excluded. 841 * Returns 1 if true, otherwise 0. 842 */ 843static intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 844{ 845struct exclude *exclude = 846last_exclude_matching(dir, pathname, dtype_p); 847if(exclude) 848return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 849return0; 850} 851 852voidpath_exclude_check_init(struct path_exclude_check *check, 853struct dir_struct *dir) 854{ 855 check->dir = dir; 856 check->exclude = NULL; 857strbuf_init(&check->path,256); 858} 859 860voidpath_exclude_check_clear(struct path_exclude_check *check) 861{ 862strbuf_release(&check->path); 863} 864 865/* 866 * For each subdirectory in name, starting with the top-most, checks 867 * to see if that subdirectory is excluded, and if so, returns the 868 * corresponding exclude structure. Otherwise, checks whether name 869 * itself (which is presumably a file) is excluded. 870 * 871 * A path to a directory known to be excluded is left in check->path to 872 * optimize for repeated checks for files in the same excluded directory. 873 */ 874struct exclude *last_exclude_matching_path(struct path_exclude_check *check, 875const char*name,int namelen, 876int*dtype) 877{ 878int i; 879struct strbuf *path = &check->path; 880struct exclude *exclude; 881 882/* 883 * we allow the caller to pass namelen as an optimization; it 884 * must match the length of the name, as we eventually call 885 * is_excluded() on the whole name string. 886 */ 887if(namelen <0) 888 namelen =strlen(name); 889 890/* 891 * If path is non-empty, and name is equal to path or a 892 * subdirectory of path, name should be excluded, because 893 * it's inside a directory which is already known to be 894 * excluded and was previously left in check->path. 895 */ 896if(path->len && 897 path->len <= namelen && 898!memcmp(name, path->buf, path->len) && 899(!name[path->len] || name[path->len] =='/')) 900return check->exclude; 901 902strbuf_setlen(path,0); 903for(i =0; name[i]; i++) { 904int ch = name[i]; 905 906if(ch =='/') { 907int dt = DT_DIR; 908 exclude =last_exclude_matching(check->dir, 909 path->buf, &dt); 910if(exclude) { 911 check->exclude = exclude; 912return exclude; 913} 914} 915strbuf_addch(path, ch); 916} 917 918/* An entry in the index; cannot be a directory with subentries */ 919strbuf_setlen(path,0); 920 921returnlast_exclude_matching(check->dir, name, dtype); 922} 923 924/* 925 * Is this name excluded? This is for a caller like show_files() that 926 * do not honor directory hierarchy and iterate through paths that are 927 * possibly in an ignored directory. 928 */ 929intis_path_excluded(struct path_exclude_check *check, 930const char*name,int namelen,int*dtype) 931{ 932struct exclude *exclude = 933last_exclude_matching_path(check, name, namelen, dtype); 934if(exclude) 935return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 936return0; 937} 938 939static struct dir_entry *dir_entry_new(const char*pathname,int len) 940{ 941struct dir_entry *ent; 942 943 ent =xmalloc(sizeof(*ent) + len +1); 944 ent->len = len; 945memcpy(ent->name, pathname, len); 946 ent->name[len] =0; 947return ent; 948} 949 950static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 951{ 952if(cache_name_exists(pathname, len, ignore_case)) 953return NULL; 954 955ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 956return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 957} 958 959struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 960{ 961if(!cache_name_is_other(pathname, len)) 962return NULL; 963 964ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 965return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 966} 967 968enum exist_status { 969 index_nonexistent =0, 970 index_directory, 971 index_gitdir 972}; 973 974/* 975 * Do not use the alphabetically stored index to look up 976 * the directory name; instead, use the case insensitive 977 * name hash. 978 */ 979static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 980{ 981struct cache_entry *ce =index_name_exists(&the_index, dirname, len +1, ignore_case); 982unsigned char endchar; 983 984if(!ce) 985return index_nonexistent; 986 endchar = ce->name[len]; 987 988/* 989 * The cache_entry structure returned will contain this dirname 990 * and possibly additional path components. 991 */ 992if(endchar =='/') 993return index_directory; 994 995/* 996 * If there are no additional path components, then this cache_entry 997 * represents a submodule. Submodules, despite being directories, 998 * are stored in the cache without a closing slash. 999 */1000if(!endchar &&S_ISGITLINK(ce->ce_mode))1001return index_gitdir;10021003/* This should never be hit, but it exists just in case. */1004return index_nonexistent;1005}10061007/*1008 * The index sorts alphabetically by entry name, which1009 * means that a gitlink sorts as '\0' at the end, while1010 * a directory (which is defined not as an entry, but as1011 * the files it contains) will sort with the '/' at the1012 * end.1013 */1014static enum exist_status directory_exists_in_index(const char*dirname,int len)1015{1016int pos;10171018if(ignore_case)1019returndirectory_exists_in_index_icase(dirname, len);10201021 pos =cache_name_pos(dirname, len);1022if(pos <0)1023 pos = -pos-1;1024while(pos < active_nr) {1025struct cache_entry *ce = active_cache[pos++];1026unsigned char endchar;10271028if(strncmp(ce->name, dirname, len))1029break;1030 endchar = ce->name[len];1031if(endchar >'/')1032break;1033if(endchar =='/')1034return index_directory;1035if(!endchar &&S_ISGITLINK(ce->ce_mode))1036return index_gitdir;1037}1038return index_nonexistent;1039}10401041/*1042 * When we find a directory when traversing the filesystem, we1043 * have three distinct cases:1044 *1045 * - ignore it1046 * - see it as a directory1047 * - recurse into it1048 *1049 * and which one we choose depends on a combination of existing1050 * git index contents and the flags passed into the directory1051 * traversal routine.1052 *1053 * Case 1: If we *already* have entries in the index under that1054 * directory name, we always recurse into the directory to see1055 * all the files.1056 *1057 * Case 2: If we *already* have that directory name as a gitlink,1058 * we always continue to see it as a gitlink, regardless of whether1059 * there is an actual git directory there or not (it might not1060 * be checked out as a subproject!)1061 *1062 * Case 3: if we didn't have it in the index previously, we1063 * have a few sub-cases:1064 *1065 * (a) if "show_other_directories" is true, we show it as1066 * just a directory, unless "hide_empty_directories" is1067 * also true and the directory is empty, in which case1068 * we just ignore it entirely.1069 * if we are looking for ignored directories, look if it1070 * contains only ignored files to decide if it must be shown as1071 * ignored or not.1072 * (b) if it looks like a git directory, and we don't have1073 * 'no_gitlinks' set we treat it as a gitlink, and show it1074 * as a directory.1075 * (c) otherwise, we recurse into it.1076 */1077enum directory_treatment {1078 show_directory,1079 ignore_directory,1080 recurse_into_directory1081};10821083static enum directory_treatment treat_directory(struct dir_struct *dir,1084const char*dirname,int len,int exclude,1085const struct path_simplify *simplify)1086{1087/* The "len-1" is to strip the final '/' */1088switch(directory_exists_in_index(dirname, len-1)) {1089case index_directory:1090return recurse_into_directory;10911092case index_gitdir:1093if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1094return ignore_directory;1095return show_directory;10961097case index_nonexistent:1098if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1099break;1100if(!(dir->flags & DIR_NO_GITLINKS)) {1101unsigned char sha1[20];1102if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1103return show_directory;1104}1105return recurse_into_directory;1106}11071108/* This is the "show_other_directories" case */11091110/* might be a sub directory in an excluded directory */1111if(!exclude) {1112struct path_exclude_check check;1113int dt = DT_DIR;1114path_exclude_check_init(&check, dir);1115 exclude =is_path_excluded(&check, dirname, len, &dt);1116path_exclude_check_clear(&check);1117}11181119/*1120 * We are looking for ignored files and our directory is not ignored,1121 * check if it contains untracked files (i.e. is listed as untracked)1122 */1123if((dir->flags & DIR_SHOW_IGNORED) && !exclude) {1124int ignored;1125 dir->flags &= ~DIR_SHOW_IGNORED;1126 ignored =read_directory_recursive(dir, dirname, len,1, simplify);1127 dir->flags |= DIR_SHOW_IGNORED;11281129if(ignored)1130return ignore_directory;1131}11321133if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1134return show_directory;1135if(!read_directory_recursive(dir, dirname, len,1, simplify))1136return ignore_directory;1137return show_directory;1138}11391140/*1141 * Decide what to do when we find a file while traversing the1142 * filesystem. Mostly two cases:1143 *1144 * 1. We are looking for ignored files1145 * (a) File is ignored, include it1146 * (b) File is in ignored path, include it1147 * (c) File is not ignored, exclude it1148 *1149 * 2. Other scenarios, include the file if not excluded1150 *1151 * Return 1 for exclude, 0 for include.1152 */1153static inttreat_file(struct dir_struct *dir,struct strbuf *path,int exclude,int*dtype)1154{1155struct path_exclude_check check;1156int exclude_file =0;11571158/* Always exclude indexed files */1159if(index_name_exists(&the_index, path->buf, path->len, ignore_case))1160return1;11611162if(exclude)1163 exclude_file = !(dir->flags & DIR_SHOW_IGNORED);1164else if(dir->flags & DIR_SHOW_IGNORED) {1165path_exclude_check_init(&check, dir);11661167if(!is_path_excluded(&check, path->buf, path->len, dtype))1168 exclude_file =1;11691170path_exclude_check_clear(&check);1171}11721173return exclude_file;1174}11751176/*1177 * This is an inexact early pruning of any recursive directory1178 * reading - if the path cannot possibly be in the pathspec,1179 * return true, and we'll skip it early.1180 */1181static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1182{1183if(simplify) {1184for(;;) {1185const char*match = simplify->path;1186int len = simplify->len;11871188if(!match)1189break;1190if(len > pathlen)1191 len = pathlen;1192if(!memcmp(path, match, len))1193return0;1194 simplify++;1195}1196return1;1197}1198return0;1199}12001201/*1202 * This function tells us whether an excluded path matches a1203 * list of "interesting" pathspecs. That is, whether a path matched1204 * by any of the pathspecs could possibly be ignored by excluding1205 * the specified path. This can happen if:1206 *1207 * 1. the path is mentioned explicitly in the pathspec1208 *1209 * 2. the path is a directory prefix of some element in the1210 * pathspec1211 */1212static intexclude_matches_pathspec(const char*path,int len,1213const struct path_simplify *simplify)1214{1215if(simplify) {1216for(; simplify->path; simplify++) {1217if(len == simplify->len1218&& !memcmp(path, simplify->path, len))1219return1;1220if(len < simplify->len1221&& simplify->path[len] =='/'1222&& !memcmp(path, simplify->path, len))1223return1;1224}1225}1226return0;1227}12281229static intget_index_dtype(const char*path,int len)1230{1231int pos;1232struct cache_entry *ce;12331234 ce =cache_name_exists(path, len,0);1235if(ce) {1236if(!ce_uptodate(ce))1237return DT_UNKNOWN;1238if(S_ISGITLINK(ce->ce_mode))1239return DT_DIR;1240/*1241 * Nobody actually cares about the1242 * difference between DT_LNK and DT_REG1243 */1244return DT_REG;1245}12461247/* Try to look it up as a directory */1248 pos =cache_name_pos(path, len);1249if(pos >=0)1250return DT_UNKNOWN;1251 pos = -pos-1;1252while(pos < active_nr) {1253 ce = active_cache[pos++];1254if(strncmp(ce->name, path, len))1255break;1256if(ce->name[len] >'/')1257break;1258if(ce->name[len] <'/')1259continue;1260if(!ce_uptodate(ce))1261break;/* continue? */1262return DT_DIR;1263}1264return DT_UNKNOWN;1265}12661267static intget_dtype(struct dirent *de,const char*path,int len)1268{1269int dtype = de ?DTYPE(de) : DT_UNKNOWN;1270struct stat st;12711272if(dtype != DT_UNKNOWN)1273return dtype;1274 dtype =get_index_dtype(path, len);1275if(dtype != DT_UNKNOWN)1276return dtype;1277if(lstat(path, &st))1278return dtype;1279if(S_ISREG(st.st_mode))1280return DT_REG;1281if(S_ISDIR(st.st_mode))1282return DT_DIR;1283if(S_ISLNK(st.st_mode))1284return DT_LNK;1285return dtype;1286}12871288enum path_treatment {1289 path_ignored,1290 path_handled,1291 path_recurse1292};12931294static enum path_treatment treat_one_path(struct dir_struct *dir,1295struct strbuf *path,1296const struct path_simplify *simplify,1297int dtype,struct dirent *de)1298{1299int exclude =is_excluded(dir, path->buf, &dtype);1300if(exclude && (dir->flags & DIR_COLLECT_IGNORED)1301&&exclude_matches_pathspec(path->buf, path->len, simplify))1302dir_add_ignored(dir, path->buf, path->len);13031304/*1305 * Excluded? If we don't explicitly want to show1306 * ignored files, ignore it1307 */1308if(exclude && !(dir->flags & DIR_SHOW_IGNORED))1309return path_ignored;13101311if(dtype == DT_UNKNOWN)1312 dtype =get_dtype(de, path->buf, path->len);13131314switch(dtype) {1315default:1316return path_ignored;1317case DT_DIR:1318strbuf_addch(path,'/');1319switch(treat_directory(dir, path->buf, path->len, exclude, simplify)) {1320case show_directory:1321break;1322case recurse_into_directory:1323return path_recurse;1324case ignore_directory:1325return path_ignored;1326}1327break;1328case DT_REG:1329case DT_LNK:1330switch(treat_file(dir, path, exclude, &dtype)) {1331case1:1332return path_ignored;1333default:1334break;1335}1336}1337return path_handled;1338}13391340static enum path_treatment treat_path(struct dir_struct *dir,1341struct dirent *de,1342struct strbuf *path,1343int baselen,1344const struct path_simplify *simplify)1345{1346int dtype;13471348if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1349return path_ignored;1350strbuf_setlen(path, baselen);1351strbuf_addstr(path, de->d_name);1352if(simplify_away(path->buf, path->len, simplify))1353return path_ignored;13541355 dtype =DTYPE(de);1356returntreat_one_path(dir, path, simplify, dtype, de);1357}13581359/*1360 * Read a directory tree. We currently ignore anything but1361 * directories, regular files and symlinks. That's because git1362 * doesn't handle them at all yet. Maybe that will change some1363 * day.1364 *1365 * Also, we ignore the name ".git" (even if it is not a directory).1366 * That likely will not change.1367 */1368static intread_directory_recursive(struct dir_struct *dir,1369const char*base,int baselen,1370int check_only,1371const struct path_simplify *simplify)1372{1373DIR*fdir;1374int contents =0;1375struct dirent *de;1376struct strbuf path = STRBUF_INIT;13771378strbuf_add(&path, base, baselen);13791380 fdir =opendir(path.len ? path.buf :".");1381if(!fdir)1382goto out;13831384while((de =readdir(fdir)) != NULL) {1385switch(treat_path(dir, de, &path, baselen, simplify)) {1386case path_recurse:1387 contents +=read_directory_recursive(dir, path.buf,1388 path.len, check_only, simplify);1389continue;1390case path_ignored:1391continue;1392case path_handled:1393break;1394}1395 contents++;1396if(check_only)1397break;1398dir_add_name(dir, path.buf, path.len);1399}1400closedir(fdir);1401 out:1402strbuf_release(&path);14031404return contents;1405}14061407static intcmp_name(const void*p1,const void*p2)1408{1409const struct dir_entry *e1 = *(const struct dir_entry **)p1;1410const struct dir_entry *e2 = *(const struct dir_entry **)p2;14111412returncache_name_compare(e1->name, e1->len,1413 e2->name, e2->len);1414}14151416static struct path_simplify *create_simplify(const char**pathspec)1417{1418int nr, alloc =0;1419struct path_simplify *simplify = NULL;14201421if(!pathspec)1422return NULL;14231424for(nr =0; ; nr++) {1425const char*match;1426if(nr >= alloc) {1427 alloc =alloc_nr(alloc);1428 simplify =xrealloc(simplify, alloc *sizeof(*simplify));1429}1430 match = *pathspec++;1431if(!match)1432break;1433 simplify[nr].path = match;1434 simplify[nr].len =simple_length(match);1435}1436 simplify[nr].path = NULL;1437 simplify[nr].len =0;1438return simplify;1439}14401441static voidfree_simplify(struct path_simplify *simplify)1442{1443free(simplify);1444}14451446static inttreat_leading_path(struct dir_struct *dir,1447const char*path,int len,1448const struct path_simplify *simplify)1449{1450struct strbuf sb = STRBUF_INIT;1451int baselen, rc =0;1452const char*cp;1453int old_flags = dir->flags;14541455while(len && path[len -1] =='/')1456 len--;1457if(!len)1458return1;1459 baselen =0;1460 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1461while(1) {1462 cp = path + baselen + !!baselen;1463 cp =memchr(cp,'/', path + len - cp);1464if(!cp)1465 baselen = len;1466else1467 baselen = cp - path;1468strbuf_setlen(&sb,0);1469strbuf_add(&sb, path, baselen);1470if(!is_directory(sb.buf))1471break;1472if(simplify_away(sb.buf, sb.len, simplify))1473break;1474if(treat_one_path(dir, &sb, simplify,1475 DT_DIR, NULL) == path_ignored)1476break;/* do not recurse into it */1477if(len <= baselen) {1478 rc =1;1479break;/* finished checking */1480}1481}1482strbuf_release(&sb);1483 dir->flags = old_flags;1484return rc;1485}14861487intread_directory(struct dir_struct *dir,const char*path,int len,const char**pathspec)1488{1489struct path_simplify *simplify;14901491if(has_symlink_leading_path(path, len))1492return dir->nr;14931494 simplify =create_simplify(pathspec);1495if(!len ||treat_leading_path(dir, path, len, simplify))1496read_directory_recursive(dir, path, len,0, simplify);1497free_simplify(simplify);1498qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1499qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1500return dir->nr;1501}15021503intfile_exists(const char*f)1504{1505struct stat sb;1506returnlstat(f, &sb) ==0;1507}15081509/*1510 * Given two normalized paths (a trailing slash is ok), if subdir is1511 * outside dir, return -1. Otherwise return the offset in subdir that1512 * can be used as relative path to dir.1513 */1514intdir_inside_of(const char*subdir,const char*dir)1515{1516int offset =0;15171518assert(dir && subdir && *dir && *subdir);15191520while(*dir && *subdir && *dir == *subdir) {1521 dir++;1522 subdir++;1523 offset++;1524}15251526/* hel[p]/me vs hel[l]/yeah */1527if(*dir && *subdir)1528return-1;15291530if(!*subdir)1531return!*dir ? offset : -1;/* same dir */15321533/* foo/[b]ar vs foo/[] */1534if(is_dir_sep(dir[-1]))1535returnis_dir_sep(subdir[-1]) ? offset : -1;15361537/* foo[/]bar vs foo[] */1538returnis_dir_sep(*subdir) ? offset +1: -1;1539}15401541intis_inside_dir(const char*dir)1542{1543char cwd[PATH_MAX];1544if(!dir)1545return0;1546if(!getcwd(cwd,sizeof(cwd)))1547die_errno("can't find the current directory");1548returndir_inside_of(cwd, dir) >=0;1549}15501551intis_empty_dir(const char*path)1552{1553DIR*dir =opendir(path);1554struct dirent *e;1555int ret =1;15561557if(!dir)1558return0;15591560while((e =readdir(dir)) != NULL)1561if(!is_dot_or_dotdot(e->d_name)) {1562 ret =0;1563break;1564}15651566closedir(dir);1567return ret;1568}15691570static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1571{1572DIR*dir;1573struct dirent *e;1574int ret =0, original_len = path->len, len, kept_down =0;1575int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1576int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1577unsigned char submodule_head[20];15781579if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1580!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1581/* Do not descend and nuke a nested git work tree. */1582if(kept_up)1583*kept_up =1;1584return0;1585}15861587 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1588 dir =opendir(path->buf);1589if(!dir) {1590/* an empty dir could be removed even if it is unreadble */1591if(!keep_toplevel)1592returnrmdir(path->buf);1593else1594return-1;1595}1596if(path->buf[original_len -1] !='/')1597strbuf_addch(path,'/');15981599 len = path->len;1600while((e =readdir(dir)) != NULL) {1601struct stat st;1602if(is_dot_or_dotdot(e->d_name))1603continue;16041605strbuf_setlen(path, len);1606strbuf_addstr(path, e->d_name);1607if(lstat(path->buf, &st))1608;/* fall thru */1609else if(S_ISDIR(st.st_mode)) {1610if(!remove_dir_recurse(path, flag, &kept_down))1611continue;/* happy */1612}else if(!only_empty && !unlink(path->buf))1613continue;/* happy, too */16141615/* path too long, stat fails, or non-directory still exists */1616 ret = -1;1617break;1618}1619closedir(dir);16201621strbuf_setlen(path, original_len);1622if(!ret && !keep_toplevel && !kept_down)1623 ret =rmdir(path->buf);1624else if(kept_up)1625/*1626 * report the uplevel that it is not an error that we1627 * did not rmdir() our directory.1628 */1629*kept_up = !ret;1630return ret;1631}16321633intremove_dir_recursively(struct strbuf *path,int flag)1634{1635returnremove_dir_recurse(path, flag, NULL);1636}16371638voidsetup_standard_excludes(struct dir_struct *dir)1639{1640const char*path;1641char*xdg_path;16421643 dir->exclude_per_dir =".gitignore";1644 path =git_path("info/exclude");1645if(!excludes_file) {1646home_config_paths(NULL, &xdg_path,"ignore");1647 excludes_file = xdg_path;1648}1649if(!access_or_warn(path, R_OK))1650add_excludes_from_file(dir, path);1651if(excludes_file && !access_or_warn(excludes_file, R_OK))1652add_excludes_from_file(dir, excludes_file);1653}16541655intremove_path(const char*name)1656{1657char*slash;16581659if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1660return-1;16611662 slash =strrchr(name,'/');1663if(slash) {1664char*dirs =xstrdup(name);1665 slash = dirs + (slash - name);1666do{1667*slash ='\0';1668}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1669free(dirs);1670}1671return0;1672}16731674static intpathspec_item_cmp(const void*a_,const void*b_)1675{1676struct pathspec_item *a, *b;16771678 a = (struct pathspec_item *)a_;1679 b = (struct pathspec_item *)b_;1680returnstrcmp(a->match, b->match);1681}16821683intinit_pathspec(struct pathspec *pathspec,const char**paths)1684{1685const char**p = paths;1686int i;16871688memset(pathspec,0,sizeof(*pathspec));1689if(!p)1690return0;1691while(*p)1692 p++;1693 pathspec->raw = paths;1694 pathspec->nr = p - paths;1695if(!pathspec->nr)1696return0;16971698 pathspec->items =xmalloc(sizeof(struct pathspec_item)*pathspec->nr);1699for(i =0; i < pathspec->nr; i++) {1700struct pathspec_item *item = pathspec->items+i;1701const char*path = paths[i];17021703 item->match = path;1704 item->len =strlen(path);1705 item->flags =0;1706if(limit_pathspec_to_literal()) {1707 item->nowildcard_len = item->len;1708}else{1709 item->nowildcard_len =simple_length(path);1710if(item->nowildcard_len < item->len) {1711 pathspec->has_wildcard =1;1712if(path[item->nowildcard_len] =='*'&&1713no_wildcard(path + item->nowildcard_len +1))1714 item->flags |= PATHSPEC_ONESTAR;1715}1716}1717}17181719qsort(pathspec->items, pathspec->nr,1720sizeof(struct pathspec_item), pathspec_item_cmp);17211722return0;1723}17241725voidfree_pathspec(struct pathspec *pathspec)1726{1727free(pathspec->items);1728 pathspec->items = NULL;1729}17301731intlimit_pathspec_to_literal(void)1732{1733static int flag = -1;1734if(flag <0)1735 flag =git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT,0);1736return flag;1737}17381739/*1740 * Frees memory within dir which was allocated for exclude lists and1741 * the exclude_stack. Does not free dir itself.1742 */1743voidclear_directory(struct dir_struct *dir)1744{1745int i, j;1746struct exclude_list_group *group;1747struct exclude_list *el;1748struct exclude_stack *stk;17491750for(i = EXC_CMDL; i <= EXC_FILE; i++) {1751 group = &dir->exclude_list_group[i];1752for(j =0; j < group->nr; j++) {1753 el = &group->el[j];1754if(i == EXC_DIRS)1755free((char*)el->src);1756clear_exclude_list(el);1757}1758free(group->el);1759}17601761 stk = dir->exclude_stack;1762while(stk) {1763struct exclude_stack *prev = stk->prev;1764free(stk);1765 stk = prev;1766}1767}