dir.con commit dir.c: git-status --ignored: don't list empty ignored directories (0104c9e)
   1/*
   2 * This handles recursive filename detection with exclude
   3 * files, index knowledge etc..
   4 *
   5 * See Documentation/technical/api-directory-listing.txt
   6 *
   7 * Copyright (C) Linus Torvalds, 2005-2006
   8 *               Junio Hamano, 2005-2006
   9 */
  10#include "cache.h"
  11#include "dir.h"
  12#include "refs.h"
  13#include "wildmatch.h"
  14
  15struct path_simplify {
  16        int len;
  17        const char *path;
  18};
  19
  20static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
  21        int check_only, const struct path_simplify *simplify);
  22static int get_dtype(struct dirent *de, const char *path, int len);
  23
  24/* helper string functions with support for the ignore_case flag */
  25int strcmp_icase(const char *a, const char *b)
  26{
  27        return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
  28}
  29
  30int strncmp_icase(const char *a, const char *b, size_t count)
  31{
  32        return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
  33}
  34
  35int fnmatch_icase(const char *pattern, const char *string, int flags)
  36{
  37        return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
  38}
  39
  40inline int git_fnmatch(const char *pattern, const char *string,
  41                       int flags, int prefix)
  42{
  43        int fnm_flags = 0;
  44        if (flags & GFNM_PATHNAME)
  45                fnm_flags |= FNM_PATHNAME;
  46        if (prefix > 0) {
  47                if (strncmp(pattern, string, prefix))
  48                        return FNM_NOMATCH;
  49                pattern += prefix;
  50                string += prefix;
  51        }
  52        if (flags & GFNM_ONESTAR) {
  53                int pattern_len = strlen(++pattern);
  54                int string_len = strlen(string);
  55                return string_len < pattern_len ||
  56                       strcmp(pattern,
  57                              string + string_len - pattern_len);
  58        }
  59        return fnmatch(pattern, string, fnm_flags);
  60}
  61
  62static int fnmatch_icase_mem(const char *pattern, int patternlen,
  63                             const char *string, int stringlen,
  64                             int flags)
  65{
  66        int match_status;
  67        struct strbuf pat_buf = STRBUF_INIT;
  68        struct strbuf str_buf = STRBUF_INIT;
  69        const char *use_pat = pattern;
  70        const char *use_str = string;
  71
  72        if (pattern[patternlen]) {
  73                strbuf_add(&pat_buf, pattern, patternlen);
  74                use_pat = pat_buf.buf;
  75        }
  76        if (string[stringlen]) {
  77                strbuf_add(&str_buf, string, stringlen);
  78                use_str = str_buf.buf;
  79        }
  80
  81        if (ignore_case)
  82                flags |= WM_CASEFOLD;
  83        match_status = wildmatch(use_pat, use_str, flags, NULL);
  84
  85        strbuf_release(&pat_buf);
  86        strbuf_release(&str_buf);
  87
  88        return match_status;
  89}
  90
  91static size_t common_prefix_len(const char **pathspec)
  92{
  93        const char *n, *first;
  94        size_t max = 0;
  95        int literal = limit_pathspec_to_literal();
  96
  97        if (!pathspec)
  98                return max;
  99
 100        first = *pathspec;
 101        while ((n = *pathspec++)) {
 102                size_t i, len = 0;
 103                for (i = 0; first == n || i < max; i++) {
 104                        char c = n[i];
 105                        if (!c || c != first[i] || (!literal && is_glob_special(c)))
 106                                break;
 107                        if (c == '/')
 108                                len = i + 1;
 109                }
 110                if (first == n || len < max) {
 111                        max = len;
 112                        if (!max)
 113                                break;
 114                }
 115        }
 116        return max;
 117}
 118
 119/*
 120 * Returns a copy of the longest leading path common among all
 121 * pathspecs.
 122 */
 123char *common_prefix(const char **pathspec)
 124{
 125        unsigned long len = common_prefix_len(pathspec);
 126
 127        return len ? xmemdupz(*pathspec, len) : NULL;
 128}
 129
 130int fill_directory(struct dir_struct *dir, const char **pathspec)
 131{
 132        size_t len;
 133
 134        /*
 135         * Calculate common prefix for the pathspec, and
 136         * use that to optimize the directory walk
 137         */
 138        len = common_prefix_len(pathspec);
 139
 140        /* Read the directory and prune it */
 141        read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
 142        return len;
 143}
 144
 145int within_depth(const char *name, int namelen,
 146                        int depth, int max_depth)
 147{
 148        const char *cp = name, *cpe = name + namelen;
 149
 150        while (cp < cpe) {
 151                if (*cp++ != '/')
 152                        continue;
 153                depth++;
 154                if (depth > max_depth)
 155                        return 0;
 156        }
 157        return 1;
 158}
 159
 160/*
 161 * Does 'match' match the given name?
 162 * A match is found if
 163 *
 164 * (1) the 'match' string is leading directory of 'name', or
 165 * (2) the 'match' string is a wildcard and matches 'name', or
 166 * (3) the 'match' string is exactly the same as 'name'.
 167 *
 168 * and the return value tells which case it was.
 169 *
 170 * It returns 0 when there is no match.
 171 */
 172static int match_one(const char *match, const char *name, int namelen)
 173{
 174        int matchlen;
 175        int literal = limit_pathspec_to_literal();
 176
 177        /* If the match was just the prefix, we matched */
 178        if (!*match)
 179                return MATCHED_RECURSIVELY;
 180
 181        if (ignore_case) {
 182                for (;;) {
 183                        unsigned char c1 = tolower(*match);
 184                        unsigned char c2 = tolower(*name);
 185                        if (c1 == '\0' || (!literal && is_glob_special(c1)))
 186                                break;
 187                        if (c1 != c2)
 188                                return 0;
 189                        match++;
 190                        name++;
 191                        namelen--;
 192                }
 193        } else {
 194                for (;;) {
 195                        unsigned char c1 = *match;
 196                        unsigned char c2 = *name;
 197                        if (c1 == '\0' || (!literal && is_glob_special(c1)))
 198                                break;
 199                        if (c1 != c2)
 200                                return 0;
 201                        match++;
 202                        name++;
 203                        namelen--;
 204                }
 205        }
 206
 207        /*
 208         * If we don't match the matchstring exactly,
 209         * we need to match by fnmatch
 210         */
 211        matchlen = strlen(match);
 212        if (strncmp_icase(match, name, matchlen)) {
 213                if (literal)
 214                        return 0;
 215                return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
 216        }
 217
 218        if (namelen == matchlen)
 219                return MATCHED_EXACTLY;
 220        if (match[matchlen-1] == '/' || name[matchlen] == '/')
 221                return MATCHED_RECURSIVELY;
 222        return 0;
 223}
 224
 225/*
 226 * Given a name and a list of pathspecs, returns the nature of the
 227 * closest (i.e. most specific) match of the name to any of the
 228 * pathspecs.
 229 *
 230 * The caller typically calls this multiple times with the same
 231 * pathspec and seen[] array but with different name/namelen
 232 * (e.g. entries from the index) and is interested in seeing if and
 233 * how each pathspec matches all the names it calls this function
 234 * with.  A mark is left in the seen[] array for each pathspec element
 235 * indicating the closest type of match that element achieved, so if
 236 * seen[n] remains zero after multiple invocations, that means the nth
 237 * pathspec did not match any names, which could indicate that the
 238 * user mistyped the nth pathspec.
 239 */
 240int match_pathspec(const char **pathspec, const char *name, int namelen,
 241                int prefix, char *seen)
 242{
 243        int i, retval = 0;
 244
 245        if (!pathspec)
 246                return 1;
 247
 248        name += prefix;
 249        namelen -= prefix;
 250
 251        for (i = 0; pathspec[i] != NULL; i++) {
 252                int how;
 253                const char *match = pathspec[i] + prefix;
 254                if (seen && seen[i] == MATCHED_EXACTLY)
 255                        continue;
 256                how = match_one(match, name, namelen);
 257                if (how) {
 258                        if (retval < how)
 259                                retval = how;
 260                        if (seen && seen[i] < how)
 261                                seen[i] = how;
 262                }
 263        }
 264        return retval;
 265}
 266
 267/*
 268 * Does 'match' match the given name?
 269 * A match is found if
 270 *
 271 * (1) the 'match' string is leading directory of 'name', or
 272 * (2) the 'match' string is a wildcard and matches 'name', or
 273 * (3) the 'match' string is exactly the same as 'name'.
 274 *
 275 * and the return value tells which case it was.
 276 *
 277 * It returns 0 when there is no match.
 278 */
 279static int match_pathspec_item(const struct pathspec_item *item, int prefix,
 280                               const char *name, int namelen)
 281{
 282        /* name/namelen has prefix cut off by caller */
 283        const char *match = item->match + prefix;
 284        int matchlen = item->len - prefix;
 285
 286        /* If the match was just the prefix, we matched */
 287        if (!*match)
 288                return MATCHED_RECURSIVELY;
 289
 290        if (matchlen <= namelen && !strncmp(match, name, matchlen)) {
 291                if (matchlen == namelen)
 292                        return MATCHED_EXACTLY;
 293
 294                if (match[matchlen-1] == '/' || name[matchlen] == '/')
 295                        return MATCHED_RECURSIVELY;
 296        }
 297
 298        if (item->nowildcard_len < item->len &&
 299            !git_fnmatch(match, name,
 300                         item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,
 301                         item->nowildcard_len - prefix))
 302                return MATCHED_FNMATCH;
 303
 304        return 0;
 305}
 306
 307/*
 308 * Given a name and a list of pathspecs, returns the nature of the
 309 * closest (i.e. most specific) match of the name to any of the
 310 * pathspecs.
 311 *
 312 * The caller typically calls this multiple times with the same
 313 * pathspec and seen[] array but with different name/namelen
 314 * (e.g. entries from the index) and is interested in seeing if and
 315 * how each pathspec matches all the names it calls this function
 316 * with.  A mark is left in the seen[] array for each pathspec element
 317 * indicating the closest type of match that element achieved, so if
 318 * seen[n] remains zero after multiple invocations, that means the nth
 319 * pathspec did not match any names, which could indicate that the
 320 * user mistyped the nth pathspec.
 321 */
 322int match_pathspec_depth(const struct pathspec *ps,
 323                         const char *name, int namelen,
 324                         int prefix, char *seen)
 325{
 326        int i, retval = 0;
 327
 328        if (!ps->nr) {
 329                if (!ps->recursive || ps->max_depth == -1)
 330                        return MATCHED_RECURSIVELY;
 331
 332                if (within_depth(name, namelen, 0, ps->max_depth))
 333                        return MATCHED_EXACTLY;
 334                else
 335                        return 0;
 336        }
 337
 338        name += prefix;
 339        namelen -= prefix;
 340
 341        for (i = ps->nr - 1; i >= 0; i--) {
 342                int how;
 343                if (seen && seen[i] == MATCHED_EXACTLY)
 344                        continue;
 345                how = match_pathspec_item(ps->items+i, prefix, name, namelen);
 346                if (ps->recursive && ps->max_depth != -1 &&
 347                    how && how != MATCHED_FNMATCH) {
 348                        int len = ps->items[i].len;
 349                        if (name[len] == '/')
 350                                len++;
 351                        if (within_depth(name+len, namelen-len, 0, ps->max_depth))
 352                                how = MATCHED_EXACTLY;
 353                        else
 354                                how = 0;
 355                }
 356                if (how) {
 357                        if (retval < how)
 358                                retval = how;
 359                        if (seen && seen[i] < how)
 360                                seen[i] = how;
 361                }
 362        }
 363        return retval;
 364}
 365
 366/*
 367 * Return the length of the "simple" part of a path match limiter.
 368 */
 369static int simple_length(const char *match)
 370{
 371        int len = -1;
 372
 373        for (;;) {
 374                unsigned char c = *match++;
 375                len++;
 376                if (c == '\0' || is_glob_special(c))
 377                        return len;
 378        }
 379}
 380
 381static int no_wildcard(const char *string)
 382{
 383        return string[simple_length(string)] == '\0';
 384}
 385
 386void parse_exclude_pattern(const char **pattern,
 387                           int *patternlen,
 388                           int *flags,
 389                           int *nowildcardlen)
 390{
 391        const char *p = *pattern;
 392        size_t i, len;
 393
 394        *flags = 0;
 395        if (*p == '!') {
 396                *flags |= EXC_FLAG_NEGATIVE;
 397                p++;
 398        }
 399        len = strlen(p);
 400        if (len && p[len - 1] == '/') {
 401                len--;
 402                *flags |= EXC_FLAG_MUSTBEDIR;
 403        }
 404        for (i = 0; i < len; i++) {
 405                if (p[i] == '/')
 406                        break;
 407        }
 408        if (i == len)
 409                *flags |= EXC_FLAG_NODIR;
 410        *nowildcardlen = simple_length(p);
 411        /*
 412         * we should have excluded the trailing slash from 'p' too,
 413         * but that's one more allocation. Instead just make sure
 414         * nowildcardlen does not exceed real patternlen
 415         */
 416        if (*nowildcardlen > len)
 417                *nowildcardlen = len;
 418        if (*p == '*' && no_wildcard(p + 1))
 419                *flags |= EXC_FLAG_ENDSWITH;
 420        *pattern = p;
 421        *patternlen = len;
 422}
 423
 424void add_exclude(const char *string, const char *base,
 425                 int baselen, struct exclude_list *el, int srcpos)
 426{
 427        struct exclude *x;
 428        int patternlen;
 429        int flags;
 430        int nowildcardlen;
 431
 432        parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
 433        if (flags & EXC_FLAG_MUSTBEDIR) {
 434                char *s;
 435                x = xmalloc(sizeof(*x) + patternlen + 1);
 436                s = (char *)(x+1);
 437                memcpy(s, string, patternlen);
 438                s[patternlen] = '\0';
 439                x->pattern = s;
 440        } else {
 441                x = xmalloc(sizeof(*x));
 442                x->pattern = string;
 443        }
 444        x->patternlen = patternlen;
 445        x->nowildcardlen = nowildcardlen;
 446        x->base = base;
 447        x->baselen = baselen;
 448        x->flags = flags;
 449        x->srcpos = srcpos;
 450        ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
 451        el->excludes[el->nr++] = x;
 452        x->el = el;
 453}
 454
 455static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
 456{
 457        int pos, len;
 458        unsigned long sz;
 459        enum object_type type;
 460        void *data;
 461        struct index_state *istate = &the_index;
 462
 463        len = strlen(path);
 464        pos = index_name_pos(istate, path, len);
 465        if (pos < 0)
 466                return NULL;
 467        if (!ce_skip_worktree(istate->cache[pos]))
 468                return NULL;
 469        data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
 470        if (!data || type != OBJ_BLOB) {
 471                free(data);
 472                return NULL;
 473        }
 474        *size = xsize_t(sz);
 475        return data;
 476}
 477
 478/*
 479 * Frees memory within el which was allocated for exclude patterns and
 480 * the file buffer.  Does not free el itself.
 481 */
 482void clear_exclude_list(struct exclude_list *el)
 483{
 484        int i;
 485
 486        for (i = 0; i < el->nr; i++)
 487                free(el->excludes[i]);
 488        free(el->excludes);
 489        free(el->filebuf);
 490
 491        el->nr = 0;
 492        el->excludes = NULL;
 493        el->filebuf = NULL;
 494}
 495
 496int add_excludes_from_file_to_list(const char *fname,
 497                                   const char *base,
 498                                   int baselen,
 499                                   struct exclude_list *el,
 500                                   int check_index)
 501{
 502        struct stat st;
 503        int fd, i, lineno = 1;
 504        size_t size = 0;
 505        char *buf, *entry;
 506
 507        fd = open(fname, O_RDONLY);
 508        if (fd < 0 || fstat(fd, &st) < 0) {
 509                if (errno != ENOENT)
 510                        warn_on_inaccessible(fname);
 511                if (0 <= fd)
 512                        close(fd);
 513                if (!check_index ||
 514                    (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
 515                        return -1;
 516                if (size == 0) {
 517                        free(buf);
 518                        return 0;
 519                }
 520                if (buf[size-1] != '\n') {
 521                        buf = xrealloc(buf, size+1);
 522                        buf[size++] = '\n';
 523                }
 524        }
 525        else {
 526                size = xsize_t(st.st_size);
 527                if (size == 0) {
 528                        close(fd);
 529                        return 0;
 530                }
 531                buf = xmalloc(size+1);
 532                if (read_in_full(fd, buf, size) != size) {
 533                        free(buf);
 534                        close(fd);
 535                        return -1;
 536                }
 537                buf[size++] = '\n';
 538                close(fd);
 539        }
 540
 541        el->filebuf = buf;
 542        entry = buf;
 543        for (i = 0; i < size; i++) {
 544                if (buf[i] == '\n') {
 545                        if (entry != buf + i && entry[0] != '#') {
 546                                buf[i - (i && buf[i-1] == '\r')] = 0;
 547                                add_exclude(entry, base, baselen, el, lineno);
 548                        }
 549                        lineno++;
 550                        entry = buf + i + 1;
 551                }
 552        }
 553        return 0;
 554}
 555
 556struct exclude_list *add_exclude_list(struct dir_struct *dir,
 557                                      int group_type, const char *src)
 558{
 559        struct exclude_list *el;
 560        struct exclude_list_group *group;
 561
 562        group = &dir->exclude_list_group[group_type];
 563        ALLOC_GROW(group->el, group->nr + 1, group->alloc);
 564        el = &group->el[group->nr++];
 565        memset(el, 0, sizeof(*el));
 566        el->src = src;
 567        return el;
 568}
 569
 570/*
 571 * Used to set up core.excludesfile and .git/info/exclude lists.
 572 */
 573void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 574{
 575        struct exclude_list *el;
 576        el = add_exclude_list(dir, EXC_FILE, fname);
 577        if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0)
 578                die("cannot use %s as an exclude file", fname);
 579}
 580
 581/*
 582 * Loads the per-directory exclude list for the substring of base
 583 * which has a char length of baselen.
 584 */
 585static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 586{
 587        struct exclude_list_group *group;
 588        struct exclude_list *el;
 589        struct exclude_stack *stk = NULL;
 590        int current;
 591
 592        if ((!dir->exclude_per_dir) ||
 593            (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
 594                return; /* too long a path -- ignore */
 595
 596        group = &dir->exclude_list_group[EXC_DIRS];
 597
 598        /* Pop the exclude lists from the EXCL_DIRS exclude_list_group
 599         * which originate from directories not in the prefix of the
 600         * path being checked. */
 601        while ((stk = dir->exclude_stack) != NULL) {
 602                if (stk->baselen <= baselen &&
 603                    !strncmp(dir->basebuf, base, stk->baselen))
 604                        break;
 605                el = &group->el[dir->exclude_stack->exclude_ix];
 606                dir->exclude_stack = stk->prev;
 607                free((char *)el->src); /* see strdup() below */
 608                clear_exclude_list(el);
 609                free(stk);
 610                group->nr--;
 611        }
 612
 613        /* Read from the parent directories and push them down. */
 614        current = stk ? stk->baselen : -1;
 615        while (current < baselen) {
 616                struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
 617                const char *cp;
 618
 619                if (current < 0) {
 620                        cp = base;
 621                        current = 0;
 622                }
 623                else {
 624                        cp = strchr(base + current + 1, '/');
 625                        if (!cp)
 626                                die("oops in prep_exclude");
 627                        cp++;
 628                }
 629                stk->prev = dir->exclude_stack;
 630                stk->baselen = cp - base;
 631                memcpy(dir->basebuf + current, base + current,
 632                       stk->baselen - current);
 633                strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
 634                /*
 635                 * dir->basebuf gets reused by the traversal, but we
 636                 * need fname to remain unchanged to ensure the src
 637                 * member of each struct exclude correctly
 638                 * back-references its source file.  Other invocations
 639                 * of add_exclude_list provide stable strings, so we
 640                 * strdup() and free() here in the caller.
 641                 */
 642                el = add_exclude_list(dir, EXC_DIRS, strdup(dir->basebuf));
 643                stk->exclude_ix = group->nr - 1;
 644                add_excludes_from_file_to_list(dir->basebuf,
 645                                               dir->basebuf, stk->baselen,
 646                                               el, 1);
 647                dir->exclude_stack = stk;
 648                current = stk->baselen;
 649        }
 650        dir->basebuf[baselen] = '\0';
 651}
 652
 653int match_basename(const char *basename, int basenamelen,
 654                   const char *pattern, int prefix, int patternlen,
 655                   int flags)
 656{
 657        if (prefix == patternlen) {
 658                if (patternlen == basenamelen &&
 659                    !strncmp_icase(pattern, basename, basenamelen))
 660                        return 1;
 661        } else if (flags & EXC_FLAG_ENDSWITH) {
 662                /* "*literal" matching against "fooliteral" */
 663                if (patternlen - 1 <= basenamelen &&
 664                    !strncmp_icase(pattern + 1,
 665                                   basename + basenamelen - (patternlen - 1),
 666                                   patternlen - 1))
 667                        return 1;
 668        } else {
 669                if (fnmatch_icase_mem(pattern, patternlen,
 670                                      basename, basenamelen,
 671                                      0) == 0)
 672                        return 1;
 673        }
 674        return 0;
 675}
 676
 677int match_pathname(const char *pathname, int pathlen,
 678                   const char *base, int baselen,
 679                   const char *pattern, int prefix, int patternlen,
 680                   int flags)
 681{
 682        const char *name;
 683        int namelen;
 684
 685        /*
 686         * match with FNM_PATHNAME; the pattern has base implicitly
 687         * in front of it.
 688         */
 689        if (*pattern == '/') {
 690                pattern++;
 691                patternlen--;
 692                prefix--;
 693        }
 694
 695        /*
 696         * baselen does not count the trailing slash. base[] may or
 697         * may not end with a trailing slash though.
 698         */
 699        if (pathlen < baselen + 1 ||
 700            (baselen && pathname[baselen] != '/') ||
 701            strncmp_icase(pathname, base, baselen))
 702                return 0;
 703
 704        namelen = baselen ? pathlen - baselen - 1 : pathlen;
 705        name = pathname + pathlen - namelen;
 706
 707        if (prefix) {
 708                /*
 709                 * if the non-wildcard part is longer than the
 710                 * remaining pathname, surely it cannot match.
 711                 */
 712                if (prefix > namelen)
 713                        return 0;
 714
 715                if (strncmp_icase(pattern, name, prefix))
 716                        return 0;
 717                pattern += prefix;
 718                patternlen -= prefix;
 719                name    += prefix;
 720                namelen -= prefix;
 721
 722                /*
 723                 * If the whole pattern did not have a wildcard,
 724                 * then our prefix match is all we need; we
 725                 * do not need to call fnmatch at all.
 726                 */
 727                if (!patternlen && !namelen)
 728                        return 1;
 729        }
 730
 731        return fnmatch_icase_mem(pattern, patternlen,
 732                                 name, namelen,
 733                                 WM_PATHNAME) == 0;
 734}
 735
 736/*
 737 * Scan the given exclude list in reverse to see whether pathname
 738 * should be ignored.  The first match (i.e. the last on the list), if
 739 * any, determines the fate.  Returns the exclude_list element which
 740 * matched, or NULL for undecided.
 741 */
 742static struct exclude *last_exclude_matching_from_list(const char *pathname,
 743                                                       int pathlen,
 744                                                       const char *basename,
 745                                                       int *dtype,
 746                                                       struct exclude_list *el)
 747{
 748        int i;
 749
 750        if (!el->nr)
 751                return NULL;    /* undefined */
 752
 753        for (i = el->nr - 1; 0 <= i; i--) {
 754                struct exclude *x = el->excludes[i];
 755                const char *exclude = x->pattern;
 756                int prefix = x->nowildcardlen;
 757
 758                if (x->flags & EXC_FLAG_MUSTBEDIR) {
 759                        if (*dtype == DT_UNKNOWN)
 760                                *dtype = get_dtype(NULL, pathname, pathlen);
 761                        if (*dtype != DT_DIR)
 762                                continue;
 763                }
 764
 765                if (x->flags & EXC_FLAG_NODIR) {
 766                        if (match_basename(basename,
 767                                           pathlen - (basename - pathname),
 768                                           exclude, prefix, x->patternlen,
 769                                           x->flags))
 770                                return x;
 771                        continue;
 772                }
 773
 774                assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
 775                if (match_pathname(pathname, pathlen,
 776                                   x->base, x->baselen ? x->baselen - 1 : 0,
 777                                   exclude, prefix, x->patternlen, x->flags))
 778                        return x;
 779        }
 780        return NULL; /* undecided */
 781}
 782
 783/*
 784 * Scan the list and let the last match determine the fate.
 785 * Return 1 for exclude, 0 for include and -1 for undecided.
 786 */
 787int is_excluded_from_list(const char *pathname,
 788                          int pathlen, const char *basename, int *dtype,
 789                          struct exclude_list *el)
 790{
 791        struct exclude *exclude;
 792        exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
 793        if (exclude)
 794                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
 795        return -1; /* undecided */
 796}
 797
 798/*
 799 * Loads the exclude lists for the directory containing pathname, then
 800 * scans all exclude lists to determine whether pathname is excluded.
 801 * Returns the exclude_list element which matched, or NULL for
 802 * undecided.
 803 */
 804static struct exclude *last_exclude_matching(struct dir_struct *dir,
 805                                             const char *pathname,
 806                                             int *dtype_p)
 807{
 808        int pathlen = strlen(pathname);
 809        int i, j;
 810        struct exclude_list_group *group;
 811        struct exclude *exclude;
 812        const char *basename = strrchr(pathname, '/');
 813        basename = (basename) ? basename+1 : pathname;
 814
 815        prep_exclude(dir, pathname, basename-pathname);
 816
 817        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
 818                group = &dir->exclude_list_group[i];
 819                for (j = group->nr - 1; j >= 0; j--) {
 820                        exclude = last_exclude_matching_from_list(
 821                                pathname, pathlen, basename, dtype_p,
 822                                &group->el[j]);
 823                        if (exclude)
 824                                return exclude;
 825                }
 826        }
 827        return NULL;
 828}
 829
 830/*
 831 * Loads the exclude lists for the directory containing pathname, then
 832 * scans all exclude lists to determine whether pathname is excluded.
 833 * Returns 1 if true, otherwise 0.
 834 */
 835static int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
 836{
 837        struct exclude *exclude =
 838                last_exclude_matching(dir, pathname, dtype_p);
 839        if (exclude)
 840                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
 841        return 0;
 842}
 843
 844void path_exclude_check_init(struct path_exclude_check *check,
 845                             struct dir_struct *dir)
 846{
 847        check->dir = dir;
 848        check->exclude = NULL;
 849        strbuf_init(&check->path, 256);
 850}
 851
 852void path_exclude_check_clear(struct path_exclude_check *check)
 853{
 854        strbuf_release(&check->path);
 855}
 856
 857/*
 858 * For each subdirectory in name, starting with the top-most, checks
 859 * to see if that subdirectory is excluded, and if so, returns the
 860 * corresponding exclude structure.  Otherwise, checks whether name
 861 * itself (which is presumably a file) is excluded.
 862 *
 863 * A path to a directory known to be excluded is left in check->path to
 864 * optimize for repeated checks for files in the same excluded directory.
 865 */
 866struct exclude *last_exclude_matching_path(struct path_exclude_check *check,
 867                                           const char *name, int namelen,
 868                                           int *dtype)
 869{
 870        int i;
 871        struct strbuf *path = &check->path;
 872        struct exclude *exclude;
 873
 874        /*
 875         * we allow the caller to pass namelen as an optimization; it
 876         * must match the length of the name, as we eventually call
 877         * is_excluded() on the whole name string.
 878         */
 879        if (namelen < 0)
 880                namelen = strlen(name);
 881
 882        /*
 883         * If path is non-empty, and name is equal to path or a
 884         * subdirectory of path, name should be excluded, because
 885         * it's inside a directory which is already known to be
 886         * excluded and was previously left in check->path.
 887         */
 888        if (path->len &&
 889            path->len <= namelen &&
 890            !memcmp(name, path->buf, path->len) &&
 891            (!name[path->len] || name[path->len] == '/'))
 892                return check->exclude;
 893
 894        strbuf_setlen(path, 0);
 895        for (i = 0; name[i]; i++) {
 896                int ch = name[i];
 897
 898                if (ch == '/') {
 899                        int dt = DT_DIR;
 900                        exclude = last_exclude_matching(check->dir,
 901                                                        path->buf, &dt);
 902                        if (exclude) {
 903                                check->exclude = exclude;
 904                                return exclude;
 905                        }
 906                }
 907                strbuf_addch(path, ch);
 908        }
 909
 910        /* An entry in the index; cannot be a directory with subentries */
 911        strbuf_setlen(path, 0);
 912
 913        return last_exclude_matching(check->dir, name, dtype);
 914}
 915
 916/*
 917 * Is this name excluded?  This is for a caller like show_files() that
 918 * do not honor directory hierarchy and iterate through paths that are
 919 * possibly in an ignored directory.
 920 */
 921int is_path_excluded(struct path_exclude_check *check,
 922                  const char *name, int namelen, int *dtype)
 923{
 924        struct exclude *exclude =
 925                last_exclude_matching_path(check, name, namelen, dtype);
 926        if (exclude)
 927                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
 928        return 0;
 929}
 930
 931static struct dir_entry *dir_entry_new(const char *pathname, int len)
 932{
 933        struct dir_entry *ent;
 934
 935        ent = xmalloc(sizeof(*ent) + len + 1);
 936        ent->len = len;
 937        memcpy(ent->name, pathname, len);
 938        ent->name[len] = 0;
 939        return ent;
 940}
 941
 942static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
 943{
 944        if (!(dir->flags & DIR_SHOW_IGNORED) &&
 945            cache_name_exists(pathname, len, ignore_case))
 946                return NULL;
 947
 948        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
 949        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
 950}
 951
 952struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
 953{
 954        if (!cache_name_is_other(pathname, len))
 955                return NULL;
 956
 957        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
 958        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
 959}
 960
 961enum exist_status {
 962        index_nonexistent = 0,
 963        index_directory,
 964        index_gitdir
 965};
 966
 967/*
 968 * Do not use the alphabetically stored index to look up
 969 * the directory name; instead, use the case insensitive
 970 * name hash.
 971 */
 972static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
 973{
 974        struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
 975        unsigned char endchar;
 976
 977        if (!ce)
 978                return index_nonexistent;
 979        endchar = ce->name[len];
 980
 981        /*
 982         * The cache_entry structure returned will contain this dirname
 983         * and possibly additional path components.
 984         */
 985        if (endchar == '/')
 986                return index_directory;
 987
 988        /*
 989         * If there are no additional path components, then this cache_entry
 990         * represents a submodule.  Submodules, despite being directories,
 991         * are stored in the cache without a closing slash.
 992         */
 993        if (!endchar && S_ISGITLINK(ce->ce_mode))
 994                return index_gitdir;
 995
 996        /* This should never be hit, but it exists just in case. */
 997        return index_nonexistent;
 998}
 999
1000/*
1001 * The index sorts alphabetically by entry name, which
1002 * means that a gitlink sorts as '\0' at the end, while
1003 * a directory (which is defined not as an entry, but as
1004 * the files it contains) will sort with the '/' at the
1005 * end.
1006 */
1007static enum exist_status directory_exists_in_index(const char *dirname, int len)
1008{
1009        int pos;
1010
1011        if (ignore_case)
1012                return directory_exists_in_index_icase(dirname, len);
1013
1014        pos = cache_name_pos(dirname, len);
1015        if (pos < 0)
1016                pos = -pos-1;
1017        while (pos < active_nr) {
1018                struct cache_entry *ce = active_cache[pos++];
1019                unsigned char endchar;
1020
1021                if (strncmp(ce->name, dirname, len))
1022                        break;
1023                endchar = ce->name[len];
1024                if (endchar > '/')
1025                        break;
1026                if (endchar == '/')
1027                        return index_directory;
1028                if (!endchar && S_ISGITLINK(ce->ce_mode))
1029                        return index_gitdir;
1030        }
1031        return index_nonexistent;
1032}
1033
1034/*
1035 * When we find a directory when traversing the filesystem, we
1036 * have three distinct cases:
1037 *
1038 *  - ignore it
1039 *  - see it as a directory
1040 *  - recurse into it
1041 *
1042 * and which one we choose depends on a combination of existing
1043 * git index contents and the flags passed into the directory
1044 * traversal routine.
1045 *
1046 * Case 1: If we *already* have entries in the index under that
1047 * directory name, we recurse into the directory to see all the files,
1048 * unless the directory is excluded and we want to show ignored
1049 * directories
1050 *
1051 * Case 2: If we *already* have that directory name as a gitlink,
1052 * we always continue to see it as a gitlink, regardless of whether
1053 * there is an actual git directory there or not (it might not
1054 * be checked out as a subproject!)
1055 *
1056 * Case 3: if we didn't have it in the index previously, we
1057 * have a few sub-cases:
1058 *
1059 *  (a) if "show_other_directories" is true, we show it as
1060 *      just a directory, unless "hide_empty_directories" is
1061 *      also true and the directory is empty, in which case
1062 *      we just ignore it entirely.
1063 *      if we are looking for ignored directories, look if it
1064 *      contains only ignored files to decide if it must be shown as
1065 *      ignored or not.
1066 *  (b) if it looks like a git directory, and we don't have
1067 *      'no_gitlinks' set we treat it as a gitlink, and show it
1068 *      as a directory.
1069 *  (c) otherwise, we recurse into it.
1070 */
1071enum directory_treatment {
1072        show_directory,
1073        ignore_directory,
1074        recurse_into_directory
1075};
1076
1077static enum directory_treatment treat_directory(struct dir_struct *dir,
1078        const char *dirname, int len, int exclude,
1079        const struct path_simplify *simplify)
1080{
1081        /* The "len-1" is to strip the final '/' */
1082        switch (directory_exists_in_index(dirname, len-1)) {
1083        case index_directory:
1084                if ((dir->flags & DIR_SHOW_OTHER_DIRECTORIES) && exclude)
1085                        break;
1086
1087                return recurse_into_directory;
1088
1089        case index_gitdir:
1090                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1091                        return ignore_directory;
1092                return show_directory;
1093
1094        case index_nonexistent:
1095                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1096                        break;
1097                if (!(dir->flags & DIR_NO_GITLINKS)) {
1098                        unsigned char sha1[20];
1099                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
1100                                return show_directory;
1101                }
1102                return recurse_into_directory;
1103        }
1104
1105        /* This is the "show_other_directories" case */
1106
1107        /* might be a sub directory in an excluded directory */
1108        if (!exclude) {
1109                struct path_exclude_check check;
1110                int dt = DT_DIR;
1111                path_exclude_check_init(&check, dir);
1112                exclude = is_path_excluded(&check, dirname, len, &dt);
1113                path_exclude_check_clear(&check);
1114        }
1115
1116        /*
1117         * We are looking for ignored files and our directory is not ignored,
1118         * check if it contains only ignored files
1119         */
1120        if ((dir->flags & DIR_SHOW_IGNORED) && !exclude) {
1121                int ignored;
1122                dir->flags &= ~DIR_SHOW_IGNORED;
1123                dir->flags |= DIR_HIDE_EMPTY_DIRECTORIES;
1124                ignored = read_directory_recursive(dir, dirname, len, 1, simplify);
1125                dir->flags &= ~DIR_HIDE_EMPTY_DIRECTORIES;
1126                dir->flags |= DIR_SHOW_IGNORED;
1127
1128                return ignored ? ignore_directory : show_directory;
1129        }
1130        if (!(dir->flags & DIR_SHOW_IGNORED) &&
1131            !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1132                return show_directory;
1133        if (!read_directory_recursive(dir, dirname, len, 1, simplify))
1134                return ignore_directory;
1135        return show_directory;
1136}
1137
1138/*
1139 * Decide what to do when we find a file while traversing the
1140 * filesystem. Mostly two cases:
1141 *
1142 *  1. We are looking for ignored files
1143 *   (a) File is ignored, include it
1144 *   (b) File is in ignored path, include it
1145 *   (c) File is not ignored, exclude it
1146 *
1147 *  2. Other scenarios, include the file if not excluded
1148 *
1149 * Return 1 for exclude, 0 for include.
1150 */
1151static int treat_file(struct dir_struct *dir, struct strbuf *path, int exclude, int *dtype)
1152{
1153        struct path_exclude_check check;
1154        int exclude_file = 0;
1155
1156        /* Always exclude indexed files */
1157        if (index_name_exists(&the_index, path->buf, path->len, ignore_case))
1158                return 1;
1159
1160        if (exclude)
1161                exclude_file = !(dir->flags & DIR_SHOW_IGNORED);
1162        else if (dir->flags & DIR_SHOW_IGNORED) {
1163                path_exclude_check_init(&check, dir);
1164
1165                if (!is_path_excluded(&check, path->buf, path->len, dtype))
1166                        exclude_file = 1;
1167
1168                path_exclude_check_clear(&check);
1169        }
1170
1171        return exclude_file;
1172}
1173
1174/*
1175 * This is an inexact early pruning of any recursive directory
1176 * reading - if the path cannot possibly be in the pathspec,
1177 * return true, and we'll skip it early.
1178 */
1179static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
1180{
1181        if (simplify) {
1182                for (;;) {
1183                        const char *match = simplify->path;
1184                        int len = simplify->len;
1185
1186                        if (!match)
1187                                break;
1188                        if (len > pathlen)
1189                                len = pathlen;
1190                        if (!memcmp(path, match, len))
1191                                return 0;
1192                        simplify++;
1193                }
1194                return 1;
1195        }
1196        return 0;
1197}
1198
1199/*
1200 * This function tells us whether an excluded path matches a
1201 * list of "interesting" pathspecs. That is, whether a path matched
1202 * by any of the pathspecs could possibly be ignored by excluding
1203 * the specified path. This can happen if:
1204 *
1205 *   1. the path is mentioned explicitly in the pathspec
1206 *
1207 *   2. the path is a directory prefix of some element in the
1208 *      pathspec
1209 */
1210static int exclude_matches_pathspec(const char *path, int len,
1211                const struct path_simplify *simplify)
1212{
1213        if (simplify) {
1214                for (; simplify->path; simplify++) {
1215                        if (len == simplify->len
1216                            && !memcmp(path, simplify->path, len))
1217                                return 1;
1218                        if (len < simplify->len
1219                            && simplify->path[len] == '/'
1220                            && !memcmp(path, simplify->path, len))
1221                                return 1;
1222                }
1223        }
1224        return 0;
1225}
1226
1227static int get_index_dtype(const char *path, int len)
1228{
1229        int pos;
1230        struct cache_entry *ce;
1231
1232        ce = cache_name_exists(path, len, 0);
1233        if (ce) {
1234                if (!ce_uptodate(ce))
1235                        return DT_UNKNOWN;
1236                if (S_ISGITLINK(ce->ce_mode))
1237                        return DT_DIR;
1238                /*
1239                 * Nobody actually cares about the
1240                 * difference between DT_LNK and DT_REG
1241                 */
1242                return DT_REG;
1243        }
1244
1245        /* Try to look it up as a directory */
1246        pos = cache_name_pos(path, len);
1247        if (pos >= 0)
1248                return DT_UNKNOWN;
1249        pos = -pos-1;
1250        while (pos < active_nr) {
1251                ce = active_cache[pos++];
1252                if (strncmp(ce->name, path, len))
1253                        break;
1254                if (ce->name[len] > '/')
1255                        break;
1256                if (ce->name[len] < '/')
1257                        continue;
1258                if (!ce_uptodate(ce))
1259                        break;  /* continue? */
1260                return DT_DIR;
1261        }
1262        return DT_UNKNOWN;
1263}
1264
1265static int get_dtype(struct dirent *de, const char *path, int len)
1266{
1267        int dtype = de ? DTYPE(de) : DT_UNKNOWN;
1268        struct stat st;
1269
1270        if (dtype != DT_UNKNOWN)
1271                return dtype;
1272        dtype = get_index_dtype(path, len);
1273        if (dtype != DT_UNKNOWN)
1274                return dtype;
1275        if (lstat(path, &st))
1276                return dtype;
1277        if (S_ISREG(st.st_mode))
1278                return DT_REG;
1279        if (S_ISDIR(st.st_mode))
1280                return DT_DIR;
1281        if (S_ISLNK(st.st_mode))
1282                return DT_LNK;
1283        return dtype;
1284}
1285
1286enum path_treatment {
1287        path_ignored,
1288        path_handled,
1289        path_recurse
1290};
1291
1292static enum path_treatment treat_one_path(struct dir_struct *dir,
1293                                          struct strbuf *path,
1294                                          const struct path_simplify *simplify,
1295                                          int dtype, struct dirent *de)
1296{
1297        int exclude = is_excluded(dir, path->buf, &dtype);
1298        if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
1299            && exclude_matches_pathspec(path->buf, path->len, simplify))
1300                dir_add_ignored(dir, path->buf, path->len);
1301
1302        /*
1303         * Excluded? If we don't explicitly want to show
1304         * ignored files, ignore it
1305         */
1306        if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
1307                return path_ignored;
1308
1309        if (dtype == DT_UNKNOWN)
1310                dtype = get_dtype(de, path->buf, path->len);
1311
1312        switch (dtype) {
1313        default:
1314                return path_ignored;
1315        case DT_DIR:
1316                strbuf_addch(path, '/');
1317                switch (treat_directory(dir, path->buf, path->len, exclude, simplify)) {
1318                case show_directory:
1319                        break;
1320                case recurse_into_directory:
1321                        return path_recurse;
1322                case ignore_directory:
1323                        return path_ignored;
1324                }
1325                break;
1326        case DT_REG:
1327        case DT_LNK:
1328                switch (treat_file(dir, path, exclude, &dtype)) {
1329                case 1:
1330                        return path_ignored;
1331                default:
1332                        break;
1333                }
1334        }
1335        return path_handled;
1336}
1337
1338static enum path_treatment treat_path(struct dir_struct *dir,
1339                                      struct dirent *de,
1340                                      struct strbuf *path,
1341                                      int baselen,
1342                                      const struct path_simplify *simplify)
1343{
1344        int dtype;
1345
1346        if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1347                return path_ignored;
1348        strbuf_setlen(path, baselen);
1349        strbuf_addstr(path, de->d_name);
1350        if (simplify_away(path->buf, path->len, simplify))
1351                return path_ignored;
1352
1353        dtype = DTYPE(de);
1354        return treat_one_path(dir, path, simplify, dtype, de);
1355}
1356
1357/*
1358 * Read a directory tree. We currently ignore anything but
1359 * directories, regular files and symlinks. That's because git
1360 * doesn't handle them at all yet. Maybe that will change some
1361 * day.
1362 *
1363 * Also, we ignore the name ".git" (even if it is not a directory).
1364 * That likely will not change.
1365 */
1366static int read_directory_recursive(struct dir_struct *dir,
1367                                    const char *base, int baselen,
1368                                    int check_only,
1369                                    const struct path_simplify *simplify)
1370{
1371        DIR *fdir;
1372        int contents = 0;
1373        struct dirent *de;
1374        struct strbuf path = STRBUF_INIT;
1375
1376        strbuf_add(&path, base, baselen);
1377
1378        fdir = opendir(path.len ? path.buf : ".");
1379        if (!fdir)
1380                goto out;
1381
1382        while ((de = readdir(fdir)) != NULL) {
1383                switch (treat_path(dir, de, &path, baselen, simplify)) {
1384                case path_recurse:
1385                        contents += read_directory_recursive(dir, path.buf,
1386                                path.len, check_only, simplify);
1387                        continue;
1388                case path_ignored:
1389                        continue;
1390                case path_handled:
1391                        break;
1392                }
1393                contents++;
1394                if (check_only)
1395                        break;
1396                dir_add_name(dir, path.buf, path.len);
1397        }
1398        closedir(fdir);
1399 out:
1400        strbuf_release(&path);
1401
1402        return contents;
1403}
1404
1405static int cmp_name(const void *p1, const void *p2)
1406{
1407        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1408        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1409
1410        return cache_name_compare(e1->name, e1->len,
1411                                  e2->name, e2->len);
1412}
1413
1414static struct path_simplify *create_simplify(const char **pathspec)
1415{
1416        int nr, alloc = 0;
1417        struct path_simplify *simplify = NULL;
1418
1419        if (!pathspec)
1420                return NULL;
1421
1422        for (nr = 0 ; ; nr++) {
1423                const char *match;
1424                if (nr >= alloc) {
1425                        alloc = alloc_nr(alloc);
1426                        simplify = xrealloc(simplify, alloc * sizeof(*simplify));
1427                }
1428                match = *pathspec++;
1429                if (!match)
1430                        break;
1431                simplify[nr].path = match;
1432                simplify[nr].len = simple_length(match);
1433        }
1434        simplify[nr].path = NULL;
1435        simplify[nr].len = 0;
1436        return simplify;
1437}
1438
1439static void free_simplify(struct path_simplify *simplify)
1440{
1441        free(simplify);
1442}
1443
1444static int treat_leading_path(struct dir_struct *dir,
1445                              const char *path, int len,
1446                              const struct path_simplify *simplify)
1447{
1448        struct strbuf sb = STRBUF_INIT;
1449        int baselen, rc = 0;
1450        const char *cp;
1451
1452        while (len && path[len - 1] == '/')
1453                len--;
1454        if (!len)
1455                return 1;
1456        baselen = 0;
1457        while (1) {
1458                cp = path + baselen + !!baselen;
1459                cp = memchr(cp, '/', path + len - cp);
1460                if (!cp)
1461                        baselen = len;
1462                else
1463                        baselen = cp - path;
1464                strbuf_setlen(&sb, 0);
1465                strbuf_add(&sb, path, baselen);
1466                if (!is_directory(sb.buf))
1467                        break;
1468                if (simplify_away(sb.buf, sb.len, simplify))
1469                        break;
1470                if (treat_one_path(dir, &sb, simplify,
1471                                   DT_DIR, NULL) == path_ignored)
1472                        break; /* do not recurse into it */
1473                if (len <= baselen) {
1474                        rc = 1;
1475                        break; /* finished checking */
1476                }
1477        }
1478        strbuf_release(&sb);
1479        return rc;
1480}
1481
1482int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
1483{
1484        struct path_simplify *simplify;
1485
1486        if (has_symlink_leading_path(path, len))
1487                return dir->nr;
1488
1489        simplify = create_simplify(pathspec);
1490        if (!len || treat_leading_path(dir, path, len, simplify))
1491                read_directory_recursive(dir, path, len, 0, simplify);
1492        free_simplify(simplify);
1493        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1494        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1495        return dir->nr;
1496}
1497
1498int file_exists(const char *f)
1499{
1500        struct stat sb;
1501        return lstat(f, &sb) == 0;
1502}
1503
1504/*
1505 * Given two normalized paths (a trailing slash is ok), if subdir is
1506 * outside dir, return -1.  Otherwise return the offset in subdir that
1507 * can be used as relative path to dir.
1508 */
1509int dir_inside_of(const char *subdir, const char *dir)
1510{
1511        int offset = 0;
1512
1513        assert(dir && subdir && *dir && *subdir);
1514
1515        while (*dir && *subdir && *dir == *subdir) {
1516                dir++;
1517                subdir++;
1518                offset++;
1519        }
1520
1521        /* hel[p]/me vs hel[l]/yeah */
1522        if (*dir && *subdir)
1523                return -1;
1524
1525        if (!*subdir)
1526                return !*dir ? offset : -1; /* same dir */
1527
1528        /* foo/[b]ar vs foo/[] */
1529        if (is_dir_sep(dir[-1]))
1530                return is_dir_sep(subdir[-1]) ? offset : -1;
1531
1532        /* foo[/]bar vs foo[] */
1533        return is_dir_sep(*subdir) ? offset + 1 : -1;
1534}
1535
1536int is_inside_dir(const char *dir)
1537{
1538        char cwd[PATH_MAX];
1539        if (!dir)
1540                return 0;
1541        if (!getcwd(cwd, sizeof(cwd)))
1542                die_errno("can't find the current directory");
1543        return dir_inside_of(cwd, dir) >= 0;
1544}
1545
1546int is_empty_dir(const char *path)
1547{
1548        DIR *dir = opendir(path);
1549        struct dirent *e;
1550        int ret = 1;
1551
1552        if (!dir)
1553                return 0;
1554
1555        while ((e = readdir(dir)) != NULL)
1556                if (!is_dot_or_dotdot(e->d_name)) {
1557                        ret = 0;
1558                        break;
1559                }
1560
1561        closedir(dir);
1562        return ret;
1563}
1564
1565static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
1566{
1567        DIR *dir;
1568        struct dirent *e;
1569        int ret = 0, original_len = path->len, len, kept_down = 0;
1570        int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1571        int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
1572        unsigned char submodule_head[20];
1573
1574        if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1575            !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
1576                /* Do not descend and nuke a nested git work tree. */
1577                if (kept_up)
1578                        *kept_up = 1;
1579                return 0;
1580        }
1581
1582        flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
1583        dir = opendir(path->buf);
1584        if (!dir) {
1585                /* an empty dir could be removed even if it is unreadble */
1586                if (!keep_toplevel)
1587                        return rmdir(path->buf);
1588                else
1589                        return -1;
1590        }
1591        if (path->buf[original_len - 1] != '/')
1592                strbuf_addch(path, '/');
1593
1594        len = path->len;
1595        while ((e = readdir(dir)) != NULL) {
1596                struct stat st;
1597                if (is_dot_or_dotdot(e->d_name))
1598                        continue;
1599
1600                strbuf_setlen(path, len);
1601                strbuf_addstr(path, e->d_name);
1602                if (lstat(path->buf, &st))
1603                        ; /* fall thru */
1604                else if (S_ISDIR(st.st_mode)) {
1605                        if (!remove_dir_recurse(path, flag, &kept_down))
1606                                continue; /* happy */
1607                } else if (!only_empty && !unlink(path->buf))
1608                        continue; /* happy, too */
1609
1610                /* path too long, stat fails, or non-directory still exists */
1611                ret = -1;
1612                break;
1613        }
1614        closedir(dir);
1615
1616        strbuf_setlen(path, original_len);
1617        if (!ret && !keep_toplevel && !kept_down)
1618                ret = rmdir(path->buf);
1619        else if (kept_up)
1620                /*
1621                 * report the uplevel that it is not an error that we
1622                 * did not rmdir() our directory.
1623                 */
1624                *kept_up = !ret;
1625        return ret;
1626}
1627
1628int remove_dir_recursively(struct strbuf *path, int flag)
1629{
1630        return remove_dir_recurse(path, flag, NULL);
1631}
1632
1633void setup_standard_excludes(struct dir_struct *dir)
1634{
1635        const char *path;
1636        char *xdg_path;
1637
1638        dir->exclude_per_dir = ".gitignore";
1639        path = git_path("info/exclude");
1640        if (!excludes_file) {
1641                home_config_paths(NULL, &xdg_path, "ignore");
1642                excludes_file = xdg_path;
1643        }
1644        if (!access_or_warn(path, R_OK))
1645                add_excludes_from_file(dir, path);
1646        if (excludes_file && !access_or_warn(excludes_file, R_OK))
1647                add_excludes_from_file(dir, excludes_file);
1648}
1649
1650int remove_path(const char *name)
1651{
1652        char *slash;
1653
1654        if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
1655                return -1;
1656
1657        slash = strrchr(name, '/');
1658        if (slash) {
1659                char *dirs = xstrdup(name);
1660                slash = dirs + (slash - name);
1661                do {
1662                        *slash = '\0';
1663                } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1664                free(dirs);
1665        }
1666        return 0;
1667}
1668
1669static int pathspec_item_cmp(const void *a_, const void *b_)
1670{
1671        struct pathspec_item *a, *b;
1672
1673        a = (struct pathspec_item *)a_;
1674        b = (struct pathspec_item *)b_;
1675        return strcmp(a->match, b->match);
1676}
1677
1678int init_pathspec(struct pathspec *pathspec, const char **paths)
1679{
1680        const char **p = paths;
1681        int i;
1682
1683        memset(pathspec, 0, sizeof(*pathspec));
1684        if (!p)
1685                return 0;
1686        while (*p)
1687                p++;
1688        pathspec->raw = paths;
1689        pathspec->nr = p - paths;
1690        if (!pathspec->nr)
1691                return 0;
1692
1693        pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1694        for (i = 0; i < pathspec->nr; i++) {
1695                struct pathspec_item *item = pathspec->items+i;
1696                const char *path = paths[i];
1697
1698                item->match = path;
1699                item->len = strlen(path);
1700                item->flags = 0;
1701                if (limit_pathspec_to_literal()) {
1702                        item->nowildcard_len = item->len;
1703                } else {
1704                        item->nowildcard_len = simple_length(path);
1705                        if (item->nowildcard_len < item->len) {
1706                                pathspec->has_wildcard = 1;
1707                                if (path[item->nowildcard_len] == '*' &&
1708                                    no_wildcard(path + item->nowildcard_len + 1))
1709                                        item->flags |= PATHSPEC_ONESTAR;
1710                        }
1711                }
1712        }
1713
1714        qsort(pathspec->items, pathspec->nr,
1715              sizeof(struct pathspec_item), pathspec_item_cmp);
1716
1717        return 0;
1718}
1719
1720void free_pathspec(struct pathspec *pathspec)
1721{
1722        free(pathspec->items);
1723        pathspec->items = NULL;
1724}
1725
1726int limit_pathspec_to_literal(void)
1727{
1728        static int flag = -1;
1729        if (flag < 0)
1730                flag = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
1731        return flag;
1732}
1733
1734/*
1735 * Frees memory within dir which was allocated for exclude lists and
1736 * the exclude_stack.  Does not free dir itself.
1737 */
1738void clear_directory(struct dir_struct *dir)
1739{
1740        int i, j;
1741        struct exclude_list_group *group;
1742        struct exclude_list *el;
1743        struct exclude_stack *stk;
1744
1745        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1746                group = &dir->exclude_list_group[i];
1747                for (j = 0; j < group->nr; j++) {
1748                        el = &group->el[j];
1749                        if (i == EXC_DIRS)
1750                                free((char *)el->src);
1751                        clear_exclude_list(el);
1752                }
1753                free(group->el);
1754        }
1755
1756        stk = dir->exclude_stack;
1757        while (stk) {
1758                struct exclude_stack *prev = stk->prev;
1759                free(stk);
1760                stk = prev;
1761        }
1762}