Git fork
at reftables-rust 4177 lines 115 kB view raw
1/* 2 * This handles recursive filename detection with exclude 3 * files, index knowledge etc.. 4 * 5 * Copyright (C) Linus Torvalds, 2005-2006 6 * Junio Hamano, 2005-2006 7 */ 8 9#define USE_THE_REPOSITORY_VARIABLE 10#define DISABLE_SIGN_COMPARE_WARNINGS 11 12#include "git-compat-util.h" 13#include "abspath.h" 14#include "config.h" 15#include "convert.h" 16#include "dir.h" 17#include "environment.h" 18#include "gettext.h" 19#include "name-hash.h" 20#include "object-file.h" 21#include "path.h" 22#include "refs.h" 23#include "repository.h" 24#include "wildmatch.h" 25#include "pathspec.h" 26#include "utf8.h" 27#include "varint.h" 28#include "ewah/ewok.h" 29#include "fsmonitor-ll.h" 30#include "read-cache-ll.h" 31#include "setup.h" 32#include "sparse-index.h" 33#include "strbuf.h" 34#include "submodule-config.h" 35#include "symlinks.h" 36#include "trace2.h" 37#include "tree.h" 38#include "hex.h" 39 40 /* 41 * The maximum size of a pattern/exclude file. If the file exceeds this size 42 * we will ignore it. 43 */ 44#define PATTERN_MAX_FILE_SIZE (100 * 1024 * 1024) 45 46/* 47 * Tells read_directory_recursive how a file or directory should be treated. 48 * Values are ordered by significance, e.g. if a directory contains both 49 * excluded and untracked files, it is listed as untracked because 50 * path_untracked > path_excluded. 51 */ 52enum path_treatment { 53 path_none = 0, 54 path_recurse, 55 path_excluded, 56 path_untracked 57}; 58 59/* 60 * Support data structure for our opendir/readdir/closedir wrappers 61 */ 62struct cached_dir { 63 DIR *fdir; 64 struct untracked_cache_dir *untracked; 65 int nr_files; 66 int nr_dirs; 67 68 const char *d_name; 69 int d_type; 70 const char *file; 71 struct untracked_cache_dir *ucd; 72}; 73 74static enum path_treatment read_directory_recursive(struct dir_struct *dir, 75 struct index_state *istate, const char *path, int len, 76 struct untracked_cache_dir *untracked, 77 int check_only, int stop_at_first_file, const struct pathspec *pathspec); 78static int resolve_dtype(int dtype, struct index_state *istate, 79 const char *path, int len); 80struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp) 81{ 82 struct dirent *e; 83 84 while ((e = readdir(dirp)) != NULL) { 85 if (!is_dot_or_dotdot(e->d_name)) 86 break; 87 } 88 return e; 89} 90 91int for_each_file_in_dir(struct strbuf *path, file_iterator fn, const void *data) 92{ 93 struct dirent *e; 94 int res = 0; 95 size_t baselen = path->len; 96 DIR *dir = opendir(path->buf); 97 98 if (!dir) 99 return 0; 100 101 while (!res && (e = readdir_skip_dot_and_dotdot(dir)) != NULL) { 102 unsigned char dtype = get_dtype(e, path, 0); 103 strbuf_setlen(path, baselen); 104 strbuf_addstr(path, e->d_name); 105 106 if (dtype == DT_REG) { 107 res = fn(path->buf, data); 108 } else if (dtype == DT_DIR) { 109 strbuf_addch(path, '/'); 110 res = for_each_file_in_dir(path, fn, data); 111 } 112 } 113 114 closedir(dir); 115 return res; 116} 117 118int count_slashes(const char *s) 119{ 120 int cnt = 0; 121 while (*s) 122 if (*s++ == '/') 123 cnt++; 124 return cnt; 125} 126 127int git_fspathcmp(const char *a, const char *b) 128{ 129 return ignore_case ? strcasecmp(a, b) : strcmp(a, b); 130} 131 132int fspatheq(const char *a, const char *b) 133{ 134 return !fspathcmp(a, b); 135} 136 137int git_fspathncmp(const char *a, const char *b, size_t count) 138{ 139 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); 140} 141 142int paths_collide(const char *a, const char *b) 143{ 144 size_t len_a = strlen(a), len_b = strlen(b); 145 146 if (len_a == len_b) 147 return fspatheq(a, b); 148 149 if (len_a < len_b) 150 return is_dir_sep(b[len_a]) && !fspathncmp(a, b, len_a); 151 return is_dir_sep(a[len_b]) && !fspathncmp(a, b, len_b); 152} 153 154unsigned int fspathhash(const char *str) 155{ 156 return ignore_case ? strihash(str) : strhash(str); 157} 158 159int git_fnmatch(const struct pathspec_item *item, 160 const char *pattern, const char *string, 161 int prefix) 162{ 163 if (prefix > 0) { 164 if (ps_strncmp(item, pattern, string, prefix)) 165 return WM_NOMATCH; 166 pattern += prefix; 167 string += prefix; 168 } 169 if (item->flags & PATHSPEC_ONESTAR) { 170 int pattern_len = strlen(++pattern); 171 int string_len = strlen(string); 172 return string_len < pattern_len || 173 ps_strcmp(item, pattern, 174 string + string_len - pattern_len); 175 } 176 if (item->magic & PATHSPEC_GLOB) 177 return wildmatch(pattern, string, 178 WM_PATHNAME | 179 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0)); 180 else 181 /* wildmatch has not learned no FNM_PATHNAME mode yet */ 182 return wildmatch(pattern, string, 183 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0); 184} 185 186static int fnmatch_icase_mem(const char *pattern, int patternlen, 187 const char *string, int stringlen, 188 int flags) 189{ 190 int match_status; 191 struct strbuf pat_buf = STRBUF_INIT; 192 struct strbuf str_buf = STRBUF_INIT; 193 const char *use_pat = pattern; 194 const char *use_str = string; 195 196 if (pattern[patternlen]) { 197 strbuf_add(&pat_buf, pattern, patternlen); 198 use_pat = pat_buf.buf; 199 } 200 if (string[stringlen]) { 201 strbuf_add(&str_buf, string, stringlen); 202 use_str = str_buf.buf; 203 } 204 205 if (ignore_case) 206 flags |= WM_CASEFOLD; 207 match_status = wildmatch(use_pat, use_str, flags); 208 209 strbuf_release(&pat_buf); 210 strbuf_release(&str_buf); 211 212 return match_status; 213} 214 215static size_t common_prefix_len(const struct pathspec *pathspec) 216{ 217 int n; 218 size_t max = 0; 219 220 /* 221 * ":(icase)path" is treated as a pathspec full of 222 * wildcard. In other words, only prefix is considered common 223 * prefix. If the pathspec is abc/foo abc/bar, running in 224 * subdir xyz, the common prefix is still xyz, not xyz/abc as 225 * in non-:(icase). 226 */ 227 GUARD_PATHSPEC(pathspec, 228 PATHSPEC_FROMTOP | 229 PATHSPEC_MAXDEPTH | 230 PATHSPEC_LITERAL | 231 PATHSPEC_GLOB | 232 PATHSPEC_ICASE | 233 PATHSPEC_EXCLUDE | 234 PATHSPEC_ATTR); 235 236 for (n = 0; n < pathspec->nr; n++) { 237 size_t i = 0, len = 0, item_len; 238 if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) 239 continue; 240 if (pathspec->items[n].magic & PATHSPEC_ICASE) 241 item_len = pathspec->items[n].prefix; 242 else 243 item_len = pathspec->items[n].nowildcard_len; 244 while (i < item_len && (n == 0 || i < max)) { 245 char c = pathspec->items[n].match[i]; 246 if (c != pathspec->items[0].match[i]) 247 break; 248 if (c == '/') 249 len = i + 1; 250 i++; 251 } 252 if (n == 0 || len < max) { 253 max = len; 254 if (!max) 255 break; 256 } 257 } 258 return max; 259} 260 261/* 262 * Returns a copy of the longest leading path common among all 263 * pathspecs. 264 */ 265char *common_prefix(const struct pathspec *pathspec) 266{ 267 unsigned long len = common_prefix_len(pathspec); 268 269 return len ? xmemdupz(pathspec->items[0].match, len) : NULL; 270} 271 272int fill_directory(struct dir_struct *dir, 273 struct index_state *istate, 274 const struct pathspec *pathspec) 275{ 276 const char *prefix; 277 size_t prefix_len; 278 279 unsigned exclusive_flags = DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO; 280 if ((dir->flags & exclusive_flags) == exclusive_flags) 281 BUG("DIR_SHOW_IGNORED and DIR_SHOW_IGNORED_TOO are exclusive"); 282 283 /* 284 * Calculate common prefix for the pathspec, and 285 * use that to optimize the directory walk 286 */ 287 prefix_len = common_prefix_len(pathspec); 288 prefix = prefix_len ? pathspec->items[0].match : ""; 289 290 /* Read the directory and prune it */ 291 read_directory(dir, istate, prefix, prefix_len, pathspec); 292 293 return prefix_len; 294} 295 296int within_depth(const char *name, int namelen, 297 int depth, int max_depth) 298{ 299 const char *cp = name, *cpe = name + namelen; 300 301 while (cp < cpe) { 302 if (*cp++ != '/') 303 continue; 304 depth++; 305 if (depth > max_depth) 306 return 0; 307 } 308 return depth <= max_depth; 309} 310 311/* 312 * Read the contents of the blob with the given OID into a buffer. 313 * Append a trailing LF to the end if the last line doesn't have one. 314 * 315 * Returns: 316 * -1 when the OID is invalid or unknown or does not refer to a blob. 317 * 0 when the blob is empty. 318 * 1 along with { data, size } of the (possibly augmented) buffer 319 * when successful. 320 * 321 * Optionally updates the given oid_stat with the given OID (when valid). 322 */ 323static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat, 324 size_t *size_out, char **data_out) 325{ 326 enum object_type type; 327 unsigned long sz; 328 char *data; 329 330 *size_out = 0; 331 *data_out = NULL; 332 333 data = odb_read_object(the_repository->objects, oid, &type, &sz); 334 if (!data || type != OBJ_BLOB) { 335 free(data); 336 return -1; 337 } 338 339 if (oid_stat) { 340 memset(&oid_stat->stat, 0, sizeof(oid_stat->stat)); 341 oidcpy(&oid_stat->oid, oid); 342 } 343 344 if (sz == 0) { 345 free(data); 346 return 0; 347 } 348 349 if (data[sz - 1] != '\n') { 350 data = xrealloc(data, st_add(sz, 1)); 351 data[sz++] = '\n'; 352 } 353 354 *size_out = xsize_t(sz); 355 *data_out = data; 356 357 return 1; 358} 359 360#define DO_MATCH_EXCLUDE (1<<0) 361#define DO_MATCH_DIRECTORY (1<<1) 362#define DO_MATCH_LEADING_PATHSPEC (1<<2) 363 364/* 365 * Does the given pathspec match the given name? A match is found if 366 * 367 * (1) the pathspec string is leading directory of 'name' ("RECURSIVELY"), or 368 * (2) the pathspec string has a leading part matching 'name' ("LEADING"), or 369 * (3) the pathspec string is a wildcard and matches 'name' ("WILDCARD"), or 370 * (4) the pathspec string is exactly the same as 'name' ("EXACT"). 371 * 372 * Return value tells which case it was (1-4), or 0 when there is no match. 373 * 374 * It may be instructive to look at a small table of concrete examples 375 * to understand the differences between 1, 2, and 4: 376 * 377 * Pathspecs 378 * | a/b | a/b/ | a/b/c 379 * ------+-----------+-----------+------------ 380 * a/b | EXACT | EXACT[1] | LEADING[2] 381 * Names a/b/ | RECURSIVE | EXACT | LEADING[2] 382 * a/b/c | RECURSIVE | RECURSIVE | EXACT 383 * 384 * [1] Only if DO_MATCH_DIRECTORY is passed; otherwise, this is NOT a match. 385 * [2] Only if DO_MATCH_LEADING_PATHSPEC is passed; otherwise, not a match. 386 */ 387static int match_pathspec_item(struct index_state *istate, 388 const struct pathspec_item *item, int prefix, 389 const char *name, int namelen, unsigned flags) 390{ 391 /* name/namelen has prefix cut off by caller */ 392 const char *match = item->match + prefix; 393 int matchlen = item->len - prefix; 394 395 /* 396 * The normal call pattern is: 397 * 1. prefix = common_prefix_len(ps); 398 * 2. prune something, or fill_directory 399 * 3. match_pathspec() 400 * 401 * 'prefix' at #1 may be shorter than the command's prefix and 402 * it's ok for #2 to match extra files. Those extras will be 403 * trimmed at #3. 404 * 405 * Suppose the pathspec is 'foo' and '../bar' running from 406 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 407 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 408 * user does not want XYZ/foo, only the "foo" part should be 409 * case-insensitive. We need to filter out XYZ/foo here. In 410 * other words, we do not trust the caller on comparing the 411 * prefix part when :(icase) is involved. We do exact 412 * comparison ourselves. 413 * 414 * Normally the caller (common_prefix_len() in fact) does 415 * _exact_ matching on name[-prefix+1..-1] and we do not need 416 * to check that part. Be defensive and check it anyway, in 417 * case common_prefix_len is changed, or a new caller is 418 * introduced that does not use common_prefix_len. 419 * 420 * If the penalty turns out too high when prefix is really 421 * long, maybe change it to 422 * strncmp(match, name, item->prefix - prefix) 423 */ 424 if (item->prefix && (item->magic & PATHSPEC_ICASE) && 425 strncmp(item->match, name - prefix, item->prefix)) 426 return 0; 427 428 if (item->attr_match_nr) { 429 if (!istate) 430 BUG("magic PATHSPEC_ATTR requires an index"); 431 if (!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item)) 432 return 0; 433 } 434 435 /* If the match was just the prefix, we matched */ 436 if (!*match) 437 return MATCHED_RECURSIVELY; 438 439 if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 440 if (matchlen == namelen) 441 return MATCHED_EXACTLY; 442 443 if (match[matchlen-1] == '/' || name[matchlen] == '/') 444 return MATCHED_RECURSIVELY; 445 } else if ((flags & DO_MATCH_DIRECTORY) && 446 match[matchlen - 1] == '/' && 447 namelen == matchlen - 1 && 448 !ps_strncmp(item, match, name, namelen)) 449 return MATCHED_EXACTLY; 450 451 if (item->nowildcard_len < item->len && 452 !git_fnmatch(item, match, name, 453 item->nowildcard_len - prefix)) 454 return MATCHED_FNMATCH; 455 456 /* Perform checks to see if "name" is a leading string of the pathspec */ 457 if ( (flags & DO_MATCH_LEADING_PATHSPEC) && 458 !(flags & DO_MATCH_EXCLUDE)) { 459 /* name is a literal prefix of the pathspec */ 460 int offset = name[namelen-1] == '/' ? 1 : 0; 461 if ((namelen < matchlen) && 462 (match[namelen-offset] == '/') && 463 !ps_strncmp(item, match, name, namelen)) 464 return MATCHED_RECURSIVELY_LEADING_PATHSPEC; 465 466 /* name doesn't match up to the first wild character */ 467 if (item->nowildcard_len < item->len && 468 ps_strncmp(item, match, name, 469 item->nowildcard_len - prefix)) 470 return 0; 471 472 /* 473 * name has no wildcard, and it didn't match as a leading 474 * pathspec so return. 475 */ 476 if (item->nowildcard_len == item->len) 477 return 0; 478 479 /* 480 * Here is where we would perform a wildmatch to check if 481 * "name" can be matched as a directory (or a prefix) against 482 * the pathspec. Since wildmatch doesn't have this capability 483 * at the present we have to punt and say that it is a match, 484 * potentially returning a false positive 485 * The submodules themselves will be able to perform more 486 * accurate matching to determine if the pathspec matches. 487 */ 488 return MATCHED_RECURSIVELY_LEADING_PATHSPEC; 489 } 490 491 return 0; 492} 493 494/* 495 * do_match_pathspec() is meant to ONLY be called by 496 * match_pathspec_with_flags(); calling it directly risks pathspecs 497 * like ':!unwanted_path' being ignored. 498 * 499 * Given a name and a list of pathspecs, returns the nature of the 500 * closest (i.e. most specific) match of the name to any of the 501 * pathspecs. 502 * 503 * The caller typically calls this multiple times with the same 504 * pathspec and seen[] array but with different name/namelen 505 * (e.g. entries from the index) and is interested in seeing if and 506 * how each pathspec matches all the names it calls this function 507 * with. A mark is left in the seen[] array for each pathspec element 508 * indicating the closest type of match that element achieved, so if 509 * seen[n] remains zero after multiple invocations, that means the nth 510 * pathspec did not match any names, which could indicate that the 511 * user mistyped the nth pathspec. 512 */ 513static int do_match_pathspec(struct index_state *istate, 514 const struct pathspec *ps, 515 const char *name, int namelen, 516 int prefix, char *seen, 517 unsigned flags) 518{ 519 int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; 520 521 GUARD_PATHSPEC(ps, 522 PATHSPEC_FROMTOP | 523 PATHSPEC_MAXDEPTH | 524 PATHSPEC_LITERAL | 525 PATHSPEC_GLOB | 526 PATHSPEC_ICASE | 527 PATHSPEC_EXCLUDE | 528 PATHSPEC_ATTR); 529 530 if (!ps->nr) { 531 if (!ps->recursive || 532 !(ps->magic & PATHSPEC_MAXDEPTH) || 533 ps->max_depth == -1) 534 return MATCHED_RECURSIVELY; 535 536 if (within_depth(name, namelen, 0, ps->max_depth)) 537 return MATCHED_EXACTLY; 538 else 539 return 0; 540 } 541 542 name += prefix; 543 namelen -= prefix; 544 545 for (i = ps->nr - 1; i >= 0; i--) { 546 int how; 547 548 if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 549 ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 550 continue; 551 552 if (seen && seen[i] == MATCHED_EXACTLY && 553 ps->items[i].nowildcard_len == ps->items[i].len) 554 continue; 555 /* 556 * Make exclude patterns optional and never report 557 * "pathspec ':(exclude)foo' matches no files" 558 */ 559 if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 560 seen[i] = MATCHED_FNMATCH; 561 how = match_pathspec_item(istate, ps->items+i, prefix, name, 562 namelen, flags); 563 if (ps->recursive && 564 (ps->magic & PATHSPEC_MAXDEPTH) && 565 ps->max_depth != -1 && 566 how && how != MATCHED_FNMATCH) { 567 int len = ps->items[i].len; 568 if (name[len] == '/') 569 len++; 570 if (within_depth(name+len, namelen-len, 0, ps->max_depth)) 571 how = MATCHED_EXACTLY; 572 else 573 how = 0; 574 } 575 if (how) { 576 if (retval < how) 577 retval = how; 578 if (seen && seen[i] < how) 579 seen[i] = how; 580 } 581 } 582 return retval; 583} 584 585static int match_pathspec_with_flags(struct index_state *istate, 586 const struct pathspec *ps, 587 const char *name, int namelen, 588 int prefix, char *seen, unsigned flags) 589{ 590 int positive, negative; 591 positive = do_match_pathspec(istate, ps, name, namelen, 592 prefix, seen, flags); 593 if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 594 return positive; 595 negative = do_match_pathspec(istate, ps, name, namelen, 596 prefix, seen, 597 flags | DO_MATCH_EXCLUDE); 598 return negative ? 0 : positive; 599} 600 601int match_pathspec(struct index_state *istate, 602 const struct pathspec *ps, 603 const char *name, int namelen, 604 int prefix, char *seen, int is_dir) 605{ 606 unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; 607 return match_pathspec_with_flags(istate, ps, name, namelen, 608 prefix, seen, flags); 609} 610 611int match_leading_pathspec(struct index_state *istate, 612 const struct pathspec *ps, 613 const char *name, int namelen, 614 int prefix, char *seen, int is_dir) 615{ 616 unsigned flags = is_dir ? DO_MATCH_DIRECTORY | DO_MATCH_LEADING_PATHSPEC : 0; 617 return match_pathspec_with_flags(istate, ps, name, namelen, 618 prefix, seen, flags); 619} 620 621/** 622 * Check if a submodule is a superset of the pathspec 623 */ 624int submodule_path_match(struct index_state *istate, 625 const struct pathspec *ps, 626 const char *submodule_name, 627 char *seen) 628{ 629 int matched = match_pathspec_with_flags(istate, ps, submodule_name, 630 strlen(submodule_name), 631 0, seen, 632 DO_MATCH_DIRECTORY | 633 DO_MATCH_LEADING_PATHSPEC); 634 return matched; 635} 636 637int report_path_error(const char *ps_matched, 638 const struct pathspec *pathspec) 639{ 640 /* 641 * Make sure all pathspec matched; otherwise it is an error. 642 */ 643 int num, errors = 0; 644 for (num = 0; num < pathspec->nr; num++) { 645 int other, found_dup; 646 647 if (ps_matched[num]) 648 continue; 649 /* 650 * The caller might have fed identical pathspec 651 * twice. Do not barf on such a mistake. 652 * FIXME: parse_pathspec should have eliminated 653 * duplicate pathspec. 654 */ 655 for (found_dup = other = 0; 656 !found_dup && other < pathspec->nr; 657 other++) { 658 if (other == num || !ps_matched[other]) 659 continue; 660 if (!strcmp(pathspec->items[other].original, 661 pathspec->items[num].original)) 662 /* 663 * Ok, we have a match already. 664 */ 665 found_dup = 1; 666 } 667 if (found_dup) 668 continue; 669 670 error(_("pathspec '%s' did not match any file(s) known to git"), 671 pathspec->items[num].original); 672 errors++; 673 } 674 return errors; 675} 676 677/* 678 * Return the length of the "simple" part of a path match limiter. 679 */ 680int simple_length(const char *match) 681{ 682 int len = -1; 683 684 for (;;) { 685 unsigned char c = *match++; 686 len++; 687 if (c == '\0' || is_glob_special(c)) 688 return len; 689 } 690} 691 692int no_wildcard(const char *string) 693{ 694 return string[simple_length(string)] == '\0'; 695} 696 697void parse_path_pattern(const char **pattern, 698 int *patternlen, 699 unsigned *flags, 700 int *nowildcardlen) 701{ 702 const char *p = *pattern; 703 size_t i, len; 704 705 *flags = 0; 706 if (*p == '!') { 707 *flags |= PATTERN_FLAG_NEGATIVE; 708 p++; 709 } 710 len = strlen(p); 711 if (len && p[len - 1] == '/') { 712 len--; 713 *flags |= PATTERN_FLAG_MUSTBEDIR; 714 } 715 for (i = 0; i < len; i++) { 716 if (p[i] == '/') 717 break; 718 } 719 if (i == len) 720 *flags |= PATTERN_FLAG_NODIR; 721 *nowildcardlen = simple_length(p); 722 /* 723 * we should have excluded the trailing slash from 'p' too, 724 * but that's one more allocation. Instead just make sure 725 * nowildcardlen does not exceed real patternlen 726 */ 727 if (*nowildcardlen > len) 728 *nowildcardlen = len; 729 if (*p == '*' && no_wildcard(p + 1)) 730 *flags |= PATTERN_FLAG_ENDSWITH; 731 *pattern = p; 732 *patternlen = len; 733} 734 735int pl_hashmap_cmp(const void *cmp_data UNUSED, 736 const struct hashmap_entry *a, 737 const struct hashmap_entry *b, 738 const void *key UNUSED) 739{ 740 const struct pattern_entry *ee1 = 741 container_of(a, struct pattern_entry, ent); 742 const struct pattern_entry *ee2 = 743 container_of(b, struct pattern_entry, ent); 744 745 size_t min_len = ee1->patternlen <= ee2->patternlen 746 ? ee1->patternlen 747 : ee2->patternlen; 748 749 return fspathncmp(ee1->pattern, ee2->pattern, min_len); 750} 751 752static char *dup_and_filter_pattern(const char *pattern) 753{ 754 char *set, *read; 755 size_t count = 0; 756 char *result = xstrdup(pattern); 757 758 set = result; 759 read = result; 760 761 while (*read) { 762 /* skip escape characters (once) */ 763 if (*read == '\\') 764 read++; 765 766 *set = *read; 767 768 set++; 769 read++; 770 count++; 771 } 772 *set = 0; 773 774 if (count > 2 && 775 *(set - 1) == '*' && 776 *(set - 2) == '/') 777 *(set - 2) = 0; 778 779 return result; 780} 781 782static void clear_pattern_entry_hashmap(struct hashmap *map) 783{ 784 struct hashmap_iter iter; 785 struct pattern_entry *entry; 786 787 hashmap_for_each_entry(map, &iter, entry, ent) { 788 free(entry->pattern); 789 } 790 hashmap_clear_and_free(map, struct pattern_entry, ent); 791} 792 793static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given) 794{ 795 struct pattern_entry *translated; 796 char *truncated; 797 char *data = NULL; 798 const char *prev, *cur, *next; 799 800 if (!pl->use_cone_patterns) 801 return; 802 803 if (given->flags & PATTERN_FLAG_NEGATIVE && 804 given->flags & PATTERN_FLAG_MUSTBEDIR && 805 !strcmp(given->pattern, "/*")) { 806 pl->full_cone = 0; 807 return; 808 } 809 810 if (!given->flags && !strcmp(given->pattern, "/*")) { 811 pl->full_cone = 1; 812 return; 813 } 814 815 if (given->patternlen < 2 || 816 *given->pattern != '/' || 817 strstr(given->pattern, "**")) { 818 /* Not a cone pattern. */ 819 warning(_("unrecognized pattern: '%s'"), given->pattern); 820 goto clear_hashmaps; 821 } 822 823 if (!(given->flags & PATTERN_FLAG_MUSTBEDIR) && 824 strcmp(given->pattern, "/*")) { 825 /* Not a cone pattern. */ 826 warning(_("unrecognized pattern: '%s'"), given->pattern); 827 goto clear_hashmaps; 828 } 829 830 prev = given->pattern; 831 cur = given->pattern + 1; 832 next = given->pattern + 2; 833 834 while (*cur) { 835 /* Watch for glob characters '*', '\', '[', '?' */ 836 if (!is_glob_special(*cur)) 837 goto increment; 838 839 /* But only if *prev != '\\' */ 840 if (*prev == '\\') 841 goto increment; 842 843 /* But allow the initial '\' */ 844 if (*cur == '\\' && 845 is_glob_special(*next)) 846 goto increment; 847 848 /* But a trailing '/' then '*' is fine */ 849 if (*prev == '/' && 850 *cur == '*' && 851 *next == 0) 852 goto increment; 853 854 /* Not a cone pattern. */ 855 warning(_("unrecognized pattern: '%s'"), given->pattern); 856 goto clear_hashmaps; 857 858 increment: 859 prev++; 860 cur++; 861 next++; 862 } 863 864 if (given->patternlen > 2 && 865 !strcmp(given->pattern + given->patternlen - 2, "/*")) { 866 struct pattern_entry *old; 867 868 if (!(given->flags & PATTERN_FLAG_NEGATIVE)) { 869 /* Not a cone pattern. */ 870 warning(_("unrecognized pattern: '%s'"), given->pattern); 871 goto clear_hashmaps; 872 } 873 874 truncated = dup_and_filter_pattern(given->pattern); 875 876 translated = xmalloc(sizeof(struct pattern_entry)); 877 translated->pattern = truncated; 878 translated->patternlen = given->patternlen - 2; 879 hashmap_entry_init(&translated->ent, 880 fspathhash(translated->pattern)); 881 882 if (!hashmap_get_entry(&pl->recursive_hashmap, 883 translated, ent, NULL)) { 884 /* We did not see the "parent" included */ 885 warning(_("unrecognized negative pattern: '%s'"), 886 given->pattern); 887 free(truncated); 888 free(translated); 889 goto clear_hashmaps; 890 } 891 892 hashmap_add(&pl->parent_hashmap, &translated->ent); 893 old = hashmap_remove_entry(&pl->recursive_hashmap, translated, ent, &data); 894 if (old) { 895 free(old->pattern); 896 free(old); 897 } 898 free(data); 899 return; 900 } 901 902 if (given->flags & PATTERN_FLAG_NEGATIVE) { 903 warning(_("unrecognized negative pattern: '%s'"), 904 given->pattern); 905 goto clear_hashmaps; 906 } 907 908 translated = xmalloc(sizeof(struct pattern_entry)); 909 910 translated->pattern = dup_and_filter_pattern(given->pattern); 911 translated->patternlen = given->patternlen; 912 hashmap_entry_init(&translated->ent, 913 fspathhash(translated->pattern)); 914 915 hashmap_add(&pl->recursive_hashmap, &translated->ent); 916 917 if (hashmap_get_entry(&pl->parent_hashmap, translated, ent, NULL)) { 918 /* we already included this at the parent level */ 919 warning(_("your sparse-checkout file may have issues: pattern '%s' is repeated"), 920 given->pattern); 921 goto clear_hashmaps; 922 } 923 924 return; 925 926clear_hashmaps: 927 warning(_("disabling cone pattern matching")); 928 clear_pattern_entry_hashmap(&pl->recursive_hashmap); 929 clear_pattern_entry_hashmap(&pl->parent_hashmap); 930 pl->use_cone_patterns = 0; 931} 932 933static int hashmap_contains_path(struct hashmap *map, 934 struct strbuf *pattern) 935{ 936 struct pattern_entry p; 937 938 /* Check straight mapping */ 939 p.pattern = pattern->buf; 940 p.patternlen = pattern->len; 941 hashmap_entry_init(&p.ent, fspathhash(p.pattern)); 942 return !!hashmap_get_entry(map, &p, ent, NULL); 943} 944 945int hashmap_contains_parent(struct hashmap *map, 946 const char *path, 947 struct strbuf *buffer) 948{ 949 char *slash_pos; 950 951 strbuf_setlen(buffer, 0); 952 953 if (path[0] != '/') 954 strbuf_addch(buffer, '/'); 955 956 strbuf_addstr(buffer, path); 957 958 slash_pos = strrchr(buffer->buf, '/'); 959 960 while (slash_pos > buffer->buf) { 961 strbuf_setlen(buffer, slash_pos - buffer->buf); 962 963 if (hashmap_contains_path(map, buffer)) 964 return 1; 965 966 slash_pos = strrchr(buffer->buf, '/'); 967 } 968 969 return 0; 970} 971 972void add_pattern(const char *string, const char *base, 973 int baselen, struct pattern_list *pl, int srcpos) 974{ 975 struct path_pattern *pattern; 976 int patternlen; 977 unsigned flags; 978 int nowildcardlen; 979 980 parse_path_pattern(&string, &patternlen, &flags, &nowildcardlen); 981 FLEX_ALLOC_MEM(pattern, pattern, string, patternlen); 982 pattern->patternlen = patternlen; 983 pattern->nowildcardlen = nowildcardlen; 984 pattern->base = base; 985 pattern->baselen = baselen; 986 pattern->flags = flags; 987 pattern->srcpos = srcpos; 988 ALLOC_GROW(pl->patterns, pl->nr + 1, pl->alloc); 989 pl->patterns[pl->nr++] = pattern; 990 pattern->pl = pl; 991 992 add_pattern_to_hashsets(pl, pattern); 993} 994 995static int read_skip_worktree_file_from_index(struct index_state *istate, 996 const char *path, 997 size_t *size_out, char **data_out, 998 struct oid_stat *oid_stat) 999{ 1000 int pos, len; 1001 1002 len = strlen(path); 1003 pos = index_name_pos(istate, path, len); 1004 if (pos < 0) 1005 return -1; 1006 if (!ce_skip_worktree(istate->cache[pos])) 1007 return -1; 1008 1009 return do_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); 1010} 1011 1012/* 1013 * Frees memory within pl which was allocated for exclude patterns and 1014 * the file buffer. Does not free pl itself. 1015 */ 1016void clear_pattern_list(struct pattern_list *pl) 1017{ 1018 int i; 1019 1020 for (i = 0; i < pl->nr; i++) 1021 free(pl->patterns[i]); 1022 free(pl->patterns); 1023 clear_pattern_entry_hashmap(&pl->recursive_hashmap); 1024 clear_pattern_entry_hashmap(&pl->parent_hashmap); 1025 1026 memset(pl, 0, sizeof(*pl)); 1027} 1028 1029static void trim_trailing_spaces(char *buf) 1030{ 1031 char *p, *last_space = NULL; 1032 1033 for (p = buf; *p; p++) 1034 switch (*p) { 1035 case ' ': 1036 if (!last_space) 1037 last_space = p; 1038 break; 1039 case '\\': 1040 p++; 1041 if (!*p) 1042 return; 1043 /* fallthrough */ 1044 default: 1045 last_space = NULL; 1046 } 1047 1048 if (last_space) 1049 *last_space = '\0'; 1050} 1051 1052/* 1053 * Given a subdirectory name and "dir" of the current directory, 1054 * search the subdir in "dir" and return it, or create a new one if it 1055 * does not exist in "dir". 1056 * 1057 * If "name" has the trailing slash, it'll be excluded in the search. 1058 */ 1059static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 1060 struct untracked_cache_dir *dir, 1061 const char *name, int len) 1062{ 1063 int first, last; 1064 struct untracked_cache_dir *d; 1065 if (!dir) 1066 return NULL; 1067 if (len && name[len - 1] == '/') 1068 len--; 1069 first = 0; 1070 last = dir->dirs_nr; 1071 while (last > first) { 1072 int cmp, next = first + ((last - first) >> 1); 1073 d = dir->dirs[next]; 1074 cmp = strncmp(name, d->name, len); 1075 if (!cmp && strlen(d->name) > len) 1076 cmp = -1; 1077 if (!cmp) 1078 return d; 1079 if (cmp < 0) { 1080 last = next; 1081 continue; 1082 } 1083 first = next+1; 1084 } 1085 1086 uc->dir_created++; 1087 FLEX_ALLOC_MEM(d, name, name, len); 1088 1089 ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); 1090 MOVE_ARRAY(dir->dirs + first + 1, dir->dirs + first, 1091 dir->dirs_nr - first); 1092 dir->dirs_nr++; 1093 dir->dirs[first] = d; 1094 return d; 1095} 1096 1097static void do_invalidate_gitignore(struct untracked_cache_dir *dir) 1098{ 1099 int i; 1100 dir->valid = 0; 1101 for (size_t i = 0; i < dir->untracked_nr; i++) 1102 free(dir->untracked[i]); 1103 dir->untracked_nr = 0; 1104 for (i = 0; i < dir->dirs_nr; i++) 1105 do_invalidate_gitignore(dir->dirs[i]); 1106} 1107 1108static void invalidate_gitignore(struct untracked_cache *uc, 1109 struct untracked_cache_dir *dir) 1110{ 1111 uc->gitignore_invalidated++; 1112 do_invalidate_gitignore(dir); 1113} 1114 1115static void invalidate_directory(struct untracked_cache *uc, 1116 struct untracked_cache_dir *dir) 1117{ 1118 int i; 1119 1120 /* 1121 * Invalidation increment here is just roughly correct. If 1122 * untracked_nr or any of dirs[].recurse is non-zero, we 1123 * should increment dir_invalidated too. But that's more 1124 * expensive to do. 1125 */ 1126 if (dir->valid) 1127 uc->dir_invalidated++; 1128 1129 dir->valid = 0; 1130 for (size_t i = 0; i < dir->untracked_nr; i++) 1131 free(dir->untracked[i]); 1132 dir->untracked_nr = 0; 1133 for (i = 0; i < dir->dirs_nr; i++) 1134 dir->dirs[i]->recurse = 0; 1135} 1136 1137/* Flags for add_patterns() */ 1138#define PATTERN_NOFOLLOW (1<<0) 1139 1140/* 1141 * Given a file with name "fname", read it (either from disk, or from 1142 * an index if 'istate' is non-null), parse it and store the 1143 * exclude rules in "pl". 1144 * 1145 * If "oid_stat" is not NULL, compute oid of the exclude file and fill 1146 * stat data from disk (only valid if add_patterns returns zero). If 1147 * oid_stat.valid is non-zero, "oid_stat" must contain good value as input. 1148 */ 1149static int add_patterns(const char *fname, const char *base, int baselen, 1150 struct pattern_list *pl, struct index_state *istate, 1151 unsigned flags, struct oid_stat *oid_stat) 1152{ 1153 struct stat st; 1154 int r; 1155 int fd; 1156 size_t size = 0; 1157 char *buf; 1158 1159 if (flags & PATTERN_NOFOLLOW) 1160 fd = open_nofollow(fname, O_RDONLY); 1161 else 1162 fd = open(fname, O_RDONLY); 1163 1164 if (fd < 0 || fstat(fd, &st) < 0) { 1165 if (fd < 0) 1166 warn_on_fopen_errors(fname); 1167 else 1168 close(fd); 1169 if (!istate) 1170 return -1; 1171 r = read_skip_worktree_file_from_index(istate, fname, 1172 &size, &buf, 1173 oid_stat); 1174 if (r != 1) 1175 return r; 1176 } else { 1177 size = xsize_t(st.st_size); 1178 if (size == 0) { 1179 if (oid_stat) { 1180 fill_stat_data(&oid_stat->stat, &st); 1181 oidcpy(&oid_stat->oid, the_hash_algo->empty_blob); 1182 oid_stat->valid = 1; 1183 } 1184 close(fd); 1185 return 0; 1186 } 1187 buf = xmallocz(size); 1188 if (read_in_full(fd, buf, size) != size) { 1189 free(buf); 1190 close(fd); 1191 return -1; 1192 } 1193 buf[size++] = '\n'; 1194 close(fd); 1195 if (oid_stat) { 1196 int pos; 1197 if (oid_stat->valid && 1198 !match_stat_data_racy(istate, &oid_stat->stat, &st)) 1199 ; /* no content change, oid_stat->oid still good */ 1200 else if (istate && 1201 (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 && 1202 !ce_stage(istate->cache[pos]) && 1203 ce_uptodate(istate->cache[pos]) && 1204 !would_convert_to_git(istate, fname)) 1205 oidcpy(&oid_stat->oid, 1206 &istate->cache[pos]->oid); 1207 else 1208 hash_object_file(the_hash_algo, buf, size, 1209 OBJ_BLOB, &oid_stat->oid); 1210 fill_stat_data(&oid_stat->stat, &st); 1211 oid_stat->valid = 1; 1212 } 1213 } 1214 1215 if (size > PATTERN_MAX_FILE_SIZE) { 1216 warning("ignoring excessively large pattern file: %s", fname); 1217 free(buf); 1218 return -1; 1219 } 1220 1221 add_patterns_from_buffer(buf, size, base, baselen, pl); 1222 free(buf); 1223 return 0; 1224} 1225 1226int add_patterns_from_buffer(char *buf, size_t size, 1227 const char *base, int baselen, 1228 struct pattern_list *pl) 1229{ 1230 char *orig = buf; 1231 int i, lineno = 1; 1232 char *entry; 1233 1234 hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); 1235 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); 1236 1237 if (skip_utf8_bom(&buf, size)) 1238 size -= buf - orig; 1239 1240 entry = buf; 1241 1242 for (i = 0; i < size; i++) { 1243 if (buf[i] == '\n') { 1244 if (entry != buf + i && entry[0] != '#') { 1245 buf[i - (i && buf[i-1] == '\r')] = 0; 1246 trim_trailing_spaces(entry); 1247 add_pattern(entry, base, baselen, pl, lineno); 1248 } 1249 lineno++; 1250 entry = buf + i + 1; 1251 } 1252 } 1253 return 0; 1254} 1255 1256int add_patterns_from_file_to_list(const char *fname, const char *base, 1257 int baselen, struct pattern_list *pl, 1258 struct index_state *istate, 1259 unsigned flags) 1260{ 1261 return add_patterns(fname, base, baselen, pl, istate, flags, NULL); 1262} 1263 1264int add_patterns_from_blob_to_list( 1265 struct object_id *oid, 1266 const char *base, int baselen, 1267 struct pattern_list *pl) 1268{ 1269 char *buf; 1270 size_t size; 1271 int r; 1272 1273 r = do_read_blob(oid, NULL, &size, &buf); 1274 if (r != 1) 1275 return r; 1276 1277 if (size > PATTERN_MAX_FILE_SIZE) { 1278 warning("ignoring excessively large pattern blob: %s", 1279 oid_to_hex(oid)); 1280 free(buf); 1281 return -1; 1282 } 1283 1284 add_patterns_from_buffer(buf, size, base, baselen, pl); 1285 free(buf); 1286 return 0; 1287} 1288 1289struct pattern_list *add_pattern_list(struct dir_struct *dir, 1290 int group_type, const char *src) 1291{ 1292 struct pattern_list *pl; 1293 struct exclude_list_group *group; 1294 1295 group = &dir->internal.exclude_list_group[group_type]; 1296 ALLOC_GROW(group->pl, group->nr + 1, group->alloc); 1297 pl = &group->pl[group->nr++]; 1298 memset(pl, 0, sizeof(*pl)); 1299 pl->src = src; 1300 return pl; 1301} 1302 1303/* 1304 * Used to set up core.excludesfile and .git/info/exclude lists. 1305 */ 1306static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname, 1307 struct oid_stat *oid_stat) 1308{ 1309 struct pattern_list *pl; 1310 /* 1311 * catch setup_standard_excludes() that's called before 1312 * dir->untracked is assigned. That function behaves 1313 * differently when dir->untracked is non-NULL. 1314 */ 1315 if (!dir->untracked) 1316 dir->internal.unmanaged_exclude_files++; 1317 pl = add_pattern_list(dir, EXC_FILE, fname); 1318 if (add_patterns(fname, "", 0, pl, NULL, 0, oid_stat) < 0) 1319 die(_("cannot use %s as an exclude file"), fname); 1320} 1321 1322void add_patterns_from_file(struct dir_struct *dir, const char *fname) 1323{ 1324 dir->internal.unmanaged_exclude_files++; /* see validate_untracked_cache() */ 1325 add_patterns_from_file_1(dir, fname, NULL); 1326} 1327 1328int match_basename(const char *basename, int basenamelen, 1329 const char *pattern, int prefix, int patternlen, 1330 unsigned flags) 1331{ 1332 if (prefix == patternlen) { 1333 if (patternlen == basenamelen && 1334 !fspathncmp(pattern, basename, basenamelen)) 1335 return 1; 1336 } else if (flags & PATTERN_FLAG_ENDSWITH) { 1337 /* "*literal" matching against "fooliteral" */ 1338 if (patternlen - 1 <= basenamelen && 1339 !fspathncmp(pattern + 1, 1340 basename + basenamelen - (patternlen - 1), 1341 patternlen - 1)) 1342 return 1; 1343 } else { 1344 if (fnmatch_icase_mem(pattern, patternlen, 1345 basename, basenamelen, 1346 0) == 0) 1347 return 1; 1348 } 1349 return 0; 1350} 1351 1352int match_pathname(const char *pathname, int pathlen, 1353 const char *base, int baselen, 1354 const char *pattern, int prefix, int patternlen) 1355{ 1356 const char *name; 1357 int namelen; 1358 1359 /* 1360 * match with FNM_PATHNAME; the pattern has base implicitly 1361 * in front of it. 1362 */ 1363 if (*pattern == '/') { 1364 pattern++; 1365 patternlen--; 1366 prefix--; 1367 } 1368 1369 /* 1370 * baselen does not count the trailing slash. base[] may or 1371 * may not end with a trailing slash though. 1372 */ 1373 if (pathlen < baselen + 1 || 1374 (baselen && pathname[baselen] != '/') || 1375 fspathncmp(pathname, base, baselen)) 1376 return 0; 1377 1378 namelen = baselen ? pathlen - baselen - 1 : pathlen; 1379 name = pathname + pathlen - namelen; 1380 1381 if (prefix) { 1382 /* 1383 * if the non-wildcard part is longer than the 1384 * remaining pathname, surely it cannot match. 1385 */ 1386 if (prefix > namelen) 1387 return 0; 1388 1389 if (fspathncmp(pattern, name, prefix)) 1390 return 0; 1391 pattern += prefix; 1392 patternlen -= prefix; 1393 name += prefix; 1394 namelen -= prefix; 1395 1396 /* 1397 * If the whole pattern did not have a wildcard, 1398 * then our prefix match is all we need; we 1399 * do not need to call fnmatch at all. 1400 */ 1401 if (!patternlen && !namelen) 1402 return 1; 1403 } 1404 1405 return fnmatch_icase_mem(pattern, patternlen, 1406 name, namelen, 1407 WM_PATHNAME) == 0; 1408} 1409 1410/* 1411 * Scan the given exclude list in reverse to see whether pathname 1412 * should be ignored. The first match (i.e. the last on the list), if 1413 * any, determines the fate. Returns the exclude_list element which 1414 * matched, or NULL for undecided. 1415 */ 1416static struct path_pattern *last_matching_pattern_from_list(const char *pathname, 1417 int pathlen, 1418 const char *basename, 1419 int *dtype, 1420 struct pattern_list *pl, 1421 struct index_state *istate) 1422{ 1423 struct path_pattern *res = NULL; /* undecided */ 1424 int i; 1425 1426 if (!pl->nr) 1427 return NULL; /* undefined */ 1428 1429 for (i = pl->nr - 1; 0 <= i; i--) { 1430 struct path_pattern *pattern = pl->patterns[i]; 1431 const char *exclude = pattern->pattern; 1432 int prefix = pattern->nowildcardlen; 1433 1434 if (pattern->flags & PATTERN_FLAG_MUSTBEDIR) { 1435 *dtype = resolve_dtype(*dtype, istate, pathname, pathlen); 1436 if (*dtype != DT_DIR) 1437 continue; 1438 } 1439 1440 if (pattern->flags & PATTERN_FLAG_NODIR) { 1441 if (match_basename(basename, 1442 pathlen - (basename - pathname), 1443 exclude, prefix, pattern->patternlen, 1444 pattern->flags)) { 1445 res = pattern; 1446 break; 1447 } 1448 continue; 1449 } 1450 1451 assert(pattern->baselen == 0 || 1452 pattern->base[pattern->baselen - 1] == '/'); 1453 if (match_pathname(pathname, pathlen, 1454 pattern->base, 1455 pattern->baselen ? pattern->baselen - 1 : 0, 1456 exclude, prefix, pattern->patternlen)) { 1457 res = pattern; 1458 break; 1459 } 1460 } 1461 return res; 1462} 1463 1464/* 1465 * Scan the list of patterns to determine if the ordered list 1466 * of patterns matches on 'pathname'. 1467 * 1468 * Return 1 for a match, 0 for not matched and -1 for undecided. 1469 */ 1470enum pattern_match_result path_matches_pattern_list( 1471 const char *pathname, int pathlen, 1472 const char *basename, int *dtype, 1473 struct pattern_list *pl, 1474 struct index_state *istate) 1475{ 1476 struct path_pattern *pattern; 1477 struct strbuf parent_pathname = STRBUF_INIT; 1478 int result = NOT_MATCHED; 1479 size_t slash_pos; 1480 1481 if (!pl->use_cone_patterns) { 1482 pattern = last_matching_pattern_from_list(pathname, pathlen, basename, 1483 dtype, pl, istate); 1484 if (pattern) { 1485 if (pattern->flags & PATTERN_FLAG_NEGATIVE) 1486 return NOT_MATCHED; 1487 else 1488 return MATCHED; 1489 } 1490 1491 return UNDECIDED; 1492 } 1493 1494 if (pl->full_cone) 1495 return MATCHED; 1496 1497 strbuf_addch(&parent_pathname, '/'); 1498 strbuf_add(&parent_pathname, pathname, pathlen); 1499 1500 /* 1501 * Directory entries are matched if and only if a file 1502 * contained immediately within them is matched. For the 1503 * case of a directory entry, modify the path to create 1504 * a fake filename within this directory, allowing us to 1505 * use the file-base matching logic in an equivalent way. 1506 */ 1507 if (parent_pathname.len > 0 && 1508 parent_pathname.buf[parent_pathname.len - 1] == '/') { 1509 slash_pos = parent_pathname.len - 1; 1510 strbuf_add(&parent_pathname, "-", 1); 1511 } else { 1512 const char *slash_ptr = strrchr(parent_pathname.buf, '/'); 1513 slash_pos = slash_ptr ? slash_ptr - parent_pathname.buf : 0; 1514 } 1515 1516 if (hashmap_contains_path(&pl->recursive_hashmap, 1517 &parent_pathname)) { 1518 result = MATCHED_RECURSIVE; 1519 goto done; 1520 } 1521 1522 if (!slash_pos) { 1523 /* include every file in root */ 1524 result = MATCHED; 1525 goto done; 1526 } 1527 1528 strbuf_setlen(&parent_pathname, slash_pos); 1529 1530 if (hashmap_contains_path(&pl->parent_hashmap, &parent_pathname)) { 1531 result = MATCHED; 1532 goto done; 1533 } 1534 1535 if (hashmap_contains_parent(&pl->recursive_hashmap, 1536 pathname, 1537 &parent_pathname)) 1538 result = MATCHED_RECURSIVE; 1539 1540done: 1541 strbuf_release(&parent_pathname); 1542 return result; 1543} 1544 1545int init_sparse_checkout_patterns(struct index_state *istate) 1546{ 1547 if (!core_apply_sparse_checkout) 1548 return 1; 1549 if (istate->sparse_checkout_patterns) 1550 return 0; 1551 1552 CALLOC_ARRAY(istate->sparse_checkout_patterns, 1); 1553 1554 if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0) { 1555 FREE_AND_NULL(istate->sparse_checkout_patterns); 1556 return -1; 1557 } 1558 1559 return 0; 1560} 1561 1562static int path_in_sparse_checkout_1(const char *path, 1563 struct index_state *istate, 1564 int require_cone_mode) 1565{ 1566 int dtype = DT_REG; 1567 enum pattern_match_result match = UNDECIDED; 1568 const char *end, *slash; 1569 1570 /* 1571 * We default to accepting a path if the path is empty, there are no 1572 * patterns, or the patterns are of the wrong type. 1573 */ 1574 if (!*path || 1575 init_sparse_checkout_patterns(istate) || 1576 (require_cone_mode && 1577 !istate->sparse_checkout_patterns->use_cone_patterns)) 1578 return 1; 1579 1580 /* 1581 * If UNDECIDED, use the match from the parent dir (recursively), or 1582 * fall back to NOT_MATCHED at the topmost level. Note that cone mode 1583 * never returns UNDECIDED, so we will execute only one iteration in 1584 * this case. 1585 */ 1586 for (end = path + strlen(path); 1587 end > path && match == UNDECIDED; 1588 end = slash) { 1589 1590 for (slash = end - 1; slash > path && *slash != '/'; slash--) 1591 ; /* do nothing */ 1592 1593 match = path_matches_pattern_list(path, end - path, 1594 slash > path ? slash + 1 : path, &dtype, 1595 istate->sparse_checkout_patterns, istate); 1596 1597 /* We are going to match the parent dir now */ 1598 dtype = DT_DIR; 1599 } 1600 return match > 0; 1601} 1602 1603int path_in_sparse_checkout(const char *path, 1604 struct index_state *istate) 1605{ 1606 return path_in_sparse_checkout_1(path, istate, 0); 1607} 1608 1609int path_in_cone_mode_sparse_checkout(const char *path, 1610 struct index_state *istate) 1611{ 1612 return path_in_sparse_checkout_1(path, istate, 1); 1613} 1614 1615static struct path_pattern *last_matching_pattern_from_lists( 1616 struct dir_struct *dir, struct index_state *istate, 1617 const char *pathname, int pathlen, 1618 const char *basename, int *dtype_p) 1619{ 1620 int i, j; 1621 struct exclude_list_group *group; 1622 struct path_pattern *pattern; 1623 for (i = EXC_CMDL; i <= EXC_FILE; i++) { 1624 group = &dir->internal.exclude_list_group[i]; 1625 for (j = group->nr - 1; j >= 0; j--) { 1626 pattern = last_matching_pattern_from_list( 1627 pathname, pathlen, basename, dtype_p, 1628 &group->pl[j], istate); 1629 if (pattern) 1630 return pattern; 1631 } 1632 } 1633 return NULL; 1634} 1635 1636/* 1637 * Loads the per-directory exclude list for the substring of base 1638 * which has a char length of baselen. 1639 */ 1640static void prep_exclude(struct dir_struct *dir, 1641 struct index_state *istate, 1642 const char *base, int baselen) 1643{ 1644 struct exclude_list_group *group; 1645 struct pattern_list *pl; 1646 struct exclude_stack *stk = NULL; 1647 struct untracked_cache_dir *untracked; 1648 int current; 1649 1650 group = &dir->internal.exclude_list_group[EXC_DIRS]; 1651 1652 /* 1653 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 1654 * which originate from directories not in the prefix of the 1655 * path being checked. 1656 */ 1657 while ((stk = dir->internal.exclude_stack) != NULL) { 1658 if (stk->baselen <= baselen && 1659 !strncmp(dir->internal.basebuf.buf, base, stk->baselen)) 1660 break; 1661 pl = &group->pl[dir->internal.exclude_stack->exclude_ix]; 1662 dir->internal.exclude_stack = stk->prev; 1663 dir->internal.pattern = NULL; 1664 free((char *)pl->src); /* see strbuf_detach() below */ 1665 clear_pattern_list(pl); 1666 free(stk); 1667 group->nr--; 1668 } 1669 1670 /* Skip traversing into sub directories if the parent is excluded */ 1671 if (dir->internal.pattern) 1672 return; 1673 1674 /* 1675 * Lazy initialization. All call sites currently just 1676 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 1677 * them seems lots of work for little benefit. 1678 */ 1679 if (!dir->internal.basebuf.buf) 1680 strbuf_init(&dir->internal.basebuf, PATH_MAX); 1681 1682 /* Read from the parent directories and push them down. */ 1683 current = stk ? stk->baselen : -1; 1684 strbuf_setlen(&dir->internal.basebuf, current < 0 ? 0 : current); 1685 if (dir->untracked) 1686 untracked = stk ? stk->ucd : dir->untracked->root; 1687 else 1688 untracked = NULL; 1689 1690 while (current < baselen) { 1691 const char *cp; 1692 struct oid_stat oid_stat; 1693 1694 CALLOC_ARRAY(stk, 1); 1695 if (current < 0) { 1696 cp = base; 1697 current = 0; 1698 } else { 1699 cp = strchr(base + current + 1, '/'); 1700 if (!cp) 1701 die("oops in prep_exclude"); 1702 cp++; 1703 untracked = 1704 lookup_untracked(dir->untracked, 1705 untracked, 1706 base + current, 1707 cp - base - current); 1708 } 1709 stk->prev = dir->internal.exclude_stack; 1710 stk->baselen = cp - base; 1711 stk->exclude_ix = group->nr; 1712 stk->ucd = untracked; 1713 pl = add_pattern_list(dir, EXC_DIRS, NULL); 1714 strbuf_add(&dir->internal.basebuf, base + current, stk->baselen - current); 1715 assert(stk->baselen == dir->internal.basebuf.len); 1716 1717 /* Abort if the directory is excluded */ 1718 if (stk->baselen) { 1719 int dt = DT_DIR; 1720 dir->internal.basebuf.buf[stk->baselen - 1] = 0; 1721 dir->internal.pattern = last_matching_pattern_from_lists(dir, 1722 istate, 1723 dir->internal.basebuf.buf, stk->baselen - 1, 1724 dir->internal.basebuf.buf + current, &dt); 1725 dir->internal.basebuf.buf[stk->baselen - 1] = '/'; 1726 if (dir->internal.pattern && 1727 dir->internal.pattern->flags & PATTERN_FLAG_NEGATIVE) 1728 dir->internal.pattern = NULL; 1729 if (dir->internal.pattern) { 1730 dir->internal.exclude_stack = stk; 1731 return; 1732 } 1733 } 1734 1735 /* Try to read per-directory file */ 1736 oidclr(&oid_stat.oid, the_repository->hash_algo); 1737 oid_stat.valid = 0; 1738 if (dir->exclude_per_dir && 1739 /* 1740 * If we know that no files have been added in 1741 * this directory (i.e. valid_cached_dir() has 1742 * been executed and set untracked->valid) .. 1743 */ 1744 (!untracked || !untracked->valid || 1745 /* 1746 * .. and .gitignore does not exist before 1747 * (i.e. null exclude_oid). Then we can skip 1748 * loading .gitignore, which would result in 1749 * ENOENT anyway. 1750 */ 1751 !is_null_oid(&untracked->exclude_oid))) { 1752 /* 1753 * dir->internal.basebuf gets reused by the traversal, 1754 * but we need fname to remain unchanged to ensure the 1755 * src member of each struct path_pattern correctly 1756 * back-references its source file. Other invocations 1757 * of add_pattern_list provide stable strings, so we 1758 * strbuf_detach() and free() here in the caller. 1759 */ 1760 struct strbuf sb = STRBUF_INIT; 1761 strbuf_addbuf(&sb, &dir->internal.basebuf); 1762 strbuf_addstr(&sb, dir->exclude_per_dir); 1763 pl->src = strbuf_detach(&sb, NULL); 1764 add_patterns(pl->src, pl->src, stk->baselen, pl, istate, 1765 PATTERN_NOFOLLOW, 1766 untracked ? &oid_stat : NULL); 1767 } 1768 /* 1769 * NEEDSWORK: when untracked cache is enabled, prep_exclude() 1770 * will first be called in valid_cached_dir() then maybe many 1771 * times more in last_matching_pattern(). When the cache is 1772 * used, last_matching_pattern() will not be called and 1773 * reading .gitignore content will be a waste. 1774 * 1775 * So when it's called by valid_cached_dir() and we can get 1776 * .gitignore SHA-1 from the index (i.e. .gitignore is not 1777 * modified on work tree), we could delay reading the 1778 * .gitignore content until we absolutely need it in 1779 * last_matching_pattern(). Be careful about ignore rule 1780 * order, though, if you do that. 1781 */ 1782 if (untracked && 1783 !oideq(&oid_stat.oid, &untracked->exclude_oid)) { 1784 invalidate_gitignore(dir->untracked, untracked); 1785 oidcpy(&untracked->exclude_oid, &oid_stat.oid); 1786 } 1787 dir->internal.exclude_stack = stk; 1788 current = stk->baselen; 1789 } 1790 strbuf_setlen(&dir->internal.basebuf, baselen); 1791} 1792 1793/* 1794 * Loads the exclude lists for the directory containing pathname, then 1795 * scans all exclude lists to determine whether pathname is excluded. 1796 * Returns the exclude_list element which matched, or NULL for 1797 * undecided. 1798 */ 1799struct path_pattern *last_matching_pattern(struct dir_struct *dir, 1800 struct index_state *istate, 1801 const char *pathname, 1802 int *dtype_p) 1803{ 1804 int pathlen = strlen(pathname); 1805 const char *basename = strrchr(pathname, '/'); 1806 basename = (basename) ? basename+1 : pathname; 1807 1808 prep_exclude(dir, istate, pathname, basename-pathname); 1809 1810 if (dir->internal.pattern) 1811 return dir->internal.pattern; 1812 1813 return last_matching_pattern_from_lists(dir, istate, pathname, pathlen, 1814 basename, dtype_p); 1815} 1816 1817/* 1818 * Loads the exclude lists for the directory containing pathname, then 1819 * scans all exclude lists to determine whether pathname is excluded. 1820 * Returns 1 if true, otherwise 0. 1821 */ 1822int is_excluded(struct dir_struct *dir, struct index_state *istate, 1823 const char *pathname, int *dtype_p) 1824{ 1825 struct path_pattern *pattern = 1826 last_matching_pattern(dir, istate, pathname, dtype_p); 1827 if (pattern) 1828 return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1; 1829 return 0; 1830} 1831 1832static struct dir_entry *dir_entry_new(const char *pathname, int len) 1833{ 1834 struct dir_entry *ent; 1835 1836 FLEX_ALLOC_MEM(ent, name, pathname, len); 1837 ent->len = len; 1838 return ent; 1839} 1840 1841static struct dir_entry *dir_add_name(struct dir_struct *dir, 1842 struct index_state *istate, 1843 const char *pathname, int len) 1844{ 1845 if (index_file_exists(istate, pathname, len, ignore_case)) 1846 return NULL; 1847 1848 ALLOC_GROW(dir->entries, dir->nr+1, dir->internal.alloc); 1849 return dir->entries[dir->nr++] = dir_entry_new(pathname, len); 1850} 1851 1852struct dir_entry *dir_add_ignored(struct dir_struct *dir, 1853 struct index_state *istate, 1854 const char *pathname, int len) 1855{ 1856 if (!index_name_is_other(istate, pathname, len)) 1857 return NULL; 1858 1859 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->internal.ignored_alloc); 1860 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); 1861} 1862 1863enum exist_status { 1864 index_nonexistent = 0, 1865 index_directory, 1866 index_gitdir 1867}; 1868 1869/* 1870 * Do not use the alphabetically sorted index to look up 1871 * the directory name; instead, use the case insensitive 1872 * directory hash. 1873 */ 1874static enum exist_status directory_exists_in_index_icase(struct index_state *istate, 1875 const char *dirname, int len) 1876{ 1877 struct cache_entry *ce; 1878 1879 if (index_dir_exists(istate, dirname, len)) 1880 return index_directory; 1881 1882 ce = index_file_exists(istate, dirname, len, ignore_case); 1883 if (ce && S_ISGITLINK(ce->ce_mode)) 1884 return index_gitdir; 1885 1886 return index_nonexistent; 1887} 1888 1889/* 1890 * The index sorts alphabetically by entry name, which 1891 * means that a gitlink sorts as '\0' at the end, while 1892 * a directory (which is defined not as an entry, but as 1893 * the files it contains) will sort with the '/' at the 1894 * end. 1895 */ 1896static enum exist_status directory_exists_in_index(struct index_state *istate, 1897 const char *dirname, int len) 1898{ 1899 int pos; 1900 1901 if (ignore_case) 1902 return directory_exists_in_index_icase(istate, dirname, len); 1903 1904 pos = index_name_pos(istate, dirname, len); 1905 if (pos < 0) 1906 pos = -pos-1; 1907 while (pos < istate->cache_nr) { 1908 const struct cache_entry *ce = istate->cache[pos++]; 1909 unsigned char endchar; 1910 1911 if (strncmp(ce->name, dirname, len)) 1912 break; 1913 endchar = ce->name[len]; 1914 if (endchar > '/') 1915 break; 1916 if (endchar == '/') 1917 return index_directory; 1918 if (!endchar && S_ISGITLINK(ce->ce_mode)) 1919 return index_gitdir; 1920 } 1921 return index_nonexistent; 1922} 1923 1924/* 1925 * When we find a directory when traversing the filesystem, we 1926 * have three distinct cases: 1927 * 1928 * - ignore it 1929 * - see it as a directory 1930 * - recurse into it 1931 * 1932 * and which one we choose depends on a combination of existing 1933 * git index contents and the flags passed into the directory 1934 * traversal routine. 1935 * 1936 * Case 1: If we *already* have entries in the index under that 1937 * directory name, we always recurse into the directory to see 1938 * all the files. 1939 * 1940 * Case 2: If we *already* have that directory name as a gitlink, 1941 * we always continue to see it as a gitlink, regardless of whether 1942 * there is an actual git directory there or not (it might not 1943 * be checked out as a subproject!) 1944 * 1945 * Case 3: if we didn't have it in the index previously, we 1946 * have a few sub-cases: 1947 * 1948 * (a) if DIR_SHOW_OTHER_DIRECTORIES flag is set, we show it as 1949 * just a directory, unless DIR_HIDE_EMPTY_DIRECTORIES is 1950 * also true, in which case we need to check if it contains any 1951 * untracked and / or ignored files. 1952 * (b) if it looks like a git directory and we don't have the 1953 * DIR_NO_GITLINKS flag, then we treat it as a gitlink, and 1954 * show it as a directory. 1955 * (c) otherwise, we recurse into it. 1956 */ 1957static enum path_treatment treat_directory(struct dir_struct *dir, 1958 struct index_state *istate, 1959 struct untracked_cache_dir *untracked, 1960 const char *dirname, int len, int baselen, int excluded, 1961 const struct pathspec *pathspec) 1962{ 1963 /* 1964 * WARNING: From this function, you can return path_recurse or you 1965 * can call read_directory_recursive() (or neither), but 1966 * you CAN'T DO BOTH. 1967 */ 1968 enum path_treatment state; 1969 int matches_how = 0; 1970 int check_only, stop_early; 1971 int old_ignored_nr, old_untracked_nr; 1972 /* The "len-1" is to strip the final '/' */ 1973 enum exist_status status = directory_exists_in_index(istate, dirname, len-1); 1974 1975 if (status == index_directory) 1976 return path_recurse; 1977 if (status == index_gitdir) 1978 return path_none; 1979 if (status != index_nonexistent) 1980 BUG("Unhandled value for directory_exists_in_index: %d\n", status); 1981 1982 /* 1983 * We don't want to descend into paths that don't match the necessary 1984 * patterns. Clearly, if we don't have a pathspec, then we can't check 1985 * for matching patterns. Also, if (excluded) then we know we matched 1986 * the exclusion patterns so as an optimization we can skip checking 1987 * for matching patterns. 1988 */ 1989 if (pathspec && !excluded) { 1990 matches_how = match_pathspec_with_flags(istate, pathspec, 1991 dirname, len, 1992 0 /* prefix */, 1993 NULL /* seen */, 1994 DO_MATCH_LEADING_PATHSPEC); 1995 if (!matches_how) 1996 return path_none; 1997 } 1998 1999 2000 if ((dir->flags & DIR_SKIP_NESTED_GIT) || 2001 !(dir->flags & DIR_NO_GITLINKS)) { 2002 /* 2003 * Determine if `dirname` is a nested repo by confirming that: 2004 * 1) we are in a nonbare repository, and 2005 * 2) `dirname` is not an immediate parent of `the_repository->gitdir`, 2006 * which could occur if the git_dir or worktree location was 2007 * manually configured by the user; see t2205 testcases 1-3 for 2008 * examples where this matters 2009 */ 2010 int nested_repo; 2011 struct strbuf sb = STRBUF_INIT; 2012 strbuf_addstr(&sb, dirname); 2013 nested_repo = is_nonbare_repository_dir(&sb); 2014 2015 if (nested_repo) { 2016 char *real_dirname, *real_gitdir; 2017 strbuf_addstr(&sb, ".git"); 2018 real_dirname = real_pathdup(sb.buf, 1); 2019 real_gitdir = real_pathdup(the_repository->gitdir, 1); 2020 2021 nested_repo = !!strcmp(real_dirname, real_gitdir); 2022 free(real_gitdir); 2023 free(real_dirname); 2024 } 2025 strbuf_release(&sb); 2026 2027 if (nested_repo) { 2028 if ((dir->flags & DIR_SKIP_NESTED_GIT) || 2029 (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC)) 2030 return path_none; 2031 return excluded ? path_excluded : path_untracked; 2032 } 2033 } 2034 2035 if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) { 2036 if (excluded && 2037 (dir->flags & DIR_SHOW_IGNORED_TOO) && 2038 (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) { 2039 2040 /* 2041 * This is an excluded directory and we are 2042 * showing ignored paths that match an exclude 2043 * pattern. (e.g. show directory as ignored 2044 * only if it matches an exclude pattern). 2045 * This path will either be 'path_excluded` 2046 * (if we are showing empty directories or if 2047 * the directory is not empty), or will be 2048 * 'path_none' (empty directory, and we are 2049 * not showing empty directories). 2050 */ 2051 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) 2052 return path_excluded; 2053 2054 if (read_directory_recursive(dir, istate, dirname, len, 2055 untracked, 1, 1, pathspec) == path_excluded) 2056 return path_excluded; 2057 2058 return path_none; 2059 } 2060 return path_recurse; 2061 } 2062 2063 assert(dir->flags & DIR_SHOW_OTHER_DIRECTORIES); 2064 2065 /* 2066 * If we have a pathspec which could match something _below_ this 2067 * directory (e.g. when checking 'subdir/' having a pathspec like 2068 * 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we 2069 * need to recurse. 2070 */ 2071 if (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC) 2072 return path_recurse; 2073 2074 /* Special cases for where this directory is excluded/ignored */ 2075 if (excluded) { 2076 /* 2077 * If DIR_SHOW_OTHER_DIRECTORIES is set and we're not 2078 * hiding empty directories, there is no need to 2079 * recurse into an ignored directory. 2080 */ 2081 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) 2082 return path_excluded; 2083 2084 /* 2085 * Even if we are hiding empty directories, we can still avoid 2086 * recursing into ignored directories for DIR_SHOW_IGNORED_TOO 2087 * if DIR_SHOW_IGNORED_TOO_MODE_MATCHING is also set. 2088 */ 2089 if ((dir->flags & DIR_SHOW_IGNORED_TOO) && 2090 (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) 2091 return path_excluded; 2092 } 2093 2094 /* 2095 * Other than the path_recurse case above, we only need to 2096 * recurse into untracked directories if any of the following 2097 * bits is set: 2098 * - DIR_SHOW_IGNORED (because then we need to determine if 2099 * there are ignored entries below) 2100 * - DIR_SHOW_IGNORED_TOO (same as above) 2101 * - DIR_HIDE_EMPTY_DIRECTORIES (because we have to determine if 2102 * the directory is empty) 2103 */ 2104 if (!excluded && 2105 !(dir->flags & (DIR_SHOW_IGNORED | 2106 DIR_SHOW_IGNORED_TOO | 2107 DIR_HIDE_EMPTY_DIRECTORIES))) { 2108 return path_untracked; 2109 } 2110 2111 /* 2112 * Even if we don't want to know all the paths under an untracked or 2113 * ignored directory, we may still need to go into the directory to 2114 * determine if it is empty (because with DIR_HIDE_EMPTY_DIRECTORIES, 2115 * an empty directory should be path_none instead of path_excluded or 2116 * path_untracked). 2117 */ 2118 check_only = ((dir->flags & DIR_HIDE_EMPTY_DIRECTORIES) && 2119 !(dir->flags & DIR_SHOW_IGNORED_TOO)); 2120 2121 /* 2122 * However, there's another optimization possible as a subset of 2123 * check_only, based on the cases we have to consider: 2124 * A) Directory matches no exclude patterns: 2125 * * Directory is empty => path_none 2126 * * Directory has an untracked file under it => path_untracked 2127 * * Directory has only ignored files under it => path_excluded 2128 * B) Directory matches an exclude pattern: 2129 * * Directory is empty => path_none 2130 * * Directory has an untracked file under it => path_excluded 2131 * * Directory has only ignored files under it => path_excluded 2132 * In case A, we can exit as soon as we've found an untracked 2133 * file but otherwise have to walk all files. In case B, though, 2134 * we can stop at the first file we find under the directory. 2135 */ 2136 stop_early = check_only && excluded; 2137 2138 /* 2139 * If /every/ file within an untracked directory is ignored, then 2140 * we want to treat the directory as ignored (for e.g. status 2141 * --porcelain), without listing the individual ignored files 2142 * underneath. To do so, we'll save the current ignored_nr, and 2143 * pop all the ones added after it if it turns out the entire 2144 * directory is ignored. Also, when DIR_SHOW_IGNORED_TOO and 2145 * !DIR_KEEP_UNTRACKED_CONTENTS then we don't want to show 2146 * untracked paths so will need to pop all those off the last 2147 * after we traverse. 2148 */ 2149 old_ignored_nr = dir->ignored_nr; 2150 old_untracked_nr = dir->nr; 2151 2152 /* Actually recurse into dirname now, we'll fixup the state later. */ 2153 untracked = lookup_untracked(dir->untracked, untracked, 2154 dirname + baselen, len - baselen); 2155 state = read_directory_recursive(dir, istate, dirname, len, untracked, 2156 check_only, stop_early, pathspec); 2157 2158 /* There are a variety of reasons we may need to fixup the state... */ 2159 if (state == path_excluded) { 2160 /* state == path_excluded implies all paths under 2161 * dirname were ignored... 2162 * 2163 * if running e.g. `git status --porcelain --ignored=matching`, 2164 * then we want to see the subpaths that are ignored. 2165 * 2166 * if running e.g. just `git status --porcelain`, then 2167 * we just want the directory itself to be listed as ignored 2168 * and not the individual paths underneath. 2169 */ 2170 int want_ignored_subpaths = 2171 ((dir->flags & DIR_SHOW_IGNORED_TOO) && 2172 (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)); 2173 2174 if (want_ignored_subpaths) { 2175 /* 2176 * with --ignored=matching, we want the subpaths 2177 * INSTEAD of the directory itself. 2178 */ 2179 state = path_none; 2180 } else { 2181 for (int i = old_ignored_nr; i < dir->ignored_nr; i++) 2182 FREE_AND_NULL(dir->ignored[i]); 2183 dir->ignored_nr = old_ignored_nr; 2184 } 2185 } 2186 2187 /* 2188 * We may need to ignore some of the untracked paths we found while 2189 * traversing subdirectories. 2190 */ 2191 if ((dir->flags & DIR_SHOW_IGNORED_TOO) && 2192 !(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) { 2193 for (int i = old_untracked_nr; i < dir->nr; i++) 2194 FREE_AND_NULL(dir->entries[i]); 2195 dir->nr = old_untracked_nr; 2196 } 2197 2198 /* 2199 * If there is nothing under the current directory and we are not 2200 * hiding empty directories, then we need to report on the 2201 * untracked or ignored status of the directory itself. 2202 */ 2203 if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) 2204 state = excluded ? path_excluded : path_untracked; 2205 2206 return state; 2207} 2208 2209/* 2210 * This is an inexact early pruning of any recursive directory 2211 * reading - if the path cannot possibly be in the pathspec, 2212 * return true, and we'll skip it early. 2213 */ 2214static int simplify_away(const char *path, int pathlen, 2215 const struct pathspec *pathspec) 2216{ 2217 int i; 2218 2219 if (!pathspec || !pathspec->nr) 2220 return 0; 2221 2222 GUARD_PATHSPEC(pathspec, 2223 PATHSPEC_FROMTOP | 2224 PATHSPEC_MAXDEPTH | 2225 PATHSPEC_LITERAL | 2226 PATHSPEC_GLOB | 2227 PATHSPEC_ICASE | 2228 PATHSPEC_EXCLUDE | 2229 PATHSPEC_ATTR); 2230 2231 for (i = 0; i < pathspec->nr; i++) { 2232 const struct pathspec_item *item = &pathspec->items[i]; 2233 int len = item->nowildcard_len; 2234 2235 if (len > pathlen) 2236 len = pathlen; 2237 if (!ps_strncmp(item, item->match, path, len)) 2238 return 0; 2239 } 2240 2241 return 1; 2242} 2243 2244/* 2245 * This function tells us whether an excluded path matches a 2246 * list of "interesting" pathspecs. That is, whether a path matched 2247 * by any of the pathspecs could possibly be ignored by excluding 2248 * the specified path. This can happen if: 2249 * 2250 * 1. the path is mentioned explicitly in the pathspec 2251 * 2252 * 2. the path is a directory prefix of some element in the 2253 * pathspec 2254 */ 2255static int exclude_matches_pathspec(const char *path, int pathlen, 2256 const struct pathspec *pathspec) 2257{ 2258 int i; 2259 2260 if (!pathspec || !pathspec->nr) 2261 return 0; 2262 2263 GUARD_PATHSPEC(pathspec, 2264 PATHSPEC_FROMTOP | 2265 PATHSPEC_MAXDEPTH | 2266 PATHSPEC_LITERAL | 2267 PATHSPEC_GLOB | 2268 PATHSPEC_ICASE | 2269 PATHSPEC_EXCLUDE | 2270 PATHSPEC_ATTR); 2271 2272 for (i = 0; i < pathspec->nr; i++) { 2273 const struct pathspec_item *item = &pathspec->items[i]; 2274 int len = item->nowildcard_len; 2275 2276 if (len == pathlen && 2277 !ps_strncmp(item, item->match, path, pathlen)) 2278 return 1; 2279 if (len > pathlen && 2280 item->match[pathlen] == '/' && 2281 !ps_strncmp(item, item->match, path, pathlen)) 2282 return 1; 2283 } 2284 return 0; 2285} 2286 2287static int get_index_dtype(struct index_state *istate, 2288 const char *path, int len) 2289{ 2290 int pos; 2291 const struct cache_entry *ce; 2292 2293 ce = index_file_exists(istate, path, len, 0); 2294 if (ce) { 2295 if (!ce_uptodate(ce)) 2296 return DT_UNKNOWN; 2297 if (S_ISGITLINK(ce->ce_mode)) 2298 return DT_DIR; 2299 /* 2300 * Nobody actually cares about the 2301 * difference between DT_LNK and DT_REG 2302 */ 2303 return DT_REG; 2304 } 2305 2306 /* Try to look it up as a directory */ 2307 pos = index_name_pos(istate, path, len); 2308 if (pos >= 0) 2309 return DT_UNKNOWN; 2310 pos = -pos-1; 2311 while (pos < istate->cache_nr) { 2312 ce = istate->cache[pos++]; 2313 if (strncmp(ce->name, path, len)) 2314 break; 2315 if (ce->name[len] > '/') 2316 break; 2317 if (ce->name[len] < '/') 2318 continue; 2319 if (!ce_uptodate(ce)) 2320 break; /* continue? */ 2321 return DT_DIR; 2322 } 2323 return DT_UNKNOWN; 2324} 2325 2326unsigned char get_dtype(struct dirent *e, struct strbuf *path, 2327 int follow_symlink) 2328{ 2329 struct stat st; 2330 unsigned char dtype = DTYPE(e); 2331 size_t base_path_len; 2332 2333 if (dtype != DT_UNKNOWN && !(follow_symlink && dtype == DT_LNK)) 2334 return dtype; 2335 2336 /* 2337 * d_type unknown or unfollowed symlink, try to fall back on [l]stat 2338 * results. If [l]stat fails, explicitly set DT_UNKNOWN. 2339 */ 2340 base_path_len = path->len; 2341 strbuf_addstr(path, e->d_name); 2342 if ((follow_symlink && stat(path->buf, &st)) || 2343 (!follow_symlink && lstat(path->buf, &st))) 2344 goto cleanup; 2345 2346 /* determine d_type from st_mode */ 2347 if (S_ISREG(st.st_mode)) 2348 dtype = DT_REG; 2349 else if (S_ISDIR(st.st_mode)) 2350 dtype = DT_DIR; 2351 else if (S_ISLNK(st.st_mode)) 2352 dtype = DT_LNK; 2353 2354cleanup: 2355 strbuf_setlen(path, base_path_len); 2356 return dtype; 2357} 2358 2359static int resolve_dtype(int dtype, struct index_state *istate, 2360 const char *path, int len) 2361{ 2362 struct stat st; 2363 2364 if (dtype != DT_UNKNOWN) 2365 return dtype; 2366 dtype = get_index_dtype(istate, path, len); 2367 if (dtype != DT_UNKNOWN) 2368 return dtype; 2369 if (lstat(path, &st)) 2370 return dtype; 2371 if (S_ISREG(st.st_mode)) 2372 return DT_REG; 2373 if (S_ISDIR(st.st_mode)) 2374 return DT_DIR; 2375 if (S_ISLNK(st.st_mode)) 2376 return DT_LNK; 2377 return dtype; 2378} 2379 2380static enum path_treatment treat_path_fast(struct dir_struct *dir, 2381 struct cached_dir *cdir, 2382 struct index_state *istate, 2383 struct strbuf *path, 2384 int baselen, 2385 const struct pathspec *pathspec) 2386{ 2387 /* 2388 * WARNING: From this function, you can return path_recurse or you 2389 * can call read_directory_recursive() (or neither), but 2390 * you CAN'T DO BOTH. 2391 */ 2392 strbuf_setlen(path, baselen); 2393 if (!cdir->ucd) { 2394 strbuf_addstr(path, cdir->file); 2395 return path_untracked; 2396 } 2397 strbuf_addstr(path, cdir->ucd->name); 2398 /* treat_one_path() does this before it calls treat_directory() */ 2399 strbuf_complete(path, '/'); 2400 if (cdir->ucd->check_only) 2401 /* 2402 * check_only is set as a result of treat_directory() getting 2403 * to its bottom. Verify again the same set of directories 2404 * with check_only set. 2405 */ 2406 return read_directory_recursive(dir, istate, path->buf, path->len, 2407 cdir->ucd, 1, 0, pathspec); 2408 /* 2409 * We get path_recurse in the first run when 2410 * directory_exists_in_index() returns index_nonexistent. We 2411 * are sure that new changes in the index does not impact the 2412 * outcome. Return now. 2413 */ 2414 return path_recurse; 2415} 2416 2417static enum path_treatment treat_path(struct dir_struct *dir, 2418 struct untracked_cache_dir *untracked, 2419 struct cached_dir *cdir, 2420 struct index_state *istate, 2421 struct strbuf *path, 2422 int baselen, 2423 const struct pathspec *pathspec) 2424{ 2425 int has_path_in_index, dtype, excluded; 2426 2427 if (!cdir->d_name) 2428 return treat_path_fast(dir, cdir, istate, path, 2429 baselen, pathspec); 2430 if (is_dot_or_dotdot(cdir->d_name) || !fspathcmp(cdir->d_name, ".git")) 2431 return path_none; 2432 strbuf_setlen(path, baselen); 2433 strbuf_addstr(path, cdir->d_name); 2434 if (simplify_away(path->buf, path->len, pathspec)) 2435 return path_none; 2436 2437 dtype = resolve_dtype(cdir->d_type, istate, path->buf, path->len); 2438 2439 /* Always exclude indexed files */ 2440 has_path_in_index = !!index_file_exists(istate, path->buf, path->len, 2441 ignore_case); 2442 if (dtype != DT_DIR && has_path_in_index) 2443 return path_none; 2444 2445 /* 2446 * When we are looking at a directory P in the working tree, 2447 * there are three cases: 2448 * 2449 * (1) P exists in the index. Everything inside the directory P in 2450 * the working tree needs to go when P is checked out from the 2451 * index. 2452 * 2453 * (2) P does not exist in the index, but there is P/Q in the index. 2454 * We know P will stay a directory when we check out the contents 2455 * of the index, but we do not know yet if there is a directory 2456 * P/Q in the working tree to be killed, so we need to recurse. 2457 * 2458 * (3) P does not exist in the index, and there is no P/Q in the index 2459 * to require P to be a directory, either. Only in this case, we 2460 * know that everything inside P will not be killed without 2461 * recursing. 2462 */ 2463 if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && 2464 (dtype == DT_DIR) && 2465 !has_path_in_index && 2466 (directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent)) 2467 return path_none; 2468 2469 excluded = is_excluded(dir, istate, path->buf, &dtype); 2470 2471 /* 2472 * Excluded? If we don't explicitly want to show 2473 * ignored files, ignore it 2474 */ 2475 if (excluded && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO))) 2476 return path_excluded; 2477 2478 switch (dtype) { 2479 default: 2480 return path_none; 2481 case DT_DIR: 2482 /* 2483 * WARNING: Do not ignore/amend the return value from 2484 * treat_directory(), and especially do not change it to return 2485 * path_recurse as that can cause exponential slowdown. 2486 * Instead, modify treat_directory() to return the right value. 2487 */ 2488 strbuf_addch(path, '/'); 2489 return treat_directory(dir, istate, untracked, 2490 path->buf, path->len, 2491 baselen, excluded, pathspec); 2492 case DT_REG: 2493 case DT_LNK: 2494 if (pathspec && 2495 !match_pathspec(istate, pathspec, path->buf, path->len, 2496 0 /* prefix */, NULL /* seen */, 2497 0 /* is_dir */)) 2498 return path_none; 2499 if (excluded) 2500 return path_excluded; 2501 return path_untracked; 2502 } 2503} 2504 2505static void add_untracked(struct untracked_cache_dir *dir, const char *name) 2506{ 2507 if (!dir) 2508 return; 2509 ALLOC_GROW(dir->untracked, dir->untracked_nr + 1, 2510 dir->untracked_alloc); 2511 dir->untracked[dir->untracked_nr++] = xstrdup(name); 2512} 2513 2514static int valid_cached_dir(struct dir_struct *dir, 2515 struct untracked_cache_dir *untracked, 2516 struct index_state *istate, 2517 struct strbuf *path, 2518 int check_only) 2519{ 2520 struct stat st; 2521 2522 if (!untracked) 2523 return 0; 2524 2525 /* 2526 * With fsmonitor, we can trust the untracked cache's valid field. 2527 */ 2528 refresh_fsmonitor(istate); 2529 if (!(dir->untracked->use_fsmonitor && untracked->valid)) { 2530 if (lstat(path->len ? path->buf : ".", &st)) { 2531 memset(&untracked->stat_data, 0, sizeof(untracked->stat_data)); 2532 return 0; 2533 } 2534 if (!untracked->valid || 2535 match_stat_data_racy(istate, &untracked->stat_data, &st)) { 2536 fill_stat_data(&untracked->stat_data, &st); 2537 return 0; 2538 } 2539 } 2540 2541 if (untracked->check_only != !!check_only) 2542 return 0; 2543 2544 /* 2545 * prep_exclude will be called eventually on this directory, 2546 * but it's called much later in last_matching_pattern(). We 2547 * need it now to determine the validity of the cache for this 2548 * path. The next calls will be nearly no-op, the way 2549 * prep_exclude() is designed. 2550 */ 2551 if (path->len && path->buf[path->len - 1] != '/') { 2552 strbuf_addch(path, '/'); 2553 prep_exclude(dir, istate, path->buf, path->len); 2554 strbuf_setlen(path, path->len - 1); 2555 } else 2556 prep_exclude(dir, istate, path->buf, path->len); 2557 2558 /* hopefully prep_exclude() haven't invalidated this entry... */ 2559 return untracked->valid; 2560} 2561 2562static int open_cached_dir(struct cached_dir *cdir, 2563 struct dir_struct *dir, 2564 struct untracked_cache_dir *untracked, 2565 struct index_state *istate, 2566 struct strbuf *path, 2567 int check_only) 2568{ 2569 const char *c_path; 2570 2571 memset(cdir, 0, sizeof(*cdir)); 2572 cdir->untracked = untracked; 2573 if (valid_cached_dir(dir, untracked, istate, path, check_only)) 2574 return 0; 2575 c_path = path->len ? path->buf : "."; 2576 cdir->fdir = opendir(c_path); 2577 if (!cdir->fdir) 2578 warning_errno(_("could not open directory '%s'"), c_path); 2579 if (dir->untracked) { 2580 invalidate_directory(dir->untracked, untracked); 2581 dir->untracked->dir_opened++; 2582 } 2583 if (!cdir->fdir) 2584 return -1; 2585 return 0; 2586} 2587 2588static int read_cached_dir(struct cached_dir *cdir) 2589{ 2590 struct dirent *de; 2591 2592 if (cdir->fdir) { 2593 de = readdir_skip_dot_and_dotdot(cdir->fdir); 2594 if (!de) { 2595 cdir->d_name = NULL; 2596 cdir->d_type = DT_UNKNOWN; 2597 return -1; 2598 } 2599 cdir->d_name = de->d_name; 2600 cdir->d_type = DTYPE(de); 2601 return 0; 2602 } 2603 while (cdir->nr_dirs < cdir->untracked->dirs_nr) { 2604 struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs]; 2605 if (!d->recurse) { 2606 cdir->nr_dirs++; 2607 continue; 2608 } 2609 cdir->ucd = d; 2610 cdir->nr_dirs++; 2611 return 0; 2612 } 2613 cdir->ucd = NULL; 2614 if (cdir->nr_files < cdir->untracked->untracked_nr) { 2615 struct untracked_cache_dir *d = cdir->untracked; 2616 cdir->file = d->untracked[cdir->nr_files++]; 2617 return 0; 2618 } 2619 return -1; 2620} 2621 2622static void close_cached_dir(struct cached_dir *cdir) 2623{ 2624 if (cdir->fdir) 2625 closedir(cdir->fdir); 2626 /* 2627 * We have gone through this directory and found no untracked 2628 * entries. Mark it valid. 2629 */ 2630 if (cdir->untracked) { 2631 cdir->untracked->valid = 1; 2632 cdir->untracked->recurse = 1; 2633 } 2634} 2635 2636static void add_path_to_appropriate_result_list(struct dir_struct *dir, 2637 struct untracked_cache_dir *untracked, 2638 struct cached_dir *cdir, 2639 struct index_state *istate, 2640 struct strbuf *path, 2641 int baselen, 2642 const struct pathspec *pathspec, 2643 enum path_treatment state) 2644{ 2645 /* add the path to the appropriate result list */ 2646 switch (state) { 2647 case path_excluded: 2648 if (dir->flags & DIR_SHOW_IGNORED) 2649 dir_add_name(dir, istate, path->buf, path->len); 2650 else if ((dir->flags & DIR_SHOW_IGNORED_TOO) || 2651 ((dir->flags & DIR_COLLECT_IGNORED) && 2652 exclude_matches_pathspec(path->buf, path->len, 2653 pathspec))) 2654 dir_add_ignored(dir, istate, path->buf, path->len); 2655 break; 2656 2657 case path_untracked: 2658 if (dir->flags & DIR_SHOW_IGNORED) 2659 break; 2660 dir_add_name(dir, istate, path->buf, path->len); 2661 if (cdir->fdir) 2662 add_untracked(untracked, path->buf + baselen); 2663 break; 2664 2665 default: 2666 break; 2667 } 2668} 2669 2670/* 2671 * Read a directory tree. We currently ignore anything but 2672 * directories, regular files and symlinks. That's because git 2673 * doesn't handle them at all yet. Maybe that will change some 2674 * day. 2675 * 2676 * Also, we ignore the name ".git" (even if it is not a directory). 2677 * That likely will not change. 2678 * 2679 * If 'stop_at_first_file' is specified, 'path_excluded' is returned 2680 * to signal that a file was found. This is the least significant value that 2681 * indicates that a file was encountered that does not depend on the order of 2682 * whether an untracked or excluded path was encountered first. 2683 * 2684 * Returns the most significant path_treatment value encountered in the scan. 2685 * If 'stop_at_first_file' is specified, `path_excluded` is the most 2686 * significant path_treatment value that will be returned. 2687 */ 2688 2689static enum path_treatment read_directory_recursive(struct dir_struct *dir, 2690 struct index_state *istate, const char *base, int baselen, 2691 struct untracked_cache_dir *untracked, int check_only, 2692 int stop_at_first_file, const struct pathspec *pathspec) 2693{ 2694 /* 2695 * WARNING: Do NOT recurse unless path_recurse is returned from 2696 * treat_path(). Recursing on any other return value 2697 * can result in exponential slowdown. 2698 */ 2699 struct cached_dir cdir; 2700 enum path_treatment state, subdir_state, dir_state = path_none; 2701 struct strbuf path = STRBUF_INIT; 2702 2703 strbuf_add(&path, base, baselen); 2704 2705 if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only)) 2706 goto out; 2707 dir->internal.visited_directories++; 2708 2709 if (untracked) 2710 untracked->check_only = !!check_only; 2711 2712 while (!read_cached_dir(&cdir)) { 2713 /* check how the file or directory should be treated */ 2714 state = treat_path(dir, untracked, &cdir, istate, &path, 2715 baselen, pathspec); 2716 dir->internal.visited_paths++; 2717 2718 if (state > dir_state) 2719 dir_state = state; 2720 2721 /* recurse into subdir if instructed by treat_path */ 2722 if (state == path_recurse) { 2723 struct untracked_cache_dir *ud; 2724 ud = lookup_untracked(dir->untracked, 2725 untracked, 2726 path.buf + baselen, 2727 path.len - baselen); 2728 subdir_state = 2729 read_directory_recursive(dir, istate, path.buf, 2730 path.len, ud, 2731 check_only, stop_at_first_file, pathspec); 2732 if (subdir_state > dir_state) 2733 dir_state = subdir_state; 2734 2735 if (pathspec && 2736 !match_pathspec(istate, pathspec, path.buf, path.len, 2737 0 /* prefix */, NULL, 2738 0 /* do NOT special case dirs */)) 2739 state = path_none; 2740 } 2741 2742 if (check_only) { 2743 if (stop_at_first_file) { 2744 /* 2745 * If stopping at first file, then 2746 * signal that a file was found by 2747 * returning `path_excluded`. This is 2748 * to return a consistent value 2749 * regardless of whether an ignored or 2750 * excluded file happened to be 2751 * encountered 1st. 2752 * 2753 * In current usage, the 2754 * `stop_at_first_file` is passed when 2755 * an ancestor directory has matched 2756 * an exclude pattern, so any found 2757 * files will be excluded. 2758 */ 2759 if (dir_state >= path_excluded) { 2760 dir_state = path_excluded; 2761 break; 2762 } 2763 } 2764 2765 /* abort early if maximum state has been reached */ 2766 if (dir_state == path_untracked) { 2767 if (cdir.fdir) 2768 add_untracked(untracked, path.buf + baselen); 2769 break; 2770 } 2771 /* skip the add_path_to_appropriate_result_list() */ 2772 continue; 2773 } 2774 2775 add_path_to_appropriate_result_list(dir, untracked, &cdir, 2776 istate, &path, baselen, 2777 pathspec, state); 2778 } 2779 close_cached_dir(&cdir); 2780 out: 2781 strbuf_release(&path); 2782 2783 return dir_state; 2784} 2785 2786int cmp_dir_entry(const void *p1, const void *p2) 2787{ 2788 const struct dir_entry *e1 = *(const struct dir_entry **)p1; 2789 const struct dir_entry *e2 = *(const struct dir_entry **)p2; 2790 2791 return name_compare(e1->name, e1->len, e2->name, e2->len); 2792} 2793 2794/* check if *out lexically strictly contains *in */ 2795int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in) 2796{ 2797 return (out->len < in->len) && 2798 (out->name[out->len - 1] == '/') && 2799 !memcmp(out->name, in->name, out->len); 2800} 2801 2802static int treat_leading_path(struct dir_struct *dir, 2803 struct index_state *istate, 2804 const char *path, int len, 2805 const struct pathspec *pathspec) 2806{ 2807 struct strbuf sb = STRBUF_INIT; 2808 struct strbuf subdir = STRBUF_INIT; 2809 int prevlen, baselen; 2810 const char *cp; 2811 struct cached_dir cdir; 2812 enum path_treatment state = path_none; 2813 2814 /* 2815 * For each directory component of path, we are going to check whether 2816 * that path is relevant given the pathspec. For example, if path is 2817 * foo/bar/baz/ 2818 * then we will ask treat_path() whether we should go into foo, then 2819 * whether we should go into bar, then whether baz is relevant. 2820 * Checking each is important because e.g. if path is 2821 * .git/info/ 2822 * then we need to check .git to know we shouldn't traverse it. 2823 * If the return from treat_path() is: 2824 * * path_none, for any path, we return false. 2825 * * path_recurse, for all path components, we return true 2826 * * <anything else> for some intermediate component, we make sure 2827 * to add that path to the relevant list but return false 2828 * signifying that we shouldn't recurse into it. 2829 */ 2830 2831 while (len && path[len - 1] == '/') 2832 len--; 2833 if (!len) 2834 return 1; 2835 2836 memset(&cdir, 0, sizeof(cdir)); 2837 cdir.d_type = DT_DIR; 2838 baselen = 0; 2839 prevlen = 0; 2840 while (1) { 2841 prevlen = baselen + !!baselen; 2842 cp = path + prevlen; 2843 cp = memchr(cp, '/', path + len - cp); 2844 if (!cp) 2845 baselen = len; 2846 else 2847 baselen = cp - path; 2848 strbuf_reset(&sb); 2849 strbuf_add(&sb, path, baselen); 2850 if (!is_directory(sb.buf)) 2851 break; 2852 strbuf_reset(&sb); 2853 strbuf_add(&sb, path, prevlen); 2854 strbuf_reset(&subdir); 2855 strbuf_add(&subdir, path+prevlen, baselen-prevlen); 2856 cdir.d_name = subdir.buf; 2857 state = treat_path(dir, NULL, &cdir, istate, &sb, prevlen, pathspec); 2858 2859 if (state != path_recurse) 2860 break; /* do not recurse into it */ 2861 if (len <= baselen) 2862 break; /* finished checking */ 2863 } 2864 add_path_to_appropriate_result_list(dir, NULL, &cdir, istate, 2865 &sb, baselen, pathspec, 2866 state); 2867 2868 strbuf_release(&subdir); 2869 strbuf_release(&sb); 2870 return state == path_recurse; 2871} 2872 2873static const char *get_ident_string(void) 2874{ 2875 static struct strbuf sb = STRBUF_INIT; 2876 struct utsname uts; 2877 2878 if (sb.len) 2879 return sb.buf; 2880 if (uname(&uts) < 0) 2881 die_errno(_("failed to get kernel name and information")); 2882 strbuf_addf(&sb, "Location %s, system %s", repo_get_work_tree(the_repository), 2883 uts.sysname); 2884 return sb.buf; 2885} 2886 2887static int ident_in_untracked(const struct untracked_cache *uc) 2888{ 2889 /* 2890 * Previous git versions may have saved many NUL separated 2891 * strings in the "ident" field, but it is insane to manage 2892 * many locations, so just take care of the first one. 2893 */ 2894 2895 return !strcmp(uc->ident.buf, get_ident_string()); 2896} 2897 2898static void set_untracked_ident(struct untracked_cache *uc) 2899{ 2900 strbuf_reset(&uc->ident); 2901 strbuf_addstr(&uc->ident, get_ident_string()); 2902 2903 /* 2904 * This strbuf used to contain a list of NUL separated 2905 * strings, so save NUL too for backward compatibility. 2906 */ 2907 strbuf_addch(&uc->ident, 0); 2908} 2909 2910static unsigned new_untracked_cache_flags(struct index_state *istate) 2911{ 2912 struct repository *repo = istate->repo; 2913 const char *val; 2914 2915 /* 2916 * This logic is coordinated with the setting of these flags in 2917 * wt-status.c#wt_status_collect_untracked(), and the evaluation 2918 * of the config setting in commit.c#git_status_config() 2919 */ 2920 if (!repo_config_get_string_tmp(repo, "status.showuntrackedfiles", &val) && 2921 !strcmp(val, "all")) 2922 return 0; 2923 2924 /* 2925 * The default, if "all" is not set, is "normal" - leading us here. 2926 * If the value is "none" then it really doesn't matter. 2927 */ 2928 return DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; 2929} 2930 2931static void new_untracked_cache(struct index_state *istate, int flags) 2932{ 2933 struct untracked_cache *uc = xcalloc(1, sizeof(*uc)); 2934 strbuf_init(&uc->ident, 100); 2935 uc->exclude_per_dir = ".gitignore"; 2936 uc->dir_flags = flags >= 0 ? flags : new_untracked_cache_flags(istate); 2937 set_untracked_ident(uc); 2938 istate->untracked = uc; 2939 istate->cache_changed |= UNTRACKED_CHANGED; 2940} 2941 2942void add_untracked_cache(struct index_state *istate) 2943{ 2944 if (!istate->untracked) { 2945 new_untracked_cache(istate, -1); 2946 } else { 2947 if (!ident_in_untracked(istate->untracked)) { 2948 free_untracked_cache(istate->untracked); 2949 new_untracked_cache(istate, -1); 2950 } 2951 } 2952} 2953 2954void remove_untracked_cache(struct index_state *istate) 2955{ 2956 if (istate->untracked) { 2957 free_untracked_cache(istate->untracked); 2958 istate->untracked = NULL; 2959 istate->cache_changed |= UNTRACKED_CHANGED; 2960 } 2961} 2962 2963static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir, 2964 int base_len, 2965 const struct pathspec *pathspec, 2966 struct index_state *istate) 2967{ 2968 struct untracked_cache_dir *root; 2969 static int untracked_cache_disabled = -1; 2970 2971 if (!dir->untracked) 2972 return NULL; 2973 if (untracked_cache_disabled < 0) 2974 untracked_cache_disabled = git_env_bool("GIT_DISABLE_UNTRACKED_CACHE", 0); 2975 if (untracked_cache_disabled) 2976 return NULL; 2977 2978 /* 2979 * We only support $GIT_DIR/info/exclude and core.excludesfile 2980 * as the global ignore rule files. Any other additions 2981 * (e.g. from command line) invalidate the cache. This 2982 * condition also catches running setup_standard_excludes() 2983 * before setting dir->untracked! 2984 */ 2985 if (dir->internal.unmanaged_exclude_files) 2986 return NULL; 2987 2988 /* 2989 * Optimize for the main use case only: whole-tree git 2990 * status. More work involved in treat_leading_path() if we 2991 * use cache on just a subset of the worktree. pathspec 2992 * support could make the matter even worse. 2993 */ 2994 if (base_len || (pathspec && pathspec->nr)) 2995 return NULL; 2996 2997 /* We don't support collecting ignore files */ 2998 if (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO | 2999 DIR_COLLECT_IGNORED)) 3000 return NULL; 3001 3002 /* 3003 * If we use .gitignore in the cache and now you change it to 3004 * .gitexclude, everything will go wrong. 3005 */ 3006 if (dir->exclude_per_dir != dir->untracked->exclude_per_dir && 3007 strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir)) 3008 return NULL; 3009 3010 /* 3011 * EXC_CMDL is not considered in the cache. If people set it, 3012 * skip the cache. 3013 */ 3014 if (dir->internal.exclude_list_group[EXC_CMDL].nr) 3015 return NULL; 3016 3017 if (!ident_in_untracked(dir->untracked)) { 3018 warning(_("untracked cache is disabled on this system or location")); 3019 return NULL; 3020 } 3021 3022 /* 3023 * If the untracked structure we received does not have the same flags 3024 * as requested in this run, we're going to need to either discard the 3025 * existing structure (and potentially later recreate), or bypass the 3026 * untracked cache mechanism for this run. 3027 */ 3028 if (dir->flags != dir->untracked->dir_flags) { 3029 /* 3030 * If the untracked structure we received does not have the same flags 3031 * as configured, then we need to reset / create a new "untracked" 3032 * structure to match the new config. 3033 * 3034 * Keeping the saved and used untracked cache consistent with the 3035 * configuration provides an opportunity for frequent users of 3036 * "git status -uall" to leverage the untracked cache by aligning their 3037 * configuration - setting "status.showuntrackedfiles" to "all" or 3038 * "normal" as appropriate. 3039 * 3040 * Previously using -uall (or setting "status.showuntrackedfiles" to 3041 * "all") was incompatible with untracked cache and *consistently* 3042 * caused surprisingly bad performance (with fscache and fsmonitor 3043 * enabled) on Windows. 3044 * 3045 * IMPROVEMENT OPPORTUNITY: If we reworked the untracked cache storage 3046 * to not be as bound up with the desired output in a given run, 3047 * and instead iterated through and stored enough information to 3048 * correctly serve both "modes", then users could get peak performance 3049 * with or without '-uall' regardless of their 3050 * "status.showuntrackedfiles" config. 3051 */ 3052 if (dir->untracked->dir_flags != new_untracked_cache_flags(istate)) { 3053 free_untracked_cache(istate->untracked); 3054 new_untracked_cache(istate, dir->flags); 3055 dir->untracked = istate->untracked; 3056 } 3057 else { 3058 /* 3059 * Current untracked cache data is consistent with config, but not 3060 * usable in this request/run; just bypass untracked cache. 3061 */ 3062 return NULL; 3063 } 3064 } 3065 3066 if (!dir->untracked->root) { 3067 /* Untracked cache existed but is not initialized; fix that */ 3068 FLEX_ALLOC_STR(dir->untracked->root, name, ""); 3069 istate->cache_changed |= UNTRACKED_CHANGED; 3070 } 3071 3072 /* Validate $GIT_DIR/info/exclude and core.excludesfile */ 3073 root = dir->untracked->root; 3074 if (!oideq(&dir->internal.ss_info_exclude.oid, 3075 &dir->untracked->ss_info_exclude.oid)) { 3076 invalidate_gitignore(dir->untracked, root); 3077 dir->untracked->ss_info_exclude = dir->internal.ss_info_exclude; 3078 } 3079 if (!oideq(&dir->internal.ss_excludes_file.oid, 3080 &dir->untracked->ss_excludes_file.oid)) { 3081 invalidate_gitignore(dir->untracked, root); 3082 dir->untracked->ss_excludes_file = dir->internal.ss_excludes_file; 3083 } 3084 3085 /* Make sure this directory is not dropped out at saving phase */ 3086 root->recurse = 1; 3087 return root; 3088} 3089 3090static void emit_traversal_statistics(struct dir_struct *dir, 3091 struct repository *repo, 3092 const char *path, 3093 int path_len) 3094{ 3095 if (!trace2_is_enabled()) 3096 return; 3097 3098 if (!path_len) { 3099 trace2_data_string("read_directory", repo, "path", ""); 3100 } else { 3101 struct strbuf tmp = STRBUF_INIT; 3102 strbuf_add(&tmp, path, path_len); 3103 trace2_data_string("read_directory", repo, "path", tmp.buf); 3104 strbuf_release(&tmp); 3105 } 3106 3107 trace2_data_intmax("read_directory", repo, 3108 "directories-visited", dir->internal.visited_directories); 3109 trace2_data_intmax("read_directory", repo, 3110 "paths-visited", dir->internal.visited_paths); 3111 3112 if (!dir->untracked) 3113 return; 3114 trace2_data_intmax("read_directory", repo, 3115 "node-creation", dir->untracked->dir_created); 3116 trace2_data_intmax("read_directory", repo, 3117 "gitignore-invalidation", 3118 dir->untracked->gitignore_invalidated); 3119 trace2_data_intmax("read_directory", repo, 3120 "directory-invalidation", 3121 dir->untracked->dir_invalidated); 3122 trace2_data_intmax("read_directory", repo, 3123 "opendir", dir->untracked->dir_opened); 3124} 3125 3126int read_directory(struct dir_struct *dir, struct index_state *istate, 3127 const char *path, int len, const struct pathspec *pathspec) 3128{ 3129 struct untracked_cache_dir *untracked; 3130 3131 trace2_region_enter("dir", "read_directory", istate->repo); 3132 dir->internal.visited_paths = 0; 3133 dir->internal.visited_directories = 0; 3134 3135 if (has_symlink_leading_path(path, len)) { 3136 trace2_region_leave("dir", "read_directory", istate->repo); 3137 return dir->nr; 3138 } 3139 3140 untracked = validate_untracked_cache(dir, len, pathspec, istate); 3141 if (!untracked) 3142 /* 3143 * make sure untracked cache code path is disabled, 3144 * e.g. prep_exclude() 3145 */ 3146 dir->untracked = NULL; 3147 if (!len || treat_leading_path(dir, istate, path, len, pathspec)) 3148 read_directory_recursive(dir, istate, path, len, untracked, 0, 0, pathspec); 3149 QSORT(dir->entries, dir->nr, cmp_dir_entry); 3150 QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry); 3151 3152 emit_traversal_statistics(dir, istate->repo, path, len); 3153 3154 trace2_region_leave("dir", "read_directory", istate->repo); 3155 if (dir->untracked) { 3156 static int force_untracked_cache = -1; 3157 3158 if (force_untracked_cache < 0) 3159 force_untracked_cache = 3160 git_env_bool("GIT_FORCE_UNTRACKED_CACHE", -1); 3161 if (force_untracked_cache < 0) 3162 force_untracked_cache = (istate->repo->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE); 3163 if (force_untracked_cache && 3164 dir->untracked == istate->untracked && 3165 (dir->untracked->dir_opened || 3166 dir->untracked->gitignore_invalidated || 3167 dir->untracked->dir_invalidated)) 3168 istate->cache_changed |= UNTRACKED_CHANGED; 3169 if (dir->untracked != istate->untracked) { 3170 FREE_AND_NULL(dir->untracked); 3171 } 3172 } 3173 3174 return dir->nr; 3175} 3176 3177int file_exists(const char *f) 3178{ 3179 struct stat sb; 3180 return lstat(f, &sb) == 0; 3181} 3182 3183int repo_file_exists(struct repository *repo, const char *path) 3184{ 3185 if (repo != the_repository) 3186 BUG("do not know how to check file existence in arbitrary repo"); 3187 3188 return file_exists(path); 3189} 3190 3191static int cmp_icase(char a, char b) 3192{ 3193 if (a == b) 3194 return 0; 3195 if (ignore_case) 3196 return toupper(a) - toupper(b); 3197 return a - b; 3198} 3199 3200/* 3201 * Given two normalized paths (a trailing slash is ok), if subdir is 3202 * outside dir, return -1. Otherwise return the offset in subdir that 3203 * can be used as relative path to dir. 3204 */ 3205int dir_inside_of(const char *subdir, const char *dir) 3206{ 3207 int offset = 0; 3208 3209 assert(dir && subdir && *dir && *subdir); 3210 3211 while (*dir && *subdir && !cmp_icase(*dir, *subdir)) { 3212 dir++; 3213 subdir++; 3214 offset++; 3215 } 3216 3217 /* hel[p]/me vs hel[l]/yeah */ 3218 if (*dir && *subdir) 3219 return -1; 3220 3221 if (!*subdir) 3222 return !*dir ? offset : -1; /* same dir */ 3223 3224 /* foo/[b]ar vs foo/[] */ 3225 if (is_dir_sep(dir[-1])) 3226 return is_dir_sep(subdir[-1]) ? offset : -1; 3227 3228 /* foo[/]bar vs foo[] */ 3229 return is_dir_sep(*subdir) ? offset + 1 : -1; 3230} 3231 3232int is_inside_dir(const char *dir) 3233{ 3234 char *cwd; 3235 int rc; 3236 3237 if (!dir) 3238 return 0; 3239 3240 cwd = xgetcwd(); 3241 rc = (dir_inside_of(cwd, dir) >= 0); 3242 free(cwd); 3243 return rc; 3244} 3245 3246int is_empty_dir(const char *path) 3247{ 3248 DIR *dir = opendir(path); 3249 struct dirent *e; 3250 int ret = 1; 3251 3252 if (!dir) 3253 return 0; 3254 3255 e = readdir_skip_dot_and_dotdot(dir); 3256 if (e) 3257 ret = 0; 3258 3259 closedir(dir); 3260 return ret; 3261} 3262 3263char *git_url_basename(const char *repo, int is_bundle, int is_bare) 3264{ 3265 const char *end = repo + strlen(repo), *start, *ptr; 3266 size_t len; 3267 char *dir; 3268 3269 /* 3270 * Skip scheme. 3271 */ 3272 start = strstr(repo, "://"); 3273 if (!start) 3274 start = repo; 3275 else 3276 start += 3; 3277 3278 /* 3279 * Skip authentication data. The stripping does happen 3280 * greedily, such that we strip up to the last '@' inside 3281 * the host part. 3282 */ 3283 for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) { 3284 if (*ptr == '@') 3285 start = ptr + 1; 3286 } 3287 3288 /* 3289 * Strip trailing spaces, slashes and /.git 3290 */ 3291 while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1]))) 3292 end--; 3293 if (end - start > 5 && is_dir_sep(end[-5]) && 3294 !strncmp(end - 4, ".git", 4)) { 3295 end -= 5; 3296 while (start < end && is_dir_sep(end[-1])) 3297 end--; 3298 } 3299 3300 /* 3301 * It should not be possible to overflow `ptrdiff_t` by passing in an 3302 * insanely long URL, but GCC does not know that and will complain 3303 * without this check. 3304 */ 3305 if (end - start < 0) 3306 die(_("No directory name could be guessed.\n" 3307 "Please specify a directory on the command line")); 3308 3309 /* 3310 * Strip trailing port number if we've got only a 3311 * hostname (that is, there is no dir separator but a 3312 * colon). This check is required such that we do not 3313 * strip URI's like '/foo/bar:2222.git', which should 3314 * result in a dir '2222' being guessed due to backwards 3315 * compatibility. 3316 */ 3317 if (memchr(start, '/', end - start) == NULL 3318 && memchr(start, ':', end - start) != NULL) { 3319 ptr = end; 3320 while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':') 3321 ptr--; 3322 if (start < ptr && ptr[-1] == ':') 3323 end = ptr - 1; 3324 } 3325 3326 /* 3327 * Find last component. To remain backwards compatible we 3328 * also regard colons as path separators, such that 3329 * cloning a repository 'foo:bar.git' would result in a 3330 * directory 'bar' being guessed. 3331 */ 3332 ptr = end; 3333 while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':') 3334 ptr--; 3335 start = ptr; 3336 3337 /* 3338 * Strip .{bundle,git}. 3339 */ 3340 len = end - start; 3341 strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git"); 3342 3343 if (!len || (len == 1 && *start == '/')) 3344 die(_("No directory name could be guessed.\n" 3345 "Please specify a directory on the command line")); 3346 3347 if (is_bare) 3348 dir = xstrfmt("%.*s.git", (int)len, start); 3349 else 3350 dir = xstrndup(start, len); 3351 /* 3352 * Replace sequences of 'control' characters and whitespace 3353 * with one ascii space, remove leading and trailing spaces. 3354 */ 3355 if (*dir) { 3356 char *out = dir; 3357 int prev_space = 1 /* strip leading whitespace */; 3358 for (end = dir; *end; ++end) { 3359 char ch = *end; 3360 if ((unsigned char)ch < '\x20') 3361 ch = '\x20'; 3362 if (isspace(ch)) { 3363 if (prev_space) 3364 continue; 3365 prev_space = 1; 3366 } else 3367 prev_space = 0; 3368 *out++ = ch; 3369 } 3370 *out = '\0'; 3371 if (out > dir && prev_space) 3372 out[-1] = '\0'; 3373 } 3374 return dir; 3375} 3376 3377void strip_dir_trailing_slashes(char *dir) 3378{ 3379 char *end = dir + strlen(dir); 3380 3381 while (dir < end - 1 && is_dir_sep(end[-1])) 3382 end--; 3383 *end = '\0'; 3384} 3385 3386static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) 3387{ 3388 DIR *dir; 3389 struct dirent *e; 3390 int ret = 0, original_len = path->len, len, kept_down = 0; 3391 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY); 3392 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL); 3393 int purge_original_cwd = (flag & REMOVE_DIR_PURGE_ORIGINAL_CWD); 3394 struct object_id submodule_head; 3395 3396 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && 3397 !repo_resolve_gitlink_ref(the_repository, path->buf, 3398 "HEAD", &submodule_head)) { 3399 /* Do not descend and nuke a nested git work tree. */ 3400 if (kept_up) 3401 *kept_up = 1; 3402 return 0; 3403 } 3404 3405 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL; 3406 dir = opendir(path->buf); 3407 if (!dir) { 3408 if (errno == ENOENT) 3409 return keep_toplevel ? -1 : 0; 3410 else if (errno == EACCES && !keep_toplevel) 3411 /* 3412 * An empty dir could be removable even if it 3413 * is unreadable: 3414 */ 3415 return rmdir(path->buf); 3416 else 3417 return -1; 3418 } 3419 strbuf_complete(path, '/'); 3420 3421 len = path->len; 3422 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) { 3423 struct stat st; 3424 3425 strbuf_setlen(path, len); 3426 strbuf_addstr(path, e->d_name); 3427 if (lstat(path->buf, &st)) { 3428 if (errno == ENOENT) 3429 /* 3430 * file disappeared, which is what we 3431 * wanted anyway 3432 */ 3433 continue; 3434 /* fall through */ 3435 } else if (S_ISDIR(st.st_mode)) { 3436 if (!remove_dir_recurse(path, flag, &kept_down)) 3437 continue; /* happy */ 3438 } else if (!only_empty && 3439 (!unlink(path->buf) || errno == ENOENT)) { 3440 continue; /* happy, too */ 3441 } 3442 3443 /* path too long, stat fails, or non-directory still exists */ 3444 ret = -1; 3445 break; 3446 } 3447 closedir(dir); 3448 3449 strbuf_setlen(path, original_len); 3450 if (!ret && !keep_toplevel && !kept_down) { 3451 if (!purge_original_cwd && 3452 startup_info->original_cwd && 3453 !strcmp(startup_info->original_cwd, path->buf)) 3454 ret = -1; /* Do not remove current working directory */ 3455 else 3456 ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; 3457 } else if (kept_up) 3458 /* 3459 * report the uplevel that it is not an error that we 3460 * did not rmdir() our directory. 3461 */ 3462 *kept_up = !ret; 3463 return ret; 3464} 3465 3466int remove_dir_recursively(struct strbuf *path, int flag) 3467{ 3468 return remove_dir_recurse(path, flag, NULL); 3469} 3470 3471static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude") 3472 3473void setup_standard_excludes(struct dir_struct *dir) 3474{ 3475 dir->exclude_per_dir = ".gitignore"; 3476 3477 /* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */ 3478 if (!excludes_file) 3479 excludes_file = xdg_config_home("ignore"); 3480 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) 3481 add_patterns_from_file_1(dir, excludes_file, 3482 dir->untracked ? &dir->internal.ss_excludes_file : NULL); 3483 3484 /* per repository user preference */ 3485 if (startup_info->have_repository) { 3486 const char *path = git_path_info_exclude(); 3487 if (!access_or_warn(path, R_OK, 0)) 3488 add_patterns_from_file_1(dir, path, 3489 dir->untracked ? &dir->internal.ss_info_exclude : NULL); 3490 } 3491} 3492 3493char *get_sparse_checkout_filename(void) 3494{ 3495 return repo_git_path(the_repository, "info/sparse-checkout"); 3496} 3497 3498int get_sparse_checkout_patterns(struct pattern_list *pl) 3499{ 3500 int res; 3501 char *sparse_filename = get_sparse_checkout_filename(); 3502 3503 pl->use_cone_patterns = core_sparse_checkout_cone; 3504 res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL, 0); 3505 3506 free(sparse_filename); 3507 return res; 3508} 3509 3510int remove_path(const char *name) 3511{ 3512 char *slash; 3513 3514 if (unlink(name) && !is_missing_file_error(errno)) 3515 return -1; 3516 3517 slash = strrchr(name, '/'); 3518 if (slash) { 3519 char *dirs = xstrdup(name); 3520 slash = dirs + (slash - name); 3521 do { 3522 *slash = '\0'; 3523 if (startup_info->original_cwd && 3524 !strcmp(startup_info->original_cwd, dirs)) 3525 break; 3526 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); 3527 free(dirs); 3528 } 3529 return 0; 3530} 3531 3532/* 3533 * Frees memory within dir which was allocated, and resets fields for further 3534 * use. Does not free dir itself. 3535 */ 3536void dir_clear(struct dir_struct *dir) 3537{ 3538 int i, j; 3539 struct exclude_list_group *group; 3540 struct pattern_list *pl; 3541 struct exclude_stack *stk; 3542 struct dir_struct new = DIR_INIT; 3543 3544 for (i = EXC_CMDL; i <= EXC_FILE; i++) { 3545 group = &dir->internal.exclude_list_group[i]; 3546 for (j = 0; j < group->nr; j++) { 3547 pl = &group->pl[j]; 3548 if (i == EXC_DIRS) 3549 free((char *)pl->src); 3550 clear_pattern_list(pl); 3551 } 3552 free(group->pl); 3553 } 3554 3555 for (i = 0; i < dir->ignored_nr; i++) 3556 free(dir->ignored[i]); 3557 for (i = 0; i < dir->nr; i++) 3558 free(dir->entries[i]); 3559 free(dir->ignored); 3560 free(dir->entries); 3561 3562 stk = dir->internal.exclude_stack; 3563 while (stk) { 3564 struct exclude_stack *prev = stk->prev; 3565 free(stk); 3566 stk = prev; 3567 } 3568 strbuf_release(&dir->internal.basebuf); 3569 3570 memcpy(dir, &new, sizeof(*dir)); 3571} 3572 3573struct ondisk_untracked_cache { 3574 struct stat_data info_exclude_stat; 3575 struct stat_data excludes_file_stat; 3576 uint32_t dir_flags; 3577}; 3578 3579#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x) 3580 3581struct write_data { 3582 int index; /* number of written untracked_cache_dir */ 3583 struct ewah_bitmap *check_only; /* from untracked_cache_dir */ 3584 struct ewah_bitmap *valid; /* from untracked_cache_dir */ 3585 struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */ 3586 struct strbuf out; 3587 struct strbuf sb_stat; 3588 struct strbuf sb_sha1; 3589}; 3590 3591static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from) 3592{ 3593 to->sd_ctime.sec = htonl(from->sd_ctime.sec); 3594 to->sd_ctime.nsec = htonl(from->sd_ctime.nsec); 3595 to->sd_mtime.sec = htonl(from->sd_mtime.sec); 3596 to->sd_mtime.nsec = htonl(from->sd_mtime.nsec); 3597 to->sd_dev = htonl(from->sd_dev); 3598 to->sd_ino = htonl(from->sd_ino); 3599 to->sd_uid = htonl(from->sd_uid); 3600 to->sd_gid = htonl(from->sd_gid); 3601 to->sd_size = htonl(from->sd_size); 3602} 3603 3604static void write_one_dir(struct untracked_cache_dir *untracked, 3605 struct write_data *wd) 3606{ 3607 struct stat_data stat_data; 3608 struct strbuf *out = &wd->out; 3609 unsigned char intbuf[16]; 3610 unsigned int value; 3611 uint8_t intlen; 3612 int i = wd->index++; 3613 3614 /* 3615 * untracked_nr should be reset whenever valid is clear, but 3616 * for safety.. 3617 */ 3618 if (!untracked->valid) { 3619 for (size_t i = 0; i < untracked->untracked_nr; i++) 3620 free(untracked->untracked[i]); 3621 untracked->untracked_nr = 0; 3622 untracked->check_only = 0; 3623 } 3624 3625 if (untracked->check_only) 3626 ewah_set(wd->check_only, i); 3627 if (untracked->valid) { 3628 ewah_set(wd->valid, i); 3629 stat_data_to_disk(&stat_data, &untracked->stat_data); 3630 strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data)); 3631 } 3632 if (!is_null_oid(&untracked->exclude_oid)) { 3633 ewah_set(wd->sha1_valid, i); 3634 strbuf_add(&wd->sb_sha1, untracked->exclude_oid.hash, 3635 the_hash_algo->rawsz); 3636 } 3637 3638 intlen = encode_varint(untracked->untracked_nr, intbuf); 3639 strbuf_add(out, intbuf, intlen); 3640 3641 /* skip non-recurse directories */ 3642 for (i = 0, value = 0; i < untracked->dirs_nr; i++) 3643 if (untracked->dirs[i]->recurse) 3644 value++; 3645 intlen = encode_varint(value, intbuf); 3646 strbuf_add(out, intbuf, intlen); 3647 3648 strbuf_add(out, untracked->name, strlen(untracked->name) + 1); 3649 3650 for (i = 0; i < untracked->untracked_nr; i++) 3651 strbuf_add(out, untracked->untracked[i], 3652 strlen(untracked->untracked[i]) + 1); 3653 3654 for (i = 0; i < untracked->dirs_nr; i++) 3655 if (untracked->dirs[i]->recurse) 3656 write_one_dir(untracked->dirs[i], wd); 3657} 3658 3659void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked) 3660{ 3661 struct ondisk_untracked_cache *ouc; 3662 struct write_data wd; 3663 unsigned char varbuf[16]; 3664 uint8_t varint_len; 3665 const unsigned hashsz = the_hash_algo->rawsz; 3666 3667 CALLOC_ARRAY(ouc, 1); 3668 stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat); 3669 stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat); 3670 ouc->dir_flags = htonl(untracked->dir_flags); 3671 3672 varint_len = encode_varint(untracked->ident.len, varbuf); 3673 strbuf_add(out, varbuf, varint_len); 3674 strbuf_addbuf(out, &untracked->ident); 3675 3676 strbuf_add(out, ouc, sizeof(*ouc)); 3677 strbuf_add(out, untracked->ss_info_exclude.oid.hash, hashsz); 3678 strbuf_add(out, untracked->ss_excludes_file.oid.hash, hashsz); 3679 strbuf_add(out, untracked->exclude_per_dir, strlen(untracked->exclude_per_dir) + 1); 3680 FREE_AND_NULL(ouc); 3681 3682 if (!untracked->root) { 3683 varint_len = encode_varint(0, varbuf); 3684 strbuf_add(out, varbuf, varint_len); 3685 return; 3686 } 3687 3688 wd.index = 0; 3689 wd.check_only = ewah_new(); 3690 wd.valid = ewah_new(); 3691 wd.sha1_valid = ewah_new(); 3692 strbuf_init(&wd.out, 1024); 3693 strbuf_init(&wd.sb_stat, 1024); 3694 strbuf_init(&wd.sb_sha1, 1024); 3695 write_one_dir(untracked->root, &wd); 3696 3697 varint_len = encode_varint(wd.index, varbuf); 3698 strbuf_add(out, varbuf, varint_len); 3699 strbuf_addbuf(out, &wd.out); 3700 ewah_serialize_strbuf(wd.valid, out); 3701 ewah_serialize_strbuf(wd.check_only, out); 3702 ewah_serialize_strbuf(wd.sha1_valid, out); 3703 strbuf_addbuf(out, &wd.sb_stat); 3704 strbuf_addbuf(out, &wd.sb_sha1); 3705 strbuf_addch(out, '\0'); /* safe guard for string lists */ 3706 3707 ewah_free(wd.valid); 3708 ewah_free(wd.check_only); 3709 ewah_free(wd.sha1_valid); 3710 strbuf_release(&wd.out); 3711 strbuf_release(&wd.sb_stat); 3712 strbuf_release(&wd.sb_sha1); 3713} 3714 3715static void free_untracked(struct untracked_cache_dir *ucd) 3716{ 3717 int i; 3718 if (!ucd) 3719 return; 3720 for (i = 0; i < ucd->dirs_nr; i++) 3721 free_untracked(ucd->dirs[i]); 3722 for (i = 0; i < ucd->untracked_nr; i++) 3723 free(ucd->untracked[i]); 3724 free(ucd->untracked); 3725 free(ucd->dirs); 3726 free(ucd); 3727} 3728 3729void free_untracked_cache(struct untracked_cache *uc) 3730{ 3731 if (!uc) 3732 return; 3733 3734 free(uc->exclude_per_dir_to_free); 3735 strbuf_release(&uc->ident); 3736 free_untracked(uc->root); 3737 free(uc); 3738} 3739 3740struct read_data { 3741 int index; 3742 struct untracked_cache_dir **ucd; 3743 struct ewah_bitmap *check_only; 3744 struct ewah_bitmap *valid; 3745 struct ewah_bitmap *sha1_valid; 3746 const unsigned char *data; 3747 const unsigned char *end; 3748}; 3749 3750static void stat_data_from_disk(struct stat_data *to, const unsigned char *data) 3751{ 3752 memcpy(to, data, sizeof(*to)); 3753 to->sd_ctime.sec = ntohl(to->sd_ctime.sec); 3754 to->sd_ctime.nsec = ntohl(to->sd_ctime.nsec); 3755 to->sd_mtime.sec = ntohl(to->sd_mtime.sec); 3756 to->sd_mtime.nsec = ntohl(to->sd_mtime.nsec); 3757 to->sd_dev = ntohl(to->sd_dev); 3758 to->sd_ino = ntohl(to->sd_ino); 3759 to->sd_uid = ntohl(to->sd_uid); 3760 to->sd_gid = ntohl(to->sd_gid); 3761 to->sd_size = ntohl(to->sd_size); 3762} 3763 3764static int read_one_dir(struct untracked_cache_dir **untracked_, 3765 struct read_data *rd) 3766{ 3767 struct untracked_cache_dir ud, *untracked; 3768 const unsigned char *data = rd->data, *end = rd->end; 3769 const unsigned char *eos; 3770 uint64_t value; 3771 int i; 3772 3773 memset(&ud, 0, sizeof(ud)); 3774 3775 value = decode_varint(&data); 3776 if (data > end) 3777 return -1; 3778 ud.recurse = 1; 3779 ud.untracked_alloc = value; 3780 ud.untracked_nr = value; 3781 if (ud.untracked_nr) 3782 ALLOC_ARRAY(ud.untracked, ud.untracked_nr); 3783 3784 ud.dirs_alloc = ud.dirs_nr = decode_varint(&data); 3785 if (data > end) 3786 return -1; 3787 ALLOC_ARRAY(ud.dirs, ud.dirs_nr); 3788 3789 eos = memchr(data, '\0', end - data); 3790 if (!eos || eos == end) 3791 return -1; 3792 3793 *untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1)); 3794 memcpy(untracked, &ud, sizeof(ud)); 3795 memcpy(untracked->name, data, eos - data + 1); 3796 data = eos + 1; 3797 3798 for (i = 0; i < untracked->untracked_nr; i++) { 3799 eos = memchr(data, '\0', end - data); 3800 if (!eos || eos == end) 3801 return -1; 3802 untracked->untracked[i] = xmemdupz(data, eos - data); 3803 data = eos + 1; 3804 } 3805 3806 rd->ucd[rd->index++] = untracked; 3807 rd->data = data; 3808 3809 for (i = 0; i < untracked->dirs_nr; i++) { 3810 if (read_one_dir(untracked->dirs + i, rd) < 0) 3811 return -1; 3812 } 3813 return 0; 3814} 3815 3816static void set_check_only(size_t pos, void *cb) 3817{ 3818 struct read_data *rd = cb; 3819 struct untracked_cache_dir *ud = rd->ucd[pos]; 3820 ud->check_only = 1; 3821} 3822 3823static void read_stat(size_t pos, void *cb) 3824{ 3825 struct read_data *rd = cb; 3826 struct untracked_cache_dir *ud = rd->ucd[pos]; 3827 if (rd->data + sizeof(struct stat_data) > rd->end) { 3828 rd->data = rd->end + 1; 3829 return; 3830 } 3831 stat_data_from_disk(&ud->stat_data, rd->data); 3832 rd->data += sizeof(struct stat_data); 3833 ud->valid = 1; 3834} 3835 3836static void read_oid(size_t pos, void *cb) 3837{ 3838 struct read_data *rd = cb; 3839 struct untracked_cache_dir *ud = rd->ucd[pos]; 3840 if (rd->data + the_hash_algo->rawsz > rd->end) { 3841 rd->data = rd->end + 1; 3842 return; 3843 } 3844 oidread(&ud->exclude_oid, rd->data, the_repository->hash_algo); 3845 rd->data += the_hash_algo->rawsz; 3846} 3847 3848static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data, 3849 const unsigned char *sha1) 3850{ 3851 stat_data_from_disk(&oid_stat->stat, data); 3852 oidread(&oid_stat->oid, sha1, the_repository->hash_algo); 3853 oid_stat->valid = 1; 3854} 3855 3856struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz) 3857{ 3858 struct untracked_cache *uc; 3859 struct read_data rd; 3860 const unsigned char *next = data, *end = (const unsigned char *)data + sz; 3861 const char *ident; 3862 uint64_t ident_len; 3863 uint64_t varint_len; 3864 ssize_t len; 3865 const char *exclude_per_dir; 3866 const unsigned hashsz = the_hash_algo->rawsz; 3867 const unsigned offset = sizeof(struct ondisk_untracked_cache); 3868 const unsigned exclude_per_dir_offset = offset + 2 * hashsz; 3869 3870 if (sz <= 1 || end[-1] != '\0') 3871 return NULL; 3872 end--; 3873 3874 ident_len = decode_varint(&next); 3875 if (next + ident_len > end) 3876 return NULL; 3877 ident = (const char *)next; 3878 next += ident_len; 3879 3880 if (next + exclude_per_dir_offset + 1 > end) 3881 return NULL; 3882 3883 CALLOC_ARRAY(uc, 1); 3884 strbuf_init(&uc->ident, ident_len); 3885 strbuf_add(&uc->ident, ident, ident_len); 3886 load_oid_stat(&uc->ss_info_exclude, 3887 next + ouc_offset(info_exclude_stat), 3888 next + offset); 3889 load_oid_stat(&uc->ss_excludes_file, 3890 next + ouc_offset(excludes_file_stat), 3891 next + offset + hashsz); 3892 uc->dir_flags = get_be32(next + ouc_offset(dir_flags)); 3893 exclude_per_dir = (const char *)next + exclude_per_dir_offset; 3894 uc->exclude_per_dir = uc->exclude_per_dir_to_free = xstrdup(exclude_per_dir); 3895 /* NUL after exclude_per_dir is covered by sizeof(*ouc) */ 3896 next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1; 3897 if (next >= end) 3898 goto done2; 3899 3900 varint_len = decode_varint(&next); 3901 if (next > end || varint_len == 0) 3902 goto done2; 3903 3904 rd.valid = ewah_new(); 3905 rd.check_only = ewah_new(); 3906 rd.sha1_valid = ewah_new(); 3907 rd.data = next; 3908 rd.end = end; 3909 rd.index = 0; 3910 ALLOC_ARRAY(rd.ucd, varint_len); 3911 3912 if (read_one_dir(&uc->root, &rd) || rd.index != varint_len) 3913 goto done; 3914 3915 next = rd.data; 3916 len = ewah_read_mmap(rd.valid, next, end - next); 3917 if (len < 0) 3918 goto done; 3919 3920 next += len; 3921 len = ewah_read_mmap(rd.check_only, next, end - next); 3922 if (len < 0) 3923 goto done; 3924 3925 next += len; 3926 len = ewah_read_mmap(rd.sha1_valid, next, end - next); 3927 if (len < 0) 3928 goto done; 3929 3930 ewah_each_bit(rd.check_only, set_check_only, &rd); 3931 rd.data = next + len; 3932 ewah_each_bit(rd.valid, read_stat, &rd); 3933 ewah_each_bit(rd.sha1_valid, read_oid, &rd); 3934 next = rd.data; 3935 3936done: 3937 free(rd.ucd); 3938 ewah_free(rd.valid); 3939 ewah_free(rd.check_only); 3940 ewah_free(rd.sha1_valid); 3941done2: 3942 if (next != end) { 3943 free_untracked_cache(uc); 3944 uc = NULL; 3945 } 3946 return uc; 3947} 3948 3949static void invalidate_one_directory(struct untracked_cache *uc, 3950 struct untracked_cache_dir *ucd) 3951{ 3952 uc->dir_invalidated++; 3953 ucd->valid = 0; 3954 for (size_t i = 0; i < ucd->untracked_nr; i++) 3955 free(ucd->untracked[i]); 3956 ucd->untracked_nr = 0; 3957} 3958 3959/* 3960 * Normally when an entry is added or removed from a directory, 3961 * invalidating that directory is enough. No need to touch its 3962 * ancestors. When a directory is shown as "foo/bar/" in git-status 3963 * however, deleting or adding an entry may have cascading effect. 3964 * 3965 * Say the "foo/bar/file" has become untracked, we need to tell the 3966 * untracked_cache_dir of "foo" that "bar/" is not an untracked 3967 * directory any more (because "bar" is managed by foo as an untracked 3968 * "file"). 3969 * 3970 * Similarly, if "foo/bar/file" moves from untracked to tracked and it 3971 * was the last untracked entry in the entire "foo", we should show 3972 * "foo/" instead. Which means we have to invalidate past "bar" up to 3973 * "foo". 3974 * 3975 * This function traverses all directories from root to leaf. If there 3976 * is a chance of one of the above cases happening, we invalidate back 3977 * to root. Otherwise we just invalidate the leaf. There may be a more 3978 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to 3979 * detect these cases and avoid unnecessary invalidation, for example, 3980 * checking for the untracked entry named "bar/" in "foo", but for now 3981 * stick to something safe and simple. 3982 */ 3983static int invalidate_one_component(struct untracked_cache *uc, 3984 struct untracked_cache_dir *dir, 3985 const char *path, int len) 3986{ 3987 const char *rest = strchr(path, '/'); 3988 3989 if (rest) { 3990 int component_len = rest - path; 3991 struct untracked_cache_dir *d = 3992 lookup_untracked(uc, dir, path, component_len); 3993 int ret = 3994 invalidate_one_component(uc, d, rest + 1, 3995 len - (component_len + 1)); 3996 if (ret) 3997 invalidate_one_directory(uc, dir); 3998 return ret; 3999 } 4000 4001 invalidate_one_directory(uc, dir); 4002 return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES; 4003} 4004 4005void untracked_cache_invalidate_path(struct index_state *istate, 4006 const char *path, int safe_path) 4007{ 4008 if (!istate->untracked || !istate->untracked->root) 4009 return; 4010 if (!safe_path && !verify_path(path, 0)) 4011 return; 4012 invalidate_one_component(istate->untracked, istate->untracked->root, 4013 path, strlen(path)); 4014} 4015 4016void untracked_cache_invalidate_trimmed_path(struct index_state *istate, 4017 const char *path, 4018 int safe_path) 4019{ 4020 size_t len = strlen(path); 4021 4022 if (!len) 4023 BUG("untracked_cache_invalidate_trimmed_path given zero length path"); 4024 4025 if (path[len - 1] != '/') { 4026 untracked_cache_invalidate_path(istate, path, safe_path); 4027 } else { 4028 struct strbuf tmp = STRBUF_INIT; 4029 4030 strbuf_add(&tmp, path, len - 1); 4031 untracked_cache_invalidate_path(istate, tmp.buf, safe_path); 4032 strbuf_release(&tmp); 4033 } 4034} 4035 4036void untracked_cache_remove_from_index(struct index_state *istate, 4037 const char *path) 4038{ 4039 untracked_cache_invalidate_path(istate, path, 1); 4040} 4041 4042void untracked_cache_add_to_index(struct index_state *istate, 4043 const char *path) 4044{ 4045 untracked_cache_invalidate_path(istate, path, 1); 4046} 4047 4048static void connect_wt_gitdir_in_nested(const char *sub_worktree, 4049 const char *sub_gitdir) 4050{ 4051 int i; 4052 struct repository subrepo; 4053 struct strbuf sub_wt = STRBUF_INIT; 4054 struct strbuf sub_gd = STRBUF_INIT; 4055 4056 const struct submodule *sub; 4057 4058 /* If the submodule has no working tree, we can ignore it. */ 4059 if (repo_init(&subrepo, sub_gitdir, sub_worktree)) 4060 return; 4061 4062 if (repo_read_index(&subrepo) < 0) 4063 die(_("index file corrupt in repo %s"), subrepo.gitdir); 4064 4065 /* TODO: audit for interaction with sparse-index. */ 4066 ensure_full_index(subrepo.index); 4067 for (i = 0; i < subrepo.index->cache_nr; i++) { 4068 const struct cache_entry *ce = subrepo.index->cache[i]; 4069 4070 if (!S_ISGITLINK(ce->ce_mode)) 4071 continue; 4072 4073 while (i + 1 < subrepo.index->cache_nr && 4074 !strcmp(ce->name, subrepo.index->cache[i + 1]->name)) 4075 /* 4076 * Skip entries with the same name in different stages 4077 * to make sure an entry is returned only once. 4078 */ 4079 i++; 4080 4081 sub = submodule_from_path(&subrepo, null_oid(the_hash_algo), ce->name); 4082 if (!sub || !is_submodule_active(&subrepo, ce->name)) 4083 /* .gitmodules broken or inactive sub */ 4084 continue; 4085 4086 strbuf_reset(&sub_wt); 4087 strbuf_reset(&sub_gd); 4088 strbuf_addf(&sub_wt, "%s/%s", sub_worktree, sub->path); 4089 submodule_name_to_gitdir(&sub_gd, &subrepo, sub->name); 4090 4091 connect_work_tree_and_git_dir(sub_wt.buf, sub_gd.buf, 1); 4092 } 4093 strbuf_release(&sub_wt); 4094 strbuf_release(&sub_gd); 4095 repo_clear(&subrepo); 4096} 4097 4098void connect_work_tree_and_git_dir(const char *work_tree_, 4099 const char *git_dir_, 4100 int recurse_into_nested) 4101{ 4102 struct strbuf gitfile_sb = STRBUF_INIT; 4103 struct strbuf cfg_sb = STRBUF_INIT; 4104 struct strbuf rel_path = STRBUF_INIT; 4105 char *git_dir, *work_tree; 4106 4107 /* Prepare .git file */ 4108 strbuf_addf(&gitfile_sb, "%s/.git", work_tree_); 4109 if (safe_create_leading_directories_const(the_repository, gitfile_sb.buf)) 4110 die(_("could not create directories for %s"), gitfile_sb.buf); 4111 4112 /* Prepare config file */ 4113 strbuf_addf(&cfg_sb, "%s/config", git_dir_); 4114 if (safe_create_leading_directories_const(the_repository, cfg_sb.buf)) 4115 die(_("could not create directories for %s"), cfg_sb.buf); 4116 4117 git_dir = real_pathdup(git_dir_, 1); 4118 work_tree = real_pathdup(work_tree_, 1); 4119 4120 /* Write .git file */ 4121 write_file(gitfile_sb.buf, "gitdir: %s", 4122 relative_path(git_dir, work_tree, &rel_path)); 4123 /* Update core.worktree setting */ 4124 repo_config_set_in_file(the_repository, cfg_sb.buf, "core.worktree", 4125 relative_path(work_tree, git_dir, &rel_path)); 4126 4127 strbuf_release(&gitfile_sb); 4128 strbuf_release(&cfg_sb); 4129 strbuf_release(&rel_path); 4130 4131 if (recurse_into_nested) 4132 connect_wt_gitdir_in_nested(work_tree, git_dir); 4133 4134 free(work_tree); 4135 free(git_dir); 4136} 4137 4138/* 4139 * Migrate the git directory of the given path from old_git_dir to new_git_dir. 4140 */ 4141void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir) 4142{ 4143 if (rename(old_git_dir, new_git_dir) < 0) 4144 die_errno(_("could not migrate git directory from '%s' to '%s'"), 4145 old_git_dir, new_git_dir); 4146 4147 connect_work_tree_and_git_dir(path, new_git_dir, 0); 4148} 4149 4150int path_match_flags(const char *const str, const enum path_match_flags flags) 4151{ 4152 const char *p = str; 4153 4154 if (flags & PATH_MATCH_NATIVE && 4155 flags & PATH_MATCH_XPLATFORM) 4156 BUG("path_match_flags() must get one match kind, not multiple!"); 4157 else if (!(flags & PATH_MATCH_KINDS_MASK)) 4158 BUG("path_match_flags() must get at least one match kind!"); 4159 4160 if (flags & PATH_MATCH_STARTS_WITH_DOT_SLASH && 4161 flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH) 4162 BUG("path_match_flags() must get one platform kind, not multiple!"); 4163 else if (!(flags & PATH_MATCH_PLATFORM_MASK)) 4164 BUG("path_match_flags() must get at least one platform kind!"); 4165 4166 if (*p++ != '.') 4167 return 0; 4168 if (flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH && 4169 *p++ != '.') 4170 return 0; 4171 4172 if (flags & PATH_MATCH_NATIVE) 4173 return is_dir_sep(*p); 4174 else if (flags & PATH_MATCH_XPLATFORM) 4175 return is_xplatform_dir_sep(*p); 4176 BUG("unreachable"); 4177}