pathspec: remove unused variable from unsupported_magic
[gitweb.git] / pathspec.c
index 49a53607bb004e8ccc34e6a18eadc8dfc2e7879d..b9a3819d65c24d4b67a9cabbb38dfc8002294ab5 100644 (file)
@@ -103,7 +103,7 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,
  */
 static unsigned prefix_pathspec(struct pathspec_item *item,
                                unsigned *p_short_magic,
-                               const char **raw, unsigned flags,
+                               unsigned flags,
                                const char *prefix, int prefixlen,
                                const char *elt)
 {
@@ -240,7 +240,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
                if (!match)
                        die(_("%s: '%s' is outside repository"), elt, copyfrom);
        }
-       *raw = item->match = match;
+       item->match = match;
        /*
         * Prefix the pathspec (keep all magic) and assign to
         * original. Useful for passing to another command.
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
                }
                strbuf_addstr(&sb, match);
                item->original = strbuf_detach(&sb, NULL);
-       } else
-               item->original = elt;
+       } else {
+               item->original = xstrdup(elt);
+       }
        item->len = strlen(item->match);
        item->prefix = prefixlen;
 
@@ -332,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern,
                                       unsigned short_magic)
 {
        struct strbuf sb = STRBUF_INIT;
-       int i, n;
-       for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
+       int i;
+       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
                const struct pathspec_magic *m = pathspec_magic + i;
                if (!(magic & m->bit))
                        continue;
@@ -343,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,
                        strbuf_addf(&sb, "'%c'", m->mnemonic);
                else
                        strbuf_addf(&sb, "'%s'", m->name);
-               n++;
        }
        /*
         * We may want to substitute "this command" with a command
@@ -364,7 +364,7 @@ void parse_pathspec(struct pathspec *pathspec,
 {
        struct pathspec_item *item;
        const char *entry = argv ? *argv : NULL;
-       int i, n, prefixlen, nr_exclude = 0;
+       int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
 
        memset(pathspec, 0, sizeof(*pathspec));
 
@@ -381,8 +381,6 @@ void parse_pathspec(struct pathspec *pathspec,
 
        /* No arguments with prefix -> prefix pathspec */
        if (!entry) {
-               static const char *raw[2];
-
                if (flags & PATHSPEC_PREFER_FULL)
                        return;
 
@@ -390,25 +388,28 @@ void parse_pathspec(struct pathspec *pathspec,
                        die("BUG: PATHSPEC_PREFER_CWD requires arguments");
 
                pathspec->items = item = xcalloc(1, sizeof(*item));
-               item->match = prefix;
-               item->original = prefix;
+               item->match = xstrdup(prefix);
+               item->original = xstrdup(prefix);
                item->nowildcard_len = item->len = strlen(prefix);
                item->prefix = item->len;
-               raw[0] = prefix;
-               raw[1] = NULL;
                pathspec->nr = 1;
-               pathspec->_raw = raw;
                return;
        }
 
        n = 0;
-       while (argv[n])
+       warn_empty_string = 1;
+       while (argv[n]) {
+               if (*argv[n] == '\0' && warn_empty_string) {
+                       warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
+                                 "please use . instead if you meant to match all paths"));
+                       warn_empty_string = 0;
+               }
                n++;
+       }
 
        pathspec->nr = n;
        ALLOC_ARRAY(pathspec->items, n);
        item = pathspec->items;
-       pathspec->_raw = argv;
        prefixlen = prefix ? strlen(prefix) : 0;
 
        for (i = 0; i < n; i++) {
@@ -416,7 +417,7 @@ void parse_pathspec(struct pathspec *pathspec,
                entry = argv[i];
 
                item[i].magic = prefix_pathspec(item + i, &short_magic,
-                                               argv + i, flags,
+                                               flags,
                                                prefix, prefixlen, entry);
                if ((flags & PATHSPEC_LITERAL_PATH) &&
                    !(magic_mask & PATHSPEC_LITERAL))
@@ -446,50 +447,33 @@ void parse_pathspec(struct pathspec *pathspec,
        if (pathspec->magic & PATHSPEC_MAXDEPTH) {
                if (flags & PATHSPEC_KEEP_ORDER)
                        die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
-               qsort(pathspec->items, pathspec->nr,
-                     sizeof(struct pathspec_item), pathspec_item_cmp);
+               QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
        }
 }
 
-/*
- * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
- * based interface - see pathspec.c:parse_pathspec().
- *
- * Arguments:
- *  - prefix - a path relative to the root of the working tree
- *  - pathspec - a list of paths underneath the prefix path
- *
- * Iterates over pathspec, prepending each path with prefix,
- * and return the resulting list.
- *
- * If pathspec is empty, return a singleton list containing prefix.
- *
- * If pathspec and prefix are both empty, return an empty list.
- *
- * This is typically used by built-in commands such as add.c, in order
- * to normalize argv arguments provided to the built-in into a list of
- * paths to process, all relative to the root of the working tree.
- */
-const char **get_pathspec(const char *prefix, const char **pathspec)
-{
-       struct pathspec ps;
-       parse_pathspec(&ps,
-                      PATHSPEC_ALL_MAGIC &
-                      ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
-                      PATHSPEC_PREFER_CWD,
-                      prefix, pathspec);
-       return ps._raw;
-}
-
 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
 {
+       int i;
+
        *dst = *src;
        ALLOC_ARRAY(dst->items, dst->nr);
        COPY_ARRAY(dst->items, src->items, dst->nr);
+
+       for (i = 0; i < dst->nr; i++) {
+               dst->items[i].match = xstrdup(src->items[i].match);
+               dst->items[i].original = xstrdup(src->items[i].original);
+       }
 }
 
 void clear_pathspec(struct pathspec *pathspec)
 {
+       int i;
+
+       for (i = 0; i < pathspec->nr; i++) {
+               free(pathspec->items[i].match);
+               free(pathspec->items[i].original);
+       }
        free(pathspec->items);
        pathspec->items = NULL;
+       pathspec->nr = 0;
 }