4e6a727570a3d9c7511009bcc71f8ae0ed2d5fa1
   1#include "cache.h"
   2#include "dir.h"
   3#include "pathspec.h"
   4
   5/*
   6 * Finds which of the given pathspecs match items in the index.
   7 *
   8 * For each pathspec, sets the corresponding entry in the seen[] array
   9 * (which should be specs items long, i.e. the same size as pathspec)
  10 * to the nature of the "closest" (i.e. most specific) match found for
  11 * that pathspec in the index, if it was a closer type of match than
  12 * the existing entry.  As an optimization, matching is skipped
  13 * altogether if seen[] already only contains non-zero entries.
  14 *
  15 * If seen[] has not already been written to, it may make sense
  16 * to use find_pathspecs_matching_against_index() instead.
  17 */
  18void add_pathspec_matches_against_index(const struct pathspec *pathspec,
  19                                        char *seen)
  20{
  21        int num_unmatched = 0, i;
  22
  23        /*
  24         * Since we are walking the index as if we were walking the directory,
  25         * we have to mark the matched pathspec as seen; otherwise we will
  26         * mistakenly think that the user gave a pathspec that did not match
  27         * anything.
  28         */
  29        for (i = 0; i < pathspec->nr; i++)
  30                if (!seen[i])
  31                        num_unmatched++;
  32        if (!num_unmatched)
  33                return;
  34        for (i = 0; i < active_nr; i++) {
  35                const struct cache_entry *ce = active_cache[i];
  36                match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen);
  37        }
  38}
  39
  40/*
  41 * Finds which of the given pathspecs match items in the index.
  42 *
  43 * This is a one-shot wrapper around add_pathspec_matches_against_index()
  44 * which allocates, populates, and returns a seen[] array indicating the
  45 * nature of the "closest" (i.e. most specific) matches which each of the
  46 * given pathspecs achieves against all items in the index.
  47 */
  48char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
  49{
  50        char *seen = xcalloc(pathspec->nr, 1);
  51        add_pathspec_matches_against_index(pathspec, seen);
  52        return seen;
  53}
  54
  55/*
  56 * Magic pathspec
  57 *
  58 * Possible future magic semantics include stuff like:
  59 *
  60 *      { PATHSPEC_RECURSIVE, '*', "recursive" },
  61 *      { PATHSPEC_REGEXP, '\0', "regexp" },
  62 *
  63 */
  64
  65static struct pathspec_magic {
  66        unsigned bit;
  67        char mnemonic; /* this cannot be ':'! */
  68        const char *name;
  69} pathspec_magic[] = {
  70        { PATHSPEC_FROMTOP, '/', "top" },
  71        { PATHSPEC_LITERAL,   0, "literal" },
  72        { PATHSPEC_GLOB,   '\0', "glob" },
  73        { PATHSPEC_ICASE,  '\0', "icase" },
  74        { PATHSPEC_EXCLUDE, '!', "exclude" },
  75};
  76
  77/*
  78 * Take an element of a pathspec and check for magic signatures.
  79 * Append the result to the prefix. Return the magic bitmap.
  80 *
  81 * For now, we only parse the syntax and throw out anything other than
  82 * "top" magic.
  83 *
  84 * NEEDSWORK: This needs to be rewritten when we start migrating
  85 * get_pathspec() users to use the "struct pathspec" interface.  For
  86 * example, a pathspec element may be marked as case-insensitive, but
  87 * the prefix part must always match literally, and a single stupid
  88 * string cannot express such a case.
  89 */
  90static unsigned prefix_pathspec(struct pathspec_item *item,
  91                                unsigned *p_short_magic,
  92                                const char **raw, unsigned flags,
  93                                const char *prefix, int prefixlen,
  94                                const char *elt)
  95{
  96        static int literal_global = -1;
  97        static int glob_global = -1;
  98        static int noglob_global = -1;
  99        static int icase_global = -1;
 100        unsigned magic = 0, short_magic = 0, global_magic = 0;
 101        const char *copyfrom = elt, *long_magic_end = NULL;
 102        char *match;
 103        int i, pathspec_prefix = -1;
 104
 105        if (literal_global < 0)
 106                literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
 107        if (literal_global)
 108                global_magic |= PATHSPEC_LITERAL;
 109
 110        if (glob_global < 0)
 111                glob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
 112        if (glob_global)
 113                global_magic |= PATHSPEC_GLOB;
 114
 115        if (noglob_global < 0)
 116                noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
 117
 118        if (glob_global && noglob_global)
 119                die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
 120
 121
 122        if (icase_global < 0)
 123                icase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
 124        if (icase_global)
 125                global_magic |= PATHSPEC_ICASE;
 126
 127        if ((global_magic & PATHSPEC_LITERAL) &&
 128            (global_magic & ~PATHSPEC_LITERAL))
 129                die(_("global 'literal' pathspec setting is incompatible "
 130                      "with all other global pathspec settings"));
 131
 132        if (flags & PATHSPEC_LITERAL_PATH)
 133                global_magic = 0;
 134
 135        if (elt[0] != ':' || literal_global ||
 136            (flags & PATHSPEC_LITERAL_PATH)) {
 137                ; /* nothing to do */
 138        } else if (elt[1] == '(') {
 139                /* longhand */
 140                const char *nextat;
 141                for (copyfrom = elt + 2;
 142                     *copyfrom && *copyfrom != ')';
 143                     copyfrom = nextat) {
 144                        size_t len = strcspn(copyfrom, ",)");
 145                        if (copyfrom[len] == ',')
 146                                nextat = copyfrom + len + 1;
 147                        else
 148                                /* handle ')' and '\0' */
 149                                nextat = copyfrom + len;
 150                        if (!len)
 151                                continue;
 152                        for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
 153                                if (strlen(pathspec_magic[i].name) == len &&
 154                                    !strncmp(pathspec_magic[i].name, copyfrom, len)) {
 155                                        magic |= pathspec_magic[i].bit;
 156                                        break;
 157                                }
 158                                if (!prefixcmp(copyfrom, "prefix:")) {
 159                                        char *endptr;
 160                                        pathspec_prefix = strtol(copyfrom + 7,
 161                                                                 &endptr, 10);
 162                                        if (endptr - copyfrom != len)
 163                                                die(_("invalid parameter for pathspec magic 'prefix'"));
 164                                        /* "i" would be wrong, but it does not matter */
 165                                        break;
 166                                }
 167                        }
 168                        if (ARRAY_SIZE(pathspec_magic) <= i)
 169                                die(_("Invalid pathspec magic '%.*s' in '%s'"),
 170                                    (int) len, copyfrom, elt);
 171                }
 172                if (*copyfrom != ')')
 173                        die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
 174                long_magic_end = copyfrom;
 175                copyfrom++;
 176        } else {
 177                /* shorthand */
 178                for (copyfrom = elt + 1;
 179                     *copyfrom && *copyfrom != ':';
 180                     copyfrom++) {
 181                        char ch = *copyfrom;
 182
 183                        if (!is_pathspec_magic(ch))
 184                                break;
 185                        for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
 186                                if (pathspec_magic[i].mnemonic == ch) {
 187                                        short_magic |= pathspec_magic[i].bit;
 188                                        break;
 189                                }
 190                        if (ARRAY_SIZE(pathspec_magic) <= i)
 191                                die(_("Unimplemented pathspec magic '%c' in '%s'"),
 192                                    ch, elt);
 193                }
 194                if (*copyfrom == ':')
 195                        copyfrom++;
 196        }
 197
 198        magic |= short_magic;
 199        *p_short_magic = short_magic;
 200
 201        /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
 202        if (noglob_global && !(magic & PATHSPEC_GLOB))
 203                global_magic |= PATHSPEC_LITERAL;
 204
 205        /* --glob-pathspec is overridden by :(literal) */
 206        if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
 207                global_magic &= ~PATHSPEC_GLOB;
 208
 209        magic |= global_magic;
 210
 211        if (pathspec_prefix >= 0 &&
 212            (prefixlen || (prefix && *prefix)))
 213                die("BUG: 'prefix' magic is supposed to be used at worktree's root");
 214
 215        if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
 216                die(_("%s: 'literal' and 'glob' are incompatible"), elt);
 217
 218        if (pathspec_prefix >= 0) {
 219                match = xstrdup(copyfrom);
 220                prefixlen = pathspec_prefix;
 221        } else if (magic & PATHSPEC_FROMTOP) {
 222                match = xstrdup(copyfrom);
 223                prefixlen = 0;
 224        } else {
 225                match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
 226                if (!match)
 227                        die(_("%s: '%s' is outside repository"), elt, copyfrom);
 228        }
 229        *raw = item->match = match;
 230        /*
 231         * Prefix the pathspec (keep all magic) and assign to
 232         * original. Useful for passing to another command.
 233         */
 234        if (flags & PATHSPEC_PREFIX_ORIGIN) {
 235                struct strbuf sb = STRBUF_INIT;
 236                const char *start = elt;
 237                if (prefixlen && !literal_global) {
 238                        /* Preserve the actual prefix length of each pattern */
 239                        if (short_magic)
 240                                die("BUG: prefixing on short magic is not supported");
 241                        else if (long_magic_end) {
 242                                strbuf_add(&sb, start, long_magic_end - start);
 243                                strbuf_addf(&sb, ",prefix:%d", prefixlen);
 244                                start = long_magic_end;
 245                        } else {
 246                                if (*start == ':')
 247                                        start++;
 248                                strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
 249                        }
 250                }
 251                strbuf_add(&sb, start, copyfrom - start);
 252                strbuf_addstr(&sb, match);
 253                item->original = strbuf_detach(&sb, NULL);
 254        } else
 255                item->original = elt;
 256        item->len = strlen(item->match);
 257        item->prefix = prefixlen;
 258
 259        if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
 260            (item->len >= 1 && item->match[item->len - 1] == '/') &&
 261            (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
 262            S_ISGITLINK(active_cache[i]->ce_mode)) {
 263                item->len--;
 264                match[item->len] = '\0';
 265        }
 266
 267        if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
 268                for (i = 0; i < active_nr; i++) {
 269                        struct cache_entry *ce = active_cache[i];
 270                        int ce_len = ce_namelen(ce);
 271
 272                        if (!S_ISGITLINK(ce->ce_mode))
 273                                continue;
 274
 275                        if (item->len <= ce_len || match[ce_len] != '/' ||
 276                            memcmp(ce->name, match, ce_len))
 277                                continue;
 278                        if (item->len == ce_len + 1) {
 279                                /* strip trailing slash */
 280                                item->len--;
 281                                match[item->len] = '\0';
 282                        } else
 283                                die (_("Pathspec '%s' is in submodule '%.*s'"),
 284                                     elt, ce_len, ce->name);
 285                }
 286
 287        if (magic & PATHSPEC_LITERAL)
 288                item->nowildcard_len = item->len;
 289        else {
 290                item->nowildcard_len = simple_length(item->match);
 291                if (item->nowildcard_len < prefixlen)
 292                        item->nowildcard_len = prefixlen;
 293        }
 294        item->flags = 0;
 295        if (magic & PATHSPEC_GLOB) {
 296                /*
 297                 * FIXME: should we enable ONESTAR in _GLOB for
 298                 * pattern "* * / * . c"?
 299                 */
 300        } else {
 301                if (item->nowildcard_len < item->len &&
 302                    item->match[item->nowildcard_len] == '*' &&
 303                    no_wildcard(item->match + item->nowildcard_len + 1))
 304                        item->flags |= PATHSPEC_ONESTAR;
 305        }
 306
 307        /* sanity checks, pathspec matchers assume these are sane */
 308        assert(item->nowildcard_len <= item->len &&
 309               item->prefix         <= item->len);
 310        return magic;
 311}
 312
 313static int pathspec_item_cmp(const void *a_, const void *b_)
 314{
 315        struct pathspec_item *a, *b;
 316
 317        a = (struct pathspec_item *)a_;
 318        b = (struct pathspec_item *)b_;
 319        return strcmp(a->match, b->match);
 320}
 321
 322static void NORETURN unsupported_magic(const char *pattern,
 323                                       unsigned magic,
 324                                       unsigned short_magic)
 325{
 326        struct strbuf sb = STRBUF_INIT;
 327        int i, n;
 328        for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
 329                const struct pathspec_magic *m = pathspec_magic + i;
 330                if (!(magic & m->bit))
 331                        continue;
 332                if (sb.len)
 333                        strbuf_addstr(&sb, " ");
 334                if (short_magic & m->bit)
 335                        strbuf_addf(&sb, "'%c'", m->mnemonic);
 336                else
 337                        strbuf_addf(&sb, "'%s'", m->name);
 338                n++;
 339        }
 340        /*
 341         * We may want to substitute "this command" with a command
 342         * name. E.g. when add--interactive dies when running
 343         * "checkout -p"
 344         */
 345        die(_("%s: pathspec magic not supported by this command: %s"),
 346            pattern, sb.buf);
 347}
 348
 349/*
 350 * Given command line arguments and a prefix, convert the input to
 351 * pathspec. die() if any magic in magic_mask is used.
 352 */
 353void parse_pathspec(struct pathspec *pathspec,
 354                    unsigned magic_mask, unsigned flags,
 355                    const char *prefix, const char **argv)
 356{
 357        struct pathspec_item *item;
 358        const char *entry = argv ? *argv : NULL;
 359        int i, n, prefixlen, nr_exclude = 0;
 360
 361        memset(pathspec, 0, sizeof(*pathspec));
 362
 363        if (flags & PATHSPEC_MAXDEPTH_VALID)
 364                pathspec->magic |= PATHSPEC_MAXDEPTH;
 365
 366        /* No arguments, no prefix -> no pathspec */
 367        if (!entry && !prefix)
 368                return;
 369
 370        if ((flags & PATHSPEC_PREFER_CWD) &&
 371            (flags & PATHSPEC_PREFER_FULL))
 372                die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
 373
 374        /* No arguments with prefix -> prefix pathspec */
 375        if (!entry) {
 376                static const char *raw[2];
 377
 378                if (flags & PATHSPEC_PREFER_FULL)
 379                        return;
 380
 381                if (!(flags & PATHSPEC_PREFER_CWD))
 382                        die("BUG: PATHSPEC_PREFER_CWD requires arguments");
 383
 384                pathspec->items = item = xmalloc(sizeof(*item));
 385                memset(item, 0, sizeof(*item));
 386                item->match = prefix;
 387                item->original = prefix;
 388                item->nowildcard_len = item->len = strlen(prefix);
 389                item->prefix = item->len;
 390                raw[0] = prefix;
 391                raw[1] = NULL;
 392                pathspec->nr = 1;
 393                pathspec->_raw = raw;
 394                return;
 395        }
 396
 397        n = 0;
 398        while (argv[n])
 399                n++;
 400
 401        pathspec->nr = n;
 402        pathspec->items = item = xmalloc(sizeof(*item) * n);
 403        pathspec->_raw = argv;
 404        prefixlen = prefix ? strlen(prefix) : 0;
 405
 406        for (i = 0; i < n; i++) {
 407                unsigned short_magic;
 408                entry = argv[i];
 409
 410                item[i].magic = prefix_pathspec(item + i, &short_magic,
 411                                                argv + i, flags,
 412                                                prefix, prefixlen, entry);
 413                if ((flags & PATHSPEC_LITERAL_PATH) &&
 414                    !(magic_mask & PATHSPEC_LITERAL))
 415                        item[i].magic |= PATHSPEC_LITERAL;
 416                if (item[i].magic & PATHSPEC_EXCLUDE)
 417                        nr_exclude++;
 418                if (item[i].magic & magic_mask)
 419                        unsupported_magic(entry,
 420                                          item[i].magic & magic_mask,
 421                                          short_magic);
 422
 423                if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
 424                    has_symlink_leading_path(item[i].match, item[i].len)) {
 425                        die(_("pathspec '%s' is beyond a symbolic link"), entry);
 426                }
 427
 428                if (item[i].nowildcard_len < item[i].len)
 429                        pathspec->has_wildcard = 1;
 430                pathspec->magic |= item[i].magic;
 431        }
 432
 433        if (nr_exclude == n)
 434                die(_("There is nothing to exclude from by :(exclude) patterns.\n"
 435                      "Perhaps you forgot to add either ':/' or '.' ?"));
 436
 437
 438        if (pathspec->magic & PATHSPEC_MAXDEPTH) {
 439                if (flags & PATHSPEC_KEEP_ORDER)
 440                        die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
 441                qsort(pathspec->items, pathspec->nr,
 442                      sizeof(struct pathspec_item), pathspec_item_cmp);
 443        }
 444}
 445
 446/*
 447 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
 448 * based interface - see pathspec.c:parse_pathspec().
 449 *
 450 * Arguments:
 451 *  - prefix - a path relative to the root of the working tree
 452 *  - pathspec - a list of paths underneath the prefix path
 453 *
 454 * Iterates over pathspec, prepending each path with prefix,
 455 * and return the resulting list.
 456 *
 457 * If pathspec is empty, return a singleton list containing prefix.
 458 *
 459 * If pathspec and prefix are both empty, return an empty list.
 460 *
 461 * This is typically used by built-in commands such as add.c, in order
 462 * to normalize argv arguments provided to the built-in into a list of
 463 * paths to process, all relative to the root of the working tree.
 464 */
 465const char **get_pathspec(const char *prefix, const char **pathspec)
 466{
 467        struct pathspec ps;
 468        parse_pathspec(&ps,
 469                       PATHSPEC_ALL_MAGIC &
 470                       ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
 471                       PATHSPEC_PREFER_CWD,
 472                       prefix, pathspec);
 473        return ps._raw;
 474}
 475
 476void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
 477{
 478        *dst = *src;
 479        dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
 480        memcpy(dst->items, src->items,
 481               sizeof(struct pathspec_item) * dst->nr);
 482}
 483
 484void free_pathspec(struct pathspec *pathspec)
 485{
 486        free(pathspec->items);
 487        pathspec->items = NULL;
 488}