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} 604else{ 605 size =xsize_t(st.st_size); 606if(size ==0) { 607close(fd); 608return0; 609} 610 buf =xmalloc(size+1); 611if(read_in_full(fd, buf, size) != size) { 612free(buf); 613close(fd); 614return-1; 615} 616 buf[size++] ='\n'; 617close(fd); 618} 619 620 el->filebuf = buf; 621 entry = buf; 622for(i =0; i < size; i++) { 623if(buf[i] =='\n') { 624if(entry != buf + i && entry[0] !='#') { 625 buf[i - (i && buf[i-1] =='\r')] =0; 626trim_trailing_spaces(entry); 627add_exclude(entry, base, baselen, el, lineno); 628} 629 lineno++; 630 entry = buf + i +1; 631} 632} 633return0; 634} 635 636struct exclude_list *add_exclude_list(struct dir_struct *dir, 637int group_type,const char*src) 638{ 639struct exclude_list *el; 640struct exclude_list_group *group; 641 642 group = &dir->exclude_list_group[group_type]; 643ALLOC_GROW(group->el, group->nr +1, group->alloc); 644 el = &group->el[group->nr++]; 645memset(el,0,sizeof(*el)); 646 el->src = src; 647return el; 648} 649 650/* 651 * Used to set up core.excludesfile and .git/info/exclude lists. 652 */ 653voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 654{ 655struct exclude_list *el; 656 el =add_exclude_list(dir, EXC_FILE, fname); 657if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 658die("cannot use%sas an exclude file", fname); 659} 660 661intmatch_basename(const char*basename,int basenamelen, 662const char*pattern,int prefix,int patternlen, 663int flags) 664{ 665if(prefix == patternlen) { 666if(patternlen == basenamelen && 667!strncmp_icase(pattern, basename, basenamelen)) 668return1; 669}else if(flags & EXC_FLAG_ENDSWITH) { 670/* "*literal" matching against "fooliteral" */ 671if(patternlen -1<= basenamelen && 672!strncmp_icase(pattern +1, 673 basename + basenamelen - (patternlen -1), 674 patternlen -1)) 675return1; 676}else{ 677if(fnmatch_icase_mem(pattern, patternlen, 678 basename, basenamelen, 6790) ==0) 680return1; 681} 682return0; 683} 684 685intmatch_pathname(const char*pathname,int pathlen, 686const char*base,int baselen, 687const char*pattern,int prefix,int patternlen, 688int flags) 689{ 690const char*name; 691int namelen; 692 693/* 694 * match with FNM_PATHNAME; the pattern has base implicitly 695 * in front of it. 696 */ 697if(*pattern =='/') { 698 pattern++; 699 patternlen--; 700 prefix--; 701} 702 703/* 704 * baselen does not count the trailing slash. base[] may or 705 * may not end with a trailing slash though. 706 */ 707if(pathlen < baselen +1|| 708(baselen && pathname[baselen] !='/') || 709strncmp_icase(pathname, base, baselen)) 710return0; 711 712 namelen = baselen ? pathlen - baselen -1: pathlen; 713 name = pathname + pathlen - namelen; 714 715if(prefix) { 716/* 717 * if the non-wildcard part is longer than the 718 * remaining pathname, surely it cannot match. 719 */ 720if(prefix > namelen) 721return0; 722 723if(strncmp_icase(pattern, name, prefix)) 724return0; 725 pattern += prefix; 726 patternlen -= prefix; 727 name += prefix; 728 namelen -= prefix; 729 730/* 731 * If the whole pattern did not have a wildcard, 732 * then our prefix match is all we need; we 733 * do not need to call fnmatch at all. 734 */ 735if(!patternlen && !namelen) 736return1; 737} 738 739returnfnmatch_icase_mem(pattern, patternlen, 740 name, namelen, 741 WM_PATHNAME) ==0; 742} 743 744/* 745 * Scan the given exclude list in reverse to see whether pathname 746 * should be ignored. The first match (i.e. the last on the list), if 747 * any, determines the fate. Returns the exclude_list element which 748 * matched, or NULL for undecided. 749 */ 750static struct exclude *last_exclude_matching_from_list(const char*pathname, 751int pathlen, 752const char*basename, 753int*dtype, 754struct exclude_list *el) 755{ 756int i; 757 758if(!el->nr) 759return NULL;/* undefined */ 760 761for(i = el->nr -1;0<= i; i--) { 762struct exclude *x = el->excludes[i]; 763const char*exclude = x->pattern; 764int prefix = x->nowildcardlen; 765 766if(x->flags & EXC_FLAG_MUSTBEDIR) { 767if(*dtype == DT_UNKNOWN) 768*dtype =get_dtype(NULL, pathname, pathlen); 769if(*dtype != DT_DIR) 770continue; 771} 772 773if(x->flags & EXC_FLAG_NODIR) { 774if(match_basename(basename, 775 pathlen - (basename - pathname), 776 exclude, prefix, x->patternlen, 777 x->flags)) 778return x; 779continue; 780} 781 782assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 783if(match_pathname(pathname, pathlen, 784 x->base, x->baselen ? x->baselen -1:0, 785 exclude, prefix, x->patternlen, x->flags)) 786return x; 787} 788return NULL;/* undecided */ 789} 790 791/* 792 * Scan the list and let the last match determine the fate. 793 * Return 1 for exclude, 0 for include and -1 for undecided. 794 */ 795intis_excluded_from_list(const char*pathname, 796int pathlen,const char*basename,int*dtype, 797struct exclude_list *el) 798{ 799struct exclude *exclude; 800 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 801if(exclude) 802return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 803return-1;/* undecided */ 804} 805 806static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 807const char*pathname,int pathlen,const char*basename, 808int*dtype_p) 809{ 810int i, j; 811struct exclude_list_group *group; 812struct exclude *exclude; 813for(i = EXC_CMDL; i <= EXC_FILE; i++) { 814 group = &dir->exclude_list_group[i]; 815for(j = group->nr -1; j >=0; j--) { 816 exclude =last_exclude_matching_from_list( 817 pathname, pathlen, basename, dtype_p, 818&group->el[j]); 819if(exclude) 820return exclude; 821} 822} 823return NULL; 824} 825 826/* 827 * Loads the per-directory exclude list for the substring of base 828 * which has a char length of baselen. 829 */ 830static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 831{ 832struct exclude_list_group *group; 833struct exclude_list *el; 834struct exclude_stack *stk = NULL; 835int current; 836 837 group = &dir->exclude_list_group[EXC_DIRS]; 838 839/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 840 * which originate from directories not in the prefix of the 841 * path being checked. */ 842while((stk = dir->exclude_stack) != NULL) { 843if(stk->baselen <= baselen && 844!strncmp(dir->basebuf, base, stk->baselen)) 845break; 846 el = &group->el[dir->exclude_stack->exclude_ix]; 847 dir->exclude_stack = stk->prev; 848 dir->exclude = NULL; 849free((char*)el->src);/* see strdup() below */ 850clear_exclude_list(el); 851free(stk); 852 group->nr--; 853} 854 855/* Skip traversing into sub directories if the parent is excluded */ 856if(dir->exclude) 857return; 858 859/* Read from the parent directories and push them down. */ 860 current = stk ? stk->baselen : -1; 861while(current < baselen) { 862struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 863const char*cp; 864 865if(current <0) { 866 cp = base; 867 current =0; 868} 869else{ 870 cp =strchr(base + current +1,'/'); 871if(!cp) 872die("oops in prep_exclude"); 873 cp++; 874} 875 stk->prev = dir->exclude_stack; 876 stk->baselen = cp - base; 877 stk->exclude_ix = group->nr; 878 el =add_exclude_list(dir, EXC_DIRS, NULL); 879memcpy(dir->basebuf + current, base + current, 880 stk->baselen - current); 881 882/* Abort if the directory is excluded */ 883if(stk->baselen) { 884int dt = DT_DIR; 885 dir->basebuf[stk->baselen -1] =0; 886 dir->exclude =last_exclude_matching_from_lists(dir, 887 dir->basebuf, stk->baselen -1, 888 dir->basebuf + current, &dt); 889 dir->basebuf[stk->baselen -1] ='/'; 890if(dir->exclude && 891 dir->exclude->flags & EXC_FLAG_NEGATIVE) 892 dir->exclude = NULL; 893if(dir->exclude) { 894 dir->basebuf[stk->baselen] =0; 895 dir->exclude_stack = stk; 896return; 897} 898} 899 900/* Try to read per-directory file unless path is too long */ 901if(dir->exclude_per_dir && 902 stk->baselen +strlen(dir->exclude_per_dir) < PATH_MAX) { 903strcpy(dir->basebuf + stk->baselen, 904 dir->exclude_per_dir); 905/* 906 * dir->basebuf gets reused by the traversal, but we 907 * need fname to remain unchanged to ensure the src 908 * member of each struct exclude correctly 909 * back-references its source file. Other invocations 910 * of add_exclude_list provide stable strings, so we 911 * strdup() and free() here in the caller. 912 */ 913 el->src =strdup(dir->basebuf); 914add_excludes_from_file_to_list(dir->basebuf, 915 dir->basebuf, stk->baselen, el,1); 916} 917 dir->exclude_stack = stk; 918 current = stk->baselen; 919} 920 dir->basebuf[baselen] ='\0'; 921} 922 923/* 924 * Loads the exclude lists for the directory containing pathname, then 925 * scans all exclude lists to determine whether pathname is excluded. 926 * Returns the exclude_list element which matched, or NULL for 927 * undecided. 928 */ 929struct exclude *last_exclude_matching(struct dir_struct *dir, 930const char*pathname, 931int*dtype_p) 932{ 933int pathlen =strlen(pathname); 934const char*basename =strrchr(pathname,'/'); 935 basename = (basename) ? basename+1: pathname; 936 937prep_exclude(dir, pathname, basename-pathname); 938 939if(dir->exclude) 940return dir->exclude; 941 942returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 943 basename, dtype_p); 944} 945 946/* 947 * Loads the exclude lists for the directory containing pathname, then 948 * scans all exclude lists to determine whether pathname is excluded. 949 * Returns 1 if true, otherwise 0. 950 */ 951intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 952{ 953struct exclude *exclude = 954last_exclude_matching(dir, pathname, dtype_p); 955if(exclude) 956return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 957return0; 958} 959 960static struct dir_entry *dir_entry_new(const char*pathname,int len) 961{ 962struct dir_entry *ent; 963 964 ent =xmalloc(sizeof(*ent) + len +1); 965 ent->len = len; 966memcpy(ent->name, pathname, len); 967 ent->name[len] =0; 968return ent; 969} 970 971static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 972{ 973if(cache_file_exists(pathname, len, ignore_case)) 974return NULL; 975 976ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 977return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 978} 979 980struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 981{ 982if(!cache_name_is_other(pathname, len)) 983return NULL; 984 985ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 986return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 987} 988 989enum exist_status { 990 index_nonexistent =0, 991 index_directory, 992 index_gitdir 993}; 994 995/* 996 * Do not use the alphabetically sorted index to look up 997 * the directory name; instead, use the case insensitive 998 * directory hash. 999 */1000static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1001{1002const struct cache_entry *ce =cache_dir_exists(dirname, len);1003unsigned char endchar;10041005if(!ce)1006return index_nonexistent;1007 endchar = ce->name[len];10081009/*1010 * The cache_entry structure returned will contain this dirname1011 * and possibly additional path components.1012 */1013if(endchar =='/')1014return index_directory;10151016/*1017 * If there are no additional path components, then this cache_entry1018 * represents a submodule. Submodules, despite being directories,1019 * are stored in the cache without a closing slash.1020 */1021if(!endchar &&S_ISGITLINK(ce->ce_mode))1022return index_gitdir;10231024/* This should never be hit, but it exists just in case. */1025return index_nonexistent;1026}10271028/*1029 * The index sorts alphabetically by entry name, which1030 * means that a gitlink sorts as '\0' at the end, while1031 * a directory (which is defined not as an entry, but as1032 * the files it contains) will sort with the '/' at the1033 * end.1034 */1035static enum exist_status directory_exists_in_index(const char*dirname,int len)1036{1037int pos;10381039if(ignore_case)1040returndirectory_exists_in_index_icase(dirname, len);10411042 pos =cache_name_pos(dirname, len);1043if(pos <0)1044 pos = -pos-1;1045while(pos < active_nr) {1046const struct cache_entry *ce = active_cache[pos++];1047unsigned char endchar;10481049if(strncmp(ce->name, dirname, len))1050break;1051 endchar = ce->name[len];1052if(endchar >'/')1053break;1054if(endchar =='/')1055return index_directory;1056if(!endchar &&S_ISGITLINK(ce->ce_mode))1057return index_gitdir;1058}1059return index_nonexistent;1060}10611062/*1063 * When we find a directory when traversing the filesystem, we1064 * have three distinct cases:1065 *1066 * - ignore it1067 * - see it as a directory1068 * - recurse into it1069 *1070 * and which one we choose depends on a combination of existing1071 * git index contents and the flags passed into the directory1072 * traversal routine.1073 *1074 * Case 1: If we *already* have entries in the index under that1075 * directory name, we always recurse into the directory to see1076 * all the files.1077 *1078 * Case 2: If we *already* have that directory name as a gitlink,1079 * we always continue to see it as a gitlink, regardless of whether1080 * there is an actual git directory there or not (it might not1081 * be checked out as a subproject!)1082 *1083 * Case 3: if we didn't have it in the index previously, we1084 * have a few sub-cases:1085 *1086 * (a) if "show_other_directories" is true, we show it as1087 * just a directory, unless "hide_empty_directories" is1088 * also true, in which case we need to check if it contains any1089 * untracked and / or ignored files.1090 * (b) if it looks like a git directory, and we don't have1091 * 'no_gitlinks' set we treat it as a gitlink, and show it1092 * as a directory.1093 * (c) otherwise, we recurse into it.1094 */1095static enum path_treatment treat_directory(struct dir_struct *dir,1096const char*dirname,int len,int exclude,1097const struct path_simplify *simplify)1098{1099/* The "len-1" is to strip the final '/' */1100switch(directory_exists_in_index(dirname, len-1)) {1101case index_directory:1102return path_recurse;11031104case index_gitdir:1105return path_none;11061107case index_nonexistent:1108if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1109break;1110if(!(dir->flags & DIR_NO_GITLINKS)) {1111unsigned char sha1[20];1112if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1113return path_untracked;1114}1115return path_recurse;1116}11171118/* This is the "show_other_directories" case */11191120if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1121return exclude ? path_excluded : path_untracked;11221123returnread_directory_recursive(dir, dirname, len,1, simplify);1124}11251126/*1127 * This is an inexact early pruning of any recursive directory1128 * reading - if the path cannot possibly be in the pathspec,1129 * return true, and we'll skip it early.1130 */1131static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1132{1133if(simplify) {1134for(;;) {1135const char*match = simplify->path;1136int len = simplify->len;11371138if(!match)1139break;1140if(len > pathlen)1141 len = pathlen;1142if(!memcmp(path, match, len))1143return0;1144 simplify++;1145}1146return1;1147}1148return0;1149}11501151/*1152 * This function tells us whether an excluded path matches a1153 * list of "interesting" pathspecs. That is, whether a path matched1154 * by any of the pathspecs could possibly be ignored by excluding1155 * the specified path. This can happen if:1156 *1157 * 1. the path is mentioned explicitly in the pathspec1158 *1159 * 2. the path is a directory prefix of some element in the1160 * pathspec1161 */1162static intexclude_matches_pathspec(const char*path,int len,1163const struct path_simplify *simplify)1164{1165if(simplify) {1166for(; simplify->path; simplify++) {1167if(len == simplify->len1168&& !memcmp(path, simplify->path, len))1169return1;1170if(len < simplify->len1171&& simplify->path[len] =='/'1172&& !memcmp(path, simplify->path, len))1173return1;1174}1175}1176return0;1177}11781179static intget_index_dtype(const char*path,int len)1180{1181int pos;1182const struct cache_entry *ce;11831184 ce =cache_file_exists(path, len,0);1185if(ce) {1186if(!ce_uptodate(ce))1187return DT_UNKNOWN;1188if(S_ISGITLINK(ce->ce_mode))1189return DT_DIR;1190/*1191 * Nobody actually cares about the1192 * difference between DT_LNK and DT_REG1193 */1194return DT_REG;1195}11961197/* Try to look it up as a directory */1198 pos =cache_name_pos(path, len);1199if(pos >=0)1200return DT_UNKNOWN;1201 pos = -pos-1;1202while(pos < active_nr) {1203 ce = active_cache[pos++];1204if(strncmp(ce->name, path, len))1205break;1206if(ce->name[len] >'/')1207break;1208if(ce->name[len] <'/')1209continue;1210if(!ce_uptodate(ce))1211break;/* continue? */1212return DT_DIR;1213}1214return DT_UNKNOWN;1215}12161217static intget_dtype(struct dirent *de,const char*path,int len)1218{1219int dtype = de ?DTYPE(de) : DT_UNKNOWN;1220struct stat st;12211222if(dtype != DT_UNKNOWN)1223return dtype;1224 dtype =get_index_dtype(path, len);1225if(dtype != DT_UNKNOWN)1226return dtype;1227if(lstat(path, &st))1228return dtype;1229if(S_ISREG(st.st_mode))1230return DT_REG;1231if(S_ISDIR(st.st_mode))1232return DT_DIR;1233if(S_ISLNK(st.st_mode))1234return DT_LNK;1235return dtype;1236}12371238static enum path_treatment treat_one_path(struct dir_struct *dir,1239struct strbuf *path,1240const struct path_simplify *simplify,1241int dtype,struct dirent *de)1242{1243int exclude;1244int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);12451246if(dtype == DT_UNKNOWN)1247 dtype =get_dtype(de, path->buf, path->len);12481249/* Always exclude indexed files */1250if(dtype != DT_DIR && has_path_in_index)1251return path_none;12521253/*1254 * When we are looking at a directory P in the working tree,1255 * there are three cases:1256 *1257 * (1) P exists in the index. Everything inside the directory P in1258 * the working tree needs to go when P is checked out from the1259 * index.1260 *1261 * (2) P does not exist in the index, but there is P/Q in the index.1262 * We know P will stay a directory when we check out the contents1263 * of the index, but we do not know yet if there is a directory1264 * P/Q in the working tree to be killed, so we need to recurse.1265 *1266 * (3) P does not exist in the index, and there is no P/Q in the index1267 * to require P to be a directory, either. Only in this case, we1268 * know that everything inside P will not be killed without1269 * recursing.1270 */1271if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1272(dtype == DT_DIR) &&1273!has_path_in_index &&1274(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1275return path_none;12761277 exclude =is_excluded(dir, path->buf, &dtype);12781279/*1280 * Excluded? If we don't explicitly want to show1281 * ignored files, ignore it1282 */1283if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1284return path_excluded;12851286switch(dtype) {1287default:1288return path_none;1289case DT_DIR:1290strbuf_addch(path,'/');1291returntreat_directory(dir, path->buf, path->len, exclude,1292 simplify);1293case DT_REG:1294case DT_LNK:1295return exclude ? path_excluded : path_untracked;1296}1297}12981299static enum path_treatment treat_path(struct dir_struct *dir,1300struct dirent *de,1301struct strbuf *path,1302int baselen,1303const struct path_simplify *simplify)1304{1305int dtype;13061307if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1308return path_none;1309strbuf_setlen(path, baselen);1310strbuf_addstr(path, de->d_name);1311if(simplify_away(path->buf, path->len, simplify))1312return path_none;13131314 dtype =DTYPE(de);1315returntreat_one_path(dir, path, simplify, dtype, de);1316}13171318/*1319 * Read a directory tree. We currently ignore anything but1320 * directories, regular files and symlinks. That's because git1321 * doesn't handle them at all yet. Maybe that will change some1322 * day.1323 *1324 * Also, we ignore the name ".git" (even if it is not a directory).1325 * That likely will not change.1326 *1327 * Returns the most significant path_treatment value encountered in the scan.1328 */1329static enum path_treatment read_directory_recursive(struct dir_struct *dir,1330const char*base,int baselen,1331int check_only,1332const struct path_simplify *simplify)1333{1334DIR*fdir;1335enum path_treatment state, subdir_state, dir_state = path_none;1336struct dirent *de;1337struct strbuf path = STRBUF_INIT;13381339strbuf_add(&path, base, baselen);13401341 fdir =opendir(path.len ? path.buf :".");1342if(!fdir)1343goto out;13441345while((de =readdir(fdir)) != NULL) {1346/* check how the file or directory should be treated */1347 state =treat_path(dir, de, &path, baselen, simplify);1348if(state > dir_state)1349 dir_state = state;13501351/* recurse into subdir if instructed by treat_path */1352if(state == path_recurse) {1353 subdir_state =read_directory_recursive(dir, path.buf,1354 path.len, check_only, simplify);1355if(subdir_state > dir_state)1356 dir_state = subdir_state;1357}13581359if(check_only) {1360/* abort early if maximum state has been reached */1361if(dir_state == path_untracked)1362break;1363/* skip the dir_add_* part */1364continue;1365}13661367/* add the path to the appropriate result list */1368switch(state) {1369case path_excluded:1370if(dir->flags & DIR_SHOW_IGNORED)1371dir_add_name(dir, path.buf, path.len);1372else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1373((dir->flags & DIR_COLLECT_IGNORED) &&1374exclude_matches_pathspec(path.buf, path.len,1375 simplify)))1376dir_add_ignored(dir, path.buf, path.len);1377break;13781379case path_untracked:1380if(!(dir->flags & DIR_SHOW_IGNORED))1381dir_add_name(dir, path.buf, path.len);1382break;13831384default:1385break;1386}1387}1388closedir(fdir);1389 out:1390strbuf_release(&path);13911392return dir_state;1393}13941395static intcmp_name(const void*p1,const void*p2)1396{1397const struct dir_entry *e1 = *(const struct dir_entry **)p1;1398const struct dir_entry *e2 = *(const struct dir_entry **)p2;13991400returncache_name_compare(e1->name, e1->len,1401 e2->name, e2->len);1402}14031404static struct path_simplify *create_simplify(const char**pathspec)1405{1406int nr, alloc =0;1407struct path_simplify *simplify = NULL;14081409if(!pathspec)1410return NULL;14111412for(nr =0; ; nr++) {1413const char*match;1414ALLOC_GROW(simplify, nr +1, alloc);1415 match = *pathspec++;1416if(!match)1417break;1418 simplify[nr].path = match;1419 simplify[nr].len =simple_length(match);1420}1421 simplify[nr].path = NULL;1422 simplify[nr].len =0;1423return simplify;1424}14251426static voidfree_simplify(struct path_simplify *simplify)1427{1428free(simplify);1429}14301431static inttreat_leading_path(struct dir_struct *dir,1432const char*path,int len,1433const struct path_simplify *simplify)1434{1435struct strbuf sb = STRBUF_INIT;1436int baselen, rc =0;1437const char*cp;1438int old_flags = dir->flags;14391440while(len && path[len -1] =='/')1441 len--;1442if(!len)1443return1;1444 baselen =0;1445 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1446while(1) {1447 cp = path + baselen + !!baselen;1448 cp =memchr(cp,'/', path + len - cp);1449if(!cp)1450 baselen = len;1451else1452 baselen = cp - path;1453strbuf_setlen(&sb,0);1454strbuf_add(&sb, path, baselen);1455if(!is_directory(sb.buf))1456break;1457if(simplify_away(sb.buf, sb.len, simplify))1458break;1459if(treat_one_path(dir, &sb, simplify,1460 DT_DIR, NULL) == path_none)1461break;/* do not recurse into it */1462if(len <= baselen) {1463 rc =1;1464break;/* finished checking */1465}1466}1467strbuf_release(&sb);1468 dir->flags = old_flags;1469return rc;1470}14711472intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1473{1474struct path_simplify *simplify;14751476/*1477 * Check out create_simplify()1478 */1479if(pathspec)1480GUARD_PATHSPEC(pathspec,1481 PATHSPEC_FROMTOP |1482 PATHSPEC_MAXDEPTH |1483 PATHSPEC_LITERAL |1484 PATHSPEC_GLOB |1485 PATHSPEC_ICASE |1486 PATHSPEC_EXCLUDE);14871488if(has_symlink_leading_path(path, len))1489return dir->nr;14901491/*1492 * exclude patterns are treated like positive ones in1493 * create_simplify. Usually exclude patterns should be a1494 * subset of positive ones, which has no impacts on1495 * create_simplify().1496 */1497 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1498if(!len ||treat_leading_path(dir, path, len, simplify))1499read_directory_recursive(dir, path, len,0, simplify);1500free_simplify(simplify);1501qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1502qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1503return dir->nr;1504}15051506intfile_exists(const char*f)1507{1508struct stat sb;1509returnlstat(f, &sb) ==0;1510}15111512/*1513 * Given two normalized paths (a trailing slash is ok), if subdir is1514 * outside dir, return -1. Otherwise return the offset in subdir that1515 * can be used as relative path to dir.1516 */1517intdir_inside_of(const char*subdir,const char*dir)1518{1519int offset =0;15201521assert(dir && subdir && *dir && *subdir);15221523while(*dir && *subdir && *dir == *subdir) {1524 dir++;1525 subdir++;1526 offset++;1527}15281529/* hel[p]/me vs hel[l]/yeah */1530if(*dir && *subdir)1531return-1;15321533if(!*subdir)1534return!*dir ? offset : -1;/* same dir */15351536/* foo/[b]ar vs foo/[] */1537if(is_dir_sep(dir[-1]))1538returnis_dir_sep(subdir[-1]) ? offset : -1;15391540/* foo[/]bar vs foo[] */1541returnis_dir_sep(*subdir) ? offset +1: -1;1542}15431544intis_inside_dir(const char*dir)1545{1546char cwd[PATH_MAX];1547if(!dir)1548return0;1549if(!getcwd(cwd,sizeof(cwd)))1550die_errno("can't find the current directory");1551returndir_inside_of(cwd, dir) >=0;1552}15531554intis_empty_dir(const char*path)1555{1556DIR*dir =opendir(path);1557struct dirent *e;1558int ret =1;15591560if(!dir)1561return0;15621563while((e =readdir(dir)) != NULL)1564if(!is_dot_or_dotdot(e->d_name)) {1565 ret =0;1566break;1567}15681569closedir(dir);1570return ret;1571}15721573static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1574{1575DIR*dir;1576struct dirent *e;1577int ret =0, original_len = path->len, len, kept_down =0;1578int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1579int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1580unsigned char submodule_head[20];15811582if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1583!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1584/* Do not descend and nuke a nested git work tree. */1585if(kept_up)1586*kept_up =1;1587return0;1588}15891590 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1591 dir =opendir(path->buf);1592if(!dir) {1593if(errno == ENOENT)1594return keep_toplevel ? -1:0;1595else if(errno == EACCES && !keep_toplevel)1596/*1597 * An empty dir could be removable even if it1598 * is unreadable:1599 */1600returnrmdir(path->buf);1601else1602return-1;1603}1604if(path->buf[original_len -1] !='/')1605strbuf_addch(path,'/');16061607 len = path->len;1608while((e =readdir(dir)) != NULL) {1609struct stat st;1610if(is_dot_or_dotdot(e->d_name))1611continue;16121613strbuf_setlen(path, len);1614strbuf_addstr(path, e->d_name);1615if(lstat(path->buf, &st)) {1616if(errno == ENOENT)1617/*1618 * file disappeared, which is what we1619 * wanted anyway1620 */1621continue;1622/* fall thru */1623}else if(S_ISDIR(st.st_mode)) {1624if(!remove_dir_recurse(path, flag, &kept_down))1625continue;/* happy */1626}else if(!only_empty &&1627(!unlink(path->buf) || errno == ENOENT)) {1628continue;/* happy, too */1629}16301631/* path too long, stat fails, or non-directory still exists */1632 ret = -1;1633break;1634}1635closedir(dir);16361637strbuf_setlen(path, original_len);1638if(!ret && !keep_toplevel && !kept_down)1639 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1640else if(kept_up)1641/*1642 * report the uplevel that it is not an error that we1643 * did not rmdir() our directory.1644 */1645*kept_up = !ret;1646return ret;1647}16481649intremove_dir_recursively(struct strbuf *path,int flag)1650{1651returnremove_dir_recurse(path, flag, NULL);1652}16531654voidsetup_standard_excludes(struct dir_struct *dir)1655{1656const char*path;1657char*xdg_path;16581659 dir->exclude_per_dir =".gitignore";1660 path =git_path("info/exclude");1661if(!excludes_file) {1662home_config_paths(NULL, &xdg_path,"ignore");1663 excludes_file = xdg_path;1664}1665if(!access_or_warn(path, R_OK,0))1666add_excludes_from_file(dir, path);1667if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1668add_excludes_from_file(dir, excludes_file);1669}16701671intremove_path(const char*name)1672{1673char*slash;16741675if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1676return-1;16771678 slash =strrchr(name,'/');1679if(slash) {1680char*dirs =xstrdup(name);1681 slash = dirs + (slash - name);1682do{1683*slash ='\0';1684}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1685free(dirs);1686}1687return0;1688}16891690/*1691 * Frees memory within dir which was allocated for exclude lists and1692 * the exclude_stack. Does not free dir itself.1693 */1694voidclear_directory(struct dir_struct *dir)1695{1696int i, j;1697struct exclude_list_group *group;1698struct exclude_list *el;1699struct exclude_stack *stk;17001701for(i = EXC_CMDL; i <= EXC_FILE; i++) {1702 group = &dir->exclude_list_group[i];1703for(j =0; j < group->nr; j++) {1704 el = &group->el[j];1705if(i == EXC_DIRS)1706free((char*)el->src);1707clear_exclude_list(el);1708}1709free(group->el);1710}17111712 stk = dir->exclude_stack;1713while(stk) {1714struct exclude_stack *prev = stk->prev;1715free(stk);1716 stk = prev;1717}1718}