dir.con commit repository: enable initialization of submodules (96dc883)
   1/*
   2 * This handles recursive filename detection with exclude
   3 * files, index knowledge etc..
   4 *
   5 * See Documentation/technical/api-directory-listing.txt
   6 *
   7 * Copyright (C) Linus Torvalds, 2005-2006
   8 *               Junio Hamano, 2005-2006
   9 */
  10#define NO_THE_INDEX_COMPATIBILITY_MACROS
  11#include "cache.h"
  12#include "config.h"
  13#include "dir.h"
  14#include "attr.h"
  15#include "refs.h"
  16#include "wildmatch.h"
  17#include "pathspec.h"
  18#include "utf8.h"
  19#include "varint.h"
  20#include "ewah/ewok.h"
  21
  22/*
  23 * Tells read_directory_recursive how a file or directory should be treated.
  24 * Values are ordered by significance, e.g. if a directory contains both
  25 * excluded and untracked files, it is listed as untracked because
  26 * path_untracked > path_excluded.
  27 */
  28enum path_treatment {
  29        path_none = 0,
  30        path_recurse,
  31        path_excluded,
  32        path_untracked
  33};
  34
  35/*
  36 * Support data structure for our opendir/readdir/closedir wrappers
  37 */
  38struct cached_dir {
  39        DIR *fdir;
  40        struct untracked_cache_dir *untracked;
  41        int nr_files;
  42        int nr_dirs;
  43
  44        struct dirent *de;
  45        const char *file;
  46        struct untracked_cache_dir *ucd;
  47};
  48
  49static enum path_treatment read_directory_recursive(struct dir_struct *dir,
  50        struct index_state *istate, const char *path, int len,
  51        struct untracked_cache_dir *untracked,
  52        int check_only, const struct pathspec *pathspec);
  53static int get_dtype(struct dirent *de, struct index_state *istate,
  54                     const char *path, int len);
  55
  56int fspathcmp(const char *a, const char *b)
  57{
  58        return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
  59}
  60
  61int fspathncmp(const char *a, const char *b, size_t count)
  62{
  63        return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
  64}
  65
  66int git_fnmatch(const struct pathspec_item *item,
  67                const char *pattern, const char *string,
  68                int prefix)
  69{
  70        if (prefix > 0) {
  71                if (ps_strncmp(item, pattern, string, prefix))
  72                        return WM_NOMATCH;
  73                pattern += prefix;
  74                string += prefix;
  75        }
  76        if (item->flags & PATHSPEC_ONESTAR) {
  77                int pattern_len = strlen(++pattern);
  78                int string_len = strlen(string);
  79                return string_len < pattern_len ||
  80                        ps_strcmp(item, pattern,
  81                                  string + string_len - pattern_len);
  82        }
  83        if (item->magic & PATHSPEC_GLOB)
  84                return wildmatch(pattern, string,
  85                                 WM_PATHNAME |
  86                                 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
  87                                 NULL);
  88        else
  89                /* wildmatch has not learned no FNM_PATHNAME mode yet */
  90                return wildmatch(pattern, string,
  91                                 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0,
  92                                 NULL);
  93}
  94
  95static int fnmatch_icase_mem(const char *pattern, int patternlen,
  96                             const char *string, int stringlen,
  97                             int flags)
  98{
  99        int match_status;
 100        struct strbuf pat_buf = STRBUF_INIT;
 101        struct strbuf str_buf = STRBUF_INIT;
 102        const char *use_pat = pattern;
 103        const char *use_str = string;
 104
 105        if (pattern[patternlen]) {
 106                strbuf_add(&pat_buf, pattern, patternlen);
 107                use_pat = pat_buf.buf;
 108        }
 109        if (string[stringlen]) {
 110                strbuf_add(&str_buf, string, stringlen);
 111                use_str = str_buf.buf;
 112        }
 113
 114        if (ignore_case)
 115                flags |= WM_CASEFOLD;
 116        match_status = wildmatch(use_pat, use_str, flags, NULL);
 117
 118        strbuf_release(&pat_buf);
 119        strbuf_release(&str_buf);
 120
 121        return match_status;
 122}
 123
 124static size_t common_prefix_len(const struct pathspec *pathspec)
 125{
 126        int n;
 127        size_t max = 0;
 128
 129        /*
 130         * ":(icase)path" is treated as a pathspec full of
 131         * wildcard. In other words, only prefix is considered common
 132         * prefix. If the pathspec is abc/foo abc/bar, running in
 133         * subdir xyz, the common prefix is still xyz, not xuz/abc as
 134         * in non-:(icase).
 135         */
 136        GUARD_PATHSPEC(pathspec,
 137                       PATHSPEC_FROMTOP |
 138                       PATHSPEC_MAXDEPTH |
 139                       PATHSPEC_LITERAL |
 140                       PATHSPEC_GLOB |
 141                       PATHSPEC_ICASE |
 142                       PATHSPEC_EXCLUDE |
 143                       PATHSPEC_ATTR);
 144
 145        for (n = 0; n < pathspec->nr; n++) {
 146                size_t i = 0, len = 0, item_len;
 147                if (pathspec->items[n].magic & PATHSPEC_EXCLUDE)
 148                        continue;
 149                if (pathspec->items[n].magic & PATHSPEC_ICASE)
 150                        item_len = pathspec->items[n].prefix;
 151                else
 152                        item_len = pathspec->items[n].nowildcard_len;
 153                while (i < item_len && (n == 0 || i < max)) {
 154                        char c = pathspec->items[n].match[i];
 155                        if (c != pathspec->items[0].match[i])
 156                                break;
 157                        if (c == '/')
 158                                len = i + 1;
 159                        i++;
 160                }
 161                if (n == 0 || len < max) {
 162                        max = len;
 163                        if (!max)
 164                                break;
 165                }
 166        }
 167        return max;
 168}
 169
 170/*
 171 * Returns a copy of the longest leading path common among all
 172 * pathspecs.
 173 */
 174char *common_prefix(const struct pathspec *pathspec)
 175{
 176        unsigned long len = common_prefix_len(pathspec);
 177
 178        return len ? xmemdupz(pathspec->items[0].match, len) : NULL;
 179}
 180
 181int fill_directory(struct dir_struct *dir,
 182                   struct index_state *istate,
 183                   const struct pathspec *pathspec)
 184{
 185        const char *prefix;
 186        size_t prefix_len;
 187
 188        /*
 189         * Calculate common prefix for the pathspec, and
 190         * use that to optimize the directory walk
 191         */
 192        prefix_len = common_prefix_len(pathspec);
 193        prefix = prefix_len ? pathspec->items[0].match : "";
 194
 195        /* Read the directory and prune it */
 196        read_directory(dir, istate, prefix, prefix_len, pathspec);
 197
 198        return prefix_len;
 199}
 200
 201int within_depth(const char *name, int namelen,
 202                        int depth, int max_depth)
 203{
 204        const char *cp = name, *cpe = name + namelen;
 205
 206        while (cp < cpe) {
 207                if (*cp++ != '/')
 208                        continue;
 209                depth++;
 210                if (depth > max_depth)
 211                        return 0;
 212        }
 213        return 1;
 214}
 215
 216#define DO_MATCH_EXCLUDE   (1<<0)
 217#define DO_MATCH_DIRECTORY (1<<1)
 218#define DO_MATCH_SUBMODULE (1<<2)
 219
 220static int match_attrs(const char *name, int namelen,
 221                       const struct pathspec_item *item)
 222{
 223        int i;
 224
 225        git_check_attr(name, item->attr_check);
 226        for (i = 0; i < item->attr_match_nr; i++) {
 227                const char *value;
 228                int matched;
 229                enum attr_match_mode match_mode;
 230
 231                value = item->attr_check->items[i].value;
 232                match_mode = item->attr_match[i].match_mode;
 233
 234                if (ATTR_TRUE(value))
 235                        matched = (match_mode == MATCH_SET);
 236                else if (ATTR_FALSE(value))
 237                        matched = (match_mode == MATCH_UNSET);
 238                else if (ATTR_UNSET(value))
 239                        matched = (match_mode == MATCH_UNSPECIFIED);
 240                else
 241                        matched = (match_mode == MATCH_VALUE &&
 242                                   !strcmp(item->attr_match[i].value, value));
 243                if (!matched)
 244                        return 0;
 245        }
 246
 247        return 1;
 248}
 249
 250/*
 251 * Does 'match' match the given name?
 252 * A match is found if
 253 *
 254 * (1) the 'match' string is leading directory of 'name', or
 255 * (2) the 'match' string is a wildcard and matches 'name', or
 256 * (3) the 'match' string is exactly the same as 'name'.
 257 *
 258 * and the return value tells which case it was.
 259 *
 260 * It returns 0 when there is no match.
 261 */
 262static int match_pathspec_item(const struct pathspec_item *item, int prefix,
 263                               const char *name, int namelen, unsigned flags)
 264{
 265        /* name/namelen has prefix cut off by caller */
 266        const char *match = item->match + prefix;
 267        int matchlen = item->len - prefix;
 268
 269        /*
 270         * The normal call pattern is:
 271         * 1. prefix = common_prefix_len(ps);
 272         * 2. prune something, or fill_directory
 273         * 3. match_pathspec()
 274         *
 275         * 'prefix' at #1 may be shorter than the command's prefix and
 276         * it's ok for #2 to match extra files. Those extras will be
 277         * trimmed at #3.
 278         *
 279         * Suppose the pathspec is 'foo' and '../bar' running from
 280         * subdir 'xyz'. The common prefix at #1 will be empty, thanks
 281         * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
 282         * user does not want XYZ/foo, only the "foo" part should be
 283         * case-insensitive. We need to filter out XYZ/foo here. In
 284         * other words, we do not trust the caller on comparing the
 285         * prefix part when :(icase) is involved. We do exact
 286         * comparison ourselves.
 287         *
 288         * Normally the caller (common_prefix_len() in fact) does
 289         * _exact_ matching on name[-prefix+1..-1] and we do not need
 290         * to check that part. Be defensive and check it anyway, in
 291         * case common_prefix_len is changed, or a new caller is
 292         * introduced that does not use common_prefix_len.
 293         *
 294         * If the penalty turns out too high when prefix is really
 295         * long, maybe change it to
 296         * strncmp(match, name, item->prefix - prefix)
 297         */
 298        if (item->prefix && (item->magic & PATHSPEC_ICASE) &&
 299            strncmp(item->match, name - prefix, item->prefix))
 300                return 0;
 301
 302        if (item->attr_match_nr && !match_attrs(name, namelen, item))
 303                return 0;
 304
 305        /* If the match was just the prefix, we matched */
 306        if (!*match)
 307                return MATCHED_RECURSIVELY;
 308
 309        if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) {
 310                if (matchlen == namelen)
 311                        return MATCHED_EXACTLY;
 312
 313                if (match[matchlen-1] == '/' || name[matchlen] == '/')
 314                        return MATCHED_RECURSIVELY;
 315        } else if ((flags & DO_MATCH_DIRECTORY) &&
 316                   match[matchlen - 1] == '/' &&
 317                   namelen == matchlen - 1 &&
 318                   !ps_strncmp(item, match, name, namelen))
 319                return MATCHED_EXACTLY;
 320
 321        if (item->nowildcard_len < item->len &&
 322            !git_fnmatch(item, match, name,
 323                         item->nowildcard_len - prefix))
 324                return MATCHED_FNMATCH;
 325
 326        /* Perform checks to see if "name" is a super set of the pathspec */
 327        if (flags & DO_MATCH_SUBMODULE) {
 328                /* name is a literal prefix of the pathspec */
 329                if ((namelen < matchlen) &&
 330                    (match[namelen] == '/') &&
 331                    !ps_strncmp(item, match, name, namelen))
 332                        return MATCHED_RECURSIVELY;
 333
 334                /* name" doesn't match up to the first wild character */
 335                if (item->nowildcard_len < item->len &&
 336                    ps_strncmp(item, match, name,
 337                               item->nowildcard_len - prefix))
 338                        return 0;
 339
 340                /*
 341                 * Here is where we would perform a wildmatch to check if
 342                 * "name" can be matched as a directory (or a prefix) against
 343                 * the pathspec.  Since wildmatch doesn't have this capability
 344                 * at the present we have to punt and say that it is a match,
 345                 * potentially returning a false positive
 346                 * The submodules themselves will be able to perform more
 347                 * accurate matching to determine if the pathspec matches.
 348                 */
 349                return MATCHED_RECURSIVELY;
 350        }
 351
 352        return 0;
 353}
 354
 355/*
 356 * Given a name and a list of pathspecs, returns the nature of the
 357 * closest (i.e. most specific) match of the name to any of the
 358 * pathspecs.
 359 *
 360 * The caller typically calls this multiple times with the same
 361 * pathspec and seen[] array but with different name/namelen
 362 * (e.g. entries from the index) and is interested in seeing if and
 363 * how each pathspec matches all the names it calls this function
 364 * with.  A mark is left in the seen[] array for each pathspec element
 365 * indicating the closest type of match that element achieved, so if
 366 * seen[n] remains zero after multiple invocations, that means the nth
 367 * pathspec did not match any names, which could indicate that the
 368 * user mistyped the nth pathspec.
 369 */
 370static int do_match_pathspec(const struct pathspec *ps,
 371                             const char *name, int namelen,
 372                             int prefix, char *seen,
 373                             unsigned flags)
 374{
 375        int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE;
 376
 377        GUARD_PATHSPEC(ps,
 378                       PATHSPEC_FROMTOP |
 379                       PATHSPEC_MAXDEPTH |
 380                       PATHSPEC_LITERAL |
 381                       PATHSPEC_GLOB |
 382                       PATHSPEC_ICASE |
 383                       PATHSPEC_EXCLUDE |
 384                       PATHSPEC_ATTR);
 385
 386        if (!ps->nr) {
 387                if (!ps->recursive ||
 388                    !(ps->magic & PATHSPEC_MAXDEPTH) ||
 389                    ps->max_depth == -1)
 390                        return MATCHED_RECURSIVELY;
 391
 392                if (within_depth(name, namelen, 0, ps->max_depth))
 393                        return MATCHED_EXACTLY;
 394                else
 395                        return 0;
 396        }
 397
 398        name += prefix;
 399        namelen -= prefix;
 400
 401        for (i = ps->nr - 1; i >= 0; i--) {
 402                int how;
 403
 404                if ((!exclude &&   ps->items[i].magic & PATHSPEC_EXCLUDE) ||
 405                    ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE)))
 406                        continue;
 407
 408                if (seen && seen[i] == MATCHED_EXACTLY)
 409                        continue;
 410                /*
 411                 * Make exclude patterns optional and never report
 412                 * "pathspec ':(exclude)foo' matches no files"
 413                 */
 414                if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE)
 415                        seen[i] = MATCHED_FNMATCH;
 416                how = match_pathspec_item(ps->items+i, prefix, name,
 417                                          namelen, flags);
 418                if (ps->recursive &&
 419                    (ps->magic & PATHSPEC_MAXDEPTH) &&
 420                    ps->max_depth != -1 &&
 421                    how && how != MATCHED_FNMATCH) {
 422                        int len = ps->items[i].len;
 423                        if (name[len] == '/')
 424                                len++;
 425                        if (within_depth(name+len, namelen-len, 0, ps->max_depth))
 426                                how = MATCHED_EXACTLY;
 427                        else
 428                                how = 0;
 429                }
 430                if (how) {
 431                        if (retval < how)
 432                                retval = how;
 433                        if (seen && seen[i] < how)
 434                                seen[i] = how;
 435                }
 436        }
 437        return retval;
 438}
 439
 440int match_pathspec(const struct pathspec *ps,
 441                   const char *name, int namelen,
 442                   int prefix, char *seen, int is_dir)
 443{
 444        int positive, negative;
 445        unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
 446        positive = do_match_pathspec(ps, name, namelen,
 447                                     prefix, seen, flags);
 448        if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
 449                return positive;
 450        negative = do_match_pathspec(ps, name, namelen,
 451                                     prefix, seen,
 452                                     flags | DO_MATCH_EXCLUDE);
 453        return negative ? 0 : positive;
 454}
 455
 456/**
 457 * Check if a submodule is a superset of the pathspec
 458 */
 459int submodule_path_match(const struct pathspec *ps,
 460                         const char *submodule_name,
 461                         char *seen)
 462{
 463        int matched = do_match_pathspec(ps, submodule_name,
 464                                        strlen(submodule_name),
 465                                        0, seen,
 466                                        DO_MATCH_DIRECTORY |
 467                                        DO_MATCH_SUBMODULE);
 468        return matched;
 469}
 470
 471int report_path_error(const char *ps_matched,
 472                      const struct pathspec *pathspec,
 473                      const char *prefix)
 474{
 475        /*
 476         * Make sure all pathspec matched; otherwise it is an error.
 477         */
 478        int num, errors = 0;
 479        for (num = 0; num < pathspec->nr; num++) {
 480                int other, found_dup;
 481
 482                if (ps_matched[num])
 483                        continue;
 484                /*
 485                 * The caller might have fed identical pathspec
 486                 * twice.  Do not barf on such a mistake.
 487                 * FIXME: parse_pathspec should have eliminated
 488                 * duplicate pathspec.
 489                 */
 490                for (found_dup = other = 0;
 491                     !found_dup && other < pathspec->nr;
 492                     other++) {
 493                        if (other == num || !ps_matched[other])
 494                                continue;
 495                        if (!strcmp(pathspec->items[other].original,
 496                                    pathspec->items[num].original))
 497                                /*
 498                                 * Ok, we have a match already.
 499                                 */
 500                                found_dup = 1;
 501                }
 502                if (found_dup)
 503                        continue;
 504
 505                error("pathspec '%s' did not match any file(s) known to git.",
 506                      pathspec->items[num].original);
 507                errors++;
 508        }
 509        return errors;
 510}
 511
 512/*
 513 * Return the length of the "simple" part of a path match limiter.
 514 */
 515int simple_length(const char *match)
 516{
 517        int len = -1;
 518
 519        for (;;) {
 520                unsigned char c = *match++;
 521                len++;
 522                if (c == '\0' || is_glob_special(c))
 523                        return len;
 524        }
 525}
 526
 527int no_wildcard(const char *string)
 528{
 529        return string[simple_length(string)] == '\0';
 530}
 531
 532void parse_exclude_pattern(const char **pattern,
 533                           int *patternlen,
 534                           unsigned *flags,
 535                           int *nowildcardlen)
 536{
 537        const char *p = *pattern;
 538        size_t i, len;
 539
 540        *flags = 0;
 541        if (*p == '!') {
 542                *flags |= EXC_FLAG_NEGATIVE;
 543                p++;
 544        }
 545        len = strlen(p);
 546        if (len && p[len - 1] == '/') {
 547                len--;
 548                *flags |= EXC_FLAG_MUSTBEDIR;
 549        }
 550        for (i = 0; i < len; i++) {
 551                if (p[i] == '/')
 552                        break;
 553        }
 554        if (i == len)
 555                *flags |= EXC_FLAG_NODIR;
 556        *nowildcardlen = simple_length(p);
 557        /*
 558         * we should have excluded the trailing slash from 'p' too,
 559         * but that's one more allocation. Instead just make sure
 560         * nowildcardlen does not exceed real patternlen
 561         */
 562        if (*nowildcardlen > len)
 563                *nowildcardlen = len;
 564        if (*p == '*' && no_wildcard(p + 1))
 565                *flags |= EXC_FLAG_ENDSWITH;
 566        *pattern = p;
 567        *patternlen = len;
 568}
 569
 570void add_exclude(const char *string, const char *base,
 571                 int baselen, struct exclude_list *el, int srcpos)
 572{
 573        struct exclude *x;
 574        int patternlen;
 575        unsigned flags;
 576        int nowildcardlen;
 577
 578        parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
 579        if (flags & EXC_FLAG_MUSTBEDIR) {
 580                FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
 581        } else {
 582                x = xmalloc(sizeof(*x));
 583                x->pattern = string;
 584        }
 585        x->patternlen = patternlen;
 586        x->nowildcardlen = nowildcardlen;
 587        x->base = base;
 588        x->baselen = baselen;
 589        x->flags = flags;
 590        x->srcpos = srcpos;
 591        ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
 592        el->excludes[el->nr++] = x;
 593        x->el = el;
 594}
 595
 596static void *read_skip_worktree_file_from_index(const struct index_state *istate,
 597                                                const char *path, size_t *size,
 598                                                struct sha1_stat *sha1_stat)
 599{
 600        int pos, len;
 601        unsigned long sz;
 602        enum object_type type;
 603        void *data;
 604
 605        len = strlen(path);
 606        pos = index_name_pos(istate, path, len);
 607        if (pos < 0)
 608                return NULL;
 609        if (!ce_skip_worktree(istate->cache[pos]))
 610                return NULL;
 611        data = read_sha1_file(istate->cache[pos]->oid.hash, &type, &sz);
 612        if (!data || type != OBJ_BLOB) {
 613                free(data);
 614                return NULL;
 615        }
 616        *size = xsize_t(sz);
 617        if (sha1_stat) {
 618                memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
 619                hashcpy(sha1_stat->sha1, istate->cache[pos]->oid.hash);
 620        }
 621        return data;
 622}
 623
 624/*
 625 * Frees memory within el which was allocated for exclude patterns and
 626 * the file buffer.  Does not free el itself.
 627 */
 628void clear_exclude_list(struct exclude_list *el)
 629{
 630        int i;
 631
 632        for (i = 0; i < el->nr; i++)
 633                free(el->excludes[i]);
 634        free(el->excludes);
 635        free(el->filebuf);
 636
 637        memset(el, 0, sizeof(*el));
 638}
 639
 640static void trim_trailing_spaces(char *buf)
 641{
 642        char *p, *last_space = NULL;
 643
 644        for (p = buf; *p; p++)
 645                switch (*p) {
 646                case ' ':
 647                        if (!last_space)
 648                                last_space = p;
 649                        break;
 650                case '\\':
 651                        p++;
 652                        if (!*p)
 653                                return;
 654                        /* fallthrough */
 655                default:
 656                        last_space = NULL;
 657                }
 658
 659        if (last_space)
 660                *last_space = '\0';
 661}
 662
 663/*
 664 * Given a subdirectory name and "dir" of the current directory,
 665 * search the subdir in "dir" and return it, or create a new one if it
 666 * does not exist in "dir".
 667 *
 668 * If "name" has the trailing slash, it'll be excluded in the search.
 669 */
 670static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
 671                                                    struct untracked_cache_dir *dir,
 672                                                    const char *name, int len)
 673{
 674        int first, last;
 675        struct untracked_cache_dir *d;
 676        if (!dir)
 677                return NULL;
 678        if (len && name[len - 1] == '/')
 679                len--;
 680        first = 0;
 681        last = dir->dirs_nr;
 682        while (last > first) {
 683                int cmp, next = (last + first) >> 1;
 684                d = dir->dirs[next];
 685                cmp = strncmp(name, d->name, len);
 686                if (!cmp && strlen(d->name) > len)
 687                        cmp = -1;
 688                if (!cmp)
 689                        return d;
 690                if (cmp < 0) {
 691                        last = next;
 692                        continue;
 693                }
 694                first = next+1;
 695        }
 696
 697        uc->dir_created++;
 698        FLEX_ALLOC_MEM(d, name, name, len);
 699
 700        ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
 701        memmove(dir->dirs + first + 1, dir->dirs + first,
 702                (dir->dirs_nr - first) * sizeof(*dir->dirs));
 703        dir->dirs_nr++;
 704        dir->dirs[first] = d;
 705        return d;
 706}
 707
 708static void do_invalidate_gitignore(struct untracked_cache_dir *dir)
 709{
 710        int i;
 711        dir->valid = 0;
 712        dir->untracked_nr = 0;
 713        for (i = 0; i < dir->dirs_nr; i++)
 714                do_invalidate_gitignore(dir->dirs[i]);
 715}
 716
 717static void invalidate_gitignore(struct untracked_cache *uc,
 718                                 struct untracked_cache_dir *dir)
 719{
 720        uc->gitignore_invalidated++;
 721        do_invalidate_gitignore(dir);
 722}
 723
 724static void invalidate_directory(struct untracked_cache *uc,
 725                                 struct untracked_cache_dir *dir)
 726{
 727        int i;
 728        uc->dir_invalidated++;
 729        dir->valid = 0;
 730        dir->untracked_nr = 0;
 731        for (i = 0; i < dir->dirs_nr; i++)
 732                dir->dirs[i]->recurse = 0;
 733}
 734
 735/*
 736 * Given a file with name "fname", read it (either from disk, or from
 737 * an index if 'istate' is non-null), parse it and store the
 738 * exclude rules in "el".
 739 *
 740 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
 741 * stat data from disk (only valid if add_excludes returns zero). If
 742 * ss_valid is non-zero, "ss" must contain good value as input.
 743 */
 744static int add_excludes(const char *fname, const char *base, int baselen,
 745                        struct exclude_list *el,
 746                        struct index_state *istate,
 747                        struct sha1_stat *sha1_stat)
 748{
 749        struct stat st;
 750        int fd, i, lineno = 1;
 751        size_t size = 0;
 752        char *buf, *entry;
 753
 754        fd = open(fname, O_RDONLY);
 755        if (fd < 0 || fstat(fd, &st) < 0) {
 756                if (fd < 0)
 757                        warn_on_fopen_errors(fname);
 758                else
 759                        close(fd);
 760                if (!istate ||
 761                    (buf = read_skip_worktree_file_from_index(istate, fname, &size, sha1_stat)) == NULL)
 762                        return -1;
 763                if (size == 0) {
 764                        free(buf);
 765                        return 0;
 766                }
 767                if (buf[size-1] != '\n') {
 768                        buf = xrealloc(buf, st_add(size, 1));
 769                        buf[size++] = '\n';
 770                }
 771        } else {
 772                size = xsize_t(st.st_size);
 773                if (size == 0) {
 774                        if (sha1_stat) {
 775                                fill_stat_data(&sha1_stat->stat, &st);
 776                                hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
 777                                sha1_stat->valid = 1;
 778                        }
 779                        close(fd);
 780                        return 0;
 781                }
 782                buf = xmallocz(size);
 783                if (read_in_full(fd, buf, size) != size) {
 784                        free(buf);
 785                        close(fd);
 786                        return -1;
 787                }
 788                buf[size++] = '\n';
 789                close(fd);
 790                if (sha1_stat) {
 791                        int pos;
 792                        if (sha1_stat->valid &&
 793                            !match_stat_data_racy(istate, &sha1_stat->stat, &st))
 794                                ; /* no content change, ss->sha1 still good */
 795                        else if (istate &&
 796                                 (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
 797                                 !ce_stage(istate->cache[pos]) &&
 798                                 ce_uptodate(istate->cache[pos]) &&
 799                                 !would_convert_to_git(istate, fname))
 800                                hashcpy(sha1_stat->sha1,
 801                                        istate->cache[pos]->oid.hash);
 802                        else
 803                                hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
 804                        fill_stat_data(&sha1_stat->stat, &st);
 805                        sha1_stat->valid = 1;
 806                }
 807        }
 808
 809        el->filebuf = buf;
 810
 811        if (skip_utf8_bom(&buf, size))
 812                size -= buf - el->filebuf;
 813
 814        entry = buf;
 815
 816        for (i = 0; i < size; i++) {
 817                if (buf[i] == '\n') {
 818                        if (entry != buf + i && entry[0] != '#') {
 819                                buf[i - (i && buf[i-1] == '\r')] = 0;
 820                                trim_trailing_spaces(entry);
 821                                add_exclude(entry, base, baselen, el, lineno);
 822                        }
 823                        lineno++;
 824                        entry = buf + i + 1;
 825                }
 826        }
 827        return 0;
 828}
 829
 830int add_excludes_from_file_to_list(const char *fname, const char *base,
 831                                   int baselen, struct exclude_list *el,
 832                                   struct index_state *istate)
 833{
 834        return add_excludes(fname, base, baselen, el, istate, NULL);
 835}
 836
 837struct exclude_list *add_exclude_list(struct dir_struct *dir,
 838                                      int group_type, const char *src)
 839{
 840        struct exclude_list *el;
 841        struct exclude_list_group *group;
 842
 843        group = &dir->exclude_list_group[group_type];
 844        ALLOC_GROW(group->el, group->nr + 1, group->alloc);
 845        el = &group->el[group->nr++];
 846        memset(el, 0, sizeof(*el));
 847        el->src = src;
 848        return el;
 849}
 850
 851/*
 852 * Used to set up core.excludesfile and .git/info/exclude lists.
 853 */
 854static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
 855                                     struct sha1_stat *sha1_stat)
 856{
 857        struct exclude_list *el;
 858        /*
 859         * catch setup_standard_excludes() that's called before
 860         * dir->untracked is assigned. That function behaves
 861         * differently when dir->untracked is non-NULL.
 862         */
 863        if (!dir->untracked)
 864                dir->unmanaged_exclude_files++;
 865        el = add_exclude_list(dir, EXC_FILE, fname);
 866        if (add_excludes(fname, "", 0, el, NULL, sha1_stat) < 0)
 867                die("cannot use %s as an exclude file", fname);
 868}
 869
 870void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 871{
 872        dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
 873        add_excludes_from_file_1(dir, fname, NULL);
 874}
 875
 876int match_basename(const char *basename, int basenamelen,
 877                   const char *pattern, int prefix, int patternlen,
 878                   unsigned flags)
 879{
 880        if (prefix == patternlen) {
 881                if (patternlen == basenamelen &&
 882                    !fspathncmp(pattern, basename, basenamelen))
 883                        return 1;
 884        } else if (flags & EXC_FLAG_ENDSWITH) {
 885                /* "*literal" matching against "fooliteral" */
 886                if (patternlen - 1 <= basenamelen &&
 887                    !fspathncmp(pattern + 1,
 888                                   basename + basenamelen - (patternlen - 1),
 889                                   patternlen - 1))
 890                        return 1;
 891        } else {
 892                if (fnmatch_icase_mem(pattern, patternlen,
 893                                      basename, basenamelen,
 894                                      0) == 0)
 895                        return 1;
 896        }
 897        return 0;
 898}
 899
 900int match_pathname(const char *pathname, int pathlen,
 901                   const char *base, int baselen,
 902                   const char *pattern, int prefix, int patternlen,
 903                   unsigned flags)
 904{
 905        const char *name;
 906        int namelen;
 907
 908        /*
 909         * match with FNM_PATHNAME; the pattern has base implicitly
 910         * in front of it.
 911         */
 912        if (*pattern == '/') {
 913                pattern++;
 914                patternlen--;
 915                prefix--;
 916        }
 917
 918        /*
 919         * baselen does not count the trailing slash. base[] may or
 920         * may not end with a trailing slash though.
 921         */
 922        if (pathlen < baselen + 1 ||
 923            (baselen && pathname[baselen] != '/') ||
 924            fspathncmp(pathname, base, baselen))
 925                return 0;
 926
 927        namelen = baselen ? pathlen - baselen - 1 : pathlen;
 928        name = pathname + pathlen - namelen;
 929
 930        if (prefix) {
 931                /*
 932                 * if the non-wildcard part is longer than the
 933                 * remaining pathname, surely it cannot match.
 934                 */
 935                if (prefix > namelen)
 936                        return 0;
 937
 938                if (fspathncmp(pattern, name, prefix))
 939                        return 0;
 940                pattern += prefix;
 941                patternlen -= prefix;
 942                name    += prefix;
 943                namelen -= prefix;
 944
 945                /*
 946                 * If the whole pattern did not have a wildcard,
 947                 * then our prefix match is all we need; we
 948                 * do not need to call fnmatch at all.
 949                 */
 950                if (!patternlen && !namelen)
 951                        return 1;
 952        }
 953
 954        return fnmatch_icase_mem(pattern, patternlen,
 955                                 name, namelen,
 956                                 WM_PATHNAME) == 0;
 957}
 958
 959/*
 960 * Scan the given exclude list in reverse to see whether pathname
 961 * should be ignored.  The first match (i.e. the last on the list), if
 962 * any, determines the fate.  Returns the exclude_list element which
 963 * matched, or NULL for undecided.
 964 */
 965static struct exclude *last_exclude_matching_from_list(const char *pathname,
 966                                                       int pathlen,
 967                                                       const char *basename,
 968                                                       int *dtype,
 969                                                       struct exclude_list *el,
 970                                                       struct index_state *istate)
 971{
 972        struct exclude *exc = NULL; /* undecided */
 973        int i;
 974
 975        if (!el->nr)
 976                return NULL;    /* undefined */
 977
 978        for (i = el->nr - 1; 0 <= i; i--) {
 979                struct exclude *x = el->excludes[i];
 980                const char *exclude = x->pattern;
 981                int prefix = x->nowildcardlen;
 982
 983                if (x->flags & EXC_FLAG_MUSTBEDIR) {
 984                        if (*dtype == DT_UNKNOWN)
 985                                *dtype = get_dtype(NULL, istate, pathname, pathlen);
 986                        if (*dtype != DT_DIR)
 987                                continue;
 988                }
 989
 990                if (x->flags & EXC_FLAG_NODIR) {
 991                        if (match_basename(basename,
 992                                           pathlen - (basename - pathname),
 993                                           exclude, prefix, x->patternlen,
 994                                           x->flags)) {
 995                                exc = x;
 996                                break;
 997                        }
 998                        continue;
 999                }
1000
1001                assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
1002                if (match_pathname(pathname, pathlen,
1003                                   x->base, x->baselen ? x->baselen - 1 : 0,
1004                                   exclude, prefix, x->patternlen, x->flags)) {
1005                        exc = x;
1006                        break;
1007                }
1008        }
1009        return exc;
1010}
1011
1012/*
1013 * Scan the list and let the last match determine the fate.
1014 * Return 1 for exclude, 0 for include and -1 for undecided.
1015 */
1016int is_excluded_from_list(const char *pathname,
1017                          int pathlen, const char *basename, int *dtype,
1018                          struct exclude_list *el, struct index_state *istate)
1019{
1020        struct exclude *exclude;
1021        exclude = last_exclude_matching_from_list(pathname, pathlen, basename,
1022                                                  dtype, el, istate);
1023        if (exclude)
1024                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1025        return -1; /* undecided */
1026}
1027
1028static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
1029                                                        struct index_state *istate,
1030                const char *pathname, int pathlen, const char *basename,
1031                int *dtype_p)
1032{
1033        int i, j;
1034        struct exclude_list_group *group;
1035        struct exclude *exclude;
1036        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1037                group = &dir->exclude_list_group[i];
1038                for (j = group->nr - 1; j >= 0; j--) {
1039                        exclude = last_exclude_matching_from_list(
1040                                pathname, pathlen, basename, dtype_p,
1041                                &group->el[j], istate);
1042                        if (exclude)
1043                                return exclude;
1044                }
1045        }
1046        return NULL;
1047}
1048
1049/*
1050 * Loads the per-directory exclude list for the substring of base
1051 * which has a char length of baselen.
1052 */
1053static void prep_exclude(struct dir_struct *dir,
1054                         struct index_state *istate,
1055                         const char *base, int baselen)
1056{
1057        struct exclude_list_group *group;
1058        struct exclude_list *el;
1059        struct exclude_stack *stk = NULL;
1060        struct untracked_cache_dir *untracked;
1061        int current;
1062
1063        group = &dir->exclude_list_group[EXC_DIRS];
1064
1065        /*
1066         * Pop the exclude lists from the EXCL_DIRS exclude_list_group
1067         * which originate from directories not in the prefix of the
1068         * path being checked.
1069         */
1070        while ((stk = dir->exclude_stack) != NULL) {
1071                if (stk->baselen <= baselen &&
1072                    !strncmp(dir->basebuf.buf, base, stk->baselen))
1073                        break;
1074                el = &group->el[dir->exclude_stack->exclude_ix];
1075                dir->exclude_stack = stk->prev;
1076                dir->exclude = NULL;
1077                free((char *)el->src); /* see strbuf_detach() below */
1078                clear_exclude_list(el);
1079                free(stk);
1080                group->nr--;
1081        }
1082
1083        /* Skip traversing into sub directories if the parent is excluded */
1084        if (dir->exclude)
1085                return;
1086
1087        /*
1088         * Lazy initialization. All call sites currently just
1089         * memset(dir, 0, sizeof(*dir)) before use. Changing all of
1090         * them seems lots of work for little benefit.
1091         */
1092        if (!dir->basebuf.buf)
1093                strbuf_init(&dir->basebuf, PATH_MAX);
1094
1095        /* Read from the parent directories and push them down. */
1096        current = stk ? stk->baselen : -1;
1097        strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
1098        if (dir->untracked)
1099                untracked = stk ? stk->ucd : dir->untracked->root;
1100        else
1101                untracked = NULL;
1102
1103        while (current < baselen) {
1104                const char *cp;
1105                struct sha1_stat sha1_stat;
1106
1107                stk = xcalloc(1, sizeof(*stk));
1108                if (current < 0) {
1109                        cp = base;
1110                        current = 0;
1111                } else {
1112                        cp = strchr(base + current + 1, '/');
1113                        if (!cp)
1114                                die("oops in prep_exclude");
1115                        cp++;
1116                        untracked =
1117                                lookup_untracked(dir->untracked, untracked,
1118                                                 base + current,
1119                                                 cp - base - current);
1120                }
1121                stk->prev = dir->exclude_stack;
1122                stk->baselen = cp - base;
1123                stk->exclude_ix = group->nr;
1124                stk->ucd = untracked;
1125                el = add_exclude_list(dir, EXC_DIRS, NULL);
1126                strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
1127                assert(stk->baselen == dir->basebuf.len);
1128
1129                /* Abort if the directory is excluded */
1130                if (stk->baselen) {
1131                        int dt = DT_DIR;
1132                        dir->basebuf.buf[stk->baselen - 1] = 0;
1133                        dir->exclude = last_exclude_matching_from_lists(dir,
1134                                                                        istate,
1135                                dir->basebuf.buf, stk->baselen - 1,
1136                                dir->basebuf.buf + current, &dt);
1137                        dir->basebuf.buf[stk->baselen - 1] = '/';
1138                        if (dir->exclude &&
1139                            dir->exclude->flags & EXC_FLAG_NEGATIVE)
1140                                dir->exclude = NULL;
1141                        if (dir->exclude) {
1142                                dir->exclude_stack = stk;
1143                                return;
1144                        }
1145                }
1146
1147                /* Try to read per-directory file */
1148                hashclr(sha1_stat.sha1);
1149                sha1_stat.valid = 0;
1150                if (dir->exclude_per_dir &&
1151                    /*
1152                     * If we know that no files have been added in
1153                     * this directory (i.e. valid_cached_dir() has
1154                     * been executed and set untracked->valid) ..
1155                     */
1156                    (!untracked || !untracked->valid ||
1157                     /*
1158                      * .. and .gitignore does not exist before
1159                      * (i.e. null exclude_sha1). Then we can skip
1160                      * loading .gitignore, which would result in
1161                      * ENOENT anyway.
1162                      */
1163                     !is_null_sha1(untracked->exclude_sha1))) {
1164                        /*
1165                         * dir->basebuf gets reused by the traversal, but we
1166                         * need fname to remain unchanged to ensure the src
1167                         * member of each struct exclude correctly
1168                         * back-references its source file.  Other invocations
1169                         * of add_exclude_list provide stable strings, so we
1170                         * strbuf_detach() and free() here in the caller.
1171                         */
1172                        struct strbuf sb = STRBUF_INIT;
1173                        strbuf_addbuf(&sb, &dir->basebuf);
1174                        strbuf_addstr(&sb, dir->exclude_per_dir);
1175                        el->src = strbuf_detach(&sb, NULL);
1176                        add_excludes(el->src, el->src, stk->baselen, el, istate,
1177                                     untracked ? &sha1_stat : NULL);
1178                }
1179                /*
1180                 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1181                 * will first be called in valid_cached_dir() then maybe many
1182                 * times more in last_exclude_matching(). When the cache is
1183                 * used, last_exclude_matching() will not be called and
1184                 * reading .gitignore content will be a waste.
1185                 *
1186                 * So when it's called by valid_cached_dir() and we can get
1187                 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1188                 * modified on work tree), we could delay reading the
1189                 * .gitignore content until we absolutely need it in
1190                 * last_exclude_matching(). Be careful about ignore rule
1191                 * order, though, if you do that.
1192                 */
1193                if (untracked &&
1194                    hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1195                        invalidate_gitignore(dir->untracked, untracked);
1196                        hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
1197                }
1198                dir->exclude_stack = stk;
1199                current = stk->baselen;
1200        }
1201        strbuf_setlen(&dir->basebuf, baselen);
1202}
1203
1204/*
1205 * Loads the exclude lists for the directory containing pathname, then
1206 * scans all exclude lists to determine whether pathname is excluded.
1207 * Returns the exclude_list element which matched, or NULL for
1208 * undecided.
1209 */
1210struct exclude *last_exclude_matching(struct dir_struct *dir,
1211                                      struct index_state *istate,
1212                                      const char *pathname,
1213                                      int *dtype_p)
1214{
1215        int pathlen = strlen(pathname);
1216        const char *basename = strrchr(pathname, '/');
1217        basename = (basename) ? basename+1 : pathname;
1218
1219        prep_exclude(dir, istate, pathname, basename-pathname);
1220
1221        if (dir->exclude)
1222                return dir->exclude;
1223
1224        return last_exclude_matching_from_lists(dir, istate, pathname, pathlen,
1225                        basename, dtype_p);
1226}
1227
1228/*
1229 * Loads the exclude lists for the directory containing pathname, then
1230 * scans all exclude lists to determine whether pathname is excluded.
1231 * Returns 1 if true, otherwise 0.
1232 */
1233int is_excluded(struct dir_struct *dir, struct index_state *istate,
1234                const char *pathname, int *dtype_p)
1235{
1236        struct exclude *exclude =
1237                last_exclude_matching(dir, istate, pathname, dtype_p);
1238        if (exclude)
1239                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1240        return 0;
1241}
1242
1243static struct dir_entry *dir_entry_new(const char *pathname, int len)
1244{
1245        struct dir_entry *ent;
1246
1247        FLEX_ALLOC_MEM(ent, name, pathname, len);
1248        ent->len = len;
1249        return ent;
1250}
1251
1252static struct dir_entry *dir_add_name(struct dir_struct *dir,
1253                                      struct index_state *istate,
1254                                      const char *pathname, int len)
1255{
1256        if (index_file_exists(istate, pathname, len, ignore_case))
1257                return NULL;
1258
1259        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
1260        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1261}
1262
1263struct dir_entry *dir_add_ignored(struct dir_struct *dir,
1264                                  struct index_state *istate,
1265                                  const char *pathname, int len)
1266{
1267        if (!index_name_is_other(istate, pathname, len))
1268                return NULL;
1269
1270        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
1271        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1272}
1273
1274enum exist_status {
1275        index_nonexistent = 0,
1276        index_directory,
1277        index_gitdir
1278};
1279
1280/*
1281 * Do not use the alphabetically sorted index to look up
1282 * the directory name; instead, use the case insensitive
1283 * directory hash.
1284 */
1285static enum exist_status directory_exists_in_index_icase(struct index_state *istate,
1286                                                         const char *dirname, int len)
1287{
1288        struct cache_entry *ce;
1289
1290        if (index_dir_exists(istate, dirname, len))
1291                return index_directory;
1292
1293        ce = index_file_exists(istate, dirname, len, ignore_case);
1294        if (ce && S_ISGITLINK(ce->ce_mode))
1295                return index_gitdir;
1296
1297        return index_nonexistent;
1298}
1299
1300/*
1301 * The index sorts alphabetically by entry name, which
1302 * means that a gitlink sorts as '\0' at the end, while
1303 * a directory (which is defined not as an entry, but as
1304 * the files it contains) will sort with the '/' at the
1305 * end.
1306 */
1307static enum exist_status directory_exists_in_index(struct index_state *istate,
1308                                                   const char *dirname, int len)
1309{
1310        int pos;
1311
1312        if (ignore_case)
1313                return directory_exists_in_index_icase(istate, dirname, len);
1314
1315        pos = index_name_pos(istate, dirname, len);
1316        if (pos < 0)
1317                pos = -pos-1;
1318        while (pos < istate->cache_nr) {
1319                const struct cache_entry *ce = istate->cache[pos++];
1320                unsigned char endchar;
1321
1322                if (strncmp(ce->name, dirname, len))
1323                        break;
1324                endchar = ce->name[len];
1325                if (endchar > '/')
1326                        break;
1327                if (endchar == '/')
1328                        return index_directory;
1329                if (!endchar && S_ISGITLINK(ce->ce_mode))
1330                        return index_gitdir;
1331        }
1332        return index_nonexistent;
1333}
1334
1335/*
1336 * When we find a directory when traversing the filesystem, we
1337 * have three distinct cases:
1338 *
1339 *  - ignore it
1340 *  - see it as a directory
1341 *  - recurse into it
1342 *
1343 * and which one we choose depends on a combination of existing
1344 * git index contents and the flags passed into the directory
1345 * traversal routine.
1346 *
1347 * Case 1: If we *already* have entries in the index under that
1348 * directory name, we always recurse into the directory to see
1349 * all the files.
1350 *
1351 * Case 2: If we *already* have that directory name as a gitlink,
1352 * we always continue to see it as a gitlink, regardless of whether
1353 * there is an actual git directory there or not (it might not
1354 * be checked out as a subproject!)
1355 *
1356 * Case 3: if we didn't have it in the index previously, we
1357 * have a few sub-cases:
1358 *
1359 *  (a) if "show_other_directories" is true, we show it as
1360 *      just a directory, unless "hide_empty_directories" is
1361 *      also true, in which case we need to check if it contains any
1362 *      untracked and / or ignored files.
1363 *  (b) if it looks like a git directory, and we don't have
1364 *      'no_gitlinks' set we treat it as a gitlink, and show it
1365 *      as a directory.
1366 *  (c) otherwise, we recurse into it.
1367 */
1368static enum path_treatment treat_directory(struct dir_struct *dir,
1369        struct index_state *istate,
1370        struct untracked_cache_dir *untracked,
1371        const char *dirname, int len, int baselen, int exclude,
1372        const struct pathspec *pathspec)
1373{
1374        /* The "len-1" is to strip the final '/' */
1375        switch (directory_exists_in_index(istate, dirname, len-1)) {
1376        case index_directory:
1377                return path_recurse;
1378
1379        case index_gitdir:
1380                return path_none;
1381
1382        case index_nonexistent:
1383                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1384                        break;
1385                if (!(dir->flags & DIR_NO_GITLINKS)) {
1386                        unsigned char sha1[20];
1387                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
1388                                return path_untracked;
1389                }
1390                return path_recurse;
1391        }
1392
1393        /* This is the "show_other_directories" case */
1394
1395        if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1396                return exclude ? path_excluded : path_untracked;
1397
1398        untracked = lookup_untracked(dir->untracked, untracked,
1399                                     dirname + baselen, len - baselen);
1400        return read_directory_recursive(dir, istate, dirname, len,
1401                                        untracked, 1, pathspec);
1402}
1403
1404/*
1405 * This is an inexact early pruning of any recursive directory
1406 * reading - if the path cannot possibly be in the pathspec,
1407 * return true, and we'll skip it early.
1408 */
1409static int simplify_away(const char *path, int pathlen,
1410                         const struct pathspec *pathspec)
1411{
1412        int i;
1413
1414        if (!pathspec || !pathspec->nr)
1415                return 0;
1416
1417        GUARD_PATHSPEC(pathspec,
1418                       PATHSPEC_FROMTOP |
1419                       PATHSPEC_MAXDEPTH |
1420                       PATHSPEC_LITERAL |
1421                       PATHSPEC_GLOB |
1422                       PATHSPEC_ICASE |
1423                       PATHSPEC_EXCLUDE |
1424                       PATHSPEC_ATTR);
1425
1426        for (i = 0; i < pathspec->nr; i++) {
1427                const struct pathspec_item *item = &pathspec->items[i];
1428                int len = item->nowildcard_len;
1429
1430                if (len > pathlen)
1431                        len = pathlen;
1432                if (!ps_strncmp(item, item->match, path, len))
1433                        return 0;
1434        }
1435
1436        return 1;
1437}
1438
1439/*
1440 * This function tells us whether an excluded path matches a
1441 * list of "interesting" pathspecs. That is, whether a path matched
1442 * by any of the pathspecs could possibly be ignored by excluding
1443 * the specified path. This can happen if:
1444 *
1445 *   1. the path is mentioned explicitly in the pathspec
1446 *
1447 *   2. the path is a directory prefix of some element in the
1448 *      pathspec
1449 */
1450static int exclude_matches_pathspec(const char *path, int pathlen,
1451                                    const struct pathspec *pathspec)
1452{
1453        int i;
1454
1455        if (!pathspec || !pathspec->nr)
1456                return 0;
1457
1458        GUARD_PATHSPEC(pathspec,
1459                       PATHSPEC_FROMTOP |
1460                       PATHSPEC_MAXDEPTH |
1461                       PATHSPEC_LITERAL |
1462                       PATHSPEC_GLOB |
1463                       PATHSPEC_ICASE |
1464                       PATHSPEC_EXCLUDE);
1465
1466        for (i = 0; i < pathspec->nr; i++) {
1467                const struct pathspec_item *item = &pathspec->items[i];
1468                int len = item->nowildcard_len;
1469
1470                if (len == pathlen &&
1471                    !ps_strncmp(item, item->match, path, pathlen))
1472                        return 1;
1473                if (len > pathlen &&
1474                    item->match[pathlen] == '/' &&
1475                    !ps_strncmp(item, item->match, path, pathlen))
1476                        return 1;
1477        }
1478        return 0;
1479}
1480
1481static int get_index_dtype(struct index_state *istate,
1482                           const char *path, int len)
1483{
1484        int pos;
1485        const struct cache_entry *ce;
1486
1487        ce = index_file_exists(istate, path, len, 0);
1488        if (ce) {
1489                if (!ce_uptodate(ce))
1490                        return DT_UNKNOWN;
1491                if (S_ISGITLINK(ce->ce_mode))
1492                        return DT_DIR;
1493                /*
1494                 * Nobody actually cares about the
1495                 * difference between DT_LNK and DT_REG
1496                 */
1497                return DT_REG;
1498        }
1499
1500        /* Try to look it up as a directory */
1501        pos = index_name_pos(istate, path, len);
1502        if (pos >= 0)
1503                return DT_UNKNOWN;
1504        pos = -pos-1;
1505        while (pos < istate->cache_nr) {
1506                ce = istate->cache[pos++];
1507                if (strncmp(ce->name, path, len))
1508                        break;
1509                if (ce->name[len] > '/')
1510                        break;
1511                if (ce->name[len] < '/')
1512                        continue;
1513                if (!ce_uptodate(ce))
1514                        break;  /* continue? */
1515                return DT_DIR;
1516        }
1517        return DT_UNKNOWN;
1518}
1519
1520static int get_dtype(struct dirent *de, struct index_state *istate,
1521                     const char *path, int len)
1522{
1523        int dtype = de ? DTYPE(de) : DT_UNKNOWN;
1524        struct stat st;
1525
1526        if (dtype != DT_UNKNOWN)
1527                return dtype;
1528        dtype = get_index_dtype(istate, path, len);
1529        if (dtype != DT_UNKNOWN)
1530                return dtype;
1531        if (lstat(path, &st))
1532                return dtype;
1533        if (S_ISREG(st.st_mode))
1534                return DT_REG;
1535        if (S_ISDIR(st.st_mode))
1536                return DT_DIR;
1537        if (S_ISLNK(st.st_mode))
1538                return DT_LNK;
1539        return dtype;
1540}
1541
1542static enum path_treatment treat_one_path(struct dir_struct *dir,
1543                                          struct untracked_cache_dir *untracked,
1544                                          struct index_state *istate,
1545                                          struct strbuf *path,
1546                                          int baselen,
1547                                          const struct pathspec *pathspec,
1548                                          int dtype, struct dirent *de)
1549{
1550        int exclude;
1551        int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);
1552
1553        if (dtype == DT_UNKNOWN)
1554                dtype = get_dtype(de, istate, path->buf, path->len);
1555
1556        /* Always exclude indexed files */
1557        if (dtype != DT_DIR && has_path_in_index)
1558                return path_none;
1559
1560        /*
1561         * When we are looking at a directory P in the working tree,
1562         * there are three cases:
1563         *
1564         * (1) P exists in the index.  Everything inside the directory P in
1565         * the working tree needs to go when P is checked out from the
1566         * index.
1567         *
1568         * (2) P does not exist in the index, but there is P/Q in the index.
1569         * We know P will stay a directory when we check out the contents
1570         * of the index, but we do not know yet if there is a directory
1571         * P/Q in the working tree to be killed, so we need to recurse.
1572         *
1573         * (3) P does not exist in the index, and there is no P/Q in the index
1574         * to require P to be a directory, either.  Only in this case, we
1575         * know that everything inside P will not be killed without
1576         * recursing.
1577         */
1578        if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
1579            (dtype == DT_DIR) &&
1580            !has_path_in_index &&
1581            (directory_exists_in_index(istate, path->buf, path->len) == index_nonexistent))
1582                return path_none;
1583
1584        exclude = is_excluded(dir, istate, path->buf, &dtype);
1585
1586        /*
1587         * Excluded? If we don't explicitly want to show
1588         * ignored files, ignore it
1589         */
1590        if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))
1591                return path_excluded;
1592
1593        switch (dtype) {
1594        default:
1595                return path_none;
1596        case DT_DIR:
1597                strbuf_addch(path, '/');
1598                return treat_directory(dir, istate, untracked, path->buf, path->len,
1599                                       baselen, exclude, pathspec);
1600        case DT_REG:
1601        case DT_LNK:
1602                return exclude ? path_excluded : path_untracked;
1603        }
1604}
1605
1606static enum path_treatment treat_path_fast(struct dir_struct *dir,
1607                                           struct untracked_cache_dir *untracked,
1608                                           struct cached_dir *cdir,
1609                                           struct index_state *istate,
1610                                           struct strbuf *path,
1611                                           int baselen,
1612                                           const struct pathspec *pathspec)
1613{
1614        strbuf_setlen(path, baselen);
1615        if (!cdir->ucd) {
1616                strbuf_addstr(path, cdir->file);
1617                return path_untracked;
1618        }
1619        strbuf_addstr(path, cdir->ucd->name);
1620        /* treat_one_path() does this before it calls treat_directory() */
1621        strbuf_complete(path, '/');
1622        if (cdir->ucd->check_only)
1623                /*
1624                 * check_only is set as a result of treat_directory() getting
1625                 * to its bottom. Verify again the same set of directories
1626                 * with check_only set.
1627                 */
1628                return read_directory_recursive(dir, istate, path->buf, path->len,
1629                                                cdir->ucd, 1, pathspec);
1630        /*
1631         * We get path_recurse in the first run when
1632         * directory_exists_in_index() returns index_nonexistent. We
1633         * are sure that new changes in the index does not impact the
1634         * outcome. Return now.
1635         */
1636        return path_recurse;
1637}
1638
1639static enum path_treatment treat_path(struct dir_struct *dir,
1640                                      struct untracked_cache_dir *untracked,
1641                                      struct cached_dir *cdir,
1642                                      struct index_state *istate,
1643                                      struct strbuf *path,
1644                                      int baselen,
1645                                      const struct pathspec *pathspec)
1646{
1647        int dtype;
1648        struct dirent *de = cdir->de;
1649
1650        if (!de)
1651                return treat_path_fast(dir, untracked, cdir, istate, path,
1652                                       baselen, pathspec);
1653        if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1654                return path_none;
1655        strbuf_setlen(path, baselen);
1656        strbuf_addstr(path, de->d_name);
1657        if (simplify_away(path->buf, path->len, pathspec))
1658                return path_none;
1659
1660        dtype = DTYPE(de);
1661        return treat_one_path(dir, untracked, istate, path, baselen, pathspec, dtype, de);
1662}
1663
1664static void add_untracked(struct untracked_cache_dir *dir, const char *name)
1665{
1666        if (!dir)
1667                return;
1668        ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,
1669                   dir->untracked_alloc);
1670        dir->untracked[dir->untracked_nr++] = xstrdup(name);
1671}
1672
1673static int valid_cached_dir(struct dir_struct *dir,
1674                            struct untracked_cache_dir *untracked,
1675                            struct index_state *istate,
1676                            struct strbuf *path,
1677                            int check_only)
1678{
1679        struct stat st;
1680
1681        if (!untracked)
1682                return 0;
1683
1684        if (stat(path->len ? path->buf : ".", &st)) {
1685                invalidate_directory(dir->untracked, untracked);
1686                memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
1687                return 0;
1688        }
1689        if (!untracked->valid ||
1690            match_stat_data_racy(istate, &untracked->stat_data, &st)) {
1691                if (untracked->valid)
1692                        invalidate_directory(dir->untracked, untracked);
1693                fill_stat_data(&untracked->stat_data, &st);
1694                return 0;
1695        }
1696
1697        if (untracked->check_only != !!check_only) {
1698                invalidate_directory(dir->untracked, untracked);
1699                return 0;
1700        }
1701
1702        /*
1703         * prep_exclude will be called eventually on this directory,
1704         * but it's called much later in last_exclude_matching(). We
1705         * need it now to determine the validity of the cache for this
1706         * path. The next calls will be nearly no-op, the way
1707         * prep_exclude() is designed.
1708         */
1709        if (path->len && path->buf[path->len - 1] != '/') {
1710                strbuf_addch(path, '/');
1711                prep_exclude(dir, istate, path->buf, path->len);
1712                strbuf_setlen(path, path->len - 1);
1713        } else
1714                prep_exclude(dir, istate, path->buf, path->len);
1715
1716        /* hopefully prep_exclude() haven't invalidated this entry... */
1717        return untracked->valid;
1718}
1719
1720static int open_cached_dir(struct cached_dir *cdir,
1721                           struct dir_struct *dir,
1722                           struct untracked_cache_dir *untracked,
1723                           struct index_state *istate,
1724                           struct strbuf *path,
1725                           int check_only)
1726{
1727        memset(cdir, 0, sizeof(*cdir));
1728        cdir->untracked = untracked;
1729        if (valid_cached_dir(dir, untracked, istate, path, check_only))
1730                return 0;
1731        cdir->fdir = opendir(path->len ? path->buf : ".");
1732        if (dir->untracked)
1733                dir->untracked->dir_opened++;
1734        if (!cdir->fdir)
1735                return -1;
1736        return 0;
1737}
1738
1739static int read_cached_dir(struct cached_dir *cdir)
1740{
1741        if (cdir->fdir) {
1742                cdir->de = readdir(cdir->fdir);
1743                if (!cdir->de)
1744                        return -1;
1745                return 0;
1746        }
1747        while (cdir->nr_dirs < cdir->untracked->dirs_nr) {
1748                struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];
1749                if (!d->recurse) {
1750                        cdir->nr_dirs++;
1751                        continue;
1752                }
1753                cdir->ucd = d;
1754                cdir->nr_dirs++;
1755                return 0;
1756        }
1757        cdir->ucd = NULL;
1758        if (cdir->nr_files < cdir->untracked->untracked_nr) {
1759                struct untracked_cache_dir *d = cdir->untracked;
1760                cdir->file = d->untracked[cdir->nr_files++];
1761                return 0;
1762        }
1763        return -1;
1764}
1765
1766static void close_cached_dir(struct cached_dir *cdir)
1767{
1768        if (cdir->fdir)
1769                closedir(cdir->fdir);
1770        /*
1771         * We have gone through this directory and found no untracked
1772         * entries. Mark it valid.
1773         */
1774        if (cdir->untracked) {
1775                cdir->untracked->valid = 1;
1776                cdir->untracked->recurse = 1;
1777        }
1778}
1779
1780/*
1781 * Read a directory tree. We currently ignore anything but
1782 * directories, regular files and symlinks. That's because git
1783 * doesn't handle them at all yet. Maybe that will change some
1784 * day.
1785 *
1786 * Also, we ignore the name ".git" (even if it is not a directory).
1787 * That likely will not change.
1788 *
1789 * Returns the most significant path_treatment value encountered in the scan.
1790 */
1791static enum path_treatment read_directory_recursive(struct dir_struct *dir,
1792        struct index_state *istate, const char *base, int baselen,
1793        struct untracked_cache_dir *untracked, int check_only,
1794        const struct pathspec *pathspec)
1795{
1796        struct cached_dir cdir;
1797        enum path_treatment state, subdir_state, dir_state = path_none;
1798        struct strbuf path = STRBUF_INIT;
1799
1800        strbuf_add(&path, base, baselen);
1801
1802        if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))
1803                goto out;
1804
1805        if (untracked)
1806                untracked->check_only = !!check_only;
1807
1808        while (!read_cached_dir(&cdir)) {
1809                /* check how the file or directory should be treated */
1810                state = treat_path(dir, untracked, &cdir, istate, &path,
1811                                   baselen, pathspec);
1812
1813                if (state > dir_state)
1814                        dir_state = state;
1815
1816                /* recurse into subdir if instructed by treat_path */
1817                if ((state == path_recurse) ||
1818                        ((state == path_untracked) &&
1819                         (dir->flags & DIR_SHOW_IGNORED_TOO) &&
1820                         (get_dtype(cdir.de, istate, path.buf, path.len) == DT_DIR))) {
1821                        struct untracked_cache_dir *ud;
1822                        ud = lookup_untracked(dir->untracked, untracked,
1823                                              path.buf + baselen,
1824                                              path.len - baselen);
1825                        subdir_state =
1826                                read_directory_recursive(dir, istate, path.buf,
1827                                                         path.len, ud,
1828                                                         check_only, pathspec);
1829                        if (subdir_state > dir_state)
1830                                dir_state = subdir_state;
1831                }
1832
1833                if (check_only) {
1834                        /* abort early if maximum state has been reached */
1835                        if (dir_state == path_untracked) {
1836                                if (cdir.fdir)
1837                                        add_untracked(untracked, path.buf + baselen);
1838                                break;
1839                        }
1840                        /* skip the dir_add_* part */
1841                        continue;
1842                }
1843
1844                /* add the path to the appropriate result list */
1845                switch (state) {
1846                case path_excluded:
1847                        if (dir->flags & DIR_SHOW_IGNORED)
1848                                dir_add_name(dir, istate, path.buf, path.len);
1849                        else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||
1850                                ((dir->flags & DIR_COLLECT_IGNORED) &&
1851                                exclude_matches_pathspec(path.buf, path.len,
1852                                                         pathspec)))
1853                                dir_add_ignored(dir, istate, path.buf, path.len);
1854                        break;
1855
1856                case path_untracked:
1857                        if (dir->flags & DIR_SHOW_IGNORED)
1858                                break;
1859                        dir_add_name(dir, istate, path.buf, path.len);
1860                        if (cdir.fdir)
1861                                add_untracked(untracked, path.buf + baselen);
1862                        break;
1863
1864                default:
1865                        break;
1866                }
1867        }
1868        close_cached_dir(&cdir);
1869 out:
1870        strbuf_release(&path);
1871
1872        return dir_state;
1873}
1874
1875int cmp_dir_entry(const void *p1, const void *p2)
1876{
1877        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1878        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1879
1880        return name_compare(e1->name, e1->len, e2->name, e2->len);
1881}
1882
1883/* check if *out lexically strictly contains *in */
1884int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in)
1885{
1886        return (out->len < in->len) &&
1887                (out->name[out->len - 1] == '/') &&
1888                !memcmp(out->name, in->name, out->len);
1889}
1890
1891static int treat_leading_path(struct dir_struct *dir,
1892                              struct index_state *istate,
1893                              const char *path, int len,
1894                              const struct pathspec *pathspec)
1895{
1896        struct strbuf sb = STRBUF_INIT;
1897        int baselen, rc = 0;
1898        const char *cp;
1899        int old_flags = dir->flags;
1900
1901        while (len && path[len - 1] == '/')
1902                len--;
1903        if (!len)
1904                return 1;
1905        baselen = 0;
1906        dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
1907        while (1) {
1908                cp = path + baselen + !!baselen;
1909                cp = memchr(cp, '/', path + len - cp);
1910                if (!cp)
1911                        baselen = len;
1912                else
1913                        baselen = cp - path;
1914                strbuf_setlen(&sb, 0);
1915                strbuf_add(&sb, path, baselen);
1916                if (!is_directory(sb.buf))
1917                        break;
1918                if (simplify_away(sb.buf, sb.len, pathspec))
1919                        break;
1920                if (treat_one_path(dir, NULL, istate, &sb, baselen, pathspec,
1921                                   DT_DIR, NULL) == path_none)
1922                        break; /* do not recurse into it */
1923                if (len <= baselen) {
1924                        rc = 1;
1925                        break; /* finished checking */
1926                }
1927        }
1928        strbuf_release(&sb);
1929        dir->flags = old_flags;
1930        return rc;
1931}
1932
1933static const char *get_ident_string(void)
1934{
1935        static struct strbuf sb = STRBUF_INIT;
1936        struct utsname uts;
1937
1938        if (sb.len)
1939                return sb.buf;
1940        if (uname(&uts) < 0)
1941                die_errno(_("failed to get kernel name and information"));
1942        strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(),
1943                    uts.sysname);
1944        return sb.buf;
1945}
1946
1947static int ident_in_untracked(const struct untracked_cache *uc)
1948{
1949        /*
1950         * Previous git versions may have saved many NUL separated
1951         * strings in the "ident" field, but it is insane to manage
1952         * many locations, so just take care of the first one.
1953         */
1954
1955        return !strcmp(uc->ident.buf, get_ident_string());
1956}
1957
1958static void set_untracked_ident(struct untracked_cache *uc)
1959{
1960        strbuf_reset(&uc->ident);
1961        strbuf_addstr(&uc->ident, get_ident_string());
1962
1963        /*
1964         * This strbuf used to contain a list of NUL separated
1965         * strings, so save NUL too for backward compatibility.
1966         */
1967        strbuf_addch(&uc->ident, 0);
1968}
1969
1970static void new_untracked_cache(struct index_state *istate)
1971{
1972        struct untracked_cache *uc = xcalloc(1, sizeof(*uc));
1973        strbuf_init(&uc->ident, 100);
1974        uc->exclude_per_dir = ".gitignore";
1975        /* should be the same flags used by git-status */
1976        uc->dir_flags = DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
1977        set_untracked_ident(uc);
1978        istate->untracked = uc;
1979        istate->cache_changed |= UNTRACKED_CHANGED;
1980}
1981
1982void add_untracked_cache(struct index_state *istate)
1983{
1984        if (!istate->untracked) {
1985                new_untracked_cache(istate);
1986        } else {
1987                if (!ident_in_untracked(istate->untracked)) {
1988                        free_untracked_cache(istate->untracked);
1989                        new_untracked_cache(istate);
1990                }
1991        }
1992}
1993
1994void remove_untracked_cache(struct index_state *istate)
1995{
1996        if (istate->untracked) {
1997                free_untracked_cache(istate->untracked);
1998                istate->untracked = NULL;
1999                istate->cache_changed |= UNTRACKED_CHANGED;
2000        }
2001}
2002
2003static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,
2004                                                      int base_len,
2005                                                      const struct pathspec *pathspec)
2006{
2007        struct untracked_cache_dir *root;
2008
2009        if (!dir->untracked || getenv("GIT_DISABLE_UNTRACKED_CACHE"))
2010                return NULL;
2011
2012        /*
2013         * We only support $GIT_DIR/info/exclude and core.excludesfile
2014         * as the global ignore rule files. Any other additions
2015         * (e.g. from command line) invalidate the cache. This
2016         * condition also catches running setup_standard_excludes()
2017         * before setting dir->untracked!
2018         */
2019        if (dir->unmanaged_exclude_files)
2020                return NULL;
2021
2022        /*
2023         * Optimize for the main use case only: whole-tree git
2024         * status. More work involved in treat_leading_path() if we
2025         * use cache on just a subset of the worktree. pathspec
2026         * support could make the matter even worse.
2027         */
2028        if (base_len || (pathspec && pathspec->nr))
2029                return NULL;
2030
2031        /* Different set of flags may produce different results */
2032        if (dir->flags != dir->untracked->dir_flags ||
2033            /*
2034             * See treat_directory(), case index_nonexistent. Without
2035             * this flag, we may need to also cache .git file content
2036             * for the resolve_gitlink_ref() call, which we don't.
2037             */
2038            !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||
2039            /* We don't support collecting ignore files */
2040            (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |
2041                           DIR_COLLECT_IGNORED)))
2042                return NULL;
2043
2044        /*
2045         * If we use .gitignore in the cache and now you change it to
2046         * .gitexclude, everything will go wrong.
2047         */
2048        if (dir->exclude_per_dir != dir->untracked->exclude_per_dir &&
2049            strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))
2050                return NULL;
2051
2052        /*
2053         * EXC_CMDL is not considered in the cache. If people set it,
2054         * skip the cache.
2055         */
2056        if (dir->exclude_list_group[EXC_CMDL].nr)
2057                return NULL;
2058
2059        if (!ident_in_untracked(dir->untracked)) {
2060                warning(_("Untracked cache is disabled on this system or location."));
2061                return NULL;
2062        }
2063
2064        if (!dir->untracked->root) {
2065                const int len = sizeof(*dir->untracked->root);
2066                dir->untracked->root = xmalloc(len);
2067                memset(dir->untracked->root, 0, len);
2068        }
2069
2070        /* Validate $GIT_DIR/info/exclude and core.excludesfile */
2071        root = dir->untracked->root;
2072        if (hashcmp(dir->ss_info_exclude.sha1,
2073                    dir->untracked->ss_info_exclude.sha1)) {
2074                invalidate_gitignore(dir->untracked, root);
2075                dir->untracked->ss_info_exclude = dir->ss_info_exclude;
2076        }
2077        if (hashcmp(dir->ss_excludes_file.sha1,
2078                    dir->untracked->ss_excludes_file.sha1)) {
2079                invalidate_gitignore(dir->untracked, root);
2080                dir->untracked->ss_excludes_file = dir->ss_excludes_file;
2081        }
2082
2083        /* Make sure this directory is not dropped out at saving phase */
2084        root->recurse = 1;
2085        return root;
2086}
2087
2088int read_directory(struct dir_struct *dir, struct index_state *istate,
2089                   const char *path, int len, const struct pathspec *pathspec)
2090{
2091        struct untracked_cache_dir *untracked;
2092
2093        if (has_symlink_leading_path(path, len))
2094                return dir->nr;
2095
2096        untracked = validate_untracked_cache(dir, len, pathspec);
2097        if (!untracked)
2098                /*
2099                 * make sure untracked cache code path is disabled,
2100                 * e.g. prep_exclude()
2101                 */
2102                dir->untracked = NULL;
2103        if (!len || treat_leading_path(dir, istate, path, len, pathspec))
2104                read_directory_recursive(dir, istate, path, len, untracked, 0, pathspec);
2105        QSORT(dir->entries, dir->nr, cmp_dir_entry);
2106        QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);
2107
2108        /*
2109         * If DIR_SHOW_IGNORED_TOO is set, read_directory_recursive() will
2110         * also pick up untracked contents of untracked dirs; by default
2111         * we discard these, but given DIR_KEEP_UNTRACKED_CONTENTS we do not.
2112         */
2113        if ((dir->flags & DIR_SHOW_IGNORED_TOO) &&
2114                     !(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {
2115                int i, j;
2116
2117                /* remove from dir->entries untracked contents of untracked dirs */
2118                for (i = j = 0; j < dir->nr; j++) {
2119                        if (i &&
2120                            check_dir_entry_contains(dir->entries[i - 1], dir->entries[j])) {
2121                                free(dir->entries[j]);
2122                                dir->entries[j] = NULL;
2123                        } else {
2124                                dir->entries[i++] = dir->entries[j];
2125                        }
2126                }
2127
2128                dir->nr = i;
2129        }
2130
2131        if (dir->untracked) {
2132                static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
2133                trace_printf_key(&trace_untracked_stats,
2134                                 "node creation: %u\n"
2135                                 "gitignore invalidation: %u\n"
2136                                 "directory invalidation: %u\n"
2137                                 "opendir: %u\n",
2138                                 dir->untracked->dir_created,
2139                                 dir->untracked->gitignore_invalidated,
2140                                 dir->untracked->dir_invalidated,
2141                                 dir->untracked->dir_opened);
2142                if (dir->untracked == istate->untracked &&
2143                    (dir->untracked->dir_opened ||
2144                     dir->untracked->gitignore_invalidated ||
2145                     dir->untracked->dir_invalidated))
2146                        istate->cache_changed |= UNTRACKED_CHANGED;
2147                if (dir->untracked != istate->untracked) {
2148                        free(dir->untracked);
2149                        dir->untracked = NULL;
2150                }
2151        }
2152        return dir->nr;
2153}
2154
2155int file_exists(const char *f)
2156{
2157        struct stat sb;
2158        return lstat(f, &sb) == 0;
2159}
2160
2161static int cmp_icase(char a, char b)
2162{
2163        if (a == b)
2164                return 0;
2165        if (ignore_case)
2166                return toupper(a) - toupper(b);
2167        return a - b;
2168}
2169
2170/*
2171 * Given two normalized paths (a trailing slash is ok), if subdir is
2172 * outside dir, return -1.  Otherwise return the offset in subdir that
2173 * can be used as relative path to dir.
2174 */
2175int dir_inside_of(const char *subdir, const char *dir)
2176{
2177        int offset = 0;
2178
2179        assert(dir && subdir && *dir && *subdir);
2180
2181        while (*dir && *subdir && !cmp_icase(*dir, *subdir)) {
2182                dir++;
2183                subdir++;
2184                offset++;
2185        }
2186
2187        /* hel[p]/me vs hel[l]/yeah */
2188        if (*dir && *subdir)
2189                return -1;
2190
2191        if (!*subdir)
2192                return !*dir ? offset : -1; /* same dir */
2193
2194        /* foo/[b]ar vs foo/[] */
2195        if (is_dir_sep(dir[-1]))
2196                return is_dir_sep(subdir[-1]) ? offset : -1;
2197
2198        /* foo[/]bar vs foo[] */
2199        return is_dir_sep(*subdir) ? offset + 1 : -1;
2200}
2201
2202int is_inside_dir(const char *dir)
2203{
2204        char *cwd;
2205        int rc;
2206
2207        if (!dir)
2208                return 0;
2209
2210        cwd = xgetcwd();
2211        rc = (dir_inside_of(cwd, dir) >= 0);
2212        free(cwd);
2213        return rc;
2214}
2215
2216int is_empty_dir(const char *path)
2217{
2218        DIR *dir = opendir(path);
2219        struct dirent *e;
2220        int ret = 1;
2221
2222        if (!dir)
2223                return 0;
2224
2225        while ((e = readdir(dir)) != NULL)
2226                if (!is_dot_or_dotdot(e->d_name)) {
2227                        ret = 0;
2228                        break;
2229                }
2230
2231        closedir(dir);
2232        return ret;
2233}
2234
2235static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
2236{
2237        DIR *dir;
2238        struct dirent *e;
2239        int ret = 0, original_len = path->len, len, kept_down = 0;
2240        int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
2241        int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
2242        unsigned char submodule_head[20];
2243
2244        if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
2245            !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
2246                /* Do not descend and nuke a nested git work tree. */
2247                if (kept_up)
2248                        *kept_up = 1;
2249                return 0;
2250        }
2251
2252        flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
2253        dir = opendir(path->buf);
2254        if (!dir) {
2255                if (errno == ENOENT)
2256                        return keep_toplevel ? -1 : 0;
2257                else if (errno == EACCES && !keep_toplevel)
2258                        /*
2259                         * An empty dir could be removable even if it
2260                         * is unreadable:
2261                         */
2262                        return rmdir(path->buf);
2263                else
2264                        return -1;
2265        }
2266        strbuf_complete(path, '/');
2267
2268        len = path->len;
2269        while ((e = readdir(dir)) != NULL) {
2270                struct stat st;
2271                if (is_dot_or_dotdot(e->d_name))
2272                        continue;
2273
2274                strbuf_setlen(path, len);
2275                strbuf_addstr(path, e->d_name);
2276                if (lstat(path->buf, &st)) {
2277                        if (errno == ENOENT)
2278                                /*
2279                                 * file disappeared, which is what we
2280                                 * wanted anyway
2281                                 */
2282                                continue;
2283                        /* fall thru */
2284                } else if (S_ISDIR(st.st_mode)) {
2285                        if (!remove_dir_recurse(path, flag, &kept_down))
2286                                continue; /* happy */
2287                } else if (!only_empty &&
2288                           (!unlink(path->buf) || errno == ENOENT)) {
2289                        continue; /* happy, too */
2290                }
2291
2292                /* path too long, stat fails, or non-directory still exists */
2293                ret = -1;
2294                break;
2295        }
2296        closedir(dir);
2297
2298        strbuf_setlen(path, original_len);
2299        if (!ret && !keep_toplevel && !kept_down)
2300                ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
2301        else if (kept_up)
2302                /*
2303                 * report the uplevel that it is not an error that we
2304                 * did not rmdir() our directory.
2305                 */
2306                *kept_up = !ret;
2307        return ret;
2308}
2309
2310int remove_dir_recursively(struct strbuf *path, int flag)
2311{
2312        return remove_dir_recurse(path, flag, NULL);
2313}
2314
2315static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
2316
2317void setup_standard_excludes(struct dir_struct *dir)
2318{
2319        dir->exclude_per_dir = ".gitignore";
2320
2321        /* core.excludefile defaulting to $XDG_HOME/git/ignore */
2322        if (!excludes_file)
2323                excludes_file = xdg_config_home("ignore");
2324        if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
2325                add_excludes_from_file_1(dir, excludes_file,
2326                                         dir->untracked ? &dir->ss_excludes_file : NULL);
2327
2328        /* per repository user preference */
2329        if (startup_info->have_repository) {
2330                const char *path = git_path_info_exclude();
2331                if (!access_or_warn(path, R_OK, 0))
2332                        add_excludes_from_file_1(dir, path,
2333                                                 dir->untracked ? &dir->ss_info_exclude : NULL);
2334        }
2335}
2336
2337int remove_path(const char *name)
2338{
2339        char *slash;
2340
2341        if (unlink(name) && !is_missing_file_error(errno))
2342                return -1;
2343
2344        slash = strrchr(name, '/');
2345        if (slash) {
2346                char *dirs = xstrdup(name);
2347                slash = dirs + (slash - name);
2348                do {
2349                        *slash = '\0';
2350                } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
2351                free(dirs);
2352        }
2353        return 0;
2354}
2355
2356/*
2357 * Frees memory within dir which was allocated for exclude lists and
2358 * the exclude_stack.  Does not free dir itself.
2359 */
2360void clear_directory(struct dir_struct *dir)
2361{
2362        int i, j;
2363        struct exclude_list_group *group;
2364        struct exclude_list *el;
2365        struct exclude_stack *stk;
2366
2367        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
2368                group = &dir->exclude_list_group[i];
2369                for (j = 0; j < group->nr; j++) {
2370                        el = &group->el[j];
2371                        if (i == EXC_DIRS)
2372                                free((char *)el->src);
2373                        clear_exclude_list(el);
2374                }
2375                free(group->el);
2376        }
2377
2378        stk = dir->exclude_stack;
2379        while (stk) {
2380                struct exclude_stack *prev = stk->prev;
2381                free(stk);
2382                stk = prev;
2383        }
2384        strbuf_release(&dir->basebuf);
2385}
2386
2387struct ondisk_untracked_cache {
2388        struct stat_data info_exclude_stat;
2389        struct stat_data excludes_file_stat;
2390        uint32_t dir_flags;
2391        unsigned char info_exclude_sha1[20];
2392        unsigned char excludes_file_sha1[20];
2393        char exclude_per_dir[FLEX_ARRAY];
2394};
2395
2396#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)
2397
2398struct write_data {
2399        int index;         /* number of written untracked_cache_dir */
2400        struct ewah_bitmap *check_only; /* from untracked_cache_dir */
2401        struct ewah_bitmap *valid;      /* from untracked_cache_dir */
2402        struct ewah_bitmap *sha1_valid; /* set if exclude_sha1 is not null */
2403        struct strbuf out;
2404        struct strbuf sb_stat;
2405        struct strbuf sb_sha1;
2406};
2407
2408static void stat_data_to_disk(struct stat_data *to, const struct stat_data *from)
2409{
2410        to->sd_ctime.sec  = htonl(from->sd_ctime.sec);
2411        to->sd_ctime.nsec = htonl(from->sd_ctime.nsec);
2412        to->sd_mtime.sec  = htonl(from->sd_mtime.sec);
2413        to->sd_mtime.nsec = htonl(from->sd_mtime.nsec);
2414        to->sd_dev        = htonl(from->sd_dev);
2415        to->sd_ino        = htonl(from->sd_ino);
2416        to->sd_uid        = htonl(from->sd_uid);
2417        to->sd_gid        = htonl(from->sd_gid);
2418        to->sd_size       = htonl(from->sd_size);
2419}
2420
2421static void write_one_dir(struct untracked_cache_dir *untracked,
2422                          struct write_data *wd)
2423{
2424        struct stat_data stat_data;
2425        struct strbuf *out = &wd->out;
2426        unsigned char intbuf[16];
2427        unsigned int intlen, value;
2428        int i = wd->index++;
2429
2430        /*
2431         * untracked_nr should be reset whenever valid is clear, but
2432         * for safety..
2433         */
2434        if (!untracked->valid) {
2435                untracked->untracked_nr = 0;
2436                untracked->check_only = 0;
2437        }
2438
2439        if (untracked->check_only)
2440                ewah_set(wd->check_only, i);
2441        if (untracked->valid) {
2442                ewah_set(wd->valid, i);
2443                stat_data_to_disk(&stat_data, &untracked->stat_data);
2444                strbuf_add(&wd->sb_stat, &stat_data, sizeof(stat_data));
2445        }
2446        if (!is_null_sha1(untracked->exclude_sha1)) {
2447                ewah_set(wd->sha1_valid, i);
2448                strbuf_add(&wd->sb_sha1, untracked->exclude_sha1, 20);
2449        }
2450
2451        intlen = encode_varint(untracked->untracked_nr, intbuf);
2452        strbuf_add(out, intbuf, intlen);
2453
2454        /* skip non-recurse directories */
2455        for (i = 0, value = 0; i < untracked->dirs_nr; i++)
2456                if (untracked->dirs[i]->recurse)
2457                        value++;
2458        intlen = encode_varint(value, intbuf);
2459        strbuf_add(out, intbuf, intlen);
2460
2461        strbuf_add(out, untracked->name, strlen(untracked->name) + 1);
2462
2463        for (i = 0; i < untracked->untracked_nr; i++)
2464                strbuf_add(out, untracked->untracked[i],
2465                           strlen(untracked->untracked[i]) + 1);
2466
2467        for (i = 0; i < untracked->dirs_nr; i++)
2468                if (untracked->dirs[i]->recurse)
2469                        write_one_dir(untracked->dirs[i], wd);
2470}
2471
2472void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked)
2473{
2474        struct ondisk_untracked_cache *ouc;
2475        struct write_data wd;
2476        unsigned char varbuf[16];
2477        int varint_len;
2478        size_t len = strlen(untracked->exclude_per_dir);
2479
2480        FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
2481        stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
2482        stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
2483        hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);
2484        hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);
2485        ouc->dir_flags = htonl(untracked->dir_flags);
2486
2487        varint_len = encode_varint(untracked->ident.len, varbuf);
2488        strbuf_add(out, varbuf, varint_len);
2489        strbuf_addbuf(out, &untracked->ident);
2490
2491        strbuf_add(out, ouc, ouc_size(len));
2492        free(ouc);
2493        ouc = NULL;
2494
2495        if (!untracked->root) {
2496                varint_len = encode_varint(0, varbuf);
2497                strbuf_add(out, varbuf, varint_len);
2498                return;
2499        }
2500
2501        wd.index      = 0;
2502        wd.check_only = ewah_new();
2503        wd.valid      = ewah_new();
2504        wd.sha1_valid = ewah_new();
2505        strbuf_init(&wd.out, 1024);
2506        strbuf_init(&wd.sb_stat, 1024);
2507        strbuf_init(&wd.sb_sha1, 1024);
2508        write_one_dir(untracked->root, &wd);
2509
2510        varint_len = encode_varint(wd.index, varbuf);
2511        strbuf_add(out, varbuf, varint_len);
2512        strbuf_addbuf(out, &wd.out);
2513        ewah_serialize_strbuf(wd.valid, out);
2514        ewah_serialize_strbuf(wd.check_only, out);
2515        ewah_serialize_strbuf(wd.sha1_valid, out);
2516        strbuf_addbuf(out, &wd.sb_stat);
2517        strbuf_addbuf(out, &wd.sb_sha1);
2518        strbuf_addch(out, '\0'); /* safe guard for string lists */
2519
2520        ewah_free(wd.valid);
2521        ewah_free(wd.check_only);
2522        ewah_free(wd.sha1_valid);
2523        strbuf_release(&wd.out);
2524        strbuf_release(&wd.sb_stat);
2525        strbuf_release(&wd.sb_sha1);
2526}
2527
2528static void free_untracked(struct untracked_cache_dir *ucd)
2529{
2530        int i;
2531        if (!ucd)
2532                return;
2533        for (i = 0; i < ucd->dirs_nr; i++)
2534                free_untracked(ucd->dirs[i]);
2535        for (i = 0; i < ucd->untracked_nr; i++)
2536                free(ucd->untracked[i]);
2537        free(ucd->untracked);
2538        free(ucd->dirs);
2539        free(ucd);
2540}
2541
2542void free_untracked_cache(struct untracked_cache *uc)
2543{
2544        if (uc)
2545                free_untracked(uc->root);
2546        free(uc);
2547}
2548
2549struct read_data {
2550        int index;
2551        struct untracked_cache_dir **ucd;
2552        struct ewah_bitmap *check_only;
2553        struct ewah_bitmap *valid;
2554        struct ewah_bitmap *sha1_valid;
2555        const unsigned char *data;
2556        const unsigned char *end;
2557};
2558
2559static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from)
2560{
2561        to->sd_ctime.sec  = get_be32(&from->sd_ctime.sec);
2562        to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec);
2563        to->sd_mtime.sec  = get_be32(&from->sd_mtime.sec);
2564        to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec);
2565        to->sd_dev        = get_be32(&from->sd_dev);
2566        to->sd_ino        = get_be32(&from->sd_ino);
2567        to->sd_uid        = get_be32(&from->sd_uid);
2568        to->sd_gid        = get_be32(&from->sd_gid);
2569        to->sd_size       = get_be32(&from->sd_size);
2570}
2571
2572static int read_one_dir(struct untracked_cache_dir **untracked_,
2573                        struct read_data *rd)
2574{
2575        struct untracked_cache_dir ud, *untracked;
2576        const unsigned char *next, *data = rd->data, *end = rd->end;
2577        unsigned int value;
2578        int i, len;
2579
2580        memset(&ud, 0, sizeof(ud));
2581
2582        next = data;
2583        value = decode_varint(&next);
2584        if (next > end)
2585                return -1;
2586        ud.recurse         = 1;
2587        ud.untracked_alloc = value;
2588        ud.untracked_nr    = value;
2589        if (ud.untracked_nr)
2590                ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
2591        data = next;
2592
2593        next = data;
2594        ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
2595        if (next > end)
2596                return -1;
2597        ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
2598        data = next;
2599
2600        len = strlen((const char *)data);
2601        next = data + len + 1;
2602        if (next > rd->end)
2603                return -1;
2604        *untracked_ = untracked = xmalloc(st_add(sizeof(*untracked), len));
2605        memcpy(untracked, &ud, sizeof(ud));
2606        memcpy(untracked->name, data, len + 1);
2607        data = next;
2608
2609        for (i = 0; i < untracked->untracked_nr; i++) {
2610                len = strlen((const char *)data);
2611                next = data + len + 1;
2612                if (next > rd->end)
2613                        return -1;
2614                untracked->untracked[i] = xstrdup((const char*)data);
2615                data = next;
2616        }
2617
2618        rd->ucd[rd->index++] = untracked;
2619        rd->data = data;
2620
2621        for (i = 0; i < untracked->dirs_nr; i++) {
2622                len = read_one_dir(untracked->dirs + i, rd);
2623                if (len < 0)
2624                        return -1;
2625        }
2626        return 0;
2627}
2628
2629static void set_check_only(size_t pos, void *cb)
2630{
2631        struct read_data *rd = cb;
2632        struct untracked_cache_dir *ud = rd->ucd[pos];
2633        ud->check_only = 1;
2634}
2635
2636static void read_stat(size_t pos, void *cb)
2637{
2638        struct read_data *rd = cb;
2639        struct untracked_cache_dir *ud = rd->ucd[pos];
2640        if (rd->data + sizeof(struct stat_data) > rd->end) {
2641                rd->data = rd->end + 1;
2642                return;
2643        }
2644        stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);
2645        rd->data += sizeof(struct stat_data);
2646        ud->valid = 1;
2647}
2648
2649static void read_sha1(size_t pos, void *cb)
2650{
2651        struct read_data *rd = cb;
2652        struct untracked_cache_dir *ud = rd->ucd[pos];
2653        if (rd->data + 20 > rd->end) {
2654                rd->data = rd->end + 1;
2655                return;
2656        }
2657        hashcpy(ud->exclude_sha1, rd->data);
2658        rd->data += 20;
2659}
2660
2661static void load_sha1_stat(struct sha1_stat *sha1_stat,
2662                           const struct stat_data *stat,
2663                           const unsigned char *sha1)
2664{
2665        stat_data_from_disk(&sha1_stat->stat, stat);
2666        hashcpy(sha1_stat->sha1, sha1);
2667        sha1_stat->valid = 1;
2668}
2669
2670struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
2671{
2672        const struct ondisk_untracked_cache *ouc;
2673        struct untracked_cache *uc;
2674        struct read_data rd;
2675        const unsigned char *next = data, *end = (const unsigned char *)data + sz;
2676        const char *ident;
2677        int ident_len, len;
2678
2679        if (sz <= 1 || end[-1] != '\0')
2680                return NULL;
2681        end--;
2682
2683        ident_len = decode_varint(&next);
2684        if (next + ident_len > end)
2685                return NULL;
2686        ident = (const char *)next;
2687        next += ident_len;
2688
2689        ouc = (const struct ondisk_untracked_cache *)next;
2690        if (next + ouc_size(0) > end)
2691                return NULL;
2692
2693        uc = xcalloc(1, sizeof(*uc));
2694        strbuf_init(&uc->ident, ident_len);
2695        strbuf_add(&uc->ident, ident, ident_len);
2696        load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,
2697                       ouc->info_exclude_sha1);
2698        load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,
2699                       ouc->excludes_file_sha1);
2700        uc->dir_flags = get_be32(&ouc->dir_flags);
2701        uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir);
2702        /* NUL after exclude_per_dir is covered by sizeof(*ouc) */
2703        next += ouc_size(strlen(ouc->exclude_per_dir));
2704        if (next >= end)
2705                goto done2;
2706
2707        len = decode_varint(&next);
2708        if (next > end || len == 0)
2709                goto done2;
2710
2711        rd.valid      = ewah_new();
2712        rd.check_only = ewah_new();
2713        rd.sha1_valid = ewah_new();
2714        rd.data       = next;
2715        rd.end        = end;
2716        rd.index      = 0;
2717        ALLOC_ARRAY(rd.ucd, len);
2718
2719        if (read_one_dir(&uc->root, &rd) || rd.index != len)
2720                goto done;
2721
2722        next = rd.data;
2723        len = ewah_read_mmap(rd.valid, next, end - next);
2724        if (len < 0)
2725                goto done;
2726
2727        next += len;
2728        len = ewah_read_mmap(rd.check_only, next, end - next);
2729        if (len < 0)
2730                goto done;
2731
2732        next += len;
2733        len = ewah_read_mmap(rd.sha1_valid, next, end - next);
2734        if (len < 0)
2735                goto done;
2736
2737        ewah_each_bit(rd.check_only, set_check_only, &rd);
2738        rd.data = next + len;
2739        ewah_each_bit(rd.valid, read_stat, &rd);
2740        ewah_each_bit(rd.sha1_valid, read_sha1, &rd);
2741        next = rd.data;
2742
2743done:
2744        free(rd.ucd);
2745        ewah_free(rd.valid);
2746        ewah_free(rd.check_only);
2747        ewah_free(rd.sha1_valid);
2748done2:
2749        if (next != end) {
2750                free_untracked_cache(uc);
2751                uc = NULL;
2752        }
2753        return uc;
2754}
2755
2756static void invalidate_one_directory(struct untracked_cache *uc,
2757                                     struct untracked_cache_dir *ucd)
2758{
2759        uc->dir_invalidated++;
2760        ucd->valid = 0;
2761        ucd->untracked_nr = 0;
2762}
2763
2764/*
2765 * Normally when an entry is added or removed from a directory,
2766 * invalidating that directory is enough. No need to touch its
2767 * ancestors. When a directory is shown as "foo/bar/" in git-status
2768 * however, deleting or adding an entry may have cascading effect.
2769 *
2770 * Say the "foo/bar/file" has become untracked, we need to tell the
2771 * untracked_cache_dir of "foo" that "bar/" is not an untracked
2772 * directory any more (because "bar" is managed by foo as an untracked
2773 * "file").
2774 *
2775 * Similarly, if "foo/bar/file" moves from untracked to tracked and it
2776 * was the last untracked entry in the entire "foo", we should show
2777 * "foo/" instead. Which means we have to invalidate past "bar" up to
2778 * "foo".
2779 *
2780 * This function traverses all directories from root to leaf. If there
2781 * is a chance of one of the above cases happening, we invalidate back
2782 * to root. Otherwise we just invalidate the leaf. There may be a more
2783 * sophisticated way than checking for SHOW_OTHER_DIRECTORIES to
2784 * detect these cases and avoid unnecessary invalidation, for example,
2785 * checking for the untracked entry named "bar/" in "foo", but for now
2786 * stick to something safe and simple.
2787 */
2788static int invalidate_one_component(struct untracked_cache *uc,
2789                                    struct untracked_cache_dir *dir,
2790                                    const char *path, int len)
2791{
2792        const char *rest = strchr(path, '/');
2793
2794        if (rest) {
2795                int component_len = rest - path;
2796                struct untracked_cache_dir *d =
2797                        lookup_untracked(uc, dir, path, component_len);
2798                int ret =
2799                        invalidate_one_component(uc, d, rest + 1,
2800                                                 len - (component_len + 1));
2801                if (ret)
2802                        invalidate_one_directory(uc, dir);
2803                return ret;
2804        }
2805
2806        invalidate_one_directory(uc, dir);
2807        return uc->dir_flags & DIR_SHOW_OTHER_DIRECTORIES;
2808}
2809
2810void untracked_cache_invalidate_path(struct index_state *istate,
2811                                     const char *path)
2812{
2813        if (!istate->untracked || !istate->untracked->root)
2814                return;
2815        invalidate_one_component(istate->untracked, istate->untracked->root,
2816                                 path, strlen(path));
2817}
2818
2819void untracked_cache_remove_from_index(struct index_state *istate,
2820                                       const char *path)
2821{
2822        untracked_cache_invalidate_path(istate, path);
2823}
2824
2825void untracked_cache_add_to_index(struct index_state *istate,
2826                                  const char *path)
2827{
2828        untracked_cache_invalidate_path(istate, path);
2829}
2830
2831/* Update gitfile and core.worktree setting to connect work tree and git dir */
2832void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)
2833{
2834        struct strbuf gitfile_sb = STRBUF_INIT;
2835        struct strbuf cfg_sb = STRBUF_INIT;
2836        struct strbuf rel_path = STRBUF_INIT;
2837        char *git_dir, *work_tree;
2838
2839        /* Prepare .git file */
2840        strbuf_addf(&gitfile_sb, "%s/.git", work_tree_);
2841        if (safe_create_leading_directories_const(gitfile_sb.buf))
2842                die(_("could not create directories for %s"), gitfile_sb.buf);
2843
2844        /* Prepare config file */
2845        strbuf_addf(&cfg_sb, "%s/config", git_dir_);
2846        if (safe_create_leading_directories_const(cfg_sb.buf))
2847                die(_("could not create directories for %s"), cfg_sb.buf);
2848
2849        git_dir = real_pathdup(git_dir_, 1);
2850        work_tree = real_pathdup(work_tree_, 1);
2851
2852        /* Write .git file */
2853        write_file(gitfile_sb.buf, "gitdir: %s",
2854                   relative_path(git_dir, work_tree, &rel_path));
2855        /* Update core.worktree setting */
2856        git_config_set_in_file(cfg_sb.buf, "core.worktree",
2857                               relative_path(work_tree, git_dir, &rel_path));
2858
2859        strbuf_release(&gitfile_sb);
2860        strbuf_release(&cfg_sb);
2861        strbuf_release(&rel_path);
2862        free(work_tree);
2863        free(git_dir);
2864}
2865
2866/*
2867 * Migrate the git directory of the given path from old_git_dir to new_git_dir.
2868 */
2869void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir)
2870{
2871        if (rename(old_git_dir, new_git_dir) < 0)
2872                die_errno(_("could not migrate git directory from '%s' to '%s'"),
2873                        old_git_dir, new_git_dir);
2874
2875        connect_work_tree_and_git_dir(path, new_git_dir);
2876}