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 24/* 25 * Tells read_directory_recursive how a file or directory should be treated. 26 * Values are ordered by significance, e.g. if a directory contains both 27 * excluded and untracked files, it is listed as untracked because 28 * path_untracked > path_excluded. 29 */ 30enum path_treatment { 31 path_none =0, 32 path_recurse, 33 path_excluded, 34 path_untracked 35}; 36 37/* 38 * Support data structure for our opendir/readdir/closedir wrappers 39 */ 40struct cached_dir { 41DIR*fdir; 42struct untracked_cache_dir *untracked; 43int nr_files; 44int nr_dirs; 45 46struct dirent *de; 47const char*file; 48struct untracked_cache_dir *ucd; 49}; 50 51static enum path_treatment read_directory_recursive(struct dir_struct *dir, 52struct index_state *istate,const char*path,int len, 53struct untracked_cache_dir *untracked, 54int check_only,int stop_at_first_file,const struct pathspec *pathspec); 55static intget_dtype(struct dirent *de,struct index_state *istate, 56const char*path,int len); 57 58intcount_slashes(const char*s) 59{ 60int cnt =0; 61while(*s) 62if(*s++ =='/') 63 cnt++; 64return cnt; 65} 66 67intfspathcmp(const char*a,const char*b) 68{ 69return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 70} 71 72intfspathncmp(const char*a,const char*b,size_t count) 73{ 74return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 75} 76 77intgit_fnmatch(const struct pathspec_item *item, 78const char*pattern,const char*string, 79int prefix) 80{ 81if(prefix >0) { 82if(ps_strncmp(item, pattern, string, prefix)) 83return WM_NOMATCH; 84 pattern += prefix; 85 string += prefix; 86} 87if(item->flags & PATHSPEC_ONESTAR) { 88int pattern_len =strlen(++pattern); 89int string_len =strlen(string); 90return string_len < pattern_len || 91ps_strcmp(item, pattern, 92 string + string_len - pattern_len); 93} 94if(item->magic & PATHSPEC_GLOB) 95returnwildmatch(pattern, string, 96 WM_PATHNAME | 97(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0)); 98else 99/* wildmatch has not learned no FNM_PATHNAME mode yet */ 100returnwildmatch(pattern, string, 101 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0); 102} 103 104static intfnmatch_icase_mem(const char*pattern,int patternlen, 105const char*string,int stringlen, 106int flags) 107{ 108int match_status; 109struct strbuf pat_buf = STRBUF_INIT; 110struct strbuf str_buf = STRBUF_INIT; 111const char*use_pat = pattern; 112const char*use_str = string; 113 114if(pattern[patternlen]) { 115strbuf_add(&pat_buf, pattern, patternlen); 116 use_pat = pat_buf.buf; 117} 118if(string[stringlen]) { 119strbuf_add(&str_buf, string, stringlen); 120 use_str = str_buf.buf; 121} 122 123if(ignore_case) 124 flags |= WM_CASEFOLD; 125 match_status =wildmatch(use_pat, use_str, flags); 126 127strbuf_release(&pat_buf); 128strbuf_release(&str_buf); 129 130return match_status; 131} 132 133static size_tcommon_prefix_len(const struct pathspec *pathspec) 134{ 135int n; 136size_t max =0; 137 138/* 139 * ":(icase)path" is treated as a pathspec full of 140 * wildcard. In other words, only prefix is considered common 141 * prefix. If the pathspec is abc/foo abc/bar, running in 142 * subdir xyz, the common prefix is still xyz, not xuz/abc as 143 * in non-:(icase). 144 */ 145GUARD_PATHSPEC(pathspec, 146 PATHSPEC_FROMTOP | 147 PATHSPEC_MAXDEPTH | 148 PATHSPEC_LITERAL | 149 PATHSPEC_GLOB | 150 PATHSPEC_ICASE | 151 PATHSPEC_EXCLUDE | 152 PATHSPEC_ATTR); 153 154for(n =0; n < pathspec->nr; n++) { 155size_t i =0, len =0, item_len; 156if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 157continue; 158if(pathspec->items[n].magic & PATHSPEC_ICASE) 159 item_len = pathspec->items[n].prefix; 160else 161 item_len = pathspec->items[n].nowildcard_len; 162while(i < item_len && (n ==0|| i < max)) { 163char c = pathspec->items[n].match[i]; 164if(c != pathspec->items[0].match[i]) 165break; 166if(c =='/') 167 len = i +1; 168 i++; 169} 170if(n ==0|| len < max) { 171 max = len; 172if(!max) 173break; 174} 175} 176return max; 177} 178 179/* 180 * Returns a copy of the longest leading path common among all 181 * pathspecs. 182 */ 183char*common_prefix(const struct pathspec *pathspec) 184{ 185unsigned long len =common_prefix_len(pathspec); 186 187return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 188} 189 190intfill_directory(struct dir_struct *dir, 191struct index_state *istate, 192const struct pathspec *pathspec) 193{ 194const char*prefix; 195size_t prefix_len; 196 197/* 198 * Calculate common prefix for the pathspec, and 199 * use that to optimize the directory walk 200 */ 201 prefix_len =common_prefix_len(pathspec); 202 prefix = prefix_len ? pathspec->items[0].match :""; 203 204/* Read the directory and prune it */ 205read_directory(dir, istate, prefix, prefix_len, pathspec); 206 207return prefix_len; 208} 209 210intwithin_depth(const char*name,int namelen, 211int depth,int max_depth) 212{ 213const char*cp = name, *cpe = name + namelen; 214 215while(cp < cpe) { 216if(*cp++ !='/') 217continue; 218 depth++; 219if(depth > max_depth) 220return0; 221} 222return1; 223} 224 225/* 226 * Read the contents of the blob with the given OID into a buffer. 227 * Append a trailing LF to the end if the last line doesn't have one. 228 * 229 * Returns: 230 * -1 when the OID is invalid or unknown or does not refer to a blob. 231 * 0 when the blob is empty. 232 * 1 along with { data, size } of the (possibly augmented) buffer 233 * when successful. 234 * 235 * Optionally updates the given oid_stat with the given OID (when valid). 236 */ 237static intdo_read_blob(const struct object_id *oid,struct oid_stat *oid_stat, 238size_t*size_out,char**data_out) 239{ 240enum object_type type; 241unsigned long sz; 242char*data; 243 244*size_out =0; 245*data_out = NULL; 246 247 data =read_object_file(oid, &type, &sz); 248if(!data || type != OBJ_BLOB) { 249free(data); 250return-1; 251} 252 253if(oid_stat) { 254memset(&oid_stat->stat,0,sizeof(oid_stat->stat)); 255oidcpy(&oid_stat->oid, oid); 256} 257 258if(sz ==0) { 259free(data); 260return0; 261} 262 263if(data[sz -1] !='\n') { 264 data =xrealloc(data,st_add(sz,1)); 265 data[sz++] ='\n'; 266} 267 268*size_out =xsize_t(sz); 269*data_out = data; 270 271return1; 272} 273 274#define DO_MATCH_EXCLUDE (1<<0) 275#define DO_MATCH_DIRECTORY (1<<1) 276#define DO_MATCH_SUBMODULE (1<<2) 277 278static intmatch_attrs(const char*name,int namelen, 279const struct pathspec_item *item) 280{ 281int i; 282 283git_check_attr(name, item->attr_check); 284for(i =0; i < item->attr_match_nr; i++) { 285const char*value; 286int matched; 287enum attr_match_mode match_mode; 288 289 value = item->attr_check->items[i].value; 290 match_mode = item->attr_match[i].match_mode; 291 292if(ATTR_TRUE(value)) 293 matched = (match_mode == MATCH_SET); 294else if(ATTR_FALSE(value)) 295 matched = (match_mode == MATCH_UNSET); 296else if(ATTR_UNSET(value)) 297 matched = (match_mode == MATCH_UNSPECIFIED); 298else 299 matched = (match_mode == MATCH_VALUE && 300!strcmp(item->attr_match[i].value, value)); 301if(!matched) 302return0; 303} 304 305return1; 306} 307 308/* 309 * Does 'match' match the given name? 310 * A match is found if 311 * 312 * (1) the 'match' string is leading directory of 'name', or 313 * (2) the 'match' string is a wildcard and matches 'name', or 314 * (3) the 'match' string is exactly the same as 'name'. 315 * 316 * and the return value tells which case it was. 317 * 318 * It returns 0 when there is no match. 319 */ 320static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 321const char*name,int namelen,unsigned flags) 322{ 323/* name/namelen has prefix cut off by caller */ 324const char*match = item->match + prefix; 325int matchlen = item->len - prefix; 326 327/* 328 * The normal call pattern is: 329 * 1. prefix = common_prefix_len(ps); 330 * 2. prune something, or fill_directory 331 * 3. match_pathspec() 332 * 333 * 'prefix' at #1 may be shorter than the command's prefix and 334 * it's ok for #2 to match extra files. Those extras will be 335 * trimmed at #3. 336 * 337 * Suppose the pathspec is 'foo' and '../bar' running from 338 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 339 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 340 * user does not want XYZ/foo, only the "foo" part should be 341 * case-insensitive. We need to filter out XYZ/foo here. In 342 * other words, we do not trust the caller on comparing the 343 * prefix part when :(icase) is involved. We do exact 344 * comparison ourselves. 345 * 346 * Normally the caller (common_prefix_len() in fact) does 347 * _exact_ matching on name[-prefix+1..-1] and we do not need 348 * to check that part. Be defensive and check it anyway, in 349 * case common_prefix_len is changed, or a new caller is 350 * introduced that does not use common_prefix_len. 351 * 352 * If the penalty turns out too high when prefix is really 353 * long, maybe change it to 354 * strncmp(match, name, item->prefix - prefix) 355 */ 356if(item->prefix && (item->magic & PATHSPEC_ICASE) && 357strncmp(item->match, name - prefix, item->prefix)) 358return0; 359 360if(item->attr_match_nr && !match_attrs(name, namelen, item)) 361return0; 362 363/* If the match was just the prefix, we matched */ 364if(!*match) 365return MATCHED_RECURSIVELY; 366 367if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 368if(matchlen == namelen) 369return MATCHED_EXACTLY; 370 371if(match[matchlen-1] =='/'|| name[matchlen] =='/') 372return MATCHED_RECURSIVELY; 373}else if((flags & DO_MATCH_DIRECTORY) && 374 match[matchlen -1] =='/'&& 375 namelen == matchlen -1&& 376!ps_strncmp(item, match, name, namelen)) 377return MATCHED_EXACTLY; 378 379if(item->nowildcard_len < item->len && 380!git_fnmatch(item, match, name, 381 item->nowildcard_len - prefix)) 382return MATCHED_FNMATCH; 383 384/* Perform checks to see if "name" is a super set of the pathspec */ 385if(flags & DO_MATCH_SUBMODULE) { 386/* name is a literal prefix of the pathspec */ 387if((namelen < matchlen) && 388(match[namelen] =='/') && 389!ps_strncmp(item, match, name, namelen)) 390return MATCHED_RECURSIVELY; 391 392/* name" doesn't match up to the first wild character */ 393if(item->nowildcard_len < item->len && 394ps_strncmp(item, match, name, 395 item->nowildcard_len - prefix)) 396return0; 397 398/* 399 * Here is where we would perform a wildmatch to check if 400 * "name" can be matched as a directory (or a prefix) against 401 * the pathspec. Since wildmatch doesn't have this capability 402 * at the present we have to punt and say that it is a match, 403 * potentially returning a false positive 404 * The submodules themselves will be able to perform more 405 * accurate matching to determine if the pathspec matches. 406 */ 407return MATCHED_RECURSIVELY; 408} 409 410return0; 411} 412 413/* 414 * Given a name and a list of pathspecs, returns the nature of the 415 * closest (i.e. most specific) match of the name to any of the 416 * pathspecs. 417 * 418 * The caller typically calls this multiple times with the same 419 * pathspec and seen[] array but with different name/namelen 420 * (e.g. entries from the index) and is interested in seeing if and 421 * how each pathspec matches all the names it calls this function 422 * with. A mark is left in the seen[] array for each pathspec element 423 * indicating the closest type of match that element achieved, so if 424 * seen[n] remains zero after multiple invocations, that means the nth 425 * pathspec did not match any names, which could indicate that the 426 * user mistyped the nth pathspec. 427 */ 428static intdo_match_pathspec(const struct pathspec *ps, 429const char*name,int namelen, 430int prefix,char*seen, 431unsigned flags) 432{ 433int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 434 435GUARD_PATHSPEC(ps, 436 PATHSPEC_FROMTOP | 437 PATHSPEC_MAXDEPTH | 438 PATHSPEC_LITERAL | 439 PATHSPEC_GLOB | 440 PATHSPEC_ICASE | 441 PATHSPEC_EXCLUDE | 442 PATHSPEC_ATTR); 443 444if(!ps->nr) { 445if(!ps->recursive || 446!(ps->magic & PATHSPEC_MAXDEPTH) || 447 ps->max_depth == -1) 448return MATCHED_RECURSIVELY; 449 450if(within_depth(name, namelen,0, ps->max_depth)) 451return MATCHED_EXACTLY; 452else 453return0; 454} 455 456 name += prefix; 457 namelen -= prefix; 458 459for(i = ps->nr -1; i >=0; i--) { 460int how; 461 462if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 463( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 464continue; 465 466if(seen && seen[i] == MATCHED_EXACTLY) 467continue; 468/* 469 * Make exclude patterns optional and never report 470 * "pathspec ':(exclude)foo' matches no files" 471 */ 472if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 473 seen[i] = MATCHED_FNMATCH; 474 how =match_pathspec_item(ps->items+i, prefix, name, 475 namelen, flags); 476if(ps->recursive && 477(ps->magic & PATHSPEC_MAXDEPTH) && 478 ps->max_depth != -1&& 479 how && how != MATCHED_FNMATCH) { 480int len = ps->items[i].len; 481if(name[len] =='/') 482 len++; 483if(within_depth(name+len, namelen-len,0, ps->max_depth)) 484 how = MATCHED_EXACTLY; 485else 486 how =0; 487} 488if(how) { 489if(retval < how) 490 retval = how; 491if(seen && seen[i] < how) 492 seen[i] = how; 493} 494} 495return retval; 496} 497 498intmatch_pathspec(const struct pathspec *ps, 499const char*name,int namelen, 500int prefix,char*seen,int is_dir) 501{ 502int positive, negative; 503unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 504 positive =do_match_pathspec(ps, name, namelen, 505 prefix, seen, flags); 506if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 507return positive; 508 negative =do_match_pathspec(ps, name, namelen, 509 prefix, seen, 510 flags | DO_MATCH_EXCLUDE); 511return negative ?0: positive; 512} 513 514/** 515 * Check if a submodule is a superset of the pathspec 516 */ 517intsubmodule_path_match(const struct pathspec *ps, 518const char*submodule_name, 519char*seen) 520{ 521int matched =do_match_pathspec(ps, submodule_name, 522strlen(submodule_name), 5230, seen, 524 DO_MATCH_DIRECTORY | 525 DO_MATCH_SUBMODULE); 526return matched; 527} 528 529intreport_path_error(const char*ps_matched, 530const struct pathspec *pathspec, 531const char*prefix) 532{ 533/* 534 * Make sure all pathspec matched; otherwise it is an error. 535 */ 536int num, errors =0; 537for(num =0; num < pathspec->nr; num++) { 538int other, found_dup; 539 540if(ps_matched[num]) 541continue; 542/* 543 * The caller might have fed identical pathspec 544 * twice. Do not barf on such a mistake. 545 * FIXME: parse_pathspec should have eliminated 546 * duplicate pathspec. 547 */ 548for(found_dup = other =0; 549!found_dup && other < pathspec->nr; 550 other++) { 551if(other == num || !ps_matched[other]) 552continue; 553if(!strcmp(pathspec->items[other].original, 554 pathspec->items[num].original)) 555/* 556 * Ok, we have a match already. 557 */ 558 found_dup =1; 559} 560if(found_dup) 561continue; 562 563error("pathspec '%s' did not match any file(s) known to git.", 564 pathspec->items[num].original); 565 errors++; 566} 567return errors; 568} 569 570/* 571 * Return the length of the "simple" part of a path match limiter. 572 */ 573intsimple_length(const char*match) 574{ 575int len = -1; 576 577for(;;) { 578unsigned char c = *match++; 579 len++; 580if(c =='\0'||is_glob_special(c)) 581return len; 582} 583} 584 585intno_wildcard(const char*string) 586{ 587return string[simple_length(string)] =='\0'; 588} 589 590voidparse_exclude_pattern(const char**pattern, 591int*patternlen, 592unsigned*flags, 593int*nowildcardlen) 594{ 595const char*p = *pattern; 596size_t i, len; 597 598*flags =0; 599if(*p =='!') { 600*flags |= EXC_FLAG_NEGATIVE; 601 p++; 602} 603 len =strlen(p); 604if(len && p[len -1] =='/') { 605 len--; 606*flags |= EXC_FLAG_MUSTBEDIR; 607} 608for(i =0; i < len; i++) { 609if(p[i] =='/') 610break; 611} 612if(i == len) 613*flags |= EXC_FLAG_NODIR; 614*nowildcardlen =simple_length(p); 615/* 616 * we should have excluded the trailing slash from 'p' too, 617 * but that's one more allocation. Instead just make sure 618 * nowildcardlen does not exceed real patternlen 619 */ 620if(*nowildcardlen > len) 621*nowildcardlen = len; 622if(*p =='*'&&no_wildcard(p +1)) 623*flags |= EXC_FLAG_ENDSWITH; 624*pattern = p; 625*patternlen = len; 626} 627 628voidadd_exclude(const char*string,const char*base, 629int baselen,struct exclude_list *el,int srcpos) 630{ 631struct exclude *x; 632int patternlen; 633unsigned flags; 634int nowildcardlen; 635 636parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 637if(flags & EXC_FLAG_MUSTBEDIR) { 638FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen); 639}else{ 640 x =xmalloc(sizeof(*x)); 641 x->pattern = string; 642} 643 x->patternlen = patternlen; 644 x->nowildcardlen = nowildcardlen; 645 x->base = base; 646 x->baselen = baselen; 647 x->flags = flags; 648 x->srcpos = srcpos; 649ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 650 el->excludes[el->nr++] = x; 651 x->el = el; 652} 653 654static intread_skip_worktree_file_from_index(const struct index_state *istate, 655const char*path, 656size_t*size_out,char**data_out, 657struct oid_stat *oid_stat) 658{ 659int pos, len; 660 661 len =strlen(path); 662 pos =index_name_pos(istate, path, len); 663if(pos <0) 664return-1; 665if(!ce_skip_worktree(istate->cache[pos])) 666return-1; 667 668returndo_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); 669} 670 671/* 672 * Frees memory within el which was allocated for exclude patterns and 673 * the file buffer. Does not free el itself. 674 */ 675voidclear_exclude_list(struct exclude_list *el) 676{ 677int i; 678 679for(i =0; i < el->nr; i++) 680free(el->excludes[i]); 681free(el->excludes); 682free(el->filebuf); 683 684memset(el,0,sizeof(*el)); 685} 686 687static voidtrim_trailing_spaces(char*buf) 688{ 689char*p, *last_space = NULL; 690 691for(p = buf; *p; p++) 692switch(*p) { 693case' ': 694if(!last_space) 695 last_space = p; 696break; 697case'\\': 698 p++; 699if(!*p) 700return; 701/* fallthrough */ 702default: 703 last_space = NULL; 704} 705 706if(last_space) 707*last_space ='\0'; 708} 709 710/* 711 * Given a subdirectory name and "dir" of the current directory, 712 * search the subdir in "dir" and return it, or create a new one if it 713 * does not exist in "dir". 714 * 715 * If "name" has the trailing slash, it'll be excluded in the search. 716 */ 717static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 718struct untracked_cache_dir *dir, 719const char*name,int len) 720{ 721int first, last; 722struct untracked_cache_dir *d; 723if(!dir) 724return NULL; 725if(len && name[len -1] =='/') 726 len--; 727 first =0; 728 last = dir->dirs_nr; 729while(last > first) { 730int cmp, next = (last + first) >>1; 731 d = dir->dirs[next]; 732 cmp =strncmp(name, d->name, len); 733if(!cmp &&strlen(d->name) > len) 734 cmp = -1; 735if(!cmp) 736return d; 737if(cmp <0) { 738 last = next; 739continue; 740} 741 first = next+1; 742} 743 744 uc->dir_created++; 745FLEX_ALLOC_MEM(d, name, name, len); 746 747ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 748MOVE_ARRAY(dir->dirs + first +1, dir->dirs + first, 749 dir->dirs_nr - first); 750 dir->dirs_nr++; 751 dir->dirs[first] = d; 752return d; 753} 754 755static voiddo_invalidate_gitignore(struct untracked_cache_dir *dir) 756{ 757int i; 758 dir->valid =0; 759 dir->untracked_nr =0; 760for(i =0; i < dir->dirs_nr; i++) 761do_invalidate_gitignore(dir->dirs[i]); 762} 763 764static voidinvalidate_gitignore(struct untracked_cache *uc, 765struct untracked_cache_dir *dir) 766{ 767 uc->gitignore_invalidated++; 768do_invalidate_gitignore(dir); 769} 770 771static voidinvalidate_directory(struct untracked_cache *uc, 772struct untracked_cache_dir *dir) 773{ 774int i; 775 776/* 777 * Invalidation increment here is just roughly correct. If 778 * untracked_nr or any of dirs[].recurse is non-zero, we 779 * should increment dir_invalidated too. But that's more 780 * expensive to do. 781 */ 782if(dir->valid) 783 uc->dir_invalidated++; 784 785 dir->valid =0; 786 dir->untracked_nr =0; 787for(i =0; i < dir->dirs_nr; i++) 788 dir->dirs[i]->recurse =0; 789} 790 791static intadd_excludes_from_buffer(char*buf,size_t size, 792const char*base,int baselen, 793struct exclude_list *el); 794 795/* 796 * Given a file with name "fname", read it (either from disk, or from 797 * an index if 'istate' is non-null), parse it and store the 798 * exclude rules in "el". 799 * 800 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 801 * stat data from disk (only valid if add_excludes returns zero). If 802 * ss_valid is non-zero, "ss" must contain good value as input. 803 */ 804static intadd_excludes(const char*fname,const char*base,int baselen, 805struct exclude_list *el,struct index_state *istate, 806struct oid_stat *oid_stat) 807{ 808struct stat st; 809int r; 810int fd; 811size_t size =0; 812char*buf; 813 814 fd =open(fname, O_RDONLY); 815if(fd <0||fstat(fd, &st) <0) { 816if(fd <0) 817warn_on_fopen_errors(fname); 818else 819close(fd); 820if(!istate) 821return-1; 822 r =read_skip_worktree_file_from_index(istate, fname, 823&size, &buf, 824 oid_stat); 825if(r !=1) 826return r; 827}else{ 828 size =xsize_t(st.st_size); 829if(size ==0) { 830if(oid_stat) { 831fill_stat_data(&oid_stat->stat, &st); 832oidcpy(&oid_stat->oid, &empty_blob_oid); 833 oid_stat->valid =1; 834} 835close(fd); 836return0; 837} 838 buf =xmallocz(size); 839if(read_in_full(fd, buf, size) != size) { 840free(buf); 841close(fd); 842return-1; 843} 844 buf[size++] ='\n'; 845close(fd); 846if(oid_stat) { 847int pos; 848if(oid_stat->valid && 849!match_stat_data_racy(istate, &oid_stat->stat, &st)) 850;/* no content change, ss->sha1 still good */ 851else if(istate && 852(pos =index_name_pos(istate, fname,strlen(fname))) >=0&& 853!ce_stage(istate->cache[pos]) && 854ce_uptodate(istate->cache[pos]) && 855!would_convert_to_git(istate, fname)) 856oidcpy(&oid_stat->oid, 857&istate->cache[pos]->oid); 858else 859hash_object_file(buf, size,"blob", 860&oid_stat->oid); 861fill_stat_data(&oid_stat->stat, &st); 862 oid_stat->valid =1; 863} 864} 865 866add_excludes_from_buffer(buf, size, base, baselen, el); 867return0; 868} 869 870static intadd_excludes_from_buffer(char*buf,size_t size, 871const char*base,int baselen, 872struct exclude_list *el) 873{ 874int i, lineno =1; 875char*entry; 876 877 el->filebuf = buf; 878 879if(skip_utf8_bom(&buf, size)) 880 size -= buf - el->filebuf; 881 882 entry = buf; 883 884for(i =0; i < size; i++) { 885if(buf[i] =='\n') { 886if(entry != buf + i && entry[0] !='#') { 887 buf[i - (i && buf[i-1] =='\r')] =0; 888trim_trailing_spaces(entry); 889add_exclude(entry, base, baselen, el, lineno); 890} 891 lineno++; 892 entry = buf + i +1; 893} 894} 895return0; 896} 897 898intadd_excludes_from_file_to_list(const char*fname,const char*base, 899int baselen,struct exclude_list *el, 900struct index_state *istate) 901{ 902returnadd_excludes(fname, base, baselen, el, istate, NULL); 903} 904 905intadd_excludes_from_blob_to_list( 906struct object_id *oid, 907const char*base,int baselen, 908struct exclude_list *el) 909{ 910char*buf; 911size_t size; 912int r; 913 914 r =do_read_blob(oid, NULL, &size, &buf); 915if(r !=1) 916return r; 917 918add_excludes_from_buffer(buf, size, base, baselen, el); 919return0; 920} 921 922struct exclude_list *add_exclude_list(struct dir_struct *dir, 923int group_type,const char*src) 924{ 925struct exclude_list *el; 926struct exclude_list_group *group; 927 928 group = &dir->exclude_list_group[group_type]; 929ALLOC_GROW(group->el, group->nr +1, group->alloc); 930 el = &group->el[group->nr++]; 931memset(el,0,sizeof(*el)); 932 el->src = src; 933return el; 934} 935 936/* 937 * Used to set up core.excludesfile and .git/info/exclude lists. 938 */ 939static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 940struct oid_stat *oid_stat) 941{ 942struct exclude_list *el; 943/* 944 * catch setup_standard_excludes() that's called before 945 * dir->untracked is assigned. That function behaves 946 * differently when dir->untracked is non-NULL. 947 */ 948if(!dir->untracked) 949 dir->unmanaged_exclude_files++; 950 el =add_exclude_list(dir, EXC_FILE, fname); 951if(add_excludes(fname,"",0, el, NULL, oid_stat) <0) 952die("cannot use%sas an exclude file", fname); 953} 954 955voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 956{ 957 dir->unmanaged_exclude_files++;/* see validate_untracked_cache() */ 958add_excludes_from_file_1(dir, fname, NULL); 959} 960 961intmatch_basename(const char*basename,int basenamelen, 962const char*pattern,int prefix,int patternlen, 963unsigned flags) 964{ 965if(prefix == patternlen) { 966if(patternlen == basenamelen && 967!fspathncmp(pattern, basename, basenamelen)) 968return1; 969}else if(flags & EXC_FLAG_ENDSWITH) { 970/* "*literal" matching against "fooliteral" */ 971if(patternlen -1<= basenamelen && 972!fspathncmp(pattern +1, 973 basename + basenamelen - (patternlen -1), 974 patternlen -1)) 975return1; 976}else{ 977if(fnmatch_icase_mem(pattern, patternlen, 978 basename, basenamelen, 9790) ==0) 980return1; 981} 982return0; 983} 984 985intmatch_pathname(const char*pathname,int pathlen, 986const char*base,int baselen, 987const char*pattern,int prefix,int patternlen, 988unsigned flags) 989{ 990const char*name; 991int namelen; 992 993/* 994 * match with FNM_PATHNAME; the pattern has base implicitly 995 * in front of it. 996 */ 997if(*pattern =='/') { 998 pattern++; 999 patternlen--;1000 prefix--;1001}10021003/*1004 * baselen does not count the trailing slash. base[] may or1005 * may not end with a trailing slash though.1006 */1007if(pathlen < baselen +1||1008(baselen && pathname[baselen] !='/') ||1009fspathncmp(pathname, base, baselen))1010return0;10111012 namelen = baselen ? pathlen - baselen -1: pathlen;1013 name = pathname + pathlen - namelen;10141015if(prefix) {1016/*1017 * if the non-wildcard part is longer than the1018 * remaining pathname, surely it cannot match.1019 */1020if(prefix > namelen)1021return0;10221023if(fspathncmp(pattern, name, prefix))1024return0;1025 pattern += prefix;1026 patternlen -= prefix;1027 name += prefix;1028 namelen -= prefix;10291030/*1031 * If the whole pattern did not have a wildcard,1032 * then our prefix match is all we need; we1033 * do not need to call fnmatch at all.1034 */1035if(!patternlen && !namelen)1036return1;1037}10381039returnfnmatch_icase_mem(pattern, patternlen,1040 name, namelen,1041 WM_PATHNAME) ==0;1042}10431044/*1045 * Scan the given exclude list in reverse to see whether pathname1046 * should be ignored. The first match (i.e. the last on the list), if1047 * any, determines the fate. Returns the exclude_list element which1048 * matched, or NULL for undecided.1049 */1050static struct exclude *last_exclude_matching_from_list(const char*pathname,1051int pathlen,1052const char*basename,1053int*dtype,1054struct exclude_list *el,1055struct index_state *istate)1056{1057struct exclude *exc = NULL;/* undecided */1058int i;10591060if(!el->nr)1061return NULL;/* undefined */10621063for(i = el->nr -1;0<= i; i--) {1064struct exclude *x = el->excludes[i];1065const char*exclude = x->pattern;1066int prefix = x->nowildcardlen;10671068if(x->flags & EXC_FLAG_MUSTBEDIR) {1069if(*dtype == DT_UNKNOWN)1070*dtype =get_dtype(NULL, istate, pathname, pathlen);1071if(*dtype != DT_DIR)1072continue;1073}10741075if(x->flags & EXC_FLAG_NODIR) {1076if(match_basename(basename,1077 pathlen - (basename - pathname),1078 exclude, prefix, x->patternlen,1079 x->flags)) {1080 exc = x;1081break;1082}1083continue;1084}10851086assert(x->baselen ==0|| x->base[x->baselen -1] =='/');1087if(match_pathname(pathname, pathlen,1088 x->base, x->baselen ? x->baselen -1:0,1089 exclude, prefix, x->patternlen, x->flags)) {1090 exc = x;1091break;1092}1093}1094return exc;1095}10961097/*1098 * Scan the list and let the last match determine the fate.1099 * Return 1 for exclude, 0 for include and -1 for undecided.1100 */1101intis_excluded_from_list(const char*pathname,1102int pathlen,const char*basename,int*dtype,1103struct exclude_list *el,struct index_state *istate)1104{1105struct exclude *exclude;1106 exclude =last_exclude_matching_from_list(pathname, pathlen, basename,1107 dtype, el, istate);1108if(exclude)1109return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1110return-1;/* undecided */1111}11121113static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,1114struct index_state *istate,1115const char*pathname,int pathlen,const char*basename,1116int*dtype_p)1117{1118int i, j;1119struct exclude_list_group *group;1120struct exclude *exclude;1121for(i = EXC_CMDL; i <= EXC_FILE; i++) {1122 group = &dir->exclude_list_group[i];1123for(j = group->nr -1; j >=0; j--) {1124 exclude =last_exclude_matching_from_list(1125 pathname, pathlen, basename, dtype_p,1126&group->el[j], istate);1127if(exclude)1128return exclude;1129}1130}1131return NULL;1132}11331134/*1135 * Loads the per-directory exclude list for the substring of base1136 * which has a char length of baselen.1137 */1138static voidprep_exclude(struct dir_struct *dir,1139struct index_state *istate,1140const char*base,int baselen)1141{1142struct exclude_list_group *group;1143struct exclude_list *el;1144struct exclude_stack *stk = NULL;1145struct untracked_cache_dir *untracked;1146int current;11471148 group = &dir->exclude_list_group[EXC_DIRS];11491150/*1151 * Pop the exclude lists from the EXCL_DIRS exclude_list_group1152 * which originate from directories not in the prefix of the1153 * path being checked.1154 */1155while((stk = dir->exclude_stack) != NULL) {1156if(stk->baselen <= baselen &&1157!strncmp(dir->basebuf.buf, base, stk->baselen))1158break;1159 el = &group->el[dir->exclude_stack->exclude_ix];1160 dir->exclude_stack = stk->prev;1161 dir->exclude = NULL;1162free((char*)el->src);/* see strbuf_detach() below */1163clear_exclude_list(el);1164free(stk);1165 group->nr--;1166}11671168/* Skip traversing into sub directories if the parent is excluded */1169if(dir->exclude)1170return;11711172/*1173 * Lazy initialization. All call sites currently just1174 * memset(dir, 0, sizeof(*dir)) before use. Changing all of1175 * them seems lots of work for little benefit.1176 */1177if(!dir->basebuf.buf)1178strbuf_init(&dir->basebuf, PATH_MAX);11791180/* Read from the parent directories and push them down. */1181 current = stk ? stk->baselen : -1;1182strbuf_setlen(&dir->basebuf, current <0?0: current);1183if(dir->untracked)1184 untracked = stk ? stk->ucd : dir->untracked->root;1185else1186 untracked = NULL;11871188while(current < baselen) {1189const char*cp;1190struct oid_stat oid_stat;11911192 stk =xcalloc(1,sizeof(*stk));1193if(current <0) {1194 cp = base;1195 current =0;1196}else{1197 cp =strchr(base + current +1,'/');1198if(!cp)1199die("oops in prep_exclude");1200 cp++;1201 untracked =1202lookup_untracked(dir->untracked, untracked,1203 base + current,1204 cp - base - current);1205}1206 stk->prev = dir->exclude_stack;1207 stk->baselen = cp - base;1208 stk->exclude_ix = group->nr;1209 stk->ucd = untracked;1210 el =add_exclude_list(dir, EXC_DIRS, NULL);1211strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1212assert(stk->baselen == dir->basebuf.len);12131214/* Abort if the directory is excluded */1215if(stk->baselen) {1216int dt = DT_DIR;1217 dir->basebuf.buf[stk->baselen -1] =0;1218 dir->exclude =last_exclude_matching_from_lists(dir,1219 istate,1220 dir->basebuf.buf, stk->baselen -1,1221 dir->basebuf.buf + current, &dt);1222 dir->basebuf.buf[stk->baselen -1] ='/';1223if(dir->exclude &&1224 dir->exclude->flags & EXC_FLAG_NEGATIVE)1225 dir->exclude = NULL;1226if(dir->exclude) {1227 dir->exclude_stack = stk;1228return;1229}1230}12311232/* Try to read per-directory file */1233oidclr(&oid_stat.oid);1234 oid_stat.valid =0;1235if(dir->exclude_per_dir &&1236/*1237 * If we know that no files have been added in1238 * this directory (i.e. valid_cached_dir() has1239 * been executed and set untracked->valid) ..1240 */1241(!untracked || !untracked->valid ||1242/*1243 * .. and .gitignore does not exist before1244 * (i.e. null exclude_sha1). Then we can skip1245 * loading .gitignore, which would result in1246 * ENOENT anyway.1247 */1248!is_null_sha1(untracked->exclude_sha1))) {1249/*1250 * dir->basebuf gets reused by the traversal, but we1251 * need fname to remain unchanged to ensure the src1252 * member of each struct exclude correctly1253 * back-references its source file. Other invocations1254 * of add_exclude_list provide stable strings, so we1255 * strbuf_detach() and free() here in the caller.1256 */1257struct strbuf sb = STRBUF_INIT;1258strbuf_addbuf(&sb, &dir->basebuf);1259strbuf_addstr(&sb, dir->exclude_per_dir);1260 el->src =strbuf_detach(&sb, NULL);1261add_excludes(el->src, el->src, stk->baselen, el, istate,1262 untracked ? &oid_stat : NULL);1263}1264/*1265 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1266 * will first be called in valid_cached_dir() then maybe many1267 * times more in last_exclude_matching(). When the cache is1268 * used, last_exclude_matching() will not be called and1269 * reading .gitignore content will be a waste.1270 *1271 * So when it's called by valid_cached_dir() and we can get1272 * .gitignore SHA-1 from the index (i.e. .gitignore is not1273 * modified on work tree), we could delay reading the1274 * .gitignore content until we absolutely need it in1275 * last_exclude_matching(). Be careful about ignore rule1276 * order, though, if you do that.1277 */1278if(untracked &&1279hashcmp(oid_stat.oid.hash, untracked->exclude_sha1)) {1280invalidate_gitignore(dir->untracked, untracked);1281hashcpy(untracked->exclude_sha1, oid_stat.oid.hash);1282}1283 dir->exclude_stack = stk;1284 current = stk->baselen;1285}1286strbuf_setlen(&dir->basebuf, baselen);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 the exclude_list element which matched, or NULL for1293 * undecided.1294 */1295struct exclude *last_exclude_matching(struct dir_struct *dir,1296struct index_state *istate,1297const char*pathname,1298int*dtype_p)1299{1300int pathlen =strlen(pathname);1301const char*basename =strrchr(pathname,'/');1302 basename = (basename) ? basename+1: pathname;13031304prep_exclude(dir, istate, pathname, basename-pathname);13051306if(dir->exclude)1307return dir->exclude;13081309returnlast_exclude_matching_from_lists(dir, istate, pathname, pathlen,1310 basename, dtype_p);1311}13121313/*1314 * Loads the exclude lists for the directory containing pathname, then1315 * scans all exclude lists to determine whether pathname is excluded.1316 * Returns 1 if true, otherwise 0.1317 */1318intis_excluded(struct dir_struct *dir,struct index_state *istate,1319const char*pathname,int*dtype_p)1320{1321struct exclude *exclude =1322last_exclude_matching(dir, istate, pathname, dtype_p);1323if(exclude)1324return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1325return0;1326}13271328static struct dir_entry *dir_entry_new(const char*pathname,int len)1329{1330struct dir_entry *ent;13311332FLEX_ALLOC_MEM(ent, name, pathname, len);1333 ent->len = len;1334return ent;1335}13361337static struct dir_entry *dir_add_name(struct dir_struct *dir,1338struct index_state *istate,1339const char*pathname,int len)1340{1341if(index_file_exists(istate, pathname, len, ignore_case))1342return NULL;13431344ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1345return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1346}13471348struct dir_entry *dir_add_ignored(struct dir_struct *dir,1349struct index_state *istate,1350const char*pathname,int len)1351{1352if(!index_name_is_other(istate, pathname, len))1353return NULL;13541355ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1356return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1357}13581359enum exist_status {1360 index_nonexistent =0,1361 index_directory,1362 index_gitdir1363};13641365/*1366 * Do not use the alphabetically sorted index to look up1367 * the directory name; instead, use the case insensitive1368 * directory hash.1369 */1370static enum exist_status directory_exists_in_index_icase(struct index_state *istate,1371const char*dirname,int len)1372{1373struct cache_entry *ce;13741375if(index_dir_exists(istate, dirname, len))1376return index_directory;13771378 ce =index_file_exists(istate, dirname, len, ignore_case);1379if(ce &&S_ISGITLINK(ce->ce_mode))1380return index_gitdir;13811382return index_nonexistent;1383}13841385/*1386 * The index sorts alphabetically by entry name, which1387 * means that a gitlink sorts as '\0' at the end, while1388 * a directory (which is defined not as an entry, but as1389 * the files it contains) will sort with the '/' at the1390 * end.1391 */1392static enum exist_status directory_exists_in_index(struct index_state *istate,1393const char*dirname,int len)1394{1395int pos;13961397if(ignore_case)1398returndirectory_exists_in_index_icase(istate, dirname, len);13991400 pos =index_name_pos(istate, dirname, len);1401if(pos <0)1402 pos = -pos-1;1403while(pos < istate->cache_nr) {1404const struct cache_entry *ce = istate->cache[pos++];1405unsigned char endchar;14061407if(strncmp(ce->name, dirname, len))1408break;1409 endchar = ce->name[len];1410if(endchar >'/')1411break;1412if(endchar =='/')1413return index_directory;1414if(!endchar &&S_ISGITLINK(ce->ce_mode))1415return index_gitdir;1416}1417return index_nonexistent;1418}14191420/*1421 * When we find a directory when traversing the filesystem, we1422 * have three distinct cases:1423 *1424 * - ignore it1425 * - see it as a directory1426 * - recurse into it1427 *1428 * and which one we choose depends on a combination of existing1429 * git index contents and the flags passed into the directory1430 * traversal routine.1431 *1432 * Case 1: If we *already* have entries in the index under that1433 * directory name, we always recurse into the directory to see1434 * all the files.1435 *1436 * Case 2: If we *already* have that directory name as a gitlink,1437 * we always continue to see it as a gitlink, regardless of whether1438 * there is an actual git directory there or not (it might not1439 * be checked out as a subproject!)1440 *1441 * Case 3: if we didn't have it in the index previously, we1442 * have a few sub-cases:1443 *1444 * (a) if "show_other_directories" is true, we show it as1445 * just a directory, unless "hide_empty_directories" is1446 * also true, in which case we need to check if it contains any1447 * untracked and / or ignored files.1448 * (b) if it looks like a git directory, and we don't have1449 * 'no_gitlinks' set we treat it as a gitlink, and show it1450 * as a directory.1451 * (c) otherwise, we recurse into it.1452 */1453static enum path_treatment treat_directory(struct dir_struct *dir,1454struct index_state *istate,1455struct untracked_cache_dir *untracked,1456const char*dirname,int len,int baselen,int exclude,1457const struct pathspec *pathspec)1458{1459/* The "len-1" is to strip the final '/' */1460switch(directory_exists_in_index(istate, dirname, len-1)) {1461case index_directory:1462return path_recurse;14631464case index_gitdir:1465return path_none;14661467case index_nonexistent:1468if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1469break;1470if(exclude &&1471(dir->flags & DIR_SHOW_IGNORED_TOO) &&1472(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {14731474/*1475 * This is an excluded directory and we are1476 * showing ignored paths that match an exclude1477 * pattern. (e.g. show directory as ignored1478 * only if it matches an exclude pattern).1479 * This path will either be 'path_excluded`1480 * (if we are showing empty directories or if1481 * the directory is not empty), or will be1482 * 'path_none' (empty directory, and we are1483 * not showing empty directories).1484 */1485if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1486return path_excluded;14871488if(read_directory_recursive(dir, istate, dirname, len,1489 untracked,1,1, pathspec) == path_excluded)1490return path_excluded;14911492return path_none;1493}1494if(!(dir->flags & DIR_NO_GITLINKS)) {1495struct object_id oid;1496if(resolve_gitlink_ref(dirname,"HEAD", &oid) ==0)1497return exclude ? path_excluded : path_untracked;1498}1499return path_recurse;1500}15011502/* This is the "show_other_directories" case */15031504if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1505return exclude ? path_excluded : path_untracked;15061507 untracked =lookup_untracked(dir->untracked, untracked,1508 dirname + baselen, len - baselen);15091510/*1511 * If this is an excluded directory, then we only need to check if1512 * the directory contains any files.1513 */1514returnread_directory_recursive(dir, istate, dirname, len,1515 untracked,1, exclude, pathspec);1516}15171518/*1519 * This is an inexact early pruning of any recursive directory1520 * reading - if the path cannot possibly be in the pathspec,1521 * return true, and we'll skip it early.1522 */1523static intsimplify_away(const char*path,int pathlen,1524const struct pathspec *pathspec)1525{1526int i;15271528if(!pathspec || !pathspec->nr)1529return0;15301531GUARD_PATHSPEC(pathspec,1532 PATHSPEC_FROMTOP |1533 PATHSPEC_MAXDEPTH |1534 PATHSPEC_LITERAL |1535 PATHSPEC_GLOB |1536 PATHSPEC_ICASE |1537 PATHSPEC_EXCLUDE |1538 PATHSPEC_ATTR);15391540for(i =0; i < pathspec->nr; i++) {1541const struct pathspec_item *item = &pathspec->items[i];1542int len = item->nowildcard_len;15431544if(len > pathlen)1545 len = pathlen;1546if(!ps_strncmp(item, item->match, path, len))1547return0;1548}15491550return1;1551}15521553/*1554 * This function tells us whether an excluded path matches a1555 * list of "interesting" pathspecs. That is, whether a path matched1556 * by any of the pathspecs could possibly be ignored by excluding1557 * the specified path. This can happen if:1558 *1559 * 1. the path is mentioned explicitly in the pathspec1560 *1561 * 2. the path is a directory prefix of some element in the1562 * pathspec1563 */1564static intexclude_matches_pathspec(const char*path,int pathlen,1565const struct pathspec *pathspec)1566{1567int i;15681569if(!pathspec || !pathspec->nr)1570return0;15711572GUARD_PATHSPEC(pathspec,1573 PATHSPEC_FROMTOP |1574 PATHSPEC_MAXDEPTH |1575 PATHSPEC_LITERAL |1576 PATHSPEC_GLOB |1577 PATHSPEC_ICASE |1578 PATHSPEC_EXCLUDE);15791580for(i =0; i < pathspec->nr; i++) {1581const struct pathspec_item *item = &pathspec->items[i];1582int len = item->nowildcard_len;15831584if(len == pathlen &&1585!ps_strncmp(item, item->match, path, pathlen))1586return1;1587if(len > pathlen &&1588 item->match[pathlen] =='/'&&1589!ps_strncmp(item, item->match, path, pathlen))1590return1;1591}1592return0;1593}15941595static intget_index_dtype(struct index_state *istate,1596const char*path,int len)1597{1598int pos;1599const struct cache_entry *ce;16001601 ce =index_file_exists(istate, path, len,0);1602if(ce) {1603if(!ce_uptodate(ce))1604return DT_UNKNOWN;1605if(S_ISGITLINK(ce->ce_mode))1606return DT_DIR;1607/*1608 * Nobody actually cares about the1609 * difference between DT_LNK and DT_REG1610 */1611return DT_REG;1612}16131614/* Try to look it up as a directory */1615 pos =index_name_pos(istate, path, len);1616if(pos >=0)1617return DT_UNKNOWN;1618 pos = -pos-1;1619while(pos < istate->cache_nr) {1620 ce = istate->cache[pos++];1621if(strncmp(ce->name, path, len))1622break;1623if(ce->name[len] >'/')1624break;1625if(ce->name[len] <'/')1626continue;1627if(!ce_uptodate(ce))1628break;/* continue? */1629return DT_DIR;1630}1631return DT_UNKNOWN;1632}16331634static intget_dtype(struct dirent *de,struct index_state *istate,1635const char*path,int len)1636{1637int dtype = de ?DTYPE(de) : DT_UNKNOWN;1638struct stat st;16391640if(dtype != DT_UNKNOWN)1641return dtype;1642 dtype =get_index_dtype(istate, path, len);1643if(dtype != DT_UNKNOWN)1644return dtype;1645if(lstat(path, &st))1646return dtype;1647if(S_ISREG(st.st_mode))1648return DT_REG;1649if(S_ISDIR(st.st_mode))1650return DT_DIR;1651if(S_ISLNK(st.st_mode))1652return DT_LNK;1653return dtype;1654}16551656static enum path_treatment treat_one_path(struct dir_struct *dir,1657struct untracked_cache_dir *untracked,1658struct index_state *istate,1659struct strbuf *path,1660int baselen,1661const struct pathspec *pathspec,1662int dtype,struct dirent *de)1663{1664int exclude;1665int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);1666enum path_treatment path_treatment;16671668if(dtype == DT_UNKNOWN)1669 dtype =get_dtype(de, istate, path->buf, path->len);16701671/* Always exclude indexed files */1672if(dtype != DT_DIR && has_path_in_index)1673return path_none;16741675/*1676 * When we are looking at a directory P in the working tree,1677 * there are three cases:1678 *1679 * (1) P exists in the index. Everything inside the directory P in1680 * the working tree needs to go when P is checked out from the1681 * index.1682 *1683 * (2) P does not exist in the index, but there is P/Q in the index.1684 * We know P will stay a directory when we check out the contents1685 * of the index, but we do not know yet if there is a directory1686 * P/Q in the working tree to be killed, so we need to recurse.1687 *1688 * (3) P does not exist in the index, and there is no P/Q in the index1689 * to require P to be a directory, either. Only in this case, we1690 * know that everything inside P will not be killed without1691 * recursing.1692 */1693if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1694(dtype == DT_DIR) &&1695!has_path_in_index &&1696(directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))1697return path_none;16981699 exclude =is_excluded(dir, istate, path->buf, &dtype);17001701/*1702 * Excluded? If we don't explicitly want to show1703 * ignored files, ignore it1704 */1705if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1706return path_excluded;17071708switch(dtype) {1709default:1710return path_none;1711case DT_DIR:1712strbuf_addch(path,'/');1713 path_treatment =treat_directory(dir, istate, untracked,1714 path->buf, path->len,1715 baselen, exclude, pathspec);1716/*1717 * If 1) we only want to return directories that1718 * match an exclude pattern and 2) this directory does1719 * not match an exclude pattern but all of its1720 * contents are excluded, then indicate that we should1721 * recurse into this directory (instead of marking the1722 * directory itself as an ignored path).1723 */1724if(!exclude &&1725 path_treatment == path_excluded &&1726(dir->flags & DIR_SHOW_IGNORED_TOO) &&1727(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING))1728return path_recurse;1729return path_treatment;1730case DT_REG:1731case DT_LNK:1732return exclude ? path_excluded : path_untracked;1733}1734}17351736static enum path_treatment treat_path_fast(struct dir_struct *dir,1737struct untracked_cache_dir *untracked,1738struct cached_dir *cdir,1739struct index_state *istate,1740struct strbuf *path,1741int baselen,1742const struct pathspec *pathspec)1743{1744strbuf_setlen(path, baselen);1745if(!cdir->ucd) {1746strbuf_addstr(path, cdir->file);1747return path_untracked;1748}1749strbuf_addstr(path, cdir->ucd->name);1750/* treat_one_path() does this before it calls treat_directory() */1751strbuf_complete(path,'/');1752if(cdir->ucd->check_only)1753/*1754 * check_only is set as a result of treat_directory() getting1755 * to its bottom. Verify again the same set of directories1756 * with check_only set.1757 */1758returnread_directory_recursive(dir, istate, path->buf, path->len,1759 cdir->ucd,1,0, pathspec);1760/*1761 * We get path_recurse in the first run when1762 * directory_exists_in_index() returns index_nonexistent. We1763 * are sure that new changes in the index does not impact the1764 * outcome. Return now.1765 */1766return path_recurse;1767}17681769static enum path_treatment treat_path(struct dir_struct *dir,1770struct untracked_cache_dir *untracked,1771struct cached_dir *cdir,1772struct index_state *istate,1773struct strbuf *path,1774int baselen,1775const struct pathspec *pathspec)1776{1777int dtype;1778struct dirent *de = cdir->de;17791780if(!de)1781returntreat_path_fast(dir, untracked, cdir, istate, path,1782 baselen, pathspec);1783if(is_dot_or_dotdot(de->d_name) || !fspathcmp(de->d_name,".git"))1784return path_none;1785strbuf_setlen(path, baselen);1786strbuf_addstr(path, de->d_name);1787if(simplify_away(path->buf, path->len, pathspec))1788return path_none;17891790 dtype =DTYPE(de);1791returntreat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);1792}17931794static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1795{1796if(!dir)1797return;1798ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1799 dir->untracked_alloc);1800 dir->untracked[dir->untracked_nr++] =xstrdup(name);1801}18021803static intvalid_cached_dir(struct dir_struct *dir,1804struct untracked_cache_dir *untracked,1805struct index_state *istate,1806struct strbuf *path,1807int check_only)1808{1809struct stat st;18101811if(!untracked)1812return0;18131814/*1815 * With fsmonitor, we can trust the untracked cache's valid field.1816 */1817refresh_fsmonitor(istate);1818if(!(dir->untracked->use_fsmonitor && untracked->valid)) {1819if(lstat(path->len ? path->buf :".", &st)) {1820memset(&untracked->stat_data,0,sizeof(untracked->stat_data));1821return0;1822}1823if(!untracked->valid ||1824match_stat_data_racy(istate, &untracked->stat_data, &st)) {1825fill_stat_data(&untracked->stat_data, &st);1826return0;1827}1828}18291830if(untracked->check_only != !!check_only)1831return0;18321833/*1834 * prep_exclude will be called eventually on this directory,1835 * but it's called much later in last_exclude_matching(). We1836 * need it now to determine the validity of the cache for this1837 * path. The next calls will be nearly no-op, the way1838 * prep_exclude() is designed.1839 */1840if(path->len && path->buf[path->len -1] !='/') {1841strbuf_addch(path,'/');1842prep_exclude(dir, istate, path->buf, path->len);1843strbuf_setlen(path, path->len -1);1844}else1845prep_exclude(dir, istate, path->buf, path->len);18461847/* hopefully prep_exclude() haven't invalidated this entry... */1848return untracked->valid;1849}18501851static intopen_cached_dir(struct cached_dir *cdir,1852struct dir_struct *dir,1853struct untracked_cache_dir *untracked,1854struct index_state *istate,1855struct strbuf *path,1856int check_only)1857{1858const char*c_path;18591860memset(cdir,0,sizeof(*cdir));1861 cdir->untracked = untracked;1862if(valid_cached_dir(dir, untracked, istate, path, check_only))1863return0;1864 c_path = path->len ? path->buf :".";1865 cdir->fdir =opendir(c_path);1866if(!cdir->fdir)1867warning_errno(_("could not open directory '%s'"), c_path);1868if(dir->untracked) {1869invalidate_directory(dir->untracked, untracked);1870 dir->untracked->dir_opened++;1871}1872if(!cdir->fdir)1873return-1;1874return0;1875}18761877static intread_cached_dir(struct cached_dir *cdir)1878{1879if(cdir->fdir) {1880 cdir->de =readdir(cdir->fdir);1881if(!cdir->de)1882return-1;1883return0;1884}1885while(cdir->nr_dirs < cdir->untracked->dirs_nr) {1886struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1887if(!d->recurse) {1888 cdir->nr_dirs++;1889continue;1890}1891 cdir->ucd = d;1892 cdir->nr_dirs++;1893return0;1894}1895 cdir->ucd = NULL;1896if(cdir->nr_files < cdir->untracked->untracked_nr) {1897struct untracked_cache_dir *d = cdir->untracked;1898 cdir->file = d->untracked[cdir->nr_files++];1899return0;1900}1901return-1;1902}19031904static voidclose_cached_dir(struct cached_dir *cdir)1905{1906if(cdir->fdir)1907closedir(cdir->fdir);1908/*1909 * We have gone through this directory and found no untracked1910 * entries. Mark it valid.1911 */1912if(cdir->untracked) {1913 cdir->untracked->valid =1;1914 cdir->untracked->recurse =1;1915}1916}19171918/*1919 * Read a directory tree. We currently ignore anything but1920 * directories, regular files and symlinks. That's because git1921 * doesn't handle them at all yet. Maybe that will change some1922 * day.1923 *1924 * Also, we ignore the name ".git" (even if it is not a directory).1925 * That likely will not change.1926 *1927 * If 'stop_at_first_file' is specified, 'path_excluded' is returned1928 * to signal that a file was found. This is the least significant value that1929 * indicates that a file was encountered that does not depend on the order of1930 * whether an untracked or exluded path was encountered first.1931 *1932 * Returns the most significant path_treatment value encountered in the scan.1933 * If 'stop_at_first_file' is specified, `path_excluded` is the most1934 * significant path_treatment value that will be returned.1935 */19361937static enum path_treatment read_directory_recursive(struct dir_struct *dir,1938struct index_state *istate,const char*base,int baselen,1939struct untracked_cache_dir *untracked,int check_only,1940int stop_at_first_file,const struct pathspec *pathspec)1941{1942struct cached_dir cdir;1943enum path_treatment state, subdir_state, dir_state = path_none;1944struct strbuf path = STRBUF_INIT;19451946strbuf_add(&path, base, baselen);19471948if(open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))1949goto out;19501951if(untracked)1952 untracked->check_only = !!check_only;19531954while(!read_cached_dir(&cdir)) {1955/* check how the file or directory should be treated */1956 state =treat_path(dir, untracked, &cdir, istate, &path,1957 baselen, pathspec);19581959if(state > dir_state)1960 dir_state = state;19611962/* recurse into subdir if instructed by treat_path */1963if((state == path_recurse) ||1964((state == path_untracked) &&1965(dir->flags & DIR_SHOW_IGNORED_TOO) &&1966(get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {1967struct untracked_cache_dir *ud;1968 ud =lookup_untracked(dir->untracked, untracked,1969 path.buf + baselen,1970 path.len - baselen);1971 subdir_state =1972read_directory_recursive(dir, istate, path.buf,1973 path.len, ud,1974 check_only, stop_at_first_file, pathspec);1975if(subdir_state > dir_state)1976 dir_state = subdir_state;1977}19781979if(check_only) {1980if(stop_at_first_file) {1981/*1982 * If stopping at first file, then1983 * signal that a file was found by1984 * returning `path_excluded`. This is1985 * to return a consistent value1986 * regardless of whether an ignored or1987 * excluded file happened to be1988 * encountered 1st.1989 *1990 * In current usage, the1991 * `stop_at_first_file` is passed when1992 * an ancestor directory has matched1993 * an exclude pattern, so any found1994 * files will be excluded.1995 */1996if(dir_state >= path_excluded) {1997 dir_state = path_excluded;1998break;1999}2000}20012002/* abort early if maximum state has been reached */2003if(dir_state == path_untracked) {2004if(cdir.fdir)2005add_untracked(untracked, path.buf + baselen);2006break;2007}2008/* skip the dir_add_* part */2009continue;2010}20112012/* add the path to the appropriate result list */2013switch(state) {2014case path_excluded:2015if(dir->flags & DIR_SHOW_IGNORED)2016dir_add_name(dir, istate, path.buf, path.len);2017else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||2018((dir->flags & DIR_COLLECT_IGNORED) &&2019exclude_matches_pathspec(path.buf, path.len,2020 pathspec)))2021dir_add_ignored(dir, istate, path.buf, path.len);2022break;20232024case path_untracked:2025if(dir->flags & DIR_SHOW_IGNORED)2026break;2027dir_add_name(dir, istate, path.buf, path.len);2028if(cdir.fdir)2029add_untracked(untracked, path.buf + baselen);2030break;20312032default:2033break;2034}2035}2036close_cached_dir(&cdir);2037 out:2038strbuf_release(&path);20392040return dir_state;2041}20422043intcmp_dir_entry(const void*p1,const void*p2)2044{2045const struct dir_entry *e1 = *(const struct dir_entry **)p1;2046const struct dir_entry *e2 = *(const struct dir_entry **)p2;20472048returnname_compare(e1->name, e1->len, e2->name, e2->len);2049}20502051/* check if *out lexically strictly contains *in */2052intcheck_dir_entry_contains(const struct dir_entry *out,const struct dir_entry *in)2053{2054return(out->len < in->len) &&2055(out->name[out->len -1] =='/') &&2056!memcmp(out->name, in->name, out->len);2057}20582059static inttreat_leading_path(struct dir_struct *dir,2060struct index_state *istate,2061const char*path,int len,2062const struct pathspec *pathspec)2063{2064struct strbuf sb = STRBUF_INIT;2065int baselen, rc =0;2066const char*cp;2067int old_flags = dir->flags;20682069while(len && path[len -1] =='/')2070 len--;2071if(!len)2072return1;2073 baselen =0;2074 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;2075while(1) {2076 cp = path + baselen + !!baselen;2077 cp =memchr(cp,'/', path + len - cp);2078if(!cp)2079 baselen = len;2080else2081 baselen = cp - path;2082strbuf_setlen(&sb,0);2083strbuf_add(&sb, path, baselen);2084if(!is_directory(sb.buf))2085break;2086if(simplify_away(sb.buf, sb.len, pathspec))2087break;2088if(treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,2089 DT_DIR, NULL) == path_none)2090break;/* do not recurse into it */2091if(len <= baselen) {2092 rc =1;2093break;/* finished checking */2094}2095}2096strbuf_release(&sb);2097 dir->flags = old_flags;2098return rc;2099}21002101static const char*get_ident_string(void)2102{2103static struct strbuf sb = STRBUF_INIT;2104struct utsname uts;21052106if(sb.len)2107return sb.buf;2108if(uname(&uts) <0)2109die_errno(_("failed to get kernel name and information"));2110strbuf_addf(&sb,"Location%s, system%s",get_git_work_tree(),2111 uts.sysname);2112return sb.buf;2113}21142115static intident_in_untracked(const struct untracked_cache *uc)2116{2117/*2118 * Previous git versions may have saved many NUL separated2119 * strings in the "ident" field, but it is insane to manage2120 * many locations, so just take care of the first one.2121 */21222123return!strcmp(uc->ident.buf,get_ident_string());2124}21252126static voidset_untracked_ident(struct untracked_cache *uc)2127{2128strbuf_reset(&uc->ident);2129strbuf_addstr(&uc->ident,get_ident_string());21302131/*2132 * This strbuf used to contain a list of NUL separated2133 * strings, so save NUL too for backward compatibility.2134 */2135strbuf_addch(&uc->ident,0);2136}21372138static voidnew_untracked_cache(struct index_state *istate)2139{2140struct untracked_cache *uc =xcalloc(1,sizeof(*uc));2141strbuf_init(&uc->ident,100);2142 uc->exclude_per_dir =".gitignore";2143/* should be the same flags used by git-status */2144 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;2145set_untracked_ident(uc);2146 istate->untracked = uc;2147 istate->cache_changed |= UNTRACKED_CHANGED;2148}21492150voidadd_untracked_cache(struct index_state *istate)2151{2152if(!istate->untracked) {2153new_untracked_cache(istate);2154}else{2155if(!ident_in_untracked(istate->untracked)) {2156free_untracked_cache(istate->untracked);2157new_untracked_cache(istate);2158}2159}2160}21612162voidremove_untracked_cache(struct index_state *istate)2163{2164if(istate->untracked) {2165free_untracked_cache(istate->untracked);2166 istate->untracked = NULL;2167 istate->cache_changed |= UNTRACKED_CHANGED;2168}2169}21702171static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,2172int base_len,2173const struct pathspec *pathspec)2174{2175struct untracked_cache_dir *root;2176static int untracked_cache_disabled = -1;21772178if(!dir->untracked)2179return NULL;2180if(untracked_cache_disabled <0)2181 untracked_cache_disabled =git_env_bool("GIT_DISABLE_UNTRACKED_CACHE",0);2182if(untracked_cache_disabled)2183return NULL;21842185/*2186 * We only support $GIT_DIR/info/exclude and core.excludesfile2187 * as the global ignore rule files. Any other additions2188 * (e.g. from command line) invalidate the cache. This2189 * condition also catches running setup_standard_excludes()2190 * before setting dir->untracked!2191 */2192if(dir->unmanaged_exclude_files)2193return NULL;21942195/*2196 * Optimize for the main use case only: whole-tree git2197 * status. More work involved in treat_leading_path() if we2198 * use cache on just a subset of the worktree. pathspec2199 * support could make the matter even worse.2200 */2201if(base_len || (pathspec && pathspec->nr))2202return NULL;22032204/* Different set of flags may produce different results */2205if(dir->flags != dir->untracked->dir_flags ||2206/*2207 * See treat_directory(), case index_nonexistent. Without2208 * this flag, we may need to also cache .git file content2209 * for the resolve_gitlink_ref() call, which we don't.2210 */2211!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||2212/* We don't support collecting ignore files */2213(dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |2214 DIR_COLLECT_IGNORED)))2215return NULL;22162217/*2218 * If we use .gitignore in the cache and now you change it to2219 * .gitexclude, everything will go wrong.2220 */2221if(dir->exclude_per_dir != dir->untracked->exclude_per_dir &&2222strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))2223return NULL;22242225/*2226 * EXC_CMDL is not considered in the cache. If people set it,2227 * skip the cache.2228 */2229if(dir->exclude_list_group[EXC_CMDL].nr)2230return NULL;22312232if(!ident_in_untracked(dir->untracked)) {2233warning(_("Untracked cache is disabled on this system or location."));2234return NULL;2235}22362237if(!dir->untracked->root) {2238const int len =sizeof(*dir->untracked->root);2239 dir->untracked->root =xmalloc(len);2240memset(dir->untracked->root,0, len);2241}22422243/* Validate $GIT_DIR/info/exclude and core.excludesfile */2244 root = dir->untracked->root;2245if(oidcmp(&dir->ss_info_exclude.oid,2246&dir->untracked->ss_info_exclude.oid)) {2247invalidate_gitignore(dir->untracked, root);2248 dir->untracked->ss_info_exclude = dir->ss_info_exclude;2249}2250if(oidcmp(&dir->ss_excludes_file.oid,2251&dir->untracked->ss_excludes_file.oid)) {2252invalidate_gitignore(dir->untracked, root);2253 dir->untracked->ss_excludes_file = dir->ss_excludes_file;2254}22552256/* Make sure this directory is not dropped out at saving phase */2257 root->recurse =1;2258return root;2259}22602261intread_directory(struct dir_struct *dir,struct index_state *istate,2262const char*path,int len,const struct pathspec *pathspec)2263{2264struct untracked_cache_dir *untracked;2265uint64_t start =getnanotime();22662267if(has_symlink_leading_path(path, len))2268return dir->nr;22692270 untracked =validate_untracked_cache(dir, len, pathspec);2271if(!untracked)2272/*2273 * make sure untracked cache code path is disabled,2274 * e.g. prep_exclude()2275 */2276 dir->untracked = NULL;2277if(!len ||treat_leading_path(dir, istate, path, len, pathspec))2278read_directory_recursive(dir, istate, path, len, untracked,0,0, pathspec);2279QSORT(dir->entries, dir->nr, cmp_dir_entry);2280QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);22812282/*2283 * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will2284 * also pick up untracked contents of untracked dirs; by default2285 * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.2286 */2287if((dir->flags & DIR_SHOW_IGNORED_TOO) &&2288!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {2289int i, j;22902291/* remove from dir->entries untracked contents of untracked dirs */2292for(i = j =0; j < dir->nr; j++) {2293if(i &&2294check_dir_entry_contains(dir->entries[i -1], dir->entries[j])) {2295FREE_AND_NULL(dir->entries[j]);2296}else{2297 dir->entries[i++] = dir->entries[j];2298}2299}23002301 dir->nr = i;2302}23032304trace_performance_since(start,"read directory %.*s", len, path);2305if(dir->untracked) {2306static int force_untracked_cache = -1;2307static struct trace_key trace_untracked_stats =TRACE_KEY_INIT(UNTRACKED_STATS);23082309if(force_untracked_cache <0)2310 force_untracked_cache =2311git_env_bool("GIT_FORCE_UNTRACKED_CACHE",0);2312trace_printf_key(&trace_untracked_stats,2313"node creation:%u\n"2314"gitignore invalidation:%u\n"2315"directory invalidation:%u\n"2316"opendir:%u\n",2317 dir->untracked->dir_created,2318 dir->untracked->gitignore_invalidated,2319 dir->untracked->dir_invalidated,2320 dir->untracked->dir_opened);2321if(force_untracked_cache &&2322 dir->untracked == istate->untracked &&2323(dir->untracked->dir_opened ||2324 dir->untracked->gitignore_invalidated ||2325 dir->untracked->dir_invalidated))2326 istate->cache_changed |= UNTRACKED_CHANGED;2327if(dir->untracked != istate->untracked) {2328FREE_AND_NULL(dir->untracked);2329}2330}2331return dir->nr;2332}23332334intfile_exists(const char*f)2335{2336struct stat sb;2337returnlstat(f, &sb) ==0;2338}23392340static intcmp_icase(char a,char b)2341{2342if(a == b)2343return0;2344if(ignore_case)2345returntoupper(a) -toupper(b);2346return a - b;2347}23482349/*2350 * Given two normalized paths (a trailing slash is ok), if subdir is2351 * outside dir, return -1. Otherwise return the offset in subdir that2352 * can be used as relative path to dir.2353 */2354intdir_inside_of(const char*subdir,const char*dir)2355{2356int offset =0;23572358assert(dir && subdir && *dir && *subdir);23592360while(*dir && *subdir && !cmp_icase(*dir, *subdir)) {2361 dir++;2362 subdir++;2363 offset++;2364}23652366/* hel[p]/me vs hel[l]/yeah */2367if(*dir && *subdir)2368return-1;23692370if(!*subdir)2371return!*dir ? offset : -1;/* same dir */23722373/* foo/[b]ar vs foo/[] */2374if(is_dir_sep(dir[-1]))2375returnis_dir_sep(subdir[-1]) ? offset : -1;23762377/* foo[/]bar vs foo[] */2378returnis_dir_sep(*subdir) ? offset +1: -1;2379}23802381intis_inside_dir(const char*dir)2382{2383char*cwd;2384int rc;23852386if(!dir)2387return0;23882389 cwd =xgetcwd();2390 rc = (dir_inside_of(cwd, dir) >=0);2391free(cwd);2392return rc;2393}23942395intis_empty_dir(const char*path)2396{2397DIR*dir =opendir(path);2398struct dirent *e;2399int ret =1;24002401if(!dir)2402return0;24032404while((e =readdir(dir)) != NULL)2405if(!is_dot_or_dotdot(e->d_name)) {2406 ret =0;2407break;2408}24092410closedir(dir);2411return ret;2412}24132414static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)2415{2416DIR*dir;2417struct dirent *e;2418int ret =0, original_len = path->len, len, kept_down =0;2419int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2420int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2421struct object_id submodule_head;24222423if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2424!resolve_gitlink_ref(path->buf,"HEAD", &submodule_head)) {2425/* Do not descend and nuke a nested git work tree. */2426if(kept_up)2427*kept_up =1;2428return0;2429}24302431 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2432 dir =opendir(path->buf);2433if(!dir) {2434if(errno == ENOENT)2435return keep_toplevel ? -1:0;2436else if(errno == EACCES && !keep_toplevel)2437/*2438 * An empty dir could be removable even if it2439 * is unreadable:2440 */2441returnrmdir(path->buf);2442else2443return-1;2444}2445strbuf_complete(path,'/');24462447 len = path->len;2448while((e =readdir(dir)) != NULL) {2449struct stat st;2450if(is_dot_or_dotdot(e->d_name))2451continue;24522453strbuf_setlen(path, len);2454strbuf_addstr(path, e->d_name);2455if(lstat(path->buf, &st)) {2456if(errno == ENOENT)2457/*2458 * file disappeared, which is what we2459 * wanted anyway2460 */2461continue;2462/* fall thru */2463}else if(S_ISDIR(st.st_mode)) {2464if(!remove_dir_recurse(path, flag, &kept_down))2465continue;/* happy */2466}else if(!only_empty &&2467(!unlink(path->buf) || errno == ENOENT)) {2468continue;/* happy, too */2469}24702471/* path too long, stat fails, or non-directory still exists */2472 ret = -1;2473break;2474}2475closedir(dir);24762477strbuf_setlen(path, original_len);2478if(!ret && !keep_toplevel && !kept_down)2479 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;2480else if(kept_up)2481/*2482 * report the uplevel that it is not an error that we2483 * did not rmdir() our directory.2484 */2485*kept_up = !ret;2486return ret;2487}24882489intremove_dir_recursively(struct strbuf *path,int flag)2490{2491returnremove_dir_recurse(path, flag, NULL);2492}24932494staticGIT_PATH_FUNC(git_path_info_exclude,"info/exclude")24952496voidsetup_standard_excludes(struct dir_struct *dir)2497{2498 dir->exclude_per_dir =".gitignore";24992500/* core.excludefile defaulting to $XDG_HOME/git/ignore */2501if(!excludes_file)2502 excludes_file =xdg_config_home("ignore");2503if(excludes_file && !access_or_warn(excludes_file, R_OK,0))2504add_excludes_from_file_1(dir, excludes_file,2505 dir->untracked ? &dir->ss_excludes_file : NULL);25062507/* per repository user preference */2508if(startup_info->have_repository) {2509const char*path =git_path_info_exclude();2510if(!access_or_warn(path, R_OK,0))2511add_excludes_from_file_1(dir, path,2512 dir->untracked ? &dir->ss_info_exclude : NULL);2513}2514}25152516intremove_path(const char*name)2517{2518char*slash;25192520if(unlink(name) && !is_missing_file_error(errno))2521return-1;25222523 slash =strrchr(name,'/');2524if(slash) {2525char*dirs =xstrdup(name);2526 slash = dirs + (slash - name);2527do{2528*slash ='\0';2529}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));2530free(dirs);2531}2532return0;2533}25342535/*2536 * Frees memory within dir which was allocated for exclude lists and2537 * the exclude_stack. Does not free dir itself.2538 */2539voidclear_directory(struct dir_struct *dir)2540{2541int i, j;2542struct exclude_list_group *group;2543struct exclude_list *el;2544struct exclude_stack *stk;25452546for(i = EXC_CMDL; i <= EXC_FILE; i++) {2547 group = &dir->exclude_list_group[i];2548for(j =0; j < group->nr; j++) {2549 el = &group->el[j];2550if(i == EXC_DIRS)2551free((char*)el->src);2552clear_exclude_list(el);2553}2554free(group->el);2555}25562557 stk = dir->exclude_stack;2558while(stk) {2559struct exclude_stack *prev = stk->prev;2560free(stk);2561 stk = prev;2562}2563strbuf_release(&dir->basebuf);2564}25652566struct ondisk_untracked_cache {2567struct stat_data info_exclude_stat;2568struct stat_data excludes_file_stat;2569uint32_t dir_flags;2570unsigned char info_exclude_sha1[20];2571unsigned char excludes_file_sha1[20];2572char exclude_per_dir[FLEX_ARRAY];2573};25742575#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)2576#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)25772578struct write_data {2579int index;/* number of written untracked_cache_dir */2580struct ewah_bitmap *check_only;/* from untracked_cache_dir */2581struct ewah_bitmap *valid;/* from untracked_cache_dir */2582struct ewah_bitmap *sha1_valid;/* set if exclude_sha1 is not null */2583struct strbuf out;2584struct strbuf sb_stat;2585struct strbuf sb_sha1;2586};25872588static voidstat_data_to_disk(struct stat_data *to,const struct stat_data *from)2589{2590 to->sd_ctime.sec =htonl(from->sd_ctime.sec);2591 to->sd_ctime.nsec =htonl(from->sd_ctime.nsec);2592 to->sd_mtime.sec =htonl(from->sd_mtime.sec);2593 to->sd_mtime.nsec =htonl(from->sd_mtime.nsec);2594 to->sd_dev =htonl(from->sd_dev);2595 to->sd_ino =htonl(from->sd_ino);2596 to->sd_uid =htonl(from->sd_uid);2597 to->sd_gid =htonl(from->sd_gid);2598 to->sd_size =htonl(from->sd_size);2599}26002601static voidwrite_one_dir(struct untracked_cache_dir *untracked,2602struct write_data *wd)2603{2604struct stat_data stat_data;2605struct strbuf *out = &wd->out;2606unsigned char intbuf[16];2607unsigned int intlen, value;2608int i = wd->index++;26092610/*2611 * untracked_nr should be reset whenever valid is clear, but2612 * for safety..2613 */2614if(!untracked->valid) {2615 untracked->untracked_nr =0;2616 untracked->check_only =0;2617}26182619if(untracked->check_only)2620ewah_set(wd->check_only, i);2621if(untracked->valid) {2622ewah_set(wd->valid, i);2623stat_data_to_disk(&stat_data, &untracked->stat_data);2624strbuf_add(&wd->sb_stat, &stat_data,sizeof(stat_data));2625}2626if(!is_null_sha1(untracked->exclude_sha1)) {2627ewah_set(wd->sha1_valid, i);2628strbuf_add(&wd->sb_sha1, untracked->exclude_sha1,20);2629}26302631 intlen =encode_varint(untracked->untracked_nr, intbuf);2632strbuf_add(out, intbuf, intlen);26332634/* skip non-recurse directories */2635for(i =0, value =0; i < untracked->dirs_nr; i++)2636if(untracked->dirs[i]->recurse)2637 value++;2638 intlen =encode_varint(value, intbuf);2639strbuf_add(out, intbuf, intlen);26402641strbuf_add(out, untracked->name,strlen(untracked->name) +1);26422643for(i =0; i < untracked->untracked_nr; i++)2644strbuf_add(out, untracked->untracked[i],2645strlen(untracked->untracked[i]) +1);26462647for(i =0; i < untracked->dirs_nr; i++)2648if(untracked->dirs[i]->recurse)2649write_one_dir(untracked->dirs[i], wd);2650}26512652voidwrite_untracked_extension(struct strbuf *out,struct untracked_cache *untracked)2653{2654struct ondisk_untracked_cache *ouc;2655struct write_data wd;2656unsigned char varbuf[16];2657int varint_len;2658size_t len =strlen(untracked->exclude_per_dir);26592660FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);2661stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2662stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2663hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);2664hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);2665 ouc->dir_flags =htonl(untracked->dir_flags);26662667 varint_len =encode_varint(untracked->ident.len, varbuf);2668strbuf_add(out, varbuf, varint_len);2669strbuf_addbuf(out, &untracked->ident);26702671strbuf_add(out, ouc,ouc_size(len));2672FREE_AND_NULL(ouc);26732674if(!untracked->root) {2675 varint_len =encode_varint(0, varbuf);2676strbuf_add(out, varbuf, varint_len);2677return;2678}26792680 wd.index =0;2681 wd.check_only =ewah_new();2682 wd.valid =ewah_new();2683 wd.sha1_valid =ewah_new();2684strbuf_init(&wd.out,1024);2685strbuf_init(&wd.sb_stat,1024);2686strbuf_init(&wd.sb_sha1,1024);2687write_one_dir(untracked->root, &wd);26882689 varint_len =encode_varint(wd.index, varbuf);2690strbuf_add(out, varbuf, varint_len);2691strbuf_addbuf(out, &wd.out);2692ewah_serialize_strbuf(wd.valid, out);2693ewah_serialize_strbuf(wd.check_only, out);2694ewah_serialize_strbuf(wd.sha1_valid, out);2695strbuf_addbuf(out, &wd.sb_stat);2696strbuf_addbuf(out, &wd.sb_sha1);2697strbuf_addch(out,'\0');/* safe guard for string lists */26982699ewah_free(wd.valid);2700ewah_free(wd.check_only);2701ewah_free(wd.sha1_valid);2702strbuf_release(&wd.out);2703strbuf_release(&wd.sb_stat);2704strbuf_release(&wd.sb_sha1);2705}27062707static voidfree_untracked(struct untracked_cache_dir *ucd)2708{2709int i;2710if(!ucd)2711return;2712for(i =0; i < ucd->dirs_nr; i++)2713free_untracked(ucd->dirs[i]);2714for(i =0; i < ucd->untracked_nr; i++)2715free(ucd->untracked[i]);2716free(ucd->untracked);2717free(ucd->dirs);2718free(ucd);2719}27202721voidfree_untracked_cache(struct untracked_cache *uc)2722{2723if(uc)2724free_untracked(uc->root);2725free(uc);2726}27272728struct read_data {2729int index;2730struct untracked_cache_dir **ucd;2731struct ewah_bitmap *check_only;2732struct ewah_bitmap *valid;2733struct ewah_bitmap *sha1_valid;2734const unsigned char*data;2735const unsigned char*end;2736};27372738static voidstat_data_from_disk(struct stat_data *to,const unsigned char*data)2739{2740memcpy(to, data,sizeof(*to));2741 to->sd_ctime.sec =ntohl(to->sd_ctime.sec);2742 to->sd_ctime.nsec =ntohl(to->sd_ctime.nsec);2743 to->sd_mtime.sec =ntohl(to->sd_mtime.sec);2744 to->sd_mtime.nsec =ntohl(to->sd_mtime.nsec);2745 to->sd_dev =ntohl(to->sd_dev);2746 to->sd_ino =ntohl(to->sd_ino);2747 to->sd_uid =ntohl(to->sd_uid);2748 to->sd_gid =ntohl(to->sd_gid);2749 to->sd_size =ntohl(to->sd_size);2750}27512752static intread_one_dir(struct untracked_cache_dir **untracked_,2753struct read_data *rd)2754{2755struct untracked_cache_dir ud, *untracked;2756const unsigned char*next, *data = rd->data, *end = rd->end;2757unsigned int value;2758int i, len;27592760memset(&ud,0,sizeof(ud));27612762 next = data;2763 value =decode_varint(&next);2764if(next > end)2765return-1;2766 ud.recurse =1;2767 ud.untracked_alloc = value;2768 ud.untracked_nr = value;2769if(ud.untracked_nr)2770ALLOC_ARRAY(ud.untracked, ud.untracked_nr);2771 data = next;27722773 next = data;2774 ud.dirs_alloc = ud.dirs_nr =decode_varint(&next);2775if(next > end)2776return-1;2777ALLOC_ARRAY(ud.dirs, ud.dirs_nr);2778 data = next;27792780 len =strlen((const char*)data);2781 next = data + len +1;2782if(next > rd->end)2783return-1;2784*untracked_ = untracked =xmalloc(st_add(sizeof(*untracked), len));2785memcpy(untracked, &ud,sizeof(ud));2786memcpy(untracked->name, data, len +1);2787 data = next;27882789for(i =0; i < untracked->untracked_nr; i++) {2790 len =strlen((const char*)data);2791 next = data + len +1;2792if(next > rd->end)2793return-1;2794 untracked->untracked[i] =xstrdup((const char*)data);2795 data = next;2796}27972798 rd->ucd[rd->index++] = untracked;2799 rd->data = data;28002801for(i =0; i < untracked->dirs_nr; i++) {2802 len =read_one_dir(untracked->dirs + i, rd);2803if(len <0)2804return-1;2805}2806return0;2807}28082809static voidset_check_only(size_t pos,void*cb)2810{2811struct read_data *rd = cb;2812struct untracked_cache_dir *ud = rd->ucd[pos];2813 ud->check_only =1;2814}28152816static voidread_stat(size_t pos,void*cb)2817{2818struct read_data *rd = cb;2819struct untracked_cache_dir *ud = rd->ucd[pos];2820if(rd->data +sizeof(struct stat_data) > rd->end) {2821 rd->data = rd->end +1;2822return;2823}2824stat_data_from_disk(&ud->stat_data, rd->data);2825 rd->data +=sizeof(struct stat_data);2826 ud->valid =1;2827}28282829static voidread_sha1(size_t pos,void*cb)2830{2831struct read_data *rd = cb;2832struct untracked_cache_dir *ud = rd->ucd[pos];2833if(rd->data +20> rd->end) {2834 rd->data = rd->end +1;2835return;2836}2837hashcpy(ud->exclude_sha1, rd->data);2838 rd->data +=20;2839}28402841static voidload_oid_stat(struct oid_stat *oid_stat,const unsigned char*data,2842const unsigned char*sha1)2843{2844stat_data_from_disk(&oid_stat->stat, data);2845hashcpy(oid_stat->oid.hash, sha1);2846 oid_stat->valid =1;2847}28482849struct untracked_cache *read_untracked_extension(const void*data,unsigned long sz)2850{2851struct untracked_cache *uc;2852struct read_data rd;2853const unsigned char*next = data, *end = (const unsigned char*)data + sz;2854const char*ident;2855int ident_len, len;2856const char*exclude_per_dir;28572858if(sz <=1|| end[-1] !='\0')2859return NULL;2860 end--;28612862 ident_len =decode_varint(&next);2863if(next + ident_len > end)2864return NULL;2865 ident = (const char*)next;2866 next += ident_len;28672868if(next +ouc_size(0) > end)2869return NULL;28702871 uc =xcalloc(1,sizeof(*uc));2872strbuf_init(&uc->ident, ident_len);2873strbuf_add(&uc->ident, ident, ident_len);2874load_oid_stat(&uc->ss_info_exclude,2875 next +ouc_offset(info_exclude_stat),2876 next +ouc_offset(info_exclude_sha1));2877load_oid_stat(&uc->ss_excludes_file,2878 next +ouc_offset(excludes_file_stat),2879 next +ouc_offset(excludes_file_sha1));2880 uc->dir_flags =get_be32(next +ouc_offset(dir_flags));2881 exclude_per_dir = (const char*)next +ouc_offset(exclude_per_dir);2882 uc->exclude_per_dir =xstrdup(exclude_per_dir);2883/* NUL after exclude_per_dir is covered by sizeof(*ouc) */2884 next +=ouc_size(strlen(exclude_per_dir));2885if(next >= end)2886goto done2;28872888 len =decode_varint(&next);2889if(next > end || len ==0)2890goto done2;28912892 rd.valid =ewah_new();2893 rd.check_only =ewah_new();2894 rd.sha1_valid =ewah_new();2895 rd.data = next;2896 rd.end = end;2897 rd.index =0;2898ALLOC_ARRAY(rd.ucd, len);28992900if(read_one_dir(&uc->root, &rd) || rd.index != len)2901goto done;29022903 next = rd.data;2904 len =ewah_read_mmap(rd.valid, next, end - next);2905if(len <0)2906goto done;29072908 next += len;2909 len =ewah_read_mmap(rd.check_only, next, end - next);2910if(len <0)2911goto done;29122913 next += len;2914 len =ewah_read_mmap(rd.sha1_valid, next, end - next);2915if(len <0)2916goto done;29172918ewah_each_bit(rd.check_only, set_check_only, &rd);2919 rd.data = next + len;2920ewah_each_bit(rd.valid, read_stat, &rd);2921ewah_each_bit(rd.sha1_valid, read_sha1, &rd);2922 next = rd.data;29232924done:2925free(rd.ucd);2926ewah_free(rd.valid);2927ewah_free(rd.check_only);2928ewah_free(rd.sha1_valid);2929done2:2930if(next != end) {2931free_untracked_cache(uc);2932 uc = NULL;2933}2934return uc;2935}29362937static voidinvalidate_one_directory(struct untracked_cache *uc,2938struct untracked_cache_dir *ucd)2939{2940 uc->dir_invalidated++;2941 ucd->valid =0;2942 ucd->untracked_nr =0;2943}29442945/*2946 * Normally when an entry is added or removed from a directory,2947 * invalidating that directory is enough. No need to touch its2948 * ancestors. When a directory is shown as "foo/bar/" in git-status2949 * however, deleting or adding an entry may have cascading effect.2950 *2951 * Say the "foo/bar/file" has become untracked, we need to tell the2952 * untracked_cache_dir of "foo" that "bar/" is not an untracked2953 * directory any more (because "bar" is managed by foo as an untracked2954 * "file").2955 *2956 * Similarly, if "foo/bar/file" moves from untracked to tracked and it2957 * was the last untracked entry in the entire "foo", we should show2958 * "foo/" instead. Which means we have to invalidate past "bar" up to2959 * "foo".2960 *2961 * This function traverses all directories from root to leaf. If there2962 * is a chance of one of the above cases happening, we invalidate back2963 * to root. Otherwise we just invalidate the leaf. There may be a more2964 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to2965 * detect these cases and avoid unnecessary invalidation, for example,2966 * checking for the untracked entry named "bar/" in "foo", but for now2967 * stick to something safe and simple.2968 */2969static intinvalidate_one_component(struct untracked_cache *uc,2970struct untracked_cache_dir *dir,2971const char*path,int len)2972{2973const char*rest =strchr(path,'/');29742975if(rest) {2976int component_len = rest - path;2977struct untracked_cache_dir *d =2978lookup_untracked(uc, dir, path, component_len);2979int ret =2980invalidate_one_component(uc, d, rest +1,2981 len - (component_len +1));2982if(ret)2983invalidate_one_directory(uc, dir);2984return ret;2985}29862987invalidate_one_directory(uc, dir);2988return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;2989}29902991voiduntracked_cache_invalidate_path(struct index_state *istate,2992const char*path,int safe_path)2993{2994if(!istate->untracked || !istate->untracked->root)2995return;2996if(!safe_path && !verify_path(path))2997return;2998invalidate_one_component(istate->untracked, istate->untracked->root,2999 path,strlen(path));3000}30013002voiduntracked_cache_remove_from_index(struct index_state *istate,3003const char*path)3004{3005untracked_cache_invalidate_path(istate, path,1);3006}30073008voiduntracked_cache_add_to_index(struct index_state *istate,3009const char*path)3010{3011untracked_cache_invalidate_path(istate, path,1);3012}30133014/* Update gitfile and core.worktree setting to connect work tree and git dir */3015voidconnect_work_tree_and_git_dir(const char*work_tree_,const char*git_dir_)3016{3017struct strbuf gitfile_sb = STRBUF_INIT;3018struct strbuf cfg_sb = STRBUF_INIT;3019struct strbuf rel_path = STRBUF_INIT;3020char*git_dir, *work_tree;30213022/* Prepare .git file */3023strbuf_addf(&gitfile_sb,"%s/.git", work_tree_);3024if(safe_create_leading_directories_const(gitfile_sb.buf))3025die(_("could not create directories for%s"), gitfile_sb.buf);30263027/* Prepare config file */3028strbuf_addf(&cfg_sb,"%s/config", git_dir_);3029if(safe_create_leading_directories_const(cfg_sb.buf))3030die(_("could not create directories for%s"), cfg_sb.buf);30313032 git_dir =real_pathdup(git_dir_,1);3033 work_tree =real_pathdup(work_tree_,1);30343035/* Write .git file */3036write_file(gitfile_sb.buf,"gitdir:%s",3037relative_path(git_dir, work_tree, &rel_path));3038/* Update core.worktree setting */3039git_config_set_in_file(cfg_sb.buf,"core.worktree",3040relative_path(work_tree, git_dir, &rel_path));30413042strbuf_release(&gitfile_sb);3043strbuf_release(&cfg_sb);3044strbuf_release(&rel_path);3045free(work_tree);3046free(git_dir);3047}30483049/*3050 * Migrate the git directory of the given path from old_git_dir to new_git_dir.3051 */3052voidrelocate_gitdir(const char*path,const char*old_git_dir,const char*new_git_dir)3053{3054if(rename(old_git_dir, new_git_dir) <0)3055die_errno(_("could not migrate git directory from '%s' to '%s'"),3056 old_git_dir, new_git_dir);30573058connect_work_tree_and_git_dir(path, new_git_dir);3059}