dir.con commit Teach git-update-index about gitlinks (e011054)
   1/*
   2 * This handles recursive filename detection with exclude
   3 * files, index knowledge etc..
   4 *
   5 * Copyright (C) Linus Torvalds, 2005-2006
   6 *               Junio Hamano, 2005-2006
   7 */
   8#include "cache.h"
   9#include "dir.h"
  10#include "refs.h"
  11
  12struct path_simplify {
  13        int len;
  14        const char *path;
  15};
  16
  17static int read_directory_recursive(struct dir_struct *dir,
  18        const char *path, const char *base, int baselen,
  19        int check_only, const struct path_simplify *simplify);
  20
  21int common_prefix(const char **pathspec)
  22{
  23        const char *path, *slash, *next;
  24        int prefix;
  25
  26        if (!pathspec)
  27                return 0;
  28
  29        path = *pathspec;
  30        slash = strrchr(path, '/');
  31        if (!slash)
  32                return 0;
  33
  34        prefix = slash - path + 1;
  35        while ((next = *++pathspec) != NULL) {
  36                int len = strlen(next);
  37                if (len >= prefix && !memcmp(path, next, len))
  38                        continue;
  39                for (;;) {
  40                        if (!len)
  41                                return 0;
  42                        if (next[--len] != '/')
  43                                continue;
  44                        if (memcmp(path, next, len+1))
  45                                continue;
  46                        prefix = len + 1;
  47                        break;
  48                }
  49        }
  50        return prefix;
  51}
  52
  53/*
  54 * Does 'match' matches the given name?
  55 * A match is found if
  56 *
  57 * (1) the 'match' string is leading directory of 'name', or
  58 * (2) the 'match' string is a wildcard and matches 'name', or
  59 * (3) the 'match' string is exactly the same as 'name'.
  60 *
  61 * and the return value tells which case it was.
  62 *
  63 * It returns 0 when there is no match.
  64 */
  65static int match_one(const char *match, const char *name, int namelen)
  66{
  67        int matchlen;
  68
  69        /* If the match was just the prefix, we matched */
  70        matchlen = strlen(match);
  71        if (!matchlen)
  72                return MATCHED_RECURSIVELY;
  73
  74        /*
  75         * If we don't match the matchstring exactly,
  76         * we need to match by fnmatch
  77         */
  78        if (strncmp(match, name, matchlen))
  79                return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
  80
  81        if (!name[matchlen])
  82                return MATCHED_EXACTLY;
  83        if (match[matchlen-1] == '/' || name[matchlen] == '/')
  84                return MATCHED_RECURSIVELY;
  85        return 0;
  86}
  87
  88/*
  89 * Given a name and a list of pathspecs, see if the name matches
  90 * any of the pathspecs.  The caller is also interested in seeing
  91 * all pathspec matches some names it calls this function with
  92 * (otherwise the user could have mistyped the unmatched pathspec),
  93 * and a mark is left in seen[] array for pathspec element that
  94 * actually matched anything.
  95 */
  96int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
  97{
  98        int retval;
  99        const char *match;
 100
 101        name += prefix;
 102        namelen -= prefix;
 103
 104        for (retval = 0; (match = *pathspec++) != NULL; seen++) {
 105                int how;
 106                if (retval && *seen == MATCHED_EXACTLY)
 107                        continue;
 108                match += prefix;
 109                how = match_one(match, name, namelen);
 110                if (how) {
 111                        if (retval < how)
 112                                retval = how;
 113                        if (*seen < how)
 114                                *seen = how;
 115                }
 116        }
 117        return retval;
 118}
 119
 120void add_exclude(const char *string, const char *base,
 121                 int baselen, struct exclude_list *which)
 122{
 123        struct exclude *x = xmalloc(sizeof (*x));
 124
 125        x->pattern = string;
 126        x->base = base;
 127        x->baselen = baselen;
 128        if (which->nr == which->alloc) {
 129                which->alloc = alloc_nr(which->alloc);
 130                which->excludes = xrealloc(which->excludes,
 131                                           which->alloc * sizeof(x));
 132        }
 133        which->excludes[which->nr++] = x;
 134}
 135
 136static int add_excludes_from_file_1(const char *fname,
 137                                    const char *base,
 138                                    int baselen,
 139                                    struct exclude_list *which)
 140{
 141        struct stat st;
 142        int fd, i;
 143        size_t size;
 144        char *buf, *entry;
 145
 146        fd = open(fname, O_RDONLY);
 147        if (fd < 0 || fstat(fd, &st) < 0)
 148                goto err;
 149        size = xsize_t(st.st_size);
 150        if (size == 0) {
 151                close(fd);
 152                return 0;
 153        }
 154        buf = xmalloc(size+1);
 155        if (read_in_full(fd, buf, size) != size)
 156                goto err;
 157        close(fd);
 158
 159        buf[size++] = '\n';
 160        entry = buf;
 161        for (i = 0; i < size; i++) {
 162                if (buf[i] == '\n') {
 163                        if (entry != buf + i && entry[0] != '#') {
 164                                buf[i - (i && buf[i-1] == '\r')] = 0;
 165                                add_exclude(entry, base, baselen, which);
 166                        }
 167                        entry = buf + i + 1;
 168                }
 169        }
 170        return 0;
 171
 172 err:
 173        if (0 <= fd)
 174                close(fd);
 175        return -1;
 176}
 177
 178void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 179{
 180        if (add_excludes_from_file_1(fname, "", 0,
 181                                     &dir->exclude_list[EXC_FILE]) < 0)
 182                die("cannot use %s as an exclude file", fname);
 183}
 184
 185int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
 186{
 187        char exclude_file[PATH_MAX];
 188        struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
 189        int current_nr = el->nr;
 190
 191        if (dir->exclude_per_dir) {
 192                memcpy(exclude_file, base, baselen);
 193                strcpy(exclude_file + baselen, dir->exclude_per_dir);
 194                add_excludes_from_file_1(exclude_file, base, baselen, el);
 195        }
 196        return current_nr;
 197}
 198
 199void pop_exclude_per_directory(struct dir_struct *dir, int stk)
 200{
 201        struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
 202
 203        while (stk < el->nr)
 204                free(el->excludes[--el->nr]);
 205}
 206
 207/* Scan the list and let the last match determines the fate.
 208 * Return 1 for exclude, 0 for include and -1 for undecided.
 209 */
 210static int excluded_1(const char *pathname,
 211                      int pathlen,
 212                      struct exclude_list *el)
 213{
 214        int i;
 215
 216        if (el->nr) {
 217                for (i = el->nr - 1; 0 <= i; i--) {
 218                        struct exclude *x = el->excludes[i];
 219                        const char *exclude = x->pattern;
 220                        int to_exclude = 1;
 221
 222                        if (*exclude == '!') {
 223                                to_exclude = 0;
 224                                exclude++;
 225                        }
 226
 227                        if (!strchr(exclude, '/')) {
 228                                /* match basename */
 229                                const char *basename = strrchr(pathname, '/');
 230                                basename = (basename) ? basename+1 : pathname;
 231                                if (fnmatch(exclude, basename, 0) == 0)
 232                                        return to_exclude;
 233                        }
 234                        else {
 235                                /* match with FNM_PATHNAME:
 236                                 * exclude has base (baselen long) implicitly
 237                                 * in front of it.
 238                                 */
 239                                int baselen = x->baselen;
 240                                if (*exclude == '/')
 241                                        exclude++;
 242
 243                                if (pathlen < baselen ||
 244                                    (baselen && pathname[baselen-1] != '/') ||
 245                                    strncmp(pathname, x->base, baselen))
 246                                    continue;
 247
 248                                if (fnmatch(exclude, pathname+baselen,
 249                                            FNM_PATHNAME) == 0)
 250                                        return to_exclude;
 251                        }
 252                }
 253        }
 254        return -1; /* undecided */
 255}
 256
 257int excluded(struct dir_struct *dir, const char *pathname)
 258{
 259        int pathlen = strlen(pathname);
 260        int st;
 261
 262        for (st = EXC_CMDL; st <= EXC_FILE; st++) {
 263                switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
 264                case 0:
 265                        return 0;
 266                case 1:
 267                        return 1;
 268                }
 269        }
 270        return 0;
 271}
 272
 273struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
 274{
 275        struct dir_entry *ent;
 276
 277        if (cache_name_pos(pathname, len) >= 0)
 278                return NULL;
 279
 280        if (dir->nr == dir->alloc) {
 281                int alloc = alloc_nr(dir->alloc);
 282                dir->alloc = alloc;
 283                dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
 284        }
 285        ent = xmalloc(sizeof(*ent) + len + 1);
 286        ent->ignored = ent->ignored_dir = 0;
 287        ent->len = len;
 288        memcpy(ent->name, pathname, len);
 289        ent->name[len] = 0;
 290        dir->entries[dir->nr++] = ent;
 291        return ent;
 292}
 293
 294enum exist_status {
 295        index_nonexistent = 0,
 296        index_directory,
 297        index_gitdir,
 298};
 299
 300/*
 301 * The index sorts alphabetically by entry name, which
 302 * means that a gitlink sorts as '\0' at the end, while
 303 * a directory (which is defined not as an entry, but as
 304 * the files it contains) will sort with the '/' at the
 305 * end.
 306 */
 307static enum exist_status directory_exists_in_index(const char *dirname, int len)
 308{
 309        int pos = cache_name_pos(dirname, len);
 310        if (pos < 0)
 311                pos = -pos-1;
 312        while (pos < active_nr) {
 313                struct cache_entry *ce = active_cache[pos++];
 314                unsigned char endchar;
 315
 316                if (strncmp(ce->name, dirname, len))
 317                        break;
 318                endchar = ce->name[len];
 319                if (endchar > '/')
 320                        break;
 321                if (endchar == '/')
 322                        return index_directory;
 323                if (!endchar && S_ISDIRLNK(ntohl(ce->ce_mode)))
 324                        return index_gitdir;
 325        }
 326        return index_nonexistent;
 327}
 328
 329/*
 330 * When we find a directory when traversing the filesystem, we
 331 * have three distinct cases:
 332 *
 333 *  - ignore it
 334 *  - see it as a directory
 335 *  - recurse into it
 336 *
 337 * and which one we choose depends on a combination of existing
 338 * git index contents and the flags passed into the directory
 339 * traversal routine.
 340 *
 341 * Case 1: If we *already* have entries in the index under that
 342 * directory name, we always recurse into the directory to see
 343 * all the files.
 344 *
 345 * Case 2: If we *already* have that directory name as a gitlink,
 346 * we always continue to see it as a gitlink, regardless of whether
 347 * there is an actual git directory there or not (it might not
 348 * be checked out as a subproject!)
 349 *
 350 * Case 3: if we didn't have it in the index previously, we
 351 * have a few sub-cases:
 352 *
 353 *  (a) if "show_other_directories" is true, we show it as
 354 *      just a directory, unless "hide_empty_directories" is
 355 *      also true and the directory is empty, in which case
 356 *      we just ignore it entirely.
 357 *  (b) if it looks like a git directory, and we don't have
 358 *      'no_dirlinks' set we treat it as a gitlink, and show it
 359 *      as a directory.
 360 *  (c) otherwise, we recurse into it.
 361 */
 362enum directory_treatment {
 363        show_directory,
 364        ignore_directory,
 365        recurse_into_directory,
 366};
 367
 368static enum directory_treatment treat_directory(struct dir_struct *dir,
 369        const char *dirname, int len,
 370        const struct path_simplify *simplify)
 371{
 372        /* The "len-1" is to strip the final '/' */
 373        switch (directory_exists_in_index(dirname, len-1)) {
 374        case index_directory:
 375                return recurse_into_directory;
 376
 377        case index_gitdir:
 378                return show_directory;
 379
 380        case index_nonexistent:
 381                if (dir->show_other_directories)
 382                        break;
 383                if (!dir->no_dirlinks) {
 384                        unsigned char sha1[20];
 385                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
 386                                return show_directory;
 387                }
 388                return recurse_into_directory;
 389        }
 390
 391        /* This is the "show_other_directories" case */
 392        if (!dir->hide_empty_directories)
 393                return show_directory;
 394        if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
 395                return ignore_directory;
 396        return show_directory;
 397}
 398
 399/*
 400 * This is an inexact early pruning of any recursive directory
 401 * reading - if the path cannot possibly be in the pathspec,
 402 * return true, and we'll skip it early.
 403 */
 404static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
 405{
 406        if (simplify) {
 407                for (;;) {
 408                        const char *match = simplify->path;
 409                        int len = simplify->len;
 410
 411                        if (!match)
 412                                break;
 413                        if (len > pathlen)
 414                                len = pathlen;
 415                        if (!memcmp(path, match, len))
 416                                return 0;
 417                        simplify++;
 418                }
 419                return 1;
 420        }
 421        return 0;
 422}
 423
 424/*
 425 * Read a directory tree. We currently ignore anything but
 426 * directories, regular files and symlinks. That's because git
 427 * doesn't handle them at all yet. Maybe that will change some
 428 * day.
 429 *
 430 * Also, we ignore the name ".git" (even if it is not a directory).
 431 * That likely will not change.
 432 */
 433static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
 434{
 435        DIR *fdir = opendir(path);
 436        int contents = 0;
 437
 438        if (fdir) {
 439                int exclude_stk;
 440                struct dirent *de;
 441                char fullname[PATH_MAX + 1];
 442                memcpy(fullname, base, baselen);
 443
 444                exclude_stk = push_exclude_per_directory(dir, base, baselen);
 445
 446                while ((de = readdir(fdir)) != NULL) {
 447                        int len;
 448
 449                        if ((de->d_name[0] == '.') &&
 450                            (de->d_name[1] == 0 ||
 451                             !strcmp(de->d_name + 1, ".") ||
 452                             !strcmp(de->d_name + 1, "git")))
 453                                continue;
 454                        len = strlen(de->d_name);
 455                        /* Ignore overly long pathnames! */
 456                        if (len + baselen + 8 > sizeof(fullname))
 457                                continue;
 458                        memcpy(fullname + baselen, de->d_name, len+1);
 459                        if (simplify_away(fullname, baselen + len, simplify))
 460                                continue;
 461                        if (excluded(dir, fullname) != dir->show_ignored) {
 462                                if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
 463                                        continue;
 464                                }
 465                        }
 466
 467                        switch (DTYPE(de)) {
 468                        struct stat st;
 469                        default:
 470                                continue;
 471                        case DT_UNKNOWN:
 472                                if (lstat(fullname, &st))
 473                                        continue;
 474                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
 475                                        break;
 476                                if (!S_ISDIR(st.st_mode))
 477                                        continue;
 478                                /* fallthrough */
 479                        case DT_DIR:
 480                                memcpy(fullname + baselen + len, "/", 2);
 481                                len++;
 482                                switch (treat_directory(dir, fullname, baselen + len, simplify)) {
 483                                case show_directory:
 484                                        break;
 485                                case recurse_into_directory:
 486                                        contents += read_directory_recursive(dir,
 487                                                fullname, fullname, baselen + len, 0, simplify);
 488                                        continue;
 489                                case ignore_directory:
 490                                        continue;
 491                                }
 492                                break;
 493                        case DT_REG:
 494                        case DT_LNK:
 495                                break;
 496                        }
 497                        contents++;
 498                        if (check_only)
 499                                goto exit_early;
 500                        else
 501                                dir_add_name(dir, fullname, baselen + len);
 502                }
 503exit_early:
 504                closedir(fdir);
 505
 506                pop_exclude_per_directory(dir, exclude_stk);
 507        }
 508
 509        return contents;
 510}
 511
 512static int cmp_name(const void *p1, const void *p2)
 513{
 514        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
 515        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
 516
 517        return cache_name_compare(e1->name, e1->len,
 518                                  e2->name, e2->len);
 519}
 520
 521/*
 522 * Return the length of the "simple" part of a path match limiter.
 523 */
 524static int simple_length(const char *match)
 525{
 526        const char special[256] = {
 527                [0] = 1, ['?'] = 1,
 528                ['\\'] = 1, ['*'] = 1,
 529                ['['] = 1
 530        };
 531        int len = -1;
 532
 533        for (;;) {
 534                unsigned char c = *match++;
 535                len++;
 536                if (special[c])
 537                        return len;
 538        }
 539}
 540
 541static struct path_simplify *create_simplify(const char **pathspec)
 542{
 543        int nr, alloc = 0;
 544        struct path_simplify *simplify = NULL;
 545
 546        if (!pathspec)
 547                return NULL;
 548
 549        for (nr = 0 ; ; nr++) {
 550                const char *match;
 551                if (nr >= alloc) {
 552                        alloc = alloc_nr(alloc);
 553                        simplify = xrealloc(simplify, alloc * sizeof(*simplify));
 554                }
 555                match = *pathspec++;
 556                if (!match)
 557                        break;
 558                simplify[nr].path = match;
 559                simplify[nr].len = simple_length(match);
 560        }
 561        simplify[nr].path = NULL;
 562        simplify[nr].len = 0;
 563        return simplify;
 564}
 565
 566static void free_simplify(struct path_simplify *simplify)
 567{
 568        if (simplify)
 569                free(simplify);
 570}
 571
 572int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
 573{
 574        struct path_simplify *simplify = create_simplify(pathspec);
 575
 576        /*
 577         * Make sure to do the per-directory exclude for all the
 578         * directories leading up to our base.
 579         */
 580        if (baselen) {
 581                if (dir->exclude_per_dir) {
 582                        char *p, *pp = xmalloc(baselen+1);
 583                        memcpy(pp, base, baselen+1);
 584                        p = pp;
 585                        while (1) {
 586                                char save = *p;
 587                                *p = 0;
 588                                push_exclude_per_directory(dir, pp, p-pp);
 589                                *p++ = save;
 590                                if (!save)
 591                                        break;
 592                                p = strchr(p, '/');
 593                                if (p)
 594                                        p++;
 595                                else
 596                                        p = pp + baselen;
 597                        }
 598                        free(pp);
 599                }
 600        }
 601
 602        read_directory_recursive(dir, path, base, baselen, 0, simplify);
 603        free_simplify(simplify);
 604        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 605        return dir->nr;
 606}
 607
 608int
 609file_exists(const char *f)
 610{
 611  struct stat sb;
 612  return stat(f, &sb) == 0;
 613}