1/* 2 * This handles recursive filename detection with exclude 3 * files, index knowledge etc.. 4 * 5 * See Documentation/technical/api-directory-listing.txt 6 * 7 * Copyright (C) Linus Torvalds, 2005-2006 8 * Junio Hamano, 2005-2006 9 */ 10#define NO_THE_INDEX_COMPATIBILITY_MACROS 11#include"cache.h" 12#include"config.h" 13#include"dir.h" 14#include"object-store.h" 15#include"attr.h" 16#include"refs.h" 17#include"wildmatch.h" 18#include"pathspec.h" 19#include"utf8.h" 20#include"varint.h" 21#include"ewah/ewok.h" 22#include"fsmonitor.h" 23#include"submodule-config.h" 24 25/* 26 * Tells read_directory_recursive how a file or directory should be treated. 27 * Values are ordered by significance, e.g. if a directory contains both 28 * excluded and untracked files, it is listed as untracked because 29 * path_untracked > path_excluded. 30 */ 31enum path_treatment { 32 path_none =0, 33 path_recurse, 34 path_excluded, 35 path_untracked 36}; 37 38/* 39 * Support data structure for our opendir/readdir/closedir wrappers 40 */ 41struct cached_dir { 42DIR*fdir; 43struct untracked_cache_dir *untracked; 44int nr_files; 45int nr_dirs; 46 47struct dirent *de; 48const char*file; 49struct untracked_cache_dir *ucd; 50}; 51 52static enum path_treatment read_directory_recursive(struct dir_struct *dir, 53struct index_state *istate,const char*path,int len, 54struct untracked_cache_dir *untracked, 55int check_only,int stop_at_first_file,const struct pathspec *pathspec); 56static intget_dtype(struct dirent *de,struct index_state *istate, 57const char*path,int len); 58 59intcount_slashes(const char*s) 60{ 61int cnt =0; 62while(*s) 63if(*s++ =='/') 64 cnt++; 65return cnt; 66} 67 68intfspathcmp(const char*a,const char*b) 69{ 70return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 71} 72 73intfspathncmp(const char*a,const char*b,size_t count) 74{ 75return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 76} 77 78intgit_fnmatch(const struct pathspec_item *item, 79const char*pattern,const char*string, 80int prefix) 81{ 82if(prefix >0) { 83if(ps_strncmp(item, pattern, string, prefix)) 84return WM_NOMATCH; 85 pattern += prefix; 86 string += prefix; 87} 88if(item->flags & PATHSPEC_ONESTAR) { 89int pattern_len =strlen(++pattern); 90int string_len =strlen(string); 91return string_len < pattern_len || 92ps_strcmp(item, pattern, 93 string + string_len - pattern_len); 94} 95if(item->magic & PATHSPEC_GLOB) 96returnwildmatch(pattern, string, 97 WM_PATHNAME | 98(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0)); 99else 100/* wildmatch has not learned no FNM_PATHNAME mode yet */ 101returnwildmatch(pattern, string, 102 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0); 103} 104 105static intfnmatch_icase_mem(const char*pattern,int patternlen, 106const char*string,int stringlen, 107int flags) 108{ 109int match_status; 110struct strbuf pat_buf = STRBUF_INIT; 111struct strbuf str_buf = STRBUF_INIT; 112const char*use_pat = pattern; 113const char*use_str = string; 114 115if(pattern[patternlen]) { 116strbuf_add(&pat_buf, pattern, patternlen); 117 use_pat = pat_buf.buf; 118} 119if(string[stringlen]) { 120strbuf_add(&str_buf, string, stringlen); 121 use_str = str_buf.buf; 122} 123 124if(ignore_case) 125 flags |= WM_CASEFOLD; 126 match_status =wildmatch(use_pat, use_str, flags); 127 128strbuf_release(&pat_buf); 129strbuf_release(&str_buf); 130 131return match_status; 132} 133 134static size_tcommon_prefix_len(const struct pathspec *pathspec) 135{ 136int n; 137size_t max =0; 138 139/* 140 * ":(icase)path" is treated as a pathspec full of 141 * wildcard. In other words, only prefix is considered common 142 * prefix. If the pathspec is abc/foo abc/bar, running in 143 * subdir xyz, the common prefix is still xyz, not xuz/abc as 144 * in non-:(icase). 145 */ 146GUARD_PATHSPEC(pathspec, 147 PATHSPEC_FROMTOP | 148 PATHSPEC_MAXDEPTH | 149 PATHSPEC_LITERAL | 150 PATHSPEC_GLOB | 151 PATHSPEC_ICASE | 152 PATHSPEC_EXCLUDE | 153 PATHSPEC_ATTR); 154 155for(n =0; n < pathspec->nr; n++) { 156size_t i =0, len =0, item_len; 157if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 158continue; 159if(pathspec->items[n].magic & PATHSPEC_ICASE) 160 item_len = pathspec->items[n].prefix; 161else 162 item_len = pathspec->items[n].nowildcard_len; 163while(i < item_len && (n ==0|| i < max)) { 164char c = pathspec->items[n].match[i]; 165if(c != pathspec->items[0].match[i]) 166break; 167if(c =='/') 168 len = i +1; 169 i++; 170} 171if(n ==0|| len < max) { 172 max = len; 173if(!max) 174break; 175} 176} 177return max; 178} 179 180/* 181 * Returns a copy of the longest leading path common among all 182 * pathspecs. 183 */ 184char*common_prefix(const struct pathspec *pathspec) 185{ 186unsigned long len =common_prefix_len(pathspec); 187 188return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 189} 190 191intfill_directory(struct dir_struct *dir, 192struct index_state *istate, 193const struct pathspec *pathspec) 194{ 195const char*prefix; 196size_t prefix_len; 197 198/* 199 * Calculate common prefix for the pathspec, and 200 * use that to optimize the directory walk 201 */ 202 prefix_len =common_prefix_len(pathspec); 203 prefix = prefix_len ? pathspec->items[0].match :""; 204 205/* Read the directory and prune it */ 206read_directory(dir, istate, prefix, prefix_len, pathspec); 207 208return prefix_len; 209} 210 211intwithin_depth(const char*name,int namelen, 212int depth,int max_depth) 213{ 214const char*cp = name, *cpe = name + namelen; 215 216while(cp < cpe) { 217if(*cp++ !='/') 218continue; 219 depth++; 220if(depth > max_depth) 221return0; 222} 223return1; 224} 225 226/* 227 * Read the contents of the blob with the given OID into a buffer. 228 * Append a trailing LF to the end if the last line doesn't have one. 229 * 230 * Returns: 231 * -1 when the OID is invalid or unknown or does not refer to a blob. 232 * 0 when the blob is empty. 233 * 1 along with { data, size } of the (possibly augmented) buffer 234 * when successful. 235 * 236 * Optionally updates the given oid_stat with the given OID (when valid). 237 */ 238static intdo_read_blob(const struct object_id *oid,struct oid_stat *oid_stat, 239size_t*size_out,char**data_out) 240{ 241enum object_type type; 242unsigned long sz; 243char*data; 244 245*size_out =0; 246*data_out = NULL; 247 248 data =read_object_file(oid, &type, &sz); 249if(!data || type != OBJ_BLOB) { 250free(data); 251return-1; 252} 253 254if(oid_stat) { 255memset(&oid_stat->stat,0,sizeof(oid_stat->stat)); 256oidcpy(&oid_stat->oid, oid); 257} 258 259if(sz ==0) { 260free(data); 261return0; 262} 263 264if(data[sz -1] !='\n') { 265 data =xrealloc(data,st_add(sz,1)); 266 data[sz++] ='\n'; 267} 268 269*size_out =xsize_t(sz); 270*data_out = data; 271 272return1; 273} 274 275#define DO_MATCH_EXCLUDE (1<<0) 276#define DO_MATCH_DIRECTORY (1<<1) 277#define DO_MATCH_SUBMODULE (1<<2) 278 279static intmatch_attrs(const struct index_state *istate, 280const char*name,int namelen, 281const struct pathspec_item *item) 282{ 283int i; 284char*to_free = NULL; 285 286if(name[namelen]) 287 name = to_free =xmemdupz(name, namelen); 288 289git_check_attr(istate, name, item->attr_check); 290 291free(to_free); 292 293for(i =0; i < item->attr_match_nr; i++) { 294const char*value; 295int matched; 296enum attr_match_mode match_mode; 297 298 value = item->attr_check->items[i].value; 299 match_mode = item->attr_match[i].match_mode; 300 301if(ATTR_TRUE(value)) 302 matched = (match_mode == MATCH_SET); 303else if(ATTR_FALSE(value)) 304 matched = (match_mode == MATCH_UNSET); 305else if(ATTR_UNSET(value)) 306 matched = (match_mode == MATCH_UNSPECIFIED); 307else 308 matched = (match_mode == MATCH_VALUE && 309!strcmp(item->attr_match[i].value, value)); 310if(!matched) 311return0; 312} 313 314return1; 315} 316 317/* 318 * Does 'match' match the given name? 319 * A match is found if 320 * 321 * (1) the 'match' string is leading directory of 'name', or 322 * (2) the 'match' string is a wildcard and matches 'name', or 323 * (3) the 'match' string is exactly the same as 'name'. 324 * 325 * and the return value tells which case it was. 326 * 327 * It returns 0 when there is no match. 328 */ 329static intmatch_pathspec_item(const struct index_state *istate, 330const struct pathspec_item *item,int prefix, 331const char*name,int namelen,unsigned flags) 332{ 333/* name/namelen has prefix cut off by caller */ 334const char*match = item->match + prefix; 335int matchlen = item->len - prefix; 336 337/* 338 * The normal call pattern is: 339 * 1. prefix = common_prefix_len(ps); 340 * 2. prune something, or fill_directory 341 * 3. match_pathspec() 342 * 343 * 'prefix' at #1 may be shorter than the command's prefix and 344 * it's ok for #2 to match extra files. Those extras will be 345 * trimmed at #3. 346 * 347 * Suppose the pathspec is 'foo' and '../bar' running from 348 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 349 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 350 * user does not want XYZ/foo, only the "foo" part should be 351 * case-insensitive. We need to filter out XYZ/foo here. In 352 * other words, we do not trust the caller on comparing the 353 * prefix part when :(icase) is involved. We do exact 354 * comparison ourselves. 355 * 356 * Normally the caller (common_prefix_len() in fact) does 357 * _exact_ matching on name[-prefix+1..-1] and we do not need 358 * to check that part. Be defensive and check it anyway, in 359 * case common_prefix_len is changed, or a new caller is 360 * introduced that does not use common_prefix_len. 361 * 362 * If the penalty turns out too high when prefix is really 363 * long, maybe change it to 364 * strncmp(match, name, item->prefix - prefix) 365 */ 366if(item->prefix && (item->magic & PATHSPEC_ICASE) && 367strncmp(item->match, name - prefix, item->prefix)) 368return0; 369 370if(item->attr_match_nr && !match_attrs(istate, name, namelen, item)) 371return0; 372 373/* If the match was just the prefix, we matched */ 374if(!*match) 375return MATCHED_RECURSIVELY; 376 377if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 378if(matchlen == namelen) 379return MATCHED_EXACTLY; 380 381if(match[matchlen-1] =='/'|| name[matchlen] =='/') 382return MATCHED_RECURSIVELY; 383}else if((flags & DO_MATCH_DIRECTORY) && 384 match[matchlen -1] =='/'&& 385 namelen == matchlen -1&& 386!ps_strncmp(item, match, name, namelen)) 387return MATCHED_EXACTLY; 388 389if(item->nowildcard_len < item->len && 390!git_fnmatch(item, match, name, 391 item->nowildcard_len - prefix)) 392return MATCHED_FNMATCH; 393 394/* Perform checks to see if "name" is a super set of the pathspec */ 395if(flags & DO_MATCH_SUBMODULE) { 396/* name is a literal prefix of the pathspec */ 397if((namelen < matchlen) && 398(match[namelen] =='/') && 399!ps_strncmp(item, match, name, namelen)) 400return MATCHED_RECURSIVELY; 401 402/* name" doesn't match up to the first wild character */ 403if(item->nowildcard_len < item->len && 404ps_strncmp(item, match, name, 405 item->nowildcard_len - prefix)) 406return0; 407 408/* 409 * Here is where we would perform a wildmatch to check if 410 * "name" can be matched as a directory (or a prefix) against 411 * the pathspec. Since wildmatch doesn't have this capability 412 * at the present we have to punt and say that it is a match, 413 * potentially returning a false positive 414 * The submodules themselves will be able to perform more 415 * accurate matching to determine if the pathspec matches. 416 */ 417return MATCHED_RECURSIVELY; 418} 419 420return0; 421} 422 423/* 424 * Given a name and a list of pathspecs, returns the nature of the 425 * closest (i.e. most specific) match of the name to any of the 426 * pathspecs. 427 * 428 * The caller typically calls this multiple times with the same 429 * pathspec and seen[] array but with different name/namelen 430 * (e.g. entries from the index) and is interested in seeing if and 431 * how each pathspec matches all the names it calls this function 432 * with. A mark is left in the seen[] array for each pathspec element 433 * indicating the closest type of match that element achieved, so if 434 * seen[n] remains zero after multiple invocations, that means the nth 435 * pathspec did not match any names, which could indicate that the 436 * user mistyped the nth pathspec. 437 */ 438static intdo_match_pathspec(const struct index_state *istate, 439const struct pathspec *ps, 440const char*name,int namelen, 441int prefix,char*seen, 442unsigned flags) 443{ 444int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 445 446GUARD_PATHSPEC(ps, 447 PATHSPEC_FROMTOP | 448 PATHSPEC_MAXDEPTH | 449 PATHSPEC_LITERAL | 450 PATHSPEC_GLOB | 451 PATHSPEC_ICASE | 452 PATHSPEC_EXCLUDE | 453 PATHSPEC_ATTR); 454 455if(!ps->nr) { 456if(!ps->recursive || 457!(ps->magic & PATHSPEC_MAXDEPTH) || 458 ps->max_depth == -1) 459return MATCHED_RECURSIVELY; 460 461if(within_depth(name, namelen,0, ps->max_depth)) 462return MATCHED_EXACTLY; 463else 464return0; 465} 466 467 name += prefix; 468 namelen -= prefix; 469 470for(i = ps->nr -1; i >=0; i--) { 471int how; 472 473if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 474( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 475continue; 476 477if(seen && seen[i] == MATCHED_EXACTLY) 478continue; 479/* 480 * Make exclude patterns optional and never report 481 * "pathspec ':(exclude)foo' matches no files" 482 */ 483if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 484 seen[i] = MATCHED_FNMATCH; 485 how =match_pathspec_item(istate, ps->items+i, prefix, name, 486 namelen, flags); 487if(ps->recursive && 488(ps->magic & PATHSPEC_MAXDEPTH) && 489 ps->max_depth != -1&& 490 how && how != MATCHED_FNMATCH) { 491int len = ps->items[i].len; 492if(name[len] =='/') 493 len++; 494if(within_depth(name+len, namelen-len,0, ps->max_depth)) 495 how = MATCHED_EXACTLY; 496else 497 how =0; 498} 499if(how) { 500if(retval < how) 501 retval = how; 502if(seen && seen[i] < how) 503 seen[i] = how; 504} 505} 506return retval; 507} 508 509intmatch_pathspec(const struct index_state *istate, 510const struct pathspec *ps, 511const char*name,int namelen, 512int prefix,char*seen,int is_dir) 513{ 514int positive, negative; 515unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 516 positive =do_match_pathspec(istate, ps, name, namelen, 517 prefix, seen, flags); 518if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 519return positive; 520 negative =do_match_pathspec(istate, ps, name, namelen, 521 prefix, seen, 522 flags | DO_MATCH_EXCLUDE); 523return negative ?0: positive; 524} 525 526/** 527 * Check if a submodule is a superset of the pathspec 528 */ 529intsubmodule_path_match(const struct index_state *istate, 530const struct pathspec *ps, 531const char*submodule_name, 532char*seen) 533{ 534int matched =do_match_pathspec(istate, ps, submodule_name, 535strlen(submodule_name), 5360, seen, 537 DO_MATCH_DIRECTORY | 538 DO_MATCH_SUBMODULE); 539return matched; 540} 541 542intreport_path_error(const char*ps_matched, 543const struct pathspec *pathspec, 544const char*prefix) 545{ 546/* 547 * Make sure all pathspec matched; otherwise it is an error. 548 */ 549int num, errors =0; 550for(num =0; num < pathspec->nr; num++) { 551int other, found_dup; 552 553if(ps_matched[num]) 554continue; 555/* 556 * The caller might have fed identical pathspec 557 * twice. Do not barf on such a mistake. 558 * FIXME: parse_pathspec should have eliminated 559 * duplicate pathspec. 560 */ 561for(found_dup = other =0; 562!found_dup && other < pathspec->nr; 563 other++) { 564if(other == num || !ps_matched[other]) 565continue; 566if(!strcmp(pathspec->items[other].original, 567 pathspec->items[num].original)) 568/* 569 * Ok, we have a match already. 570 */ 571 found_dup =1; 572} 573if(found_dup) 574continue; 575 576error(_("pathspec '%s' did not match any file(s) known to git"), 577 pathspec->items[num].original); 578 errors++; 579} 580return errors; 581} 582 583/* 584 * Return the length of the "simple" part of a path match limiter. 585 */ 586intsimple_length(const char*match) 587{ 588int len = -1; 589 590for(;;) { 591unsigned char c = *match++; 592 len++; 593if(c =='\0'||is_glob_special(c)) 594return len; 595} 596} 597 598intno_wildcard(const char*string) 599{ 600return string[simple_length(string)] =='\0'; 601} 602 603voidparse_exclude_pattern(const char**pattern, 604int*patternlen, 605unsigned*flags, 606int*nowildcardlen) 607{ 608const char*p = *pattern; 609size_t i, len; 610 611*flags =0; 612if(*p =='!') { 613*flags |= EXC_FLAG_NEGATIVE; 614 p++; 615} 616 len =strlen(p); 617if(len && p[len -1] =='/') { 618 len--; 619*flags |= EXC_FLAG_MUSTBEDIR; 620} 621for(i =0; i < len; i++) { 622if(p[i] =='/') 623break; 624} 625if(i == len) 626*flags |= EXC_FLAG_NODIR; 627*nowildcardlen =simple_length(p); 628/* 629 * we should have excluded the trailing slash from 'p' too, 630 * but that's one more allocation. Instead just make sure 631 * nowildcardlen does not exceed real patternlen 632 */ 633if(*nowildcardlen > len) 634*nowildcardlen = len; 635if(*p =='*'&&no_wildcard(p +1)) 636*flags |= EXC_FLAG_ENDSWITH; 637*pattern = p; 638*patternlen = len; 639} 640 641voidadd_exclude(const char*string,const char*base, 642int baselen,struct exclude_list *el,int srcpos) 643{ 644struct exclude *x; 645int patternlen; 646unsigned flags; 647int nowildcardlen; 648 649parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 650if(flags & EXC_FLAG_MUSTBEDIR) { 651FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen); 652}else{ 653 x =xmalloc(sizeof(*x)); 654 x->pattern = string; 655} 656 x->patternlen = patternlen; 657 x->nowildcardlen = nowildcardlen; 658 x->base = base; 659 x->baselen = baselen; 660 x->flags = flags; 661 x->srcpos = srcpos; 662ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 663 el->excludes[el->nr++] = x; 664 x->el = el; 665} 666 667static intread_skip_worktree_file_from_index(const struct index_state *istate, 668const char*path, 669size_t*size_out,char**data_out, 670struct oid_stat *oid_stat) 671{ 672int pos, len; 673 674 len =strlen(path); 675 pos =index_name_pos(istate, path, len); 676if(pos <0) 677return-1; 678if(!ce_skip_worktree(istate->cache[pos])) 679return-1; 680 681returndo_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); 682} 683 684/* 685 * Frees memory within el which was allocated for exclude patterns and 686 * the file buffer. Does not free el itself. 687 */ 688voidclear_exclude_list(struct exclude_list *el) 689{ 690int i; 691 692for(i =0; i < el->nr; i++) 693free(el->excludes[i]); 694free(el->excludes); 695free(el->filebuf); 696 697memset(el,0,sizeof(*el)); 698} 699 700static voidtrim_trailing_spaces(char*buf) 701{ 702char*p, *last_space = NULL; 703 704for(p = buf; *p; p++) 705switch(*p) { 706case' ': 707if(!last_space) 708 last_space = p; 709break; 710case'\\': 711 p++; 712if(!*p) 713return; 714/* fallthrough */ 715default: 716 last_space = NULL; 717} 718 719if(last_space) 720*last_space ='\0'; 721} 722 723/* 724 * Given a subdirectory name and "dir" of the current directory, 725 * search the subdir in "dir" and return it, or create a new one if it 726 * does not exist in "dir". 727 * 728 * If "name" has the trailing slash, it'll be excluded in the search. 729 */ 730static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 731struct untracked_cache_dir *dir, 732const char*name,int len) 733{ 734int first, last; 735struct untracked_cache_dir *d; 736if(!dir) 737return NULL; 738if(len && name[len -1] =='/') 739 len--; 740 first =0; 741 last = dir->dirs_nr; 742while(last > first) { 743int cmp, next = (last + first) >>1; 744 d = dir->dirs[next]; 745 cmp =strncmp(name, d->name, len); 746if(!cmp &&strlen(d->name) > len) 747 cmp = -1; 748if(!cmp) 749return d; 750if(cmp <0) { 751 last = next; 752continue; 753} 754 first = next+1; 755} 756 757 uc->dir_created++; 758FLEX_ALLOC_MEM(d, name, name, len); 759 760ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 761MOVE_ARRAY(dir->dirs + first +1, dir->dirs + first, 762 dir->dirs_nr - first); 763 dir->dirs_nr++; 764 dir->dirs[first] = d; 765return d; 766} 767 768static voiddo_invalidate_gitignore(struct untracked_cache_dir *dir) 769{ 770int i; 771 dir->valid =0; 772 dir->untracked_nr =0; 773for(i =0; i < dir->dirs_nr; i++) 774do_invalidate_gitignore(dir->dirs[i]); 775} 776 777static voidinvalidate_gitignore(struct untracked_cache *uc, 778struct untracked_cache_dir *dir) 779{ 780 uc->gitignore_invalidated++; 781do_invalidate_gitignore(dir); 782} 783 784static voidinvalidate_directory(struct untracked_cache *uc, 785struct untracked_cache_dir *dir) 786{ 787int i; 788 789/* 790 * Invalidation increment here is just roughly correct. If 791 * untracked_nr or any of dirs[].recurse is non-zero, we 792 * should increment dir_invalidated too. But that's more 793 * expensive to do. 794 */ 795if(dir->valid) 796 uc->dir_invalidated++; 797 798 dir->valid =0; 799 dir->untracked_nr =0; 800for(i =0; i < dir->dirs_nr; i++) 801 dir->dirs[i]->recurse =0; 802} 803 804static intadd_excludes_from_buffer(char*buf,size_t size, 805const char*base,int baselen, 806struct exclude_list *el); 807 808/* 809 * Given a file with name "fname", read it (either from disk, or from 810 * an index if 'istate' is non-null), parse it and store the 811 * exclude rules in "el". 812 * 813 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 814 * stat data from disk (only valid if add_excludes returns zero). If 815 * ss_valid is non-zero, "ss" must contain good value as input. 816 */ 817static intadd_excludes(const char*fname,const char*base,int baselen, 818struct exclude_list *el,struct index_state *istate, 819struct oid_stat *oid_stat) 820{ 821struct stat st; 822int r; 823int fd; 824size_t size =0; 825char*buf; 826 827 fd =open(fname, O_RDONLY); 828if(fd <0||fstat(fd, &st) <0) { 829if(fd <0) 830warn_on_fopen_errors(fname); 831else 832close(fd); 833if(!istate) 834return-1; 835 r =read_skip_worktree_file_from_index(istate, fname, 836&size, &buf, 837 oid_stat); 838if(r !=1) 839return r; 840}else{ 841 size =xsize_t(st.st_size); 842if(size ==0) { 843if(oid_stat) { 844fill_stat_data(&oid_stat->stat, &st); 845oidcpy(&oid_stat->oid, the_hash_algo->empty_blob); 846 oid_stat->valid =1; 847} 848close(fd); 849return0; 850} 851 buf =xmallocz(size); 852if(read_in_full(fd, buf, size) != size) { 853free(buf); 854close(fd); 855return-1; 856} 857 buf[size++] ='\n'; 858close(fd); 859if(oid_stat) { 860int pos; 861if(oid_stat->valid && 862!match_stat_data_racy(istate, &oid_stat->stat, &st)) 863;/* no content change, ss->sha1 still good */ 864else if(istate && 865(pos =index_name_pos(istate, fname,strlen(fname))) >=0&& 866!ce_stage(istate->cache[pos]) && 867ce_uptodate(istate->cache[pos]) && 868!would_convert_to_git(istate, fname)) 869oidcpy(&oid_stat->oid, 870&istate->cache[pos]->oid); 871else 872hash_object_file(buf, size,"blob", 873&oid_stat->oid); 874fill_stat_data(&oid_stat->stat, &st); 875 oid_stat->valid =1; 876} 877} 878 879add_excludes_from_buffer(buf, size, base, baselen, el); 880return0; 881} 882 883static intadd_excludes_from_buffer(char*buf,size_t size, 884const char*base,int baselen, 885struct exclude_list *el) 886{ 887int i, lineno =1; 888char*entry; 889 890 el->filebuf = buf; 891 892if(skip_utf8_bom(&buf, size)) 893 size -= buf - el->filebuf; 894 895 entry = buf; 896 897for(i =0; i < size; i++) { 898if(buf[i] =='\n') { 899if(entry != buf + i && entry[0] !='#') { 900 buf[i - (i && buf[i-1] =='\r')] =0; 901trim_trailing_spaces(entry); 902add_exclude(entry, base, baselen, el, lineno); 903} 904 lineno++; 905 entry = buf + i +1; 906} 907} 908return0; 909} 910 911intadd_excludes_from_file_to_list(const char*fname,const char*base, 912int baselen,struct exclude_list *el, 913struct index_state *istate) 914{ 915returnadd_excludes(fname, base, baselen, el, istate, NULL); 916} 917 918intadd_excludes_from_blob_to_list( 919struct object_id *oid, 920const char*base,int baselen, 921struct exclude_list *el) 922{ 923char*buf; 924size_t size; 925int r; 926 927 r =do_read_blob(oid, NULL, &size, &buf); 928if(r !=1) 929return r; 930 931add_excludes_from_buffer(buf, size, base, baselen, el); 932return0; 933} 934 935struct exclude_list *add_exclude_list(struct dir_struct *dir, 936int group_type,const char*src) 937{ 938struct exclude_list *el; 939struct exclude_list_group *group; 940 941 group = &dir->exclude_list_group[group_type]; 942ALLOC_GROW(group->el, group->nr +1, group->alloc); 943 el = &group->el[group->nr++]; 944memset(el,0,sizeof(*el)); 945 el->src = src; 946return el; 947} 948 949/* 950 * Used to set up core.excludesfile and .git/info/exclude lists. 951 */ 952static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 953struct oid_stat *oid_stat) 954{ 955struct exclude_list *el; 956/* 957 * catch setup_standard_excludes() that's called before 958 * dir->untracked is assigned. That function behaves 959 * differently when dir->untracked is non-NULL. 960 */ 961if(!dir->untracked) 962 dir->unmanaged_exclude_files++; 963 el =add_exclude_list(dir, EXC_FILE, fname); 964if(add_excludes(fname,"",0, el, NULL, oid_stat) <0) 965die(_("cannot use%sas an exclude file"), fname); 966} 967 968voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 969{ 970 dir->unmanaged_exclude_files++;/* see validate_untracked_cache() */ 971add_excludes_from_file_1(dir, fname, NULL); 972} 973 974intmatch_basename(const char*basename,int basenamelen, 975const char*pattern,int prefix,int patternlen, 976unsigned flags) 977{ 978if(prefix == patternlen) { 979if(patternlen == basenamelen && 980!fspathncmp(pattern, basename, basenamelen)) 981return1; 982}else if(flags & EXC_FLAG_ENDSWITH) { 983/* "*literal" matching against "fooliteral" */ 984if(patternlen -1<= basenamelen && 985!fspathncmp(pattern +1, 986 basename + basenamelen - (patternlen -1), 987 patternlen -1)) 988return1; 989}else{ 990if(fnmatch_icase_mem(pattern, patternlen, 991 basename, basenamelen, 9920) ==0) 993return1; 994} 995return0; 996} 997 998intmatch_pathname(const char*pathname,int pathlen, 999const char*base,int baselen,1000const char*pattern,int prefix,int patternlen,1001unsigned flags)1002{1003const char*name;1004int namelen;10051006/*1007 * match with FNM_PATHNAME; the pattern has base implicitly1008 * in front of it.1009 */1010if(*pattern =='/') {1011 pattern++;1012 patternlen--;1013 prefix--;1014}10151016/*1017 * baselen does not count the trailing slash. base[] may or1018 * may not end with a trailing slash though.1019 */1020if(pathlen < baselen +1||1021(baselen && pathname[baselen] !='/') ||1022fspathncmp(pathname, base, baselen))1023return0;10241025 namelen = baselen ? pathlen - baselen -1: pathlen;1026 name = pathname + pathlen - namelen;10271028if(prefix) {1029/*1030 * if the non-wildcard part is longer than the1031 * remaining pathname, surely it cannot match.1032 */1033if(prefix > namelen)1034return0;10351036if(fspathncmp(pattern, name, prefix))1037return0;1038 pattern += prefix;1039 patternlen -= prefix;1040 name += prefix;1041 namelen -= prefix;10421043/*1044 * If the whole pattern did not have a wildcard,1045 * then our prefix match is all we need; we1046 * do not need to call fnmatch at all.1047 */1048if(!patternlen && !namelen)1049return1;1050}10511052returnfnmatch_icase_mem(pattern, patternlen,1053 name, namelen,1054 WM_PATHNAME) ==0;1055}10561057/*1058 * Scan the given exclude list in reverse to see whether pathname1059 * should be ignored. The first match (i.e. the last on the list), if1060 * any, determines the fate. Returns the exclude_list element which1061 * matched, or NULL for undecided.1062 */1063static struct exclude *last_exclude_matching_from_list(const char*pathname,1064int pathlen,1065const char*basename,1066int*dtype,1067struct exclude_list *el,1068struct index_state *istate)1069{1070struct exclude *exc = NULL;/* undecided */1071int i;10721073if(!el->nr)1074return NULL;/* undefined */10751076for(i = el->nr -1;0<= i; i--) {1077struct exclude *x = el->excludes[i];1078const char*exclude = x->pattern;1079int prefix = x->nowildcardlen;10801081if(x->flags & EXC_FLAG_MUSTBEDIR) {1082if(*dtype == DT_UNKNOWN)1083*dtype =get_dtype(NULL, istate, pathname, pathlen);1084if(*dtype != DT_DIR)1085continue;1086}10871088if(x->flags & EXC_FLAG_NODIR) {1089if(match_basename(basename,1090 pathlen - (basename - pathname),1091 exclude, prefix, x->patternlen,1092 x->flags)) {1093 exc = x;1094break;1095}1096continue;1097}10981099assert(x->baselen ==0|| x->base[x->baselen -1] =='/');1100if(match_pathname(pathname, pathlen,1101 x->base, x->baselen ? x->baselen -1:0,1102 exclude, prefix, x->patternlen, x->flags)) {1103 exc = x;1104break;1105}1106}1107return exc;1108}11091110/*1111 * Scan the list and let the last match determine the fate.1112 * Return 1 for exclude, 0 for include and -1 for undecided.1113 */1114intis_excluded_from_list(const char*pathname,1115int pathlen,const char*basename,int*dtype,1116struct exclude_list *el,struct index_state *istate)1117{1118struct exclude *exclude;1119 exclude =last_exclude_matching_from_list(pathname, pathlen, basename,1120 dtype, el, istate);1121if(exclude)1122return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1123return-1;/* undecided */1124}11251126static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,1127struct index_state *istate,1128const char*pathname,int pathlen,const char*basename,1129int*dtype_p)1130{1131int i, j;1132struct exclude_list_group *group;1133struct exclude *exclude;1134for(i = EXC_CMDL; i <= EXC_FILE; i++) {1135 group = &dir->exclude_list_group[i];1136for(j = group->nr -1; j >=0; j--) {1137 exclude =last_exclude_matching_from_list(1138 pathname, pathlen, basename, dtype_p,1139&group->el[j], istate);1140if(exclude)1141return exclude;1142}1143}1144return NULL;1145}11461147/*1148 * Loads the per-directory exclude list for the substring of base1149 * which has a char length of baselen.1150 */1151static voidprep_exclude(struct dir_struct *dir,1152struct index_state *istate,1153const char*base,int baselen)1154{1155struct exclude_list_group *group;1156struct exclude_list *el;1157struct exclude_stack *stk = NULL;1158struct untracked_cache_dir *untracked;1159int current;11601161 group = &dir->exclude_list_group[EXC_DIRS];11621163/*1164 * Pop the exclude lists from the EXCL_DIRS exclude_list_group1165 * which originate from directories not in the prefix of the1166 * path being checked.1167 */1168while((stk = dir->exclude_stack) != NULL) {1169if(stk->baselen <= baselen &&1170!strncmp(dir->basebuf.buf, base, stk->baselen))1171break;1172 el = &group->el[dir->exclude_stack->exclude_ix];1173 dir->exclude_stack = stk->prev;1174 dir->exclude = NULL;1175free((char*)el->src);/* see strbuf_detach() below */1176clear_exclude_list(el);1177free(stk);1178 group->nr--;1179}11801181/* Skip traversing into sub directories if the parent is excluded */1182if(dir->exclude)1183return;11841185/*1186 * Lazy initialization. All call sites currently just1187 * memset(dir, 0, sizeof(*dir)) before use. Changing all of1188 * them seems lots of work for little benefit.1189 */1190if(!dir->basebuf.buf)1191strbuf_init(&dir->basebuf, PATH_MAX);11921193/* Read from the parent directories and push them down. */1194 current = stk ? stk->baselen : -1;1195strbuf_setlen(&dir->basebuf, current <0?0: current);1196if(dir->untracked)1197 untracked = stk ? stk->ucd : dir->untracked->root;1198else1199 untracked = NULL;12001201while(current < baselen) {1202const char*cp;1203struct oid_stat oid_stat;12041205 stk =xcalloc(1,sizeof(*stk));1206if(current <0) {1207 cp = base;1208 current =0;1209}else{1210 cp =strchr(base + current +1,'/');1211if(!cp)1212die("oops in prep_exclude");1213 cp++;1214 untracked =1215lookup_untracked(dir->untracked, untracked,1216 base + current,1217 cp - base - current);1218}1219 stk->prev = dir->exclude_stack;1220 stk->baselen = cp - base;1221 stk->exclude_ix = group->nr;1222 stk->ucd = untracked;1223 el =add_exclude_list(dir, EXC_DIRS, NULL);1224strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1225assert(stk->baselen == dir->basebuf.len);12261227/* Abort if the directory is excluded */1228if(stk->baselen) {1229int dt = DT_DIR;1230 dir->basebuf.buf[stk->baselen -1] =0;1231 dir->exclude =last_exclude_matching_from_lists(dir,1232 istate,1233 dir->basebuf.buf, stk->baselen -1,1234 dir->basebuf.buf + current, &dt);1235 dir->basebuf.buf[stk->baselen -1] ='/';1236if(dir->exclude &&1237 dir->exclude->flags & EXC_FLAG_NEGATIVE)1238 dir->exclude = NULL;1239if(dir->exclude) {1240 dir->exclude_stack = stk;1241return;1242}1243}12441245/* Try to read per-directory file */1246oidclr(&oid_stat.oid);1247 oid_stat.valid =0;1248if(dir->exclude_per_dir &&1249/*1250 * If we know that no files have been added in1251 * this directory (i.e. valid_cached_dir() has1252 * been executed and set untracked->valid) ..1253 */1254(!untracked || !untracked->valid ||1255/*1256 * .. and .gitignore does not exist before1257 * (i.e. null exclude_oid). Then we can skip1258 * loading .gitignore, which would result in1259 * ENOENT anyway.1260 */1261!is_null_oid(&untracked->exclude_oid))) {1262/*1263 * dir->basebuf gets reused by the traversal, but we1264 * need fname to remain unchanged to ensure the src1265 * member of each struct exclude correctly1266 * back-references its source file. Other invocations1267 * of add_exclude_list provide stable strings, so we1268 * strbuf_detach() and free() here in the caller.1269 */1270struct strbuf sb = STRBUF_INIT;1271strbuf_addbuf(&sb, &dir->basebuf);1272strbuf_addstr(&sb, dir->exclude_per_dir);1273 el->src =strbuf_detach(&sb, NULL);1274add_excludes(el->src, el->src, stk->baselen, el, istate,1275 untracked ? &oid_stat : NULL);1276}1277/*1278 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1279 * will first be called in valid_cached_dir() then maybe many1280 * times more in last_exclude_matching(). When the cache is1281 * used, last_exclude_matching() will not be called and1282 * reading .gitignore content will be a waste.1283 *1284 * So when it's called by valid_cached_dir() and we can get1285 * .gitignore SHA-1 from the index (i.e. .gitignore is not1286 * modified on work tree), we could delay reading the1287 * .gitignore content until we absolutely need it in1288 * last_exclude_matching(). Be careful about ignore rule1289 * order, though, if you do that.1290 */1291if(untracked &&1292!oideq(&oid_stat.oid, &untracked->exclude_oid)) {1293invalidate_gitignore(dir->untracked, untracked);1294oidcpy(&untracked->exclude_oid, &oid_stat.oid);1295}1296 dir->exclude_stack = stk;1297 current = stk->baselen;1298}1299strbuf_setlen(&dir->basebuf, baselen);1300}13011302/*1303 * Loads the exclude lists for the directory containing pathname, then1304 * scans all exclude lists to determine whether pathname is excluded.1305 * Returns the exclude_list element which matched, or NULL for1306 * undecided.1307 */1308struct exclude *last_exclude_matching(struct dir_struct *dir,1309struct index_state *istate,1310const char*pathname,1311int*dtype_p)1312{1313int pathlen =strlen(pathname);1314const char*basename =strrchr(pathname,'/');1315 basename = (basename) ? basename+1: pathname;13161317prep_exclude(dir, istate, pathname, basename-pathname);13181319if(dir->exclude)1320return dir->exclude;13211322returnlast_exclude_matching_from_lists(dir, istate, pathname, pathlen,1323 basename, dtype_p);1324}13251326/*1327 * Loads the exclude lists for the directory containing pathname, then1328 * scans all exclude lists to determine whether pathname is excluded.1329 * Returns 1 if true, otherwise 0.1330 */1331intis_excluded(struct dir_struct *dir,struct index_state *istate,1332const char*pathname,int*dtype_p)1333{1334struct exclude *exclude =1335last_exclude_matching(dir, istate, pathname, dtype_p);1336if(exclude)1337return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1338return0;1339}13401341static struct dir_entry *dir_entry_new(const char*pathname,int len)1342{1343struct dir_entry *ent;13441345FLEX_ALLOC_MEM(ent, name, pathname, len);1346 ent->len = len;1347return ent;1348}13491350static struct dir_entry *dir_add_name(struct dir_struct *dir,1351struct index_state *istate,1352const char*pathname,int len)1353{1354if(index_file_exists(istate, pathname, len, ignore_case))1355return NULL;13561357ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1358return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1359}13601361struct dir_entry *dir_add_ignored(struct dir_struct *dir,1362struct index_state *istate,1363const char*pathname,int len)1364{1365if(!index_name_is_other(istate, pathname, len))1366return NULL;13671368ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1369return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1370}13711372enum exist_status {1373 index_nonexistent =0,1374 index_directory,1375 index_gitdir1376};13771378/*1379 * Do not use the alphabetically sorted index to look up1380 * the directory name; instead, use the case insensitive1381 * directory hash.1382 */1383static enum exist_status directory_exists_in_index_icase(struct index_state *istate,1384const char*dirname,int len)1385{1386struct cache_entry *ce;13871388if(index_dir_exists(istate, dirname, len))1389return index_directory;13901391 ce =index_file_exists(istate, dirname, len, ignore_case);1392if(ce &&S_ISGITLINK(ce->ce_mode))1393return index_gitdir;13941395return index_nonexistent;1396}13971398/*1399 * The index sorts alphabetically by entry name, which1400 * means that a gitlink sorts as '\0' at the end, while1401 * a directory (which is defined not as an entry, but as1402 * the files it contains) will sort with the '/' at the1403 * end.1404 */1405static enum exist_status directory_exists_in_index(struct index_state *istate,1406const char*dirname,int len)1407{1408int pos;14091410if(ignore_case)1411returndirectory_exists_in_index_icase(istate, dirname, len);14121413 pos =index_name_pos(istate, dirname, len);1414if(pos <0)1415 pos = -pos-1;1416while(pos < istate->cache_nr) {1417const struct cache_entry *ce = istate->cache[pos++];1418unsigned char endchar;14191420if(strncmp(ce->name, dirname, len))1421break;1422 endchar = ce->name[len];1423if(endchar >'/')1424break;1425if(endchar =='/')1426return index_directory;1427if(!endchar &&S_ISGITLINK(ce->ce_mode))1428return index_gitdir;1429}1430return index_nonexistent;1431}14321433/*1434 * When we find a directory when traversing the filesystem, we1435 * have three distinct cases:1436 *1437 * - ignore it1438 * - see it as a directory1439 * - recurse into it1440 *1441 * and which one we choose depends on a combination of existing1442 * git index contents and the flags passed into the directory1443 * traversal routine.1444 *1445 * Case 1: If we *already* have entries in the index under that1446 * directory name, we always recurse into the directory to see1447 * all the files.1448 *1449 * Case 2: If we *already* have that directory name as a gitlink,1450 * we always continue to see it as a gitlink, regardless of whether1451 * there is an actual git directory there or not (it might not1452 * be checked out as a subproject!)1453 *1454 * Case 3: if we didn't have it in the index previously, we1455 * have a few sub-cases:1456 *1457 * (a) if "show_other_directories" is true, we show it as1458 * just a directory, unless "hide_empty_directories" is1459 * also true, in which case we need to check if it contains any1460 * untracked and / or ignored files.1461 * (b) if it looks like a git directory, and we don't have1462 * 'no_gitlinks' set we treat it as a gitlink, and show it1463 * as a directory.1464 * (c) otherwise, we recurse into it.1465 */1466static enum path_treatment treat_directory(struct dir_struct *dir,1467struct index_state *istate,1468struct untracked_cache_dir *untracked,1469const char*dirname,int len,int baselen,int exclude,1470const struct pathspec *pathspec)1471{1472/* The "len-1" is to strip the final '/' */1473switch(directory_exists_in_index(istate, dirname, len-1)) {1474case index_directory:1475return path_recurse;14761477case index_gitdir:1478return path_none;14791480case index_nonexistent:1481if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1482break;1483if(exclude &&1484(dir->flags & DIR_SHOW_IGNORED_TOO) &&1485(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {14861487/*1488 * This is an excluded directory and we are1489 * showing ignored paths that match an exclude1490 * pattern. (e.g. show directory as ignored1491 * only if it matches an exclude pattern).1492 * This path will either be 'path_excluded`1493 * (if we are showing empty directories or if1494 * the directory is not empty), or will be1495 * 'path_none' (empty directory, and we are1496 * not showing empty directories).1497 */1498if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1499return path_excluded;15001501if(read_directory_recursive(dir, istate, dirname, len,1502 untracked,1,1, pathspec) == path_excluded)1503return path_excluded;15041505return path_none;1506}1507if(!(dir->flags & DIR_NO_GITLINKS)) {1508struct object_id oid;1509if(resolve_gitlink_ref(dirname,"HEAD", &oid) ==0)1510return exclude ? path_excluded : path_untracked;1511}1512return path_recurse;1513}15141515/* This is the "show_other_directories" case */15161517if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1518return exclude ? path_excluded : path_untracked;15191520 untracked =lookup_untracked(dir->untracked, untracked,1521 dirname + baselen, len - baselen);15221523/*1524 * If this is an excluded directory, then we only need to check if1525 * the directory contains any files.1526 */1527returnread_directory_recursive(dir, istate, dirname, len,1528 untracked,1, exclude, pathspec);1529}15301531/*1532 * This is an inexact early pruning of any recursive directory1533 * reading - if the path cannot possibly be in the pathspec,1534 * return true, and we'll skip it early.1535 */1536static intsimplify_away(const char*path,int pathlen,1537const struct pathspec *pathspec)1538{1539int i;15401541if(!pathspec || !pathspec->nr)1542return0;15431544GUARD_PATHSPEC(pathspec,1545 PATHSPEC_FROMTOP |1546 PATHSPEC_MAXDEPTH |1547 PATHSPEC_LITERAL |1548 PATHSPEC_GLOB |1549 PATHSPEC_ICASE |1550 PATHSPEC_EXCLUDE |1551 PATHSPEC_ATTR);15521553for(i =0; i < pathspec->nr; i++) {1554const struct pathspec_item *item = &pathspec->items[i];1555int len = item->nowildcard_len;15561557if(len > pathlen)1558 len = pathlen;1559if(!ps_strncmp(item, item->match, path, len))1560return0;1561}15621563return1;1564}15651566/*1567 * This function tells us whether an excluded path matches a1568 * list of "interesting" pathspecs. That is, whether a path matched1569 * by any of the pathspecs could possibly be ignored by excluding1570 * the specified path. This can happen if:1571 *1572 * 1. the path is mentioned explicitly in the pathspec1573 *1574 * 2. the path is a directory prefix of some element in the1575 * pathspec1576 */1577static intexclude_matches_pathspec(const char*path,int pathlen,1578const struct pathspec *pathspec)1579{1580int i;15811582if(!pathspec || !pathspec->nr)1583return0;15841585GUARD_PATHSPEC(pathspec,1586 PATHSPEC_FROMTOP |1587 PATHSPEC_MAXDEPTH |1588 PATHSPEC_LITERAL |1589 PATHSPEC_GLOB |1590 PATHSPEC_ICASE |1591 PATHSPEC_EXCLUDE);15921593for(i =0; i < pathspec->nr; i++) {1594const struct pathspec_item *item = &pathspec->items[i];1595int len = item->nowildcard_len;15961597if(len == pathlen &&1598!ps_strncmp(item, item->match, path, pathlen))1599return1;1600if(len > pathlen &&1601 item->match[pathlen] =='/'&&1602!ps_strncmp(item, item->match, path, pathlen))1603return1;1604}1605return0;1606}16071608static intget_index_dtype(struct index_state *istate,1609const char*path,int len)1610{1611int pos;1612const struct cache_entry *ce;16131614 ce =index_file_exists(istate, path, len,0);1615if(ce) {1616if(!ce_uptodate(ce))1617return DT_UNKNOWN;1618if(S_ISGITLINK(ce->ce_mode))1619return DT_DIR;1620/*1621 * Nobody actually cares about the1622 * difference between DT_LNK and DT_REG1623 */1624return DT_REG;1625}16261627/* Try to look it up as a directory */1628 pos =index_name_pos(istate, path, len);1629if(pos >=0)1630return DT_UNKNOWN;1631 pos = -pos-1;1632while(pos < istate->cache_nr) {1633 ce = istate->cache[pos++];1634if(strncmp(ce->name, path, len))1635break;1636if(ce->name[len] >'/')1637break;1638if(ce->name[len] <'/')1639continue;1640if(!ce_uptodate(ce))1641break;/* continue? */1642return DT_DIR;1643}1644return DT_UNKNOWN;1645}16461647static intget_dtype(struct dirent *de,struct index_state *istate,1648const char*path,int len)1649{1650int dtype = de ?DTYPE(de) : DT_UNKNOWN;1651struct stat st;16521653if(dtype != DT_UNKNOWN)1654return dtype;1655 dtype =get_index_dtype(istate, path, len);1656if(dtype != DT_UNKNOWN)1657return dtype;1658if(lstat(path, &st))1659return dtype;1660if(S_ISREG(st.st_mode))1661return DT_REG;1662if(S_ISDIR(st.st_mode))1663return DT_DIR;1664if(S_ISLNK(st.st_mode))1665return DT_LNK;1666return dtype;1667}16681669static enum path_treatment treat_one_path(struct dir_struct *dir,1670struct untracked_cache_dir *untracked,1671struct index_state *istate,1672struct strbuf *path,1673int baselen,1674const struct pathspec *pathspec,1675int dtype,struct dirent *de)1676{1677int exclude;1678int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);1679enum path_treatment path_treatment;16801681if(dtype == DT_UNKNOWN)1682 dtype =get_dtype(de, istate, path->buf, path->len);16831684/* Always exclude indexed files */1685if(dtype != DT_DIR && has_path_in_index)1686return path_none;16871688/*1689 * When we are looking at a directory P in the working tree,1690 * there are three cases:1691 *1692 * (1) P exists in the index. Everything inside the directory P in1693 * the working tree needs to go when P is checked out from the1694 * index.1695 *1696 * (2) P does not exist in the index, but there is P/Q in the index.1697 * We know P will stay a directory when we check out the contents1698 * of the index, but we do not know yet if there is a directory1699 * P/Q in the working tree to be killed, so we need to recurse.1700 *1701 * (3) P does not exist in the index, and there is no P/Q in the index1702 * to require P to be a directory, either. Only in this case, we1703 * know that everything inside P will not be killed without1704 * recursing.1705 */1706if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1707(dtype == DT_DIR) &&1708!has_path_in_index &&1709(directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))1710return path_none;17111712 exclude =is_excluded(dir, istate, path->buf, &dtype);17131714/*1715 * Excluded? If we don't explicitly want to show1716 * ignored files, ignore it1717 */1718if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1719return path_excluded;17201721switch(dtype) {1722default:1723return path_none;1724case DT_DIR:1725strbuf_addch(path,'/');1726 path_treatment =treat_directory(dir, istate, untracked,1727 path->buf, path->len,1728 baselen, exclude, pathspec);1729/*1730 * If 1) we only want to return directories that1731 * match an exclude pattern and 2) this directory does1732 * not match an exclude pattern but all of its1733 * contents are excluded, then indicate that we should1734 * recurse into this directory (instead of marking the1735 * directory itself as an ignored path).1736 */1737if(!exclude &&1738 path_treatment == path_excluded &&1739(dir->flags & DIR_SHOW_IGNORED_TOO) &&1740(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING))1741return path_recurse;1742return path_treatment;1743case DT_REG:1744case DT_LNK:1745return exclude ? path_excluded : path_untracked;1746}1747}17481749static enum path_treatment treat_path_fast(struct dir_struct *dir,1750struct untracked_cache_dir *untracked,1751struct cached_dir *cdir,1752struct index_state *istate,1753struct strbuf *path,1754int baselen,1755const struct pathspec *pathspec)1756{1757strbuf_setlen(path, baselen);1758if(!cdir->ucd) {1759strbuf_addstr(path, cdir->file);1760return path_untracked;1761}1762strbuf_addstr(path, cdir->ucd->name);1763/* treat_one_path() does this before it calls treat_directory() */1764strbuf_complete(path,'/');1765if(cdir->ucd->check_only)1766/*1767 * check_only is set as a result of treat_directory() getting1768 * to its bottom. Verify again the same set of directories1769 * with check_only set.1770 */1771returnread_directory_recursive(dir, istate, path->buf, path->len,1772 cdir->ucd,1,0, pathspec);1773/*1774 * We get path_recurse in the first run when1775 * directory_exists_in_index() returns index_nonexistent. We1776 * are sure that new changes in the index does not impact the1777 * outcome. Return now.1778 */1779return path_recurse;1780}17811782static enum path_treatment treat_path(struct dir_struct *dir,1783struct untracked_cache_dir *untracked,1784struct cached_dir *cdir,1785struct index_state *istate,1786struct strbuf *path,1787int baselen,1788const struct pathspec *pathspec)1789{1790int dtype;1791struct dirent *de = cdir->de;17921793if(!de)1794returntreat_path_fast(dir, untracked, cdir, istate, path,1795 baselen, pathspec);1796if(is_dot_or_dotdot(de->d_name) || !fspathcmp(de->d_name,".git"))1797return path_none;1798strbuf_setlen(path, baselen);1799strbuf_addstr(path, de->d_name);1800if(simplify_away(path->buf, path->len, pathspec))1801return path_none;18021803 dtype =DTYPE(de);1804returntreat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);1805}18061807static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1808{1809if(!dir)1810return;1811ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1812 dir->untracked_alloc);1813 dir->untracked[dir->untracked_nr++] =xstrdup(name);1814}18151816static intvalid_cached_dir(struct dir_struct *dir,1817struct untracked_cache_dir *untracked,1818struct index_state *istate,1819struct strbuf *path,1820int check_only)1821{1822struct stat st;18231824if(!untracked)1825return0;18261827/*1828 * With fsmonitor, we can trust the untracked cache's valid field.1829 */1830refresh_fsmonitor(istate);1831if(!(dir->untracked->use_fsmonitor && untracked->valid)) {1832if(lstat(path->len ? path->buf :".", &st)) {1833memset(&untracked->stat_data,0,sizeof(untracked->stat_data));1834return0;1835}1836if(!untracked->valid ||1837match_stat_data_racy(istate, &untracked->stat_data, &st)) {1838fill_stat_data(&untracked->stat_data, &st);1839return0;1840}1841}18421843if(untracked->check_only != !!check_only)1844return0;18451846/*1847 * prep_exclude will be called eventually on this directory,1848 * but it's called much later in last_exclude_matching(). We1849 * need it now to determine the validity of the cache for this1850 * path. The next calls will be nearly no-op, the way1851 * prep_exclude() is designed.1852 */1853if(path->len && path->buf[path->len -1] !='/') {1854strbuf_addch(path,'/');1855prep_exclude(dir, istate, path->buf, path->len);1856strbuf_setlen(path, path->len -1);1857}else1858prep_exclude(dir, istate, path->buf, path->len);18591860/* hopefully prep_exclude() haven't invalidated this entry... */1861return untracked->valid;1862}18631864static intopen_cached_dir(struct cached_dir *cdir,1865struct dir_struct *dir,1866struct untracked_cache_dir *untracked,1867struct index_state *istate,1868struct strbuf *path,1869int check_only)1870{1871const char*c_path;18721873memset(cdir,0,sizeof(*cdir));1874 cdir->untracked = untracked;1875if(valid_cached_dir(dir, untracked, istate, path, check_only))1876return0;1877 c_path = path->len ? path->buf :".";1878 cdir->fdir =opendir(c_path);1879if(!cdir->fdir)1880warning_errno(_("could not open directory '%s'"), c_path);1881if(dir->untracked) {1882invalidate_directory(dir->untracked, untracked);1883 dir->untracked->dir_opened++;1884}1885if(!cdir->fdir)1886return-1;1887return0;1888}18891890static intread_cached_dir(struct cached_dir *cdir)1891{1892if(cdir->fdir) {1893 cdir->de =readdir(cdir->fdir);1894if(!cdir->de)1895return-1;1896return0;1897}1898while(cdir->nr_dirs < cdir->untracked->dirs_nr) {1899struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1900if(!d->recurse) {1901 cdir->nr_dirs++;1902continue;1903}1904 cdir->ucd = d;1905 cdir->nr_dirs++;1906return0;1907}1908 cdir->ucd = NULL;1909if(cdir->nr_files < cdir->untracked->untracked_nr) {1910struct untracked_cache_dir *d = cdir->untracked;1911 cdir->file = d->untracked[cdir->nr_files++];1912return0;1913}1914return-1;1915}19161917static voidclose_cached_dir(struct cached_dir *cdir)1918{1919if(cdir->fdir)1920closedir(cdir->fdir);1921/*1922 * We have gone through this directory and found no untracked1923 * entries. Mark it valid.1924 */1925if(cdir->untracked) {1926 cdir->untracked->valid =1;1927 cdir->untracked->recurse =1;1928}1929}19301931/*1932 * Read a directory tree. We currently ignore anything but1933 * directories, regular files and symlinks. That's because git1934 * doesn't handle them at all yet. Maybe that will change some1935 * day.1936 *1937 * Also, we ignore the name ".git" (even if it is not a directory).1938 * That likely will not change.1939 *1940 * If 'stop_at_first_file' is specified, 'path_excluded' is returned1941 * to signal that a file was found. This is the least significant value that1942 * indicates that a file was encountered that does not depend on the order of1943 * whether an untracked or exluded path was encountered first.1944 *1945 * Returns the most significant path_treatment value encountered in the scan.1946 * If 'stop_at_first_file' is specified, `path_excluded` is the most1947 * significant path_treatment value that will be returned.1948 */19491950static enum path_treatment read_directory_recursive(struct dir_struct *dir,1951struct index_state *istate,const char*base,int baselen,1952struct untracked_cache_dir *untracked,int check_only,1953int stop_at_first_file,const struct pathspec *pathspec)1954{1955struct cached_dir cdir;1956enum path_treatment state, subdir_state, dir_state = path_none;1957struct strbuf path = STRBUF_INIT;19581959strbuf_add(&path, base, baselen);19601961if(open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))1962goto out;19631964if(untracked)1965 untracked->check_only = !!check_only;19661967while(!read_cached_dir(&cdir)) {1968/* check how the file or directory should be treated */1969 state =treat_path(dir, untracked, &cdir, istate, &path,1970 baselen, pathspec);19711972if(state > dir_state)1973 dir_state = state;19741975/* recurse into subdir if instructed by treat_path */1976if((state == path_recurse) ||1977((state == path_untracked) &&1978(dir->flags & DIR_SHOW_IGNORED_TOO) &&1979(get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {1980struct untracked_cache_dir *ud;1981 ud =lookup_untracked(dir->untracked, untracked,1982 path.buf + baselen,1983 path.len - baselen);1984 subdir_state =1985read_directory_recursive(dir, istate, path.buf,1986 path.len, ud,1987 check_only, stop_at_first_file, pathspec);1988if(subdir_state > dir_state)1989 dir_state = subdir_state;1990}19911992if(check_only) {1993if(stop_at_first_file) {1994/*1995 * If stopping at first file, then1996 * signal that a file was found by1997 * returning `path_excluded`. This is1998 * to return a consistent value1999 * regardless of whether an ignored or2000 * excluded file happened to be2001 * encountered 1st.2002 *2003 * In current usage, the2004 * `stop_at_first_file` is passed when2005 * an ancestor directory has matched2006 * an exclude pattern, so any found2007 * files will be excluded.2008 */2009if(dir_state >= path_excluded) {2010 dir_state = path_excluded;2011break;2012}2013}20142015/* abort early if maximum state has been reached */2016if(dir_state == path_untracked) {2017if(cdir.fdir)2018add_untracked(untracked, path.buf + baselen);2019break;2020}2021/* skip the dir_add_* part */2022continue;2023}20242025/* add the path to the appropriate result list */2026switch(state) {2027case path_excluded:2028if(dir->flags & DIR_SHOW_IGNORED)2029dir_add_name(dir, istate, path.buf, path.len);2030else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||2031((dir->flags & DIR_COLLECT_IGNORED) &&2032exclude_matches_pathspec(path.buf, path.len,2033 pathspec)))2034dir_add_ignored(dir, istate, path.buf, path.len);2035break;20362037case path_untracked:2038if(dir->flags & DIR_SHOW_IGNORED)2039break;2040dir_add_name(dir, istate, path.buf, path.len);2041if(cdir.fdir)2042add_untracked(untracked, path.buf + baselen);2043break;20442045default:2046break;2047}2048}2049close_cached_dir(&cdir);2050 out:2051strbuf_release(&path);20522053return dir_state;2054}20552056intcmp_dir_entry(const void*p1,const void*p2)2057{2058const struct dir_entry *e1 = *(const struct dir_entry **)p1;2059const struct dir_entry *e2 = *(const struct dir_entry **)p2;20602061returnname_compare(e1->name, e1->len, e2->name, e2->len);2062}20632064/* check if *out lexically strictly contains *in */2065intcheck_dir_entry_contains(const struct dir_entry *out,const struct dir_entry *in)2066{2067return(out->len < in->len) &&2068(out->name[out->len -1] =='/') &&2069!memcmp(out->name, in->name, out->len);2070}20712072static inttreat_leading_path(struct dir_struct *dir,2073struct index_state *istate,2074const char*path,int len,2075const struct pathspec *pathspec)2076{2077struct strbuf sb = STRBUF_INIT;2078int baselen, rc =0;2079const char*cp;2080int old_flags = dir->flags;20812082while(len && path[len -1] =='/')2083 len--;2084if(!len)2085return1;2086 baselen =0;2087 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;2088while(1) {2089 cp = path + baselen + !!baselen;2090 cp =memchr(cp,'/', path + len - cp);2091if(!cp)2092 baselen = len;2093else2094 baselen = cp - path;2095strbuf_setlen(&sb,0);2096strbuf_add(&sb, path, baselen);2097if(!is_directory(sb.buf))2098break;2099if(simplify_away(sb.buf, sb.len, pathspec))2100break;2101if(treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,2102 DT_DIR, NULL) == path_none)2103break;/* do not recurse into it */2104if(len <= baselen) {2105 rc =1;2106break;/* finished checking */2107}2108}2109strbuf_release(&sb);2110 dir->flags = old_flags;2111return rc;2112}21132114static const char*get_ident_string(void)2115{2116static struct strbuf sb = STRBUF_INIT;2117struct utsname uts;21182119if(sb.len)2120return sb.buf;2121if(uname(&uts) <0)2122die_errno(_("failed to get kernel name and information"));2123strbuf_addf(&sb,"Location%s, system%s",get_git_work_tree(),2124 uts.sysname);2125return sb.buf;2126}21272128static intident_in_untracked(const struct untracked_cache *uc)2129{2130/*2131 * Previous git versions may have saved many NUL separated2132 * strings in the "ident" field, but it is insane to manage2133 * many locations, so just take care of the first one.2134 */21352136return!strcmp(uc->ident.buf,get_ident_string());2137}21382139static voidset_untracked_ident(struct untracked_cache *uc)2140{2141strbuf_reset(&uc->ident);2142strbuf_addstr(&uc->ident,get_ident_string());21432144/*2145 * This strbuf used to contain a list of NUL separated2146 * strings, so save NUL too for backward compatibility.2147 */2148strbuf_addch(&uc->ident,0);2149}21502151static voidnew_untracked_cache(struct index_state *istate)2152{2153struct untracked_cache *uc =xcalloc(1,sizeof(*uc));2154strbuf_init(&uc->ident,100);2155 uc->exclude_per_dir =".gitignore";2156/* should be the same flags used by git-status */2157 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;2158set_untracked_ident(uc);2159 istate->untracked = uc;2160 istate->cache_changed |= UNTRACKED_CHANGED;2161}21622163voidadd_untracked_cache(struct index_state *istate)2164{2165if(!istate->untracked) {2166new_untracked_cache(istate);2167}else{2168if(!ident_in_untracked(istate->untracked)) {2169free_untracked_cache(istate->untracked);2170new_untracked_cache(istate);2171}2172}2173}21742175voidremove_untracked_cache(struct index_state *istate)2176{2177if(istate->untracked) {2178free_untracked_cache(istate->untracked);2179 istate->untracked = NULL;2180 istate->cache_changed |= UNTRACKED_CHANGED;2181}2182}21832184static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,2185int base_len,2186const struct pathspec *pathspec)2187{2188struct untracked_cache_dir *root;2189static int untracked_cache_disabled = -1;21902191if(!dir->untracked)2192return NULL;2193if(untracked_cache_disabled <0)2194 untracked_cache_disabled =git_env_bool("GIT_DISABLE_UNTRACKED_CACHE",0);2195if(untracked_cache_disabled)2196return NULL;21972198/*2199 * We only support $GIT_DIR/info/exclude and core.excludesfile2200 * as the global ignore rule files. Any other additions2201 * (e.g. from command line) invalidate the cache. This2202 * condition also catches running setup_standard_excludes()2203 * before setting dir->untracked!2204 */2205if(dir->unmanaged_exclude_files)2206return NULL;22072208/*2209 * Optimize for the main use case only: whole-tree git2210 * status. More work involved in treat_leading_path() if we2211 * use cache on just a subset of the worktree. pathspec2212 * support could make the matter even worse.2213 */2214if(base_len || (pathspec && pathspec->nr))2215return NULL;22162217/* Different set of flags may produce different results */2218if(dir->flags != dir->untracked->dir_flags ||2219/*2220 * See treat_directory(), case index_nonexistent. Without2221 * this flag, we may need to also cache .git file content2222 * for the resolve_gitlink_ref() call, which we don't.2223 */2224!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||2225/* We don't support collecting ignore files */2226(dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |2227 DIR_COLLECT_IGNORED)))2228return NULL;22292230/*2231 * If we use .gitignore in the cache and now you change it to2232 * .gitexclude, everything will go wrong.2233 */2234if(dir->exclude_per_dir != dir->untracked->exclude_per_dir &&2235strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))2236return NULL;22372238/*2239 * EXC_CMDL is not considered in the cache. If people set it,2240 * skip the cache.2241 */2242if(dir->exclude_list_group[EXC_CMDL].nr)2243return NULL;22442245if(!ident_in_untracked(dir->untracked)) {2246warning(_("untracked cache is disabled on this system or location"));2247return NULL;2248}22492250if(!dir->untracked->root) {2251const int len =sizeof(*dir->untracked->root);2252 dir->untracked->root =xmalloc(len);2253memset(dir->untracked->root,0, len);2254}22552256/* Validate $GIT_DIR/info/exclude and core.excludesfile */2257 root = dir->untracked->root;2258if(!oideq(&dir->ss_info_exclude.oid,2259&dir->untracked->ss_info_exclude.oid)) {2260invalidate_gitignore(dir->untracked, root);2261 dir->untracked->ss_info_exclude = dir->ss_info_exclude;2262}2263if(!oideq(&dir->ss_excludes_file.oid,2264&dir->untracked->ss_excludes_file.oid)) {2265invalidate_gitignore(dir->untracked, root);2266 dir->untracked->ss_excludes_file = dir->ss_excludes_file;2267}22682269/* Make sure this directory is not dropped out at saving phase */2270 root->recurse =1;2271return root;2272}22732274intread_directory(struct dir_struct *dir,struct index_state *istate,2275const char*path,int len,const struct pathspec *pathspec)2276{2277struct untracked_cache_dir *untracked;22782279trace_performance_enter();22802281if(has_symlink_leading_path(path, len)) {2282trace_performance_leave("read directory %.*s", len, path);2283return dir->nr;2284}22852286 untracked =validate_untracked_cache(dir, len, pathspec);2287if(!untracked)2288/*2289 * make sure untracked cache code path is disabled,2290 * e.g. prep_exclude()2291 */2292 dir->untracked = NULL;2293if(!len ||treat_leading_path(dir, istate, path, len, pathspec))2294read_directory_recursive(dir, istate, path, len, untracked,0,0, pathspec);2295QSORT(dir->entries, dir->nr, cmp_dir_entry);2296QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);22972298/*2299 * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will2300 * also pick up untracked contents of untracked dirs; by default2301 * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.2302 */2303if((dir->flags & DIR_SHOW_IGNORED_TOO) &&2304!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {2305int i, j;23062307/* remove from dir->entries untracked contents of untracked dirs */2308for(i = j =0; j < dir->nr; j++) {2309if(i &&2310check_dir_entry_contains(dir->entries[i -1], dir->entries[j])) {2311FREE_AND_NULL(dir->entries[j]);2312}else{2313 dir->entries[i++] = dir->entries[j];2314}2315}23162317 dir->nr = i;2318}23192320trace_performance_leave("read directory %.*s", len, path);2321if(dir->untracked) {2322static int force_untracked_cache = -1;2323static struct trace_key trace_untracked_stats =TRACE_KEY_INIT(UNTRACKED_STATS);23242325if(force_untracked_cache <0)2326 force_untracked_cache =2327git_env_bool("GIT_FORCE_UNTRACKED_CACHE",0);2328trace_printf_key(&trace_untracked_stats,2329"node creation:%u\n"2330"gitignore invalidation:%u\n"2331"directory invalidation:%u\n"2332"opendir:%u\n",2333 dir->untracked->dir_created,2334 dir->untracked->gitignore_invalidated,2335 dir->untracked->dir_invalidated,2336 dir->untracked->dir_opened);2337if(force_untracked_cache &&2338 dir->untracked == istate->untracked &&2339(dir->untracked->dir_opened ||2340 dir->untracked->gitignore_invalidated ||2341 dir->untracked->dir_invalidated))2342 istate->cache_changed |= UNTRACKED_CHANGED;2343if(dir->untracked != istate->untracked) {2344FREE_AND_NULL(dir->untracked);2345}2346}2347return dir->nr;2348}23492350intfile_exists(const char*f)2351{2352struct stat sb;2353returnlstat(f, &sb) ==0;2354}23552356static intcmp_icase(char a,char b)2357{2358if(a == b)2359return0;2360if(ignore_case)2361returntoupper(a) -toupper(b);2362return a - b;2363}23642365/*2366 * Given two normalized paths (a trailing slash is ok), if subdir is2367 * outside dir, return -1. Otherwise return the offset in subdir that2368 * can be used as relative path to dir.2369 */2370intdir_inside_of(const char*subdir,const char*dir)2371{2372int offset =0;23732374assert(dir && subdir && *dir && *subdir);23752376while(*dir && *subdir && !cmp_icase(*dir, *subdir)) {2377 dir++;2378 subdir++;2379 offset++;2380}23812382/* hel[p]/me vs hel[l]/yeah */2383if(*dir && *subdir)2384return-1;23852386if(!*subdir)2387return!*dir ? offset : -1;/* same dir */23882389/* foo/[b]ar vs foo/[] */2390if(is_dir_sep(dir[-1]))2391returnis_dir_sep(subdir[-1]) ? offset : -1;23922393/* foo[/]bar vs foo[] */2394returnis_dir_sep(*subdir) ? offset +1: -1;2395}23962397intis_inside_dir(const char*dir)2398{2399char*cwd;2400int rc;24012402if(!dir)2403return0;24042405 cwd =xgetcwd();2406 rc = (dir_inside_of(cwd, dir) >=0);2407free(cwd);2408return rc;2409}24102411intis_empty_dir(const char*path)2412{2413DIR*dir =opendir(path);2414struct dirent *e;2415int ret =1;24162417if(!dir)2418return0;24192420while((e =readdir(dir)) != NULL)2421if(!is_dot_or_dotdot(e->d_name)) {2422 ret =0;2423break;2424}24252426closedir(dir);2427return ret;2428}24292430static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)2431{2432DIR*dir;2433struct dirent *e;2434int ret =0, original_len = path->len, len, kept_down =0;2435int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2436int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2437struct object_id submodule_head;24382439if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2440!resolve_gitlink_ref(path->buf,"HEAD", &submodule_head)) {2441/* Do not descend and nuke a nested git work tree. */2442if(kept_up)2443*kept_up =1;2444return0;2445}24462447 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2448 dir =opendir(path->buf);2449if(!dir) {2450if(errno == ENOENT)2451return keep_toplevel ? -1:0;2452else if(errno == EACCES && !keep_toplevel)2453/*2454 * An empty dir could be removable even if it2455 * is unreadable:2456 */2457returnrmdir(path->buf);2458else2459return-1;2460}2461strbuf_complete(path,'/');24622463 len = path->len;2464while((e =readdir(dir)) != NULL) {2465struct stat st;2466if(is_dot_or_dotdot(e->d_name))2467continue;24682469strbuf_setlen(path, len);2470strbuf_addstr(path, e->d_name);2471if(lstat(path->buf, &st)) {2472if(errno == ENOENT)2473/*2474 * file disappeared, which is what we2475 * wanted anyway2476 */2477continue;2478/* fall thru */2479}else if(S_ISDIR(st.st_mode)) {2480if(!remove_dir_recurse(path, flag, &kept_down))2481continue;/* happy */2482}else if(!only_empty &&2483(!unlink(path->buf) || errno == ENOENT)) {2484continue;/* happy, too */2485}24862487/* path too long, stat fails, or non-directory still exists */2488 ret = -1;2489break;2490}2491closedir(dir);24922493strbuf_setlen(path, original_len);2494if(!ret && !keep_toplevel && !kept_down)2495 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;2496else if(kept_up)2497/*2498 * report the uplevel that it is not an error that we2499 * did not rmdir() our directory.2500 */2501*kept_up = !ret;2502return ret;2503}25042505intremove_dir_recursively(struct strbuf *path,int flag)2506{2507returnremove_dir_recurse(path, flag, NULL);2508}25092510staticGIT_PATH_FUNC(git_path_info_exclude,"info/exclude")25112512voidsetup_standard_excludes(struct dir_struct *dir)2513{2514 dir->exclude_per_dir =".gitignore";25152516/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */2517if(!excludes_file)2518 excludes_file =xdg_config_home("ignore");2519if(excludes_file && !access_or_warn(excludes_file, R_OK,0))2520add_excludes_from_file_1(dir, excludes_file,2521 dir->untracked ? &dir->ss_excludes_file : NULL);25222523/* per repository user preference */2524if(startup_info->have_repository) {2525const char*path =git_path_info_exclude();2526if(!access_or_warn(path, R_OK,0))2527add_excludes_from_file_1(dir, path,2528 dir->untracked ? &dir->ss_info_exclude : NULL);2529}2530}25312532intremove_path(const char*name)2533{2534char*slash;25352536if(unlink(name) && !is_missing_file_error(errno))2537return-1;25382539 slash =strrchr(name,'/');2540if(slash) {2541char*dirs =xstrdup(name);2542 slash = dirs + (slash - name);2543do{2544*slash ='\0';2545}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));2546free(dirs);2547}2548return0;2549}25502551/*2552 * Frees memory within dir which was allocated for exclude lists and2553 * the exclude_stack. Does not free dir itself.2554 */2555voidclear_directory(struct dir_struct *dir)2556{2557int i, j;2558struct exclude_list_group *group;2559struct exclude_list *el;2560struct exclude_stack *stk;25612562for(i = EXC_CMDL; i <= EXC_FILE; i++) {2563 group = &dir->exclude_list_group[i];2564for(j =0; j < group->nr; j++) {2565 el = &group->el[j];2566if(i == EXC_DIRS)2567free((char*)el->src);2568clear_exclude_list(el);2569}2570free(group->el);2571}25722573 stk = dir->exclude_stack;2574while(stk) {2575struct exclude_stack *prev = stk->prev;2576free(stk);2577 stk = prev;2578}2579strbuf_release(&dir->basebuf);2580}25812582struct ondisk_untracked_cache {2583struct stat_data info_exclude_stat;2584struct stat_data excludes_file_stat;2585uint32_t dir_flags;2586unsigned char info_exclude_sha1[20];2587unsigned char excludes_file_sha1[20];2588char exclude_per_dir[FLEX_ARRAY];2589};25902591#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)2592#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)25932594struct write_data {2595int index;/* number of written untracked_cache_dir */2596struct ewah_bitmap *check_only;/* from untracked_cache_dir */2597struct ewah_bitmap *valid;/* from untracked_cache_dir */2598struct ewah_bitmap *sha1_valid;/* set if exclude_sha1 is not null */2599struct strbuf out;2600struct strbuf sb_stat;2601struct strbuf sb_sha1;2602};26032604static voidstat_data_to_disk(struct stat_data *to,const struct stat_data *from)2605{2606 to->sd_ctime.sec =htonl(from->sd_ctime.sec);2607 to->sd_ctime.nsec =htonl(from->sd_ctime.nsec);2608 to->sd_mtime.sec =htonl(from->sd_mtime.sec);2609 to->sd_mtime.nsec =htonl(from->sd_mtime.nsec);2610 to->sd_dev =htonl(from->sd_dev);2611 to->sd_ino =htonl(from->sd_ino);2612 to->sd_uid =htonl(from->sd_uid);2613 to->sd_gid =htonl(from->sd_gid);2614 to->sd_size =htonl(from->sd_size);2615}26162617static voidwrite_one_dir(struct untracked_cache_dir *untracked,2618struct write_data *wd)2619{2620struct stat_data stat_data;2621struct strbuf *out = &wd->out;2622unsigned char intbuf[16];2623unsigned int intlen, value;2624int i = wd->index++;26252626/*2627 * untracked_nr should be reset whenever valid is clear, but2628 * for safety..2629 */2630if(!untracked->valid) {2631 untracked->untracked_nr =0;2632 untracked->check_only =0;2633}26342635if(untracked->check_only)2636ewah_set(wd->check_only, i);2637if(untracked->valid) {2638ewah_set(wd->valid, i);2639stat_data_to_disk(&stat_data, &untracked->stat_data);2640strbuf_add(&wd->sb_stat, &stat_data,sizeof(stat_data));2641}2642if(!is_null_oid(&untracked->exclude_oid)) {2643ewah_set(wd->sha1_valid, i);2644strbuf_add(&wd->sb_sha1, untracked->exclude_oid.hash,2645 the_hash_algo->rawsz);2646}26472648 intlen =encode_varint(untracked->untracked_nr, intbuf);2649strbuf_add(out, intbuf, intlen);26502651/* skip non-recurse directories */2652for(i =0, value =0; i < untracked->dirs_nr; i++)2653if(untracked->dirs[i]->recurse)2654 value++;2655 intlen =encode_varint(value, intbuf);2656strbuf_add(out, intbuf, intlen);26572658strbuf_add(out, untracked->name,strlen(untracked->name) +1);26592660for(i =0; i < untracked->untracked_nr; i++)2661strbuf_add(out, untracked->untracked[i],2662strlen(untracked->untracked[i]) +1);26632664for(i =0; i < untracked->dirs_nr; i++)2665if(untracked->dirs[i]->recurse)2666write_one_dir(untracked->dirs[i], wd);2667}26682669voidwrite_untracked_extension(struct strbuf *out,struct untracked_cache *untracked)2670{2671struct ondisk_untracked_cache *ouc;2672struct write_data wd;2673unsigned char varbuf[16];2674int varint_len;2675size_t len =strlen(untracked->exclude_per_dir);26762677FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);2678stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2679stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2680hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);2681hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);2682 ouc->dir_flags =htonl(untracked->dir_flags);26832684 varint_len =encode_varint(untracked->ident.len, varbuf);2685strbuf_add(out, varbuf, varint_len);2686strbuf_addbuf(out, &untracked->ident);26872688strbuf_add(out, ouc,ouc_size(len));2689FREE_AND_NULL(ouc);26902691if(!untracked->root) {2692 varint_len =encode_varint(0, varbuf);2693strbuf_add(out, varbuf, varint_len);2694return;2695}26962697 wd.index =0;2698 wd.check_only =ewah_new();2699 wd.valid =ewah_new();2700 wd.sha1_valid =ewah_new();2701strbuf_init(&wd.out,1024);2702strbuf_init(&wd.sb_stat,1024);2703strbuf_init(&wd.sb_sha1,1024);2704write_one_dir(untracked->root, &wd);27052706 varint_len =encode_varint(wd.index, varbuf);2707strbuf_add(out, varbuf, varint_len);2708strbuf_addbuf(out, &wd.out);2709ewah_serialize_strbuf(wd.valid, out);2710ewah_serialize_strbuf(wd.check_only, out);2711ewah_serialize_strbuf(wd.sha1_valid, out);2712strbuf_addbuf(out, &wd.sb_stat);2713strbuf_addbuf(out, &wd.sb_sha1);2714strbuf_addch(out,'\0');/* safe guard for string lists */27152716ewah_free(wd.valid);2717ewah_free(wd.check_only);2718ewah_free(wd.sha1_valid);2719strbuf_release(&wd.out);2720strbuf_release(&wd.sb_stat);2721strbuf_release(&wd.sb_sha1);2722}27232724static voidfree_untracked(struct untracked_cache_dir *ucd)2725{2726int i;2727if(!ucd)2728return;2729for(i =0; i < ucd->dirs_nr; i++)2730free_untracked(ucd->dirs[i]);2731for(i =0; i < ucd->untracked_nr; i++)2732free(ucd->untracked[i]);2733free(ucd->untracked);2734free(ucd->dirs);2735free(ucd);2736}27372738voidfree_untracked_cache(struct untracked_cache *uc)2739{2740if(uc)2741free_untracked(uc->root);2742free(uc);2743}27442745struct read_data {2746int index;2747struct untracked_cache_dir **ucd;2748struct ewah_bitmap *check_only;2749struct ewah_bitmap *valid;2750struct ewah_bitmap *sha1_valid;2751const unsigned char*data;2752const unsigned char*end;2753};27542755static voidstat_data_from_disk(struct stat_data *to,const unsigned char*data)2756{2757memcpy(to, data,sizeof(*to));2758 to->sd_ctime.sec =ntohl(to->sd_ctime.sec);2759 to->sd_ctime.nsec =ntohl(to->sd_ctime.nsec);2760 to->sd_mtime.sec =ntohl(to->sd_mtime.sec);2761 to->sd_mtime.nsec =ntohl(to->sd_mtime.nsec);2762 to->sd_dev =ntohl(to->sd_dev);2763 to->sd_ino =ntohl(to->sd_ino);2764 to->sd_uid =ntohl(to->sd_uid);2765 to->sd_gid =ntohl(to->sd_gid);2766 to->sd_size =ntohl(to->sd_size);2767}27682769static intread_one_dir(struct untracked_cache_dir **untracked_,2770struct read_data *rd)2771{2772struct untracked_cache_dir ud, *untracked;2773const unsigned char*next, *data = rd->data, *end = rd->end;2774unsigned int value;2775int i, len;27762777memset(&ud,0,sizeof(ud));27782779 next = data;2780 value =decode_varint(&next);2781if(next > end)2782return-1;2783 ud.recurse =1;2784 ud.untracked_alloc = value;2785 ud.untracked_nr = value;2786if(ud.untracked_nr)2787ALLOC_ARRAY(ud.untracked, ud.untracked_nr);2788 data = next;27892790 next = data;2791 ud.dirs_alloc = ud.dirs_nr =decode_varint(&next);2792if(next > end)2793return-1;2794ALLOC_ARRAY(ud.dirs, ud.dirs_nr);2795 data = next;27962797 len =strlen((const char*)data);2798 next = data + len +1;2799if(next > rd->end)2800return-1;2801*untracked_ = untracked =xmalloc(st_add(sizeof(*untracked), len));2802memcpy(untracked, &ud,sizeof(ud));2803memcpy(untracked->name, data, len +1);2804 data = next;28052806for(i =0; i < untracked->untracked_nr; i++) {2807 len =strlen((const char*)data);2808 next = data + len +1;2809if(next > rd->end)2810return-1;2811 untracked->untracked[i] =xstrdup((const char*)data);2812 data = next;2813}28142815 rd->ucd[rd->index++] = untracked;2816 rd->data = data;28172818for(i =0; i < untracked->dirs_nr; i++) {2819 len =read_one_dir(untracked->dirs + i, rd);2820if(len <0)2821return-1;2822}2823return0;2824}28252826static voidset_check_only(size_t pos,void*cb)2827{2828struct read_data *rd = cb;2829struct untracked_cache_dir *ud = rd->ucd[pos];2830 ud->check_only =1;2831}28322833static voidread_stat(size_t pos,void*cb)2834{2835struct read_data *rd = cb;2836struct untracked_cache_dir *ud = rd->ucd[pos];2837if(rd->data +sizeof(struct stat_data) > rd->end) {2838 rd->data = rd->end +1;2839return;2840}2841stat_data_from_disk(&ud->stat_data, rd->data);2842 rd->data +=sizeof(struct stat_data);2843 ud->valid =1;2844}28452846static voidread_oid(size_t pos,void*cb)2847{2848struct read_data *rd = cb;2849struct untracked_cache_dir *ud = rd->ucd[pos];2850if(rd->data + the_hash_algo->rawsz > rd->end) {2851 rd->data = rd->end +1;2852return;2853}2854hashcpy(ud->exclude_oid.hash, rd->data);2855 rd->data += the_hash_algo->rawsz;2856}28572858static voidload_oid_stat(struct oid_stat *oid_stat,const unsigned char*data,2859const unsigned char*sha1)2860{2861stat_data_from_disk(&oid_stat->stat, data);2862hashcpy(oid_stat->oid.hash, sha1);2863 oid_stat->valid =1;2864}28652866struct untracked_cache *read_untracked_extension(const void*data,unsigned long sz)2867{2868struct untracked_cache *uc;2869struct read_data rd;2870const unsigned char*next = data, *end = (const unsigned char*)data + sz;2871const char*ident;2872int ident_len;2873 ssize_t len;2874const char*exclude_per_dir;28752876if(sz <=1|| end[-1] !='\0')2877return NULL;2878 end--;28792880 ident_len =decode_varint(&next);2881if(next + ident_len > end)2882return NULL;2883 ident = (const char*)next;2884 next += ident_len;28852886if(next +ouc_size(0) > end)2887return NULL;28882889 uc =xcalloc(1,sizeof(*uc));2890strbuf_init(&uc->ident, ident_len);2891strbuf_add(&uc->ident, ident, ident_len);2892load_oid_stat(&uc->ss_info_exclude,2893 next +ouc_offset(info_exclude_stat),2894 next +ouc_offset(info_exclude_sha1));2895load_oid_stat(&uc->ss_excludes_file,2896 next +ouc_offset(excludes_file_stat),2897 next +ouc_offset(excludes_file_sha1));2898 uc->dir_flags =get_be32(next +ouc_offset(dir_flags));2899 exclude_per_dir = (const char*)next +ouc_offset(exclude_per_dir);2900 uc->exclude_per_dir =xstrdup(exclude_per_dir);2901/* NUL after exclude_per_dir is covered by sizeof(*ouc) */2902 next +=ouc_size(strlen(exclude_per_dir));2903if(next >= end)2904goto done2;29052906 len =decode_varint(&next);2907if(next > end || len ==0)2908goto done2;29092910 rd.valid =ewah_new();2911 rd.check_only =ewah_new();2912 rd.sha1_valid =ewah_new();2913 rd.data = next;2914 rd.end = end;2915 rd.index =0;2916ALLOC_ARRAY(rd.ucd, len);29172918if(read_one_dir(&uc->root, &rd) || rd.index != len)2919goto done;29202921 next = rd.data;2922 len =ewah_read_mmap(rd.valid, next, end - next);2923if(len <0)2924goto done;29252926 next += len;2927 len =ewah_read_mmap(rd.check_only, next, end - next);2928if(len <0)2929goto done;29302931 next += len;2932 len =ewah_read_mmap(rd.sha1_valid, next, end - next);2933if(len <0)2934goto done;29352936ewah_each_bit(rd.check_only, set_check_only, &rd);2937 rd.data = next + len;2938ewah_each_bit(rd.valid, read_stat, &rd);2939ewah_each_bit(rd.sha1_valid, read_oid, &rd);2940 next = rd.data;29412942done:2943free(rd.ucd);2944ewah_free(rd.valid);2945ewah_free(rd.check_only);2946ewah_free(rd.sha1_valid);2947done2:2948if(next != end) {2949free_untracked_cache(uc);2950 uc = NULL;2951}2952return uc;2953}29542955static voidinvalidate_one_directory(struct untracked_cache *uc,2956struct untracked_cache_dir *ucd)2957{2958 uc->dir_invalidated++;2959 ucd->valid =0;2960 ucd->untracked_nr =0;2961}29622963/*2964 * Normally when an entry is added or removed from a directory,2965 * invalidating that directory is enough. No need to touch its2966 * ancestors. When a directory is shown as "foo/bar/" in git-status2967 * however, deleting or adding an entry may have cascading effect.2968 *2969 * Say the "foo/bar/file" has become untracked, we need to tell the2970 * untracked_cache_dir of "foo" that "bar/" is not an untracked2971 * directory any more (because "bar" is managed by foo as an untracked2972 * "file").2973 *2974 * Similarly, if "foo/bar/file" moves from untracked to tracked and it2975 * was the last untracked entry in the entire "foo", we should show2976 * "foo/" instead. Which means we have to invalidate past "bar" up to2977 * "foo".2978 *2979 * This function traverses all directories from root to leaf. If there2980 * is a chance of one of the above cases happening, we invalidate back2981 * to root. Otherwise we just invalidate the leaf. There may be a more2982 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to2983 * detect these cases and avoid unnecessary invalidation, for example,2984 * checking for the untracked entry named "bar/" in "foo", but for now2985 * stick to something safe and simple.2986 */2987static intinvalidate_one_component(struct untracked_cache *uc,2988struct untracked_cache_dir *dir,2989const char*path,int len)2990{2991const char*rest =strchr(path,'/');29922993if(rest) {2994int component_len = rest - path;2995struct untracked_cache_dir *d =2996lookup_untracked(uc, dir, path, component_len);2997int ret =2998invalidate_one_component(uc, d, rest +1,2999 len - (component_len +1));3000if(ret)3001invalidate_one_directory(uc, dir);3002return ret;3003}30043005invalidate_one_directory(uc, dir);3006return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;3007}30083009voiduntracked_cache_invalidate_path(struct index_state *istate,3010const char*path,int safe_path)3011{3012if(!istate->untracked || !istate->untracked->root)3013return;3014if(!safe_path && !verify_path(path,0))3015return;3016invalidate_one_component(istate->untracked, istate->untracked->root,3017 path,strlen(path));3018}30193020voiduntracked_cache_remove_from_index(struct index_state *istate,3021const char*path)3022{3023untracked_cache_invalidate_path(istate, path,1);3024}30253026voiduntracked_cache_add_to_index(struct index_state *istate,3027const char*path)3028{3029untracked_cache_invalidate_path(istate, path,1);3030}30313032static voidconnect_wt_gitdir_in_nested(const char*sub_worktree,3033const char*sub_gitdir)3034{3035int i;3036struct repository subrepo;3037struct strbuf sub_wt = STRBUF_INIT;3038struct strbuf sub_gd = STRBUF_INIT;30393040const struct submodule *sub;30413042/* If the submodule has no working tree, we can ignore it. */3043if(repo_init(&subrepo, sub_gitdir, sub_worktree))3044return;30453046if(repo_read_index(&subrepo) <0)3047die(_("index file corrupt in repo%s"), subrepo.gitdir);30483049for(i =0; i < subrepo.index->cache_nr; i++) {3050const struct cache_entry *ce = subrepo.index->cache[i];30513052if(!S_ISGITLINK(ce->ce_mode))3053continue;30543055while(i +1< subrepo.index->cache_nr &&3056!strcmp(ce->name, subrepo.index->cache[i +1]->name))3057/*3058 * Skip entries with the same name in different stages3059 * to make sure an entry is returned only once.3060 */3061 i++;30623063 sub =submodule_from_path(&subrepo, &null_oid, ce->name);3064if(!sub || !is_submodule_active(&subrepo, ce->name))3065/* .gitmodules broken or inactive sub */3066continue;30673068strbuf_reset(&sub_wt);3069strbuf_reset(&sub_gd);3070strbuf_addf(&sub_wt,"%s/%s", sub_worktree, sub->path);3071strbuf_addf(&sub_gd,"%s/modules/%s", sub_gitdir, sub->name);30723073connect_work_tree_and_git_dir(sub_wt.buf, sub_gd.buf,1);3074}3075strbuf_release(&sub_wt);3076strbuf_release(&sub_gd);3077repo_clear(&subrepo);3078}30793080voidconnect_work_tree_and_git_dir(const char*work_tree_,3081const char*git_dir_,3082int recurse_into_nested)3083{3084struct strbuf gitfile_sb = STRBUF_INIT;3085struct strbuf cfg_sb = STRBUF_INIT;3086struct strbuf rel_path = STRBUF_INIT;3087char*git_dir, *work_tree;30883089/* Prepare .git file */3090strbuf_addf(&gitfile_sb,"%s/.git", work_tree_);3091if(safe_create_leading_directories_const(gitfile_sb.buf))3092die(_("could not create directories for%s"), gitfile_sb.buf);30933094/* Prepare config file */3095strbuf_addf(&cfg_sb,"%s/config", git_dir_);3096if(safe_create_leading_directories_const(cfg_sb.buf))3097die(_("could not create directories for%s"), cfg_sb.buf);30983099 git_dir =real_pathdup(git_dir_,1);3100 work_tree =real_pathdup(work_tree_,1);31013102/* Write .git file */3103write_file(gitfile_sb.buf,"gitdir:%s",3104relative_path(git_dir, work_tree, &rel_path));3105/* Update core.worktree setting */3106git_config_set_in_file(cfg_sb.buf,"core.worktree",3107relative_path(work_tree, git_dir, &rel_path));31083109strbuf_release(&gitfile_sb);3110strbuf_release(&cfg_sb);3111strbuf_release(&rel_path);31123113if(recurse_into_nested)3114connect_wt_gitdir_in_nested(work_tree, git_dir);31153116free(work_tree);3117free(git_dir);3118}31193120/*3121 * Migrate the git directory of the given path from old_git_dir to new_git_dir.3122 */3123voidrelocate_gitdir(const char*path,const char*old_git_dir,const char*new_git_dir)3124{3125if(rename(old_git_dir, new_git_dir) <0)3126die_errno(_("could not migrate git directory from '%s' to '%s'"),3127 old_git_dir, new_git_dir);31283129connect_work_tree_and_git_dir(path, new_git_dir,0);3130}