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 */ 388int num, errors =0; 389for(num =0; num < pathspec->nr; num++) { 390int other, found_dup; 391 392if(ps_matched[num]) 393continue; 394/* 395 * The caller might have fed identical pathspec 396 * twice. Do not barf on such a mistake. 397 * FIXME: parse_pathspec should have eliminated 398 * duplicate pathspec. 399 */ 400for(found_dup = other =0; 401!found_dup && other < pathspec->nr; 402 other++) { 403if(other == num || !ps_matched[other]) 404continue; 405if(!strcmp(pathspec->items[other].original, 406 pathspec->items[num].original)) 407/* 408 * Ok, we have a match already. 409 */ 410 found_dup =1; 411} 412if(found_dup) 413continue; 414 415error("pathspec '%s' did not match any file(s) known to git.", 416 pathspec->items[num].original); 417 errors++; 418} 419return errors; 420} 421 422/* 423 * Return the length of the "simple" part of a path match limiter. 424 */ 425intsimple_length(const char*match) 426{ 427int len = -1; 428 429for(;;) { 430unsigned char c = *match++; 431 len++; 432if(c =='\0'||is_glob_special(c)) 433return len; 434} 435} 436 437intno_wildcard(const char*string) 438{ 439return string[simple_length(string)] =='\0'; 440} 441 442voidparse_exclude_pattern(const char**pattern, 443int*patternlen, 444int*flags, 445int*nowildcardlen) 446{ 447const char*p = *pattern; 448size_t i, len; 449 450*flags =0; 451if(*p =='!') { 452*flags |= EXC_FLAG_NEGATIVE; 453 p++; 454} 455 len =strlen(p); 456if(len && p[len -1] =='/') { 457 len--; 458*flags |= EXC_FLAG_MUSTBEDIR; 459} 460for(i =0; i < len; i++) { 461if(p[i] =='/') 462break; 463} 464if(i == len) 465*flags |= EXC_FLAG_NODIR; 466*nowildcardlen =simple_length(p); 467/* 468 * we should have excluded the trailing slash from 'p' too, 469 * but that's one more allocation. Instead just make sure 470 * nowildcardlen does not exceed real patternlen 471 */ 472if(*nowildcardlen > len) 473*nowildcardlen = len; 474if(*p =='*'&&no_wildcard(p +1)) 475*flags |= EXC_FLAG_ENDSWITH; 476*pattern = p; 477*patternlen = len; 478} 479 480voidadd_exclude(const char*string,const char*base, 481int baselen,struct exclude_list *el,int srcpos) 482{ 483struct exclude *x; 484int patternlen; 485int flags; 486int nowildcardlen; 487 488parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 489if(flags & EXC_FLAG_MUSTBEDIR) { 490char*s; 491 x =xmalloc(sizeof(*x) + patternlen +1); 492 s = (char*)(x+1); 493memcpy(s, string, patternlen); 494 s[patternlen] ='\0'; 495 x->pattern = s; 496}else{ 497 x =xmalloc(sizeof(*x)); 498 x->pattern = string; 499} 500 x->patternlen = patternlen; 501 x->nowildcardlen = nowildcardlen; 502 x->base = base; 503 x->baselen = baselen; 504 x->flags = flags; 505 x->srcpos = srcpos; 506ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 507 el->excludes[el->nr++] = x; 508 x->el = el; 509} 510 511static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 512{ 513int pos, len; 514unsigned long sz; 515enum object_type type; 516void*data; 517 518 len =strlen(path); 519 pos =cache_name_pos(path, len); 520if(pos <0) 521return NULL; 522if(!ce_skip_worktree(active_cache[pos])) 523return NULL; 524 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 525if(!data || type != OBJ_BLOB) { 526free(data); 527return NULL; 528} 529*size =xsize_t(sz); 530return data; 531} 532 533/* 534 * Frees memory within el which was allocated for exclude patterns and 535 * the file buffer. Does not free el itself. 536 */ 537voidclear_exclude_list(struct exclude_list *el) 538{ 539int i; 540 541for(i =0; i < el->nr; i++) 542free(el->excludes[i]); 543free(el->excludes); 544free(el->filebuf); 545 546 el->nr =0; 547 el->excludes = NULL; 548 el->filebuf = NULL; 549} 550 551static voidtrim_trailing_spaces(char*buf) 552{ 553char*p, *last_space = NULL; 554 555for(p = buf; *p; p++) 556switch(*p) { 557case' ': 558if(!last_space) 559 last_space = p; 560break; 561case'\\': 562 p++; 563if(!*p) 564return; 565/* fallthrough */ 566default: 567 last_space = NULL; 568} 569 570if(last_space) 571*last_space ='\0'; 572} 573 574intadd_excludes_from_file_to_list(const char*fname, 575const char*base, 576int baselen, 577struct exclude_list *el, 578int check_index) 579{ 580struct stat st; 581int fd, i, lineno =1; 582size_t size =0; 583char*buf, *entry; 584 585 fd =open(fname, O_RDONLY); 586if(fd <0||fstat(fd, &st) <0) { 587if(errno != ENOENT) 588warn_on_inaccessible(fname); 589if(0<= fd) 590close(fd); 591if(!check_index || 592(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 593return-1; 594if(size ==0) { 595free(buf); 596return0; 597} 598if(buf[size-1] !='\n') { 599 buf =xrealloc(buf, size+1); 600 buf[size++] ='\n'; 601} 602}else{ 603 size =xsize_t(st.st_size); 604if(size ==0) { 605close(fd); 606return0; 607} 608 buf =xmalloc(size+1); 609if(read_in_full(fd, buf, size) != size) { 610free(buf); 611close(fd); 612return-1; 613} 614 buf[size++] ='\n'; 615close(fd); 616} 617 618 el->filebuf = buf; 619 620if(skip_utf8_bom(&buf, size)) 621 size -= buf - el->filebuf; 622 623 entry = buf; 624 625for(i =0; i < size; i++) { 626if(buf[i] =='\n') { 627if(entry != buf + i && entry[0] !='#') { 628 buf[i - (i && buf[i-1] =='\r')] =0; 629trim_trailing_spaces(entry); 630add_exclude(entry, base, baselen, el, lineno); 631} 632 lineno++; 633 entry = buf + i +1; 634} 635} 636return0; 637} 638 639struct exclude_list *add_exclude_list(struct dir_struct *dir, 640int group_type,const char*src) 641{ 642struct exclude_list *el; 643struct exclude_list_group *group; 644 645 group = &dir->exclude_list_group[group_type]; 646ALLOC_GROW(group->el, group->nr +1, group->alloc); 647 el = &group->el[group->nr++]; 648memset(el,0,sizeof(*el)); 649 el->src = src; 650return el; 651} 652 653/* 654 * Used to set up core.excludesfile and .git/info/exclude lists. 655 */ 656voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 657{ 658struct exclude_list *el; 659 el =add_exclude_list(dir, EXC_FILE, fname); 660if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 661die("cannot use%sas an exclude file", fname); 662} 663 664intmatch_basename(const char*basename,int basenamelen, 665const char*pattern,int prefix,int patternlen, 666int flags) 667{ 668if(prefix == patternlen) { 669if(patternlen == basenamelen && 670!strncmp_icase(pattern, basename, basenamelen)) 671return1; 672}else if(flags & EXC_FLAG_ENDSWITH) { 673/* "*literal" matching against "fooliteral" */ 674if(patternlen -1<= basenamelen && 675!strncmp_icase(pattern +1, 676 basename + basenamelen - (patternlen -1), 677 patternlen -1)) 678return1; 679}else{ 680if(fnmatch_icase_mem(pattern, patternlen, 681 basename, basenamelen, 6820) ==0) 683return1; 684} 685return0; 686} 687 688intmatch_pathname(const char*pathname,int pathlen, 689const char*base,int baselen, 690const char*pattern,int prefix,int patternlen, 691int flags) 692{ 693const char*name; 694int namelen; 695 696/* 697 * match with FNM_PATHNAME; the pattern has base implicitly 698 * in front of it. 699 */ 700if(*pattern =='/') { 701 pattern++; 702 patternlen--; 703 prefix--; 704} 705 706/* 707 * baselen does not count the trailing slash. base[] may or 708 * may not end with a trailing slash though. 709 */ 710if(pathlen < baselen +1|| 711(baselen && pathname[baselen] !='/') || 712strncmp_icase(pathname, base, baselen)) 713return0; 714 715 namelen = baselen ? pathlen - baselen -1: pathlen; 716 name = pathname + pathlen - namelen; 717 718if(prefix) { 719/* 720 * if the non-wildcard part is longer than the 721 * remaining pathname, surely it cannot match. 722 */ 723if(prefix > namelen) 724return0; 725 726if(strncmp_icase(pattern, name, prefix)) 727return0; 728 pattern += prefix; 729 patternlen -= prefix; 730 name += prefix; 731 namelen -= prefix; 732 733/* 734 * If the whole pattern did not have a wildcard, 735 * then our prefix match is all we need; we 736 * do not need to call fnmatch at all. 737 */ 738if(!patternlen && !namelen) 739return1; 740} 741 742returnfnmatch_icase_mem(pattern, patternlen, 743 name, namelen, 744 WM_PATHNAME) ==0; 745} 746 747/* 748 * Scan the given exclude list in reverse to see whether pathname 749 * should be ignored. The first match (i.e. the last on the list), if 750 * any, determines the fate. Returns the exclude_list element which 751 * matched, or NULL for undecided. 752 */ 753static struct exclude *last_exclude_matching_from_list(const char*pathname, 754int pathlen, 755const char*basename, 756int*dtype, 757struct exclude_list *el) 758{ 759int i; 760 761if(!el->nr) 762return NULL;/* undefined */ 763 764for(i = el->nr -1;0<= i; i--) { 765struct exclude *x = el->excludes[i]; 766const char*exclude = x->pattern; 767int prefix = x->nowildcardlen; 768 769if(x->flags & EXC_FLAG_MUSTBEDIR) { 770if(*dtype == DT_UNKNOWN) 771*dtype =get_dtype(NULL, pathname, pathlen); 772if(*dtype != DT_DIR) 773continue; 774} 775 776if(x->flags & EXC_FLAG_NODIR) { 777if(match_basename(basename, 778 pathlen - (basename - pathname), 779 exclude, prefix, x->patternlen, 780 x->flags)) 781return x; 782continue; 783} 784 785assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 786if(match_pathname(pathname, pathlen, 787 x->base, x->baselen ? x->baselen -1:0, 788 exclude, prefix, x->patternlen, x->flags)) 789return x; 790} 791return NULL;/* undecided */ 792} 793 794/* 795 * Scan the list and let the last match determine the fate. 796 * Return 1 for exclude, 0 for include and -1 for undecided. 797 */ 798intis_excluded_from_list(const char*pathname, 799int pathlen,const char*basename,int*dtype, 800struct exclude_list *el) 801{ 802struct exclude *exclude; 803 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 804if(exclude) 805return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 806return-1;/* undecided */ 807} 808 809static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 810const char*pathname,int pathlen,const char*basename, 811int*dtype_p) 812{ 813int i, j; 814struct exclude_list_group *group; 815struct exclude *exclude; 816for(i = EXC_CMDL; i <= EXC_FILE; i++) { 817 group = &dir->exclude_list_group[i]; 818for(j = group->nr -1; j >=0; j--) { 819 exclude =last_exclude_matching_from_list( 820 pathname, pathlen, basename, dtype_p, 821&group->el[j]); 822if(exclude) 823return exclude; 824} 825} 826return NULL; 827} 828 829/* 830 * Loads the per-directory exclude list for the substring of base 831 * which has a char length of baselen. 832 */ 833static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 834{ 835struct exclude_list_group *group; 836struct exclude_list *el; 837struct exclude_stack *stk = NULL; 838int current; 839 840 group = &dir->exclude_list_group[EXC_DIRS]; 841 842/* 843 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 844 * which originate from directories not in the prefix of the 845 * path being checked. 846 */ 847while((stk = dir->exclude_stack) != NULL) { 848if(stk->baselen <= baselen && 849!strncmp(dir->basebuf.buf, base, stk->baselen)) 850break; 851 el = &group->el[dir->exclude_stack->exclude_ix]; 852 dir->exclude_stack = stk->prev; 853 dir->exclude = NULL; 854free((char*)el->src);/* see strbuf_detach() below */ 855clear_exclude_list(el); 856free(stk); 857 group->nr--; 858} 859 860/* Skip traversing into sub directories if the parent is excluded */ 861if(dir->exclude) 862return; 863 864/* 865 * Lazy initialization. All call sites currently just 866 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 867 * them seems lots of work for little benefit. 868 */ 869if(!dir->basebuf.buf) 870strbuf_init(&dir->basebuf, PATH_MAX); 871 872/* Read from the parent directories and push them down. */ 873 current = stk ? stk->baselen : -1; 874strbuf_setlen(&dir->basebuf, current <0?0: current); 875while(current < baselen) { 876const char*cp; 877 878 stk =xcalloc(1,sizeof(*stk)); 879if(current <0) { 880 cp = base; 881 current =0; 882}else{ 883 cp =strchr(base + current +1,'/'); 884if(!cp) 885die("oops in prep_exclude"); 886 cp++; 887} 888 stk->prev = dir->exclude_stack; 889 stk->baselen = cp - base; 890 stk->exclude_ix = group->nr; 891 el =add_exclude_list(dir, EXC_DIRS, NULL); 892strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 893assert(stk->baselen == dir->basebuf.len); 894 895/* Abort if the directory is excluded */ 896if(stk->baselen) { 897int dt = DT_DIR; 898 dir->basebuf.buf[stk->baselen -1] =0; 899 dir->exclude =last_exclude_matching_from_lists(dir, 900 dir->basebuf.buf, stk->baselen -1, 901 dir->basebuf.buf + current, &dt); 902 dir->basebuf.buf[stk->baselen -1] ='/'; 903if(dir->exclude && 904 dir->exclude->flags & EXC_FLAG_NEGATIVE) 905 dir->exclude = NULL; 906if(dir->exclude) { 907 dir->exclude_stack = stk; 908return; 909} 910} 911 912/* Try to read per-directory file */ 913if(dir->exclude_per_dir) { 914/* 915 * dir->basebuf gets reused by the traversal, but we 916 * need fname to remain unchanged to ensure the src 917 * member of each struct exclude correctly 918 * back-references its source file. Other invocations 919 * of add_exclude_list provide stable strings, so we 920 * strbuf_detach() and free() here in the caller. 921 */ 922struct strbuf sb = STRBUF_INIT; 923strbuf_addbuf(&sb, &dir->basebuf); 924strbuf_addstr(&sb, dir->exclude_per_dir); 925 el->src =strbuf_detach(&sb, NULL); 926add_excludes_from_file_to_list(el->src, el->src, 927 stk->baselen, el,1); 928} 929 dir->exclude_stack = stk; 930 current = stk->baselen; 931} 932strbuf_setlen(&dir->basebuf, baselen); 933} 934 935/* 936 * Loads the exclude lists for the directory containing pathname, then 937 * scans all exclude lists to determine whether pathname is excluded. 938 * Returns the exclude_list element which matched, or NULL for 939 * undecided. 940 */ 941struct exclude *last_exclude_matching(struct dir_struct *dir, 942const char*pathname, 943int*dtype_p) 944{ 945int pathlen =strlen(pathname); 946const char*basename =strrchr(pathname,'/'); 947 basename = (basename) ? basename+1: pathname; 948 949prep_exclude(dir, pathname, basename-pathname); 950 951if(dir->exclude) 952return dir->exclude; 953 954returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 955 basename, dtype_p); 956} 957 958/* 959 * Loads the exclude lists for the directory containing pathname, then 960 * scans all exclude lists to determine whether pathname is excluded. 961 * Returns 1 if true, otherwise 0. 962 */ 963intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 964{ 965struct exclude *exclude = 966last_exclude_matching(dir, pathname, dtype_p); 967if(exclude) 968return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 969return0; 970} 971 972static struct dir_entry *dir_entry_new(const char*pathname,int len) 973{ 974struct dir_entry *ent; 975 976 ent =xmalloc(sizeof(*ent) + len +1); 977 ent->len = len; 978memcpy(ent->name, pathname, len); 979 ent->name[len] =0; 980return ent; 981} 982 983static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 984{ 985if(cache_file_exists(pathname, len, ignore_case)) 986return NULL; 987 988ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 989return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 990} 991 992struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 993{ 994if(!cache_name_is_other(pathname, len)) 995return NULL; 996 997ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 998return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 999}10001001enum exist_status {1002 index_nonexistent =0,1003 index_directory,1004 index_gitdir1005};10061007/*1008 * Do not use the alphabetically sorted index to look up1009 * the directory name; instead, use the case insensitive1010 * directory hash.1011 */1012static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1013{1014const struct cache_entry *ce =cache_dir_exists(dirname, len);1015unsigned char endchar;10161017if(!ce)1018return index_nonexistent;1019 endchar = ce->name[len];10201021/*1022 * The cache_entry structure returned will contain this dirname1023 * and possibly additional path components.1024 */1025if(endchar =='/')1026return index_directory;10271028/*1029 * If there are no additional path components, then this cache_entry1030 * represents a submodule. Submodules, despite being directories,1031 * are stored in the cache without a closing slash.1032 */1033if(!endchar &&S_ISGITLINK(ce->ce_mode))1034return index_gitdir;10351036/* This should never be hit, but it exists just in case. */1037return index_nonexistent;1038}10391040/*1041 * The index sorts alphabetically by entry name, which1042 * means that a gitlink sorts as '\0' at the end, while1043 * a directory (which is defined not as an entry, but as1044 * the files it contains) will sort with the '/' at the1045 * end.1046 */1047static enum exist_status directory_exists_in_index(const char*dirname,int len)1048{1049int pos;10501051if(ignore_case)1052returndirectory_exists_in_index_icase(dirname, len);10531054 pos =cache_name_pos(dirname, len);1055if(pos <0)1056 pos = -pos-1;1057while(pos < active_nr) {1058const struct cache_entry *ce = active_cache[pos++];1059unsigned char endchar;10601061if(strncmp(ce->name, dirname, len))1062break;1063 endchar = ce->name[len];1064if(endchar >'/')1065break;1066if(endchar =='/')1067return index_directory;1068if(!endchar &&S_ISGITLINK(ce->ce_mode))1069return index_gitdir;1070}1071return index_nonexistent;1072}10731074/*1075 * When we find a directory when traversing the filesystem, we1076 * have three distinct cases:1077 *1078 * - ignore it1079 * - see it as a directory1080 * - recurse into it1081 *1082 * and which one we choose depends on a combination of existing1083 * git index contents and the flags passed into the directory1084 * traversal routine.1085 *1086 * Case 1: If we *already* have entries in the index under that1087 * directory name, we always recurse into the directory to see1088 * all the files.1089 *1090 * Case 2: If we *already* have that directory name as a gitlink,1091 * we always continue to see it as a gitlink, regardless of whether1092 * there is an actual git directory there or not (it might not1093 * be checked out as a subproject!)1094 *1095 * Case 3: if we didn't have it in the index previously, we1096 * have a few sub-cases:1097 *1098 * (a) if "show_other_directories" is true, we show it as1099 * just a directory, unless "hide_empty_directories" is1100 * also true, in which case we need to check if it contains any1101 * untracked and / or ignored files.1102 * (b) if it looks like a git directory, and we don't have1103 * 'no_gitlinks' set we treat it as a gitlink, and show it1104 * as a directory.1105 * (c) otherwise, we recurse into it.1106 */1107static enum path_treatment treat_directory(struct dir_struct *dir,1108const char*dirname,int len,int exclude,1109const struct path_simplify *simplify)1110{1111/* The "len-1" is to strip the final '/' */1112switch(directory_exists_in_index(dirname, len-1)) {1113case index_directory:1114return path_recurse;11151116case index_gitdir:1117return path_none;11181119case index_nonexistent:1120if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1121break;1122if(!(dir->flags & DIR_NO_GITLINKS)) {1123unsigned char sha1[20];1124if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1125return path_untracked;1126}1127return path_recurse;1128}11291130/* This is the "show_other_directories" case */11311132if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1133return exclude ? path_excluded : path_untracked;11341135returnread_directory_recursive(dir, dirname, len,1, simplify);1136}11371138/*1139 * This is an inexact early pruning of any recursive directory1140 * reading - if the path cannot possibly be in the pathspec,1141 * return true, and we'll skip it early.1142 */1143static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1144{1145if(simplify) {1146for(;;) {1147const char*match = simplify->path;1148int len = simplify->len;11491150if(!match)1151break;1152if(len > pathlen)1153 len = pathlen;1154if(!memcmp(path, match, len))1155return0;1156 simplify++;1157}1158return1;1159}1160return0;1161}11621163/*1164 * This function tells us whether an excluded path matches a1165 * list of "interesting" pathspecs. That is, whether a path matched1166 * by any of the pathspecs could possibly be ignored by excluding1167 * the specified path. This can happen if:1168 *1169 * 1. the path is mentioned explicitly in the pathspec1170 *1171 * 2. the path is a directory prefix of some element in the1172 * pathspec1173 */1174static intexclude_matches_pathspec(const char*path,int len,1175const struct path_simplify *simplify)1176{1177if(simplify) {1178for(; simplify->path; simplify++) {1179if(len == simplify->len1180&& !memcmp(path, simplify->path, len))1181return1;1182if(len < simplify->len1183&& simplify->path[len] =='/'1184&& !memcmp(path, simplify->path, len))1185return1;1186}1187}1188return0;1189}11901191static intget_index_dtype(const char*path,int len)1192{1193int pos;1194const struct cache_entry *ce;11951196 ce =cache_file_exists(path, len,0);1197if(ce) {1198if(!ce_uptodate(ce))1199return DT_UNKNOWN;1200if(S_ISGITLINK(ce->ce_mode))1201return DT_DIR;1202/*1203 * Nobody actually cares about the1204 * difference between DT_LNK and DT_REG1205 */1206return DT_REG;1207}12081209/* Try to look it up as a directory */1210 pos =cache_name_pos(path, len);1211if(pos >=0)1212return DT_UNKNOWN;1213 pos = -pos-1;1214while(pos < active_nr) {1215 ce = active_cache[pos++];1216if(strncmp(ce->name, path, len))1217break;1218if(ce->name[len] >'/')1219break;1220if(ce->name[len] <'/')1221continue;1222if(!ce_uptodate(ce))1223break;/* continue? */1224return DT_DIR;1225}1226return DT_UNKNOWN;1227}12281229static intget_dtype(struct dirent *de,const char*path,int len)1230{1231int dtype = de ?DTYPE(de) : DT_UNKNOWN;1232struct stat st;12331234if(dtype != DT_UNKNOWN)1235return dtype;1236 dtype =get_index_dtype(path, len);1237if(dtype != DT_UNKNOWN)1238return dtype;1239if(lstat(path, &st))1240return dtype;1241if(S_ISREG(st.st_mode))1242return DT_REG;1243if(S_ISDIR(st.st_mode))1244return DT_DIR;1245if(S_ISLNK(st.st_mode))1246return DT_LNK;1247return dtype;1248}12491250static enum path_treatment treat_one_path(struct dir_struct *dir,1251struct strbuf *path,1252const struct path_simplify *simplify,1253int dtype,struct dirent *de)1254{1255int exclude;1256int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);12571258if(dtype == DT_UNKNOWN)1259 dtype =get_dtype(de, path->buf, path->len);12601261/* Always exclude indexed files */1262if(dtype != DT_DIR && has_path_in_index)1263return path_none;12641265/*1266 * When we are looking at a directory P in the working tree,1267 * there are three cases:1268 *1269 * (1) P exists in the index. Everything inside the directory P in1270 * the working tree needs to go when P is checked out from the1271 * index.1272 *1273 * (2) P does not exist in the index, but there is P/Q in the index.1274 * We know P will stay a directory when we check out the contents1275 * of the index, but we do not know yet if there is a directory1276 * P/Q in the working tree to be killed, so we need to recurse.1277 *1278 * (3) P does not exist in the index, and there is no P/Q in the index1279 * to require P to be a directory, either. Only in this case, we1280 * know that everything inside P will not be killed without1281 * recursing.1282 */1283if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1284(dtype == DT_DIR) &&1285!has_path_in_index &&1286(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1287return path_none;12881289 exclude =is_excluded(dir, path->buf, &dtype);12901291/*1292 * Excluded? If we don't explicitly want to show1293 * ignored files, ignore it1294 */1295if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1296return path_excluded;12971298switch(dtype) {1299default:1300return path_none;1301case DT_DIR:1302strbuf_addch(path,'/');1303returntreat_directory(dir, path->buf, path->len, exclude,1304 simplify);1305case DT_REG:1306case DT_LNK:1307return exclude ? path_excluded : path_untracked;1308}1309}13101311static enum path_treatment treat_path(struct dir_struct *dir,1312struct dirent *de,1313struct strbuf *path,1314int baselen,1315const struct path_simplify *simplify)1316{1317int dtype;13181319if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1320return path_none;1321strbuf_setlen(path, baselen);1322strbuf_addstr(path, de->d_name);1323if(simplify_away(path->buf, path->len, simplify))1324return path_none;13251326 dtype =DTYPE(de);1327returntreat_one_path(dir, path, simplify, dtype, de);1328}13291330/*1331 * Read a directory tree. We currently ignore anything but1332 * directories, regular files and symlinks. That's because git1333 * doesn't handle them at all yet. Maybe that will change some1334 * day.1335 *1336 * Also, we ignore the name ".git" (even if it is not a directory).1337 * That likely will not change.1338 *1339 * Returns the most significant path_treatment value encountered in the scan.1340 */1341static enum path_treatment read_directory_recursive(struct dir_struct *dir,1342const char*base,int baselen,1343int check_only,1344const struct path_simplify *simplify)1345{1346DIR*fdir;1347enum path_treatment state, subdir_state, dir_state = path_none;1348struct dirent *de;1349struct strbuf path = STRBUF_INIT;13501351strbuf_add(&path, base, baselen);13521353 fdir =opendir(path.len ? path.buf :".");1354if(!fdir)1355goto out;13561357while((de =readdir(fdir)) != NULL) {1358/* check how the file or directory should be treated */1359 state =treat_path(dir, de, &path, baselen, simplify);1360if(state > dir_state)1361 dir_state = state;13621363/* recurse into subdir if instructed by treat_path */1364if(state == path_recurse) {1365 subdir_state =read_directory_recursive(dir, path.buf,1366 path.len, check_only, simplify);1367if(subdir_state > dir_state)1368 dir_state = subdir_state;1369}13701371if(check_only) {1372/* abort early if maximum state has been reached */1373if(dir_state == path_untracked)1374break;1375/* skip the dir_add_* part */1376continue;1377}13781379/* add the path to the appropriate result list */1380switch(state) {1381case path_excluded:1382if(dir->flags & DIR_SHOW_IGNORED)1383dir_add_name(dir, path.buf, path.len);1384else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1385((dir->flags & DIR_COLLECT_IGNORED) &&1386exclude_matches_pathspec(path.buf, path.len,1387 simplify)))1388dir_add_ignored(dir, path.buf, path.len);1389break;13901391case path_untracked:1392if(!(dir->flags & DIR_SHOW_IGNORED))1393dir_add_name(dir, path.buf, path.len);1394break;13951396default:1397break;1398}1399}1400closedir(fdir);1401 out:1402strbuf_release(&path);14031404return dir_state;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;14111412returnname_compare(e1->name, e1->len, e2->name, e2->len);1413}14141415static struct path_simplify *create_simplify(const char**pathspec)1416{1417int nr, alloc =0;1418struct path_simplify *simplify = NULL;14191420if(!pathspec)1421return NULL;14221423for(nr =0; ; nr++) {1424const char*match;1425ALLOC_GROW(simplify, nr +1, alloc);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;1449int old_flags = dir->flags;14501451while(len && path[len -1] =='/')1452 len--;1453if(!len)1454return1;1455 baselen =0;1456 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1457while(1) {1458 cp = path + baselen + !!baselen;1459 cp =memchr(cp,'/', path + len - cp);1460if(!cp)1461 baselen = len;1462else1463 baselen = cp - path;1464strbuf_setlen(&sb,0);1465strbuf_add(&sb, path, baselen);1466if(!is_directory(sb.buf))1467break;1468if(simplify_away(sb.buf, sb.len, simplify))1469break;1470if(treat_one_path(dir, &sb, simplify,1471 DT_DIR, NULL) == path_none)1472break;/* do not recurse into it */1473if(len <= baselen) {1474 rc =1;1475break;/* finished checking */1476}1477}1478strbuf_release(&sb);1479 dir->flags = old_flags;1480return rc;1481}14821483intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1484{1485struct path_simplify *simplify;14861487/*1488 * Check out create_simplify()1489 */1490if(pathspec)1491GUARD_PATHSPEC(pathspec,1492 PATHSPEC_FROMTOP |1493 PATHSPEC_MAXDEPTH |1494 PATHSPEC_LITERAL |1495 PATHSPEC_GLOB |1496 PATHSPEC_ICASE |1497 PATHSPEC_EXCLUDE);14981499if(has_symlink_leading_path(path, len))1500return dir->nr;15011502/*1503 * exclude patterns are treated like positive ones in1504 * create_simplify. Usually exclude patterns should be a1505 * subset of positive ones, which has no impacts on1506 * create_simplify().1507 */1508 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1509if(!len ||treat_leading_path(dir, path, len, simplify))1510read_directory_recursive(dir, path, len,0, simplify);1511free_simplify(simplify);1512qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1513qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1514return dir->nr;1515}15161517intfile_exists(const char*f)1518{1519struct stat sb;1520returnlstat(f, &sb) ==0;1521}15221523/*1524 * Given two normalized paths (a trailing slash is ok), if subdir is1525 * outside dir, return -1. Otherwise return the offset in subdir that1526 * can be used as relative path to dir.1527 */1528intdir_inside_of(const char*subdir,const char*dir)1529{1530int offset =0;15311532assert(dir && subdir && *dir && *subdir);15331534while(*dir && *subdir && *dir == *subdir) {1535 dir++;1536 subdir++;1537 offset++;1538}15391540/* hel[p]/me vs hel[l]/yeah */1541if(*dir && *subdir)1542return-1;15431544if(!*subdir)1545return!*dir ? offset : -1;/* same dir */15461547/* foo/[b]ar vs foo/[] */1548if(is_dir_sep(dir[-1]))1549returnis_dir_sep(subdir[-1]) ? offset : -1;15501551/* foo[/]bar vs foo[] */1552returnis_dir_sep(*subdir) ? offset +1: -1;1553}15541555intis_inside_dir(const char*dir)1556{1557char*cwd;1558int rc;15591560if(!dir)1561return0;15621563 cwd =xgetcwd();1564 rc = (dir_inside_of(cwd, dir) >=0);1565free(cwd);1566return rc;1567}15681569intis_empty_dir(const char*path)1570{1571DIR*dir =opendir(path);1572struct dirent *e;1573int ret =1;15741575if(!dir)1576return0;15771578while((e =readdir(dir)) != NULL)1579if(!is_dot_or_dotdot(e->d_name)) {1580 ret =0;1581break;1582}15831584closedir(dir);1585return ret;1586}15871588static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1589{1590DIR*dir;1591struct dirent *e;1592int ret =0, original_len = path->len, len, kept_down =0;1593int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1594int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1595unsigned char submodule_head[20];15961597if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1598!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1599/* Do not descend and nuke a nested git work tree. */1600if(kept_up)1601*kept_up =1;1602return0;1603}16041605 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1606 dir =opendir(path->buf);1607if(!dir) {1608if(errno == ENOENT)1609return keep_toplevel ? -1:0;1610else if(errno == EACCES && !keep_toplevel)1611/*1612 * An empty dir could be removable even if it1613 * is unreadable:1614 */1615returnrmdir(path->buf);1616else1617return-1;1618}1619if(path->buf[original_len -1] !='/')1620strbuf_addch(path,'/');16211622 len = path->len;1623while((e =readdir(dir)) != NULL) {1624struct stat st;1625if(is_dot_or_dotdot(e->d_name))1626continue;16271628strbuf_setlen(path, len);1629strbuf_addstr(path, e->d_name);1630if(lstat(path->buf, &st)) {1631if(errno == ENOENT)1632/*1633 * file disappeared, which is what we1634 * wanted anyway1635 */1636continue;1637/* fall thru */1638}else if(S_ISDIR(st.st_mode)) {1639if(!remove_dir_recurse(path, flag, &kept_down))1640continue;/* happy */1641}else if(!only_empty &&1642(!unlink(path->buf) || errno == ENOENT)) {1643continue;/* happy, too */1644}16451646/* path too long, stat fails, or non-directory still exists */1647 ret = -1;1648break;1649}1650closedir(dir);16511652strbuf_setlen(path, original_len);1653if(!ret && !keep_toplevel && !kept_down)1654 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1655else if(kept_up)1656/*1657 * report the uplevel that it is not an error that we1658 * did not rmdir() our directory.1659 */1660*kept_up = !ret;1661return ret;1662}16631664intremove_dir_recursively(struct strbuf *path,int flag)1665{1666returnremove_dir_recurse(path, flag, NULL);1667}16681669voidsetup_standard_excludes(struct dir_struct *dir)1670{1671const char*path;16721673 dir->exclude_per_dir =".gitignore";16741675/* core.excludefile defaulting to $XDG_HOME/git/ignore */1676if(!excludes_file)1677 excludes_file =xdg_config_home("ignore");1678if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1679add_excludes_from_file(dir, excludes_file);16801681/* per repository user preference */1682 path =git_path("info/exclude");1683if(!access_or_warn(path, R_OK,0))1684add_excludes_from_file(dir, path);1685}16861687intremove_path(const char*name)1688{1689char*slash;16901691if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1692return-1;16931694 slash =strrchr(name,'/');1695if(slash) {1696char*dirs =xstrdup(name);1697 slash = dirs + (slash - name);1698do{1699*slash ='\0';1700}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1701free(dirs);1702}1703return0;1704}17051706/*1707 * Frees memory within dir which was allocated for exclude lists and1708 * the exclude_stack. Does not free dir itself.1709 */1710voidclear_directory(struct dir_struct *dir)1711{1712int i, j;1713struct exclude_list_group *group;1714struct exclude_list *el;1715struct exclude_stack *stk;17161717for(i = EXC_CMDL; i <= EXC_FILE; i++) {1718 group = &dir->exclude_list_group[i];1719for(j =0; j < group->nr; j++) {1720 el = &group->el[j];1721if(i == EXC_DIRS)1722free((char*)el->src);1723clear_exclude_list(el);1724}1725free(group->el);1726}17271728 stk = dir->exclude_stack;1729while(stk) {1730struct exclude_stack *prev = stk->prev;1731free(stk);1732 stk = prev;1733}1734strbuf_release(&dir->basebuf);1735}