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"attr.h" 15#include"refs.h" 16#include"wildmatch.h" 17#include"pathspec.h" 18#include"utf8.h" 19#include"varint.h" 20#include"ewah/ewok.h" 21 22/* 23 * Tells read_directory_recursive how a file or directory should be treated. 24 * Values are ordered by significance, e.g. if a directory contains both 25 * excluded and untracked files, it is listed as untracked because 26 * path_untracked > path_excluded. 27 */ 28enum path_treatment { 29 path_none =0, 30 path_recurse, 31 path_excluded, 32 path_untracked 33}; 34 35/* 36 * Support data structure for our opendir/readdir/closedir wrappers 37 */ 38struct cached_dir { 39DIR*fdir; 40struct untracked_cache_dir *untracked; 41int nr_files; 42int nr_dirs; 43 44struct dirent *de; 45const char*file; 46struct untracked_cache_dir *ucd; 47}; 48 49static enum path_treatment read_directory_recursive(struct dir_struct *dir, 50struct index_state *istate,const char*path,int len, 51struct untracked_cache_dir *untracked, 52int check_only,int stop_at_first_file,const struct pathspec *pathspec); 53static intget_dtype(struct dirent *de,struct index_state *istate, 54const char*path,int len); 55 56intcount_slashes(const char*s) 57{ 58int cnt =0; 59while(*s) 60if(*s++ =='/') 61 cnt++; 62return cnt; 63} 64 65intfspathcmp(const char*a,const char*b) 66{ 67return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 68} 69 70intfspathncmp(const char*a,const char*b,size_t count) 71{ 72return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 73} 74 75intgit_fnmatch(const struct pathspec_item *item, 76const char*pattern,const char*string, 77int prefix) 78{ 79if(prefix >0) { 80if(ps_strncmp(item, pattern, string, prefix)) 81return WM_NOMATCH; 82 pattern += prefix; 83 string += prefix; 84} 85if(item->flags & PATHSPEC_ONESTAR) { 86int pattern_len =strlen(++pattern); 87int string_len =strlen(string); 88return string_len < pattern_len || 89ps_strcmp(item, pattern, 90 string + string_len - pattern_len); 91} 92if(item->magic & PATHSPEC_GLOB) 93returnwildmatch(pattern, string, 94 WM_PATHNAME | 95(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0)); 96else 97/* wildmatch has not learned no FNM_PATHNAME mode yet */ 98returnwildmatch(pattern, string, 99 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0); 100} 101 102static intfnmatch_icase_mem(const char*pattern,int patternlen, 103const char*string,int stringlen, 104int flags) 105{ 106int match_status; 107struct strbuf pat_buf = STRBUF_INIT; 108struct strbuf str_buf = STRBUF_INIT; 109const char*use_pat = pattern; 110const char*use_str = string; 111 112if(pattern[patternlen]) { 113strbuf_add(&pat_buf, pattern, patternlen); 114 use_pat = pat_buf.buf; 115} 116if(string[stringlen]) { 117strbuf_add(&str_buf, string, stringlen); 118 use_str = str_buf.buf; 119} 120 121if(ignore_case) 122 flags |= WM_CASEFOLD; 123 match_status =wildmatch(use_pat, use_str, flags); 124 125strbuf_release(&pat_buf); 126strbuf_release(&str_buf); 127 128return match_status; 129} 130 131static size_tcommon_prefix_len(const struct pathspec *pathspec) 132{ 133int n; 134size_t max =0; 135 136/* 137 * ":(icase)path" is treated as a pathspec full of 138 * wildcard. In other words, only prefix is considered common 139 * prefix. If the pathspec is abc/foo abc/bar, running in 140 * subdir xyz, the common prefix is still xyz, not xuz/abc as 141 * in non-:(icase). 142 */ 143GUARD_PATHSPEC(pathspec, 144 PATHSPEC_FROMTOP | 145 PATHSPEC_MAXDEPTH | 146 PATHSPEC_LITERAL | 147 PATHSPEC_GLOB | 148 PATHSPEC_ICASE | 149 PATHSPEC_EXCLUDE | 150 PATHSPEC_ATTR); 151 152for(n =0; n < pathspec->nr; n++) { 153size_t i =0, len =0, item_len; 154if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 155continue; 156if(pathspec->items[n].magic & PATHSPEC_ICASE) 157 item_len = pathspec->items[n].prefix; 158else 159 item_len = pathspec->items[n].nowildcard_len; 160while(i < item_len && (n ==0|| i < max)) { 161char c = pathspec->items[n].match[i]; 162if(c != pathspec->items[0].match[i]) 163break; 164if(c =='/') 165 len = i +1; 166 i++; 167} 168if(n ==0|| len < max) { 169 max = len; 170if(!max) 171break; 172} 173} 174return max; 175} 176 177/* 178 * Returns a copy of the longest leading path common among all 179 * pathspecs. 180 */ 181char*common_prefix(const struct pathspec *pathspec) 182{ 183unsigned long len =common_prefix_len(pathspec); 184 185return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 186} 187 188intfill_directory(struct dir_struct *dir, 189struct index_state *istate, 190const struct pathspec *pathspec) 191{ 192const char*prefix; 193size_t prefix_len; 194 195/* 196 * Calculate common prefix for the pathspec, and 197 * use that to optimize the directory walk 198 */ 199 prefix_len =common_prefix_len(pathspec); 200 prefix = prefix_len ? pathspec->items[0].match :""; 201 202/* Read the directory and prune it */ 203read_directory(dir, istate, prefix, prefix_len, pathspec); 204 205return prefix_len; 206} 207 208intwithin_depth(const char*name,int namelen, 209int depth,int max_depth) 210{ 211const char*cp = name, *cpe = name + namelen; 212 213while(cp < cpe) { 214if(*cp++ !='/') 215continue; 216 depth++; 217if(depth > max_depth) 218return0; 219} 220return1; 221} 222 223/* 224 * Read the contents of the blob with the given OID into a buffer. 225 * Append a trailing LF to the end if the last line doesn't have one. 226 * 227 * Returns: 228 * -1 when the OID is invalid or unknown or does not refer to a blob. 229 * 0 when the blob is empty. 230 * 1 along with { data, size } of the (possibly augmented) buffer 231 * when successful. 232 * 233 * Optionally updates the given sha1_stat with the given OID (when valid). 234 */ 235static intdo_read_blob(const struct object_id *oid, 236struct sha1_stat *sha1_stat, 237size_t*size_out, 238char**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_sha1_file(oid->hash, &type, &sz); 248if(!data || type != OBJ_BLOB) { 249free(data); 250return-1; 251} 252 253if(sha1_stat) { 254memset(&sha1_stat->stat,0,sizeof(sha1_stat->stat)); 255hashcpy(sha1_stat->sha1, oid->hash); 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, 657char**data_out, 658struct sha1_stat *sha1_stat) 659{ 660int pos, len; 661 662 len =strlen(path); 663 pos =index_name_pos(istate, path, len); 664if(pos <0) 665return-1; 666if(!ce_skip_worktree(istate->cache[pos])) 667return-1; 668 669returndo_read_blob(&istate->cache[pos]->oid, sha1_stat, size_out, data_out); 670} 671 672/* 673 * Frees memory within el which was allocated for exclude patterns and 674 * the file buffer. Does not free el itself. 675 */ 676voidclear_exclude_list(struct exclude_list *el) 677{ 678int i; 679 680for(i =0; i < el->nr; i++) 681free(el->excludes[i]); 682free(el->excludes); 683free(el->filebuf); 684 685memset(el,0,sizeof(*el)); 686} 687 688static voidtrim_trailing_spaces(char*buf) 689{ 690char*p, *last_space = NULL; 691 692for(p = buf; *p; p++) 693switch(*p) { 694case' ': 695if(!last_space) 696 last_space = p; 697break; 698case'\\': 699 p++; 700if(!*p) 701return; 702/* fallthrough */ 703default: 704 last_space = NULL; 705} 706 707if(last_space) 708*last_space ='\0'; 709} 710 711/* 712 * Given a subdirectory name and "dir" of the current directory, 713 * search the subdir in "dir" and return it, or create a new one if it 714 * does not exist in "dir". 715 * 716 * If "name" has the trailing slash, it'll be excluded in the search. 717 */ 718static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 719struct untracked_cache_dir *dir, 720const char*name,int len) 721{ 722int first, last; 723struct untracked_cache_dir *d; 724if(!dir) 725return NULL; 726if(len && name[len -1] =='/') 727 len--; 728 first =0; 729 last = dir->dirs_nr; 730while(last > first) { 731int cmp, next = (last + first) >>1; 732 d = dir->dirs[next]; 733 cmp =strncmp(name, d->name, len); 734if(!cmp &&strlen(d->name) > len) 735 cmp = -1; 736if(!cmp) 737return d; 738if(cmp <0) { 739 last = next; 740continue; 741} 742 first = next+1; 743} 744 745 uc->dir_created++; 746FLEX_ALLOC_MEM(d, name, name, len); 747 748ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 749memmove(dir->dirs + first +1, dir->dirs + first, 750(dir->dirs_nr - first) *sizeof(*dir->dirs)); 751 dir->dirs_nr++; 752 dir->dirs[first] = d; 753return d; 754} 755 756static voiddo_invalidate_gitignore(struct untracked_cache_dir *dir) 757{ 758int i; 759 dir->valid =0; 760 dir->untracked_nr =0; 761for(i =0; i < dir->dirs_nr; i++) 762do_invalidate_gitignore(dir->dirs[i]); 763} 764 765static voidinvalidate_gitignore(struct untracked_cache *uc, 766struct untracked_cache_dir *dir) 767{ 768 uc->gitignore_invalidated++; 769do_invalidate_gitignore(dir); 770} 771 772static voidinvalidate_directory(struct untracked_cache *uc, 773struct untracked_cache_dir *dir) 774{ 775int i; 776 uc->dir_invalidated++; 777 dir->valid =0; 778 dir->untracked_nr =0; 779for(i =0; i < dir->dirs_nr; i++) 780 dir->dirs[i]->recurse =0; 781} 782 783static intadd_excludes_from_buffer(char*buf,size_t size, 784const char*base,int baselen, 785struct exclude_list *el); 786 787/* 788 * Given a file with name "fname", read it (either from disk, or from 789 * an index if 'istate' is non-null), parse it and store the 790 * exclude rules in "el". 791 * 792 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 793 * stat data from disk (only valid if add_excludes returns zero). If 794 * ss_valid is non-zero, "ss" must contain good value as input. 795 */ 796static intadd_excludes(const char*fname,const char*base,int baselen, 797struct exclude_list *el, 798struct index_state *istate, 799struct sha1_stat *sha1_stat) 800{ 801struct stat st; 802int r; 803int fd; 804size_t size =0; 805char*buf; 806 807 fd =open(fname, O_RDONLY); 808if(fd <0||fstat(fd, &st) <0) { 809if(fd <0) 810warn_on_fopen_errors(fname); 811else 812close(fd); 813if(!istate) 814return-1; 815 r =read_skip_worktree_file_from_index(istate, fname, 816&size, &buf, 817 sha1_stat); 818if(r !=1) 819return r; 820}else{ 821 size =xsize_t(st.st_size); 822if(size ==0) { 823if(sha1_stat) { 824fill_stat_data(&sha1_stat->stat, &st); 825hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); 826 sha1_stat->valid =1; 827} 828close(fd); 829return0; 830} 831 buf =xmallocz(size); 832if(read_in_full(fd, buf, size) != size) { 833free(buf); 834close(fd); 835return-1; 836} 837 buf[size++] ='\n'; 838close(fd); 839if(sha1_stat) { 840int pos; 841if(sha1_stat->valid && 842!match_stat_data_racy(istate, &sha1_stat->stat, &st)) 843;/* no content change, ss->sha1 still good */ 844else if(istate && 845(pos =index_name_pos(istate, fname,strlen(fname))) >=0&& 846!ce_stage(istate->cache[pos]) && 847ce_uptodate(istate->cache[pos]) && 848!would_convert_to_git(istate, fname)) 849hashcpy(sha1_stat->sha1, 850 istate->cache[pos]->oid.hash); 851else 852hash_sha1_file(buf, size,"blob", sha1_stat->sha1); 853fill_stat_data(&sha1_stat->stat, &st); 854 sha1_stat->valid =1; 855} 856} 857 858add_excludes_from_buffer(buf, size, base, baselen, el); 859return0; 860} 861 862static intadd_excludes_from_buffer(char*buf,size_t size, 863const char*base,int baselen, 864struct exclude_list *el) 865{ 866int i, lineno =1; 867char*entry; 868 869 el->filebuf = buf; 870 871if(skip_utf8_bom(&buf, size)) 872 size -= buf - el->filebuf; 873 874 entry = buf; 875 876for(i =0; i < size; i++) { 877if(buf[i] =='\n') { 878if(entry != buf + i && entry[0] !='#') { 879 buf[i - (i && buf[i-1] =='\r')] =0; 880trim_trailing_spaces(entry); 881add_exclude(entry, base, baselen, el, lineno); 882} 883 lineno++; 884 entry = buf + i +1; 885} 886} 887return0; 888} 889 890intadd_excludes_from_file_to_list(const char*fname,const char*base, 891int baselen,struct exclude_list *el, 892struct index_state *istate) 893{ 894returnadd_excludes(fname, base, baselen, el, istate, NULL); 895} 896 897intadd_excludes_from_blob_to_list( 898struct object_id *oid, 899const char*base,int baselen, 900struct exclude_list *el) 901{ 902char*buf; 903size_t size; 904int r; 905 906 r =do_read_blob(oid, NULL, &size, &buf); 907if(r !=1) 908return r; 909 910add_excludes_from_buffer(buf, size, base, baselen, el); 911return0; 912} 913 914struct exclude_list *add_exclude_list(struct dir_struct *dir, 915int group_type,const char*src) 916{ 917struct exclude_list *el; 918struct exclude_list_group *group; 919 920 group = &dir->exclude_list_group[group_type]; 921ALLOC_GROW(group->el, group->nr +1, group->alloc); 922 el = &group->el[group->nr++]; 923memset(el,0,sizeof(*el)); 924 el->src = src; 925return el; 926} 927 928/* 929 * Used to set up core.excludesfile and .git/info/exclude lists. 930 */ 931static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 932struct sha1_stat *sha1_stat) 933{ 934struct exclude_list *el; 935/* 936 * catch setup_standard_excludes() that's called before 937 * dir->untracked is assigned. That function behaves 938 * differently when dir->untracked is non-NULL. 939 */ 940if(!dir->untracked) 941 dir->unmanaged_exclude_files++; 942 el =add_exclude_list(dir, EXC_FILE, fname); 943if(add_excludes(fname,"",0, el, NULL, sha1_stat) <0) 944die("cannot use%sas an exclude file", fname); 945} 946 947voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 948{ 949 dir->unmanaged_exclude_files++;/* see validate_untracked_cache() */ 950add_excludes_from_file_1(dir, fname, NULL); 951} 952 953intmatch_basename(const char*basename,int basenamelen, 954const char*pattern,int prefix,int patternlen, 955unsigned flags) 956{ 957if(prefix == patternlen) { 958if(patternlen == basenamelen && 959!fspathncmp(pattern, basename, basenamelen)) 960return1; 961}else if(flags & EXC_FLAG_ENDSWITH) { 962/* "*literal" matching against "fooliteral" */ 963if(patternlen -1<= basenamelen && 964!fspathncmp(pattern +1, 965 basename + basenamelen - (patternlen -1), 966 patternlen -1)) 967return1; 968}else{ 969if(fnmatch_icase_mem(pattern, patternlen, 970 basename, basenamelen, 9710) ==0) 972return1; 973} 974return0; 975} 976 977intmatch_pathname(const char*pathname,int pathlen, 978const char*base,int baselen, 979const char*pattern,int prefix,int patternlen, 980unsigned flags) 981{ 982const char*name; 983int namelen; 984 985/* 986 * match with FNM_PATHNAME; the pattern has base implicitly 987 * in front of it. 988 */ 989if(*pattern =='/') { 990 pattern++; 991 patternlen--; 992 prefix--; 993} 994 995/* 996 * baselen does not count the trailing slash. base[] may or 997 * may not end with a trailing slash though. 998 */ 999if(pathlen < baselen +1||1000(baselen && pathname[baselen] !='/') ||1001fspathncmp(pathname, base, baselen))1002return0;10031004 namelen = baselen ? pathlen - baselen -1: pathlen;1005 name = pathname + pathlen - namelen;10061007if(prefix) {1008/*1009 * if the non-wildcard part is longer than the1010 * remaining pathname, surely it cannot match.1011 */1012if(prefix > namelen)1013return0;10141015if(fspathncmp(pattern, name, prefix))1016return0;1017 pattern += prefix;1018 patternlen -= prefix;1019 name += prefix;1020 namelen -= prefix;10211022/*1023 * If the whole pattern did not have a wildcard,1024 * then our prefix match is all we need; we1025 * do not need to call fnmatch at all.1026 */1027if(!patternlen && !namelen)1028return1;1029}10301031returnfnmatch_icase_mem(pattern, patternlen,1032 name, namelen,1033 WM_PATHNAME) ==0;1034}10351036/*1037 * Scan the given exclude list in reverse to see whether pathname1038 * should be ignored. The first match (i.e. the last on the list), if1039 * any, determines the fate. Returns the exclude_list element which1040 * matched, or NULL for undecided.1041 */1042static struct exclude *last_exclude_matching_from_list(const char*pathname,1043int pathlen,1044const char*basename,1045int*dtype,1046struct exclude_list *el,1047struct index_state *istate)1048{1049struct exclude *exc = NULL;/* undecided */1050int i;10511052if(!el->nr)1053return NULL;/* undefined */10541055for(i = el->nr -1;0<= i; i--) {1056struct exclude *x = el->excludes[i];1057const char*exclude = x->pattern;1058int prefix = x->nowildcardlen;10591060if(x->flags & EXC_FLAG_MUSTBEDIR) {1061if(*dtype == DT_UNKNOWN)1062*dtype =get_dtype(NULL, istate, pathname, pathlen);1063if(*dtype != DT_DIR)1064continue;1065}10661067if(x->flags & EXC_FLAG_NODIR) {1068if(match_basename(basename,1069 pathlen - (basename - pathname),1070 exclude, prefix, x->patternlen,1071 x->flags)) {1072 exc = x;1073break;1074}1075continue;1076}10771078assert(x->baselen ==0|| x->base[x->baselen -1] =='/');1079if(match_pathname(pathname, pathlen,1080 x->base, x->baselen ? x->baselen -1:0,1081 exclude, prefix, x->patternlen, x->flags)) {1082 exc = x;1083break;1084}1085}1086return exc;1087}10881089/*1090 * Scan the list and let the last match determine the fate.1091 * Return 1 for exclude, 0 for include and -1 for undecided.1092 */1093intis_excluded_from_list(const char*pathname,1094int pathlen,const char*basename,int*dtype,1095struct exclude_list *el,struct index_state *istate)1096{1097struct exclude *exclude;1098 exclude =last_exclude_matching_from_list(pathname, pathlen, basename,1099 dtype, el, istate);1100if(exclude)1101return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1102return-1;/* undecided */1103}11041105static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,1106struct index_state *istate,1107const char*pathname,int pathlen,const char*basename,1108int*dtype_p)1109{1110int i, j;1111struct exclude_list_group *group;1112struct exclude *exclude;1113for(i = EXC_CMDL; i <= EXC_FILE; i++) {1114 group = &dir->exclude_list_group[i];1115for(j = group->nr -1; j >=0; j--) {1116 exclude =last_exclude_matching_from_list(1117 pathname, pathlen, basename, dtype_p,1118&group->el[j], istate);1119if(exclude)1120return exclude;1121}1122}1123return NULL;1124}11251126/*1127 * Loads the per-directory exclude list for the substring of base1128 * which has a char length of baselen.1129 */1130static voidprep_exclude(struct dir_struct *dir,1131struct index_state *istate,1132const char*base,int baselen)1133{1134struct exclude_list_group *group;1135struct exclude_list *el;1136struct exclude_stack *stk = NULL;1137struct untracked_cache_dir *untracked;1138int current;11391140 group = &dir->exclude_list_group[EXC_DIRS];11411142/*1143 * Pop the exclude lists from the EXCL_DIRS exclude_list_group1144 * which originate from directories not in the prefix of the1145 * path being checked.1146 */1147while((stk = dir->exclude_stack) != NULL) {1148if(stk->baselen <= baselen &&1149!strncmp(dir->basebuf.buf, base, stk->baselen))1150break;1151 el = &group->el[dir->exclude_stack->exclude_ix];1152 dir->exclude_stack = stk->prev;1153 dir->exclude = NULL;1154free((char*)el->src);/* see strbuf_detach() below */1155clear_exclude_list(el);1156free(stk);1157 group->nr--;1158}11591160/* Skip traversing into sub directories if the parent is excluded */1161if(dir->exclude)1162return;11631164/*1165 * Lazy initialization. All call sites currently just1166 * memset(dir, 0, sizeof(*dir)) before use. Changing all of1167 * them seems lots of work for little benefit.1168 */1169if(!dir->basebuf.buf)1170strbuf_init(&dir->basebuf, PATH_MAX);11711172/* Read from the parent directories and push them down. */1173 current = stk ? stk->baselen : -1;1174strbuf_setlen(&dir->basebuf, current <0?0: current);1175if(dir->untracked)1176 untracked = stk ? stk->ucd : dir->untracked->root;1177else1178 untracked = NULL;11791180while(current < baselen) {1181const char*cp;1182struct sha1_stat sha1_stat;11831184 stk =xcalloc(1,sizeof(*stk));1185if(current <0) {1186 cp = base;1187 current =0;1188}else{1189 cp =strchr(base + current +1,'/');1190if(!cp)1191die("oops in prep_exclude");1192 cp++;1193 untracked =1194lookup_untracked(dir->untracked, untracked,1195 base + current,1196 cp - base - current);1197}1198 stk->prev = dir->exclude_stack;1199 stk->baselen = cp - base;1200 stk->exclude_ix = group->nr;1201 stk->ucd = untracked;1202 el =add_exclude_list(dir, EXC_DIRS, NULL);1203strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1204assert(stk->baselen == dir->basebuf.len);12051206/* Abort if the directory is excluded */1207if(stk->baselen) {1208int dt = DT_DIR;1209 dir->basebuf.buf[stk->baselen -1] =0;1210 dir->exclude =last_exclude_matching_from_lists(dir,1211 istate,1212 dir->basebuf.buf, stk->baselen -1,1213 dir->basebuf.buf + current, &dt);1214 dir->basebuf.buf[stk->baselen -1] ='/';1215if(dir->exclude &&1216 dir->exclude->flags & EXC_FLAG_NEGATIVE)1217 dir->exclude = NULL;1218if(dir->exclude) {1219 dir->exclude_stack = stk;1220return;1221}1222}12231224/* Try to read per-directory file */1225hashclr(sha1_stat.sha1);1226 sha1_stat.valid =0;1227if(dir->exclude_per_dir &&1228/*1229 * If we know that no files have been added in1230 * this directory (i.e. valid_cached_dir() has1231 * been executed and set untracked->valid) ..1232 */1233(!untracked || !untracked->valid ||1234/*1235 * .. and .gitignore does not exist before1236 * (i.e. null exclude_sha1). Then we can skip1237 * loading .gitignore, which would result in1238 * ENOENT anyway.1239 */1240!is_null_sha1(untracked->exclude_sha1))) {1241/*1242 * dir->basebuf gets reused by the traversal, but we1243 * need fname to remain unchanged to ensure the src1244 * member of each struct exclude correctly1245 * back-references its source file. Other invocations1246 * of add_exclude_list provide stable strings, so we1247 * strbuf_detach() and free() here in the caller.1248 */1249struct strbuf sb = STRBUF_INIT;1250strbuf_addbuf(&sb, &dir->basebuf);1251strbuf_addstr(&sb, dir->exclude_per_dir);1252 el->src =strbuf_detach(&sb, NULL);1253add_excludes(el->src, el->src, stk->baselen, el, istate,1254 untracked ? &sha1_stat : NULL);1255}1256/*1257 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1258 * will first be called in valid_cached_dir() then maybe many1259 * times more in last_exclude_matching(). When the cache is1260 * used, last_exclude_matching() will not be called and1261 * reading .gitignore content will be a waste.1262 *1263 * So when it's called by valid_cached_dir() and we can get1264 * .gitignore SHA-1 from the index (i.e. .gitignore is not1265 * modified on work tree), we could delay reading the1266 * .gitignore content until we absolutely need it in1267 * last_exclude_matching(). Be careful about ignore rule1268 * order, though, if you do that.1269 */1270if(untracked &&1271hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {1272invalidate_gitignore(dir->untracked, untracked);1273hashcpy(untracked->exclude_sha1, sha1_stat.sha1);1274}1275 dir->exclude_stack = stk;1276 current = stk->baselen;1277}1278strbuf_setlen(&dir->basebuf, baselen);1279}12801281/*1282 * Loads the exclude lists for the directory containing pathname, then1283 * scans all exclude lists to determine whether pathname is excluded.1284 * Returns the exclude_list element which matched, or NULL for1285 * undecided.1286 */1287struct exclude *last_exclude_matching(struct dir_struct *dir,1288struct index_state *istate,1289const char*pathname,1290int*dtype_p)1291{1292int pathlen =strlen(pathname);1293const char*basename =strrchr(pathname,'/');1294 basename = (basename) ? basename+1: pathname;12951296prep_exclude(dir, istate, pathname, basename-pathname);12971298if(dir->exclude)1299return dir->exclude;13001301returnlast_exclude_matching_from_lists(dir, istate, pathname, pathlen,1302 basename, dtype_p);1303}13041305/*1306 * Loads the exclude lists for the directory containing pathname, then1307 * scans all exclude lists to determine whether pathname is excluded.1308 * Returns 1 if true, otherwise 0.1309 */1310intis_excluded(struct dir_struct *dir,struct index_state *istate,1311const char*pathname,int*dtype_p)1312{1313struct exclude *exclude =1314last_exclude_matching(dir, istate, pathname, dtype_p);1315if(exclude)1316return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1317return0;1318}13191320static struct dir_entry *dir_entry_new(const char*pathname,int len)1321{1322struct dir_entry *ent;13231324FLEX_ALLOC_MEM(ent, name, pathname, len);1325 ent->len = len;1326return ent;1327}13281329static struct dir_entry *dir_add_name(struct dir_struct *dir,1330struct index_state *istate,1331const char*pathname,int len)1332{1333if(index_file_exists(istate, pathname, len, ignore_case))1334return NULL;13351336ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1337return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1338}13391340struct dir_entry *dir_add_ignored(struct dir_struct *dir,1341struct index_state *istate,1342const char*pathname,int len)1343{1344if(!index_name_is_other(istate, pathname, len))1345return NULL;13461347ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1348return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1349}13501351enum exist_status {1352 index_nonexistent =0,1353 index_directory,1354 index_gitdir1355};13561357/*1358 * Do not use the alphabetically sorted index to look up1359 * the directory name; instead, use the case insensitive1360 * directory hash.1361 */1362static enum exist_status directory_exists_in_index_icase(struct index_state *istate,1363const char*dirname,int len)1364{1365struct cache_entry *ce;13661367if(index_dir_exists(istate, dirname, len))1368return index_directory;13691370 ce =index_file_exists(istate, dirname, len, ignore_case);1371if(ce &&S_ISGITLINK(ce->ce_mode))1372return index_gitdir;13731374return index_nonexistent;1375}13761377/*1378 * The index sorts alphabetically by entry name, which1379 * means that a gitlink sorts as '\0' at the end, while1380 * a directory (which is defined not as an entry, but as1381 * the files it contains) will sort with the '/' at the1382 * end.1383 */1384static enum exist_status directory_exists_in_index(struct index_state *istate,1385const char*dirname,int len)1386{1387int pos;13881389if(ignore_case)1390returndirectory_exists_in_index_icase(istate, dirname, len);13911392 pos =index_name_pos(istate, dirname, len);1393if(pos <0)1394 pos = -pos-1;1395while(pos < istate->cache_nr) {1396const struct cache_entry *ce = istate->cache[pos++];1397unsigned char endchar;13981399if(strncmp(ce->name, dirname, len))1400break;1401 endchar = ce->name[len];1402if(endchar >'/')1403break;1404if(endchar =='/')1405return index_directory;1406if(!endchar &&S_ISGITLINK(ce->ce_mode))1407return index_gitdir;1408}1409return index_nonexistent;1410}14111412/*1413 * When we find a directory when traversing the filesystem, we1414 * have three distinct cases:1415 *1416 * - ignore it1417 * - see it as a directory1418 * - recurse into it1419 *1420 * and which one we choose depends on a combination of existing1421 * git index contents and the flags passed into the directory1422 * traversal routine.1423 *1424 * Case 1: If we *already* have entries in the index under that1425 * directory name, we always recurse into the directory to see1426 * all the files.1427 *1428 * Case 2: If we *already* have that directory name as a gitlink,1429 * we always continue to see it as a gitlink, regardless of whether1430 * there is an actual git directory there or not (it might not1431 * be checked out as a subproject!)1432 *1433 * Case 3: if we didn't have it in the index previously, we1434 * have a few sub-cases:1435 *1436 * (a) if "show_other_directories" is true, we show it as1437 * just a directory, unless "hide_empty_directories" is1438 * also true, in which case we need to check if it contains any1439 * untracked and / or ignored files.1440 * (b) if it looks like a git directory, and we don't have1441 * 'no_gitlinks' set we treat it as a gitlink, and show it1442 * as a directory.1443 * (c) otherwise, we recurse into it.1444 */1445static enum path_treatment treat_directory(struct dir_struct *dir,1446struct index_state *istate,1447struct untracked_cache_dir *untracked,1448const char*dirname,int len,int baselen,int exclude,1449const struct pathspec *pathspec)1450{1451/* The "len-1" is to strip the final '/' */1452switch(directory_exists_in_index(istate, dirname, len-1)) {1453case index_directory:1454return path_recurse;14551456case index_gitdir:1457return path_none;14581459case index_nonexistent:1460if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1461break;1462if(!(dir->flags & DIR_NO_GITLINKS)) {1463unsigned char sha1[20];1464if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1465return path_untracked;1466}1467return path_recurse;1468}14691470/* This is the "show_other_directories" case */14711472if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1473return exclude ? path_excluded : path_untracked;14741475 untracked =lookup_untracked(dir->untracked, untracked,1476 dirname + baselen, len - baselen);14771478/*1479 * If this is an excluded directory, then we only need to check if1480 * the directory contains any files.1481 */1482returnread_directory_recursive(dir, istate, dirname, len,1483 untracked,1, exclude, pathspec);1484}14851486/*1487 * This is an inexact early pruning of any recursive directory1488 * reading - if the path cannot possibly be in the pathspec,1489 * return true, and we'll skip it early.1490 */1491static intsimplify_away(const char*path,int pathlen,1492const struct pathspec *pathspec)1493{1494int i;14951496if(!pathspec || !pathspec->nr)1497return0;14981499GUARD_PATHSPEC(pathspec,1500 PATHSPEC_FROMTOP |1501 PATHSPEC_MAXDEPTH |1502 PATHSPEC_LITERAL |1503 PATHSPEC_GLOB |1504 PATHSPEC_ICASE |1505 PATHSPEC_EXCLUDE |1506 PATHSPEC_ATTR);15071508for(i =0; i < pathspec->nr; i++) {1509const struct pathspec_item *item = &pathspec->items[i];1510int len = item->nowildcard_len;15111512if(len > pathlen)1513 len = pathlen;1514if(!ps_strncmp(item, item->match, path, len))1515return0;1516}15171518return1;1519}15201521/*1522 * This function tells us whether an excluded path matches a1523 * list of "interesting" pathspecs. That is, whether a path matched1524 * by any of the pathspecs could possibly be ignored by excluding1525 * the specified path. This can happen if:1526 *1527 * 1. the path is mentioned explicitly in the pathspec1528 *1529 * 2. the path is a directory prefix of some element in the1530 * pathspec1531 */1532static intexclude_matches_pathspec(const char*path,int pathlen,1533const struct pathspec *pathspec)1534{1535int i;15361537if(!pathspec || !pathspec->nr)1538return0;15391540GUARD_PATHSPEC(pathspec,1541 PATHSPEC_FROMTOP |1542 PATHSPEC_MAXDEPTH |1543 PATHSPEC_LITERAL |1544 PATHSPEC_GLOB |1545 PATHSPEC_ICASE |1546 PATHSPEC_EXCLUDE);15471548for(i =0; i < pathspec->nr; i++) {1549const struct pathspec_item *item = &pathspec->items[i];1550int len = item->nowildcard_len;15511552if(len == pathlen &&1553!ps_strncmp(item, item->match, path, pathlen))1554return1;1555if(len > pathlen &&1556 item->match[pathlen] =='/'&&1557!ps_strncmp(item, item->match, path, pathlen))1558return1;1559}1560return0;1561}15621563static intget_index_dtype(struct index_state *istate,1564const char*path,int len)1565{1566int pos;1567const struct cache_entry *ce;15681569 ce =index_file_exists(istate, path, len,0);1570if(ce) {1571if(!ce_uptodate(ce))1572return DT_UNKNOWN;1573if(S_ISGITLINK(ce->ce_mode))1574return DT_DIR;1575/*1576 * Nobody actually cares about the1577 * difference between DT_LNK and DT_REG1578 */1579return DT_REG;1580}15811582/* Try to look it up as a directory */1583 pos =index_name_pos(istate, path, len);1584if(pos >=0)1585return DT_UNKNOWN;1586 pos = -pos-1;1587while(pos < istate->cache_nr) {1588 ce = istate->cache[pos++];1589if(strncmp(ce->name, path, len))1590break;1591if(ce->name[len] >'/')1592break;1593if(ce->name[len] <'/')1594continue;1595if(!ce_uptodate(ce))1596break;/* continue? */1597return DT_DIR;1598}1599return DT_UNKNOWN;1600}16011602static intget_dtype(struct dirent *de,struct index_state *istate,1603const char*path,int len)1604{1605int dtype = de ?DTYPE(de) : DT_UNKNOWN;1606struct stat st;16071608if(dtype != DT_UNKNOWN)1609return dtype;1610 dtype =get_index_dtype(istate, path, len);1611if(dtype != DT_UNKNOWN)1612return dtype;1613if(lstat(path, &st))1614return dtype;1615if(S_ISREG(st.st_mode))1616return DT_REG;1617if(S_ISDIR(st.st_mode))1618return DT_DIR;1619if(S_ISLNK(st.st_mode))1620return DT_LNK;1621return dtype;1622}16231624static enum path_treatment treat_one_path(struct dir_struct *dir,1625struct untracked_cache_dir *untracked,1626struct index_state *istate,1627struct strbuf *path,1628int baselen,1629const struct pathspec *pathspec,1630int dtype,struct dirent *de)1631{1632int exclude;1633int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);16341635if(dtype == DT_UNKNOWN)1636 dtype =get_dtype(de, istate, path->buf, path->len);16371638/* Always exclude indexed files */1639if(dtype != DT_DIR && has_path_in_index)1640return path_none;16411642/*1643 * When we are looking at a directory P in the working tree,1644 * there are three cases:1645 *1646 * (1) P exists in the index. Everything inside the directory P in1647 * the working tree needs to go when P is checked out from the1648 * index.1649 *1650 * (2) P does not exist in the index, but there is P/Q in the index.1651 * We know P will stay a directory when we check out the contents1652 * of the index, but we do not know yet if there is a directory1653 * P/Q in the working tree to be killed, so we need to recurse.1654 *1655 * (3) P does not exist in the index, and there is no P/Q in the index1656 * to require P to be a directory, either. Only in this case, we1657 * know that everything inside P will not be killed without1658 * recursing.1659 */1660if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1661(dtype == DT_DIR) &&1662!has_path_in_index &&1663(directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))1664return path_none;16651666 exclude =is_excluded(dir, istate, path->buf, &dtype);16671668/*1669 * Excluded? If we don't explicitly want to show1670 * ignored files, ignore it1671 */1672if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1673return path_excluded;16741675switch(dtype) {1676default:1677return path_none;1678case DT_DIR:1679strbuf_addch(path,'/');1680returntreat_directory(dir, istate, untracked, path->buf, path->len,1681 baselen, exclude, pathspec);1682case DT_REG:1683case DT_LNK:1684return exclude ? path_excluded : path_untracked;1685}1686}16871688static enum path_treatment treat_path_fast(struct dir_struct *dir,1689struct untracked_cache_dir *untracked,1690struct cached_dir *cdir,1691struct index_state *istate,1692struct strbuf *path,1693int baselen,1694const struct pathspec *pathspec)1695{1696strbuf_setlen(path, baselen);1697if(!cdir->ucd) {1698strbuf_addstr(path, cdir->file);1699return path_untracked;1700}1701strbuf_addstr(path, cdir->ucd->name);1702/* treat_one_path() does this before it calls treat_directory() */1703strbuf_complete(path,'/');1704if(cdir->ucd->check_only)1705/*1706 * check_only is set as a result of treat_directory() getting1707 * to its bottom. Verify again the same set of directories1708 * with check_only set.1709 */1710returnread_directory_recursive(dir, istate, path->buf, path->len,1711 cdir->ucd,1,0, pathspec);1712/*1713 * We get path_recurse in the first run when1714 * directory_exists_in_index() returns index_nonexistent. We1715 * are sure that new changes in the index does not impact the1716 * outcome. Return now.1717 */1718return path_recurse;1719}17201721static enum path_treatment treat_path(struct dir_struct *dir,1722struct untracked_cache_dir *untracked,1723struct cached_dir *cdir,1724struct index_state *istate,1725struct strbuf *path,1726int baselen,1727const struct pathspec *pathspec)1728{1729int dtype;1730struct dirent *de = cdir->de;17311732if(!de)1733returntreat_path_fast(dir, untracked, cdir, istate, path,1734 baselen, pathspec);1735if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1736return path_none;1737strbuf_setlen(path, baselen);1738strbuf_addstr(path, de->d_name);1739if(simplify_away(path->buf, path->len, pathspec))1740return path_none;17411742 dtype =DTYPE(de);1743returntreat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);1744}17451746static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1747{1748if(!dir)1749return;1750ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1751 dir->untracked_alloc);1752 dir->untracked[dir->untracked_nr++] =xstrdup(name);1753}17541755static intvalid_cached_dir(struct dir_struct *dir,1756struct untracked_cache_dir *untracked,1757struct index_state *istate,1758struct strbuf *path,1759int check_only)1760{1761struct stat st;17621763if(!untracked)1764return0;17651766if(stat(path->len ? path->buf :".", &st)) {1767invalidate_directory(dir->untracked, untracked);1768memset(&untracked->stat_data,0,sizeof(untracked->stat_data));1769return0;1770}1771if(!untracked->valid ||1772match_stat_data_racy(istate, &untracked->stat_data, &st)) {1773if(untracked->valid)1774invalidate_directory(dir->untracked, untracked);1775fill_stat_data(&untracked->stat_data, &st);1776return0;1777}17781779if(untracked->check_only != !!check_only) {1780invalidate_directory(dir->untracked, untracked);1781return0;1782}17831784/*1785 * prep_exclude will be called eventually on this directory,1786 * but it's called much later in last_exclude_matching(). We1787 * need it now to determine the validity of the cache for this1788 * path. The next calls will be nearly no-op, the way1789 * prep_exclude() is designed.1790 */1791if(path->len && path->buf[path->len -1] !='/') {1792strbuf_addch(path,'/');1793prep_exclude(dir, istate, path->buf, path->len);1794strbuf_setlen(path, path->len -1);1795}else1796prep_exclude(dir, istate, path->buf, path->len);17971798/* hopefully prep_exclude() haven't invalidated this entry... */1799return untracked->valid;1800}18011802static intopen_cached_dir(struct cached_dir *cdir,1803struct dir_struct *dir,1804struct untracked_cache_dir *untracked,1805struct index_state *istate,1806struct strbuf *path,1807int check_only)1808{1809memset(cdir,0,sizeof(*cdir));1810 cdir->untracked = untracked;1811if(valid_cached_dir(dir, untracked, istate, path, check_only))1812return0;1813 cdir->fdir =opendir(path->len ? path->buf :".");1814if(dir->untracked)1815 dir->untracked->dir_opened++;1816if(!cdir->fdir)1817return-1;1818return0;1819}18201821static intread_cached_dir(struct cached_dir *cdir)1822{1823if(cdir->fdir) {1824 cdir->de =readdir(cdir->fdir);1825if(!cdir->de)1826return-1;1827return0;1828}1829while(cdir->nr_dirs < cdir->untracked->dirs_nr) {1830struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1831if(!d->recurse) {1832 cdir->nr_dirs++;1833continue;1834}1835 cdir->ucd = d;1836 cdir->nr_dirs++;1837return0;1838}1839 cdir->ucd = NULL;1840if(cdir->nr_files < cdir->untracked->untracked_nr) {1841struct untracked_cache_dir *d = cdir->untracked;1842 cdir->file = d->untracked[cdir->nr_files++];1843return0;1844}1845return-1;1846}18471848static voidclose_cached_dir(struct cached_dir *cdir)1849{1850if(cdir->fdir)1851closedir(cdir->fdir);1852/*1853 * We have gone through this directory and found no untracked1854 * entries. Mark it valid.1855 */1856if(cdir->untracked) {1857 cdir->untracked->valid =1;1858 cdir->untracked->recurse =1;1859}1860}18611862/*1863 * Read a directory tree. We currently ignore anything but1864 * directories, regular files and symlinks. That's because git1865 * doesn't handle them at all yet. Maybe that will change some1866 * day.1867 *1868 * Also, we ignore the name ".git" (even if it is not a directory).1869 * That likely will not change.1870 *1871 * If 'stop_at_first_file' is specified, 'path_excluded' is returned1872 * to signal that a file was found. This is the least significant value that1873 * indicates that a file was encountered that does not depend on the order of1874 * whether an untracked or exluded path was encountered first.1875 *1876 * Returns the most significant path_treatment value encountered in the scan.1877 * If 'stop_at_first_file' is specified, `path_excluded` is the most1878 * significant path_treatment value that will be returned.1879 */18801881static enum path_treatment read_directory_recursive(struct dir_struct *dir,1882struct index_state *istate,const char*base,int baselen,1883struct untracked_cache_dir *untracked,int check_only,1884int stop_at_first_file,const struct pathspec *pathspec)1885{1886struct cached_dir cdir;1887enum path_treatment state, subdir_state, dir_state = path_none;1888struct strbuf path = STRBUF_INIT;18891890strbuf_add(&path, base, baselen);18911892if(open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))1893goto out;18941895if(untracked)1896 untracked->check_only = !!check_only;18971898while(!read_cached_dir(&cdir)) {1899/* check how the file or directory should be treated */1900 state =treat_path(dir, untracked, &cdir, istate, &path,1901 baselen, pathspec);19021903if(state > dir_state)1904 dir_state = state;19051906/* recurse into subdir if instructed by treat_path */1907if((state == path_recurse) ||1908((state == path_untracked) &&1909(dir->flags & DIR_SHOW_IGNORED_TOO) &&1910(get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {1911struct untracked_cache_dir *ud;1912 ud =lookup_untracked(dir->untracked, untracked,1913 path.buf + baselen,1914 path.len - baselen);1915 subdir_state =1916read_directory_recursive(dir, istate, path.buf,1917 path.len, ud,1918 check_only, stop_at_first_file, pathspec);1919if(subdir_state > dir_state)1920 dir_state = subdir_state;1921}19221923if(check_only) {1924if(stop_at_first_file) {1925/*1926 * If stopping at first file, then1927 * signal that a file was found by1928 * returning `path_excluded`. This is1929 * to return a consistent value1930 * regardless of whether an ignored or1931 * excluded file happened to be1932 * encountered 1st.1933 *1934 * In current usage, the1935 * `stop_at_first_file` is passed when1936 * an ancestor directory has matched1937 * an exclude pattern, so any found1938 * files will be excluded.1939 */1940if(dir_state >= path_excluded) {1941 dir_state = path_excluded;1942break;1943}1944}19451946/* abort early if maximum state has been reached */1947if(dir_state == path_untracked) {1948if(cdir.fdir)1949add_untracked(untracked, path.buf + baselen);1950break;1951}1952/* skip the dir_add_* part */1953continue;1954}19551956/* add the path to the appropriate result list */1957switch(state) {1958case path_excluded:1959if(dir->flags & DIR_SHOW_IGNORED)1960dir_add_name(dir, istate, path.buf, path.len);1961else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1962((dir->flags & DIR_COLLECT_IGNORED) &&1963exclude_matches_pathspec(path.buf, path.len,1964 pathspec)))1965dir_add_ignored(dir, istate, path.buf, path.len);1966break;19671968case path_untracked:1969if(dir->flags & DIR_SHOW_IGNORED)1970break;1971dir_add_name(dir, istate, path.buf, path.len);1972if(cdir.fdir)1973add_untracked(untracked, path.buf + baselen);1974break;19751976default:1977break;1978}1979}1980close_cached_dir(&cdir);1981 out:1982strbuf_release(&path);19831984return dir_state;1985}19861987intcmp_dir_entry(const void*p1,const void*p2)1988{1989const struct dir_entry *e1 = *(const struct dir_entry **)p1;1990const struct dir_entry *e2 = *(const struct dir_entry **)p2;19911992returnname_compare(e1->name, e1->len, e2->name, e2->len);1993}19941995/* check if *out lexically strictly contains *in */1996intcheck_dir_entry_contains(const struct dir_entry *out,const struct dir_entry *in)1997{1998return(out->len < in->len) &&1999(out->name[out->len -1] =='/') &&2000!memcmp(out->name, in->name, out->len);2001}20022003static inttreat_leading_path(struct dir_struct *dir,2004struct index_state *istate,2005const char*path,int len,2006const struct pathspec *pathspec)2007{2008struct strbuf sb = STRBUF_INIT;2009int baselen, rc =0;2010const char*cp;2011int old_flags = dir->flags;20122013while(len && path[len -1] =='/')2014 len--;2015if(!len)2016return1;2017 baselen =0;2018 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;2019while(1) {2020 cp = path + baselen + !!baselen;2021 cp =memchr(cp,'/', path + len - cp);2022if(!cp)2023 baselen = len;2024else2025 baselen = cp - path;2026strbuf_setlen(&sb,0);2027strbuf_add(&sb, path, baselen);2028if(!is_directory(sb.buf))2029break;2030if(simplify_away(sb.buf, sb.len, pathspec))2031break;2032if(treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,2033 DT_DIR, NULL) == path_none)2034break;/* do not recurse into it */2035if(len <= baselen) {2036 rc =1;2037break;/* finished checking */2038}2039}2040strbuf_release(&sb);2041 dir->flags = old_flags;2042return rc;2043}20442045static const char*get_ident_string(void)2046{2047static struct strbuf sb = STRBUF_INIT;2048struct utsname uts;20492050if(sb.len)2051return sb.buf;2052if(uname(&uts) <0)2053die_errno(_("failed to get kernel name and information"));2054strbuf_addf(&sb,"Location%s, system%s",get_git_work_tree(),2055 uts.sysname);2056return sb.buf;2057}20582059static intident_in_untracked(const struct untracked_cache *uc)2060{2061/*2062 * Previous git versions may have saved many NUL separated2063 * strings in the "ident" field, but it is insane to manage2064 * many locations, so just take care of the first one.2065 */20662067return!strcmp(uc->ident.buf,get_ident_string());2068}20692070static voidset_untracked_ident(struct untracked_cache *uc)2071{2072strbuf_reset(&uc->ident);2073strbuf_addstr(&uc->ident,get_ident_string());20742075/*2076 * This strbuf used to contain a list of NUL separated2077 * strings, so save NUL too for backward compatibility.2078 */2079strbuf_addch(&uc->ident,0);2080}20812082static voidnew_untracked_cache(struct index_state *istate)2083{2084struct untracked_cache *uc =xcalloc(1,sizeof(*uc));2085strbuf_init(&uc->ident,100);2086 uc->exclude_per_dir =".gitignore";2087/* should be the same flags used by git-status */2088 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;2089set_untracked_ident(uc);2090 istate->untracked = uc;2091 istate->cache_changed |= UNTRACKED_CHANGED;2092}20932094voidadd_untracked_cache(struct index_state *istate)2095{2096if(!istate->untracked) {2097new_untracked_cache(istate);2098}else{2099if(!ident_in_untracked(istate->untracked)) {2100free_untracked_cache(istate->untracked);2101new_untracked_cache(istate);2102}2103}2104}21052106voidremove_untracked_cache(struct index_state *istate)2107{2108if(istate->untracked) {2109free_untracked_cache(istate->untracked);2110 istate->untracked = NULL;2111 istate->cache_changed |= UNTRACKED_CHANGED;2112}2113}21142115static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,2116int base_len,2117const struct pathspec *pathspec)2118{2119struct untracked_cache_dir *root;21202121if(!dir->untracked ||getenv("GIT_DISABLE_UNTRACKED_CACHE"))2122return NULL;21232124/*2125 * We only support $GIT_DIR/info/exclude and core.excludesfile2126 * as the global ignore rule files. Any other additions2127 * (e.g. from command line) invalidate the cache. This2128 * condition also catches running setup_standard_excludes()2129 * before setting dir->untracked!2130 */2131if(dir->unmanaged_exclude_files)2132return NULL;21332134/*2135 * Optimize for the main use case only: whole-tree git2136 * status. More work involved in treat_leading_path() if we2137 * use cache on just a subset of the worktree. pathspec2138 * support could make the matter even worse.2139 */2140if(base_len || (pathspec && pathspec->nr))2141return NULL;21422143/* Different set of flags may produce different results */2144if(dir->flags != dir->untracked->dir_flags ||2145/*2146 * See treat_directory(), case index_nonexistent. Without2147 * this flag, we may need to also cache .git file content2148 * for the resolve_gitlink_ref() call, which we don't.2149 */2150!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||2151/* We don't support collecting ignore files */2152(dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |2153 DIR_COLLECT_IGNORED)))2154return NULL;21552156/*2157 * If we use .gitignore in the cache and now you change it to2158 * .gitexclude, everything will go wrong.2159 */2160if(dir->exclude_per_dir != dir->untracked->exclude_per_dir &&2161strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))2162return NULL;21632164/*2165 * EXC_CMDL is not considered in the cache. If people set it,2166 * skip the cache.2167 */2168if(dir->exclude_list_group[EXC_CMDL].nr)2169return NULL;21702171if(!ident_in_untracked(dir->untracked)) {2172warning(_("Untracked cache is disabled on this system or location."));2173return NULL;2174}21752176if(!dir->untracked->root) {2177const int len =sizeof(*dir->untracked->root);2178 dir->untracked->root =xmalloc(len);2179memset(dir->untracked->root,0, len);2180}21812182/* Validate $GIT_DIR/info/exclude and core.excludesfile */2183 root = dir->untracked->root;2184if(hashcmp(dir->ss_info_exclude.sha1,2185 dir->untracked->ss_info_exclude.sha1)) {2186invalidate_gitignore(dir->untracked, root);2187 dir->untracked->ss_info_exclude = dir->ss_info_exclude;2188}2189if(hashcmp(dir->ss_excludes_file.sha1,2190 dir->untracked->ss_excludes_file.sha1)) {2191invalidate_gitignore(dir->untracked, root);2192 dir->untracked->ss_excludes_file = dir->ss_excludes_file;2193}21942195/* Make sure this directory is not dropped out at saving phase */2196 root->recurse =1;2197return root;2198}21992200intread_directory(struct dir_struct *dir,struct index_state *istate,2201const char*path,int len,const struct pathspec *pathspec)2202{2203struct untracked_cache_dir *untracked;22042205if(has_symlink_leading_path(path, len))2206return dir->nr;22072208 untracked =validate_untracked_cache(dir, len, pathspec);2209if(!untracked)2210/*2211 * make sure untracked cache code path is disabled,2212 * e.g. prep_exclude()2213 */2214 dir->untracked = NULL;2215if(!len ||treat_leading_path(dir, istate, path, len, pathspec))2216read_directory_recursive(dir, istate, path, len, untracked,0,0, pathspec);2217QSORT(dir->entries, dir->nr, cmp_dir_entry);2218QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);22192220/*2221 * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will2222 * also pick up untracked contents of untracked dirs; by default2223 * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.2224 */2225if((dir->flags & DIR_SHOW_IGNORED_TOO) &&2226!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {2227int i, j;22282229/* remove from dir->entries untracked contents of untracked dirs */2230for(i = j =0; j < dir->nr; j++) {2231if(i &&2232check_dir_entry_contains(dir->entries[i -1], dir->entries[j])) {2233FREE_AND_NULL(dir->entries[j]);2234}else{2235 dir->entries[i++] = dir->entries[j];2236}2237}22382239 dir->nr = i;2240}22412242if(dir->untracked) {2243static struct trace_key trace_untracked_stats =TRACE_KEY_INIT(UNTRACKED_STATS);2244trace_printf_key(&trace_untracked_stats,2245"node creation:%u\n"2246"gitignore invalidation:%u\n"2247"directory invalidation:%u\n"2248"opendir:%u\n",2249 dir->untracked->dir_created,2250 dir->untracked->gitignore_invalidated,2251 dir->untracked->dir_invalidated,2252 dir->untracked->dir_opened);2253if(dir->untracked == istate->untracked &&2254(dir->untracked->dir_opened ||2255 dir->untracked->gitignore_invalidated ||2256 dir->untracked->dir_invalidated))2257 istate->cache_changed |= UNTRACKED_CHANGED;2258if(dir->untracked != istate->untracked) {2259FREE_AND_NULL(dir->untracked);2260}2261}2262return dir->nr;2263}22642265intfile_exists(const char*f)2266{2267struct stat sb;2268returnlstat(f, &sb) ==0;2269}22702271static intcmp_icase(char a,char b)2272{2273if(a == b)2274return0;2275if(ignore_case)2276returntoupper(a) -toupper(b);2277return a - b;2278}22792280/*2281 * Given two normalized paths (a trailing slash is ok), if subdir is2282 * outside dir, return -1. Otherwise return the offset in subdir that2283 * can be used as relative path to dir.2284 */2285intdir_inside_of(const char*subdir,const char*dir)2286{2287int offset =0;22882289assert(dir && subdir && *dir && *subdir);22902291while(*dir && *subdir && !cmp_icase(*dir, *subdir)) {2292 dir++;2293 subdir++;2294 offset++;2295}22962297/* hel[p]/me vs hel[l]/yeah */2298if(*dir && *subdir)2299return-1;23002301if(!*subdir)2302return!*dir ? offset : -1;/* same dir */23032304/* foo/[b]ar vs foo/[] */2305if(is_dir_sep(dir[-1]))2306returnis_dir_sep(subdir[-1]) ? offset : -1;23072308/* foo[/]bar vs foo[] */2309returnis_dir_sep(*subdir) ? offset +1: -1;2310}23112312intis_inside_dir(const char*dir)2313{2314char*cwd;2315int rc;23162317if(!dir)2318return0;23192320 cwd =xgetcwd();2321 rc = (dir_inside_of(cwd, dir) >=0);2322free(cwd);2323return rc;2324}23252326intis_empty_dir(const char*path)2327{2328DIR*dir =opendir(path);2329struct dirent *e;2330int ret =1;23312332if(!dir)2333return0;23342335while((e =readdir(dir)) != NULL)2336if(!is_dot_or_dotdot(e->d_name)) {2337 ret =0;2338break;2339}23402341closedir(dir);2342return ret;2343}23442345static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)2346{2347DIR*dir;2348struct dirent *e;2349int ret =0, original_len = path->len, len, kept_down =0;2350int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2351int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2352unsigned char submodule_head[20];23532354if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2355!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {2356/* Do not descend and nuke a nested git work tree. */2357if(kept_up)2358*kept_up =1;2359return0;2360}23612362 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2363 dir =opendir(path->buf);2364if(!dir) {2365if(errno == ENOENT)2366return keep_toplevel ? -1:0;2367else if(errno == EACCES && !keep_toplevel)2368/*2369 * An empty dir could be removable even if it2370 * is unreadable:2371 */2372returnrmdir(path->buf);2373else2374return-1;2375}2376strbuf_complete(path,'/');23772378 len = path->len;2379while((e =readdir(dir)) != NULL) {2380struct stat st;2381if(is_dot_or_dotdot(e->d_name))2382continue;23832384strbuf_setlen(path, len);2385strbuf_addstr(path, e->d_name);2386if(lstat(path->buf, &st)) {2387if(errno == ENOENT)2388/*2389 * file disappeared, which is what we2390 * wanted anyway2391 */2392continue;2393/* fall thru */2394}else if(S_ISDIR(st.st_mode)) {2395if(!remove_dir_recurse(path, flag, &kept_down))2396continue;/* happy */2397}else if(!only_empty &&2398(!unlink(path->buf) || errno == ENOENT)) {2399continue;/* happy, too */2400}24012402/* path too long, stat fails, or non-directory still exists */2403 ret = -1;2404break;2405}2406closedir(dir);24072408strbuf_setlen(path, original_len);2409if(!ret && !keep_toplevel && !kept_down)2410 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;2411else if(kept_up)2412/*2413 * report the uplevel that it is not an error that we2414 * did not rmdir() our directory.2415 */2416*kept_up = !ret;2417return ret;2418}24192420intremove_dir_recursively(struct strbuf *path,int flag)2421{2422returnremove_dir_recurse(path, flag, NULL);2423}24242425staticGIT_PATH_FUNC(git_path_info_exclude,"info/exclude")24262427voidsetup_standard_excludes(struct dir_struct *dir)2428{2429 dir->exclude_per_dir =".gitignore";24302431/* core.excludefile defaulting to $XDG_HOME/git/ignore */2432if(!excludes_file)2433 excludes_file =xdg_config_home("ignore");2434if(excludes_file && !access_or_warn(excludes_file, R_OK,0))2435add_excludes_from_file_1(dir, excludes_file,2436 dir->untracked ? &dir->ss_excludes_file : NULL);24372438/* per repository user preference */2439if(startup_info->have_repository) {2440const char*path =git_path_info_exclude();2441if(!access_or_warn(path, R_OK,0))2442add_excludes_from_file_1(dir, path,2443 dir->untracked ? &dir->ss_info_exclude : NULL);2444}2445}24462447intremove_path(const char*name)2448{2449char*slash;24502451if(unlink(name) && !is_missing_file_error(errno))2452return-1;24532454 slash =strrchr(name,'/');2455if(slash) {2456char*dirs =xstrdup(name);2457 slash = dirs + (slash - name);2458do{2459*slash ='\0';2460}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));2461free(dirs);2462}2463return0;2464}24652466/*2467 * Frees memory within dir which was allocated for exclude lists and2468 * the exclude_stack. Does not free dir itself.2469 */2470voidclear_directory(struct dir_struct *dir)2471{2472int i, j;2473struct exclude_list_group *group;2474struct exclude_list *el;2475struct exclude_stack *stk;24762477for(i = EXC_CMDL; i <= EXC_FILE; i++) {2478 group = &dir->exclude_list_group[i];2479for(j =0; j < group->nr; j++) {2480 el = &group->el[j];2481if(i == EXC_DIRS)2482free((char*)el->src);2483clear_exclude_list(el);2484}2485free(group->el);2486}24872488 stk = dir->exclude_stack;2489while(stk) {2490struct exclude_stack *prev = stk->prev;2491free(stk);2492 stk = prev;2493}2494strbuf_release(&dir->basebuf);2495}24962497struct ondisk_untracked_cache {2498struct stat_data info_exclude_stat;2499struct stat_data excludes_file_stat;2500uint32_t dir_flags;2501unsigned char info_exclude_sha1[20];2502unsigned char excludes_file_sha1[20];2503char exclude_per_dir[FLEX_ARRAY];2504};25052506#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)2507#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)25082509struct write_data {2510int index;/* number of written untracked_cache_dir */2511struct ewah_bitmap *check_only;/* from untracked_cache_dir */2512struct ewah_bitmap *valid;/* from untracked_cache_dir */2513struct ewah_bitmap *sha1_valid;/* set if exclude_sha1 is not null */2514struct strbuf out;2515struct strbuf sb_stat;2516struct strbuf sb_sha1;2517};25182519static voidstat_data_to_disk(struct stat_data *to,const struct stat_data *from)2520{2521 to->sd_ctime.sec =htonl(from->sd_ctime.sec);2522 to->sd_ctime.nsec =htonl(from->sd_ctime.nsec);2523 to->sd_mtime.sec =htonl(from->sd_mtime.sec);2524 to->sd_mtime.nsec =htonl(from->sd_mtime.nsec);2525 to->sd_dev =htonl(from->sd_dev);2526 to->sd_ino =htonl(from->sd_ino);2527 to->sd_uid =htonl(from->sd_uid);2528 to->sd_gid =htonl(from->sd_gid);2529 to->sd_size =htonl(from->sd_size);2530}25312532static voidwrite_one_dir(struct untracked_cache_dir *untracked,2533struct write_data *wd)2534{2535struct stat_data stat_data;2536struct strbuf *out = &wd->out;2537unsigned char intbuf[16];2538unsigned int intlen, value;2539int i = wd->index++;25402541/*2542 * untracked_nr should be reset whenever valid is clear, but2543 * for safety..2544 */2545if(!untracked->valid) {2546 untracked->untracked_nr =0;2547 untracked->check_only =0;2548}25492550if(untracked->check_only)2551ewah_set(wd->check_only, i);2552if(untracked->valid) {2553ewah_set(wd->valid, i);2554stat_data_to_disk(&stat_data, &untracked->stat_data);2555strbuf_add(&wd->sb_stat, &stat_data,sizeof(stat_data));2556}2557if(!is_null_sha1(untracked->exclude_sha1)) {2558ewah_set(wd->sha1_valid, i);2559strbuf_add(&wd->sb_sha1, untracked->exclude_sha1,20);2560}25612562 intlen =encode_varint(untracked->untracked_nr, intbuf);2563strbuf_add(out, intbuf, intlen);25642565/* skip non-recurse directories */2566for(i =0, value =0; i < untracked->dirs_nr; i++)2567if(untracked->dirs[i]->recurse)2568 value++;2569 intlen =encode_varint(value, intbuf);2570strbuf_add(out, intbuf, intlen);25712572strbuf_add(out, untracked->name,strlen(untracked->name) +1);25732574for(i =0; i < untracked->untracked_nr; i++)2575strbuf_add(out, untracked->untracked[i],2576strlen(untracked->untracked[i]) +1);25772578for(i =0; i < untracked->dirs_nr; i++)2579if(untracked->dirs[i]->recurse)2580write_one_dir(untracked->dirs[i], wd);2581}25822583voidwrite_untracked_extension(struct strbuf *out,struct untracked_cache *untracked)2584{2585struct ondisk_untracked_cache *ouc;2586struct write_data wd;2587unsigned char varbuf[16];2588int varint_len;2589size_t len =strlen(untracked->exclude_per_dir);25902591FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);2592stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2593stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2594hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);2595hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);2596 ouc->dir_flags =htonl(untracked->dir_flags);25972598 varint_len =encode_varint(untracked->ident.len, varbuf);2599strbuf_add(out, varbuf, varint_len);2600strbuf_addbuf(out, &untracked->ident);26012602strbuf_add(out, ouc,ouc_size(len));2603FREE_AND_NULL(ouc);26042605if(!untracked->root) {2606 varint_len =encode_varint(0, varbuf);2607strbuf_add(out, varbuf, varint_len);2608return;2609}26102611 wd.index =0;2612 wd.check_only =ewah_new();2613 wd.valid =ewah_new();2614 wd.sha1_valid =ewah_new();2615strbuf_init(&wd.out,1024);2616strbuf_init(&wd.sb_stat,1024);2617strbuf_init(&wd.sb_sha1,1024);2618write_one_dir(untracked->root, &wd);26192620 varint_len =encode_varint(wd.index, varbuf);2621strbuf_add(out, varbuf, varint_len);2622strbuf_addbuf(out, &wd.out);2623ewah_serialize_strbuf(wd.valid, out);2624ewah_serialize_strbuf(wd.check_only, out);2625ewah_serialize_strbuf(wd.sha1_valid, out);2626strbuf_addbuf(out, &wd.sb_stat);2627strbuf_addbuf(out, &wd.sb_sha1);2628strbuf_addch(out,'\0');/* safe guard for string lists */26292630ewah_free(wd.valid);2631ewah_free(wd.check_only);2632ewah_free(wd.sha1_valid);2633strbuf_release(&wd.out);2634strbuf_release(&wd.sb_stat);2635strbuf_release(&wd.sb_sha1);2636}26372638static voidfree_untracked(struct untracked_cache_dir *ucd)2639{2640int i;2641if(!ucd)2642return;2643for(i =0; i < ucd->dirs_nr; i++)2644free_untracked(ucd->dirs[i]);2645for(i =0; i < ucd->untracked_nr; i++)2646free(ucd->untracked[i]);2647free(ucd->untracked);2648free(ucd->dirs);2649free(ucd);2650}26512652voidfree_untracked_cache(struct untracked_cache *uc)2653{2654if(uc)2655free_untracked(uc->root);2656free(uc);2657}26582659struct read_data {2660int index;2661struct untracked_cache_dir **ucd;2662struct ewah_bitmap *check_only;2663struct ewah_bitmap *valid;2664struct ewah_bitmap *sha1_valid;2665const unsigned char*data;2666const unsigned char*end;2667};26682669static voidstat_data_from_disk(struct stat_data *to,const unsigned char*data)2670{2671memcpy(to, data,sizeof(*to));2672 to->sd_ctime.sec =ntohl(to->sd_ctime.sec);2673 to->sd_ctime.nsec =ntohl(to->sd_ctime.nsec);2674 to->sd_mtime.sec =ntohl(to->sd_mtime.sec);2675 to->sd_mtime.nsec =ntohl(to->sd_mtime.nsec);2676 to->sd_dev =ntohl(to->sd_dev);2677 to->sd_ino =ntohl(to->sd_ino);2678 to->sd_uid =ntohl(to->sd_uid);2679 to->sd_gid =ntohl(to->sd_gid);2680 to->sd_size =ntohl(to->sd_size);2681}26822683static intread_one_dir(struct untracked_cache_dir **untracked_,2684struct read_data *rd)2685{2686struct untracked_cache_dir ud, *untracked;2687const unsigned char*next, *data = rd->data, *end = rd->end;2688unsigned int value;2689int i, len;26902691memset(&ud,0,sizeof(ud));26922693 next = data;2694 value =decode_varint(&next);2695if(next > end)2696return-1;2697 ud.recurse =1;2698 ud.untracked_alloc = value;2699 ud.untracked_nr = value;2700if(ud.untracked_nr)2701ALLOC_ARRAY(ud.untracked, ud.untracked_nr);2702 data = next;27032704 next = data;2705 ud.dirs_alloc = ud.dirs_nr =decode_varint(&next);2706if(next > end)2707return-1;2708ALLOC_ARRAY(ud.dirs, ud.dirs_nr);2709 data = next;27102711 len =strlen((const char*)data);2712 next = data + len +1;2713if(next > rd->end)2714return-1;2715*untracked_ = untracked =xmalloc(st_add(sizeof(*untracked), len));2716memcpy(untracked, &ud,sizeof(ud));2717memcpy(untracked->name, data, len +1);2718 data = next;27192720for(i =0; i < untracked->untracked_nr; i++) {2721 len =strlen((const char*)data);2722 next = data + len +1;2723if(next > rd->end)2724return-1;2725 untracked->untracked[i] =xstrdup((const char*)data);2726 data = next;2727}27282729 rd->ucd[rd->index++] = untracked;2730 rd->data = data;27312732for(i =0; i < untracked->dirs_nr; i++) {2733 len =read_one_dir(untracked->dirs + i, rd);2734if(len <0)2735return-1;2736}2737return0;2738}27392740static voidset_check_only(size_t pos,void*cb)2741{2742struct read_data *rd = cb;2743struct untracked_cache_dir *ud = rd->ucd[pos];2744 ud->check_only =1;2745}27462747static voidread_stat(size_t pos,void*cb)2748{2749struct read_data *rd = cb;2750struct untracked_cache_dir *ud = rd->ucd[pos];2751if(rd->data +sizeof(struct stat_data) > rd->end) {2752 rd->data = rd->end +1;2753return;2754}2755stat_data_from_disk(&ud->stat_data, rd->data);2756 rd->data +=sizeof(struct stat_data);2757 ud->valid =1;2758}27592760static voidread_sha1(size_t pos,void*cb)2761{2762struct read_data *rd = cb;2763struct untracked_cache_dir *ud = rd->ucd[pos];2764if(rd->data +20> rd->end) {2765 rd->data = rd->end +1;2766return;2767}2768hashcpy(ud->exclude_sha1, rd->data);2769 rd->data +=20;2770}27712772static voidload_sha1_stat(struct sha1_stat *sha1_stat,2773const unsigned char*data,2774const unsigned char*sha1)2775{2776stat_data_from_disk(&sha1_stat->stat, data);2777hashcpy(sha1_stat->sha1, sha1);2778 sha1_stat->valid =1;2779}27802781struct untracked_cache *read_untracked_extension(const void*data,unsigned long sz)2782{2783struct untracked_cache *uc;2784struct read_data rd;2785const unsigned char*next = data, *end = (const unsigned char*)data + sz;2786const char*ident;2787int ident_len, len;2788const char*exclude_per_dir;27892790if(sz <=1|| end[-1] !='\0')2791return NULL;2792 end--;27932794 ident_len =decode_varint(&next);2795if(next + ident_len > end)2796return NULL;2797 ident = (const char*)next;2798 next += ident_len;27992800if(next +ouc_size(0) > end)2801return NULL;28022803 uc =xcalloc(1,sizeof(*uc));2804strbuf_init(&uc->ident, ident_len);2805strbuf_add(&uc->ident, ident, ident_len);2806load_sha1_stat(&uc->ss_info_exclude,2807 next +ouc_offset(info_exclude_stat),2808 next +ouc_offset(info_exclude_sha1));2809load_sha1_stat(&uc->ss_excludes_file,2810 next +ouc_offset(excludes_file_stat),2811 next +ouc_offset(excludes_file_sha1));2812 uc->dir_flags =get_be32(next +ouc_offset(dir_flags));2813 exclude_per_dir = (const char*)next +ouc_offset(exclude_per_dir);2814 uc->exclude_per_dir =xstrdup(exclude_per_dir);2815/* NUL after exclude_per_dir is covered by sizeof(*ouc) */2816 next +=ouc_size(strlen(exclude_per_dir));2817if(next >= end)2818goto done2;28192820 len =decode_varint(&next);2821if(next > end || len ==0)2822goto done2;28232824 rd.valid =ewah_new();2825 rd.check_only =ewah_new();2826 rd.sha1_valid =ewah_new();2827 rd.data = next;2828 rd.end = end;2829 rd.index =0;2830ALLOC_ARRAY(rd.ucd, len);28312832if(read_one_dir(&uc->root, &rd) || rd.index != len)2833goto done;28342835 next = rd.data;2836 len =ewah_read_mmap(rd.valid, next, end - next);2837if(len <0)2838goto done;28392840 next += len;2841 len =ewah_read_mmap(rd.check_only, next, end - next);2842if(len <0)2843goto done;28442845 next += len;2846 len =ewah_read_mmap(rd.sha1_valid, next, end - next);2847if(len <0)2848goto done;28492850ewah_each_bit(rd.check_only, set_check_only, &rd);2851 rd.data = next + len;2852ewah_each_bit(rd.valid, read_stat, &rd);2853ewah_each_bit(rd.sha1_valid, read_sha1, &rd);2854 next = rd.data;28552856done:2857free(rd.ucd);2858ewah_free(rd.valid);2859ewah_free(rd.check_only);2860ewah_free(rd.sha1_valid);2861done2:2862if(next != end) {2863free_untracked_cache(uc);2864 uc = NULL;2865}2866return uc;2867}28682869static voidinvalidate_one_directory(struct untracked_cache *uc,2870struct untracked_cache_dir *ucd)2871{2872 uc->dir_invalidated++;2873 ucd->valid =0;2874 ucd->untracked_nr =0;2875}28762877/*2878 * Normally when an entry is added or removed from a directory,2879 * invalidating that directory is enough. No need to touch its2880 * ancestors. When a directory is shown as "foo/bar/" in git-status2881 * however, deleting or adding an entry may have cascading effect.2882 *2883 * Say the "foo/bar/file" has become untracked, we need to tell the2884 * untracked_cache_dir of "foo" that "bar/" is not an untracked2885 * directory any more (because "bar" is managed by foo as an untracked2886 * "file").2887 *2888 * Similarly, if "foo/bar/file" moves from untracked to tracked and it2889 * was the last untracked entry in the entire "foo", we should show2890 * "foo/" instead. Which means we have to invalidate past "bar" up to2891 * "foo".2892 *2893 * This function traverses all directories from root to leaf. If there2894 * is a chance of one of the above cases happening, we invalidate back2895 * to root. Otherwise we just invalidate the leaf. There may be a more2896 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to2897 * detect these cases and avoid unnecessary invalidation, for example,2898 * checking for the untracked entry named "bar/" in "foo", but for now2899 * stick to something safe and simple.2900 */2901static intinvalidate_one_component(struct untracked_cache *uc,2902struct untracked_cache_dir *dir,2903const char*path,int len)2904{2905const char*rest =strchr(path,'/');29062907if(rest) {2908int component_len = rest - path;2909struct untracked_cache_dir *d =2910lookup_untracked(uc, dir, path, component_len);2911int ret =2912invalidate_one_component(uc, d, rest +1,2913 len - (component_len +1));2914if(ret)2915invalidate_one_directory(uc, dir);2916return ret;2917}29182919invalidate_one_directory(uc, dir);2920return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;2921}29222923voiduntracked_cache_invalidate_path(struct index_state *istate,2924const char*path)2925{2926if(!istate->untracked || !istate->untracked->root)2927return;2928invalidate_one_component(istate->untracked, istate->untracked->root,2929 path,strlen(path));2930}29312932voiduntracked_cache_remove_from_index(struct index_state *istate,2933const char*path)2934{2935untracked_cache_invalidate_path(istate, path);2936}29372938voiduntracked_cache_add_to_index(struct index_state *istate,2939const char*path)2940{2941untracked_cache_invalidate_path(istate, path);2942}29432944/* Update gitfile and core.worktree setting to connect work tree and git dir */2945voidconnect_work_tree_and_git_dir(const char*work_tree_,const char*git_dir_)2946{2947struct strbuf gitfile_sb = STRBUF_INIT;2948struct strbuf cfg_sb = STRBUF_INIT;2949struct strbuf rel_path = STRBUF_INIT;2950char*git_dir, *work_tree;29512952/* Prepare .git file */2953strbuf_addf(&gitfile_sb,"%s/.git", work_tree_);2954if(safe_create_leading_directories_const(gitfile_sb.buf))2955die(_("could not create directories for%s"), gitfile_sb.buf);29562957/* Prepare config file */2958strbuf_addf(&cfg_sb,"%s/config", git_dir_);2959if(safe_create_leading_directories_const(cfg_sb.buf))2960die(_("could not create directories for%s"), cfg_sb.buf);29612962 git_dir =real_pathdup(git_dir_,1);2963 work_tree =real_pathdup(work_tree_,1);29642965/* Write .git file */2966write_file(gitfile_sb.buf,"gitdir:%s",2967relative_path(git_dir, work_tree, &rel_path));2968/* Update core.worktree setting */2969git_config_set_in_file(cfg_sb.buf,"core.worktree",2970relative_path(work_tree, git_dir, &rel_path));29712972strbuf_release(&gitfile_sb);2973strbuf_release(&cfg_sb);2974strbuf_release(&rel_path);2975free(work_tree);2976free(git_dir);2977}29782979/*2980 * Migrate the git directory of the given path from old_git_dir to new_git_dir.2981 */2982voidrelocate_gitdir(const char*path,const char*old_git_dir,const char*new_git_dir)2983{2984if(rename(old_git_dir, new_git_dir) <0)2985die_errno(_("could not migrate git directory from '%s' to '%s'"),2986 old_git_dir, new_git_dir);29872988connect_work_tree_and_git_dir(path, new_git_dir);2989}