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#include"pathspec.h" 15#include"utf8.h" 16 17struct path_simplify { 18int len; 19const char*path; 20}; 21 22/* 23 * Tells read_directory_recursive how a file or directory should be treated. 24 * Values are ordered by significance, e.g. if a directory contains both 25 * excluded and untracked files, it is listed as untracked because 26 * path_untracked > path_excluded. 27 */ 28enum path_treatment { 29 path_none =0, 30 path_recurse, 31 path_excluded, 32 path_untracked 33}; 34 35static enum path_treatment read_directory_recursive(struct dir_struct *dir, 36const char*path,int len, 37int check_only,const struct path_simplify *simplify); 38static intget_dtype(struct dirent *de,const char*path,int len); 39 40/* helper string functions with support for the ignore_case flag */ 41intstrcmp_icase(const char*a,const char*b) 42{ 43return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 44} 45 46intstrncmp_icase(const char*a,const char*b,size_t count) 47{ 48return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 49} 50 51intfnmatch_icase(const char*pattern,const char*string,int flags) 52{ 53returnwildmatch(pattern, string, 54 flags | (ignore_case ? WM_CASEFOLD :0), 55 NULL); 56} 57 58intgit_fnmatch(const struct pathspec_item *item, 59const char*pattern,const char*string, 60int prefix) 61{ 62if(prefix >0) { 63if(ps_strncmp(item, pattern, string, prefix)) 64return WM_NOMATCH; 65 pattern += prefix; 66 string += prefix; 67} 68if(item->flags & PATHSPEC_ONESTAR) { 69int pattern_len =strlen(++pattern); 70int string_len =strlen(string); 71return string_len < pattern_len || 72ps_strcmp(item, pattern, 73 string + string_len - pattern_len); 74} 75if(item->magic & PATHSPEC_GLOB) 76returnwildmatch(pattern, string, 77 WM_PATHNAME | 78(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0), 79 NULL); 80else 81/* wildmatch has not learned no FNM_PATHNAME mode yet */ 82returnwildmatch(pattern, string, 83 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0, 84 NULL); 85} 86 87static intfnmatch_icase_mem(const char*pattern,int patternlen, 88const char*string,int stringlen, 89int flags) 90{ 91int match_status; 92struct strbuf pat_buf = STRBUF_INIT; 93struct strbuf str_buf = STRBUF_INIT; 94const char*use_pat = pattern; 95const char*use_str = string; 96 97if(pattern[patternlen]) { 98strbuf_add(&pat_buf, pattern, patternlen); 99 use_pat = pat_buf.buf; 100} 101if(string[stringlen]) { 102strbuf_add(&str_buf, string, stringlen); 103 use_str = str_buf.buf; 104} 105 106if(ignore_case) 107 flags |= WM_CASEFOLD; 108 match_status =wildmatch(use_pat, use_str, flags, NULL); 109 110strbuf_release(&pat_buf); 111strbuf_release(&str_buf); 112 113return match_status; 114} 115 116static size_tcommon_prefix_len(const struct pathspec *pathspec) 117{ 118int n; 119size_t max =0; 120 121/* 122 * ":(icase)path" is treated as a pathspec full of 123 * wildcard. In other words, only prefix is considered common 124 * prefix. If the pathspec is abc/foo abc/bar, running in 125 * subdir xyz, the common prefix is still xyz, not xuz/abc as 126 * in non-:(icase). 127 */ 128GUARD_PATHSPEC(pathspec, 129 PATHSPEC_FROMTOP | 130 PATHSPEC_MAXDEPTH | 131 PATHSPEC_LITERAL | 132 PATHSPEC_GLOB | 133 PATHSPEC_ICASE | 134 PATHSPEC_EXCLUDE); 135 136for(n =0; n < pathspec->nr; n++) { 137size_t i =0, len =0, item_len; 138if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 139continue; 140if(pathspec->items[n].magic & PATHSPEC_ICASE) 141 item_len = pathspec->items[n].prefix; 142else 143 item_len = pathspec->items[n].nowildcard_len; 144while(i < item_len && (n ==0|| i < max)) { 145char c = pathspec->items[n].match[i]; 146if(c != pathspec->items[0].match[i]) 147break; 148if(c =='/') 149 len = i +1; 150 i++; 151} 152if(n ==0|| len < max) { 153 max = len; 154if(!max) 155break; 156} 157} 158return max; 159} 160 161/* 162 * Returns a copy of the longest leading path common among all 163 * pathspecs. 164 */ 165char*common_prefix(const struct pathspec *pathspec) 166{ 167unsigned long len =common_prefix_len(pathspec); 168 169return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 170} 171 172intfill_directory(struct dir_struct *dir,const struct pathspec *pathspec) 173{ 174size_t len; 175 176/* 177 * Calculate common prefix for the pathspec, and 178 * use that to optimize the directory walk 179 */ 180 len =common_prefix_len(pathspec); 181 182/* Read the directory and prune it */ 183read_directory(dir, pathspec->nr ? pathspec->_raw[0] :"", len, pathspec); 184return len; 185} 186 187intwithin_depth(const char*name,int namelen, 188int depth,int max_depth) 189{ 190const char*cp = name, *cpe = name + namelen; 191 192while(cp < cpe) { 193if(*cp++ !='/') 194continue; 195 depth++; 196if(depth > max_depth) 197return0; 198} 199return1; 200} 201 202#define DO_MATCH_EXCLUDE 1 203#define DO_MATCH_DIRECTORY 2 204 205/* 206 * Does 'match' match the given name? 207 * A match is found if 208 * 209 * (1) the 'match' string is leading directory of 'name', or 210 * (2) the 'match' string is a wildcard and matches 'name', or 211 * (3) the 'match' string is exactly the same as 'name'. 212 * 213 * and the return value tells which case it was. 214 * 215 * It returns 0 when there is no match. 216 */ 217static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 218const char*name,int namelen,unsigned flags) 219{ 220/* name/namelen has prefix cut off by caller */ 221const char*match = item->match + prefix; 222int matchlen = item->len - prefix; 223 224/* 225 * The normal call pattern is: 226 * 1. prefix = common_prefix_len(ps); 227 * 2. prune something, or fill_directory 228 * 3. match_pathspec() 229 * 230 * 'prefix' at #1 may be shorter than the command's prefix and 231 * it's ok for #2 to match extra files. Those extras will be 232 * trimmed at #3. 233 * 234 * Suppose the pathspec is 'foo' and '../bar' running from 235 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 236 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 237 * user does not want XYZ/foo, only the "foo" part should be 238 * case-insensitive. We need to filter out XYZ/foo here. In 239 * other words, we do not trust the caller on comparing the 240 * prefix part when :(icase) is involved. We do exact 241 * comparison ourselves. 242 * 243 * Normally the caller (common_prefix_len() in fact) does 244 * _exact_ matching on name[-prefix+1..-1] and we do not need 245 * to check that part. Be defensive and check it anyway, in 246 * case common_prefix_len is changed, or a new caller is 247 * introduced that does not use common_prefix_len. 248 * 249 * If the penalty turns out too high when prefix is really 250 * long, maybe change it to 251 * strncmp(match, name, item->prefix - prefix) 252 */ 253if(item->prefix && (item->magic & PATHSPEC_ICASE) && 254strncmp(item->match, name - prefix, item->prefix)) 255return0; 256 257/* If the match was just the prefix, we matched */ 258if(!*match) 259return MATCHED_RECURSIVELY; 260 261if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 262if(matchlen == namelen) 263return MATCHED_EXACTLY; 264 265if(match[matchlen-1] =='/'|| name[matchlen] =='/') 266return MATCHED_RECURSIVELY; 267}else if((flags & DO_MATCH_DIRECTORY) && 268 match[matchlen -1] =='/'&& 269 namelen == matchlen -1&& 270!ps_strncmp(item, match, name, namelen)) 271return MATCHED_EXACTLY; 272 273if(item->nowildcard_len < item->len && 274!git_fnmatch(item, match, name, 275 item->nowildcard_len - prefix)) 276return MATCHED_FNMATCH; 277 278return0; 279} 280 281/* 282 * Given a name and a list of pathspecs, returns the nature of the 283 * closest (i.e. most specific) match of the name to any of the 284 * pathspecs. 285 * 286 * The caller typically calls this multiple times with the same 287 * pathspec and seen[] array but with different name/namelen 288 * (e.g. entries from the index) and is interested in seeing if and 289 * how each pathspec matches all the names it calls this function 290 * with. A mark is left in the seen[] array for each pathspec element 291 * indicating the closest type of match that element achieved, so if 292 * seen[n] remains zero after multiple invocations, that means the nth 293 * pathspec did not match any names, which could indicate that the 294 * user mistyped the nth pathspec. 295 */ 296static intdo_match_pathspec(const struct pathspec *ps, 297const char*name,int namelen, 298int prefix,char*seen, 299unsigned flags) 300{ 301int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 302 303GUARD_PATHSPEC(ps, 304 PATHSPEC_FROMTOP | 305 PATHSPEC_MAXDEPTH | 306 PATHSPEC_LITERAL | 307 PATHSPEC_GLOB | 308 PATHSPEC_ICASE | 309 PATHSPEC_EXCLUDE); 310 311if(!ps->nr) { 312if(!ps->recursive || 313!(ps->magic & PATHSPEC_MAXDEPTH) || 314 ps->max_depth == -1) 315return MATCHED_RECURSIVELY; 316 317if(within_depth(name, namelen,0, ps->max_depth)) 318return MATCHED_EXACTLY; 319else 320return0; 321} 322 323 name += prefix; 324 namelen -= prefix; 325 326for(i = ps->nr -1; i >=0; i--) { 327int how; 328 329if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 330( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 331continue; 332 333if(seen && seen[i] == MATCHED_EXACTLY) 334continue; 335/* 336 * Make exclude patterns optional and never report 337 * "pathspec ':(exclude)foo' matches no files" 338 */ 339if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 340 seen[i] = MATCHED_FNMATCH; 341 how =match_pathspec_item(ps->items+i, prefix, name, 342 namelen, flags); 343if(ps->recursive && 344(ps->magic & PATHSPEC_MAXDEPTH) && 345 ps->max_depth != -1&& 346 how && how != MATCHED_FNMATCH) { 347int len = ps->items[i].len; 348if(name[len] =='/') 349 len++; 350if(within_depth(name+len, namelen-len,0, ps->max_depth)) 351 how = MATCHED_EXACTLY; 352else 353 how =0; 354} 355if(how) { 356if(retval < how) 357 retval = how; 358if(seen && seen[i] < how) 359 seen[i] = how; 360} 361} 362return retval; 363} 364 365intmatch_pathspec(const struct pathspec *ps, 366const char*name,int namelen, 367int prefix,char*seen,int is_dir) 368{ 369int positive, negative; 370unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 371 positive =do_match_pathspec(ps, name, namelen, 372 prefix, seen, flags); 373if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 374return positive; 375 negative =do_match_pathspec(ps, name, namelen, 376 prefix, seen, 377 flags | DO_MATCH_EXCLUDE); 378return negative ?0: positive; 379} 380 381intreport_path_error(const char*ps_matched, 382const struct pathspec *pathspec, 383const char*prefix) 384{ 385/* 386 * Make sure all pathspec matched; otherwise it is an error. 387 */ 388struct strbuf sb = STRBUF_INIT; 389int num, errors =0; 390for(num =0; num < pathspec->nr; num++) { 391int other, found_dup; 392 393if(ps_matched[num]) 394continue; 395/* 396 * The caller might have fed identical pathspec 397 * twice. Do not barf on such a mistake. 398 * FIXME: parse_pathspec should have eliminated 399 * duplicate pathspec. 400 */ 401for(found_dup = other =0; 402!found_dup && other < pathspec->nr; 403 other++) { 404if(other == num || !ps_matched[other]) 405continue; 406if(!strcmp(pathspec->items[other].original, 407 pathspec->items[num].original)) 408/* 409 * Ok, we have a match already. 410 */ 411 found_dup =1; 412} 413if(found_dup) 414continue; 415 416error("pathspec '%s' did not match any file(s) known to git.", 417 pathspec->items[num].original); 418 errors++; 419} 420strbuf_release(&sb); 421return errors; 422} 423 424/* 425 * Return the length of the "simple" part of a path match limiter. 426 */ 427intsimple_length(const char*match) 428{ 429int len = -1; 430 431for(;;) { 432unsigned char c = *match++; 433 len++; 434if(c =='\0'||is_glob_special(c)) 435return len; 436} 437} 438 439intno_wildcard(const char*string) 440{ 441return string[simple_length(string)] =='\0'; 442} 443 444voidparse_exclude_pattern(const char**pattern, 445int*patternlen, 446int*flags, 447int*nowildcardlen) 448{ 449const char*p = *pattern; 450size_t i, len; 451 452*flags =0; 453if(*p =='!') { 454*flags |= EXC_FLAG_NEGATIVE; 455 p++; 456} 457 len =strlen(p); 458if(len && p[len -1] =='/') { 459 len--; 460*flags |= EXC_FLAG_MUSTBEDIR; 461} 462for(i =0; i < len; i++) { 463if(p[i] =='/') 464break; 465} 466if(i == len) 467*flags |= EXC_FLAG_NODIR; 468*nowildcardlen =simple_length(p); 469/* 470 * we should have excluded the trailing slash from 'p' too, 471 * but that's one more allocation. Instead just make sure 472 * nowildcardlen does not exceed real patternlen 473 */ 474if(*nowildcardlen > len) 475*nowildcardlen = len; 476if(*p =='*'&&no_wildcard(p +1)) 477*flags |= EXC_FLAG_ENDSWITH; 478*pattern = p; 479*patternlen = len; 480} 481 482voidadd_exclude(const char*string,const char*base, 483int baselen,struct exclude_list *el,int srcpos) 484{ 485struct exclude *x; 486int patternlen; 487int flags; 488int nowildcardlen; 489 490parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 491if(flags & EXC_FLAG_MUSTBEDIR) { 492char*s; 493 x =xmalloc(sizeof(*x) + patternlen +1); 494 s = (char*)(x+1); 495memcpy(s, string, patternlen); 496 s[patternlen] ='\0'; 497 x->pattern = s; 498}else{ 499 x =xmalloc(sizeof(*x)); 500 x->pattern = string; 501} 502 x->patternlen = patternlen; 503 x->nowildcardlen = nowildcardlen; 504 x->base = base; 505 x->baselen = baselen; 506 x->flags = flags; 507 x->srcpos = srcpos; 508ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 509 el->excludes[el->nr++] = x; 510 x->el = el; 511} 512 513static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 514{ 515int pos, len; 516unsigned long sz; 517enum object_type type; 518void*data; 519 520 len =strlen(path); 521 pos =cache_name_pos(path, len); 522if(pos <0) 523return NULL; 524if(!ce_skip_worktree(active_cache[pos])) 525return NULL; 526 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 527if(!data || type != OBJ_BLOB) { 528free(data); 529return NULL; 530} 531*size =xsize_t(sz); 532return data; 533} 534 535/* 536 * Frees memory within el which was allocated for exclude patterns and 537 * the file buffer. Does not free el itself. 538 */ 539voidclear_exclude_list(struct exclude_list *el) 540{ 541int i; 542 543for(i =0; i < el->nr; i++) 544free(el->excludes[i]); 545free(el->excludes); 546free(el->filebuf); 547 548 el->nr =0; 549 el->excludes = NULL; 550 el->filebuf = NULL; 551} 552 553static voidtrim_trailing_spaces(char*buf) 554{ 555char*p, *last_space = NULL; 556 557for(p = buf; *p; p++) 558switch(*p) { 559case' ': 560if(!last_space) 561 last_space = p; 562break; 563case'\\': 564 p++; 565if(!*p) 566return; 567/* fallthrough */ 568default: 569 last_space = NULL; 570} 571 572if(last_space) 573*last_space ='\0'; 574} 575 576intadd_excludes_from_file_to_list(const char*fname, 577const char*base, 578int baselen, 579struct exclude_list *el, 580int check_index) 581{ 582struct stat st; 583int fd, i, lineno =1; 584size_t size =0; 585char*buf, *entry; 586 587 fd =open(fname, O_RDONLY); 588if(fd <0||fstat(fd, &st) <0) { 589if(errno != ENOENT) 590warn_on_inaccessible(fname); 591if(0<= fd) 592close(fd); 593if(!check_index || 594(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 595return-1; 596if(size ==0) { 597free(buf); 598return0; 599} 600if(buf[size-1] !='\n') { 601 buf =xrealloc(buf, size+1); 602 buf[size++] ='\n'; 603} 604}else{ 605 size =xsize_t(st.st_size); 606if(size ==0) { 607close(fd); 608return0; 609} 610 buf =xmalloc(size+1); 611if(read_in_full(fd, buf, size) != size) { 612free(buf); 613close(fd); 614return-1; 615} 616 buf[size++] ='\n'; 617close(fd); 618} 619 620 el->filebuf = buf; 621 622if(skip_utf8_bom(&buf, size)) 623 size -= buf - el->filebuf; 624 625 entry = buf; 626 627for(i =0; i < size; i++) { 628if(buf[i] =='\n') { 629if(entry != buf + i && entry[0] !='#') { 630 buf[i - (i && buf[i-1] =='\r')] =0; 631trim_trailing_spaces(entry); 632add_exclude(entry, base, baselen, el, lineno); 633} 634 lineno++; 635 entry = buf + i +1; 636} 637} 638return0; 639} 640 641struct exclude_list *add_exclude_list(struct dir_struct *dir, 642int group_type,const char*src) 643{ 644struct exclude_list *el; 645struct exclude_list_group *group; 646 647 group = &dir->exclude_list_group[group_type]; 648ALLOC_GROW(group->el, group->nr +1, group->alloc); 649 el = &group->el[group->nr++]; 650memset(el,0,sizeof(*el)); 651 el->src = src; 652return el; 653} 654 655/* 656 * Used to set up core.excludesfile and .git/info/exclude lists. 657 */ 658voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 659{ 660struct exclude_list *el; 661 el =add_exclude_list(dir, EXC_FILE, fname); 662if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 663die("cannot use%sas an exclude file", fname); 664} 665 666intmatch_basename(const char*basename,int basenamelen, 667const char*pattern,int prefix,int patternlen, 668int flags) 669{ 670if(prefix == patternlen) { 671if(patternlen == basenamelen && 672!strncmp_icase(pattern, basename, basenamelen)) 673return1; 674}else if(flags & EXC_FLAG_ENDSWITH) { 675/* "*literal" matching against "fooliteral" */ 676if(patternlen -1<= basenamelen && 677!strncmp_icase(pattern +1, 678 basename + basenamelen - (patternlen -1), 679 patternlen -1)) 680return1; 681}else{ 682if(fnmatch_icase_mem(pattern, patternlen, 683 basename, basenamelen, 6840) ==0) 685return1; 686} 687return0; 688} 689 690intmatch_pathname(const char*pathname,int pathlen, 691const char*base,int baselen, 692const char*pattern,int prefix,int patternlen, 693int flags) 694{ 695const char*name; 696int namelen; 697 698/* 699 * match with FNM_PATHNAME; the pattern has base implicitly 700 * in front of it. 701 */ 702if(*pattern =='/') { 703 pattern++; 704 patternlen--; 705 prefix--; 706} 707 708/* 709 * baselen does not count the trailing slash. base[] may or 710 * may not end with a trailing slash though. 711 */ 712if(pathlen < baselen +1|| 713(baselen && pathname[baselen] !='/') || 714strncmp_icase(pathname, base, baselen)) 715return0; 716 717 namelen = baselen ? pathlen - baselen -1: pathlen; 718 name = pathname + pathlen - namelen; 719 720if(prefix) { 721/* 722 * if the non-wildcard part is longer than the 723 * remaining pathname, surely it cannot match. 724 */ 725if(prefix > namelen) 726return0; 727 728if(strncmp_icase(pattern, name, prefix)) 729return0; 730 pattern += prefix; 731 patternlen -= prefix; 732 name += prefix; 733 namelen -= prefix; 734 735/* 736 * If the whole pattern did not have a wildcard, 737 * then our prefix match is all we need; we 738 * do not need to call fnmatch at all. 739 */ 740if(!patternlen && !namelen) 741return1; 742} 743 744returnfnmatch_icase_mem(pattern, patternlen, 745 name, namelen, 746 WM_PATHNAME) ==0; 747} 748 749/* 750 * Scan the given exclude list in reverse to see whether pathname 751 * should be ignored. The first match (i.e. the last on the list), if 752 * any, determines the fate. Returns the exclude_list element which 753 * matched, or NULL for undecided. 754 */ 755static struct exclude *last_exclude_matching_from_list(const char*pathname, 756int pathlen, 757const char*basename, 758int*dtype, 759struct exclude_list *el) 760{ 761int i; 762 763if(!el->nr) 764return NULL;/* undefined */ 765 766for(i = el->nr -1;0<= i; i--) { 767struct exclude *x = el->excludes[i]; 768const char*exclude = x->pattern; 769int prefix = x->nowildcardlen; 770 771if(x->flags & EXC_FLAG_MUSTBEDIR) { 772if(*dtype == DT_UNKNOWN) 773*dtype =get_dtype(NULL, pathname, pathlen); 774if(*dtype != DT_DIR) 775continue; 776} 777 778if(x->flags & EXC_FLAG_NODIR) { 779if(match_basename(basename, 780 pathlen - (basename - pathname), 781 exclude, prefix, x->patternlen, 782 x->flags)) 783return x; 784continue; 785} 786 787assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 788if(match_pathname(pathname, pathlen, 789 x->base, x->baselen ? x->baselen -1:0, 790 exclude, prefix, x->patternlen, x->flags)) 791return x; 792} 793return NULL;/* undecided */ 794} 795 796/* 797 * Scan the list and let the last match determine the fate. 798 * Return 1 for exclude, 0 for include and -1 for undecided. 799 */ 800intis_excluded_from_list(const char*pathname, 801int pathlen,const char*basename,int*dtype, 802struct exclude_list *el) 803{ 804struct exclude *exclude; 805 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 806if(exclude) 807return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 808return-1;/* undecided */ 809} 810 811static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 812const char*pathname,int pathlen,const char*basename, 813int*dtype_p) 814{ 815int i, j; 816struct exclude_list_group *group; 817struct exclude *exclude; 818for(i = EXC_CMDL; i <= EXC_FILE; i++) { 819 group = &dir->exclude_list_group[i]; 820for(j = group->nr -1; j >=0; j--) { 821 exclude =last_exclude_matching_from_list( 822 pathname, pathlen, basename, dtype_p, 823&group->el[j]); 824if(exclude) 825return exclude; 826} 827} 828return NULL; 829} 830 831/* 832 * Loads the per-directory exclude list for the substring of base 833 * which has a char length of baselen. 834 */ 835static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 836{ 837struct exclude_list_group *group; 838struct exclude_list *el; 839struct exclude_stack *stk = NULL; 840int current; 841 842 group = &dir->exclude_list_group[EXC_DIRS]; 843 844/* 845 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 846 * which originate from directories not in the prefix of the 847 * path being checked. 848 */ 849while((stk = dir->exclude_stack) != NULL) { 850if(stk->baselen <= baselen && 851!strncmp(dir->basebuf.buf, base, stk->baselen)) 852break; 853 el = &group->el[dir->exclude_stack->exclude_ix]; 854 dir->exclude_stack = stk->prev; 855 dir->exclude = NULL; 856free((char*)el->src);/* see strbuf_detach() below */ 857clear_exclude_list(el); 858free(stk); 859 group->nr--; 860} 861 862/* Skip traversing into sub directories if the parent is excluded */ 863if(dir->exclude) 864return; 865 866/* 867 * Lazy initialization. All call sites currently just 868 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 869 * them seems lots of work for little benefit. 870 */ 871if(!dir->basebuf.buf) 872strbuf_init(&dir->basebuf, PATH_MAX); 873 874/* Read from the parent directories and push them down. */ 875 current = stk ? stk->baselen : -1; 876strbuf_setlen(&dir->basebuf, current <0?0: current); 877while(current < baselen) { 878const char*cp; 879 880 stk =xcalloc(1,sizeof(*stk)); 881if(current <0) { 882 cp = base; 883 current =0; 884}else{ 885 cp =strchr(base + current +1,'/'); 886if(!cp) 887die("oops in prep_exclude"); 888 cp++; 889} 890 stk->prev = dir->exclude_stack; 891 stk->baselen = cp - base; 892 stk->exclude_ix = group->nr; 893 el =add_exclude_list(dir, EXC_DIRS, NULL); 894strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 895assert(stk->baselen == dir->basebuf.len); 896 897/* Abort if the directory is excluded */ 898if(stk->baselen) { 899int dt = DT_DIR; 900 dir->basebuf.buf[stk->baselen -1] =0; 901 dir->exclude =last_exclude_matching_from_lists(dir, 902 dir->basebuf.buf, stk->baselen -1, 903 dir->basebuf.buf + current, &dt); 904 dir->basebuf.buf[stk->baselen -1] ='/'; 905if(dir->exclude && 906 dir->exclude->flags & EXC_FLAG_NEGATIVE) 907 dir->exclude = NULL; 908if(dir->exclude) { 909 dir->exclude_stack = stk; 910return; 911} 912} 913 914/* Try to read per-directory file */ 915if(dir->exclude_per_dir) { 916/* 917 * dir->basebuf gets reused by the traversal, but we 918 * need fname to remain unchanged to ensure the src 919 * member of each struct exclude correctly 920 * back-references its source file. Other invocations 921 * of add_exclude_list provide stable strings, so we 922 * strbuf_detach() and free() here in the caller. 923 */ 924struct strbuf sb = STRBUF_INIT; 925strbuf_addbuf(&sb, &dir->basebuf); 926strbuf_addstr(&sb, dir->exclude_per_dir); 927 el->src =strbuf_detach(&sb, NULL); 928add_excludes_from_file_to_list(el->src, el->src, 929 stk->baselen, el,1); 930} 931 dir->exclude_stack = stk; 932 current = stk->baselen; 933} 934strbuf_setlen(&dir->basebuf, baselen); 935} 936 937/* 938 * Loads the exclude lists for the directory containing pathname, then 939 * scans all exclude lists to determine whether pathname is excluded. 940 * Returns the exclude_list element which matched, or NULL for 941 * undecided. 942 */ 943struct exclude *last_exclude_matching(struct dir_struct *dir, 944const char*pathname, 945int*dtype_p) 946{ 947int pathlen =strlen(pathname); 948const char*basename =strrchr(pathname,'/'); 949 basename = (basename) ? basename+1: pathname; 950 951prep_exclude(dir, pathname, basename-pathname); 952 953if(dir->exclude) 954return dir->exclude; 955 956returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 957 basename, dtype_p); 958} 959 960/* 961 * Loads the exclude lists for the directory containing pathname, then 962 * scans all exclude lists to determine whether pathname is excluded. 963 * Returns 1 if true, otherwise 0. 964 */ 965intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 966{ 967struct exclude *exclude = 968last_exclude_matching(dir, pathname, dtype_p); 969if(exclude) 970return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 971return0; 972} 973 974static struct dir_entry *dir_entry_new(const char*pathname,int len) 975{ 976struct dir_entry *ent; 977 978 ent =xmalloc(sizeof(*ent) + len +1); 979 ent->len = len; 980memcpy(ent->name, pathname, len); 981 ent->name[len] =0; 982return ent; 983} 984 985static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 986{ 987if(cache_file_exists(pathname, len, ignore_case)) 988return NULL; 989 990ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 991return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 992} 993 994struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 995{ 996if(!cache_name_is_other(pathname, len)) 997return NULL; 998 999ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1000return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1001}10021003enum exist_status {1004 index_nonexistent =0,1005 index_directory,1006 index_gitdir1007};10081009/*1010 * Do not use the alphabetically sorted index to look up1011 * the directory name; instead, use the case insensitive1012 * directory hash.1013 */1014static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1015{1016const struct cache_entry *ce =cache_dir_exists(dirname, len);1017unsigned char endchar;10181019if(!ce)1020return index_nonexistent;1021 endchar = ce->name[len];10221023/*1024 * The cache_entry structure returned will contain this dirname1025 * and possibly additional path components.1026 */1027if(endchar =='/')1028return index_directory;10291030/*1031 * If there are no additional path components, then this cache_entry1032 * represents a submodule. Submodules, despite being directories,1033 * are stored in the cache without a closing slash.1034 */1035if(!endchar &&S_ISGITLINK(ce->ce_mode))1036return index_gitdir;10371038/* This should never be hit, but it exists just in case. */1039return index_nonexistent;1040}10411042/*1043 * The index sorts alphabetically by entry name, which1044 * means that a gitlink sorts as '\0' at the end, while1045 * a directory (which is defined not as an entry, but as1046 * the files it contains) will sort with the '/' at the1047 * end.1048 */1049static enum exist_status directory_exists_in_index(const char*dirname,int len)1050{1051int pos;10521053if(ignore_case)1054returndirectory_exists_in_index_icase(dirname, len);10551056 pos =cache_name_pos(dirname, len);1057if(pos <0)1058 pos = -pos-1;1059while(pos < active_nr) {1060const struct cache_entry *ce = active_cache[pos++];1061unsigned char endchar;10621063if(strncmp(ce->name, dirname, len))1064break;1065 endchar = ce->name[len];1066if(endchar >'/')1067break;1068if(endchar =='/')1069return index_directory;1070if(!endchar &&S_ISGITLINK(ce->ce_mode))1071return index_gitdir;1072}1073return index_nonexistent;1074}10751076/*1077 * When we find a directory when traversing the filesystem, we1078 * have three distinct cases:1079 *1080 * - ignore it1081 * - see it as a directory1082 * - recurse into it1083 *1084 * and which one we choose depends on a combination of existing1085 * git index contents and the flags passed into the directory1086 * traversal routine.1087 *1088 * Case 1: If we *already* have entries in the index under that1089 * directory name, we always recurse into the directory to see1090 * all the files.1091 *1092 * Case 2: If we *already* have that directory name as a gitlink,1093 * we always continue to see it as a gitlink, regardless of whether1094 * there is an actual git directory there or not (it might not1095 * be checked out as a subproject!)1096 *1097 * Case 3: if we didn't have it in the index previously, we1098 * have a few sub-cases:1099 *1100 * (a) if "show_other_directories" is true, we show it as1101 * just a directory, unless "hide_empty_directories" is1102 * also true, in which case we need to check if it contains any1103 * untracked and / or ignored files.1104 * (b) if it looks like a git directory, and we don't have1105 * 'no_gitlinks' set we treat it as a gitlink, and show it1106 * as a directory.1107 * (c) otherwise, we recurse into it.1108 */1109static enum path_treatment treat_directory(struct dir_struct *dir,1110const char*dirname,int len,int exclude,1111const struct path_simplify *simplify)1112{1113/* The "len-1" is to strip the final '/' */1114switch(directory_exists_in_index(dirname, len-1)) {1115case index_directory:1116return path_recurse;11171118case index_gitdir:1119return path_none;11201121case index_nonexistent:1122if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1123break;1124if(!(dir->flags & DIR_NO_GITLINKS)) {1125unsigned char sha1[20];1126if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1127return path_untracked;1128}1129return path_recurse;1130}11311132/* This is the "show_other_directories" case */11331134if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1135return exclude ? path_excluded : path_untracked;11361137returnread_directory_recursive(dir, dirname, len,1, simplify);1138}11391140/*1141 * This is an inexact early pruning of any recursive directory1142 * reading - if the path cannot possibly be in the pathspec,1143 * return true, and we'll skip it early.1144 */1145static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1146{1147if(simplify) {1148for(;;) {1149const char*match = simplify->path;1150int len = simplify->len;11511152if(!match)1153break;1154if(len > pathlen)1155 len = pathlen;1156if(!memcmp(path, match, len))1157return0;1158 simplify++;1159}1160return1;1161}1162return0;1163}11641165/*1166 * This function tells us whether an excluded path matches a1167 * list of "interesting" pathspecs. That is, whether a path matched1168 * by any of the pathspecs could possibly be ignored by excluding1169 * the specified path. This can happen if:1170 *1171 * 1. the path is mentioned explicitly in the pathspec1172 *1173 * 2. the path is a directory prefix of some element in the1174 * pathspec1175 */1176static intexclude_matches_pathspec(const char*path,int len,1177const struct path_simplify *simplify)1178{1179if(simplify) {1180for(; simplify->path; simplify++) {1181if(len == simplify->len1182&& !memcmp(path, simplify->path, len))1183return1;1184if(len < simplify->len1185&& simplify->path[len] =='/'1186&& !memcmp(path, simplify->path, len))1187return1;1188}1189}1190return0;1191}11921193static intget_index_dtype(const char*path,int len)1194{1195int pos;1196const struct cache_entry *ce;11971198 ce =cache_file_exists(path, len,0);1199if(ce) {1200if(!ce_uptodate(ce))1201return DT_UNKNOWN;1202if(S_ISGITLINK(ce->ce_mode))1203return DT_DIR;1204/*1205 * Nobody actually cares about the1206 * difference between DT_LNK and DT_REG1207 */1208return DT_REG;1209}12101211/* Try to look it up as a directory */1212 pos =cache_name_pos(path, len);1213if(pos >=0)1214return DT_UNKNOWN;1215 pos = -pos-1;1216while(pos < active_nr) {1217 ce = active_cache[pos++];1218if(strncmp(ce->name, path, len))1219break;1220if(ce->name[len] >'/')1221break;1222if(ce->name[len] <'/')1223continue;1224if(!ce_uptodate(ce))1225break;/* continue? */1226return DT_DIR;1227}1228return DT_UNKNOWN;1229}12301231static intget_dtype(struct dirent *de,const char*path,int len)1232{1233int dtype = de ?DTYPE(de) : DT_UNKNOWN;1234struct stat st;12351236if(dtype != DT_UNKNOWN)1237return dtype;1238 dtype =get_index_dtype(path, len);1239if(dtype != DT_UNKNOWN)1240return dtype;1241if(lstat(path, &st))1242return dtype;1243if(S_ISREG(st.st_mode))1244return DT_REG;1245if(S_ISDIR(st.st_mode))1246return DT_DIR;1247if(S_ISLNK(st.st_mode))1248return DT_LNK;1249return dtype;1250}12511252static enum path_treatment treat_one_path(struct dir_struct *dir,1253struct strbuf *path,1254const struct path_simplify *simplify,1255int dtype,struct dirent *de)1256{1257int exclude;1258int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);12591260if(dtype == DT_UNKNOWN)1261 dtype =get_dtype(de, path->buf, path->len);12621263/* Always exclude indexed files */1264if(dtype != DT_DIR && has_path_in_index)1265return path_none;12661267/*1268 * When we are looking at a directory P in the working tree,1269 * there are three cases:1270 *1271 * (1) P exists in the index. Everything inside the directory P in1272 * the working tree needs to go when P is checked out from the1273 * index.1274 *1275 * (2) P does not exist in the index, but there is P/Q in the index.1276 * We know P will stay a directory when we check out the contents1277 * of the index, but we do not know yet if there is a directory1278 * P/Q in the working tree to be killed, so we need to recurse.1279 *1280 * (3) P does not exist in the index, and there is no P/Q in the index1281 * to require P to be a directory, either. Only in this case, we1282 * know that everything inside P will not be killed without1283 * recursing.1284 */1285if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1286(dtype == DT_DIR) &&1287!has_path_in_index &&1288(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1289return path_none;12901291 exclude =is_excluded(dir, path->buf, &dtype);12921293/*1294 * Excluded? If we don't explicitly want to show1295 * ignored files, ignore it1296 */1297if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1298return path_excluded;12991300switch(dtype) {1301default:1302return path_none;1303case DT_DIR:1304strbuf_addch(path,'/');1305returntreat_directory(dir, path->buf, path->len, exclude,1306 simplify);1307case DT_REG:1308case DT_LNK:1309return exclude ? path_excluded : path_untracked;1310}1311}13121313static enum path_treatment treat_path(struct dir_struct *dir,1314struct dirent *de,1315struct strbuf *path,1316int baselen,1317const struct path_simplify *simplify)1318{1319int dtype;13201321if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1322return path_none;1323strbuf_setlen(path, baselen);1324strbuf_addstr(path, de->d_name);1325if(simplify_away(path->buf, path->len, simplify))1326return path_none;13271328 dtype =DTYPE(de);1329returntreat_one_path(dir, path, simplify, dtype, de);1330}13311332/*1333 * Read a directory tree. We currently ignore anything but1334 * directories, regular files and symlinks. That's because git1335 * doesn't handle them at all yet. Maybe that will change some1336 * day.1337 *1338 * Also, we ignore the name ".git" (even if it is not a directory).1339 * That likely will not change.1340 *1341 * Returns the most significant path_treatment value encountered in the scan.1342 */1343static enum path_treatment read_directory_recursive(struct dir_struct *dir,1344const char*base,int baselen,1345int check_only,1346const struct path_simplify *simplify)1347{1348DIR*fdir;1349enum path_treatment state, subdir_state, dir_state = path_none;1350struct dirent *de;1351struct strbuf path = STRBUF_INIT;13521353strbuf_add(&path, base, baselen);13541355 fdir =opendir(path.len ? path.buf :".");1356if(!fdir)1357goto out;13581359while((de =readdir(fdir)) != NULL) {1360/* check how the file or directory should be treated */1361 state =treat_path(dir, de, &path, baselen, simplify);1362if(state > dir_state)1363 dir_state = state;13641365/* recurse into subdir if instructed by treat_path */1366if(state == path_recurse) {1367 subdir_state =read_directory_recursive(dir, path.buf,1368 path.len, check_only, simplify);1369if(subdir_state > dir_state)1370 dir_state = subdir_state;1371}13721373if(check_only) {1374/* abort early if maximum state has been reached */1375if(dir_state == path_untracked)1376break;1377/* skip the dir_add_* part */1378continue;1379}13801381/* add the path to the appropriate result list */1382switch(state) {1383case path_excluded:1384if(dir->flags & DIR_SHOW_IGNORED)1385dir_add_name(dir, path.buf, path.len);1386else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1387((dir->flags & DIR_COLLECT_IGNORED) &&1388exclude_matches_pathspec(path.buf, path.len,1389 simplify)))1390dir_add_ignored(dir, path.buf, path.len);1391break;13921393case path_untracked:1394if(!(dir->flags & DIR_SHOW_IGNORED))1395dir_add_name(dir, path.buf, path.len);1396break;13971398default:1399break;1400}1401}1402closedir(fdir);1403 out:1404strbuf_release(&path);14051406return dir_state;1407}14081409static intcmp_name(const void*p1,const void*p2)1410{1411const struct dir_entry *e1 = *(const struct dir_entry **)p1;1412const struct dir_entry *e2 = *(const struct dir_entry **)p2;14131414returnname_compare(e1->name, e1->len, e2->name, e2->len);1415}14161417static struct path_simplify *create_simplify(const char**pathspec)1418{1419int nr, alloc =0;1420struct path_simplify *simplify = NULL;14211422if(!pathspec)1423return NULL;14241425for(nr =0; ; nr++) {1426const char*match;1427ALLOC_GROW(simplify, nr +1, alloc);1428 match = *pathspec++;1429if(!match)1430break;1431 simplify[nr].path = match;1432 simplify[nr].len =simple_length(match);1433}1434 simplify[nr].path = NULL;1435 simplify[nr].len =0;1436return simplify;1437}14381439static voidfree_simplify(struct path_simplify *simplify)1440{1441free(simplify);1442}14431444static inttreat_leading_path(struct dir_struct *dir,1445const char*path,int len,1446const struct path_simplify *simplify)1447{1448struct strbuf sb = STRBUF_INIT;1449int baselen, rc =0;1450const char*cp;1451int old_flags = dir->flags;14521453while(len && path[len -1] =='/')1454 len--;1455if(!len)1456return1;1457 baselen =0;1458 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1459while(1) {1460 cp = path + baselen + !!baselen;1461 cp =memchr(cp,'/', path + len - cp);1462if(!cp)1463 baselen = len;1464else1465 baselen = cp - path;1466strbuf_setlen(&sb,0);1467strbuf_add(&sb, path, baselen);1468if(!is_directory(sb.buf))1469break;1470if(simplify_away(sb.buf, sb.len, simplify))1471break;1472if(treat_one_path(dir, &sb, simplify,1473 DT_DIR, NULL) == path_none)1474break;/* do not recurse into it */1475if(len <= baselen) {1476 rc =1;1477break;/* finished checking */1478}1479}1480strbuf_release(&sb);1481 dir->flags = old_flags;1482return rc;1483}14841485intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1486{1487struct path_simplify *simplify;14881489/*1490 * Check out create_simplify()1491 */1492if(pathspec)1493GUARD_PATHSPEC(pathspec,1494 PATHSPEC_FROMTOP |1495 PATHSPEC_MAXDEPTH |1496 PATHSPEC_LITERAL |1497 PATHSPEC_GLOB |1498 PATHSPEC_ICASE |1499 PATHSPEC_EXCLUDE);15001501if(has_symlink_leading_path(path, len))1502return dir->nr;15031504/*1505 * exclude patterns are treated like positive ones in1506 * create_simplify. Usually exclude patterns should be a1507 * subset of positive ones, which has no impacts on1508 * create_simplify().1509 */1510 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1511if(!len ||treat_leading_path(dir, path, len, simplify))1512read_directory_recursive(dir, path, len,0, simplify);1513free_simplify(simplify);1514qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1515qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1516return dir->nr;1517}15181519intfile_exists(const char*f)1520{1521struct stat sb;1522returnlstat(f, &sb) ==0;1523}15241525/*1526 * Given two normalized paths (a trailing slash is ok), if subdir is1527 * outside dir, return -1. Otherwise return the offset in subdir that1528 * can be used as relative path to dir.1529 */1530intdir_inside_of(const char*subdir,const char*dir)1531{1532int offset =0;15331534assert(dir && subdir && *dir && *subdir);15351536while(*dir && *subdir && *dir == *subdir) {1537 dir++;1538 subdir++;1539 offset++;1540}15411542/* hel[p]/me vs hel[l]/yeah */1543if(*dir && *subdir)1544return-1;15451546if(!*subdir)1547return!*dir ? offset : -1;/* same dir */15481549/* foo/[b]ar vs foo/[] */1550if(is_dir_sep(dir[-1]))1551returnis_dir_sep(subdir[-1]) ? offset : -1;15521553/* foo[/]bar vs foo[] */1554returnis_dir_sep(*subdir) ? offset +1: -1;1555}15561557intis_inside_dir(const char*dir)1558{1559char*cwd;1560int rc;15611562if(!dir)1563return0;15641565 cwd =xgetcwd();1566 rc = (dir_inside_of(cwd, dir) >=0);1567free(cwd);1568return rc;1569}15701571intis_empty_dir(const char*path)1572{1573DIR*dir =opendir(path);1574struct dirent *e;1575int ret =1;15761577if(!dir)1578return0;15791580while((e =readdir(dir)) != NULL)1581if(!is_dot_or_dotdot(e->d_name)) {1582 ret =0;1583break;1584}15851586closedir(dir);1587return ret;1588}15891590static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1591{1592DIR*dir;1593struct dirent *e;1594int ret =0, original_len = path->len, len, kept_down =0;1595int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1596int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1597unsigned char submodule_head[20];15981599if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1600!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1601/* Do not descend and nuke a nested git work tree. */1602if(kept_up)1603*kept_up =1;1604return0;1605}16061607 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1608 dir =opendir(path->buf);1609if(!dir) {1610if(errno == ENOENT)1611return keep_toplevel ? -1:0;1612else if(errno == EACCES && !keep_toplevel)1613/*1614 * An empty dir could be removable even if it1615 * is unreadable:1616 */1617returnrmdir(path->buf);1618else1619return-1;1620}1621if(path->buf[original_len -1] !='/')1622strbuf_addch(path,'/');16231624 len = path->len;1625while((e =readdir(dir)) != NULL) {1626struct stat st;1627if(is_dot_or_dotdot(e->d_name))1628continue;16291630strbuf_setlen(path, len);1631strbuf_addstr(path, e->d_name);1632if(lstat(path->buf, &st)) {1633if(errno == ENOENT)1634/*1635 * file disappeared, which is what we1636 * wanted anyway1637 */1638continue;1639/* fall thru */1640}else if(S_ISDIR(st.st_mode)) {1641if(!remove_dir_recurse(path, flag, &kept_down))1642continue;/* happy */1643}else if(!only_empty &&1644(!unlink(path->buf) || errno == ENOENT)) {1645continue;/* happy, too */1646}16471648/* path too long, stat fails, or non-directory still exists */1649 ret = -1;1650break;1651}1652closedir(dir);16531654strbuf_setlen(path, original_len);1655if(!ret && !keep_toplevel && !kept_down)1656 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1657else if(kept_up)1658/*1659 * report the uplevel that it is not an error that we1660 * did not rmdir() our directory.1661 */1662*kept_up = !ret;1663return ret;1664}16651666intremove_dir_recursively(struct strbuf *path,int flag)1667{1668returnremove_dir_recurse(path, flag, NULL);1669}16701671voidsetup_standard_excludes(struct dir_struct *dir)1672{1673const char*path;16741675 dir->exclude_per_dir =".gitignore";16761677/* core.excludefile defaulting to $XDG_HOME/git/ignore */1678if(!excludes_file)1679 excludes_file =xdg_config_home("ignore");1680if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1681add_excludes_from_file(dir, excludes_file);16821683/* per repository user preference */1684 path =git_path("info/exclude");1685if(!access_or_warn(path, R_OK,0))1686add_excludes_from_file(dir, path);1687}16881689intremove_path(const char*name)1690{1691char*slash;16921693if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1694return-1;16951696 slash =strrchr(name,'/');1697if(slash) {1698char*dirs =xstrdup(name);1699 slash = dirs + (slash - name);1700do{1701*slash ='\0';1702}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1703free(dirs);1704}1705return0;1706}17071708/*1709 * Frees memory within dir which was allocated for exclude lists and1710 * the exclude_stack. Does not free dir itself.1711 */1712voidclear_directory(struct dir_struct *dir)1713{1714int i, j;1715struct exclude_list_group *group;1716struct exclude_list *el;1717struct exclude_stack *stk;17181719for(i = EXC_CMDL; i <= EXC_FILE; i++) {1720 group = &dir->exclude_list_group[i];1721for(j =0; j < group->nr; j++) {1722 el = &group->el[j];1723if(i == EXC_DIRS)1724free((char*)el->src);1725clear_exclude_list(el);1726}1727free(group->el);1728}17291730 stk = dir->exclude_stack;1731while(stk) {1732struct exclude_stack *prev = stk->prev;1733free(stk);1734 stk = prev;1735}1736strbuf_release(&dir->basebuf);1737}