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 581/* 582 * Loads the per-directory exclude list for the substring of base 583 * which has a char length of baselen. 584 */ 585static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 586{ 587struct exclude_list_group *group; 588struct exclude_list *el; 589struct exclude_stack *stk = NULL; 590int current; 591 592if((!dir->exclude_per_dir) || 593(baselen +strlen(dir->exclude_per_dir) >= PATH_MAX)) 594return;/* too long a path -- ignore */ 595 596 group = &dir->exclude_list_group[EXC_DIRS]; 597 598/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 599 * which originate from directories not in the prefix of the 600 * path being checked. */ 601while((stk = dir->exclude_stack) != NULL) { 602if(stk->baselen <= baselen && 603!strncmp(dir->basebuf, base, stk->baselen)) 604break; 605 el = &group->el[dir->exclude_stack->exclude_ix]; 606 dir->exclude_stack = stk->prev; 607free((char*)el->src);/* see strdup() below */ 608clear_exclude_list(el); 609free(stk); 610 group->nr--; 611} 612 613/* Read from the parent directories and push them down. */ 614 current = stk ? stk->baselen : -1; 615while(current < baselen) { 616struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 617const char*cp; 618 619if(current <0) { 620 cp = base; 621 current =0; 622} 623else{ 624 cp =strchr(base + current +1,'/'); 625if(!cp) 626die("oops in prep_exclude"); 627 cp++; 628} 629 stk->prev = dir->exclude_stack; 630 stk->baselen = cp - base; 631memcpy(dir->basebuf + current, base + current, 632 stk->baselen - current); 633strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); 634/* 635 * dir->basebuf gets reused by the traversal, but we 636 * need fname to remain unchanged to ensure the src 637 * member of each struct exclude correctly 638 * back-references its source file. Other invocations 639 * of add_exclude_list provide stable strings, so we 640 * strdup() and free() here in the caller. 641 */ 642 el =add_exclude_list(dir, EXC_DIRS,strdup(dir->basebuf)); 643 stk->exclude_ix = group->nr -1; 644add_excludes_from_file_to_list(dir->basebuf, 645 dir->basebuf, stk->baselen, 646 el,1); 647 dir->exclude_stack = stk; 648 current = stk->baselen; 649} 650 dir->basebuf[baselen] ='\0'; 651} 652 653intmatch_basename(const char*basename,int basenamelen, 654const char*pattern,int prefix,int patternlen, 655int flags) 656{ 657if(prefix == patternlen) { 658if(patternlen == basenamelen && 659!strncmp_icase(pattern, basename, basenamelen)) 660return1; 661}else if(flags & EXC_FLAG_ENDSWITH) { 662/* "*literal" matching against "fooliteral" */ 663if(patternlen -1<= basenamelen && 664!strncmp_icase(pattern +1, 665 basename + basenamelen - (patternlen -1), 666 patternlen -1)) 667return1; 668}else{ 669if(fnmatch_icase_mem(pattern, patternlen, 670 basename, basenamelen, 6710) ==0) 672return1; 673} 674return0; 675} 676 677intmatch_pathname(const char*pathname,int pathlen, 678const char*base,int baselen, 679const char*pattern,int prefix,int patternlen, 680int flags) 681{ 682const char*name; 683int namelen; 684 685/* 686 * match with FNM_PATHNAME; the pattern has base implicitly 687 * in front of it. 688 */ 689if(*pattern =='/') { 690 pattern++; 691 patternlen--; 692 prefix--; 693} 694 695/* 696 * baselen does not count the trailing slash. base[] may or 697 * may not end with a trailing slash though. 698 */ 699if(pathlen < baselen +1|| 700(baselen && pathname[baselen] !='/') || 701strncmp_icase(pathname, base, baselen)) 702return0; 703 704 namelen = baselen ? pathlen - baselen -1: pathlen; 705 name = pathname + pathlen - namelen; 706 707if(prefix) { 708/* 709 * if the non-wildcard part is longer than the 710 * remaining pathname, surely it cannot match. 711 */ 712if(prefix > namelen) 713return0; 714 715if(strncmp_icase(pattern, name, prefix)) 716return0; 717 pattern += prefix; 718 patternlen -= prefix; 719 name += prefix; 720 namelen -= prefix; 721 722/* 723 * If the whole pattern did not have a wildcard, 724 * then our prefix match is all we need; we 725 * do not need to call fnmatch at all. 726 */ 727if(!patternlen && !namelen) 728return1; 729} 730 731returnfnmatch_icase_mem(pattern, patternlen, 732 name, namelen, 733 WM_PATHNAME) ==0; 734} 735 736/* 737 * Scan the given exclude list in reverse to see whether pathname 738 * should be ignored. The first match (i.e. the last on the list), if 739 * any, determines the fate. Returns the exclude_list element which 740 * matched, or NULL for undecided. 741 */ 742static struct exclude *last_exclude_matching_from_list(const char*pathname, 743int pathlen, 744const char*basename, 745int*dtype, 746struct exclude_list *el) 747{ 748int i; 749 750if(!el->nr) 751return NULL;/* undefined */ 752 753for(i = el->nr -1;0<= i; i--) { 754struct exclude *x = el->excludes[i]; 755const char*exclude = x->pattern; 756int prefix = x->nowildcardlen; 757 758if(x->flags & EXC_FLAG_MUSTBEDIR) { 759if(*dtype == DT_UNKNOWN) 760*dtype =get_dtype(NULL, pathname, pathlen); 761if(*dtype != DT_DIR) 762continue; 763} 764 765if(x->flags & EXC_FLAG_NODIR) { 766if(match_basename(basename, 767 pathlen - (basename - pathname), 768 exclude, prefix, x->patternlen, 769 x->flags)) 770return x; 771continue; 772} 773 774assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 775if(match_pathname(pathname, pathlen, 776 x->base, x->baselen ? x->baselen -1:0, 777 exclude, prefix, x->patternlen, x->flags)) 778return x; 779} 780return NULL;/* undecided */ 781} 782 783/* 784 * Scan the list and let the last match determine the fate. 785 * Return 1 for exclude, 0 for include and -1 for undecided. 786 */ 787intis_excluded_from_list(const char*pathname, 788int pathlen,const char*basename,int*dtype, 789struct exclude_list *el) 790{ 791struct exclude *exclude; 792 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 793if(exclude) 794return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 795return-1;/* undecided */ 796} 797 798/* 799 * Loads the exclude lists for the directory containing pathname, then 800 * scans all exclude lists to determine whether pathname is excluded. 801 * Returns the exclude_list element which matched, or NULL for 802 * undecided. 803 */ 804static struct exclude *last_exclude_matching(struct dir_struct *dir, 805const char*pathname, 806int*dtype_p) 807{ 808int pathlen =strlen(pathname); 809int i, j; 810struct exclude_list_group *group; 811struct exclude *exclude; 812const char*basename =strrchr(pathname,'/'); 813 basename = (basename) ? basename+1: pathname; 814 815prep_exclude(dir, pathname, basename-pathname); 816 817for(i = EXC_CMDL; i <= EXC_FILE; i++) { 818 group = &dir->exclude_list_group[i]; 819for(j = group->nr -1; j >=0; j--) { 820 exclude =last_exclude_matching_from_list( 821 pathname, pathlen, basename, dtype_p, 822&group->el[j]); 823if(exclude) 824return exclude; 825} 826} 827return NULL; 828} 829 830/* 831 * Loads the exclude lists for the directory containing pathname, then 832 * scans all exclude lists to determine whether pathname is excluded. 833 * Returns 1 if true, otherwise 0. 834 */ 835static intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 836{ 837struct exclude *exclude = 838last_exclude_matching(dir, pathname, dtype_p); 839if(exclude) 840return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 841return0; 842} 843 844voidpath_exclude_check_init(struct path_exclude_check *check, 845struct dir_struct *dir) 846{ 847 check->dir = dir; 848 check->exclude = NULL; 849strbuf_init(&check->path,256); 850} 851 852voidpath_exclude_check_clear(struct path_exclude_check *check) 853{ 854strbuf_release(&check->path); 855} 856 857/* 858 * For each subdirectory in name, starting with the top-most, checks 859 * to see if that subdirectory is excluded, and if so, returns the 860 * corresponding exclude structure. Otherwise, checks whether name 861 * itself (which is presumably a file) is excluded. 862 * 863 * A path to a directory known to be excluded is left in check->path to 864 * optimize for repeated checks for files in the same excluded directory. 865 */ 866struct exclude *last_exclude_matching_path(struct path_exclude_check *check, 867const char*name,int namelen, 868int*dtype) 869{ 870int i; 871struct strbuf *path = &check->path; 872struct exclude *exclude; 873 874/* 875 * we allow the caller to pass namelen as an optimization; it 876 * must match the length of the name, as we eventually call 877 * is_excluded() on the whole name string. 878 */ 879if(namelen <0) 880 namelen =strlen(name); 881 882/* 883 * If path is non-empty, and name is equal to path or a 884 * subdirectory of path, name should be excluded, because 885 * it's inside a directory which is already known to be 886 * excluded and was previously left in check->path. 887 */ 888if(path->len && 889 path->len <= namelen && 890!memcmp(name, path->buf, path->len) && 891(!name[path->len] || name[path->len] =='/')) 892return check->exclude; 893 894strbuf_setlen(path,0); 895for(i =0; name[i]; i++) { 896int ch = name[i]; 897 898if(ch =='/') { 899int dt = DT_DIR; 900 exclude =last_exclude_matching(check->dir, 901 path->buf, &dt); 902if(exclude) { 903 check->exclude = exclude; 904return exclude; 905} 906} 907strbuf_addch(path, ch); 908} 909 910/* An entry in the index; cannot be a directory with subentries */ 911strbuf_setlen(path,0); 912 913returnlast_exclude_matching(check->dir, name, dtype); 914} 915 916/* 917 * Is this name excluded? This is for a caller like show_files() that 918 * do not honor directory hierarchy and iterate through paths that are 919 * possibly in an ignored directory. 920 */ 921intis_path_excluded(struct path_exclude_check *check, 922const char*name,int namelen,int*dtype) 923{ 924struct exclude *exclude = 925last_exclude_matching_path(check, name, namelen, dtype); 926if(exclude) 927return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 928return0; 929} 930 931static struct dir_entry *dir_entry_new(const char*pathname,int len) 932{ 933struct dir_entry *ent; 934 935 ent =xmalloc(sizeof(*ent) + len +1); 936 ent->len = len; 937memcpy(ent->name, pathname, len); 938 ent->name[len] =0; 939return ent; 940} 941 942static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 943{ 944if(!(dir->flags & DIR_SHOW_IGNORED) && 945cache_name_exists(pathname, len, ignore_case)) 946return NULL; 947 948ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 949return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 950} 951 952struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 953{ 954if(!cache_name_is_other(pathname, len)) 955return NULL; 956 957ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 958return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 959} 960 961enum exist_status { 962 index_nonexistent =0, 963 index_directory, 964 index_gitdir 965}; 966 967/* 968 * Do not use the alphabetically stored index to look up 969 * the directory name; instead, use the case insensitive 970 * name hash. 971 */ 972static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 973{ 974struct cache_entry *ce =index_name_exists(&the_index, dirname, len +1, ignore_case); 975unsigned char endchar; 976 977if(!ce) 978return index_nonexistent; 979 endchar = ce->name[len]; 980 981/* 982 * The cache_entry structure returned will contain this dirname 983 * and possibly additional path components. 984 */ 985if(endchar =='/') 986return index_directory; 987 988/* 989 * If there are no additional path components, then this cache_entry 990 * represents a submodule. Submodules, despite being directories, 991 * are stored in the cache without a closing slash. 992 */ 993if(!endchar &&S_ISGITLINK(ce->ce_mode)) 994return index_gitdir; 995 996/* This should never be hit, but it exists just in case. */ 997return index_nonexistent; 998} 9991000/*1001 * The index sorts alphabetically by entry name, which1002 * means that a gitlink sorts as '\0' at the end, while1003 * a directory (which is defined not as an entry, but as1004 * the files it contains) will sort with the '/' at the1005 * end.1006 */1007static enum exist_status directory_exists_in_index(const char*dirname,int len)1008{1009int pos;10101011if(ignore_case)1012returndirectory_exists_in_index_icase(dirname, len);10131014 pos =cache_name_pos(dirname, len);1015if(pos <0)1016 pos = -pos-1;1017while(pos < active_nr) {1018struct cache_entry *ce = active_cache[pos++];1019unsigned char endchar;10201021if(strncmp(ce->name, dirname, len))1022break;1023 endchar = ce->name[len];1024if(endchar >'/')1025break;1026if(endchar =='/')1027return index_directory;1028if(!endchar &&S_ISGITLINK(ce->ce_mode))1029return index_gitdir;1030}1031return index_nonexistent;1032}10331034/*1035 * When we find a directory when traversing the filesystem, we1036 * have three distinct cases:1037 *1038 * - ignore it1039 * - see it as a directory1040 * - recurse into it1041 *1042 * and which one we choose depends on a combination of existing1043 * git index contents and the flags passed into the directory1044 * traversal routine.1045 *1046 * Case 1: If we *already* have entries in the index under that1047 * directory name, we recurse into the directory to see all the files,1048 * unless the directory is excluded and we want to show ignored1049 * directories1050 *1051 * Case 2: If we *already* have that directory name as a gitlink,1052 * we always continue to see it as a gitlink, regardless of whether1053 * there is an actual git directory there or not (it might not1054 * be checked out as a subproject!)1055 *1056 * Case 3: if we didn't have it in the index previously, we1057 * have a few sub-cases:1058 *1059 * (a) if "show_other_directories" is true, we show it as1060 * just a directory, unless "hide_empty_directories" is1061 * also true and the directory is empty, in which case1062 * we just ignore it entirely.1063 * if we are looking for ignored directories, look if it1064 * contains only ignored files to decide if it must be shown as1065 * ignored or not.1066 * (b) if it looks like a git directory, and we don't have1067 * 'no_gitlinks' set we treat it as a gitlink, and show it1068 * as a directory.1069 * (c) otherwise, we recurse into it.1070 */1071enum directory_treatment {1072 show_directory,1073 ignore_directory,1074 recurse_into_directory1075};10761077static enum directory_treatment treat_directory(struct dir_struct *dir,1078const char*dirname,int len,int exclude,1079const struct path_simplify *simplify)1080{1081/* The "len-1" is to strip the final '/' */1082switch(directory_exists_in_index(dirname, len-1)) {1083case index_directory:1084if((dir->flags & DIR_SHOW_OTHER_DIRECTORIES) && exclude)1085break;10861087return recurse_into_directory;10881089case index_gitdir:1090if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1091return ignore_directory;1092return show_directory;10931094case index_nonexistent:1095if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1096break;1097if(!(dir->flags & DIR_NO_GITLINKS)) {1098unsigned char sha1[20];1099if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1100return show_directory;1101}1102return recurse_into_directory;1103}11041105/* This is the "show_other_directories" case */11061107/* might be a sub directory in an excluded directory */1108if(!exclude) {1109struct path_exclude_check check;1110int dt = DT_DIR;1111path_exclude_check_init(&check, dir);1112 exclude =is_path_excluded(&check, dirname, len, &dt);1113path_exclude_check_clear(&check);1114}11151116/*1117 * We are looking for ignored files and our directory is not ignored,1118 * check if it contains only ignored files1119 */1120if((dir->flags & DIR_SHOW_IGNORED) && !exclude) {1121int ignored;1122 dir->flags &= ~DIR_SHOW_IGNORED;1123 ignored =read_directory_recursive(dir, dirname, len,1, simplify);1124 dir->flags |= DIR_SHOW_IGNORED;11251126return ignored ? ignore_directory : show_directory;1127}11281129if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1130return show_directory;1131if(!read_directory_recursive(dir, dirname, len,1, simplify))1132return ignore_directory;1133return show_directory;1134}11351136/*1137 * Decide what to do when we find a file while traversing the1138 * filesystem. Mostly two cases:1139 *1140 * 1. We are looking for ignored files1141 * (a) File is ignored, include it1142 * (b) File is in ignored path, include it1143 * (c) File is not ignored, exclude it1144 *1145 * 2. Other scenarios, include the file if not excluded1146 *1147 * Return 1 for exclude, 0 for include.1148 */1149static inttreat_file(struct dir_struct *dir,struct strbuf *path,int exclude,int*dtype)1150{1151struct path_exclude_check check;1152int exclude_file =0;11531154/* Always exclude indexed files */1155if(index_name_exists(&the_index, path->buf, path->len, ignore_case))1156return1;11571158if(exclude)1159 exclude_file = !(dir->flags & DIR_SHOW_IGNORED);1160else if(dir->flags & DIR_SHOW_IGNORED) {1161path_exclude_check_init(&check, dir);11621163if(!is_path_excluded(&check, path->buf, path->len, dtype))1164 exclude_file =1;11651166path_exclude_check_clear(&check);1167}11681169return exclude_file;1170}11711172/*1173 * This is an inexact early pruning of any recursive directory1174 * reading - if the path cannot possibly be in the pathspec,1175 * return true, and we'll skip it early.1176 */1177static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1178{1179if(simplify) {1180for(;;) {1181const char*match = simplify->path;1182int len = simplify->len;11831184if(!match)1185break;1186if(len > pathlen)1187 len = pathlen;1188if(!memcmp(path, match, len))1189return0;1190 simplify++;1191}1192return1;1193}1194return0;1195}11961197/*1198 * This function tells us whether an excluded path matches a1199 * list of "interesting" pathspecs. That is, whether a path matched1200 * by any of the pathspecs could possibly be ignored by excluding1201 * the specified path. This can happen if:1202 *1203 * 1. the path is mentioned explicitly in the pathspec1204 *1205 * 2. the path is a directory prefix of some element in the1206 * pathspec1207 */1208static intexclude_matches_pathspec(const char*path,int len,1209const struct path_simplify *simplify)1210{1211if(simplify) {1212for(; simplify->path; simplify++) {1213if(len == simplify->len1214&& !memcmp(path, simplify->path, len))1215return1;1216if(len < simplify->len1217&& simplify->path[len] =='/'1218&& !memcmp(path, simplify->path, len))1219return1;1220}1221}1222return0;1223}12241225static intget_index_dtype(const char*path,int len)1226{1227int pos;1228struct cache_entry *ce;12291230 ce =cache_name_exists(path, len,0);1231if(ce) {1232if(!ce_uptodate(ce))1233return DT_UNKNOWN;1234if(S_ISGITLINK(ce->ce_mode))1235return DT_DIR;1236/*1237 * Nobody actually cares about the1238 * difference between DT_LNK and DT_REG1239 */1240return DT_REG;1241}12421243/* Try to look it up as a directory */1244 pos =cache_name_pos(path, len);1245if(pos >=0)1246return DT_UNKNOWN;1247 pos = -pos-1;1248while(pos < active_nr) {1249 ce = active_cache[pos++];1250if(strncmp(ce->name, path, len))1251break;1252if(ce->name[len] >'/')1253break;1254if(ce->name[len] <'/')1255continue;1256if(!ce_uptodate(ce))1257break;/* continue? */1258return DT_DIR;1259}1260return DT_UNKNOWN;1261}12621263static intget_dtype(struct dirent *de,const char*path,int len)1264{1265int dtype = de ?DTYPE(de) : DT_UNKNOWN;1266struct stat st;12671268if(dtype != DT_UNKNOWN)1269return dtype;1270 dtype =get_index_dtype(path, len);1271if(dtype != DT_UNKNOWN)1272return dtype;1273if(lstat(path, &st))1274return dtype;1275if(S_ISREG(st.st_mode))1276return DT_REG;1277if(S_ISDIR(st.st_mode))1278return DT_DIR;1279if(S_ISLNK(st.st_mode))1280return DT_LNK;1281return dtype;1282}12831284enum path_treatment {1285 path_ignored,1286 path_handled,1287 path_recurse1288};12891290static enum path_treatment treat_one_path(struct dir_struct *dir,1291struct strbuf *path,1292const struct path_simplify *simplify,1293int dtype,struct dirent *de)1294{1295int exclude =is_excluded(dir, path->buf, &dtype);1296if(exclude && (dir->flags & DIR_COLLECT_IGNORED)1297&&exclude_matches_pathspec(path->buf, path->len, simplify))1298dir_add_ignored(dir, path->buf, path->len);12991300/*1301 * Excluded? If we don't explicitly want to show1302 * ignored files, ignore it1303 */1304if(exclude && !(dir->flags & DIR_SHOW_IGNORED))1305return path_ignored;13061307if(dtype == DT_UNKNOWN)1308 dtype =get_dtype(de, path->buf, path->len);13091310switch(dtype) {1311default:1312return path_ignored;1313case DT_DIR:1314strbuf_addch(path,'/');1315switch(treat_directory(dir, path->buf, path->len, exclude, simplify)) {1316case show_directory:1317break;1318case recurse_into_directory:1319return path_recurse;1320case ignore_directory:1321return path_ignored;1322}1323break;1324case DT_REG:1325case DT_LNK:1326switch(treat_file(dir, path, exclude, &dtype)) {1327case1:1328return path_ignored;1329default:1330break;1331}1332}1333return path_handled;1334}13351336static enum path_treatment treat_path(struct dir_struct *dir,1337struct dirent *de,1338struct strbuf *path,1339int baselen,1340const struct path_simplify *simplify)1341{1342int dtype;13431344if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1345return path_ignored;1346strbuf_setlen(path, baselen);1347strbuf_addstr(path, de->d_name);1348if(simplify_away(path->buf, path->len, simplify))1349return path_ignored;13501351 dtype =DTYPE(de);1352returntreat_one_path(dir, path, simplify, dtype, de);1353}13541355/*1356 * Read a directory tree. We currently ignore anything but1357 * directories, regular files and symlinks. That's because git1358 * doesn't handle them at all yet. Maybe that will change some1359 * day.1360 *1361 * Also, we ignore the name ".git" (even if it is not a directory).1362 * That likely will not change.1363 */1364static intread_directory_recursive(struct dir_struct *dir,1365const char*base,int baselen,1366int check_only,1367const struct path_simplify *simplify)1368{1369DIR*fdir;1370int contents =0;1371struct dirent *de;1372struct strbuf path = STRBUF_INIT;13731374strbuf_add(&path, base, baselen);13751376 fdir =opendir(path.len ? path.buf :".");1377if(!fdir)1378goto out;13791380while((de =readdir(fdir)) != NULL) {1381switch(treat_path(dir, de, &path, baselen, simplify)) {1382case path_recurse:1383 contents +=read_directory_recursive(dir, path.buf,1384 path.len, check_only, simplify);1385continue;1386case path_ignored:1387continue;1388case path_handled:1389break;1390}1391 contents++;1392if(check_only)1393break;1394dir_add_name(dir, path.buf, path.len);1395}1396closedir(fdir);1397 out:1398strbuf_release(&path);13991400return contents;1401}14021403static intcmp_name(const void*p1,const void*p2)1404{1405const struct dir_entry *e1 = *(const struct dir_entry **)p1;1406const struct dir_entry *e2 = *(const struct dir_entry **)p2;14071408returncache_name_compare(e1->name, e1->len,1409 e2->name, e2->len);1410}14111412static struct path_simplify *create_simplify(const char**pathspec)1413{1414int nr, alloc =0;1415struct path_simplify *simplify = NULL;14161417if(!pathspec)1418return NULL;14191420for(nr =0; ; nr++) {1421const char*match;1422if(nr >= alloc) {1423 alloc =alloc_nr(alloc);1424 simplify =xrealloc(simplify, alloc *sizeof(*simplify));1425}1426 match = *pathspec++;1427if(!match)1428break;1429 simplify[nr].path = match;1430 simplify[nr].len =simple_length(match);1431}1432 simplify[nr].path = NULL;1433 simplify[nr].len =0;1434return simplify;1435}14361437static voidfree_simplify(struct path_simplify *simplify)1438{1439free(simplify);1440}14411442static inttreat_leading_path(struct dir_struct *dir,1443const char*path,int len,1444const struct path_simplify *simplify)1445{1446struct strbuf sb = STRBUF_INIT;1447int baselen, rc =0;1448const char*cp;14491450while(len && path[len -1] =='/')1451 len--;1452if(!len)1453return1;1454 baselen =0;1455while(1) {1456 cp = path + baselen + !!baselen;1457 cp =memchr(cp,'/', path + len - cp);1458if(!cp)1459 baselen = len;1460else1461 baselen = cp - path;1462strbuf_setlen(&sb,0);1463strbuf_add(&sb, path, baselen);1464if(!is_directory(sb.buf))1465break;1466if(simplify_away(sb.buf, sb.len, simplify))1467break;1468if(treat_one_path(dir, &sb, simplify,1469 DT_DIR, NULL) == path_ignored)1470break;/* do not recurse into it */1471if(len <= baselen) {1472 rc =1;1473break;/* finished checking */1474}1475}1476strbuf_release(&sb);1477return rc;1478}14791480intread_directory(struct dir_struct *dir,const char*path,int len,const char**pathspec)1481{1482struct path_simplify *simplify;14831484if(has_symlink_leading_path(path, len))1485return dir->nr;14861487 simplify =create_simplify(pathspec);1488if(!len ||treat_leading_path(dir, path, len, simplify))1489read_directory_recursive(dir, path, len,0, simplify);1490free_simplify(simplify);1491qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1492qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1493return dir->nr;1494}14951496intfile_exists(const char*f)1497{1498struct stat sb;1499returnlstat(f, &sb) ==0;1500}15011502/*1503 * Given two normalized paths (a trailing slash is ok), if subdir is1504 * outside dir, return -1. Otherwise return the offset in subdir that1505 * can be used as relative path to dir.1506 */1507intdir_inside_of(const char*subdir,const char*dir)1508{1509int offset =0;15101511assert(dir && subdir && *dir && *subdir);15121513while(*dir && *subdir && *dir == *subdir) {1514 dir++;1515 subdir++;1516 offset++;1517}15181519/* hel[p]/me vs hel[l]/yeah */1520if(*dir && *subdir)1521return-1;15221523if(!*subdir)1524return!*dir ? offset : -1;/* same dir */15251526/* foo/[b]ar vs foo/[] */1527if(is_dir_sep(dir[-1]))1528returnis_dir_sep(subdir[-1]) ? offset : -1;15291530/* foo[/]bar vs foo[] */1531returnis_dir_sep(*subdir) ? offset +1: -1;1532}15331534intis_inside_dir(const char*dir)1535{1536char cwd[PATH_MAX];1537if(!dir)1538return0;1539if(!getcwd(cwd,sizeof(cwd)))1540die_errno("can't find the current directory");1541returndir_inside_of(cwd, dir) >=0;1542}15431544intis_empty_dir(const char*path)1545{1546DIR*dir =opendir(path);1547struct dirent *e;1548int ret =1;15491550if(!dir)1551return0;15521553while((e =readdir(dir)) != NULL)1554if(!is_dot_or_dotdot(e->d_name)) {1555 ret =0;1556break;1557}15581559closedir(dir);1560return ret;1561}15621563static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1564{1565DIR*dir;1566struct dirent *e;1567int ret =0, original_len = path->len, len, kept_down =0;1568int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1569int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1570unsigned char submodule_head[20];15711572if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1573!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1574/* Do not descend and nuke a nested git work tree. */1575if(kept_up)1576*kept_up =1;1577return0;1578}15791580 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1581 dir =opendir(path->buf);1582if(!dir) {1583/* an empty dir could be removed even if it is unreadble */1584if(!keep_toplevel)1585returnrmdir(path->buf);1586else1587return-1;1588}1589if(path->buf[original_len -1] !='/')1590strbuf_addch(path,'/');15911592 len = path->len;1593while((e =readdir(dir)) != NULL) {1594struct stat st;1595if(is_dot_or_dotdot(e->d_name))1596continue;15971598strbuf_setlen(path, len);1599strbuf_addstr(path, e->d_name);1600if(lstat(path->buf, &st))1601;/* fall thru */1602else if(S_ISDIR(st.st_mode)) {1603if(!remove_dir_recurse(path, flag, &kept_down))1604continue;/* happy */1605}else if(!only_empty && !unlink(path->buf))1606continue;/* happy, too */16071608/* path too long, stat fails, or non-directory still exists */1609 ret = -1;1610break;1611}1612closedir(dir);16131614strbuf_setlen(path, original_len);1615if(!ret && !keep_toplevel && !kept_down)1616 ret =rmdir(path->buf);1617else if(kept_up)1618/*1619 * report the uplevel that it is not an error that we1620 * did not rmdir() our directory.1621 */1622*kept_up = !ret;1623return ret;1624}16251626intremove_dir_recursively(struct strbuf *path,int flag)1627{1628returnremove_dir_recurse(path, flag, NULL);1629}16301631voidsetup_standard_excludes(struct dir_struct *dir)1632{1633const char*path;1634char*xdg_path;16351636 dir->exclude_per_dir =".gitignore";1637 path =git_path("info/exclude");1638if(!excludes_file) {1639home_config_paths(NULL, &xdg_path,"ignore");1640 excludes_file = xdg_path;1641}1642if(!access_or_warn(path, R_OK))1643add_excludes_from_file(dir, path);1644if(excludes_file && !access_or_warn(excludes_file, R_OK))1645add_excludes_from_file(dir, excludes_file);1646}16471648intremove_path(const char*name)1649{1650char*slash;16511652if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1653return-1;16541655 slash =strrchr(name,'/');1656if(slash) {1657char*dirs =xstrdup(name);1658 slash = dirs + (slash - name);1659do{1660*slash ='\0';1661}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1662free(dirs);1663}1664return0;1665}16661667static intpathspec_item_cmp(const void*a_,const void*b_)1668{1669struct pathspec_item *a, *b;16701671 a = (struct pathspec_item *)a_;1672 b = (struct pathspec_item *)b_;1673returnstrcmp(a->match, b->match);1674}16751676intinit_pathspec(struct pathspec *pathspec,const char**paths)1677{1678const char**p = paths;1679int i;16801681memset(pathspec,0,sizeof(*pathspec));1682if(!p)1683return0;1684while(*p)1685 p++;1686 pathspec->raw = paths;1687 pathspec->nr = p - paths;1688if(!pathspec->nr)1689return0;16901691 pathspec->items =xmalloc(sizeof(struct pathspec_item)*pathspec->nr);1692for(i =0; i < pathspec->nr; i++) {1693struct pathspec_item *item = pathspec->items+i;1694const char*path = paths[i];16951696 item->match = path;1697 item->len =strlen(path);1698 item->flags =0;1699if(limit_pathspec_to_literal()) {1700 item->nowildcard_len = item->len;1701}else{1702 item->nowildcard_len =simple_length(path);1703if(item->nowildcard_len < item->len) {1704 pathspec->has_wildcard =1;1705if(path[item->nowildcard_len] =='*'&&1706no_wildcard(path + item->nowildcard_len +1))1707 item->flags |= PATHSPEC_ONESTAR;1708}1709}1710}17111712qsort(pathspec->items, pathspec->nr,1713sizeof(struct pathspec_item), pathspec_item_cmp);17141715return0;1716}17171718voidfree_pathspec(struct pathspec *pathspec)1719{1720free(pathspec->items);1721 pathspec->items = NULL;1722}17231724intlimit_pathspec_to_literal(void)1725{1726static int flag = -1;1727if(flag <0)1728 flag =git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT,0);1729return flag;1730}17311732/*1733 * Frees memory within dir which was allocated for exclude lists and1734 * the exclude_stack. Does not free dir itself.1735 */1736voidclear_directory(struct dir_struct *dir)1737{1738int i, j;1739struct exclude_list_group *group;1740struct exclude_list *el;1741struct exclude_stack *stk;17421743for(i = EXC_CMDL; i <= EXC_FILE; i++) {1744 group = &dir->exclude_list_group[i];1745for(j =0; j < group->nr; j++) {1746 el = &group->el[j];1747if(i == EXC_DIRS)1748free((char*)el->src);1749clear_exclude_list(el);1750}1751free(group->el);1752}17531754 stk = dir->exclude_stack;1755while(stk) {1756struct exclude_stack *prev = stk->prev;1757free(stk);1758 stk = prev;1759}1760}