]> git.ipfire.org Git - thirdparty/git.git/blobdiff - pathspec.c
Merge branch 'ws/request-pull-code-cleanup'
[thirdparty/git.git] / pathspec.c
index f6356bde16fcd3d85dfbb906b042c7b40a4487a6..7ababb315944d5536ec251b19937beef5def2538 100644 (file)
@@ -67,11 +67,11 @@ static struct pathspec_magic {
        char mnemonic; /* this cannot be ':'! */
        const char *name;
 } pathspec_magic[] = {
-       { PATHSPEC_FROMTOP, '/', "top" },
-       { PATHSPEC_LITERAL,   0, "literal" },
-       { PATHSPEC_GLOB,   '\0', "glob" },
-       { PATHSPEC_ICASE,  '\0', "icase" },
-       { PATHSPEC_EXCLUDE, '!', "exclude" },
+       { PATHSPEC_FROMTOP,  '/', "top" },
+       { PATHSPEC_LITERAL, '\0', "literal" },
+       { PATHSPEC_GLOB,    '\0', "glob" },
+       { PATHSPEC_ICASE,   '\0', "icase" },
+       { PATHSPEC_EXCLUDE,  '!', "exclude" },
 };
 
 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -245,48 +245,102 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
        return pos;
 }
 
+static const char *parse_element_magic(unsigned *magic, int *prefix_len,
+                                      const char *elem)
+{
+       if (elem[0] != ':' || get_literal_global())
+               return elem; /* nothing to do */
+       else if (elem[1] == '(')
+               /* longhand */
+               return parse_long_magic(magic, prefix_len, elem);
+       else
+               /* shorthand */
+               return parse_short_magic(magic, elem);
+}
+
+static void strip_submodule_slash_cheap(struct pathspec_item *item)
+{
+       if (item->len >= 1 && item->match[item->len - 1] == '/') {
+               int i = cache_name_pos(item->match, item->len - 1);
+
+               if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
+                       item->len--;
+                       item->match[item->len] = '\0';
+               }
+       }
+}
+
+static void strip_submodule_slash_expensive(struct pathspec_item *item)
+{
+       int i;
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               int ce_len = ce_namelen(ce);
+
+               if (!S_ISGITLINK(ce->ce_mode))
+                       continue;
+
+               if (item->len <= ce_len || item->match[ce_len] != '/' ||
+                   memcmp(ce->name, item->match, ce_len))
+                       continue;
+
+               if (item->len == ce_len + 1) {
+                       /* strip trailing slash */
+                       item->len--;
+                       item->match[item->len] = '\0';
+               } else {
+                       die(_("Pathspec '%s' is in submodule '%.*s'"),
+                           item->original, ce_len, ce->name);
+               }
+       }
+}
+
+static void die_inside_submodule_path(struct pathspec_item *item)
+{
+       int i;
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               int ce_len = ce_namelen(ce);
+
+               if (!S_ISGITLINK(ce->ce_mode))
+                       continue;
+
+               if (item->len < ce_len ||
+                   !(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
+                   memcmp(ce->name, item->match, ce_len))
+                       continue;
+
+               die(_("Pathspec '%s' is in submodule '%.*s'"),
+                   item->original, ce_len, ce->name);
+       }
+}
+
 /*
- * Take an element of a pathspec and check for magic signatures.
- * Append the result to the prefix. Return the magic bitmap.
- *
- * For now, we only parse the syntax and throw out anything other than
- * "top" magic.
- *
- * NEEDSWORK: This needs to be rewritten when we start migrating
- * get_pathspec() users to use the "struct pathspec" interface.  For
- * example, a pathspec element may be marked as case-insensitive, but
- * the prefix part must always match literally, and a single stupid
- * string cannot express such a case.
+ * Perform the initialization of a pathspec_item based on a pathspec element.
  */
-static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
-                               const char *prefix, int prefixlen,
-                               const char *elt)
+static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
+                              const char *prefix, int prefixlen,
+                              const char *elt)
 {
        unsigned magic = 0, element_magic = 0;
        const char *copyfrom = elt;
        char *match;
-       int i, pathspec_prefix = -1;
-
-       if (elt[0] != ':' || get_literal_global() ||
-           (flags & PATHSPEC_LITERAL_PATH)) {
-               ; /* nothing to do */
-       } else if (elt[1] == '(') {
-               /* longhand */
-               copyfrom = parse_long_magic(&element_magic,
-                                           &pathspec_prefix,
-                                           elt);
-       } else {
-               /* shorthand */
-               copyfrom = parse_short_magic(&element_magic, elt);
-       }
-
-       magic |= element_magic;
+       int pathspec_prefix = -1;
 
        /* PATHSPEC_LITERAL_PATH ignores magic */
-       if (flags & PATHSPEC_LITERAL_PATH)
+       if (flags & PATHSPEC_LITERAL_PATH) {
                magic = PATHSPEC_LITERAL;
-       else
+       } else {
+               copyfrom = parse_element_magic(&element_magic,
+                                              &pathspec_prefix,
+                                              elt);
+               magic |= element_magic;
                magic |= get_global_magic(element_magic);
+       }
+
+       item->magic = magic;
 
        if (pathspec_prefix >= 0 &&
            (prefixlen || (prefix && *prefix)))
@@ -295,6 +349,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
        if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
                die(_("%s: 'literal' and 'glob' are incompatible"), elt);
 
+       /* Create match string which will be used for pathspec matching */
        if (pathspec_prefix >= 0) {
                match = xstrdup(copyfrom);
                prefixlen = pathspec_prefix;
@@ -302,11 +357,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
                match = xstrdup(copyfrom);
                prefixlen = 0;
        } else {
-               match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
+               match = prefix_path_gently(prefix, prefixlen,
+                                          &prefixlen, copyfrom);
                if (!match)
                        die(_("%s: '%s' is outside repository"), elt, copyfrom);
        }
+
        item->match = match;
+       item->len = strlen(item->match);
+       item->prefix = prefixlen;
+
        /*
         * Prefix the pathspec (keep all magic) and assign to
         * original. Useful for passing to another command.
@@ -323,44 +383,21 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
        } else {
                item->original = xstrdup(elt);
        }
-       item->len = strlen(item->match);
-       item->prefix = prefixlen;
 
-       if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
-           (item->len >= 1 && item->match[item->len - 1] == '/') &&
-           (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
-           S_ISGITLINK(active_cache[i]->ce_mode)) {
-               item->len--;
-               match[item->len] = '\0';
-       }
+       if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
+               strip_submodule_slash_cheap(item);
 
        if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
-               for (i = 0; i < active_nr; i++) {
-                       struct cache_entry *ce = active_cache[i];
-                       int ce_len = ce_namelen(ce);
-
-                       if (!S_ISGITLINK(ce->ce_mode))
-                               continue;
-
-                       if (item->len <= ce_len || match[ce_len] != '/' ||
-                           memcmp(ce->name, match, ce_len))
-                               continue;
-                       if (item->len == ce_len + 1) {
-                               /* strip trailing slash */
-                               item->len--;
-                               match[item->len] = '\0';
-                       } else
-                               die (_("Pathspec '%s' is in submodule '%.*s'"),
-                                    elt, ce_len, ce->name);
-               }
+               strip_submodule_slash_expensive(item);
 
-       if (magic & PATHSPEC_LITERAL)
+       if (magic & PATHSPEC_LITERAL) {
                item->nowildcard_len = item->len;
-       else {
+       else {
                item->nowildcard_len = simple_length(item->match);
                if (item->nowildcard_len < prefixlen)
                        item->nowildcard_len = prefixlen;
        }
+
        item->flags = 0;
        if (magic & PATHSPEC_GLOB) {
                /*
@@ -375,9 +412,18 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
        }
 
        /* sanity checks, pathspec matchers assume these are sane */
-       assert(item->nowildcard_len <= item->len &&
-              item->prefix         <= item->len);
-       return magic;
+       if (item->nowildcard_len > item->len ||
+           item->prefix         > item->len) {
+               /*
+                * This case can be triggered by the user pointing us to a
+                * pathspec inside a submodule, which is an input error.
+                * Detect that here and complain, but fallback in the
+                * non-submodule case to a BUG, as we have no idea what
+                * would trigger that.
+                */
+               die_inside_submodule_path(item);
+               die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
+       }
 }
 
 static int pathspec_item_cmp(const void *a_, const void *b_)
@@ -477,8 +523,7 @@ void parse_pathspec(struct pathspec *pathspec,
        for (i = 0; i < n; i++) {
                entry = argv[i];
 
-               item[i].magic = prefix_pathspec(item + i, flags,
-                                               prefix, prefixlen, entry);
+               init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
 
                if (item[i].magic & PATHSPEC_EXCLUDE)
                        nr_exclude++;