]> git.ipfire.org Git - thirdparty/git.git/blobdiff - pathspec.c
Merge branch 'ws/request-pull-code-cleanup'
[thirdparty/git.git] / pathspec.c
index 77df55da6a2c4c03fddab40c4fb2b06799d64a6f..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)
@@ -157,96 +157,190 @@ static int get_global_magic(int element_magic)
 }
 
 /*
- * Take an element of a pathspec and check for magic signatures.
- * Append the result to the prefix. Return the magic bitmap.
+ * Parse the pathspec element looking for long magic
  *
- * For now, we only parse the syntax and throw out anything other than
- * "top" magic.
+ * saves all magic in 'magic'
+ * if prefix magic is used, save the prefix length in 'prefix_len'
+ * returns the position in 'elem' after all magic has been parsed
+ */
+static const char *parse_long_magic(unsigned *magic, int *prefix_len,
+                                   const char *elem)
+{
+       const char *pos;
+       const char *nextat;
+
+       for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
+               size_t len = strcspn(pos, ",)");
+               int i;
+
+               if (pos[len] == ',')
+                       nextat = pos + len + 1; /* handle ',' */
+               else
+                       nextat = pos + len; /* handle ')' and '\0' */
+
+               if (!len)
+                       continue;
+
+               if (starts_with(pos, "prefix:")) {
+                       char *endptr;
+                       *prefix_len = strtol(pos + 7, &endptr, 10);
+                       if (endptr - pos != len)
+                               die(_("invalid parameter for pathspec magic 'prefix'"));
+                       continue;
+               }
+
+               for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
+                       if (strlen(pathspec_magic[i].name) == len &&
+                           !strncmp(pathspec_magic[i].name, pos, len)) {
+                               *magic |= pathspec_magic[i].bit;
+                               break;
+                       }
+               }
+
+               if (ARRAY_SIZE(pathspec_magic) <= i)
+                       die(_("Invalid pathspec magic '%.*s' in '%s'"),
+                           (int) len, pos, elem);
+       }
+
+       if (*pos != ')')
+               die(_("Missing ')' at the end of pathspec magic in '%s'"),
+                   elem);
+       pos++;
+
+       return pos;
+}
+
+/*
+ * Parse the pathspec element looking for short 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.
+ * saves all magic in 'magic'
+ * returns the position in 'elem' after all magic has been parsed
  */
-static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
-                               const char *prefix, int prefixlen,
-                               const char *elt)
+static const char *parse_short_magic(unsigned *magic, const char *elem)
 {
-       unsigned magic = 0, element_magic = 0;
-       const char *copyfrom = elt;
-       char *match;
-       int i, pathspec_prefix = -1;
+       const char *pos;
 
-       if (elt[0] != ':' || get_literal_global() ||
-           (flags & PATHSPEC_LITERAL_PATH)) {
-               ; /* nothing to do */
-       } else if (elt[1] == '(') {
-               /* longhand */
-               const char *nextat;
-               for (copyfrom = elt + 2;
-                    *copyfrom && *copyfrom != ')';
-                    copyfrom = nextat) {
-                       size_t len = strcspn(copyfrom, ",)");
-                       if (copyfrom[len] == ',')
-                               nextat = copyfrom + len + 1;
-                       else
-                               /* handle ')' and '\0' */
-                               nextat = copyfrom + len;
-                       if (!len)
-                               continue;
-                       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
-                               if (strlen(pathspec_magic[i].name) == len &&
-                                   !strncmp(pathspec_magic[i].name, copyfrom, len)) {
-                                       element_magic |= pathspec_magic[i].bit;
-                                       break;
-                               }
-                               if (starts_with(copyfrom, "prefix:")) {
-                                       char *endptr;
-                                       pathspec_prefix = strtol(copyfrom + 7,
-                                                                &endptr, 10);
-                                       if (endptr - copyfrom != len)
-                                               die(_("invalid parameter for pathspec magic 'prefix'"));
-                                       /* "i" would be wrong, but it does not matter */
-                                       break;
-                               }
+       for (pos = elem + 1; *pos && *pos != ':'; pos++) {
+               char ch = *pos;
+               int i;
+
+               if (!is_pathspec_magic(ch))
+                       break;
+
+               for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
+                       if (pathspec_magic[i].mnemonic == ch) {
+                               *magic |= pathspec_magic[i].bit;
+                               break;
                        }
-                       if (ARRAY_SIZE(pathspec_magic) <= i)
-                               die(_("Invalid pathspec magic '%.*s' in '%s'"),
-                                   (int) len, copyfrom, elt);
                }
-               if (*copyfrom != ')')
-                       die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
-               copyfrom++;
-       } else {
+
+               if (ARRAY_SIZE(pathspec_magic) <= i)
+                       die(_("Unimplemented pathspec magic '%c' in '%s'"),
+                           ch, elem);
+       }
+
+       if (*pos == ':')
+               pos++;
+
+       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 */
-               for (copyfrom = elt + 1;
-                    *copyfrom && *copyfrom != ':';
-                    copyfrom++) {
-                       char ch = *copyfrom;
+               return parse_short_magic(magic, elem);
+}
 
-                       if (!is_pathspec_magic(ch))
-                               break;
-                       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
-                               if (pathspec_magic[i].mnemonic == ch) {
-                                       element_magic |= pathspec_magic[i].bit;
-                                       break;
-                               }
-                       if (ARRAY_SIZE(pathspec_magic) <= i)
-                               die(_("Unimplemented pathspec magic '%c' in '%s'"),
-                                   ch, elt);
+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';
                }
-               if (*copyfrom == ':')
-                       copyfrom++;
        }
+}
 
-       magic |= element_magic;
+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);
+       }
+}
+
+/*
+ * Perform the initialization of a pathspec_item based on a pathspec element.
+ */
+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 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)))
@@ -255,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;
@@ -262,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.
@@ -283,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) {
                /*
@@ -335,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_)
@@ -437,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++;