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 16struct path_simplify { 17int len; 18const char*path; 19}; 20 21/* 22 * Tells read_directory_recursive how a file or directory should be treated. 23 * Values are ordered by significance, e.g. if a directory contains both 24 * excluded and untracked files, it is listed as untracked because 25 * path_untracked > path_excluded. 26 */ 27enum path_treatment { 28 path_none =0, 29 path_recurse, 30 path_excluded, 31 path_untracked 32}; 33 34static enum path_treatment read_directory_recursive(struct dir_struct *dir, 35const char*path,int len, 36int check_only,const struct path_simplify *simplify); 37static intget_dtype(struct dirent *de,const char*path,int len); 38 39/* helper string functions with support for the ignore_case flag */ 40intstrcmp_icase(const char*a,const char*b) 41{ 42return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 43} 44 45intstrncmp_icase(const char*a,const char*b,size_t count) 46{ 47return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 48} 49 50intfnmatch_icase(const char*pattern,const char*string,int flags) 51{ 52returnwildmatch(pattern, string, 53 flags | (ignore_case ? WM_CASEFOLD :0), 54 NULL); 55} 56 57intgit_fnmatch(const struct pathspec_item *item, 58const char*pattern,const char*string, 59int prefix) 60{ 61if(prefix >0) { 62if(ps_strncmp(item, pattern, string, prefix)) 63return WM_NOMATCH; 64 pattern += prefix; 65 string += prefix; 66} 67if(item->flags & PATHSPEC_ONESTAR) { 68int pattern_len =strlen(++pattern); 69int string_len =strlen(string); 70return string_len < pattern_len || 71ps_strcmp(item, pattern, 72 string + string_len - pattern_len); 73} 74if(item->magic & PATHSPEC_GLOB) 75returnwildmatch(pattern, string, 76 WM_PATHNAME | 77(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0), 78 NULL); 79else 80/* wildmatch has not learned no FNM_PATHNAME mode yet */ 81returnwildmatch(pattern, string, 82 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0, 83 NULL); 84} 85 86static intfnmatch_icase_mem(const char*pattern,int patternlen, 87const char*string,int stringlen, 88int flags) 89{ 90int match_status; 91struct strbuf pat_buf = STRBUF_INIT; 92struct strbuf str_buf = STRBUF_INIT; 93const char*use_pat = pattern; 94const char*use_str = string; 95 96if(pattern[patternlen]) { 97strbuf_add(&pat_buf, pattern, patternlen); 98 use_pat = pat_buf.buf; 99} 100if(string[stringlen]) { 101strbuf_add(&str_buf, string, stringlen); 102 use_str = str_buf.buf; 103} 104 105if(ignore_case) 106 flags |= WM_CASEFOLD; 107 match_status =wildmatch(use_pat, use_str, flags, NULL); 108 109strbuf_release(&pat_buf); 110strbuf_release(&str_buf); 111 112return match_status; 113} 114 115static size_tcommon_prefix_len(const struct pathspec *pathspec) 116{ 117int n; 118size_t max =0; 119 120/* 121 * ":(icase)path" is treated as a pathspec full of 122 * wildcard. In other words, only prefix is considered common 123 * prefix. If the pathspec is abc/foo abc/bar, running in 124 * subdir xyz, the common prefix is still xyz, not xuz/abc as 125 * in non-:(icase). 126 */ 127GUARD_PATHSPEC(pathspec, 128 PATHSPEC_FROMTOP | 129 PATHSPEC_MAXDEPTH | 130 PATHSPEC_LITERAL | 131 PATHSPEC_GLOB | 132 PATHSPEC_ICASE | 133 PATHSPEC_EXCLUDE); 134 135for(n =0; n < pathspec->nr; n++) { 136size_t i =0, len =0, item_len; 137if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 138continue; 139if(pathspec->items[n].magic & PATHSPEC_ICASE) 140 item_len = pathspec->items[n].prefix; 141else 142 item_len = pathspec->items[n].nowildcard_len; 143while(i < item_len && (n ==0|| i < max)) { 144char c = pathspec->items[n].match[i]; 145if(c != pathspec->items[0].match[i]) 146break; 147if(c =='/') 148 len = i +1; 149 i++; 150} 151if(n ==0|| len < max) { 152 max = len; 153if(!max) 154break; 155} 156} 157return max; 158} 159 160/* 161 * Returns a copy of the longest leading path common among all 162 * pathspecs. 163 */ 164char*common_prefix(const struct pathspec *pathspec) 165{ 166unsigned long len =common_prefix_len(pathspec); 167 168return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 169} 170 171intfill_directory(struct dir_struct *dir,const struct pathspec *pathspec) 172{ 173size_t len; 174 175/* 176 * Calculate common prefix for the pathspec, and 177 * use that to optimize the directory walk 178 */ 179 len =common_prefix_len(pathspec); 180 181/* Read the directory and prune it */ 182read_directory(dir, pathspec->nr ? pathspec->_raw[0] :"", len, pathspec); 183return len; 184} 185 186intwithin_depth(const char*name,int namelen, 187int depth,int max_depth) 188{ 189const char*cp = name, *cpe = name + namelen; 190 191while(cp < cpe) { 192if(*cp++ !='/') 193continue; 194 depth++; 195if(depth > max_depth) 196return0; 197} 198return1; 199} 200 201#define DO_MATCH_EXCLUDE 1 202#define DO_MATCH_DIRECTORY 2 203 204/* 205 * Does 'match' match the given name? 206 * A match is found if 207 * 208 * (1) the 'match' string is leading directory of 'name', or 209 * (2) the 'match' string is a wildcard and matches 'name', or 210 * (3) the 'match' string is exactly the same as 'name'. 211 * 212 * and the return value tells which case it was. 213 * 214 * It returns 0 when there is no match. 215 */ 216static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 217const char*name,int namelen,unsigned flags) 218{ 219/* name/namelen has prefix cut off by caller */ 220const char*match = item->match + prefix; 221int matchlen = item->len - prefix; 222 223/* 224 * The normal call pattern is: 225 * 1. prefix = common_prefix_len(ps); 226 * 2. prune something, or fill_directory 227 * 3. match_pathspec() 228 * 229 * 'prefix' at #1 may be shorter than the command's prefix and 230 * it's ok for #2 to match extra files. Those extras will be 231 * trimmed at #3. 232 * 233 * Suppose the pathspec is 'foo' and '../bar' running from 234 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 235 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 236 * user does not want XYZ/foo, only the "foo" part should be 237 * case-insensitive. We need to filter out XYZ/foo here. In 238 * other words, we do not trust the caller on comparing the 239 * prefix part when :(icase) is involved. We do exact 240 * comparison ourselves. 241 * 242 * Normally the caller (common_prefix_len() in fact) does 243 * _exact_ matching on name[-prefix+1..-1] and we do not need 244 * to check that part. Be defensive and check it anyway, in 245 * case common_prefix_len is changed, or a new caller is 246 * introduced that does not use common_prefix_len. 247 * 248 * If the penalty turns out too high when prefix is really 249 * long, maybe change it to 250 * strncmp(match, name, item->prefix - prefix) 251 */ 252if(item->prefix && (item->magic & PATHSPEC_ICASE) && 253strncmp(item->match, name - prefix, item->prefix)) 254return0; 255 256/* If the match was just the prefix, we matched */ 257if(!*match) 258return MATCHED_RECURSIVELY; 259 260if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 261if(matchlen == namelen) 262return MATCHED_EXACTLY; 263 264if(match[matchlen-1] =='/'|| name[matchlen] =='/') 265return MATCHED_RECURSIVELY; 266}else if((flags & DO_MATCH_DIRECTORY) && 267 match[matchlen -1] =='/'&& 268 namelen == matchlen -1&& 269!ps_strncmp(item, match, name, namelen)) 270return MATCHED_EXACTLY; 271 272if(item->nowildcard_len < item->len && 273!git_fnmatch(item, match, name, 274 item->nowildcard_len - prefix)) 275return MATCHED_FNMATCH; 276 277return0; 278} 279 280/* 281 * Given a name and a list of pathspecs, returns the nature of the 282 * closest (i.e. most specific) match of the name to any of the 283 * pathspecs. 284 * 285 * The caller typically calls this multiple times with the same 286 * pathspec and seen[] array but with different name/namelen 287 * (e.g. entries from the index) and is interested in seeing if and 288 * how each pathspec matches all the names it calls this function 289 * with. A mark is left in the seen[] array for each pathspec element 290 * indicating the closest type of match that element achieved, so if 291 * seen[n] remains zero after multiple invocations, that means the nth 292 * pathspec did not match any names, which could indicate that the 293 * user mistyped the nth pathspec. 294 */ 295static intdo_match_pathspec(const struct pathspec *ps, 296const char*name,int namelen, 297int prefix,char*seen, 298unsigned flags) 299{ 300int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 301 302GUARD_PATHSPEC(ps, 303 PATHSPEC_FROMTOP | 304 PATHSPEC_MAXDEPTH | 305 PATHSPEC_LITERAL | 306 PATHSPEC_GLOB | 307 PATHSPEC_ICASE | 308 PATHSPEC_EXCLUDE); 309 310if(!ps->nr) { 311if(!ps->recursive || 312!(ps->magic & PATHSPEC_MAXDEPTH) || 313 ps->max_depth == -1) 314return MATCHED_RECURSIVELY; 315 316if(within_depth(name, namelen,0, ps->max_depth)) 317return MATCHED_EXACTLY; 318else 319return0; 320} 321 322 name += prefix; 323 namelen -= prefix; 324 325for(i = ps->nr -1; i >=0; i--) { 326int how; 327 328if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 329( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 330continue; 331 332if(seen && seen[i] == MATCHED_EXACTLY) 333continue; 334/* 335 * Make exclude patterns optional and never report 336 * "pathspec ':(exclude)foo' matches no files" 337 */ 338if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 339 seen[i] = MATCHED_FNMATCH; 340 how =match_pathspec_item(ps->items+i, prefix, name, 341 namelen, flags); 342if(ps->recursive && 343(ps->magic & PATHSPEC_MAXDEPTH) && 344 ps->max_depth != -1&& 345 how && how != MATCHED_FNMATCH) { 346int len = ps->items[i].len; 347if(name[len] =='/') 348 len++; 349if(within_depth(name+len, namelen-len,0, ps->max_depth)) 350 how = MATCHED_EXACTLY; 351else 352 how =0; 353} 354if(how) { 355if(retval < how) 356 retval = how; 357if(seen && seen[i] < how) 358 seen[i] = how; 359} 360} 361return retval; 362} 363 364intmatch_pathspec(const struct pathspec *ps, 365const char*name,int namelen, 366int prefix,char*seen,int is_dir) 367{ 368int positive, negative; 369unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 370 positive =do_match_pathspec(ps, name, namelen, 371 prefix, seen, flags); 372if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 373return positive; 374 negative =do_match_pathspec(ps, name, namelen, 375 prefix, seen, 376 flags | DO_MATCH_EXCLUDE); 377return negative ?0: positive; 378} 379 380intreport_path_error(const char*ps_matched, 381const struct pathspec *pathspec, 382const char*prefix) 383{ 384/* 385 * Make sure all pathspec matched; otherwise it is an error. 386 */ 387struct strbuf sb = STRBUF_INIT; 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} 419strbuf_release(&sb); 420return errors; 421} 422 423/* 424 * Return the length of the "simple" part of a path match limiter. 425 */ 426intsimple_length(const char*match) 427{ 428int len = -1; 429 430for(;;) { 431unsigned char c = *match++; 432 len++; 433if(c =='\0'||is_glob_special(c)) 434return len; 435} 436} 437 438intno_wildcard(const char*string) 439{ 440return string[simple_length(string)] =='\0'; 441} 442 443voidparse_exclude_pattern(const char**pattern, 444int*patternlen, 445int*flags, 446int*nowildcardlen) 447{ 448const char*p = *pattern; 449size_t i, len; 450 451*flags =0; 452if(*p =='!') { 453*flags |= EXC_FLAG_NEGATIVE; 454 p++; 455} 456 len =strlen(p); 457if(len && p[len -1] =='/') { 458 len--; 459*flags |= EXC_FLAG_MUSTBEDIR; 460} 461for(i =0; i < len; i++) { 462if(p[i] =='/') 463break; 464} 465if(i == len) 466*flags |= EXC_FLAG_NODIR; 467*nowildcardlen =simple_length(p); 468/* 469 * we should have excluded the trailing slash from 'p' too, 470 * but that's one more allocation. Instead just make sure 471 * nowildcardlen does not exceed real patternlen 472 */ 473if(*nowildcardlen > len) 474*nowildcardlen = len; 475if(*p =='*'&&no_wildcard(p +1)) 476*flags |= EXC_FLAG_ENDSWITH; 477*pattern = p; 478*patternlen = len; 479} 480 481voidadd_exclude(const char*string,const char*base, 482int baselen,struct exclude_list *el,int srcpos) 483{ 484struct exclude *x; 485int patternlen; 486int flags; 487int nowildcardlen; 488 489parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 490if(flags & EXC_FLAG_MUSTBEDIR) { 491char*s; 492 x =xmalloc(sizeof(*x) + patternlen +1); 493 s = (char*)(x+1); 494memcpy(s, string, patternlen); 495 s[patternlen] ='\0'; 496 x->pattern = s; 497}else{ 498 x =xmalloc(sizeof(*x)); 499 x->pattern = string; 500} 501 x->patternlen = patternlen; 502 x->nowildcardlen = nowildcardlen; 503 x->base = base; 504 x->baselen = baselen; 505 x->flags = flags; 506 x->srcpos = srcpos; 507ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 508 el->excludes[el->nr++] = x; 509 x->el = el; 510} 511 512static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 513{ 514int pos, len; 515unsigned long sz; 516enum object_type type; 517void*data; 518 519 len =strlen(path); 520 pos =cache_name_pos(path, len); 521if(pos <0) 522return NULL; 523if(!ce_skip_worktree(active_cache[pos])) 524return NULL; 525 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 526if(!data || type != OBJ_BLOB) { 527free(data); 528return NULL; 529} 530*size =xsize_t(sz); 531return data; 532} 533 534/* 535 * Frees memory within el which was allocated for exclude patterns and 536 * the file buffer. Does not free el itself. 537 */ 538voidclear_exclude_list(struct exclude_list *el) 539{ 540int i; 541 542for(i =0; i < el->nr; i++) 543free(el->excludes[i]); 544free(el->excludes); 545free(el->filebuf); 546 547 el->nr =0; 548 el->excludes = NULL; 549 el->filebuf = NULL; 550} 551 552static voidtrim_trailing_spaces(char*buf) 553{ 554char*p, *last_space = NULL; 555 556for(p = buf; *p; p++) 557switch(*p) { 558case' ': 559if(!last_space) 560 last_space = p; 561break; 562case'\\': 563 p++; 564if(!*p) 565return; 566/* fallthrough */ 567default: 568 last_space = NULL; 569} 570 571if(last_space) 572*last_space ='\0'; 573} 574 575intadd_excludes_from_file_to_list(const char*fname, 576const char*base, 577int baselen, 578struct exclude_list *el, 579int check_index) 580{ 581struct stat st; 582int fd, i, lineno =1; 583size_t size =0; 584char*buf, *entry; 585 586 fd =open(fname, O_RDONLY); 587if(fd <0||fstat(fd, &st) <0) { 588if(errno != ENOENT) 589warn_on_inaccessible(fname); 590if(0<= fd) 591close(fd); 592if(!check_index || 593(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 594return-1; 595if(size ==0) { 596free(buf); 597return0; 598} 599if(buf[size-1] !='\n') { 600 buf =xrealloc(buf, size+1); 601 buf[size++] ='\n'; 602} 603}else{ 604 size =xsize_t(st.st_size); 605if(size ==0) { 606close(fd); 607return0; 608} 609 buf =xmalloc(size+1); 610if(read_in_full(fd, buf, size) != size) { 611free(buf); 612close(fd); 613return-1; 614} 615 buf[size++] ='\n'; 616close(fd); 617} 618 619 el->filebuf = buf; 620 entry = buf; 621for(i =0; i < size; i++) { 622if(buf[i] =='\n') { 623if(entry != buf + i && entry[0] !='#') { 624 buf[i - (i && buf[i-1] =='\r')] =0; 625trim_trailing_spaces(entry); 626add_exclude(entry, base, baselen, el, lineno); 627} 628 lineno++; 629 entry = buf + i +1; 630} 631} 632return0; 633} 634 635struct exclude_list *add_exclude_list(struct dir_struct *dir, 636int group_type,const char*src) 637{ 638struct exclude_list *el; 639struct exclude_list_group *group; 640 641 group = &dir->exclude_list_group[group_type]; 642ALLOC_GROW(group->el, group->nr +1, group->alloc); 643 el = &group->el[group->nr++]; 644memset(el,0,sizeof(*el)); 645 el->src = src; 646return el; 647} 648 649/* 650 * Used to set up core.excludesfile and .git/info/exclude lists. 651 */ 652voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 653{ 654struct exclude_list *el; 655 el =add_exclude_list(dir, EXC_FILE, fname); 656if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 657die("cannot use%sas an exclude file", fname); 658} 659 660intmatch_basename(const char*basename,int basenamelen, 661const char*pattern,int prefix,int patternlen, 662int flags) 663{ 664if(prefix == patternlen) { 665if(patternlen == basenamelen && 666!strncmp_icase(pattern, basename, basenamelen)) 667return1; 668}else if(flags & EXC_FLAG_ENDSWITH) { 669/* "*literal" matching against "fooliteral" */ 670if(patternlen -1<= basenamelen && 671!strncmp_icase(pattern +1, 672 basename + basenamelen - (patternlen -1), 673 patternlen -1)) 674return1; 675}else{ 676if(fnmatch_icase_mem(pattern, patternlen, 677 basename, basenamelen, 6780) ==0) 679return1; 680} 681return0; 682} 683 684intmatch_pathname(const char*pathname,int pathlen, 685const char*base,int baselen, 686const char*pattern,int prefix,int patternlen, 687int flags) 688{ 689const char*name; 690int namelen; 691 692/* 693 * match with FNM_PATHNAME; the pattern has base implicitly 694 * in front of it. 695 */ 696if(*pattern =='/') { 697 pattern++; 698 patternlen--; 699 prefix--; 700} 701 702/* 703 * baselen does not count the trailing slash. base[] may or 704 * may not end with a trailing slash though. 705 */ 706if(pathlen < baselen +1|| 707(baselen && pathname[baselen] !='/') || 708strncmp_icase(pathname, base, baselen)) 709return0; 710 711 namelen = baselen ? pathlen - baselen -1: pathlen; 712 name = pathname + pathlen - namelen; 713 714if(prefix) { 715/* 716 * if the non-wildcard part is longer than the 717 * remaining pathname, surely it cannot match. 718 */ 719if(prefix > namelen) 720return0; 721 722if(strncmp_icase(pattern, name, prefix)) 723return0; 724 pattern += prefix; 725 patternlen -= prefix; 726 name += prefix; 727 namelen -= prefix; 728 729/* 730 * If the whole pattern did not have a wildcard, 731 * then our prefix match is all we need; we 732 * do not need to call fnmatch at all. 733 */ 734if(!patternlen && !namelen) 735return1; 736/* 737 * This can happen when we ignore some exclude rules 738 * on directories in other to see if negative rules 739 * may match. E.g. 740 * 741 * /abc 742 * !/abc/def/ghi 743 * 744 * The pattern of interest is "/abc". On the first 745 * try, we should match path "abc" with this pattern 746 * in the "if" statement right above, but the caller 747 * ignores it. 748 * 749 * On the second try with paths within "abc", 750 * e.g. "abc/xyz", we come here and try to match it 751 * with "/abc". 752 */ 753if(!patternlen && namelen && *name =='/') 754return1; 755} 756 757returnfnmatch_icase_mem(pattern, patternlen, 758 name, namelen, 759 WM_PATHNAME) ==0; 760} 761 762/* 763 * Return non-zero if pathname is a directory and an ancestor of the 764 * literal path in a (negative) pattern. This is used to keep 765 * descending in "foo" and "foo/bar" when the pattern is 766 * "!foo/bar/.gitignore". "foo/notbar" will not be descended however. 767 */ 768static intmatch_neg_path(const char*pathname,int pathlen,int*dtype, 769const char*base,int baselen, 770const char*pattern,int prefix,int patternlen, 771int flags) 772{ 773assert((flags & EXC_FLAG_NEGATIVE) && !(flags & EXC_FLAG_NODIR)); 774 775if(*dtype == DT_UNKNOWN) 776*dtype =get_dtype(NULL, pathname, pathlen); 777if(*dtype != DT_DIR) 778return0; 779 780if(*pattern =='/') { 781 pattern++; 782 patternlen--; 783 prefix--; 784} 785 786if(baselen) { 787if(((pathlen < baselen && base[pathlen] =='/') || 788 pathlen == baselen) && 789!strncmp_icase(pathname, base, pathlen)) 790return1; 791 pathname += baselen +1; 792 pathlen -= baselen +1; 793} 794 795 796if(prefix && 797((pathlen < prefix && pattern[pathlen] =='/') && 798!strncmp_icase(pathname, pattern, pathlen))) 799return1; 800 801return0; 802} 803 804/* 805 * Scan the given exclude list in reverse to see whether pathname 806 * should be ignored. The first match (i.e. the last on the list), if 807 * any, determines the fate. Returns the exclude_list element which 808 * matched, or NULL for undecided. 809 */ 810static struct exclude *last_exclude_matching_from_list(const char*pathname, 811int pathlen, 812const char*basename, 813int*dtype, 814struct exclude_list *el) 815{ 816struct exclude *exc = NULL;/* undecided */ 817int i, matched_negative_path =0; 818 819if(!el->nr) 820return NULL;/* undefined */ 821 822for(i = el->nr -1;0<= i; i--) { 823struct exclude *x = el->excludes[i]; 824const char*exclude = x->pattern; 825int prefix = x->nowildcardlen; 826 827if(x->flags & EXC_FLAG_MUSTBEDIR) { 828if(*dtype == DT_UNKNOWN) 829*dtype =get_dtype(NULL, pathname, pathlen); 830if(*dtype != DT_DIR) 831continue; 832} 833 834if(x->flags & EXC_FLAG_NODIR) { 835if(match_basename(basename, 836 pathlen - (basename - pathname), 837 exclude, prefix, x->patternlen, 838 x->flags)) { 839 exc = x; 840break; 841} 842continue; 843} 844 845assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 846if(match_pathname(pathname, pathlen, 847 x->base, x->baselen ? x->baselen -1:0, 848 exclude, prefix, x->patternlen, x->flags)) { 849 exc = x; 850break; 851} 852 853if((x->flags & EXC_FLAG_NEGATIVE) && !matched_negative_path && 854match_neg_path(pathname, pathlen, dtype, x->base, 855 x->baselen ? x->baselen -1:0, 856 exclude, prefix, x->patternlen, x->flags)) 857 matched_negative_path =1; 858} 859if(exc && 860!(exc->flags & EXC_FLAG_NEGATIVE) && 861!(exc->flags & EXC_FLAG_NODIR) && 862 matched_negative_path) 863 exc = NULL; 864return exc; 865} 866 867/* 868 * Scan the list and let the last match determine the fate. 869 * Return 1 for exclude, 0 for include and -1 for undecided. 870 */ 871intis_excluded_from_list(const char*pathname, 872int pathlen,const char*basename,int*dtype, 873struct exclude_list *el) 874{ 875struct exclude *exclude; 876 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 877if(exclude) 878return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 879return-1;/* undecided */ 880} 881 882static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 883const char*pathname,int pathlen,const char*basename, 884int*dtype_p) 885{ 886int i, j; 887struct exclude_list_group *group; 888struct exclude *exclude; 889for(i = EXC_CMDL; i <= EXC_FILE; i++) { 890 group = &dir->exclude_list_group[i]; 891for(j = group->nr -1; j >=0; j--) { 892 exclude =last_exclude_matching_from_list( 893 pathname, pathlen, basename, dtype_p, 894&group->el[j]); 895if(exclude) 896return exclude; 897} 898} 899return NULL; 900} 901 902/* 903 * Loads the per-directory exclude list for the substring of base 904 * which has a char length of baselen. 905 */ 906static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 907{ 908struct exclude_list_group *group; 909struct exclude_list *el; 910struct exclude_stack *stk = NULL; 911int current; 912 913 group = &dir->exclude_list_group[EXC_DIRS]; 914 915/* 916 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 917 * which originate from directories not in the prefix of the 918 * path being checked. 919 */ 920while((stk = dir->exclude_stack) != NULL) { 921if(stk->baselen <= baselen && 922!strncmp(dir->basebuf.buf, base, stk->baselen)) 923break; 924 el = &group->el[dir->exclude_stack->exclude_ix]; 925 dir->exclude_stack = stk->prev; 926 dir->exclude = NULL; 927free((char*)el->src);/* see strbuf_detach() below */ 928clear_exclude_list(el); 929free(stk); 930 group->nr--; 931} 932 933/* Skip traversing into sub directories if the parent is excluded */ 934if(dir->exclude) 935return; 936 937/* 938 * Lazy initialization. All call sites currently just 939 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 940 * them seems lots of work for little benefit. 941 */ 942if(!dir->basebuf.buf) 943strbuf_init(&dir->basebuf, PATH_MAX); 944 945/* Read from the parent directories and push them down. */ 946 current = stk ? stk->baselen : -1; 947strbuf_setlen(&dir->basebuf, current <0?0: current); 948while(current < baselen) { 949const char*cp; 950 951 stk =xcalloc(1,sizeof(*stk)); 952if(current <0) { 953 cp = base; 954 current =0; 955}else{ 956 cp =strchr(base + current +1,'/'); 957if(!cp) 958die("oops in prep_exclude"); 959 cp++; 960} 961 stk->prev = dir->exclude_stack; 962 stk->baselen = cp - base; 963 stk->exclude_ix = group->nr; 964 el =add_exclude_list(dir, EXC_DIRS, NULL); 965strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 966assert(stk->baselen == dir->basebuf.len); 967 968/* Abort if the directory is excluded */ 969if(stk->baselen) { 970int dt = DT_DIR; 971 dir->basebuf.buf[stk->baselen -1] =0; 972 dir->exclude =last_exclude_matching_from_lists(dir, 973 dir->basebuf.buf, stk->baselen -1, 974 dir->basebuf.buf + current, &dt); 975 dir->basebuf.buf[stk->baselen -1] ='/'; 976if(dir->exclude && 977 dir->exclude->flags & EXC_FLAG_NEGATIVE) 978 dir->exclude = NULL; 979if(dir->exclude) { 980 dir->exclude_stack = stk; 981return; 982} 983} 984 985/* Try to read per-directory file */ 986if(dir->exclude_per_dir) { 987/* 988 * dir->basebuf gets reused by the traversal, but we 989 * need fname to remain unchanged to ensure the src 990 * member of each struct exclude correctly 991 * back-references its source file. Other invocations 992 * of add_exclude_list provide stable strings, so we 993 * strbuf_detach() and free() here in the caller. 994 */ 995struct strbuf sb = STRBUF_INIT; 996strbuf_addbuf(&sb, &dir->basebuf); 997strbuf_addstr(&sb, dir->exclude_per_dir); 998 el->src =strbuf_detach(&sb, NULL); 999add_excludes_from_file_to_list(el->src, el->src,1000 stk->baselen, el,1);1001}1002 dir->exclude_stack = stk;1003 current = stk->baselen;1004}1005strbuf_setlen(&dir->basebuf, baselen);1006}10071008/*1009 * Loads the exclude lists for the directory containing pathname, then1010 * scans all exclude lists to determine whether pathname is excluded.1011 * Returns the exclude_list element which matched, or NULL for1012 * undecided.1013 */1014struct exclude *last_exclude_matching(struct dir_struct *dir,1015const char*pathname,1016int*dtype_p)1017{1018int pathlen =strlen(pathname);1019const char*basename =strrchr(pathname,'/');1020 basename = (basename) ? basename+1: pathname;10211022prep_exclude(dir, pathname, basename-pathname);10231024if(dir->exclude)1025return dir->exclude;10261027returnlast_exclude_matching_from_lists(dir, pathname, pathlen,1028 basename, dtype_p);1029}10301031/*1032 * Loads the exclude lists for the directory containing pathname, then1033 * scans all exclude lists to determine whether pathname is excluded.1034 * Returns 1 if true, otherwise 0.1035 */1036intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p)1037{1038struct exclude *exclude =1039last_exclude_matching(dir, pathname, dtype_p);1040if(exclude)1041return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1042return0;1043}10441045static struct dir_entry *dir_entry_new(const char*pathname,int len)1046{1047struct dir_entry *ent;10481049 ent =xmalloc(sizeof(*ent) + len +1);1050 ent->len = len;1051memcpy(ent->name, pathname, len);1052 ent->name[len] =0;1053return ent;1054}10551056static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len)1057{1058if(cache_file_exists(pathname, len, ignore_case))1059return NULL;10601061ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1062return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1063}10641065struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len)1066{1067if(!cache_name_is_other(pathname, len))1068return NULL;10691070ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1071return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1072}10731074enum exist_status {1075 index_nonexistent =0,1076 index_directory,1077 index_gitdir1078};10791080/*1081 * Do not use the alphabetically sorted index to look up1082 * the directory name; instead, use the case insensitive1083 * directory hash.1084 */1085static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1086{1087const struct cache_entry *ce =cache_dir_exists(dirname, len);1088unsigned char endchar;10891090if(!ce)1091return index_nonexistent;1092 endchar = ce->name[len];10931094/*1095 * The cache_entry structure returned will contain this dirname1096 * and possibly additional path components.1097 */1098if(endchar =='/')1099return index_directory;11001101/*1102 * If there are no additional path components, then this cache_entry1103 * represents a submodule. Submodules, despite being directories,1104 * are stored in the cache without a closing slash.1105 */1106if(!endchar &&S_ISGITLINK(ce->ce_mode))1107return index_gitdir;11081109/* This should never be hit, but it exists just in case. */1110return index_nonexistent;1111}11121113/*1114 * The index sorts alphabetically by entry name, which1115 * means that a gitlink sorts as '\0' at the end, while1116 * a directory (which is defined not as an entry, but as1117 * the files it contains) will sort with the '/' at the1118 * end.1119 */1120static enum exist_status directory_exists_in_index(const char*dirname,int len)1121{1122int pos;11231124if(ignore_case)1125returndirectory_exists_in_index_icase(dirname, len);11261127 pos =cache_name_pos(dirname, len);1128if(pos <0)1129 pos = -pos-1;1130while(pos < active_nr) {1131const struct cache_entry *ce = active_cache[pos++];1132unsigned char endchar;11331134if(strncmp(ce->name, dirname, len))1135break;1136 endchar = ce->name[len];1137if(endchar >'/')1138break;1139if(endchar =='/')1140return index_directory;1141if(!endchar &&S_ISGITLINK(ce->ce_mode))1142return index_gitdir;1143}1144return index_nonexistent;1145}11461147/*1148 * When we find a directory when traversing the filesystem, we1149 * have three distinct cases:1150 *1151 * - ignore it1152 * - see it as a directory1153 * - recurse into it1154 *1155 * and which one we choose depends on a combination of existing1156 * git index contents and the flags passed into the directory1157 * traversal routine.1158 *1159 * Case 1: If we *already* have entries in the index under that1160 * directory name, we always recurse into the directory to see1161 * all the files.1162 *1163 * Case 2: If we *already* have that directory name as a gitlink,1164 * we always continue to see it as a gitlink, regardless of whether1165 * there is an actual git directory there or not (it might not1166 * be checked out as a subproject!)1167 *1168 * Case 3: if we didn't have it in the index previously, we1169 * have a few sub-cases:1170 *1171 * (a) if "show_other_directories" is true, we show it as1172 * just a directory, unless "hide_empty_directories" is1173 * also true, in which case we need to check if it contains any1174 * untracked and / or ignored files.1175 * (b) if it looks like a git directory, and we don't have1176 * 'no_gitlinks' set we treat it as a gitlink, and show it1177 * as a directory.1178 * (c) otherwise, we recurse into it.1179 */1180static enum path_treatment treat_directory(struct dir_struct *dir,1181const char*dirname,int len,int exclude,1182const struct path_simplify *simplify)1183{1184/* The "len-1" is to strip the final '/' */1185switch(directory_exists_in_index(dirname, len-1)) {1186case index_directory:1187return path_recurse;11881189case index_gitdir:1190return path_none;11911192case index_nonexistent:1193if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1194break;1195if(!(dir->flags & DIR_NO_GITLINKS)) {1196unsigned char sha1[20];1197if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1198return path_untracked;1199}1200return path_recurse;1201}12021203/* This is the "show_other_directories" case */12041205if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1206return exclude ? path_excluded : path_untracked;12071208returnread_directory_recursive(dir, dirname, len,1, simplify);1209}12101211/*1212 * This is an inexact early pruning of any recursive directory1213 * reading - if the path cannot possibly be in the pathspec,1214 * return true, and we'll skip it early.1215 */1216static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1217{1218if(simplify) {1219for(;;) {1220const char*match = simplify->path;1221int len = simplify->len;12221223if(!match)1224break;1225if(len > pathlen)1226 len = pathlen;1227if(!memcmp(path, match, len))1228return0;1229 simplify++;1230}1231return1;1232}1233return0;1234}12351236/*1237 * This function tells us whether an excluded path matches a1238 * list of "interesting" pathspecs. That is, whether a path matched1239 * by any of the pathspecs could possibly be ignored by excluding1240 * the specified path. This can happen if:1241 *1242 * 1. the path is mentioned explicitly in the pathspec1243 *1244 * 2. the path is a directory prefix of some element in the1245 * pathspec1246 */1247static intexclude_matches_pathspec(const char*path,int len,1248const struct path_simplify *simplify)1249{1250if(simplify) {1251for(; simplify->path; simplify++) {1252if(len == simplify->len1253&& !memcmp(path, simplify->path, len))1254return1;1255if(len < simplify->len1256&& simplify->path[len] =='/'1257&& !memcmp(path, simplify->path, len))1258return1;1259}1260}1261return0;1262}12631264static intget_index_dtype(const char*path,int len)1265{1266int pos;1267const struct cache_entry *ce;12681269 ce =cache_file_exists(path, len,0);1270if(ce) {1271if(!ce_uptodate(ce))1272return DT_UNKNOWN;1273if(S_ISGITLINK(ce->ce_mode))1274return DT_DIR;1275/*1276 * Nobody actually cares about the1277 * difference between DT_LNK and DT_REG1278 */1279return DT_REG;1280}12811282/* Try to look it up as a directory */1283 pos =cache_name_pos(path, len);1284if(pos >=0)1285return DT_UNKNOWN;1286 pos = -pos-1;1287while(pos < active_nr) {1288 ce = active_cache[pos++];1289if(strncmp(ce->name, path, len))1290break;1291if(ce->name[len] >'/')1292break;1293if(ce->name[len] <'/')1294continue;1295if(!ce_uptodate(ce))1296break;/* continue? */1297return DT_DIR;1298}1299return DT_UNKNOWN;1300}13011302static intget_dtype(struct dirent *de,const char*path,int len)1303{1304int dtype = de ?DTYPE(de) : DT_UNKNOWN;1305struct stat st;13061307if(dtype != DT_UNKNOWN)1308return dtype;1309 dtype =get_index_dtype(path, len);1310if(dtype != DT_UNKNOWN)1311return dtype;1312if(lstat(path, &st))1313return dtype;1314if(S_ISREG(st.st_mode))1315return DT_REG;1316if(S_ISDIR(st.st_mode))1317return DT_DIR;1318if(S_ISLNK(st.st_mode))1319return DT_LNK;1320return dtype;1321}13221323static enum path_treatment treat_one_path(struct dir_struct *dir,1324struct strbuf *path,1325const struct path_simplify *simplify,1326int dtype,struct dirent *de)1327{1328int exclude;1329int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);13301331if(dtype == DT_UNKNOWN)1332 dtype =get_dtype(de, path->buf, path->len);13331334/* Always exclude indexed files */1335if(dtype != DT_DIR && has_path_in_index)1336return path_none;13371338/*1339 * When we are looking at a directory P in the working tree,1340 * there are three cases:1341 *1342 * (1) P exists in the index. Everything inside the directory P in1343 * the working tree needs to go when P is checked out from the1344 * index.1345 *1346 * (2) P does not exist in the index, but there is P/Q in the index.1347 * We know P will stay a directory when we check out the contents1348 * of the index, but we do not know yet if there is a directory1349 * P/Q in the working tree to be killed, so we need to recurse.1350 *1351 * (3) P does not exist in the index, and there is no P/Q in the index1352 * to require P to be a directory, either. Only in this case, we1353 * know that everything inside P will not be killed without1354 * recursing.1355 */1356if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1357(dtype == DT_DIR) &&1358!has_path_in_index &&1359(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1360return path_none;13611362 exclude =is_excluded(dir, path->buf, &dtype);13631364/*1365 * Excluded? If we don't explicitly want to show1366 * ignored files, ignore it1367 */1368if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1369return path_excluded;13701371switch(dtype) {1372default:1373return path_none;1374case DT_DIR:1375strbuf_addch(path,'/');1376returntreat_directory(dir, path->buf, path->len, exclude,1377 simplify);1378case DT_REG:1379case DT_LNK:1380return exclude ? path_excluded : path_untracked;1381}1382}13831384static enum path_treatment treat_path(struct dir_struct *dir,1385struct dirent *de,1386struct strbuf *path,1387int baselen,1388const struct path_simplify *simplify)1389{1390int dtype;13911392if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1393return path_none;1394strbuf_setlen(path, baselen);1395strbuf_addstr(path, de->d_name);1396if(simplify_away(path->buf, path->len, simplify))1397return path_none;13981399 dtype =DTYPE(de);1400returntreat_one_path(dir, path, simplify, dtype, de);1401}14021403/*1404 * Read a directory tree. We currently ignore anything but1405 * directories, regular files and symlinks. That's because git1406 * doesn't handle them at all yet. Maybe that will change some1407 * day.1408 *1409 * Also, we ignore the name ".git" (even if it is not a directory).1410 * That likely will not change.1411 *1412 * Returns the most significant path_treatment value encountered in the scan.1413 */1414static enum path_treatment read_directory_recursive(struct dir_struct *dir,1415const char*base,int baselen,1416int check_only,1417const struct path_simplify *simplify)1418{1419DIR*fdir;1420enum path_treatment state, subdir_state, dir_state = path_none;1421struct dirent *de;1422struct strbuf path = STRBUF_INIT;14231424strbuf_add(&path, base, baselen);14251426 fdir =opendir(path.len ? path.buf :".");1427if(!fdir)1428goto out;14291430while((de =readdir(fdir)) != NULL) {1431/* check how the file or directory should be treated */1432 state =treat_path(dir, de, &path, baselen, simplify);1433if(state > dir_state)1434 dir_state = state;14351436/* recurse into subdir if instructed by treat_path */1437if(state == path_recurse) {1438 subdir_state =read_directory_recursive(dir, path.buf,1439 path.len, check_only, simplify);1440if(subdir_state > dir_state)1441 dir_state = subdir_state;1442}14431444if(check_only) {1445/* abort early if maximum state has been reached */1446if(dir_state == path_untracked)1447break;1448/* skip the dir_add_* part */1449continue;1450}14511452/* add the path to the appropriate result list */1453switch(state) {1454case path_excluded:1455if(dir->flags & DIR_SHOW_IGNORED)1456dir_add_name(dir, path.buf, path.len);1457else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1458((dir->flags & DIR_COLLECT_IGNORED) &&1459exclude_matches_pathspec(path.buf, path.len,1460 simplify)))1461dir_add_ignored(dir, path.buf, path.len);1462break;14631464case path_untracked:1465if(!(dir->flags & DIR_SHOW_IGNORED))1466dir_add_name(dir, path.buf, path.len);1467break;14681469default:1470break;1471}1472}1473closedir(fdir);1474 out:1475strbuf_release(&path);14761477return dir_state;1478}14791480static intcmp_name(const void*p1,const void*p2)1481{1482const struct dir_entry *e1 = *(const struct dir_entry **)p1;1483const struct dir_entry *e2 = *(const struct dir_entry **)p2;14841485returnname_compare(e1->name, e1->len, e2->name, e2->len);1486}14871488static struct path_simplify *create_simplify(const char**pathspec)1489{1490int nr, alloc =0;1491struct path_simplify *simplify = NULL;14921493if(!pathspec)1494return NULL;14951496for(nr =0; ; nr++) {1497const char*match;1498ALLOC_GROW(simplify, nr +1, alloc);1499 match = *pathspec++;1500if(!match)1501break;1502 simplify[nr].path = match;1503 simplify[nr].len =simple_length(match);1504}1505 simplify[nr].path = NULL;1506 simplify[nr].len =0;1507return simplify;1508}15091510static voidfree_simplify(struct path_simplify *simplify)1511{1512free(simplify);1513}15141515static inttreat_leading_path(struct dir_struct *dir,1516const char*path,int len,1517const struct path_simplify *simplify)1518{1519struct strbuf sb = STRBUF_INIT;1520int baselen, rc =0;1521const char*cp;1522int old_flags = dir->flags;15231524while(len && path[len -1] =='/')1525 len--;1526if(!len)1527return1;1528 baselen =0;1529 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1530while(1) {1531 cp = path + baselen + !!baselen;1532 cp =memchr(cp,'/', path + len - cp);1533if(!cp)1534 baselen = len;1535else1536 baselen = cp - path;1537strbuf_setlen(&sb,0);1538strbuf_add(&sb, path, baselen);1539if(!is_directory(sb.buf))1540break;1541if(simplify_away(sb.buf, sb.len, simplify))1542break;1543if(treat_one_path(dir, &sb, simplify,1544 DT_DIR, NULL) == path_none)1545break;/* do not recurse into it */1546if(len <= baselen) {1547 rc =1;1548break;/* finished checking */1549}1550}1551strbuf_release(&sb);1552 dir->flags = old_flags;1553return rc;1554}15551556intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1557{1558struct path_simplify *simplify;15591560/*1561 * Check out create_simplify()1562 */1563if(pathspec)1564GUARD_PATHSPEC(pathspec,1565 PATHSPEC_FROMTOP |1566 PATHSPEC_MAXDEPTH |1567 PATHSPEC_LITERAL |1568 PATHSPEC_GLOB |1569 PATHSPEC_ICASE |1570 PATHSPEC_EXCLUDE);15711572if(has_symlink_leading_path(path, len))1573return dir->nr;15741575/*1576 * exclude patterns are treated like positive ones in1577 * create_simplify. Usually exclude patterns should be a1578 * subset of positive ones, which has no impacts on1579 * create_simplify().1580 */1581 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1582if(!len ||treat_leading_path(dir, path, len, simplify))1583read_directory_recursive(dir, path, len,0, simplify);1584free_simplify(simplify);1585qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1586qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1587return dir->nr;1588}15891590intfile_exists(const char*f)1591{1592struct stat sb;1593returnlstat(f, &sb) ==0;1594}15951596/*1597 * Given two normalized paths (a trailing slash is ok), if subdir is1598 * outside dir, return -1. Otherwise return the offset in subdir that1599 * can be used as relative path to dir.1600 */1601intdir_inside_of(const char*subdir,const char*dir)1602{1603int offset =0;16041605assert(dir && subdir && *dir && *subdir);16061607while(*dir && *subdir && *dir == *subdir) {1608 dir++;1609 subdir++;1610 offset++;1611}16121613/* hel[p]/me vs hel[l]/yeah */1614if(*dir && *subdir)1615return-1;16161617if(!*subdir)1618return!*dir ? offset : -1;/* same dir */16191620/* foo/[b]ar vs foo/[] */1621if(is_dir_sep(dir[-1]))1622returnis_dir_sep(subdir[-1]) ? offset : -1;16231624/* foo[/]bar vs foo[] */1625returnis_dir_sep(*subdir) ? offset +1: -1;1626}16271628intis_inside_dir(const char*dir)1629{1630char*cwd;1631int rc;16321633if(!dir)1634return0;16351636 cwd =xgetcwd();1637 rc = (dir_inside_of(cwd, dir) >=0);1638free(cwd);1639return rc;1640}16411642intis_empty_dir(const char*path)1643{1644DIR*dir =opendir(path);1645struct dirent *e;1646int ret =1;16471648if(!dir)1649return0;16501651while((e =readdir(dir)) != NULL)1652if(!is_dot_or_dotdot(e->d_name)) {1653 ret =0;1654break;1655}16561657closedir(dir);1658return ret;1659}16601661static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1662{1663DIR*dir;1664struct dirent *e;1665int ret =0, original_len = path->len, len, kept_down =0;1666int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1667int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1668unsigned char submodule_head[20];16691670if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1671!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1672/* Do not descend and nuke a nested git work tree. */1673if(kept_up)1674*kept_up =1;1675return0;1676}16771678 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1679 dir =opendir(path->buf);1680if(!dir) {1681if(errno == ENOENT)1682return keep_toplevel ? -1:0;1683else if(errno == EACCES && !keep_toplevel)1684/*1685 * An empty dir could be removable even if it1686 * is unreadable:1687 */1688returnrmdir(path->buf);1689else1690return-1;1691}1692if(path->buf[original_len -1] !='/')1693strbuf_addch(path,'/');16941695 len = path->len;1696while((e =readdir(dir)) != NULL) {1697struct stat st;1698if(is_dot_or_dotdot(e->d_name))1699continue;17001701strbuf_setlen(path, len);1702strbuf_addstr(path, e->d_name);1703if(lstat(path->buf, &st)) {1704if(errno == ENOENT)1705/*1706 * file disappeared, which is what we1707 * wanted anyway1708 */1709continue;1710/* fall thru */1711}else if(S_ISDIR(st.st_mode)) {1712if(!remove_dir_recurse(path, flag, &kept_down))1713continue;/* happy */1714}else if(!only_empty &&1715(!unlink(path->buf) || errno == ENOENT)) {1716continue;/* happy, too */1717}17181719/* path too long, stat fails, or non-directory still exists */1720 ret = -1;1721break;1722}1723closedir(dir);17241725strbuf_setlen(path, original_len);1726if(!ret && !keep_toplevel && !kept_down)1727 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1728else if(kept_up)1729/*1730 * report the uplevel that it is not an error that we1731 * did not rmdir() our directory.1732 */1733*kept_up = !ret;1734return ret;1735}17361737intremove_dir_recursively(struct strbuf *path,int flag)1738{1739returnremove_dir_recurse(path, flag, NULL);1740}17411742voidsetup_standard_excludes(struct dir_struct *dir)1743{1744const char*path;1745char*xdg_path;17461747 dir->exclude_per_dir =".gitignore";1748 path =git_path("info/exclude");1749if(!excludes_file) {1750home_config_paths(NULL, &xdg_path,"ignore");1751 excludes_file = xdg_path;1752}1753if(!access_or_warn(path, R_OK,0))1754add_excludes_from_file(dir, path);1755if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1756add_excludes_from_file(dir, excludes_file);1757}17581759intremove_path(const char*name)1760{1761char*slash;17621763if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1764return-1;17651766 slash =strrchr(name,'/');1767if(slash) {1768char*dirs =xstrdup(name);1769 slash = dirs + (slash - name);1770do{1771*slash ='\0';1772}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1773free(dirs);1774}1775return0;1776}17771778/*1779 * Frees memory within dir which was allocated for exclude lists and1780 * the exclude_stack. Does not free dir itself.1781 */1782voidclear_directory(struct dir_struct *dir)1783{1784int i, j;1785struct exclude_list_group *group;1786struct exclude_list *el;1787struct exclude_stack *stk;17881789for(i = EXC_CMDL; i <= EXC_FILE; i++) {1790 group = &dir->exclude_list_group[i];1791for(j =0; j < group->nr; j++) {1792 el = &group->el[j];1793if(i == EXC_DIRS)1794free((char*)el->src);1795clear_exclude_list(el);1796}1797free(group->el);1798}17991800 stk = dir->exclude_stack;1801while(stk) {1802struct exclude_stack *prev = stk->prev;1803free(stk);1804 stk = prev;1805}1806strbuf_release(&dir->basebuf);1807}