1/* 2 * This handles recursive filename detection with exclude 3 * files, index knowledge etc.. 4 * 5 * See Documentation/technical/api-directory-listing.txt 6 * 7 * Copyright (C) Linus Torvalds, 2005-2006 8 * Junio Hamano, 2005-2006 9 */ 10#include "cache.h" 11#include "dir.h" 12#include "refs.h" 13#include "wildmatch.h" 14#include "pathspec.h" 15#include "utf8.h" 16#include "varint.h" 17#include "ewah/ewok.h" 18 19struct path_simplify { 20 int len; 21 const char *path; 22}; 23 24/* 25 * Tells read_directory_recursive how a file or directory should be treated. 26 * Values are ordered by significance, e.g. if a directory contains both 27 * excluded and untracked files, it is listed as untracked because 28 * path_untracked > path_excluded. 29 */ 30enum path_treatment { 31 path_none = 0, 32 path_recurse, 33 path_excluded, 34 path_untracked 35}; 36 37/* 38 * Support data structure for our opendir/readdir/closedir wrappers 39 */ 40struct cached_dir { 41 DIR *fdir; 42 struct untracked_cache_dir *untracked; 43 int nr_files; 44 int nr_dirs; 45 46 struct dirent *de; 47 const char *file; 48 struct untracked_cache_dir *ucd; 49}; 50 51static enum path_treatment read_directory_recursive(struct dir_struct *dir, 52 const char *path, int len, struct untracked_cache_dir *untracked, 53 int check_only, const struct path_simplify *simplify); 54static int get_dtype(struct dirent *de, const char *path, int len); 55 56static struct trace_key trace_exclude = TRACE_KEY_INIT(EXCLUDE); 57 58/* helper string functions with support for the ignore_case flag */ 59int strcmp_icase(const char *a, const char *b) 60{ 61 return ignore_case ? strcasecmp(a, b) : strcmp(a, b); 62} 63 64int strncmp_icase(const char *a, const char *b, size_t count) 65{ 66 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); 67} 68 69int fnmatch_icase(const char *pattern, const char *string, int flags) 70{ 71 return wildmatch(pattern, string, 72 flags | (ignore_case ? WM_CASEFOLD : 0), 73 NULL); 74} 75 76int git_fnmatch(const struct pathspec_item *item, 77 const char *pattern, const char *string, 78 int prefix) 79{ 80 if (prefix > 0) { 81 if (ps_strncmp(item, pattern, string, prefix)) 82 return WM_NOMATCH; 83 pattern += prefix; 84 string += prefix; 85 } 86 if (item->flags & PATHSPEC_ONESTAR) { 87 int pattern_len = strlen(++pattern); 88 int string_len = strlen(string); 89 return string_len < pattern_len || 90 ps_strcmp(item, pattern, 91 string + string_len - pattern_len); 92 } 93 if (item->magic & PATHSPEC_GLOB) 94 return wildmatch(pattern, string, 95 WM_PATHNAME | 96 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0), 97 NULL); 98 else 99 /* wildmatch has not learned no FNM_PATHNAME mode yet */ 100 return wildmatch(pattern, string, 101 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0, 102 NULL); 103} 104 105static int fnmatch_icase_mem(const char *pattern, int patternlen, 106 const char *string, int stringlen, 107 int flags) 108{ 109 int match_status; 110 struct strbuf pat_buf = STRBUF_INIT; 111 struct strbuf str_buf = STRBUF_INIT; 112 const char *use_pat = pattern; 113 const char *use_str = string; 114 115 if (pattern[patternlen]) { 116 strbuf_add(&pat_buf, pattern, patternlen); 117 use_pat = pat_buf.buf; 118 } 119 if (string[stringlen]) { 120 strbuf_add(&str_buf, string, stringlen); 121 use_str = str_buf.buf; 122 } 123 124 if (ignore_case) 125 flags |= WM_CASEFOLD; 126 match_status = wildmatch(use_pat, use_str, flags, NULL); 127 128 strbuf_release(&pat_buf); 129 strbuf_release(&str_buf); 130 131 return match_status; 132} 133 134static size_t common_prefix_len(const struct pathspec *pathspec) 135{ 136 int n; 137 size_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 */ 146 GUARD_PATHSPEC(pathspec, 147 PATHSPEC_FROMTOP | 148 PATHSPEC_MAXDEPTH | 149 PATHSPEC_LITERAL | 150 PATHSPEC_GLOB | 151 PATHSPEC_ICASE | 152 PATHSPEC_EXCLUDE); 153 154 for (n = 0; n < pathspec->nr; n++) { 155 size_t i = 0, len = 0, item_len; 156 if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) 157 continue; 158 if (pathspec->items[n].magic & PATHSPEC_ICASE) 159 item_len = pathspec->items[n].prefix; 160 else 161 item_len = pathspec->items[n].nowildcard_len; 162 while (i < item_len && (n == 0 || i < max)) { 163 char c = pathspec->items[n].match[i]; 164 if (c != pathspec->items[0].match[i]) 165 break; 166 if (c == '/') 167 len = i + 1; 168 i++; 169 } 170 if (n == 0 || len < max) { 171 max = len; 172 if (!max) 173 break; 174 } 175 } 176 return max; 177} 178 179/* 180 * Returns a copy of the longest leading path common among all 181 * pathspecs. 182 */ 183char *common_prefix(const struct pathspec *pathspec) 184{ 185 unsigned long len = common_prefix_len(pathspec); 186 187 return len ? xmemdupz(pathspec->items[0].match, len) : NULL; 188} 189 190int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) 191{ 192 size_t len; 193 194 /* 195 * Calculate common prefix for the pathspec, and 196 * use that to optimize the directory walk 197 */ 198 len = common_prefix_len(pathspec); 199 200 /* Read the directory and prune it */ 201 read_directory(dir, pathspec->nr ? pathspec->_raw[0] : "", len, pathspec); 202 return len; 203} 204 205int within_depth(const char *name, int namelen, 206 int depth, int max_depth) 207{ 208 const char *cp = name, *cpe = name + namelen; 209 210 while (cp < cpe) { 211 if (*cp++ != '/') 212 continue; 213 depth++; 214 if (depth > max_depth) 215 return 0; 216 } 217 return 1; 218} 219 220#define DO_MATCH_EXCLUDE 1 221#define DO_MATCH_DIRECTORY 2 222 223/* 224 * Does 'match' match the given name? 225 * A match is found if 226 * 227 * (1) the 'match' string is leading directory of 'name', or 228 * (2) the 'match' string is a wildcard and matches 'name', or 229 * (3) the 'match' string is exactly the same as 'name'. 230 * 231 * and the return value tells which case it was. 232 * 233 * It returns 0 when there is no match. 234 */ 235static int match_pathspec_item(const struct pathspec_item *item, int prefix, 236 const char *name, int namelen, unsigned flags) 237{ 238 /* name/namelen has prefix cut off by caller */ 239 const char *match = item->match + prefix; 240 int matchlen = item->len - prefix; 241 242 /* 243 * The normal call pattern is: 244 * 1. prefix = common_prefix_len(ps); 245 * 2. prune something, or fill_directory 246 * 3. match_pathspec() 247 * 248 * 'prefix' at #1 may be shorter than the command's prefix and 249 * it's ok for #2 to match extra files. Those extras will be 250 * trimmed at #3. 251 * 252 * Suppose the pathspec is 'foo' and '../bar' running from 253 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 254 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 255 * user does not want XYZ/foo, only the "foo" part should be 256 * case-insensitive. We need to filter out XYZ/foo here. In 257 * other words, we do not trust the caller on comparing the 258 * prefix part when :(icase) is involved. We do exact 259 * comparison ourselves. 260 * 261 * Normally the caller (common_prefix_len() in fact) does 262 * _exact_ matching on name[-prefix+1..-1] and we do not need 263 * to check that part. Be defensive and check it anyway, in 264 * case common_prefix_len is changed, or a new caller is 265 * introduced that does not use common_prefix_len. 266 * 267 * If the penalty turns out too high when prefix is really 268 * long, maybe change it to 269 * strncmp(match, name, item->prefix - prefix) 270 */ 271 if (item->prefix && (item->magic & PATHSPEC_ICASE) && 272 strncmp(item->match, name - prefix, item->prefix)) 273 return 0; 274 275 /* If the match was just the prefix, we matched */ 276 if (!*match) 277 return MATCHED_RECURSIVELY; 278 279 if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 280 if (matchlen == namelen) 281 return MATCHED_EXACTLY; 282 283 if (match[matchlen-1] == '/' || name[matchlen] == '/') 284 return MATCHED_RECURSIVELY; 285 } else if ((flags & DO_MATCH_DIRECTORY) && 286 match[matchlen - 1] == '/' && 287 namelen == matchlen - 1 && 288 !ps_strncmp(item, match, name, namelen)) 289 return MATCHED_EXACTLY; 290 291 if (item->nowildcard_len < item->len && 292 !git_fnmatch(item, match, name, 293 item->nowildcard_len - prefix)) 294 return MATCHED_FNMATCH; 295 296 return 0; 297} 298 299/* 300 * Given a name and a list of pathspecs, returns the nature of the 301 * closest (i.e. most specific) match of the name to any of the 302 * pathspecs. 303 * 304 * The caller typically calls this multiple times with the same 305 * pathspec and seen[] array but with different name/namelen 306 * (e.g. entries from the index) and is interested in seeing if and 307 * how each pathspec matches all the names it calls this function 308 * with. A mark is left in the seen[] array for each pathspec element 309 * indicating the closest type of match that element achieved, so if 310 * seen[n] remains zero after multiple invocations, that means the nth 311 * pathspec did not match any names, which could indicate that the 312 * user mistyped the nth pathspec. 313 */ 314static int do_match_pathspec(const struct pathspec *ps, 315 const char *name, int namelen, 316 int prefix, char *seen, 317 unsigned flags) 318{ 319 int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; 320 321 GUARD_PATHSPEC(ps, 322 PATHSPEC_FROMTOP | 323 PATHSPEC_MAXDEPTH | 324 PATHSPEC_LITERAL | 325 PATHSPEC_GLOB | 326 PATHSPEC_ICASE | 327 PATHSPEC_EXCLUDE); 328 329 if (!ps->nr) { 330 if (!ps->recursive || 331 !(ps->magic & PATHSPEC_MAXDEPTH) || 332 ps->max_depth == -1) 333 return MATCHED_RECURSIVELY; 334 335 if (within_depth(name, namelen, 0, ps->max_depth)) 336 return MATCHED_EXACTLY; 337 else 338 return 0; 339 } 340 341 name += prefix; 342 namelen -= prefix; 343 344 for (i = ps->nr - 1; i >= 0; i--) { 345 int how; 346 347 if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 348 ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 349 continue; 350 351 if (seen && seen[i] == MATCHED_EXACTLY) 352 continue; 353 /* 354 * Make exclude patterns optional and never report 355 * "pathspec ':(exclude)foo' matches no files" 356 */ 357 if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 358 seen[i] = MATCHED_FNMATCH; 359 how = match_pathspec_item(ps->items+i, prefix, name, 360 namelen, flags); 361 if (ps->recursive && 362 (ps->magic & PATHSPEC_MAXDEPTH) && 363 ps->max_depth != -1 && 364 how && how != MATCHED_FNMATCH) { 365 int len = ps->items[i].len; 366 if (name[len] == '/') 367 len++; 368 if (within_depth(name+len, namelen-len, 0, ps->max_depth)) 369 how = MATCHED_EXACTLY; 370 else 371 how = 0; 372 } 373 if (how) { 374 if (retval < how) 375 retval = how; 376 if (seen && seen[i] < how) 377 seen[i] = how; 378 } 379 } 380 return retval; 381} 382 383int match_pathspec(const struct pathspec *ps, 384 const char *name, int namelen, 385 int prefix, char *seen, int is_dir) 386{ 387 int positive, negative; 388 unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; 389 positive = do_match_pathspec(ps, name, namelen, 390 prefix, seen, flags); 391 if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 392 return positive; 393 negative = do_match_pathspec(ps, name, namelen, 394 prefix, seen, 395 flags | DO_MATCH_EXCLUDE); 396 return negative ? 0 : positive; 397} 398 399int report_path_error(const char *ps_matched, 400 const struct pathspec *pathspec, 401 const char *prefix) 402{ 403 /* 404 * Make sure all pathspec matched; otherwise it is an error. 405 */ 406 int num, errors = 0; 407 for (num = 0; num < pathspec->nr; num++) { 408 int other, found_dup; 409 410 if (ps_matched[num]) 411 continue; 412 /* 413 * The caller might have fed identical pathspec 414 * twice. Do not barf on such a mistake. 415 * FIXME: parse_pathspec should have eliminated 416 * duplicate pathspec. 417 */ 418 for (found_dup = other = 0; 419 !found_dup && other < pathspec->nr; 420 other++) { 421 if (other == num || !ps_matched[other]) 422 continue; 423 if (!strcmp(pathspec->items[other].original, 424 pathspec->items[num].original)) 425 /* 426 * Ok, we have a match already. 427 */ 428 found_dup = 1; 429 } 430 if (found_dup) 431 continue; 432 433 error("pathspec '%s' did not match any file(s) known to git.", 434 pathspec->items[num].original); 435 errors++; 436 } 437 return errors; 438} 439 440/* 441 * Return the length of the "simple" part of a path match limiter. 442 */ 443int simple_length(const char *match) 444{ 445 int len = -1; 446 447 for (;;) { 448 unsigned char c = *match++; 449 len++; 450 if (c == '\0' || is_glob_special(c)) 451 return len; 452 } 453} 454 455int no_wildcard(const char *string) 456{ 457 return string[simple_length(string)] == '\0'; 458} 459 460void parse_exclude_pattern(const char **pattern, 461 int *patternlen, 462 int *flags, 463 int *nowildcardlen) 464{ 465 const char *p = *pattern; 466 size_t i, len; 467 468 *flags = 0; 469 if (*p == '!') { 470 *flags |= EXC_FLAG_NEGATIVE; 471 p++; 472 } 473 len = strlen(p); 474 if (len && p[len - 1] == '/') { 475 len--; 476 *flags |= EXC_FLAG_MUSTBEDIR; 477 } 478 for (i = 0; i < len; i++) { 479 if (p[i] == '/') 480 break; 481 } 482 if (i == len) 483 *flags |= EXC_FLAG_NODIR; 484 *nowildcardlen = simple_length(p); 485 /* 486 * we should have excluded the trailing slash from 'p' too, 487 * but that's one more allocation. Instead just make sure 488 * nowildcardlen does not exceed real patternlen 489 */ 490 if (*nowildcardlen > len) 491 *nowildcardlen = len; 492 if (*p == '*' && no_wildcard(p + 1)) 493 *flags |= EXC_FLAG_ENDSWITH; 494 *pattern = p; 495 *patternlen = len; 496} 497 498void add_exclude(const char *string, const char *base, 499 int baselen, struct exclude_list *el, int srcpos) 500{ 501 struct exclude *x; 502 int patternlen; 503 int flags; 504 int nowildcardlen; 505 506 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 507 if (flags & EXC_FLAG_MUSTBEDIR) { 508 char *s; 509 x = xmalloc(sizeof(*x) + patternlen + 1); 510 s = (char *)(x+1); 511 memcpy(s, string, patternlen); 512 s[patternlen] = '\0'; 513 x->pattern = s; 514 } else { 515 x = xmalloc(sizeof(*x)); 516 x->pattern = string; 517 } 518 x->patternlen = patternlen; 519 x->nowildcardlen = nowildcardlen; 520 x->base = base; 521 x->baselen = baselen; 522 x->flags = flags; 523 x->srcpos = srcpos; 524 string_list_init(&x->sticky_paths, 1); 525 ALLOC_GROW(el->excludes, el->nr + 1, el->alloc); 526 el->excludes[el->nr++] = x; 527 x->el = el; 528} 529 530static void *read_skip_worktree_file_from_index(const char *path, size_t *size, 531 struct sha1_stat *sha1_stat) 532{ 533 int pos, len; 534 unsigned long sz; 535 enum object_type type; 536 void *data; 537 538 len = strlen(path); 539 pos = cache_name_pos(path, len); 540 if (pos < 0) 541 return NULL; 542 if (!ce_skip_worktree(active_cache[pos])) 543 return NULL; 544 data = read_sha1_file(active_cache[pos]->sha1, &type, &sz); 545 if (!data || type != OBJ_BLOB) { 546 free(data); 547 return NULL; 548 } 549 *size = xsize_t(sz); 550 if (sha1_stat) { 551 memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat)); 552 hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 553 } 554 return data; 555} 556 557/* 558 * Frees memory within el which was allocated for exclude patterns and 559 * the file buffer. Does not free el itself. 560 */ 561void clear_exclude_list(struct exclude_list *el) 562{ 563 int i; 564 565 for (i = 0; i < el->nr; i++) { 566 string_list_clear(&el->excludes[i]->sticky_paths, 0); 567 free(el->excludes[i]); 568 } 569 free(el->excludes); 570 free(el->filebuf); 571 572 memset(el, 0, sizeof(*el)); 573} 574 575static void trim_trailing_spaces(char *buf) 576{ 577 char *p, *last_space = NULL; 578 579 for (p = buf; *p; p++) 580 switch (*p) { 581 case ' ': 582 if (!last_space) 583 last_space = p; 584 break; 585 case '\\': 586 p++; 587 if (!*p) 588 return; 589 /* fallthrough */ 590 default: 591 last_space = NULL; 592 } 593 594 if (last_space) 595 *last_space = '\0'; 596} 597 598/* 599 * Given a subdirectory name and "dir" of the current directory, 600 * search the subdir in "dir" and return it, or create a new one if it 601 * does not exist in "dir". 602 * 603 * If "name" has the trailing slash, it'll be excluded in the search. 604 */ 605static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 606 struct untracked_cache_dir *dir, 607 const char *name, int len) 608{ 609 int first, last; 610 struct untracked_cache_dir *d; 611 if (!dir) 612 return NULL; 613 if (len && name[len - 1] == '/') 614 len--; 615 first = 0; 616 last = dir->dirs_nr; 617 while (last > first) { 618 int cmp, next = (last + first) >> 1; 619 d = dir->dirs[next]; 620 cmp = strncmp(name, d->name, len); 621 if (!cmp && strlen(d->name) > len) 622 cmp = -1; 623 if (!cmp) 624 return d; 625 if (cmp < 0) { 626 last = next; 627 continue; 628 } 629 first = next+1; 630 } 631 632 uc->dir_created++; 633 d = xmalloc(sizeof(*d) + len + 1); 634 memset(d, 0, sizeof(*d)); 635 memcpy(d->name, name, len); 636 d->name[len] = '\0'; 637 638 ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); 639 memmove(dir->dirs + first + 1, dir->dirs + first, 640 (dir->dirs_nr - first) * sizeof(*dir->dirs)); 641 dir->dirs_nr++; 642 dir->dirs[first] = d; 643 return d; 644} 645 646static void do_invalidate_gitignore(struct untracked_cache_dir *dir) 647{ 648 int i; 649 dir->valid = 0; 650 dir->untracked_nr = 0; 651 for (i = 0; i < dir->dirs_nr; i++) 652 do_invalidate_gitignore(dir->dirs[i]); 653} 654 655static void invalidate_gitignore(struct untracked_cache *uc, 656 struct untracked_cache_dir *dir) 657{ 658 uc->gitignore_invalidated++; 659 do_invalidate_gitignore(dir); 660} 661 662static void invalidate_directory(struct untracked_cache *uc, 663 struct untracked_cache_dir *dir) 664{ 665 int i; 666 uc->dir_invalidated++; 667 dir->valid = 0; 668 dir->untracked_nr = 0; 669 for (i = 0; i < dir->dirs_nr; i++) 670 dir->dirs[i]->recurse = 0; 671} 672 673/* 674 * Given a file with name "fname", read it (either from disk, or from 675 * the index if "check_index" is non-zero), parse it and store the 676 * exclude rules in "el". 677 * 678 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 679 * stat data from disk (only valid if add_excludes returns zero). If 680 * ss_valid is non-zero, "ss" must contain good value as input. 681 */ 682static int add_excludes(const char *fname, const char *base, int baselen, 683 struct exclude_list *el, int check_index, 684 struct sha1_stat *sha1_stat) 685{ 686 struct stat st; 687 int fd, i, lineno = 1; 688 size_t size = 0; 689 char *buf, *entry; 690 691 fd = open(fname, O_RDONLY); 692 if (fd < 0 || fstat(fd, &st) < 0) { 693 if (errno != ENOENT) 694 warn_on_inaccessible(fname); 695 if (0 <= fd) 696 close(fd); 697 if (!check_index || 698 (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) 699 return -1; 700 if (size == 0) { 701 free(buf); 702 return 0; 703 } 704 if (buf[size-1] != '\n') { 705 buf = xrealloc(buf, size+1); 706 buf[size++] = '\n'; 707 } 708 } else { 709 size = xsize_t(st.st_size); 710 if (size == 0) { 711 if (sha1_stat) { 712 fill_stat_data(&sha1_stat->stat, &st); 713 hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); 714 sha1_stat->valid = 1; 715 } 716 close(fd); 717 return 0; 718 } 719 buf = xmalloc(size+1); 720 if (read_in_full(fd, buf, size) != size) { 721 free(buf); 722 close(fd); 723 return -1; 724 } 725 buf[size++] = '\n'; 726 close(fd); 727 if (sha1_stat) { 728 int pos; 729 if (sha1_stat->valid && 730 !match_stat_data_racy(&the_index, &sha1_stat->stat, &st)) 731 ; /* no content change, ss->sha1 still good */ 732 else if (check_index && 733 (pos = cache_name_pos(fname, strlen(fname))) >= 0 && 734 !ce_stage(active_cache[pos]) && 735 ce_uptodate(active_cache[pos]) && 736 !would_convert_to_git(fname)) 737 hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 738 else 739 hash_sha1_file(buf, size, "blob", sha1_stat->sha1); 740 fill_stat_data(&sha1_stat->stat, &st); 741 sha1_stat->valid = 1; 742 } 743 } 744 745 el->filebuf = buf; 746 747 if (skip_utf8_bom(&buf, size)) 748 size -= buf - el->filebuf; 749 750 entry = buf; 751 752 for (i = 0; i < size; i++) { 753 if (buf[i] == '\n') { 754 if (entry != buf + i && entry[0] != '#') { 755 buf[i - (i && buf[i-1] == '\r')] = 0; 756 trim_trailing_spaces(entry); 757 add_exclude(entry, base, baselen, el, lineno); 758 } 759 lineno++; 760 entry = buf + i + 1; 761 } 762 } 763 return 0; 764} 765 766int add_excludes_from_file_to_list(const char *fname, const char *base, 767 int baselen, struct exclude_list *el, 768 int check_index) 769{ 770 return add_excludes(fname, base, baselen, el, check_index, NULL); 771} 772 773struct exclude_list *add_exclude_list(struct dir_struct *dir, 774 int group_type, const char *src) 775{ 776 struct exclude_list *el; 777 struct exclude_list_group *group; 778 779 group = &dir->exclude_list_group[group_type]; 780 ALLOC_GROW(group->el, group->nr + 1, group->alloc); 781 el = &group->el[group->nr++]; 782 memset(el, 0, sizeof(*el)); 783 el->src = src; 784 return el; 785} 786 787/* 788 * Used to set up core.excludesfile and .git/info/exclude lists. 789 */ 790static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname, 791 struct sha1_stat *sha1_stat) 792{ 793 struct exclude_list *el; 794 /* 795 * catch setup_standard_excludes() that's called before 796 * dir->untracked is assigned. That function behaves 797 * differently when dir->untracked is non-NULL. 798 */ 799 if (!dir->untracked) 800 dir->unmanaged_exclude_files++; 801 el = add_exclude_list(dir, EXC_FILE, fname); 802 if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0) 803 die("cannot use %s as an exclude file", fname); 804} 805 806void add_excludes_from_file(struct dir_struct *dir, const char *fname) 807{ 808 dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */ 809 add_excludes_from_file_1(dir, fname, NULL); 810} 811 812int match_basename(const char *basename, int basenamelen, 813 const char *pattern, int prefix, int patternlen, 814 int flags) 815{ 816 if (prefix == patternlen) { 817 if (patternlen == basenamelen && 818 !strncmp_icase(pattern, basename, basenamelen)) 819 return 1; 820 } else if (flags & EXC_FLAG_ENDSWITH) { 821 /* "*literal" matching against "fooliteral" */ 822 if (patternlen - 1 <= basenamelen && 823 !strncmp_icase(pattern + 1, 824 basename + basenamelen - (patternlen - 1), 825 patternlen - 1)) 826 return 1; 827 } else { 828 if (fnmatch_icase_mem(pattern, patternlen, 829 basename, basenamelen, 830 0) == 0) 831 return 1; 832 } 833 return 0; 834} 835 836int match_pathname(const char *pathname, int pathlen, 837 const char *base, int baselen, 838 const char *pattern, int prefix, int patternlen, 839 int flags) 840{ 841 const char *name; 842 int namelen; 843 844 /* 845 * match with FNM_PATHNAME; the pattern has base implicitly 846 * in front of it. 847 */ 848 if (*pattern == '/') { 849 pattern++; 850 patternlen--; 851 prefix--; 852 } 853 854 /* 855 * baselen does not count the trailing slash. base[] may or 856 * may not end with a trailing slash though. 857 */ 858 if (pathlen < baselen + 1 || 859 (baselen && pathname[baselen] != '/') || 860 strncmp_icase(pathname, base, baselen)) 861 return 0; 862 863 namelen = baselen ? pathlen - baselen - 1 : pathlen; 864 name = pathname + pathlen - namelen; 865 866 if (prefix) { 867 /* 868 * if the non-wildcard part is longer than the 869 * remaining pathname, surely it cannot match. 870 */ 871 if (prefix > namelen) 872 return 0; 873 874 if (strncmp_icase(pattern, name, prefix)) 875 return 0; 876 pattern += prefix; 877 patternlen -= prefix; 878 name += prefix; 879 namelen -= prefix; 880 881 /* 882 * If the whole pattern did not have a wildcard, 883 * then our prefix match is all we need; we 884 * do not need to call fnmatch at all. 885 */ 886 if (!patternlen && (!namelen || *name == '/')) 887 return 1; 888 } 889 890 return fnmatch_icase_mem(pattern, patternlen, 891 name, namelen, 892 WM_PATHNAME) == 0; 893} 894 895static void add_sticky(struct exclude *exc, const char *pathname, int pathlen) 896{ 897 struct strbuf sb = STRBUF_INIT; 898 int i; 899 900 for (i = exc->sticky_paths.nr - 1; i >= 0; i--) { 901 const char *sticky = exc->sticky_paths.items[i].string; 902 int len = strlen(sticky); 903 904 if (pathlen < len && sticky[pathlen] == '/' && 905 !strncmp(pathname, sticky, pathlen)) 906 return; 907 } 908 909 strbuf_add(&sb, pathname, pathlen); 910 string_list_append_nodup(&exc->sticky_paths, strbuf_detach(&sb, NULL)); 911} 912 913static int match_sticky(struct exclude *exc, const char *pathname, int pathlen, int dtype) 914{ 915 int i; 916 917 for (i = exc->sticky_paths.nr - 1; i >= 0; i--) { 918 const char *sticky = exc->sticky_paths.items[i].string; 919 int len = strlen(sticky); 920 921 if (pathlen == len && dtype == DT_DIR && 922 !strncmp(pathname, sticky, len)) 923 return 1; 924 925 if (pathlen > len && pathname[len] == '/' && 926 !strncmp(pathname, sticky, len)) 927 return 1; 928 } 929 930 return 0; 931} 932 933/* 934 * Scan the given exclude list in reverse to see whether pathname 935 * should be ignored. The first match (i.e. the last on the list), if 936 * any, determines the fate. Returns the exclude_list element which 937 * matched, or NULL for undecided. 938 */ 939static struct exclude *last_exclude_matching_from_list(const char *pathname, 940 int pathlen, 941 const char *basename, 942 int *dtype, 943 struct exclude_list *el) 944{ 945 struct exclude *exc = NULL; /* undecided */ 946 int i; 947 948 if (!el->nr) 949 return NULL; /* undefined */ 950 951 trace_printf_key(&trace_exclude, "exclude: from %s\n", el->src); 952 953 for (i = el->nr - 1; 0 <= i; i--) { 954 struct exclude *x = el->excludes[i]; 955 const char *exclude = x->pattern; 956 int prefix = x->nowildcardlen; 957 958 if (x->sticky_paths.nr) { 959 if (*dtype == DT_UNKNOWN) 960 *dtype = get_dtype(NULL, pathname, pathlen); 961 if (match_sticky(x, pathname, pathlen, *dtype)) { 962 exc = x; 963 break; 964 } 965 continue; 966 } 967 968 if (x->flags & EXC_FLAG_MUSTBEDIR) { 969 if (*dtype == DT_UNKNOWN) 970 *dtype = get_dtype(NULL, pathname, pathlen); 971 if (*dtype != DT_DIR) 972 continue; 973 } 974 975 if (x->flags & EXC_FLAG_NODIR) { 976 if (match_basename(basename, 977 pathlen - (basename - pathname), 978 exclude, prefix, x->patternlen, 979 x->flags)) { 980 exc = x; 981 break; 982 } 983 continue; 984 } 985 986 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/'); 987 if (match_pathname(pathname, pathlen, 988 x->base, x->baselen ? x->baselen - 1 : 0, 989 exclude, prefix, x->patternlen, x->flags)) { 990 exc = x; 991 break; 992 } 993 } 994 995 if (!exc) { 996 trace_printf_key(&trace_exclude, "exclude: %.*s => n/a\n", 997 pathlen, pathname); 998 return NULL; 999 }10001001 trace_printf_key(&trace_exclude, "exclude: %.*s vs %s at line %d => %s%s\n",1002 pathlen, pathname, exc->pattern, exc->srcpos,1003 exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes",1004 exc->sticky_paths.nr ? " (stuck)" : "");1005 return exc;1006}10071008/*1009 * Scan the list and let the last match determine the fate.1010 * Return 1 for exclude, 0 for include and -1 for undecided.1011 */1012int is_excluded_from_list(const char *pathname,1013 int pathlen, const char *basename, int *dtype,1014 struct exclude_list *el)1015{1016 struct exclude *exclude;1017 exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);1018 if (exclude)1019 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;1020 return -1; /* undecided */1021}10221023static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,1024 const char *pathname, int pathlen, const char *basename,1025 int *dtype_p)1026{1027 int i, j;1028 struct exclude_list_group *group;1029 struct exclude *exclude;1030 for (i = EXC_CMDL; i <= EXC_FILE; i++) {1031 group = &dir->exclude_list_group[i];1032 for (j = group->nr - 1; j >= 0; j--) {1033 exclude = last_exclude_matching_from_list(1034 pathname, pathlen, basename, dtype_p,1035 &group->el[j]);1036 if (exclude)1037 return exclude;1038 }1039 }1040 return NULL;1041}10421043/*1044 * Loads the per-directory exclude list for the substring of base1045 * which has a char length of baselen.1046 */1047static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)1048{1049 struct exclude_list_group *group;1050 struct exclude_list *el;1051 struct exclude_stack *stk = NULL;1052 struct untracked_cache_dir *untracked;1053 int current;10541055 group = &dir->exclude_list_group[EXC_DIRS];10561057 /*1058 * Pop the exclude lists from the EXCL_DIRS exclude_list_group1059 * which originate from directories not in the prefix of the1060 * path being checked.1061 */1062 while ((stk = dir->exclude_stack) != NULL) {1063 if (stk->baselen <= baselen &&1064 !strncmp(dir->basebuf.buf, base, stk->baselen))1065 break;1066 el = &group->el[dir->exclude_stack->exclude_ix];1067 dir->exclude_stack = stk->prev;1068 dir->exclude = NULL;1069 free((char *)el->src); /* see strbuf_detach() below */1070 clear_exclude_list(el);1071 free(stk);1072 group->nr--;1073 }10741075 /* Skip traversing into sub directories if the parent is excluded */1076 if (dir->exclude)1077 return;10781079 /*1080 * Lazy initialization. All call sites currently just1081 * memset(dir, 0, sizeof(*dir)) before use. Changing all of1082 * them seems lots of work for little benefit.1083 */1084 if (!dir->basebuf.buf)1085 strbuf_init(&dir->basebuf, PATH_MAX);10861087 /* Read from the parent directories and push them down. */1088 current = stk ? stk->baselen : -1;1089 strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);1090 if (dir->untracked)1091 untracked = stk ? stk->ucd : dir->untracked->root;1092 else1093 untracked = NULL;10941095 while (current < baselen) {1096 const char *cp;1097 struct sha1_stat sha1_stat;10981099 stk = xcalloc(1, sizeof(*stk));1100 if (current < 0) {1101 cp = base;1102 current = 0;1103 } else {1104 cp = strchr(base + current + 1, '/');1105 if (!cp)1106 die("oops in prep_exclude");1107 cp++;1108 untracked =1109 lookup_untracked(dir->untracked, untracked,1110 base + current,1111 cp - base - current);1112 }1113 stk->prev = dir->exclude_stack;1114 stk->baselen = cp - base;1115 stk->exclude_ix = group->nr;1116 stk->ucd = untracked;1117 el = add_exclude_list(dir, EXC_DIRS, NULL);1118 strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1119 assert(stk->baselen == dir->basebuf.len);11201121 /* Abort if the directory is excluded */1122 if (stk->baselen) {1123 int dt = DT_DIR;1124 dir->basebuf.buf[stk->baselen - 1] = 0;1125 dir->exclude = last_exclude_matching_from_lists(dir,1126 dir->basebuf.buf, stk->baselen - 1,1127 dir->basebuf.buf + current, &dt);1128 dir->basebuf.buf[stk->baselen - 1] = '/';1129 if (dir->exclude &&1130 dir->exclude->flags & EXC_FLAG_NEGATIVE)1131 dir->exclude = NULL;1132 if (dir->exclude) {1133 dir->exclude_stack = stk;1134 return;1135 }1136 }11371138 /* Try to read per-directory file */1139 hashclr(sha1_stat.sha1);1140 sha1_stat.valid = 0;1141 if (dir->exclude_per_dir &&1142 /*1143 * If we know that no files have been added in1144 * this directory (i.e. valid_cached_dir() has1145 * been executed and set untracked->valid) ..1146 */1147 (!untracked || !untracked->valid ||1148 /*1149 * .. and .gitignore does not exist before1150 * (i.e. null exclude_sha1). Then we can skip1151 * loading .gitignore, which would result in1152 * ENOENT anyway.1153 */1154 !is_null_sha1(untracked->exclude_sha1))) {1155 /*1156 * dir->basebuf gets reused by the traversal, but we1157 * need fname to remain unchanged to ensure the src1158 * member of each struct exclude correctly1159 * back-references its source file. Other invocations1160 * of add_exclude_list provide stable strings, so we1161 * strbuf_detach() and free() here in the caller.1162 */1163 struct strbuf sb = STRBUF_INIT;1164 strbuf_addbuf(&sb, &dir->basebuf);1165 strbuf_addstr(&sb, dir->exclude_per_dir);1166 el->src = strbuf_detach(&sb, NULL);1167 add_excludes(el->src, el->src, stk->baselen, el, 1,1168 untracked ? &sha1_stat : NULL);1169 }1170 /*1171 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1172 * will first be called in valid_cached_dir() then maybe many1173 * times more in last_exclude_matching(). When the cache is1174 * used, last_exclude_matching() will not be called and1175 * reading .gitignore content will be a waste.1176 *1177 * So when it's called by valid_cached_dir() and we can get1178 * .gitignore SHA-1 from the index (i.e. .gitignore is not1179 * modified on work tree), we could delay reading the1180 * .gitignore content until we absolutely need it in1181 * last_exclude_matching(). Be careful about ignore rule1182 * order, though, if you do that.1183 */1184 if (untracked &&1185 hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {1186 invalidate_gitignore(dir->untracked, untracked);1187 hashcpy(untracked->exclude_sha1, sha1_stat.sha1);1188 }1189 dir->exclude_stack = stk;1190 current = stk->baselen;1191 }1192 strbuf_setlen(&dir->basebuf, baselen);1193}11941195/*1196 * Loads the exclude lists for the directory containing pathname, then1197 * scans all exclude lists to determine whether pathname is excluded.1198 * Returns the exclude_list element which matched, or NULL for1199 * undecided.1200 */1201struct exclude *last_exclude_matching(struct dir_struct *dir,1202 const char *pathname,1203 int *dtype_p)1204{1205 int pathlen = strlen(pathname);1206 const char *basename = strrchr(pathname, '/');1207 basename = (basename) ? basename+1 : pathname;12081209 prep_exclude(dir, pathname, basename-pathname);12101211 if (dir->exclude)1212 return dir->exclude;12131214 return last_exclude_matching_from_lists(dir, pathname, pathlen,1215 basename, dtype_p);1216}12171218/*1219 * Loads the exclude lists for the directory containing pathname, then1220 * scans all exclude lists to determine whether pathname is excluded.1221 * Returns 1 if true, otherwise 0.1222 */1223int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)1224{1225 struct exclude *exclude =1226 last_exclude_matching(dir, pathname, dtype_p);1227 if (exclude)1228 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;1229 return 0;1230}12311232static struct dir_entry *dir_entry_new(const char *pathname, int len)1233{1234 struct dir_entry *ent;12351236 ent = xmalloc(sizeof(*ent) + len + 1);1237 ent->len = len;1238 memcpy(ent->name, pathname, len);1239 ent->name[len] = 0;1240 return ent;1241}12421243static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)1244{1245 if (cache_file_exists(pathname, len, ignore_case))1246 return NULL;12471248 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1249 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);1250}12511252struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)1253{1254 if (!cache_name_is_other(pathname, len))1255 return NULL;12561257 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1258 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);1259}12601261enum exist_status {1262 index_nonexistent = 0,1263 index_directory,1264 index_gitdir1265};12661267/*1268 * Do not use the alphabetically sorted index to look up1269 * the directory name; instead, use the case insensitive1270 * directory hash.1271 */1272static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)1273{1274 struct cache_entry *ce;12751276 if (cache_dir_exists(dirname, len))1277 return index_directory;12781279 ce = cache_file_exists(dirname, len, ignore_case);1280 if (ce && S_ISGITLINK(ce->ce_mode))1281 return index_gitdir;12821283 return index_nonexistent;1284}12851286/*1287 * The index sorts alphabetically by entry name, which1288 * means that a gitlink sorts as '\0' at the end, while1289 * a directory (which is defined not as an entry, but as1290 * the files it contains) will sort with the '/' at the1291 * end.1292 */1293static enum exist_status directory_exists_in_index(const char *dirname, int len)1294{1295 int pos;12961297 if (ignore_case)1298 return directory_exists_in_index_icase(dirname, len);12991300 pos = cache_name_pos(dirname, len);1301 if (pos < 0)1302 pos = -pos-1;1303 while (pos < active_nr) {1304 const struct cache_entry *ce = active_cache[pos++];1305 unsigned char endchar;13061307 if (strncmp(ce->name, dirname, len))1308 break;1309 endchar = ce->name[len];1310 if (endchar > '/')1311 break;1312 if (endchar == '/')1313 return index_directory;1314 if (!endchar && S_ISGITLINK(ce->ce_mode))1315 return index_gitdir;1316 }1317 return index_nonexistent;1318}13191320/*1321 * When we find a directory when traversing the filesystem, we1322 * have three distinct cases:1323 *1324 * - ignore it1325 * - see it as a directory1326 * - recurse into it1327 *1328 * and which one we choose depends on a combination of existing1329 * git index contents and the flags passed into the directory1330 * traversal routine.1331 *1332 * Case 1: If we *already* have entries in the index under that1333 * directory name, we always recurse into the directory to see1334 * all the files.1335 *1336 * Case 2: If we *already* have that directory name as a gitlink,1337 * we always continue to see it as a gitlink, regardless of whether1338 * there is an actual git directory there or not (it might not1339 * be checked out as a subproject!)1340 *1341 * Case 3: if we didn't have it in the index previously, we1342 * have a few sub-cases:1343 *1344 * (a) if "show_other_directories" is true, we show it as1345 * just a directory, unless "hide_empty_directories" is1346 * also true, in which case we need to check if it contains any1347 * untracked and / or ignored files.1348 * (b) if it looks like a git directory, and we don't have1349 * 'no_gitlinks' set we treat it as a gitlink, and show it1350 * as a directory.1351 * (c) otherwise, we recurse into it.1352 */1353static enum path_treatment treat_directory(struct dir_struct *dir,1354 struct untracked_cache_dir *untracked,1355 const char *dirname, int len, int baselen, int exclude,1356 const struct path_simplify *simplify)1357{1358 /* The "len-1" is to strip the final '/' */1359 switch (directory_exists_in_index(dirname, len-1)) {1360 case index_directory:1361 return path_recurse;13621363 case index_gitdir:1364 return path_none;13651366 case index_nonexistent:1367 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1368 break;1369 if (!(dir->flags & DIR_NO_GITLINKS)) {1370 unsigned char sha1[20];1371 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)1372 return path_untracked;1373 }1374 return path_recurse;1375 }13761377 /* This is the "show_other_directories" case */13781379 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1380 return exclude ? path_excluded : path_untracked;13811382 untracked = lookup_untracked(dir->untracked, untracked,1383 dirname + baselen, len - baselen);1384 return read_directory_recursive(dir, dirname, len,1385 untracked, 1, simplify);1386}13871388/*1389 * This is an inexact early pruning of any recursive directory1390 * reading - if the path cannot possibly be in the pathspec,1391 * return true, and we'll skip it early.1392 */1393static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)1394{1395 if (simplify) {1396 for (;;) {1397 const char *match = simplify->path;1398 int len = simplify->len;13991400 if (!match)1401 break;1402 if (len > pathlen)1403 len = pathlen;1404 if (!memcmp(path, match, len))1405 return 0;1406 simplify++;1407 }1408 return 1;1409 }1410 return 0;1411}14121413/*1414 * This function tells us whether an excluded path matches a1415 * list of "interesting" pathspecs. That is, whether a path matched1416 * by any of the pathspecs could possibly be ignored by excluding1417 * the specified path. This can happen if:1418 *1419 * 1. the path is mentioned explicitly in the pathspec1420 *1421 * 2. the path is a directory prefix of some element in the1422 * pathspec1423 */1424static int exclude_matches_pathspec(const char *path, int len,1425 const struct path_simplify *simplify)1426{1427 if (simplify) {1428 for (; simplify->path; simplify++) {1429 if (len == simplify->len1430 && !memcmp(path, simplify->path, len))1431 return 1;1432 if (len < simplify->len1433 && simplify->path[len] == '/'1434 && !memcmp(path, simplify->path, len))1435 return 1;1436 }1437 }1438 return 0;1439}14401441static int get_index_dtype(const char *path, int len)1442{1443 int pos;1444 const struct cache_entry *ce;14451446 ce = cache_file_exists(path, len, 0);1447 if (ce) {1448 if (!ce_uptodate(ce))1449 return DT_UNKNOWN;1450 if (S_ISGITLINK(ce->ce_mode))1451 return DT_DIR;1452 /*1453 * Nobody actually cares about the1454 * difference between DT_LNK and DT_REG1455 */1456 return DT_REG;1457 }14581459 /* Try to look it up as a directory */1460 pos = cache_name_pos(path, len);1461 if (pos >= 0)1462 return DT_UNKNOWN;1463 pos = -pos-1;1464 while (pos < active_nr) {1465 ce = active_cache[pos++];1466 if (strncmp(ce->name, path, len))1467 break;1468 if (ce->name[len] > '/')1469 break;1470 if (ce->name[len] < '/')1471 continue;1472 if (!ce_uptodate(ce))1473 break; /* continue? */1474 return DT_DIR;1475 }1476 return DT_UNKNOWN;1477}14781479static int get_dtype(struct dirent *de, const char *path, int len)1480{1481 int dtype = de ? DTYPE(de) : DT_UNKNOWN;1482 struct stat st;14831484 if (dtype != DT_UNKNOWN)1485 return dtype;1486 dtype = get_index_dtype(path, len);1487 if (dtype != DT_UNKNOWN)1488 return dtype;1489 if (lstat(path, &st))1490 return dtype;1491 if (S_ISREG(st.st_mode))1492 return DT_REG;1493 if (S_ISDIR(st.st_mode))1494 return DT_DIR;1495 if (S_ISLNK(st.st_mode))1496 return DT_LNK;1497 return dtype;1498}14991500static enum path_treatment treat_one_path(struct dir_struct *dir,1501 struct untracked_cache_dir *untracked,1502 struct strbuf *path,1503 int baselen,1504 const struct path_simplify *simplify,1505 int dtype, struct dirent *de)1506{1507 int exclude;1508 int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);15091510 if (dtype == DT_UNKNOWN)1511 dtype = get_dtype(de, path->buf, path->len);15121513 /* Always exclude indexed files */1514 if (dtype != DT_DIR && has_path_in_index)1515 return path_none;15161517 /*1518 * When we are looking at a directory P in the working tree,1519 * there are three cases:1520 *1521 * (1) P exists in the index. Everything inside the directory P in1522 * the working tree needs to go when P is checked out from the1523 * index.1524 *1525 * (2) P does not exist in the index, but there is P/Q in the index.1526 * We know P will stay a directory when we check out the contents1527 * of the index, but we do not know yet if there is a directory1528 * P/Q in the working tree to be killed, so we need to recurse.1529 *1530 * (3) P does not exist in the index, and there is no P/Q in the index1531 * to require P to be a directory, either. Only in this case, we1532 * know that everything inside P will not be killed without1533 * recursing.1534 */1535 if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1536 (dtype == DT_DIR) &&1537 !has_path_in_index &&1538 (directory_exists_in_index(path->buf, path->len) == index_nonexistent))1539 return path_none;15401541 exclude = is_excluded(dir, path->buf, &dtype);15421543 /*1544 * Excluded? If we don't explicitly want to show1545 * ignored files, ignore it1546 */1547 if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1548 return path_excluded;15491550 switch (dtype) {1551 default:1552 return path_none;1553 case DT_DIR:1554 strbuf_addch(path, '/');1555 return treat_directory(dir, untracked, path->buf, path->len,1556 baselen, exclude, simplify);1557 case DT_REG:1558 case DT_LNK:1559 return exclude ? path_excluded : path_untracked;1560 }1561}15621563static enum path_treatment treat_path_fast(struct dir_struct *dir,1564 struct untracked_cache_dir *untracked,1565 struct cached_dir *cdir,1566 struct strbuf *path,1567 int baselen,1568 const struct path_simplify *simplify)1569{1570 strbuf_setlen(path, baselen);1571 if (!cdir->ucd) {1572 strbuf_addstr(path, cdir->file);1573 return path_untracked;1574 }1575 strbuf_addstr(path, cdir->ucd->name);1576 /* treat_one_path() does this before it calls treat_directory() */1577 strbuf_complete(path, '/');1578 if (cdir->ucd->check_only)1579 /*1580 * check_only is set as a result of treat_directory() getting1581 * to its bottom. Verify again the same set of directories1582 * with check_only set.1583 */1584 return read_directory_recursive(dir, path->buf, path->len,1585 cdir->ucd, 1, simplify);1586 /*1587 * We get path_recurse in the first run when1588 * directory_exists_in_index() returns index_nonexistent. We1589 * are sure that new changes in the index does not impact the1590 * outcome. Return now.1591 */1592 return path_recurse;1593}15941595static enum path_treatment treat_path(struct dir_struct *dir,1596 struct untracked_cache_dir *untracked,1597 struct cached_dir *cdir,1598 struct strbuf *path,1599 int baselen,1600 const struct path_simplify *simplify)1601{1602 int dtype;1603 struct dirent *de = cdir->de;16041605 if (!de)1606 return treat_path_fast(dir, untracked, cdir, path,1607 baselen, simplify);1608 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))1609 return path_none;1610 strbuf_setlen(path, baselen);1611 strbuf_addstr(path, de->d_name);1612 if (simplify_away(path->buf, path->len, simplify))1613 return path_none;16141615 dtype = DTYPE(de);1616 return treat_one_path(dir, untracked, path, baselen, simplify, dtype, de);1617}16181619static void add_untracked(struct untracked_cache_dir *dir, const char *name)1620{1621 if (!dir)1622 return;1623 ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,1624 dir->untracked_alloc);1625 dir->untracked[dir->untracked_nr++] = xstrdup(name);1626}16271628static int valid_cached_dir(struct dir_struct *dir,1629 struct untracked_cache_dir *untracked,1630 struct strbuf *path,1631 int check_only)1632{1633 struct stat st;16341635 if (!untracked)1636 return 0;16371638 if (stat(path->len ? path->buf : ".", &st)) {1639 invalidate_directory(dir->untracked, untracked);1640 memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));1641 return 0;1642 }1643 if (!untracked->valid ||1644 match_stat_data_racy(&the_index, &untracked->stat_data, &st)) {1645 if (untracked->valid)1646 invalidate_directory(dir->untracked, untracked);1647 fill_stat_data(&untracked->stat_data, &st);1648 return 0;1649 }16501651 if (untracked->check_only != !!check_only) {1652 invalidate_directory(dir->untracked, untracked);1653 return 0;1654 }16551656 /*1657 * prep_exclude will be called eventually on this directory,1658 * but it's called much later in last_exclude_matching(). We1659 * need it now to determine the validity of the cache for this1660 * path. The next calls will be nearly no-op, the way1661 * prep_exclude() is designed.1662 */1663 if (path->len && path->buf[path->len - 1] != '/') {1664 strbuf_addch(path, '/');1665 prep_exclude(dir, path->buf, path->len);1666 strbuf_setlen(path, path->len - 1);1667 } else1668 prep_exclude(dir, path->buf, path->len);16691670 /* hopefully prep_exclude() haven't invalidated this entry... */1671 return untracked->valid;1672}16731674static int open_cached_dir(struct cached_dir *cdir,1675 struct dir_struct *dir,1676 struct untracked_cache_dir *untracked,1677 struct strbuf *path,1678 int check_only)1679{1680 memset(cdir, 0, sizeof(*cdir));1681 cdir->untracked = untracked;1682 if (valid_cached_dir(dir, untracked, path, check_only))1683 return 0;1684 cdir->fdir = opendir(path->len ? path->buf : ".");1685 if (dir->untracked)1686 dir->untracked->dir_opened++;1687 if (!cdir->fdir)1688 return -1;1689 return 0;1690}16911692static int read_cached_dir(struct cached_dir *cdir)1693{1694 if (cdir->fdir) {1695 cdir->de = readdir(cdir->fdir);1696 if (!cdir->de)1697 return -1;1698 return 0;1699 }1700 while (cdir->nr_dirs < cdir->untracked->dirs_nr) {1701 struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1702 if (!d->recurse) {1703 cdir->nr_dirs++;1704 continue;1705 }1706 cdir->ucd = d;1707 cdir->nr_dirs++;1708 return 0;1709 }1710 cdir->ucd = NULL;1711 if (cdir->nr_files < cdir->untracked->untracked_nr) {1712 struct untracked_cache_dir *d = cdir->untracked;1713 cdir->file = d->untracked[cdir->nr_files++];1714 return 0;1715 }1716 return -1;1717}17181719static void close_cached_dir(struct cached_dir *cdir)1720{1721 if (cdir->fdir)1722 closedir(cdir->fdir);1723 /*1724 * We have gone through this directory and found no untracked1725 * entries. Mark it valid.1726 */1727 if (cdir->untracked) {1728 cdir->untracked->valid = 1;1729 cdir->untracked->recurse = 1;1730 }1731}17321733/*1734 * Read a directory tree. We currently ignore anything but1735 * directories, regular files and symlinks. That's because git1736 * doesn't handle them at all yet. Maybe that will change some1737 * day.1738 *1739 * Also, we ignore the name ".git" (even if it is not a directory).1740 * That likely will not change.1741 *1742 * Returns the most significant path_treatment value encountered in the scan.1743 */1744static enum path_treatment read_directory_recursive(struct dir_struct *dir,1745 const char *base, int baselen,1746 struct untracked_cache_dir *untracked, int check_only,1747 const struct path_simplify *simplify)1748{1749 struct cached_dir cdir;1750 enum path_treatment state, subdir_state, dir_state = path_none;1751 struct strbuf path = STRBUF_INIT;1752 static int level = 0;17531754 strbuf_add(&path, base, baselen);17551756 trace_printf_key(&trace_exclude, "exclude: [%d] enter '%.*s'\n",1757 level++, baselen, base);17581759 if (open_cached_dir(&cdir, dir, untracked, &path, check_only))1760 goto out;17611762 if (untracked)1763 untracked->check_only = !!check_only;17641765 while (!read_cached_dir(&cdir)) {1766 /* check how the file or directory should be treated */1767 state = treat_path(dir, untracked, &cdir, &path, baselen, simplify);17681769 if (state > dir_state)1770 dir_state = state;17711772 /* recurse into subdir if instructed by treat_path */1773 if (state == path_recurse) {1774 struct untracked_cache_dir *ud;1775 ud = lookup_untracked(dir->untracked, untracked,1776 path.buf + baselen,1777 path.len - baselen);1778 subdir_state =1779 read_directory_recursive(dir, path.buf, path.len,1780 ud, check_only, simplify);1781 if (subdir_state > dir_state)1782 dir_state = subdir_state;1783 }17841785 if (check_only) {1786 /* abort early if maximum state has been reached */1787 if (dir_state == path_untracked) {1788 if (cdir.fdir)1789 add_untracked(untracked, path.buf + baselen);1790 break;1791 }1792 /* skip the dir_add_* part */1793 continue;1794 }17951796 /* add the path to the appropriate result list */1797 switch (state) {1798 case path_excluded:1799 if (dir->flags & DIR_SHOW_IGNORED)1800 dir_add_name(dir, path.buf, path.len);1801 else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||1802 ((dir->flags & DIR_COLLECT_IGNORED) &&1803 exclude_matches_pathspec(path.buf, path.len,1804 simplify)))1805 dir_add_ignored(dir, path.buf, path.len);1806 break;18071808 case path_untracked:1809 if (dir->flags & DIR_SHOW_IGNORED)1810 break;1811 dir_add_name(dir, path.buf, path.len);1812 if (cdir.fdir)1813 add_untracked(untracked, path.buf + baselen);1814 break;18151816 default:1817 break;1818 }1819 }1820 close_cached_dir(&cdir);1821 out:1822 trace_printf_key(&trace_exclude, "exclude: [%d] leave '%.*s'\n",1823 --level, baselen, base);1824 strbuf_release(&path);18251826 return dir_state;1827}18281829static int cmp_name(const void *p1, const void *p2)1830{1831 const struct dir_entry *e1 = *(const struct dir_entry **)p1;1832 const struct dir_entry *e2 = *(const struct dir_entry **)p2;18331834 return name_compare(e1->name, e1->len, e2->name, e2->len);1835}18361837static struct path_simplify *create_simplify(const char **pathspec)1838{1839 int nr, alloc = 0;1840 struct path_simplify *simplify = NULL;18411842 if (!pathspec)1843 return NULL;18441845 for (nr = 0 ; ; nr++) {1846 const char *match;1847 ALLOC_GROW(simplify, nr + 1, alloc);1848 match = *pathspec++;1849 if (!match)1850 break;1851 simplify[nr].path = match;1852 simplify[nr].len = simple_length(match);1853 }1854 simplify[nr].path = NULL;1855 simplify[nr].len = 0;1856 return simplify;1857}18581859static void free_simplify(struct path_simplify *simplify)1860{1861 free(simplify);1862}18631864static int treat_leading_path(struct dir_struct *dir,1865 const char *path, int len,1866 const struct path_simplify *simplify)1867{1868 struct strbuf sb = STRBUF_INIT;1869 int baselen, rc = 0;1870 const char *cp;1871 int old_flags = dir->flags;18721873 while (len && path[len - 1] == '/')1874 len--;1875 if (!len)1876 return 1;1877 baselen = 0;1878 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1879 while (1) {1880 cp = path + baselen + !!baselen;1881 cp = memchr(cp, '/', path + len - cp);1882 if (!cp)1883 baselen = len;1884 else1885 baselen = cp - path;1886 strbuf_setlen(&sb, 0);1887 strbuf_add(&sb, path, baselen);1888 if (!is_directory(sb.buf))1889 break;1890 if (simplify_away(sb.buf, sb.len, simplify))1891 break;1892 if (treat_one_path(dir, NULL, &sb, baselen, simplify,1893 DT_DIR, NULL) == path_none)1894 break; /* do not recurse into it */1895 if (len <= baselen) {1896 rc = 1;1897 break; /* finished checking */1898 }1899 }1900 strbuf_release(&sb);1901 dir->flags = old_flags;1902 return rc;1903}19041905static const char *get_ident_string(void)1906{1907 static struct strbuf sb = STRBUF_INIT;1908 struct utsname uts;19091910 if (sb.len)1911 return sb.buf;1912 if (uname(&uts) < 0)1913 die_errno(_("failed to get kernel name and information"));1914 strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(),1915 uts.sysname);1916 return sb.buf;1917}19181919static int ident_in_untracked(const struct untracked_cache *uc)1920{1921 /*1922 * Previous git versions may have saved many NUL separated1923 * strings in the "ident" field, but it is insane to manage1924 * many locations, so just take care of the first one.1925 */19261927 return !strcmp(uc->ident.buf, get_ident_string());1928}19291930static void set_untracked_ident(struct untracked_cache *uc)1931{1932 strbuf_reset(&uc->ident);1933 strbuf_addstr(&uc->ident, get_ident_string());19341935 /*1936 * This strbuf used to contain a list of NUL separated1937 * strings, so save NUL too for backward compatibility.1938 */1939 strbuf_addch(&uc->ident, 0);1940}19411942static void new_untracked_cache(struct index_state *istate)1943{1944 struct untracked_cache *uc = xcalloc(1, sizeof(*uc));1945 strbuf_init(&uc->ident, 100);1946 uc->exclude_per_dir = ".gitignore";1947 /* should be the same flags used by git-status */1948 uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;1949 set_untracked_ident(uc);1950 istate->untracked = uc;1951 istate->cache_changed |= UNTRACKED_CHANGED;1952}19531954void add_untracked_cache(struct index_state *istate)1955{1956 if (!istate->untracked) {1957 new_untracked_cache(istate);1958 } else {1959 if (!ident_in_untracked(istate->untracked)) {1960 free_untracked_cache(istate->untracked);1961 new_untracked_cache(istate);1962 }1963 }1964}19651966void remove_untracked_cache(struct index_state *istate)1967{1968 if (istate->untracked) {1969 free_untracked_cache(istate->untracked);1970 istate->untracked = NULL;1971 istate->cache_changed |= UNTRACKED_CHANGED;1972 }1973}19741975static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,1976 int base_len,1977 const struct pathspec *pathspec)1978{1979 struct untracked_cache_dir *root;19801981 if (!dir->untracked || getenv("GIT_DISABLE_UNTRACKED_CACHE"))1982 return NULL;19831984 /*1985 * We only support $GIT_DIR/info/exclude and core.excludesfile1986 * as the global ignore rule files. Any other additions1987 * (e.g. from command line) invalidate the cache. This1988 * condition also catches running setup_standard_excludes()1989 * before setting dir->untracked!1990 */1991 if (dir->unmanaged_exclude_files)1992 return NULL;19931994 /*1995 * Optimize for the main use case only: whole-tree git1996 * status. More work involved in treat_leading_path() if we1997 * use cache on just a subset of the worktree. pathspec1998 * support could make the matter even worse.1999 */2000 if (base_len || (pathspec && pathspec->nr))2001 return NULL;20022003 /* Different set of flags may produce different results */2004 if (dir->flags != dir->untracked->dir_flags ||2005 /*2006 * See treat_directory(), case index_nonexistent. Without2007 * this flag, we may need to also cache .git file content2008 * for the resolve_gitlink_ref() call, which we don't.2009 */2010 !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||2011 /* We don't support collecting ignore files */2012 (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |2013 DIR_COLLECT_IGNORED)))2014 return NULL;20152016 /*2017 * If we use .gitignore in the cache and now you change it to2018 * .gitexclude, everything will go wrong.2019 */2020 if (dir->exclude_per_dir != dir->untracked->exclude_per_dir &&2021 strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))2022 return NULL;20232024 /*2025 * EXC_CMDL is not considered in the cache. If people set it,2026 * skip the cache.2027 */2028 if (dir->exclude_list_group[EXC_CMDL].nr)2029 return NULL;20302031 if (!ident_in_untracked(dir->untracked)) {2032 warning(_("Untracked cache is disabled on this system or location."));2033 return NULL;2034 }20352036 if (!dir->untracked->root) {2037 const int len = sizeof(*dir->untracked->root);2038 dir->untracked->root = xmalloc(len);2039 memset(dir->untracked->root, 0, len);2040 }20412042 /* Validate $GIT_DIR/info/exclude and core.excludesfile */2043 root = dir->untracked->root;2044 if (hashcmp(dir->ss_info_exclude.sha1,2045 dir->untracked->ss_info_exclude.sha1)) {2046 invalidate_gitignore(dir->untracked, root);2047 dir->untracked->ss_info_exclude = dir->ss_info_exclude;2048 }2049 if (hashcmp(dir->ss_excludes_file.sha1,2050 dir->untracked->ss_excludes_file.sha1)) {2051 invalidate_gitignore(dir->untracked, root);2052 dir->untracked->ss_excludes_file = dir->ss_excludes_file;2053 }20542055 /* Make sure this directory is not dropped out at saving phase */2056 root->recurse = 1;2057 return root;2058}20592060static void clear_sticky(struct dir_struct *dir)2061{2062 struct exclude_list_group *g;2063 struct exclude_list *el;2064 struct exclude *x;2065 int i, j, k;20662067 for (i = EXC_CMDL; i <= EXC_FILE; i++) {2068 g = &dir->exclude_list_group[i];2069 for (j = g->nr - 1; j >= 0; j--) {2070 el = &g->el[j];2071 for (k = el->nr - 1; 0 <= k; k--) {2072 x = el->excludes[k];2073 string_list_clear(&x->sticky_paths, 0);2074 }2075 }2076 }2077}20782079int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)2080{2081 struct path_simplify *simplify;2082 struct untracked_cache_dir *untracked;20832084 /*2085 * Check out create_simplify()2086 */2087 if (pathspec)2088 GUARD_PATHSPEC(pathspec,2089 PATHSPEC_FROMTOP |2090 PATHSPEC_MAXDEPTH |2091 PATHSPEC_LITERAL |2092 PATHSPEC_GLOB |2093 PATHSPEC_ICASE |2094 PATHSPEC_EXCLUDE);20952096 if (has_symlink_leading_path(path, len))2097 return dir->nr;20982099 /*2100 * exclude patterns are treated like positive ones in2101 * create_simplify. Usually exclude patterns should be a2102 * subset of positive ones, which has no impacts on2103 * create_simplify().2104 */2105 simplify = create_simplify(pathspec ? pathspec->_raw : NULL);2106 untracked = validate_untracked_cache(dir, len, pathspec);2107 if (!untracked)2108 /*2109 * make sure untracked cache code path is disabled,2110 * e.g. prep_exclude()2111 */2112 dir->untracked = NULL;2113 if (!len || treat_leading_path(dir, path, len, simplify))2114 read_directory_recursive(dir, path, len, untracked, 0, simplify);2115 free_simplify(simplify);2116 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);2117 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);2118 if (dir->untracked) {2119 static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);2120 trace_printf_key(&trace_untracked_stats,2121 "node creation: %u\n"2122 "gitignore invalidation: %u\n"2123 "directory invalidation: %u\n"2124 "opendir: %u\n",2125 dir->untracked->dir_created,2126 dir->untracked->gitignore_invalidated,2127 dir->untracked->dir_invalidated,2128 dir->untracked->dir_opened);2129 if (dir->untracked == the_index.untracked &&2130 (dir->untracked->dir_opened ||2131 dir->untracked->gitignore_invalidated ||2132 dir->untracked->dir_invalidated))2133 the_index.cache_changed |= UNTRACKED_CHANGED;2134 if (dir->untracked != the_index.untracked) {2135 free(dir->untracked);2136 dir->untracked = NULL;2137 }2138 }2139 return dir->nr;2140}21412142int file_exists(const char *f)2143{2144 struct stat sb;2145 return lstat(f, &sb) == 0;2146}21472148static int cmp_icase(char a, char b)2149{2150 if (a == b)2151 return 0;2152 if (ignore_case)2153 return toupper(a) - toupper(b);2154 return a - b;2155}21562157/*2158 * Given two normalized paths (a trailing slash is ok), if subdir is2159 * outside dir, return -1. Otherwise return the offset in subdir that2160 * can be used as relative path to dir.2161 */2162int dir_inside_of(const char *subdir, const char *dir)2163{2164 int offset = 0;21652166 assert(dir && subdir && *dir && *subdir);21672168 while (*dir && *subdir && !cmp_icase(*dir, *subdir)) {2169 dir++;2170 subdir++;2171 offset++;2172 }21732174 /* hel[p]/me vs hel[l]/yeah */2175 if (*dir && *subdir)2176 return -1;21772178 if (!*subdir)2179 return !*dir ? offset : -1; /* same dir */21802181 /* foo/[b]ar vs foo/[] */2182 if (is_dir_sep(dir[-1]))2183 return is_dir_sep(subdir[-1]) ? offset : -1;21842185 /* foo[/]bar vs foo[] */2186 return is_dir_sep(*subdir) ? offset + 1 : -1;2187}21882189int is_inside_dir(const char *dir)2190{2191 char *cwd;2192 int rc;21932194 if (!dir)2195 return 0;21962197 cwd = xgetcwd();2198 rc = (dir_inside_of(cwd, dir) >= 0);2199 free(cwd);2200 return rc;2201}22022203int is_empty_dir(const char *path)2204{2205 DIR *dir = opendir(path);2206 struct dirent *e;2207 int ret = 1;22082209 if (!dir)2210 return 0;22112212 while ((e = readdir(dir)) != NULL)2213 if (!is_dot_or_dotdot(e->d_name)) {2214 ret = 0;2215 break;2216 }22172218 closedir(dir);2219 return ret;2220}22212222static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)2223{2224 DIR *dir;2225 struct dirent *e;2226 int ret = 0, original_len = path->len, len, kept_down = 0;2227 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2228 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2229 unsigned char submodule_head[20];22302231 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2232 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {2233 /* Do not descend and nuke a nested git work tree. */2234 if (kept_up)2235 *kept_up = 1;2236 return 0;2237 }22382239 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2240 dir = opendir(path->buf);2241 if (!dir) {2242 if (errno == ENOENT)2243 return keep_toplevel ? -1 : 0;2244 else if (errno == EACCES && !keep_toplevel)2245 /*2246 * An empty dir could be removable even if it2247 * is unreadable:2248 */2249 return rmdir(path->buf);2250 else2251 return -1;2252 }2253 strbuf_complete(path, '/');22542255 len = path->len;2256 while ((e = readdir(dir)) != NULL) {2257 struct stat st;2258 if (is_dot_or_dotdot(e->d_name))2259 continue;22602261 strbuf_setlen(path, len);2262 strbuf_addstr(path, e->d_name);2263 if (lstat(path->buf, &st)) {2264 if (errno == ENOENT)2265 /*2266 * file disappeared, which is what we2267 * wanted anyway2268 */2269 continue;2270 /* fall thru */2271 } else if (S_ISDIR(st.st_mode)) {2272 if (!remove_dir_recurse(path, flag, &kept_down))2273 continue; /* happy */2274 } else if (!only_empty &&2275 (!unlink(path->buf) || errno == ENOENT)) {2276 continue; /* happy, too */2277 }22782279 /* path too long, stat fails, or non-directory still exists */2280 ret = -1;2281 break;2282 }2283 closedir(dir);22842285 strbuf_setlen(path, original_len);2286 if (!ret && !keep_toplevel && !kept_down)2287 ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;2288 else if (kept_up)2289 /*2290 * report the uplevel that it is not an error that we2291 * did not rmdir() our directory.2292 */2293 *kept_up = !ret;2294 return ret;2295}22962297int remove_dir_recursively(struct strbuf *path, int flag)2298{2299 return remove_dir_recurse(path, flag, NULL);2300}23012302static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")23032304void setup_standard_excludes(struct dir_struct *dir)2305{2306 const char *path;23072308 dir->exclude_per_dir = ".gitignore";23092310 /* core.excludefile defaulting to $XDG_HOME/git/ignore */2311 if (!excludes_file)2312 excludes_file = xdg_config_home("ignore");2313 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))2314 add_excludes_from_file_1(dir, excludes_file,2315 dir->untracked ? &dir->ss_excludes_file : NULL);23162317 /* per repository user preference */2318 path = git_path_info_exclude();2319 if (!access_or_warn(path, R_OK, 0))2320 add_excludes_from_file_1(dir, path,2321 dir->untracked ? &dir->ss_info_exclude : NULL);2322}23232324int remove_path(const char *name)2325{2326 char *slash;23272328 if (unlink(name) && errno != ENOENT && errno != ENOTDIR)2329 return -1;23302331 slash = strrchr(name, '/');2332 if (slash) {2333 char *dirs = xstrdup(name);2334 slash = dirs + (slash - name);2335 do {2336 *slash = '\0';2337 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));2338 free(dirs);2339 }2340 return 0;2341}23422343/*2344 * Frees memory within dir which was allocated for exclude lists and2345 * the exclude_stack. Does not free dir itself.2346 */2347void clear_directory(struct dir_struct *dir)2348{2349 int i, j;2350 struct exclude_list_group *group;2351 struct exclude_list *el;2352 struct exclude_stack *stk;23532354 for (i = EXC_CMDL; i <= EXC_FILE; i++) {2355 group = &dir->exclude_list_group[i];2356 for (j = 0; j < group->nr; j++) {2357 el = &group->el[j];2358 if (i == EXC_DIRS)2359 free((char *)el->src);2360 clear_exclude_list(el);2361 }2362 free(group->el);2363 }23642365 stk = dir->exclude_stack;2366 while (stk) {2367 struct exclude_stack *prev = stk->prev;2368 free(stk);2369 stk = prev;2370 }2371 strbuf_release(&dir->basebuf);2372}23732374struct ondisk_untracked_cache {2375 struct stat_data info_exclude_stat;2376 struct stat_data excludes_file_stat;2377 uint32_t dir_flags;2378 unsigned char info_exclude_sha1[20];2379 unsigned char excludes_file_sha1[20];2380 char exclude_per_dir[FLEX_ARRAY];2381};23822383#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)23842385struct write_data {2386 int index; /* number of written untracked_cache_dir */2387 struct ewah_bitmap *check_only; /* from untracked_cache_dir */2388 struct ewah_bitmap *valid; /* from untracked_cache_dir */2389 struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */2390 struct strbuf out;2391 struct strbuf sb_stat;2392 struct strbuf sb_sha1;2393};23942395static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from)2396{2397 to->sd_ctime.sec = htonl(from->sd_ctime.sec);2398 to->sd_ctime.nsec = htonl(from->sd_ctime.nsec);2399 to->sd_mtime.sec = htonl(from->sd_mtime.sec);2400 to->sd_mtime.nsec = htonl(from->sd_mtime.nsec);2401 to->sd_dev = htonl(from->sd_dev);2402 to->sd_ino = htonl(from->sd_ino);2403 to->sd_uid = htonl(from->sd_uid);2404 to->sd_gid = htonl(from->sd_gid);2405 to->sd_size = htonl(from->sd_size);2406}24072408static void write_one_dir(struct untracked_cache_dir *untracked,2409 struct write_data *wd)2410{2411 struct stat_data stat_data;2412 struct strbuf *out = &wd->out;2413 unsigned char intbuf[16];2414 unsigned int intlen, value;2415 int i = wd->index++;24162417 /*2418 * untracked_nr should be reset whenever valid is clear, but2419 * for safety..2420 */2421 if (!untracked->valid) {2422 untracked->untracked_nr = 0;2423 untracked->check_only = 0;2424 }24252426 if (untracked->check_only)2427 ewah_set(wd->check_only, i);2428 if (untracked->valid) {2429 ewah_set(wd->valid, i);2430 stat_data_to_disk(&stat_data, &untracked->stat_data);2431 strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data));2432 }2433 if (!is_null_sha1(untracked->exclude_sha1)) {2434 ewah_set(wd->sha1_valid, i);2435 strbuf_add(&wd->sb_sha1, untracked->exclude_sha1, 20);2436 }24372438 intlen = encode_varint(untracked->untracked_nr, intbuf);2439 strbuf_add(out, intbuf, intlen);24402441 /* skip non-recurse directories */2442 for (i = 0, value = 0; i < untracked->dirs_nr; i++)2443 if (untracked->dirs[i]->recurse)2444 value++;2445 intlen = encode_varint(value, intbuf);2446 strbuf_add(out, intbuf, intlen);24472448 strbuf_add(out, untracked->name, strlen(untracked->name) + 1);24492450 for (i = 0; i < untracked->untracked_nr; i++)2451 strbuf_add(out, untracked->untracked[i],2452 strlen(untracked->untracked[i]) + 1);24532454 for (i = 0; i < untracked->dirs_nr; i++)2455 if (untracked->dirs[i]->recurse)2456 write_one_dir(untracked->dirs[i], wd);2457}24582459void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked)2460{2461 struct ondisk_untracked_cache *ouc;2462 struct write_data wd;2463 unsigned char varbuf[16];2464 int len = 0, varint_len;2465 if (untracked->exclude_per_dir)2466 len = strlen(untracked->exclude_per_dir);2467 ouc = xmalloc(sizeof(*ouc) + len + 1);2468 stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2469 stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2470 hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);2471 hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);2472 ouc->dir_flags = htonl(untracked->dir_flags);2473 memcpy(ouc->exclude_per_dir, untracked->exclude_per_dir, len + 1);24742475 varint_len = encode_varint(untracked->ident.len, varbuf);2476 strbuf_add(out, varbuf, varint_len);2477 strbuf_add(out, untracked->ident.buf, untracked->ident.len);24782479 strbuf_add(out, ouc, ouc_size(len));2480 free(ouc);2481 ouc = NULL;24822483 if (!untracked->root) {2484 varint_len = encode_varint(0, varbuf);2485 strbuf_add(out, varbuf, varint_len);2486 return;2487 }24882489 wd.index = 0;2490 wd.check_only = ewah_new();2491 wd.valid = ewah_new();2492 wd.sha1_valid = ewah_new();2493 strbuf_init(&wd.out, 1024);2494 strbuf_init(&wd.sb_stat, 1024);2495 strbuf_init(&wd.sb_sha1, 1024);2496 write_one_dir(untracked->root, &wd);24972498 varint_len = encode_varint(wd.index, varbuf);2499 strbuf_add(out, varbuf, varint_len);2500 strbuf_addbuf(out, &wd.out);2501 ewah_serialize_strbuf(wd.valid, out);2502 ewah_serialize_strbuf(wd.check_only, out);2503 ewah_serialize_strbuf(wd.sha1_valid, out);2504 strbuf_addbuf(out, &wd.sb_stat);2505 strbuf_addbuf(out, &wd.sb_sha1);2506 strbuf_addch(out, '\0'); /* safe guard for string lists */25072508 ewah_free(wd.valid);2509 ewah_free(wd.check_only);2510 ewah_free(wd.sha1_valid);2511 strbuf_release(&wd.out);2512 strbuf_release(&wd.sb_stat);2513 strbuf_release(&wd.sb_sha1);2514}25152516static void free_untracked(struct untracked_cache_dir *ucd)2517{2518 int i;2519 if (!ucd)2520 return;2521 for (i = 0; i < ucd->dirs_nr; i++)2522 free_untracked(ucd->dirs[i]);2523 for (i = 0; i < ucd->untracked_nr; i++)2524 free(ucd->untracked[i]);2525 free(ucd->untracked);2526 free(ucd->dirs);2527 free(ucd);2528}25292530void free_untracked_cache(struct untracked_cache *uc)2531{2532 if (uc)2533 free_untracked(uc->root);2534 free(uc);2535}25362537struct read_data {2538 int index;2539 struct untracked_cache_dir **ucd;2540 struct ewah_bitmap *check_only;2541 struct ewah_bitmap *valid;2542 struct ewah_bitmap *sha1_valid;2543 const unsigned char *data;2544 const unsigned char *end;2545};25462547static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from)2548{2549 to->sd_ctime.sec = get_be32(&from->sd_ctime.sec);2550 to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec);2551 to->sd_mtime.sec = get_be32(&from->sd_mtime.sec);2552 to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec);2553 to->sd_dev = get_be32(&from->sd_dev);2554 to->sd_ino = get_be32(&from->sd_ino);2555 to->sd_uid = get_be32(&from->sd_uid);2556 to->sd_gid = get_be32(&from->sd_gid);2557 to->sd_size = get_be32(&from->sd_size);2558}25592560static int read_one_dir(struct untracked_cache_dir **untracked_,2561 struct read_data *rd)2562{2563 struct untracked_cache_dir ud, *untracked;2564 const unsigned char *next, *data = rd->data, *end = rd->end;2565 unsigned int value;2566 int i, len;25672568 memset(&ud, 0, sizeof(ud));25692570 next = data;2571 value = decode_varint(&next);2572 if (next > end)2573 return -1;2574 ud.recurse = 1;2575 ud.untracked_alloc = value;2576 ud.untracked_nr = value;2577 if (ud.untracked_nr)2578 ud.untracked = xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);2579 data = next;25802581 next = data;2582 ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);2583 if (next > end)2584 return -1;2585 ud.dirs = xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);2586 data = next;25872588 len = strlen((const char *)data);2589 next = data + len + 1;2590 if (next > rd->end)2591 return -1;2592 *untracked_ = untracked = xmalloc(sizeof(*untracked) + len);2593 memcpy(untracked, &ud, sizeof(ud));2594 memcpy(untracked->name, data, len + 1);2595 data = next;25962597 for (i = 0; i < untracked->untracked_nr; i++) {2598 len = strlen((const char *)data);2599 next = data + len + 1;2600 if (next > rd->end)2601 return -1;2602 untracked->untracked[i] = xstrdup((const char*)data);2603 data = next;2604 }26052606 rd->ucd[rd->index++] = untracked;2607 rd->data = data;26082609 for (i = 0; i < untracked->dirs_nr; i++) {2610 len = read_one_dir(untracked->dirs + i, rd);2611 if (len < 0)2612 return -1;2613 }2614 return 0;2615}26162617static void set_check_only(size_t pos, void *cb)2618{2619 struct read_data *rd = cb;2620 struct untracked_cache_dir *ud = rd->ucd[pos];2621 ud->check_only = 1;2622}26232624static void read_stat(size_t pos, void *cb)2625{2626 struct read_data *rd = cb;2627 struct untracked_cache_dir *ud = rd->ucd[pos];2628 if (rd->data + sizeof(struct stat_data) > rd->end) {2629 rd->data = rd->end + 1;2630 return;2631 }2632 stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);2633 rd->data += sizeof(struct stat_data);2634 ud->valid = 1;2635}26362637static void read_sha1(size_t pos, void *cb)2638{2639 struct read_data *rd = cb;2640 struct untracked_cache_dir *ud = rd->ucd[pos];2641 if (rd->data + 20 > rd->end) {2642 rd->data = rd->end + 1;2643 return;2644 }2645 hashcpy(ud->exclude_sha1, rd->data);2646 rd->data += 20;2647}26482649static void load_sha1_stat(struct sha1_stat *sha1_stat,2650 const struct stat_data *stat,2651 const unsigned char *sha1)2652{2653 stat_data_from_disk(&sha1_stat->stat, stat);2654 hashcpy(sha1_stat->sha1, sha1);2655 sha1_stat->valid = 1;2656}26572658struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)2659{2660 const struct ondisk_untracked_cache *ouc;2661 struct untracked_cache *uc;2662 struct read_data rd;2663 const unsigned char *next = data, *end = (const unsigned char *)data + sz;2664 const char *ident;2665 int ident_len, len;26662667 if (sz <= 1 || end[-1] != '\0')2668 return NULL;2669 end--;26702671 ident_len = decode_varint(&next);2672 if (next + ident_len > end)2673 return NULL;2674 ident = (const char *)next;2675 next += ident_len;26762677 ouc = (const struct ondisk_untracked_cache *)next;2678 if (next + ouc_size(0) > end)2679 return NULL;26802681 uc = xcalloc(1, sizeof(*uc));2682 strbuf_init(&uc->ident, ident_len);2683 strbuf_add(&uc->ident, ident, ident_len);2684 load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,2685 ouc->info_exclude_sha1);2686 load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,2687 ouc->excludes_file_sha1);2688 uc->dir_flags = get_be32(&ouc->dir_flags);2689 uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir);2690 /* NUL after exclude_per_dir is covered by sizeof(*ouc) */2691 next += ouc_size(strlen(ouc->exclude_per_dir));2692 if (next >= end)2693 goto done2;26942695 len = decode_varint(&next);2696 if (next > end || len == 0)2697 goto done2;26982699 rd.valid = ewah_new();2700 rd.check_only = ewah_new();2701 rd.sha1_valid = ewah_new();2702 rd.data = next;2703 rd.end = end;2704 rd.index = 0;2705 rd.ucd = xmalloc(sizeof(*rd.ucd) * len);27062707 if (read_one_dir(&uc->root, &rd) || rd.index != len)2708 goto done;27092710 next = rd.data;2711 len = ewah_read_mmap(rd.valid, next, end - next);2712 if (len < 0)2713 goto done;27142715 next += len;2716 len = ewah_read_mmap(rd.check_only, next, end - next);2717 if (len < 0)2718 goto done;27192720 next += len;2721 len = ewah_read_mmap(rd.sha1_valid, next, end - next);2722 if (len < 0)2723 goto done;27242725 ewah_each_bit(rd.check_only, set_check_only, &rd);2726 rd.data = next + len;2727 ewah_each_bit(rd.valid, read_stat, &rd);2728 ewah_each_bit(rd.sha1_valid, read_sha1, &rd);2729 next = rd.data;27302731done:2732 free(rd.ucd);2733 ewah_free(rd.valid);2734 ewah_free(rd.check_only);2735 ewah_free(rd.sha1_valid);2736done2:2737 if (next != end) {2738 free_untracked_cache(uc);2739 uc = NULL;2740 }2741 return uc;2742}27432744static void invalidate_one_directory(struct untracked_cache *uc,2745 struct untracked_cache_dir *ucd)2746{2747 uc->dir_invalidated++;2748 ucd->valid = 0;2749 ucd->untracked_nr = 0;2750}27512752/*2753 * Normally when an entry is added or removed from a directory,2754 * invalidating that directory is enough. No need to touch its2755 * ancestors. When a directory is shown as "foo/bar/" in git-status2756 * however, deleting or adding an entry may have cascading effect.2757 *2758 * Say the "foo/bar/file" has become untracked, we need to tell the2759 * untracked_cache_dir of "foo" that "bar/" is not an untracked2760 * directory any more (because "bar" is managed by foo as an untracked2761 * "file").2762 *2763 * Similarly, if "foo/bar/file" moves from untracked to tracked and it2764 * was the last untracked entry in the entire "foo", we should show2765 * "foo/" instead. Which means we have to invalidate past "bar" up to2766 * "foo".2767 *2768 * This function traverses all directories from root to leaf. If there2769 * is a chance of one of the above cases happening, we invalidate back2770 * to root. Otherwise we just invalidate the leaf. There may be a more2771 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to2772 * detect these cases and avoid unnecessary invalidation, for example,2773 * checking for the untracked entry named "bar/" in "foo", but for now2774 * stick to something safe and simple.2775 */2776static int invalidate_one_component(struct untracked_cache *uc,2777 struct untracked_cache_dir *dir,2778 const char *path, int len)2779{2780 const char *rest = strchr(path, '/');27812782 if (rest) {2783 int component_len = rest - path;2784 struct untracked_cache_dir *d =2785 lookup_untracked(uc, dir, path, component_len);2786 int ret =2787 invalidate_one_component(uc, d, rest + 1,2788 len - (component_len + 1));2789 if (ret)2790 invalidate_one_directory(uc, dir);2791 return ret;2792 }27932794 invalidate_one_directory(uc, dir);2795 return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;2796}27972798void untracked_cache_invalidate_path(struct index_state *istate,2799 const char *path)2800{2801 if (!istate->untracked || !istate->untracked->root)2802 return;2803 invalidate_one_component(istate->untracked, istate->untracked->root,2804 path, strlen(path));2805}28062807void untracked_cache_remove_from_index(struct index_state *istate,2808 const char *path)2809{2810 untracked_cache_invalidate_path(istate, path);2811}28122813void untracked_cache_add_to_index(struct index_state *istate,2814 const char *path)2815{2816 untracked_cache_invalidate_path(istate, path);2817}