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#define NO_THE_INDEX_COMPATIBILITY_MACROS 11#include"cache.h" 12#include"config.h" 13#include"dir.h" 14#include"object-store.h" 15#include"attr.h" 16#include"refs.h" 17#include"wildmatch.h" 18#include"pathspec.h" 19#include"utf8.h" 20#include"varint.h" 21#include"ewah/ewok.h" 22#include"fsmonitor.h" 23#include"submodule-config.h" 24 25/* 26 * Tells read_directory_recursive how a file or directory should be treated. 27 * Values are ordered by significance, e.g. if a directory contains both 28 * excluded and untracked files, it is listed as untracked because 29 * path_untracked > path_excluded. 30 */ 31enum path_treatment { 32 path_none =0, 33 path_recurse, 34 path_excluded, 35 path_untracked 36}; 37 38/* 39 * Support data structure for our opendir/readdir/closedir wrappers 40 */ 41struct cached_dir { 42DIR*fdir; 43struct untracked_cache_dir *untracked; 44int nr_files; 45int nr_dirs; 46 47struct dirent *de; 48const char*file; 49struct untracked_cache_dir *ucd; 50}; 51 52static enum path_treatment read_directory_recursive(struct dir_struct *dir, 53struct index_state *istate,const char*path,int len, 54struct untracked_cache_dir *untracked, 55int check_only,int stop_at_first_file,const struct pathspec *pathspec); 56static intget_dtype(struct dirent *de,struct index_state *istate, 57const char*path,int len); 58 59intcount_slashes(const char*s) 60{ 61int cnt =0; 62while(*s) 63if(*s++ =='/') 64 cnt++; 65return cnt; 66} 67 68intfspathcmp(const char*a,const char*b) 69{ 70return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 71} 72 73intfspathncmp(const char*a,const char*b,size_t count) 74{ 75return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 76} 77 78intgit_fnmatch(const struct pathspec_item *item, 79const char*pattern,const char*string, 80int prefix) 81{ 82if(prefix >0) { 83if(ps_strncmp(item, pattern, string, prefix)) 84return WM_NOMATCH; 85 pattern += prefix; 86 string += prefix; 87} 88if(item->flags & PATHSPEC_ONESTAR) { 89int pattern_len =strlen(++pattern); 90int string_len =strlen(string); 91return string_len < pattern_len || 92ps_strcmp(item, pattern, 93 string + string_len - pattern_len); 94} 95if(item->magic & PATHSPEC_GLOB) 96returnwildmatch(pattern, string, 97 WM_PATHNAME | 98(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0)); 99else 100/* wildmatch has not learned no FNM_PATHNAME mode yet */ 101returnwildmatch(pattern, string, 102 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0); 103} 104 105static intfnmatch_icase_mem(const char*pattern,int patternlen, 106const char*string,int stringlen, 107int flags) 108{ 109int match_status; 110struct strbuf pat_buf = STRBUF_INIT; 111struct strbuf str_buf = STRBUF_INIT; 112const char*use_pat = pattern; 113const char*use_str = string; 114 115if(pattern[patternlen]) { 116strbuf_add(&pat_buf, pattern, patternlen); 117 use_pat = pat_buf.buf; 118} 119if(string[stringlen]) { 120strbuf_add(&str_buf, string, stringlen); 121 use_str = str_buf.buf; 122} 123 124if(ignore_case) 125 flags |= WM_CASEFOLD; 126 match_status =wildmatch(use_pat, use_str, flags); 127 128strbuf_release(&pat_buf); 129strbuf_release(&str_buf); 130 131return match_status; 132} 133 134static size_tcommon_prefix_len(const struct pathspec *pathspec) 135{ 136int n; 137size_t max =0; 138 139/* 140 * ":(icase)path" is treated as a pathspec full of 141 * wildcard. In other words, only prefix is considered common 142 * prefix. If the pathspec is abc/foo abc/bar, running in 143 * subdir xyz, the common prefix is still xyz, not xuz/abc as 144 * in non-:(icase). 145 */ 146GUARD_PATHSPEC(pathspec, 147 PATHSPEC_FROMTOP | 148 PATHSPEC_MAXDEPTH | 149 PATHSPEC_LITERAL | 150 PATHSPEC_GLOB | 151 PATHSPEC_ICASE | 152 PATHSPEC_EXCLUDE | 153 PATHSPEC_ATTR); 154 155for(n =0; n < pathspec->nr; n++) { 156size_t i =0, len =0, item_len; 157if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 158continue; 159if(pathspec->items[n].magic & PATHSPEC_ICASE) 160 item_len = pathspec->items[n].prefix; 161else 162 item_len = pathspec->items[n].nowildcard_len; 163while(i < item_len && (n ==0|| i < max)) { 164char c = pathspec->items[n].match[i]; 165if(c != pathspec->items[0].match[i]) 166break; 167if(c =='/') 168 len = i +1; 169 i++; 170} 171if(n ==0|| len < max) { 172 max = len; 173if(!max) 174break; 175} 176} 177return max; 178} 179 180/* 181 * Returns a copy of the longest leading path common among all 182 * pathspecs. 183 */ 184char*common_prefix(const struct pathspec *pathspec) 185{ 186unsigned long len =common_prefix_len(pathspec); 187 188return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 189} 190 191intfill_directory(struct dir_struct *dir, 192struct index_state *istate, 193const struct pathspec *pathspec) 194{ 195const char*prefix; 196size_t prefix_len; 197 198/* 199 * Calculate common prefix for the pathspec, and 200 * use that to optimize the directory walk 201 */ 202 prefix_len =common_prefix_len(pathspec); 203 prefix = prefix_len ? pathspec->items[0].match :""; 204 205/* Read the directory and prune it */ 206read_directory(dir, istate, prefix, prefix_len, pathspec); 207 208return prefix_len; 209} 210 211intwithin_depth(const char*name,int namelen, 212int depth,int max_depth) 213{ 214const char*cp = name, *cpe = name + namelen; 215 216while(cp < cpe) { 217if(*cp++ !='/') 218continue; 219 depth++; 220if(depth > max_depth) 221return0; 222} 223return1; 224} 225 226/* 227 * Read the contents of the blob with the given OID into a buffer. 228 * Append a trailing LF to the end if the last line doesn't have one. 229 * 230 * Returns: 231 * -1 when the OID is invalid or unknown or does not refer to a blob. 232 * 0 when the blob is empty. 233 * 1 along with { data, size } of the (possibly augmented) buffer 234 * when successful. 235 * 236 * Optionally updates the given oid_stat with the given OID (when valid). 237 */ 238static intdo_read_blob(const struct object_id *oid,struct oid_stat *oid_stat, 239size_t*size_out,char**data_out) 240{ 241enum object_type type; 242unsigned long sz; 243char*data; 244 245*size_out =0; 246*data_out = NULL; 247 248 data =read_object_file(oid, &type, &sz); 249if(!data || type != OBJ_BLOB) { 250free(data); 251return-1; 252} 253 254if(oid_stat) { 255memset(&oid_stat->stat,0,sizeof(oid_stat->stat)); 256oidcpy(&oid_stat->oid, oid); 257} 258 259if(sz ==0) { 260free(data); 261return0; 262} 263 264if(data[sz -1] !='\n') { 265 data =xrealloc(data,st_add(sz,1)); 266 data[sz++] ='\n'; 267} 268 269*size_out =xsize_t(sz); 270*data_out = data; 271 272return1; 273} 274 275#define DO_MATCH_EXCLUDE (1<<0) 276#define DO_MATCH_DIRECTORY (1<<1) 277#define DO_MATCH_SUBMODULE (1<<2) 278 279/* 280 * Does 'match' match the given name? 281 * A match is found if 282 * 283 * (1) the 'match' string is leading directory of 'name', or 284 * (2) the 'match' string is a wildcard and matches 'name', or 285 * (3) the 'match' string is exactly the same as 'name'. 286 * 287 * and the return value tells which case it was. 288 * 289 * It returns 0 when there is no match. 290 */ 291static intmatch_pathspec_item(const struct index_state *istate, 292const struct pathspec_item *item,int prefix, 293const char*name,int namelen,unsigned flags) 294{ 295/* name/namelen has prefix cut off by caller */ 296const char*match = item->match + prefix; 297int matchlen = item->len - prefix; 298 299/* 300 * The normal call pattern is: 301 * 1. prefix = common_prefix_len(ps); 302 * 2. prune something, or fill_directory 303 * 3. match_pathspec() 304 * 305 * 'prefix' at #1 may be shorter than the command's prefix and 306 * it's ok for #2 to match extra files. Those extras will be 307 * trimmed at #3. 308 * 309 * Suppose the pathspec is 'foo' and '../bar' running from 310 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 311 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 312 * user does not want XYZ/foo, only the "foo" part should be 313 * case-insensitive. We need to filter out XYZ/foo here. In 314 * other words, we do not trust the caller on comparing the 315 * prefix part when :(icase) is involved. We do exact 316 * comparison ourselves. 317 * 318 * Normally the caller (common_prefix_len() in fact) does 319 * _exact_ matching on name[-prefix+1..-1] and we do not need 320 * to check that part. Be defensive and check it anyway, in 321 * case common_prefix_len is changed, or a new caller is 322 * introduced that does not use common_prefix_len. 323 * 324 * If the penalty turns out too high when prefix is really 325 * long, maybe change it to 326 * strncmp(match, name, item->prefix - prefix) 327 */ 328if(item->prefix && (item->magic & PATHSPEC_ICASE) && 329strncmp(item->match, name - prefix, item->prefix)) 330return0; 331 332if(item->attr_match_nr && 333!match_pathspec_attrs(istate, name, namelen, item)) 334return0; 335 336/* If the match was just the prefix, we matched */ 337if(!*match) 338return MATCHED_RECURSIVELY; 339 340if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 341if(matchlen == namelen) 342return MATCHED_EXACTLY; 343 344if(match[matchlen-1] =='/'|| name[matchlen] =='/') 345return MATCHED_RECURSIVELY; 346}else if((flags & DO_MATCH_DIRECTORY) && 347 match[matchlen -1] =='/'&& 348 namelen == matchlen -1&& 349!ps_strncmp(item, match, name, namelen)) 350return MATCHED_EXACTLY; 351 352if(item->nowildcard_len < item->len && 353!git_fnmatch(item, match, name, 354 item->nowildcard_len - prefix)) 355return MATCHED_FNMATCH; 356 357/* Perform checks to see if "name" is a super set of the pathspec */ 358if(flags & DO_MATCH_SUBMODULE) { 359/* name is a literal prefix of the pathspec */ 360if((namelen < matchlen) && 361(match[namelen] =='/') && 362!ps_strncmp(item, match, name, namelen)) 363return MATCHED_RECURSIVELY; 364 365/* name" doesn't match up to the first wild character */ 366if(item->nowildcard_len < item->len && 367ps_strncmp(item, match, name, 368 item->nowildcard_len - prefix)) 369return0; 370 371/* 372 * Here is where we would perform a wildmatch to check if 373 * "name" can be matched as a directory (or a prefix) against 374 * the pathspec. Since wildmatch doesn't have this capability 375 * at the present we have to punt and say that it is a match, 376 * potentially returning a false positive 377 * The submodules themselves will be able to perform more 378 * accurate matching to determine if the pathspec matches. 379 */ 380return MATCHED_RECURSIVELY; 381} 382 383return0; 384} 385 386/* 387 * Given a name and a list of pathspecs, returns the nature of the 388 * closest (i.e. most specific) match of the name to any of the 389 * pathspecs. 390 * 391 * The caller typically calls this multiple times with the same 392 * pathspec and seen[] array but with different name/namelen 393 * (e.g. entries from the index) and is interested in seeing if and 394 * how each pathspec matches all the names it calls this function 395 * with. A mark is left in the seen[] array for each pathspec element 396 * indicating the closest type of match that element achieved, so if 397 * seen[n] remains zero after multiple invocations, that means the nth 398 * pathspec did not match any names, which could indicate that the 399 * user mistyped the nth pathspec. 400 */ 401static intdo_match_pathspec(const struct index_state *istate, 402const struct pathspec *ps, 403const char*name,int namelen, 404int prefix,char*seen, 405unsigned flags) 406{ 407int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 408 409GUARD_PATHSPEC(ps, 410 PATHSPEC_FROMTOP | 411 PATHSPEC_MAXDEPTH | 412 PATHSPEC_LITERAL | 413 PATHSPEC_GLOB | 414 PATHSPEC_ICASE | 415 PATHSPEC_EXCLUDE | 416 PATHSPEC_ATTR); 417 418if(!ps->nr) { 419if(!ps->recursive || 420!(ps->magic & PATHSPEC_MAXDEPTH) || 421 ps->max_depth == -1) 422return MATCHED_RECURSIVELY; 423 424if(within_depth(name, namelen,0, ps->max_depth)) 425return MATCHED_EXACTLY; 426else 427return0; 428} 429 430 name += prefix; 431 namelen -= prefix; 432 433for(i = ps->nr -1; i >=0; i--) { 434int how; 435 436if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 437( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 438continue; 439 440if(seen && seen[i] == MATCHED_EXACTLY) 441continue; 442/* 443 * Make exclude patterns optional and never report 444 * "pathspec ':(exclude)foo' matches no files" 445 */ 446if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 447 seen[i] = MATCHED_FNMATCH; 448 how =match_pathspec_item(istate, ps->items+i, prefix, name, 449 namelen, flags); 450if(ps->recursive && 451(ps->magic & PATHSPEC_MAXDEPTH) && 452 ps->max_depth != -1&& 453 how && how != MATCHED_FNMATCH) { 454int len = ps->items[i].len; 455if(name[len] =='/') 456 len++; 457if(within_depth(name+len, namelen-len,0, ps->max_depth)) 458 how = MATCHED_EXACTLY; 459else 460 how =0; 461} 462if(how) { 463if(retval < how) 464 retval = how; 465if(seen && seen[i] < how) 466 seen[i] = how; 467} 468} 469return retval; 470} 471 472intmatch_pathspec(const struct index_state *istate, 473const struct pathspec *ps, 474const char*name,int namelen, 475int prefix,char*seen,int is_dir) 476{ 477int positive, negative; 478unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 479 positive =do_match_pathspec(istate, ps, name, namelen, 480 prefix, seen, flags); 481if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 482return positive; 483 negative =do_match_pathspec(istate, ps, name, namelen, 484 prefix, seen, 485 flags | DO_MATCH_EXCLUDE); 486return negative ?0: positive; 487} 488 489/** 490 * Check if a submodule is a superset of the pathspec 491 */ 492intsubmodule_path_match(const struct index_state *istate, 493const struct pathspec *ps, 494const char*submodule_name, 495char*seen) 496{ 497int matched =do_match_pathspec(istate, ps, submodule_name, 498strlen(submodule_name), 4990, seen, 500 DO_MATCH_DIRECTORY | 501 DO_MATCH_SUBMODULE); 502return matched; 503} 504 505intreport_path_error(const char*ps_matched, 506const struct pathspec *pathspec, 507const char*prefix) 508{ 509/* 510 * Make sure all pathspec matched; otherwise it is an error. 511 */ 512int num, errors =0; 513for(num =0; num < pathspec->nr; num++) { 514int other, found_dup; 515 516if(ps_matched[num]) 517continue; 518/* 519 * The caller might have fed identical pathspec 520 * twice. Do not barf on such a mistake. 521 * FIXME: parse_pathspec should have eliminated 522 * duplicate pathspec. 523 */ 524for(found_dup = other =0; 525!found_dup && other < pathspec->nr; 526 other++) { 527if(other == num || !ps_matched[other]) 528continue; 529if(!strcmp(pathspec->items[other].original, 530 pathspec->items[num].original)) 531/* 532 * Ok, we have a match already. 533 */ 534 found_dup =1; 535} 536if(found_dup) 537continue; 538 539error(_("pathspec '%s' did not match any file(s) known to git"), 540 pathspec->items[num].original); 541 errors++; 542} 543return errors; 544} 545 546/* 547 * Return the length of the "simple" part of a path match limiter. 548 */ 549intsimple_length(const char*match) 550{ 551int len = -1; 552 553for(;;) { 554unsigned char c = *match++; 555 len++; 556if(c =='\0'||is_glob_special(c)) 557return len; 558} 559} 560 561intno_wildcard(const char*string) 562{ 563return string[simple_length(string)] =='\0'; 564} 565 566voidparse_exclude_pattern(const char**pattern, 567int*patternlen, 568unsigned*flags, 569int*nowildcardlen) 570{ 571const char*p = *pattern; 572size_t i, len; 573 574*flags =0; 575if(*p =='!') { 576*flags |= EXC_FLAG_NEGATIVE; 577 p++; 578} 579 len =strlen(p); 580if(len && p[len -1] =='/') { 581 len--; 582*flags |= EXC_FLAG_MUSTBEDIR; 583} 584for(i =0; i < len; i++) { 585if(p[i] =='/') 586break; 587} 588if(i == len) 589*flags |= EXC_FLAG_NODIR; 590*nowildcardlen =simple_length(p); 591/* 592 * we should have excluded the trailing slash from 'p' too, 593 * but that's one more allocation. Instead just make sure 594 * nowildcardlen does not exceed real patternlen 595 */ 596if(*nowildcardlen > len) 597*nowildcardlen = len; 598if(*p =='*'&&no_wildcard(p +1)) 599*flags |= EXC_FLAG_ENDSWITH; 600*pattern = p; 601*patternlen = len; 602} 603 604voidadd_exclude(const char*string,const char*base, 605int baselen,struct exclude_list *el,int srcpos) 606{ 607struct exclude *x; 608int patternlen; 609unsigned flags; 610int nowildcardlen; 611 612parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 613if(flags & EXC_FLAG_MUSTBEDIR) { 614FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen); 615}else{ 616 x =xmalloc(sizeof(*x)); 617 x->pattern = string; 618} 619 x->patternlen = patternlen; 620 x->nowildcardlen = nowildcardlen; 621 x->base = base; 622 x->baselen = baselen; 623 x->flags = flags; 624 x->srcpos = srcpos; 625ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 626 el->excludes[el->nr++] = x; 627 x->el = el; 628} 629 630static intread_skip_worktree_file_from_index(const struct index_state *istate, 631const char*path, 632size_t*size_out,char**data_out, 633struct oid_stat *oid_stat) 634{ 635int pos, len; 636 637 len =strlen(path); 638 pos =index_name_pos(istate, path, len); 639if(pos <0) 640return-1; 641if(!ce_skip_worktree(istate->cache[pos])) 642return-1; 643 644returndo_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); 645} 646 647/* 648 * Frees memory within el which was allocated for exclude patterns and 649 * the file buffer. Does not free el itself. 650 */ 651voidclear_exclude_list(struct exclude_list *el) 652{ 653int i; 654 655for(i =0; i < el->nr; i++) 656free(el->excludes[i]); 657free(el->excludes); 658free(el->filebuf); 659 660memset(el,0,sizeof(*el)); 661} 662 663static voidtrim_trailing_spaces(char*buf) 664{ 665char*p, *last_space = NULL; 666 667for(p = buf; *p; p++) 668switch(*p) { 669case' ': 670if(!last_space) 671 last_space = p; 672break; 673case'\\': 674 p++; 675if(!*p) 676return; 677/* fallthrough */ 678default: 679 last_space = NULL; 680} 681 682if(last_space) 683*last_space ='\0'; 684} 685 686/* 687 * Given a subdirectory name and "dir" of the current directory, 688 * search the subdir in "dir" and return it, or create a new one if it 689 * does not exist in "dir". 690 * 691 * If "name" has the trailing slash, it'll be excluded in the search. 692 */ 693static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 694struct untracked_cache_dir *dir, 695const char*name,int len) 696{ 697int first, last; 698struct untracked_cache_dir *d; 699if(!dir) 700return NULL; 701if(len && name[len -1] =='/') 702 len--; 703 first =0; 704 last = dir->dirs_nr; 705while(last > first) { 706int cmp, next = (last + first) >>1; 707 d = dir->dirs[next]; 708 cmp =strncmp(name, d->name, len); 709if(!cmp &&strlen(d->name) > len) 710 cmp = -1; 711if(!cmp) 712return d; 713if(cmp <0) { 714 last = next; 715continue; 716} 717 first = next+1; 718} 719 720 uc->dir_created++; 721FLEX_ALLOC_MEM(d, name, name, len); 722 723ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 724MOVE_ARRAY(dir->dirs + first +1, dir->dirs + first, 725 dir->dirs_nr - first); 726 dir->dirs_nr++; 727 dir->dirs[first] = d; 728return d; 729} 730 731static voiddo_invalidate_gitignore(struct untracked_cache_dir *dir) 732{ 733int i; 734 dir->valid =0; 735 dir->untracked_nr =0; 736for(i =0; i < dir->dirs_nr; i++) 737do_invalidate_gitignore(dir->dirs[i]); 738} 739 740static voidinvalidate_gitignore(struct untracked_cache *uc, 741struct untracked_cache_dir *dir) 742{ 743 uc->gitignore_invalidated++; 744do_invalidate_gitignore(dir); 745} 746 747static voidinvalidate_directory(struct untracked_cache *uc, 748struct untracked_cache_dir *dir) 749{ 750int i; 751 752/* 753 * Invalidation increment here is just roughly correct. If 754 * untracked_nr or any of dirs[].recurse is non-zero, we 755 * should increment dir_invalidated too. But that's more 756 * expensive to do. 757 */ 758if(dir->valid) 759 uc->dir_invalidated++; 760 761 dir->valid =0; 762 dir->untracked_nr =0; 763for(i =0; i < dir->dirs_nr; i++) 764 dir->dirs[i]->recurse =0; 765} 766 767static intadd_excludes_from_buffer(char*buf,size_t size, 768const char*base,int baselen, 769struct exclude_list *el); 770 771/* 772 * Given a file with name "fname", read it (either from disk, or from 773 * an index if 'istate' is non-null), parse it and store the 774 * exclude rules in "el". 775 * 776 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 777 * stat data from disk (only valid if add_excludes returns zero). If 778 * ss_valid is non-zero, "ss" must contain good value as input. 779 */ 780static intadd_excludes(const char*fname,const char*base,int baselen, 781struct exclude_list *el,struct index_state *istate, 782struct oid_stat *oid_stat) 783{ 784struct stat st; 785int r; 786int fd; 787size_t size =0; 788char*buf; 789 790 fd =open(fname, O_RDONLY); 791if(fd <0||fstat(fd, &st) <0) { 792if(fd <0) 793warn_on_fopen_errors(fname); 794else 795close(fd); 796if(!istate) 797return-1; 798 r =read_skip_worktree_file_from_index(istate, fname, 799&size, &buf, 800 oid_stat); 801if(r !=1) 802return r; 803}else{ 804 size =xsize_t(st.st_size); 805if(size ==0) { 806if(oid_stat) { 807fill_stat_data(&oid_stat->stat, &st); 808oidcpy(&oid_stat->oid, the_hash_algo->empty_blob); 809 oid_stat->valid =1; 810} 811close(fd); 812return0; 813} 814 buf =xmallocz(size); 815if(read_in_full(fd, buf, size) != size) { 816free(buf); 817close(fd); 818return-1; 819} 820 buf[size++] ='\n'; 821close(fd); 822if(oid_stat) { 823int pos; 824if(oid_stat->valid && 825!match_stat_data_racy(istate, &oid_stat->stat, &st)) 826;/* no content change, ss->sha1 still good */ 827else if(istate && 828(pos =index_name_pos(istate, fname,strlen(fname))) >=0&& 829!ce_stage(istate->cache[pos]) && 830ce_uptodate(istate->cache[pos]) && 831!would_convert_to_git(istate, fname)) 832oidcpy(&oid_stat->oid, 833&istate->cache[pos]->oid); 834else 835hash_object_file(buf, size,"blob", 836&oid_stat->oid); 837fill_stat_data(&oid_stat->stat, &st); 838 oid_stat->valid =1; 839} 840} 841 842add_excludes_from_buffer(buf, size, base, baselen, el); 843return0; 844} 845 846static intadd_excludes_from_buffer(char*buf,size_t size, 847const char*base,int baselen, 848struct exclude_list *el) 849{ 850int i, lineno =1; 851char*entry; 852 853 el->filebuf = buf; 854 855if(skip_utf8_bom(&buf, size)) 856 size -= buf - el->filebuf; 857 858 entry = buf; 859 860for(i =0; i < size; i++) { 861if(buf[i] =='\n') { 862if(entry != buf + i && entry[0] !='#') { 863 buf[i - (i && buf[i-1] =='\r')] =0; 864trim_trailing_spaces(entry); 865add_exclude(entry, base, baselen, el, lineno); 866} 867 lineno++; 868 entry = buf + i +1; 869} 870} 871return0; 872} 873 874intadd_excludes_from_file_to_list(const char*fname,const char*base, 875int baselen,struct exclude_list *el, 876struct index_state *istate) 877{ 878returnadd_excludes(fname, base, baselen, el, istate, NULL); 879} 880 881intadd_excludes_from_blob_to_list( 882struct object_id *oid, 883const char*base,int baselen, 884struct exclude_list *el) 885{ 886char*buf; 887size_t size; 888int r; 889 890 r =do_read_blob(oid, NULL, &size, &buf); 891if(r !=1) 892return r; 893 894add_excludes_from_buffer(buf, size, base, baselen, el); 895return0; 896} 897 898struct exclude_list *add_exclude_list(struct dir_struct *dir, 899int group_type,const char*src) 900{ 901struct exclude_list *el; 902struct exclude_list_group *group; 903 904 group = &dir->exclude_list_group[group_type]; 905ALLOC_GROW(group->el, group->nr +1, group->alloc); 906 el = &group->el[group->nr++]; 907memset(el,0,sizeof(*el)); 908 el->src = src; 909return el; 910} 911 912/* 913 * Used to set up core.excludesfile and .git/info/exclude lists. 914 */ 915static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 916struct oid_stat *oid_stat) 917{ 918struct exclude_list *el; 919/* 920 * catch setup_standard_excludes() that's called before 921 * dir->untracked is assigned. That function behaves 922 * differently when dir->untracked is non-NULL. 923 */ 924if(!dir->untracked) 925 dir->unmanaged_exclude_files++; 926 el =add_exclude_list(dir, EXC_FILE, fname); 927if(add_excludes(fname,"",0, el, NULL, oid_stat) <0) 928die(_("cannot use%sas an exclude file"), fname); 929} 930 931voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 932{ 933 dir->unmanaged_exclude_files++;/* see validate_untracked_cache() */ 934add_excludes_from_file_1(dir, fname, NULL); 935} 936 937intmatch_basename(const char*basename,int basenamelen, 938const char*pattern,int prefix,int patternlen, 939unsigned flags) 940{ 941if(prefix == patternlen) { 942if(patternlen == basenamelen && 943!fspathncmp(pattern, basename, basenamelen)) 944return1; 945}else if(flags & EXC_FLAG_ENDSWITH) { 946/* "*literal" matching against "fooliteral" */ 947if(patternlen -1<= basenamelen && 948!fspathncmp(pattern +1, 949 basename + basenamelen - (patternlen -1), 950 patternlen -1)) 951return1; 952}else{ 953if(fnmatch_icase_mem(pattern, patternlen, 954 basename, basenamelen, 9550) ==0) 956return1; 957} 958return0; 959} 960 961intmatch_pathname(const char*pathname,int pathlen, 962const char*base,int baselen, 963const char*pattern,int prefix,int patternlen, 964unsigned flags) 965{ 966const char*name; 967int namelen; 968 969/* 970 * match with FNM_PATHNAME; the pattern has base implicitly 971 * in front of it. 972 */ 973if(*pattern =='/') { 974 pattern++; 975 patternlen--; 976 prefix--; 977} 978 979/* 980 * baselen does not count the trailing slash. base[] may or 981 * may not end with a trailing slash though. 982 */ 983if(pathlen < baselen +1|| 984(baselen && pathname[baselen] !='/') || 985fspathncmp(pathname, base, baselen)) 986return0; 987 988 namelen = baselen ? pathlen - baselen -1: pathlen; 989 name = pathname + pathlen - namelen; 990 991if(prefix) { 992/* 993 * if the non-wildcard part is longer than the 994 * remaining pathname, surely it cannot match. 995 */ 996if(prefix > namelen) 997return0; 998 999if(fspathncmp(pattern, name, prefix))1000return0;1001 pattern += prefix;1002 patternlen -= prefix;1003 name += prefix;1004 namelen -= prefix;10051006/*1007 * If the whole pattern did not have a wildcard,1008 * then our prefix match is all we need; we1009 * do not need to call fnmatch at all.1010 */1011if(!patternlen && !namelen)1012return1;1013}10141015returnfnmatch_icase_mem(pattern, patternlen,1016 name, namelen,1017 WM_PATHNAME) ==0;1018}10191020/*1021 * Scan the given exclude list in reverse to see whether pathname1022 * should be ignored. The first match (i.e. the last on the list), if1023 * any, determines the fate. Returns the exclude_list element which1024 * matched, or NULL for undecided.1025 */1026static struct exclude *last_exclude_matching_from_list(const char*pathname,1027int pathlen,1028const char*basename,1029int*dtype,1030struct exclude_list *el,1031struct index_state *istate)1032{1033struct exclude *exc = NULL;/* undecided */1034int i;10351036if(!el->nr)1037return NULL;/* undefined */10381039for(i = el->nr -1;0<= i; i--) {1040struct exclude *x = el->excludes[i];1041const char*exclude = x->pattern;1042int prefix = x->nowildcardlen;10431044if(x->flags & EXC_FLAG_MUSTBEDIR) {1045if(*dtype == DT_UNKNOWN)1046*dtype =get_dtype(NULL, istate, pathname, pathlen);1047if(*dtype != DT_DIR)1048continue;1049}10501051if(x->flags & EXC_FLAG_NODIR) {1052if(match_basename(basename,1053 pathlen - (basename - pathname),1054 exclude, prefix, x->patternlen,1055 x->flags)) {1056 exc = x;1057break;1058}1059continue;1060}10611062assert(x->baselen ==0|| x->base[x->baselen -1] =='/');1063if(match_pathname(pathname, pathlen,1064 x->base, x->baselen ? x->baselen -1:0,1065 exclude, prefix, x->patternlen, x->flags)) {1066 exc = x;1067break;1068}1069}1070return exc;1071}10721073/*1074 * Scan the list and let the last match determine the fate.1075 * Return 1 for exclude, 0 for include and -1 for undecided.1076 */1077intis_excluded_from_list(const char*pathname,1078int pathlen,const char*basename,int*dtype,1079struct exclude_list *el,struct index_state *istate)1080{1081struct exclude *exclude;1082 exclude =last_exclude_matching_from_list(pathname, pathlen, basename,1083 dtype, el, istate);1084if(exclude)1085return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1086return-1;/* undecided */1087}10881089static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,1090struct index_state *istate,1091const char*pathname,int pathlen,const char*basename,1092int*dtype_p)1093{1094int i, j;1095struct exclude_list_group *group;1096struct exclude *exclude;1097for(i = EXC_CMDL; i <= EXC_FILE; i++) {1098 group = &dir->exclude_list_group[i];1099for(j = group->nr -1; j >=0; j--) {1100 exclude =last_exclude_matching_from_list(1101 pathname, pathlen, basename, dtype_p,1102&group->el[j], istate);1103if(exclude)1104return exclude;1105}1106}1107return NULL;1108}11091110/*1111 * Loads the per-directory exclude list for the substring of base1112 * which has a char length of baselen.1113 */1114static voidprep_exclude(struct dir_struct *dir,1115struct index_state *istate,1116const char*base,int baselen)1117{1118struct exclude_list_group *group;1119struct exclude_list *el;1120struct exclude_stack *stk = NULL;1121struct untracked_cache_dir *untracked;1122int current;11231124 group = &dir->exclude_list_group[EXC_DIRS];11251126/*1127 * Pop the exclude lists from the EXCL_DIRS exclude_list_group1128 * which originate from directories not in the prefix of the1129 * path being checked.1130 */1131while((stk = dir->exclude_stack) != NULL) {1132if(stk->baselen <= baselen &&1133!strncmp(dir->basebuf.buf, base, stk->baselen))1134break;1135 el = &group->el[dir->exclude_stack->exclude_ix];1136 dir->exclude_stack = stk->prev;1137 dir->exclude = NULL;1138free((char*)el->src);/* see strbuf_detach() below */1139clear_exclude_list(el);1140free(stk);1141 group->nr--;1142}11431144/* Skip traversing into sub directories if the parent is excluded */1145if(dir->exclude)1146return;11471148/*1149 * Lazy initialization. All call sites currently just1150 * memset(dir, 0, sizeof(*dir)) before use. Changing all of1151 * them seems lots of work for little benefit.1152 */1153if(!dir->basebuf.buf)1154strbuf_init(&dir->basebuf, PATH_MAX);11551156/* Read from the parent directories and push them down. */1157 current = stk ? stk->baselen : -1;1158strbuf_setlen(&dir->basebuf, current <0?0: current);1159if(dir->untracked)1160 untracked = stk ? stk->ucd : dir->untracked->root;1161else1162 untracked = NULL;11631164while(current < baselen) {1165const char*cp;1166struct oid_stat oid_stat;11671168 stk =xcalloc(1,sizeof(*stk));1169if(current <0) {1170 cp = base;1171 current =0;1172}else{1173 cp =strchr(base + current +1,'/');1174if(!cp)1175die("oops in prep_exclude");1176 cp++;1177 untracked =1178lookup_untracked(dir->untracked, untracked,1179 base + current,1180 cp - base - current);1181}1182 stk->prev = dir->exclude_stack;1183 stk->baselen = cp - base;1184 stk->exclude_ix = group->nr;1185 stk->ucd = untracked;1186 el =add_exclude_list(dir, EXC_DIRS, NULL);1187strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1188assert(stk->baselen == dir->basebuf.len);11891190/* Abort if the directory is excluded */1191if(stk->baselen) {1192int dt = DT_DIR;1193 dir->basebuf.buf[stk->baselen -1] =0;1194 dir->exclude =last_exclude_matching_from_lists(dir,1195 istate,1196 dir->basebuf.buf, stk->baselen -1,1197 dir->basebuf.buf + current, &dt);1198 dir->basebuf.buf[stk->baselen -1] ='/';1199if(dir->exclude &&1200 dir->exclude->flags & EXC_FLAG_NEGATIVE)1201 dir->exclude = NULL;1202if(dir->exclude) {1203 dir->exclude_stack = stk;1204return;1205}1206}12071208/* Try to read per-directory file */1209oidclr(&oid_stat.oid);1210 oid_stat.valid =0;1211if(dir->exclude_per_dir &&1212/*1213 * If we know that no files have been added in1214 * this directory (i.e. valid_cached_dir() has1215 * been executed and set untracked->valid) ..1216 */1217(!untracked || !untracked->valid ||1218/*1219 * .. and .gitignore does not exist before1220 * (i.e. null exclude_oid). Then we can skip1221 * loading .gitignore, which would result in1222 * ENOENT anyway.1223 */1224!is_null_oid(&untracked->exclude_oid))) {1225/*1226 * dir->basebuf gets reused by the traversal, but we1227 * need fname to remain unchanged to ensure the src1228 * member of each struct exclude correctly1229 * back-references its source file. Other invocations1230 * of add_exclude_list provide stable strings, so we1231 * strbuf_detach() and free() here in the caller.1232 */1233struct strbuf sb = STRBUF_INIT;1234strbuf_addbuf(&sb, &dir->basebuf);1235strbuf_addstr(&sb, dir->exclude_per_dir);1236 el->src =strbuf_detach(&sb, NULL);1237add_excludes(el->src, el->src, stk->baselen, el, istate,1238 untracked ? &oid_stat : NULL);1239}1240/*1241 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1242 * will first be called in valid_cached_dir() then maybe many1243 * times more in last_exclude_matching(). When the cache is1244 * used, last_exclude_matching() will not be called and1245 * reading .gitignore content will be a waste.1246 *1247 * So when it's called by valid_cached_dir() and we can get1248 * .gitignore SHA-1 from the index (i.e. .gitignore is not1249 * modified on work tree), we could delay reading the1250 * .gitignore content until we absolutely need it in1251 * last_exclude_matching(). Be careful about ignore rule1252 * order, though, if you do that.1253 */1254if(untracked &&1255!oideq(&oid_stat.oid, &untracked->exclude_oid)) {1256invalidate_gitignore(dir->untracked, untracked);1257oidcpy(&untracked->exclude_oid, &oid_stat.oid);1258}1259 dir->exclude_stack = stk;1260 current = stk->baselen;1261}1262strbuf_setlen(&dir->basebuf, baselen);1263}12641265/*1266 * Loads the exclude lists for the directory containing pathname, then1267 * scans all exclude lists to determine whether pathname is excluded.1268 * Returns the exclude_list element which matched, or NULL for1269 * undecided.1270 */1271struct exclude *last_exclude_matching(struct dir_struct *dir,1272struct index_state *istate,1273const char*pathname,1274int*dtype_p)1275{1276int pathlen =strlen(pathname);1277const char*basename =strrchr(pathname,'/');1278 basename = (basename) ? basename+1: pathname;12791280prep_exclude(dir, istate, pathname, basename-pathname);12811282if(dir->exclude)1283return dir->exclude;12841285returnlast_exclude_matching_from_lists(dir, istate, pathname, pathlen,1286 basename, dtype_p);1287}12881289/*1290 * Loads the exclude lists for the directory containing pathname, then1291 * scans all exclude lists to determine whether pathname is excluded.1292 * Returns 1 if true, otherwise 0.1293 */1294intis_excluded(struct dir_struct *dir,struct index_state *istate,1295const char*pathname,int*dtype_p)1296{1297struct exclude *exclude =1298last_exclude_matching(dir, istate, pathname, dtype_p);1299if(exclude)1300return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1301return0;1302}13031304static struct dir_entry *dir_entry_new(const char*pathname,int len)1305{1306struct dir_entry *ent;13071308FLEX_ALLOC_MEM(ent, name, pathname, len);1309 ent->len = len;1310return ent;1311}13121313static struct dir_entry *dir_add_name(struct dir_struct *dir,1314struct index_state *istate,1315const char*pathname,int len)1316{1317if(index_file_exists(istate, pathname, len, ignore_case))1318return NULL;13191320ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1321return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1322}13231324struct dir_entry *dir_add_ignored(struct dir_struct *dir,1325struct index_state *istate,1326const char*pathname,int len)1327{1328if(!index_name_is_other(istate, pathname, len))1329return NULL;13301331ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1332return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1333}13341335enum exist_status {1336 index_nonexistent =0,1337 index_directory,1338 index_gitdir1339};13401341/*1342 * Do not use the alphabetically sorted index to look up1343 * the directory name; instead, use the case insensitive1344 * directory hash.1345 */1346static enum exist_status directory_exists_in_index_icase(struct index_state *istate,1347const char*dirname,int len)1348{1349struct cache_entry *ce;13501351if(index_dir_exists(istate, dirname, len))1352return index_directory;13531354 ce =index_file_exists(istate, dirname, len, ignore_case);1355if(ce &&S_ISGITLINK(ce->ce_mode))1356return index_gitdir;13571358return index_nonexistent;1359}13601361/*1362 * The index sorts alphabetically by entry name, which1363 * means that a gitlink sorts as '\0' at the end, while1364 * a directory (which is defined not as an entry, but as1365 * the files it contains) will sort with the '/' at the1366 * end.1367 */1368static enum exist_status directory_exists_in_index(struct index_state *istate,1369const char*dirname,int len)1370{1371int pos;13721373if(ignore_case)1374returndirectory_exists_in_index_icase(istate, dirname, len);13751376 pos =index_name_pos(istate, dirname, len);1377if(pos <0)1378 pos = -pos-1;1379while(pos < istate->cache_nr) {1380const struct cache_entry *ce = istate->cache[pos++];1381unsigned char endchar;13821383if(strncmp(ce->name, dirname, len))1384break;1385 endchar = ce->name[len];1386if(endchar >'/')1387break;1388if(endchar =='/')1389return index_directory;1390if(!endchar &&S_ISGITLINK(ce->ce_mode))1391return index_gitdir;1392}1393return index_nonexistent;1394}13951396/*1397 * When we find a directory when traversing the filesystem, we1398 * have three distinct cases:1399 *1400 * - ignore it1401 * - see it as a directory1402 * - recurse into it1403 *1404 * and which one we choose depends on a combination of existing1405 * git index contents and the flags passed into the directory1406 * traversal routine.1407 *1408 * Case 1: If we *already* have entries in the index under that1409 * directory name, we always recurse into the directory to see1410 * all the files.1411 *1412 * Case 2: If we *already* have that directory name as a gitlink,1413 * we always continue to see it as a gitlink, regardless of whether1414 * there is an actual git directory there or not (it might not1415 * be checked out as a subproject!)1416 *1417 * Case 3: if we didn't have it in the index previously, we1418 * have a few sub-cases:1419 *1420 * (a) if "show_other_directories" is true, we show it as1421 * just a directory, unless "hide_empty_directories" is1422 * also true, in which case we need to check if it contains any1423 * untracked and / or ignored files.1424 * (b) if it looks like a git directory, and we don't have1425 * 'no_gitlinks' set we treat it as a gitlink, and show it1426 * as a directory.1427 * (c) otherwise, we recurse into it.1428 */1429static enum path_treatment treat_directory(struct dir_struct *dir,1430struct index_state *istate,1431struct untracked_cache_dir *untracked,1432const char*dirname,int len,int baselen,int exclude,1433const struct pathspec *pathspec)1434{1435/* The "len-1" is to strip the final '/' */1436switch(directory_exists_in_index(istate, dirname, len-1)) {1437case index_directory:1438return path_recurse;14391440case index_gitdir:1441return path_none;14421443case index_nonexistent:1444if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1445break;1446if(exclude &&1447(dir->flags & DIR_SHOW_IGNORED_TOO) &&1448(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {14491450/*1451 * This is an excluded directory and we are1452 * showing ignored paths that match an exclude1453 * pattern. (e.g. show directory as ignored1454 * only if it matches an exclude pattern).1455 * This path will either be 'path_excluded`1456 * (if we are showing empty directories or if1457 * the directory is not empty), or will be1458 * 'path_none' (empty directory, and we are1459 * not showing empty directories).1460 */1461if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1462return path_excluded;14631464if(read_directory_recursive(dir, istate, dirname, len,1465 untracked,1,1, pathspec) == path_excluded)1466return path_excluded;14671468return path_none;1469}1470if(!(dir->flags & DIR_NO_GITLINKS)) {1471struct object_id oid;1472if(resolve_gitlink_ref(dirname,"HEAD", &oid) ==0)1473return exclude ? path_excluded : path_untracked;1474}1475return path_recurse;1476}14771478/* This is the "show_other_directories" case */14791480if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1481return exclude ? path_excluded : path_untracked;14821483 untracked =lookup_untracked(dir->untracked, untracked,1484 dirname + baselen, len - baselen);14851486/*1487 * If this is an excluded directory, then we only need to check if1488 * the directory contains any files.1489 */1490returnread_directory_recursive(dir, istate, dirname, len,1491 untracked,1, exclude, pathspec);1492}14931494/*1495 * This is an inexact early pruning of any recursive directory1496 * reading - if the path cannot possibly be in the pathspec,1497 * return true, and we'll skip it early.1498 */1499static intsimplify_away(const char*path,int pathlen,1500const struct pathspec *pathspec)1501{1502int i;15031504if(!pathspec || !pathspec->nr)1505return0;15061507GUARD_PATHSPEC(pathspec,1508 PATHSPEC_FROMTOP |1509 PATHSPEC_MAXDEPTH |1510 PATHSPEC_LITERAL |1511 PATHSPEC_GLOB |1512 PATHSPEC_ICASE |1513 PATHSPEC_EXCLUDE |1514 PATHSPEC_ATTR);15151516for(i =0; i < pathspec->nr; i++) {1517const struct pathspec_item *item = &pathspec->items[i];1518int len = item->nowildcard_len;15191520if(len > pathlen)1521 len = pathlen;1522if(!ps_strncmp(item, item->match, path, len))1523return0;1524}15251526return1;1527}15281529/*1530 * This function tells us whether an excluded path matches a1531 * list of "interesting" pathspecs. That is, whether a path matched1532 * by any of the pathspecs could possibly be ignored by excluding1533 * the specified path. This can happen if:1534 *1535 * 1. the path is mentioned explicitly in the pathspec1536 *1537 * 2. the path is a directory prefix of some element in the1538 * pathspec1539 */1540static intexclude_matches_pathspec(const char*path,int pathlen,1541const struct pathspec *pathspec)1542{1543int i;15441545if(!pathspec || !pathspec->nr)1546return0;15471548GUARD_PATHSPEC(pathspec,1549 PATHSPEC_FROMTOP |1550 PATHSPEC_MAXDEPTH |1551 PATHSPEC_LITERAL |1552 PATHSPEC_GLOB |1553 PATHSPEC_ICASE |1554 PATHSPEC_EXCLUDE);15551556for(i =0; i < pathspec->nr; i++) {1557const struct pathspec_item *item = &pathspec->items[i];1558int len = item->nowildcard_len;15591560if(len == pathlen &&1561!ps_strncmp(item, item->match, path, pathlen))1562return1;1563if(len > pathlen &&1564 item->match[pathlen] =='/'&&1565!ps_strncmp(item, item->match, path, pathlen))1566return1;1567}1568return0;1569}15701571static intget_index_dtype(struct index_state *istate,1572const char*path,int len)1573{1574int pos;1575const struct cache_entry *ce;15761577 ce =index_file_exists(istate, path, len,0);1578if(ce) {1579if(!ce_uptodate(ce))1580return DT_UNKNOWN;1581if(S_ISGITLINK(ce->ce_mode))1582return DT_DIR;1583/*1584 * Nobody actually cares about the1585 * difference between DT_LNK and DT_REG1586 */1587return DT_REG;1588}15891590/* Try to look it up as a directory */1591 pos =index_name_pos(istate, path, len);1592if(pos >=0)1593return DT_UNKNOWN;1594 pos = -pos-1;1595while(pos < istate->cache_nr) {1596 ce = istate->cache[pos++];1597if(strncmp(ce->name, path, len))1598break;1599if(ce->name[len] >'/')1600break;1601if(ce->name[len] <'/')1602continue;1603if(!ce_uptodate(ce))1604break;/* continue? */1605return DT_DIR;1606}1607return DT_UNKNOWN;1608}16091610static intget_dtype(struct dirent *de,struct index_state *istate,1611const char*path,int len)1612{1613int dtype = de ?DTYPE(de) : DT_UNKNOWN;1614struct stat st;16151616if(dtype != DT_UNKNOWN)1617return dtype;1618 dtype =get_index_dtype(istate, path, len);1619if(dtype != DT_UNKNOWN)1620return dtype;1621if(lstat(path, &st))1622return dtype;1623if(S_ISREG(st.st_mode))1624return DT_REG;1625if(S_ISDIR(st.st_mode))1626return DT_DIR;1627if(S_ISLNK(st.st_mode))1628return DT_LNK;1629return dtype;1630}16311632static enum path_treatment treat_one_path(struct dir_struct *dir,1633struct untracked_cache_dir *untracked,1634struct index_state *istate,1635struct strbuf *path,1636int baselen,1637const struct pathspec *pathspec,1638int dtype,struct dirent *de)1639{1640int exclude;1641int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);1642enum path_treatment path_treatment;16431644if(dtype == DT_UNKNOWN)1645 dtype =get_dtype(de, istate, path->buf, path->len);16461647/* Always exclude indexed files */1648if(dtype != DT_DIR && has_path_in_index)1649return path_none;16501651/*1652 * When we are looking at a directory P in the working tree,1653 * there are three cases:1654 *1655 * (1) P exists in the index. Everything inside the directory P in1656 * the working tree needs to go when P is checked out from the1657 * index.1658 *1659 * (2) P does not exist in the index, but there is P/Q in the index.1660 * We know P will stay a directory when we check out the contents1661 * of the index, but we do not know yet if there is a directory1662 * P/Q in the working tree to be killed, so we need to recurse.1663 *1664 * (3) P does not exist in the index, and there is no P/Q in the index1665 * to require P to be a directory, either. Only in this case, we1666 * know that everything inside P will not be killed without1667 * recursing.1668 */1669if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1670(dtype == DT_DIR) &&1671!has_path_in_index &&1672(directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))1673return path_none;16741675 exclude =is_excluded(dir, istate, path->buf, &dtype);16761677/*1678 * Excluded? If we don't explicitly want to show1679 * ignored files, ignore it1680 */1681if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1682return path_excluded;16831684switch(dtype) {1685default:1686return path_none;1687case DT_DIR:1688strbuf_addch(path,'/');1689 path_treatment =treat_directory(dir, istate, untracked,1690 path->buf, path->len,1691 baselen, exclude, pathspec);1692/*1693 * If 1) we only want to return directories that1694 * match an exclude pattern and 2) this directory does1695 * not match an exclude pattern but all of its1696 * contents are excluded, then indicate that we should1697 * recurse into this directory (instead of marking the1698 * directory itself as an ignored path).1699 */1700if(!exclude &&1701 path_treatment == path_excluded &&1702(dir->flags & DIR_SHOW_IGNORED_TOO) &&1703(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING))1704return path_recurse;1705return path_treatment;1706case DT_REG:1707case DT_LNK:1708return exclude ? path_excluded : path_untracked;1709}1710}17111712static enum path_treatment treat_path_fast(struct dir_struct *dir,1713struct untracked_cache_dir *untracked,1714struct cached_dir *cdir,1715struct index_state *istate,1716struct strbuf *path,1717int baselen,1718const struct pathspec *pathspec)1719{1720strbuf_setlen(path, baselen);1721if(!cdir->ucd) {1722strbuf_addstr(path, cdir->file);1723return path_untracked;1724}1725strbuf_addstr(path, cdir->ucd->name);1726/* treat_one_path() does this before it calls treat_directory() */1727strbuf_complete(path,'/');1728if(cdir->ucd->check_only)1729/*1730 * check_only is set as a result of treat_directory() getting1731 * to its bottom. Verify again the same set of directories1732 * with check_only set.1733 */1734returnread_directory_recursive(dir, istate, path->buf, path->len,1735 cdir->ucd,1,0, pathspec);1736/*1737 * We get path_recurse in the first run when1738 * directory_exists_in_index() returns index_nonexistent. We1739 * are sure that new changes in the index does not impact the1740 * outcome. Return now.1741 */1742return path_recurse;1743}17441745static enum path_treatment treat_path(struct dir_struct *dir,1746struct untracked_cache_dir *untracked,1747struct cached_dir *cdir,1748struct index_state *istate,1749struct strbuf *path,1750int baselen,1751const struct pathspec *pathspec)1752{1753int dtype;1754struct dirent *de = cdir->de;17551756if(!de)1757returntreat_path_fast(dir, untracked, cdir, istate, path,1758 baselen, pathspec);1759if(is_dot_or_dotdot(de->d_name) || !fspathcmp(de->d_name,".git"))1760return path_none;1761strbuf_setlen(path, baselen);1762strbuf_addstr(path, de->d_name);1763if(simplify_away(path->buf, path->len, pathspec))1764return path_none;17651766 dtype =DTYPE(de);1767returntreat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);1768}17691770static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1771{1772if(!dir)1773return;1774ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1775 dir->untracked_alloc);1776 dir->untracked[dir->untracked_nr++] =xstrdup(name);1777}17781779static intvalid_cached_dir(struct dir_struct *dir,1780struct untracked_cache_dir *untracked,1781struct index_state *istate,1782struct strbuf *path,1783int check_only)1784{1785struct stat st;17861787if(!untracked)1788return0;17891790/*1791 * With fsmonitor, we can trust the untracked cache's valid field.1792 */1793refresh_fsmonitor(istate);1794if(!(dir->untracked->use_fsmonitor && untracked->valid)) {1795if(lstat(path->len ? path->buf :".", &st)) {1796memset(&untracked->stat_data,0,sizeof(untracked->stat_data));1797return0;1798}1799if(!untracked->valid ||1800match_stat_data_racy(istate, &untracked->stat_data, &st)) {1801fill_stat_data(&untracked->stat_data, &st);1802return0;1803}1804}18051806if(untracked->check_only != !!check_only)1807return0;18081809/*1810 * prep_exclude will be called eventually on this directory,1811 * but it's called much later in last_exclude_matching(). We1812 * need it now to determine the validity of the cache for this1813 * path. The next calls will be nearly no-op, the way1814 * prep_exclude() is designed.1815 */1816if(path->len && path->buf[path->len -1] !='/') {1817strbuf_addch(path,'/');1818prep_exclude(dir, istate, path->buf, path->len);1819strbuf_setlen(path, path->len -1);1820}else1821prep_exclude(dir, istate, path->buf, path->len);18221823/* hopefully prep_exclude() haven't invalidated this entry... */1824return untracked->valid;1825}18261827static intopen_cached_dir(struct cached_dir *cdir,1828struct dir_struct *dir,1829struct untracked_cache_dir *untracked,1830struct index_state *istate,1831struct strbuf *path,1832int check_only)1833{1834const char*c_path;18351836memset(cdir,0,sizeof(*cdir));1837 cdir->untracked = untracked;1838if(valid_cached_dir(dir, untracked, istate, path, check_only))1839return0;1840 c_path = path->len ? path->buf :".";1841 cdir->fdir =opendir(c_path);1842if(!cdir->fdir)1843warning_errno(_("could not open directory '%s'"), c_path);1844if(dir->untracked) {1845invalidate_directory(dir->untracked, untracked);1846 dir->untracked->dir_opened++;1847}1848if(!cdir->fdir)1849return-1;1850return0;1851}18521853static intread_cached_dir(struct cached_dir *cdir)1854{1855if(cdir->fdir) {1856 cdir->de =readdir(cdir->fdir);1857if(!cdir->de)1858return-1;1859return0;1860}1861while(cdir->nr_dirs < cdir->untracked->dirs_nr) {1862struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1863if(!d->recurse) {1864 cdir->nr_dirs++;1865continue;1866}1867 cdir->ucd = d;1868 cdir->nr_dirs++;1869return0;1870}1871 cdir->ucd = NULL;1872if(cdir->nr_files < cdir->untracked->untracked_nr) {1873struct untracked_cache_dir *d = cdir->untracked;1874 cdir->file = d->untracked[cdir->nr_files++];1875return0;1876}1877return-1;1878}18791880static voidclose_cached_dir(struct cached_dir *cdir)1881{1882if(cdir->fdir)1883closedir(cdir->fdir);1884/*1885 * We have gone through this directory and found no untracked1886 * entries. Mark it valid.1887 */1888if(cdir->untracked) {1889 cdir->untracked->valid =1;1890 cdir->untracked->recurse =1;1891}1892}18931894/*1895 * Read a directory tree. We currently ignore anything but1896 * directories, regular files and symlinks. That's because git1897 * doesn't handle them at all yet. Maybe that will change some1898 * day.1899 *1900 * Also, we ignore the name ".git" (even if it is not a directory).1901 * That likely will not change.1902 *1903 * If 'stop_at_first_file' is specified, 'path_excluded' is returned1904 * to signal that a file was found. This is the least significant value that1905 * indicates that a file was encountered that does not depend on the order of1906 * whether an untracked or exluded path was encountered first.1907 *1908 * Returns the most significant path_treatment value encountered in the scan.1909 * If 'stop_at_first_file' is specified, `path_excluded` is the most1910 * significant path_treatment value that will be returned.1911 */19121913static enum path_treatment read_directory_recursive(struct dir_struct *dir,1914struct index_state *istate,const char*base,int baselen,1915struct untracked_cache_dir *untracked,int check_only,1916int stop_at_first_file,const struct pathspec *pathspec)1917{1918struct cached_dir cdir;1919enum path_treatment state, subdir_state, dir_state = path_none;1920struct strbuf path = STRBUF_INIT;19211922strbuf_add(&path, base, baselen);19231924if(open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))1925goto out;19261927if(untracked)1928 untracked->check_only = !!check_only;19291930while(!read_cached_dir(&cdir)) {1931/* check how the file or directory should be treated */1932 state =treat_path(dir, untracked, &cdir, istate, &path,1933 baselen, pathspec);19341935if(state > dir_state)1936 dir_state = state;19371938/* recurse into subdir if instructed by treat_path */1939if((state == path_recurse) ||1940((state == path_untracked) &&1941(dir->flags & DIR_SHOW_IGNORED_TOO) &&1942(get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {1943struct untracked_cache_dir *ud;1944 ud =lookup_untracked(dir->untracked, untracked,1945 path.buf + baselen,1946 path.len - baselen);1947 subdir_state =1948read_directory_recursive(dir, istate, path.buf,1949 path.len, ud,1950 check_only, stop_at_first_file, pathspec);1951if(subdir_state > dir_state)1952 dir_state = subdir_state;1953}19541955if(check_only) {1956if(stop_at_first_file) {1957/*1958 * If stopping at first file, then1959 * signal that a file was found by1960 * returning `path_excluded`. This is1961 * to return a consistent value1962 * regardless of whether an ignored or1963 * excluded file happened to be1964 * encountered 1st.1965 *1966 * In current usage, the1967 * `stop_at_first_file` is passed when1968 * an ancestor directory has matched1969 * an exclude pattern, so any found1970 * files will be excluded.1971 */1972if(dir_state >= path_excluded) {1973 dir_state = path_excluded;1974break;1975}1976}19771978/* abort early if maximum state has been reached */1979if(dir_state == path_untracked) {1980if(cdir.fdir)1981add_untracked(untracked, path.buf + baselen);1982break;1983}1984/* skip the dir_add_* part */1985continue;1986}19871988/* add the path to the appropriate result list */1989switch(state) {1990case path_excluded:1991if(dir->flags & DIR_SHOW_IGNORED)1992dir_add_name(dir, istate, path.buf, path.len);1993else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1994((dir->flags & DIR_COLLECT_IGNORED) &&1995exclude_matches_pathspec(path.buf, path.len,1996 pathspec)))1997dir_add_ignored(dir, istate, path.buf, path.len);1998break;19992000case path_untracked:2001if(dir->flags & DIR_SHOW_IGNORED)2002break;2003dir_add_name(dir, istate, path.buf, path.len);2004if(cdir.fdir)2005add_untracked(untracked, path.buf + baselen);2006break;20072008default:2009break;2010}2011}2012close_cached_dir(&cdir);2013 out:2014strbuf_release(&path);20152016return dir_state;2017}20182019intcmp_dir_entry(const void*p1,const void*p2)2020{2021const struct dir_entry *e1 = *(const struct dir_entry **)p1;2022const struct dir_entry *e2 = *(const struct dir_entry **)p2;20232024returnname_compare(e1->name, e1->len, e2->name, e2->len);2025}20262027/* check if *out lexically strictly contains *in */2028intcheck_dir_entry_contains(const struct dir_entry *out,const struct dir_entry *in)2029{2030return(out->len < in->len) &&2031(out->name[out->len -1] =='/') &&2032!memcmp(out->name, in->name, out->len);2033}20342035static inttreat_leading_path(struct dir_struct *dir,2036struct index_state *istate,2037const char*path,int len,2038const struct pathspec *pathspec)2039{2040struct strbuf sb = STRBUF_INIT;2041int baselen, rc =0;2042const char*cp;2043int old_flags = dir->flags;20442045while(len && path[len -1] =='/')2046 len--;2047if(!len)2048return1;2049 baselen =0;2050 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;2051while(1) {2052 cp = path + baselen + !!baselen;2053 cp =memchr(cp,'/', path + len - cp);2054if(!cp)2055 baselen = len;2056else2057 baselen = cp - path;2058strbuf_setlen(&sb,0);2059strbuf_add(&sb, path, baselen);2060if(!is_directory(sb.buf))2061break;2062if(simplify_away(sb.buf, sb.len, pathspec))2063break;2064if(treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,2065 DT_DIR, NULL) == path_none)2066break;/* do not recurse into it */2067if(len <= baselen) {2068 rc =1;2069break;/* finished checking */2070}2071}2072strbuf_release(&sb);2073 dir->flags = old_flags;2074return rc;2075}20762077static const char*get_ident_string(void)2078{2079static struct strbuf sb = STRBUF_INIT;2080struct utsname uts;20812082if(sb.len)2083return sb.buf;2084if(uname(&uts) <0)2085die_errno(_("failed to get kernel name and information"));2086strbuf_addf(&sb,"Location%s, system%s",get_git_work_tree(),2087 uts.sysname);2088return sb.buf;2089}20902091static intident_in_untracked(const struct untracked_cache *uc)2092{2093/*2094 * Previous git versions may have saved many NUL separated2095 * strings in the "ident" field, but it is insane to manage2096 * many locations, so just take care of the first one.2097 */20982099return!strcmp(uc->ident.buf,get_ident_string());2100}21012102static voidset_untracked_ident(struct untracked_cache *uc)2103{2104strbuf_reset(&uc->ident);2105strbuf_addstr(&uc->ident,get_ident_string());21062107/*2108 * This strbuf used to contain a list of NUL separated2109 * strings, so save NUL too for backward compatibility.2110 */2111strbuf_addch(&uc->ident,0);2112}21132114static voidnew_untracked_cache(struct index_state *istate)2115{2116struct untracked_cache *uc =xcalloc(1,sizeof(*uc));2117strbuf_init(&uc->ident,100);2118 uc->exclude_per_dir =".gitignore";2119/* should be the same flags used by git-status */2120 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;2121set_untracked_ident(uc);2122 istate->untracked = uc;2123 istate->cache_changed |= UNTRACKED_CHANGED;2124}21252126voidadd_untracked_cache(struct index_state *istate)2127{2128if(!istate->untracked) {2129new_untracked_cache(istate);2130}else{2131if(!ident_in_untracked(istate->untracked)) {2132free_untracked_cache(istate->untracked);2133new_untracked_cache(istate);2134}2135}2136}21372138voidremove_untracked_cache(struct index_state *istate)2139{2140if(istate->untracked) {2141free_untracked_cache(istate->untracked);2142 istate->untracked = NULL;2143 istate->cache_changed |= UNTRACKED_CHANGED;2144}2145}21462147static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,2148int base_len,2149const struct pathspec *pathspec)2150{2151struct untracked_cache_dir *root;2152static int untracked_cache_disabled = -1;21532154if(!dir->untracked)2155return NULL;2156if(untracked_cache_disabled <0)2157 untracked_cache_disabled =git_env_bool("GIT_DISABLE_UNTRACKED_CACHE",0);2158if(untracked_cache_disabled)2159return NULL;21602161/*2162 * We only support $GIT_DIR/info/exclude and core.excludesfile2163 * as the global ignore rule files. Any other additions2164 * (e.g. from command line) invalidate the cache. This2165 * condition also catches running setup_standard_excludes()2166 * before setting dir->untracked!2167 */2168if(dir->unmanaged_exclude_files)2169return NULL;21702171/*2172 * Optimize for the main use case only: whole-tree git2173 * status. More work involved in treat_leading_path() if we2174 * use cache on just a subset of the worktree. pathspec2175 * support could make the matter even worse.2176 */2177if(base_len || (pathspec && pathspec->nr))2178return NULL;21792180/* Different set of flags may produce different results */2181if(dir->flags != dir->untracked->dir_flags ||2182/*2183 * See treat_directory(), case index_nonexistent. Without2184 * this flag, we may need to also cache .git file content2185 * for the resolve_gitlink_ref() call, which we don't.2186 */2187!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||2188/* We don't support collecting ignore files */2189(dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |2190 DIR_COLLECT_IGNORED)))2191return NULL;21922193/*2194 * If we use .gitignore in the cache and now you change it to2195 * .gitexclude, everything will go wrong.2196 */2197if(dir->exclude_per_dir != dir->untracked->exclude_per_dir &&2198strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))2199return NULL;22002201/*2202 * EXC_CMDL is not considered in the cache. If people set it,2203 * skip the cache.2204 */2205if(dir->exclude_list_group[EXC_CMDL].nr)2206return NULL;22072208if(!ident_in_untracked(dir->untracked)) {2209warning(_("untracked cache is disabled on this system or location"));2210return NULL;2211}22122213if(!dir->untracked->root) {2214const int len =sizeof(*dir->untracked->root);2215 dir->untracked->root =xmalloc(len);2216memset(dir->untracked->root,0, len);2217}22182219/* Validate $GIT_DIR/info/exclude and core.excludesfile */2220 root = dir->untracked->root;2221if(!oideq(&dir->ss_info_exclude.oid,2222&dir->untracked->ss_info_exclude.oid)) {2223invalidate_gitignore(dir->untracked, root);2224 dir->untracked->ss_info_exclude = dir->ss_info_exclude;2225}2226if(!oideq(&dir->ss_excludes_file.oid,2227&dir->untracked->ss_excludes_file.oid)) {2228invalidate_gitignore(dir->untracked, root);2229 dir->untracked->ss_excludes_file = dir->ss_excludes_file;2230}22312232/* Make sure this directory is not dropped out at saving phase */2233 root->recurse =1;2234return root;2235}22362237intread_directory(struct dir_struct *dir,struct index_state *istate,2238const char*path,int len,const struct pathspec *pathspec)2239{2240struct untracked_cache_dir *untracked;22412242trace_performance_enter();22432244if(has_symlink_leading_path(path, len)) {2245trace_performance_leave("read directory %.*s", len, path);2246return dir->nr;2247}22482249 untracked =validate_untracked_cache(dir, len, pathspec);2250if(!untracked)2251/*2252 * make sure untracked cache code path is disabled,2253 * e.g. prep_exclude()2254 */2255 dir->untracked = NULL;2256if(!len ||treat_leading_path(dir, istate, path, len, pathspec))2257read_directory_recursive(dir, istate, path, len, untracked,0,0, pathspec);2258QSORT(dir->entries, dir->nr, cmp_dir_entry);2259QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);22602261/*2262 * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will2263 * also pick up untracked contents of untracked dirs; by default2264 * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.2265 */2266if((dir->flags & DIR_SHOW_IGNORED_TOO) &&2267!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {2268int i, j;22692270/* remove from dir->entries untracked contents of untracked dirs */2271for(i = j =0; j < dir->nr; j++) {2272if(i &&2273check_dir_entry_contains(dir->entries[i -1], dir->entries[j])) {2274FREE_AND_NULL(dir->entries[j]);2275}else{2276 dir->entries[i++] = dir->entries[j];2277}2278}22792280 dir->nr = i;2281}22822283trace_performance_leave("read directory %.*s", len, path);2284if(dir->untracked) {2285static int force_untracked_cache = -1;2286static struct trace_key trace_untracked_stats =TRACE_KEY_INIT(UNTRACKED_STATS);22872288if(force_untracked_cache <0)2289 force_untracked_cache =2290git_env_bool("GIT_FORCE_UNTRACKED_CACHE",0);2291trace_printf_key(&trace_untracked_stats,2292"node creation:%u\n"2293"gitignore invalidation:%u\n"2294"directory invalidation:%u\n"2295"opendir:%u\n",2296 dir->untracked->dir_created,2297 dir->untracked->gitignore_invalidated,2298 dir->untracked->dir_invalidated,2299 dir->untracked->dir_opened);2300if(force_untracked_cache &&2301 dir->untracked == istate->untracked &&2302(dir->untracked->dir_opened ||2303 dir->untracked->gitignore_invalidated ||2304 dir->untracked->dir_invalidated))2305 istate->cache_changed |= UNTRACKED_CHANGED;2306if(dir->untracked != istate->untracked) {2307FREE_AND_NULL(dir->untracked);2308}2309}2310return dir->nr;2311}23122313intfile_exists(const char*f)2314{2315struct stat sb;2316returnlstat(f, &sb) ==0;2317}23182319static intcmp_icase(char a,char b)2320{2321if(a == b)2322return0;2323if(ignore_case)2324returntoupper(a) -toupper(b);2325return a - b;2326}23272328/*2329 * Given two normalized paths (a trailing slash is ok), if subdir is2330 * outside dir, return -1. Otherwise return the offset in subdir that2331 * can be used as relative path to dir.2332 */2333intdir_inside_of(const char*subdir,const char*dir)2334{2335int offset =0;23362337assert(dir && subdir && *dir && *subdir);23382339while(*dir && *subdir && !cmp_icase(*dir, *subdir)) {2340 dir++;2341 subdir++;2342 offset++;2343}23442345/* hel[p]/me vs hel[l]/yeah */2346if(*dir && *subdir)2347return-1;23482349if(!*subdir)2350return!*dir ? offset : -1;/* same dir */23512352/* foo/[b]ar vs foo/[] */2353if(is_dir_sep(dir[-1]))2354returnis_dir_sep(subdir[-1]) ? offset : -1;23552356/* foo[/]bar vs foo[] */2357returnis_dir_sep(*subdir) ? offset +1: -1;2358}23592360intis_inside_dir(const char*dir)2361{2362char*cwd;2363int rc;23642365if(!dir)2366return0;23672368 cwd =xgetcwd();2369 rc = (dir_inside_of(cwd, dir) >=0);2370free(cwd);2371return rc;2372}23732374intis_empty_dir(const char*path)2375{2376DIR*dir =opendir(path);2377struct dirent *e;2378int ret =1;23792380if(!dir)2381return0;23822383while((e =readdir(dir)) != NULL)2384if(!is_dot_or_dotdot(e->d_name)) {2385 ret =0;2386break;2387}23882389closedir(dir);2390return ret;2391}23922393static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)2394{2395DIR*dir;2396struct dirent *e;2397int ret =0, original_len = path->len, len, kept_down =0;2398int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2399int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2400struct object_id submodule_head;24012402if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2403!resolve_gitlink_ref(path->buf,"HEAD", &submodule_head)) {2404/* Do not descend and nuke a nested git work tree. */2405if(kept_up)2406*kept_up =1;2407return0;2408}24092410 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2411 dir =opendir(path->buf);2412if(!dir) {2413if(errno == ENOENT)2414return keep_toplevel ? -1:0;2415else if(errno == EACCES && !keep_toplevel)2416/*2417 * An empty dir could be removable even if it2418 * is unreadable:2419 */2420returnrmdir(path->buf);2421else2422return-1;2423}2424strbuf_complete(path,'/');24252426 len = path->len;2427while((e =readdir(dir)) != NULL) {2428struct stat st;2429if(is_dot_or_dotdot(e->d_name))2430continue;24312432strbuf_setlen(path, len);2433strbuf_addstr(path, e->d_name);2434if(lstat(path->buf, &st)) {2435if(errno == ENOENT)2436/*2437 * file disappeared, which is what we2438 * wanted anyway2439 */2440continue;2441/* fall thru */2442}else if(S_ISDIR(st.st_mode)) {2443if(!remove_dir_recurse(path, flag, &kept_down))2444continue;/* happy */2445}else if(!only_empty &&2446(!unlink(path->buf) || errno == ENOENT)) {2447continue;/* happy, too */2448}24492450/* path too long, stat fails, or non-directory still exists */2451 ret = -1;2452break;2453}2454closedir(dir);24552456strbuf_setlen(path, original_len);2457if(!ret && !keep_toplevel && !kept_down)2458 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;2459else if(kept_up)2460/*2461 * report the uplevel that it is not an error that we2462 * did not rmdir() our directory.2463 */2464*kept_up = !ret;2465return ret;2466}24672468intremove_dir_recursively(struct strbuf *path,int flag)2469{2470returnremove_dir_recurse(path, flag, NULL);2471}24722473staticGIT_PATH_FUNC(git_path_info_exclude,"info/exclude")24742475voidsetup_standard_excludes(struct dir_struct *dir)2476{2477 dir->exclude_per_dir =".gitignore";24782479/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */2480if(!excludes_file)2481 excludes_file =xdg_config_home("ignore");2482if(excludes_file && !access_or_warn(excludes_file, R_OK,0))2483add_excludes_from_file_1(dir, excludes_file,2484 dir->untracked ? &dir->ss_excludes_file : NULL);24852486/* per repository user preference */2487if(startup_info->have_repository) {2488const char*path =git_path_info_exclude();2489if(!access_or_warn(path, R_OK,0))2490add_excludes_from_file_1(dir, path,2491 dir->untracked ? &dir->ss_info_exclude : NULL);2492}2493}24942495intremove_path(const char*name)2496{2497char*slash;24982499if(unlink(name) && !is_missing_file_error(errno))2500return-1;25012502 slash =strrchr(name,'/');2503if(slash) {2504char*dirs =xstrdup(name);2505 slash = dirs + (slash - name);2506do{2507*slash ='\0';2508}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));2509free(dirs);2510}2511return0;2512}25132514/*2515 * Frees memory within dir which was allocated for exclude lists and2516 * the exclude_stack. Does not free dir itself.2517 */2518voidclear_directory(struct dir_struct *dir)2519{2520int i, j;2521struct exclude_list_group *group;2522struct exclude_list *el;2523struct exclude_stack *stk;25242525for(i = EXC_CMDL; i <= EXC_FILE; i++) {2526 group = &dir->exclude_list_group[i];2527for(j =0; j < group->nr; j++) {2528 el = &group->el[j];2529if(i == EXC_DIRS)2530free((char*)el->src);2531clear_exclude_list(el);2532}2533free(group->el);2534}25352536 stk = dir->exclude_stack;2537while(stk) {2538struct exclude_stack *prev = stk->prev;2539free(stk);2540 stk = prev;2541}2542strbuf_release(&dir->basebuf);2543}25442545struct ondisk_untracked_cache {2546struct stat_data info_exclude_stat;2547struct stat_data excludes_file_stat;2548uint32_t dir_flags;2549unsigned char info_exclude_sha1[20];2550unsigned char excludes_file_sha1[20];2551char exclude_per_dir[FLEX_ARRAY];2552};25532554#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)2555#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)25562557struct write_data {2558int index;/* number of written untracked_cache_dir */2559struct ewah_bitmap *check_only;/* from untracked_cache_dir */2560struct ewah_bitmap *valid;/* from untracked_cache_dir */2561struct ewah_bitmap *sha1_valid;/* set if exclude_sha1 is not null */2562struct strbuf out;2563struct strbuf sb_stat;2564struct strbuf sb_sha1;2565};25662567static voidstat_data_to_disk(struct stat_data *to,const struct stat_data *from)2568{2569 to->sd_ctime.sec =htonl(from->sd_ctime.sec);2570 to->sd_ctime.nsec =htonl(from->sd_ctime.nsec);2571 to->sd_mtime.sec =htonl(from->sd_mtime.sec);2572 to->sd_mtime.nsec =htonl(from->sd_mtime.nsec);2573 to->sd_dev =htonl(from->sd_dev);2574 to->sd_ino =htonl(from->sd_ino);2575 to->sd_uid =htonl(from->sd_uid);2576 to->sd_gid =htonl(from->sd_gid);2577 to->sd_size =htonl(from->sd_size);2578}25792580static voidwrite_one_dir(struct untracked_cache_dir *untracked,2581struct write_data *wd)2582{2583struct stat_data stat_data;2584struct strbuf *out = &wd->out;2585unsigned char intbuf[16];2586unsigned int intlen, value;2587int i = wd->index++;25882589/*2590 * untracked_nr should be reset whenever valid is clear, but2591 * for safety..2592 */2593if(!untracked->valid) {2594 untracked->untracked_nr =0;2595 untracked->check_only =0;2596}25972598if(untracked->check_only)2599ewah_set(wd->check_only, i);2600if(untracked->valid) {2601ewah_set(wd->valid, i);2602stat_data_to_disk(&stat_data, &untracked->stat_data);2603strbuf_add(&wd->sb_stat, &stat_data,sizeof(stat_data));2604}2605if(!is_null_oid(&untracked->exclude_oid)) {2606ewah_set(wd->sha1_valid, i);2607strbuf_add(&wd->sb_sha1, untracked->exclude_oid.hash,2608 the_hash_algo->rawsz);2609}26102611 intlen =encode_varint(untracked->untracked_nr, intbuf);2612strbuf_add(out, intbuf, intlen);26132614/* skip non-recurse directories */2615for(i =0, value =0; i < untracked->dirs_nr; i++)2616if(untracked->dirs[i]->recurse)2617 value++;2618 intlen =encode_varint(value, intbuf);2619strbuf_add(out, intbuf, intlen);26202621strbuf_add(out, untracked->name,strlen(untracked->name) +1);26222623for(i =0; i < untracked->untracked_nr; i++)2624strbuf_add(out, untracked->untracked[i],2625strlen(untracked->untracked[i]) +1);26262627for(i =0; i < untracked->dirs_nr; i++)2628if(untracked->dirs[i]->recurse)2629write_one_dir(untracked->dirs[i], wd);2630}26312632voidwrite_untracked_extension(struct strbuf *out,struct untracked_cache *untracked)2633{2634struct ondisk_untracked_cache *ouc;2635struct write_data wd;2636unsigned char varbuf[16];2637int varint_len;2638size_t len =strlen(untracked->exclude_per_dir);26392640FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);2641stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2642stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2643hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);2644hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);2645 ouc->dir_flags =htonl(untracked->dir_flags);26462647 varint_len =encode_varint(untracked->ident.len, varbuf);2648strbuf_add(out, varbuf, varint_len);2649strbuf_addbuf(out, &untracked->ident);26502651strbuf_add(out, ouc,ouc_size(len));2652FREE_AND_NULL(ouc);26532654if(!untracked->root) {2655 varint_len =encode_varint(0, varbuf);2656strbuf_add(out, varbuf, varint_len);2657return;2658}26592660 wd.index =0;2661 wd.check_only =ewah_new();2662 wd.valid =ewah_new();2663 wd.sha1_valid =ewah_new();2664strbuf_init(&wd.out,1024);2665strbuf_init(&wd.sb_stat,1024);2666strbuf_init(&wd.sb_sha1,1024);2667write_one_dir(untracked->root, &wd);26682669 varint_len =encode_varint(wd.index, varbuf);2670strbuf_add(out, varbuf, varint_len);2671strbuf_addbuf(out, &wd.out);2672ewah_serialize_strbuf(wd.valid, out);2673ewah_serialize_strbuf(wd.check_only, out);2674ewah_serialize_strbuf(wd.sha1_valid, out);2675strbuf_addbuf(out, &wd.sb_stat);2676strbuf_addbuf(out, &wd.sb_sha1);2677strbuf_addch(out,'\0');/* safe guard for string lists */26782679ewah_free(wd.valid);2680ewah_free(wd.check_only);2681ewah_free(wd.sha1_valid);2682strbuf_release(&wd.out);2683strbuf_release(&wd.sb_stat);2684strbuf_release(&wd.sb_sha1);2685}26862687static voidfree_untracked(struct untracked_cache_dir *ucd)2688{2689int i;2690if(!ucd)2691return;2692for(i =0; i < ucd->dirs_nr; i++)2693free_untracked(ucd->dirs[i]);2694for(i =0; i < ucd->untracked_nr; i++)2695free(ucd->untracked[i]);2696free(ucd->untracked);2697free(ucd->dirs);2698free(ucd);2699}27002701voidfree_untracked_cache(struct untracked_cache *uc)2702{2703if(uc)2704free_untracked(uc->root);2705free(uc);2706}27072708struct read_data {2709int index;2710struct untracked_cache_dir **ucd;2711struct ewah_bitmap *check_only;2712struct ewah_bitmap *valid;2713struct ewah_bitmap *sha1_valid;2714const unsigned char*data;2715const unsigned char*end;2716};27172718static voidstat_data_from_disk(struct stat_data *to,const unsigned char*data)2719{2720memcpy(to, data,sizeof(*to));2721 to->sd_ctime.sec =ntohl(to->sd_ctime.sec);2722 to->sd_ctime.nsec =ntohl(to->sd_ctime.nsec);2723 to->sd_mtime.sec =ntohl(to->sd_mtime.sec);2724 to->sd_mtime.nsec =ntohl(to->sd_mtime.nsec);2725 to->sd_dev =ntohl(to->sd_dev);2726 to->sd_ino =ntohl(to->sd_ino);2727 to->sd_uid =ntohl(to->sd_uid);2728 to->sd_gid =ntohl(to->sd_gid);2729 to->sd_size =ntohl(to->sd_size);2730}27312732static intread_one_dir(struct untracked_cache_dir **untracked_,2733struct read_data *rd)2734{2735struct untracked_cache_dir ud, *untracked;2736const unsigned char*next, *data = rd->data, *end = rd->end;2737unsigned int value;2738int i, len;27392740memset(&ud,0,sizeof(ud));27412742 next = data;2743 value =decode_varint(&next);2744if(next > end)2745return-1;2746 ud.recurse =1;2747 ud.untracked_alloc = value;2748 ud.untracked_nr = value;2749if(ud.untracked_nr)2750ALLOC_ARRAY(ud.untracked, ud.untracked_nr);2751 data = next;27522753 next = data;2754 ud.dirs_alloc = ud.dirs_nr =decode_varint(&next);2755if(next > end)2756return-1;2757ALLOC_ARRAY(ud.dirs, ud.dirs_nr);2758 data = next;27592760 len =strlen((const char*)data);2761 next = data + len +1;2762if(next > rd->end)2763return-1;2764*untracked_ = untracked =xmalloc(st_add(sizeof(*untracked), len));2765memcpy(untracked, &ud,sizeof(ud));2766memcpy(untracked->name, data, len +1);2767 data = next;27682769for(i =0; i < untracked->untracked_nr; i++) {2770 len =strlen((const char*)data);2771 next = data + len +1;2772if(next > rd->end)2773return-1;2774 untracked->untracked[i] =xstrdup((const char*)data);2775 data = next;2776}27772778 rd->ucd[rd->index++] = untracked;2779 rd->data = data;27802781for(i =0; i < untracked->dirs_nr; i++) {2782 len =read_one_dir(untracked->dirs + i, rd);2783if(len <0)2784return-1;2785}2786return0;2787}27882789static voidset_check_only(size_t pos,void*cb)2790{2791struct read_data *rd = cb;2792struct untracked_cache_dir *ud = rd->ucd[pos];2793 ud->check_only =1;2794}27952796static voidread_stat(size_t pos,void*cb)2797{2798struct read_data *rd = cb;2799struct untracked_cache_dir *ud = rd->ucd[pos];2800if(rd->data +sizeof(struct stat_data) > rd->end) {2801 rd->data = rd->end +1;2802return;2803}2804stat_data_from_disk(&ud->stat_data, rd->data);2805 rd->data +=sizeof(struct stat_data);2806 ud->valid =1;2807}28082809static voidread_oid(size_t pos,void*cb)2810{2811struct read_data *rd = cb;2812struct untracked_cache_dir *ud = rd->ucd[pos];2813if(rd->data + the_hash_algo->rawsz > rd->end) {2814 rd->data = rd->end +1;2815return;2816}2817hashcpy(ud->exclude_oid.hash, rd->data);2818 rd->data += the_hash_algo->rawsz;2819}28202821static voidload_oid_stat(struct oid_stat *oid_stat,const unsigned char*data,2822const unsigned char*sha1)2823{2824stat_data_from_disk(&oid_stat->stat, data);2825hashcpy(oid_stat->oid.hash, sha1);2826 oid_stat->valid =1;2827}28282829struct untracked_cache *read_untracked_extension(const void*data,unsigned long sz)2830{2831struct untracked_cache *uc;2832struct read_data rd;2833const unsigned char*next = data, *end = (const unsigned char*)data + sz;2834const char*ident;2835int ident_len;2836 ssize_t len;2837const char*exclude_per_dir;28382839if(sz <=1|| end[-1] !='\0')2840return NULL;2841 end--;28422843 ident_len =decode_varint(&next);2844if(next + ident_len > end)2845return NULL;2846 ident = (const char*)next;2847 next += ident_len;28482849if(next +ouc_size(0) > end)2850return NULL;28512852 uc =xcalloc(1,sizeof(*uc));2853strbuf_init(&uc->ident, ident_len);2854strbuf_add(&uc->ident, ident, ident_len);2855load_oid_stat(&uc->ss_info_exclude,2856 next +ouc_offset(info_exclude_stat),2857 next +ouc_offset(info_exclude_sha1));2858load_oid_stat(&uc->ss_excludes_file,2859 next +ouc_offset(excludes_file_stat),2860 next +ouc_offset(excludes_file_sha1));2861 uc->dir_flags =get_be32(next +ouc_offset(dir_flags));2862 exclude_per_dir = (const char*)next +ouc_offset(exclude_per_dir);2863 uc->exclude_per_dir =xstrdup(exclude_per_dir);2864/* NUL after exclude_per_dir is covered by sizeof(*ouc) */2865 next +=ouc_size(strlen(exclude_per_dir));2866if(next >= end)2867goto done2;28682869 len =decode_varint(&next);2870if(next > end || len ==0)2871goto done2;28722873 rd.valid =ewah_new();2874 rd.check_only =ewah_new();2875 rd.sha1_valid =ewah_new();2876 rd.data = next;2877 rd.end = end;2878 rd.index =0;2879ALLOC_ARRAY(rd.ucd, len);28802881if(read_one_dir(&uc->root, &rd) || rd.index != len)2882goto done;28832884 next = rd.data;2885 len =ewah_read_mmap(rd.valid, next, end - next);2886if(len <0)2887goto done;28882889 next += len;2890 len =ewah_read_mmap(rd.check_only, next, end - next);2891if(len <0)2892goto done;28932894 next += len;2895 len =ewah_read_mmap(rd.sha1_valid, next, end - next);2896if(len <0)2897goto done;28982899ewah_each_bit(rd.check_only, set_check_only, &rd);2900 rd.data = next + len;2901ewah_each_bit(rd.valid, read_stat, &rd);2902ewah_each_bit(rd.sha1_valid, read_oid, &rd);2903 next = rd.data;29042905done:2906free(rd.ucd);2907ewah_free(rd.valid);2908ewah_free(rd.check_only);2909ewah_free(rd.sha1_valid);2910done2:2911if(next != end) {2912free_untracked_cache(uc);2913 uc = NULL;2914}2915return uc;2916}29172918static voidinvalidate_one_directory(struct untracked_cache *uc,2919struct untracked_cache_dir *ucd)2920{2921 uc->dir_invalidated++;2922 ucd->valid =0;2923 ucd->untracked_nr =0;2924}29252926/*2927 * Normally when an entry is added or removed from a directory,2928 * invalidating that directory is enough. No need to touch its2929 * ancestors. When a directory is shown as "foo/bar/" in git-status2930 * however, deleting or adding an entry may have cascading effect.2931 *2932 * Say the "foo/bar/file" has become untracked, we need to tell the2933 * untracked_cache_dir of "foo" that "bar/" is not an untracked2934 * directory any more (because "bar" is managed by foo as an untracked2935 * "file").2936 *2937 * Similarly, if "foo/bar/file" moves from untracked to tracked and it2938 * was the last untracked entry in the entire "foo", we should show2939 * "foo/" instead. Which means we have to invalidate past "bar" up to2940 * "foo".2941 *2942 * This function traverses all directories from root to leaf. If there2943 * is a chance of one of the above cases happening, we invalidate back2944 * to root. Otherwise we just invalidate the leaf. There may be a more2945 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to2946 * detect these cases and avoid unnecessary invalidation, for example,2947 * checking for the untracked entry named "bar/" in "foo", but for now2948 * stick to something safe and simple.2949 */2950static intinvalidate_one_component(struct untracked_cache *uc,2951struct untracked_cache_dir *dir,2952const char*path,int len)2953{2954const char*rest =strchr(path,'/');29552956if(rest) {2957int component_len = rest - path;2958struct untracked_cache_dir *d =2959lookup_untracked(uc, dir, path, component_len);2960int ret =2961invalidate_one_component(uc, d, rest +1,2962 len - (component_len +1));2963if(ret)2964invalidate_one_directory(uc, dir);2965return ret;2966}29672968invalidate_one_directory(uc, dir);2969return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;2970}29712972voiduntracked_cache_invalidate_path(struct index_state *istate,2973const char*path,int safe_path)2974{2975if(!istate->untracked || !istate->untracked->root)2976return;2977if(!safe_path && !verify_path(path,0))2978return;2979invalidate_one_component(istate->untracked, istate->untracked->root,2980 path,strlen(path));2981}29822983voiduntracked_cache_remove_from_index(struct index_state *istate,2984const char*path)2985{2986untracked_cache_invalidate_path(istate, path,1);2987}29882989voiduntracked_cache_add_to_index(struct index_state *istate,2990const char*path)2991{2992untracked_cache_invalidate_path(istate, path,1);2993}29942995static voidconnect_wt_gitdir_in_nested(const char*sub_worktree,2996const char*sub_gitdir)2997{2998int i;2999struct repository subrepo;3000struct strbuf sub_wt = STRBUF_INIT;3001struct strbuf sub_gd = STRBUF_INIT;30023003const struct submodule *sub;30043005/* If the submodule has no working tree, we can ignore it. */3006if(repo_init(&subrepo, sub_gitdir, sub_worktree))3007return;30083009if(repo_read_index(&subrepo) <0)3010die(_("index file corrupt in repo%s"), subrepo.gitdir);30113012for(i =0; i < subrepo.index->cache_nr; i++) {3013const struct cache_entry *ce = subrepo.index->cache[i];30143015if(!S_ISGITLINK(ce->ce_mode))3016continue;30173018while(i +1< subrepo.index->cache_nr &&3019!strcmp(ce->name, subrepo.index->cache[i +1]->name))3020/*3021 * Skip entries with the same name in different stages3022 * to make sure an entry is returned only once.3023 */3024 i++;30253026 sub =submodule_from_path(&subrepo, &null_oid, ce->name);3027if(!sub || !is_submodule_active(&subrepo, ce->name))3028/* .gitmodules broken or inactive sub */3029continue;30303031strbuf_reset(&sub_wt);3032strbuf_reset(&sub_gd);3033strbuf_addf(&sub_wt,"%s/%s", sub_worktree, sub->path);3034strbuf_addf(&sub_gd,"%s/modules/%s", sub_gitdir, sub->name);30353036connect_work_tree_and_git_dir(sub_wt.buf, sub_gd.buf,1);3037}3038strbuf_release(&sub_wt);3039strbuf_release(&sub_gd);3040repo_clear(&subrepo);3041}30423043voidconnect_work_tree_and_git_dir(const char*work_tree_,3044const char*git_dir_,3045int recurse_into_nested)3046{3047struct strbuf gitfile_sb = STRBUF_INIT;3048struct strbuf cfg_sb = STRBUF_INIT;3049struct strbuf rel_path = STRBUF_INIT;3050char*git_dir, *work_tree;30513052/* Prepare .git file */3053strbuf_addf(&gitfile_sb,"%s/.git", work_tree_);3054if(safe_create_leading_directories_const(gitfile_sb.buf))3055die(_("could not create directories for%s"), gitfile_sb.buf);30563057/* Prepare config file */3058strbuf_addf(&cfg_sb,"%s/config", git_dir_);3059if(safe_create_leading_directories_const(cfg_sb.buf))3060die(_("could not create directories for%s"), cfg_sb.buf);30613062 git_dir =real_pathdup(git_dir_,1);3063 work_tree =real_pathdup(work_tree_,1);30643065/* Write .git file */3066write_file(gitfile_sb.buf,"gitdir:%s",3067relative_path(git_dir, work_tree, &rel_path));3068/* Update core.worktree setting */3069git_config_set_in_file(cfg_sb.buf,"core.worktree",3070relative_path(work_tree, git_dir, &rel_path));30713072strbuf_release(&gitfile_sb);3073strbuf_release(&cfg_sb);3074strbuf_release(&rel_path);30753076if(recurse_into_nested)3077connect_wt_gitdir_in_nested(work_tree, git_dir);30783079free(work_tree);3080free(git_dir);3081}30823083/*3084 * Migrate the git directory of the given path from old_git_dir to new_git_dir.3085 */3086voidrelocate_gitdir(const char*path,const char*old_git_dir,const char*new_git_dir)3087{3088if(rename(old_git_dir, new_git_dir) <0)3089die_errno(_("could not migrate git directory from '%s' to '%s'"),3090 old_git_dir, new_git_dir);30913092connect_work_tree_and_git_dir(path, new_git_dir,0);3093}