submodule: add die_in_unpopulated_submodule function
authorBrandon Williams <bmwill@google.com>
Tue, 9 May 2017 19:17:59 +0000 (12:17 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 May 2017 05:47:39 +0000 (14:47 +0900)
Currently 'git add' is the only command which dies when launched from an
unpopulated submodule (the place-holder directory for a submodule which
hasn't been checked out). This is triggered implicitly by passing the
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE flag to 'parse_pathspec()'.

Instead make this desire more explicit by creating a function
'die_in_unpopulated_submodule()' which dies if the provided 'prefix' has
a leading path component which matches a submodule in the the index.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/add.c
pathspec.c
submodule.c
submodule.h
t/t6134-pathspec-in-submodule.sh
index 9f53f020d0fc7184ac803a4346bdf426a814ed72..ec58e3679665a5d4a1f8375cb3d15a5c140dafad 100644 (file)
@@ -17,6 +17,7 @@
 #include "revision.h"
 #include "bulk-checkin.h"
 #include "argv-array.h"
+#include "submodule.h"
 
 static const char * const builtin_add_usage[] = {
        N_("git add [<options>] [--] <pathspec>..."),
@@ -379,6 +380,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        if (read_cache() < 0)
                die(_("index file corrupt"));
 
+       die_in_unpopulated_submodule(&the_index, prefix);
+
        /*
         * Check the "pathspec '%s' did not match any files" block
         * below before enabling new magic.
index 904cfb739378bb8d8efc8fdb4327eb0c251bc181..9b7634c5aa8ff9c1671247f612164a787d6c4f56 100644 (file)
@@ -424,27 +424,6 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item)
        }
 }
 
-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.
  */
@@ -547,14 +526,6 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
        /* sanity checks, pathspec matchers assume these are sane */
        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: error initializing pathspec_item");
        }
 }
index d3299e29c0855b8503f444f659e2b24bba8ecb1f..885663c4212577383e93655f5b95ea1dd2913709 100644 (file)
@@ -282,6 +282,36 @@ int is_submodule_populated_gently(const char *path, int *return_error_code)
        return ret;
 }
 
+/*
+ * Dies if the provided 'prefix' corresponds to an unpopulated submodule
+ */
+void die_in_unpopulated_submodule(const struct index_state *istate,
+                                 const char *prefix)
+{
+       int i, prefixlen;
+
+       if (!prefix)
+               return;
+
+       prefixlen = strlen(prefix);
+
+       for (i = 0; i < istate->cache_nr; i++) {
+               struct cache_entry *ce = istate->cache[i];
+               int ce_len = ce_namelen(ce);
+
+               if (!S_ISGITLINK(ce->ce_mode))
+                       continue;
+               if (prefixlen <= ce_len)
+                       continue;
+               if (strncmp(ce->name, prefix, ce_len))
+                       continue;
+               if (prefix[ce_len] != '/')
+                       continue;
+
+               die(_("in unpopulated submodule '%s'"), ce->name);
+       }
+}
+
 int parse_submodule_update_strategy(const char *value,
                struct submodule_update_strategy *dst)
 {
index 1277480add48140c6bf8c6cbc51cb962e27637ae..d11b4da40f66331142e9b50fe2bf30cab0bb91f4 100644 (file)
@@ -49,6 +49,8 @@ extern int is_submodule_initialized(const char *path);
  * Otherwise the return error code is the same as of resolve_gitdir_gently.
  */
 extern int is_submodule_populated_gently(const char *path, int *return_error_code);
+extern void die_in_unpopulated_submodule(const struct index_state *istate,
+                                        const char *prefix);
 extern int parse_submodule_update_strategy(const char *value,
                struct submodule_update_strategy *dst);
 extern const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
index fd401ca605681a1586aa29bd83fe51004bd5c1ab..0f1cb49cedc645c5a9b0c56257d3aca4740d5cd3 100755 (executable)
@@ -24,13 +24,9 @@ test_expect_success 'error message for path inside submodule' '
        test_cmp expect actual
 '
 
-cat <<EOF >expect
-fatal: Pathspec '.' is in submodule 'sub'
-EOF
-
 test_expect_success 'error message for path inside submodule from within submodule' '
        test_must_fail git -C sub add . 2>actual &&
-       test_cmp expect actual
+       test_i18ngrep "in unpopulated submodule" actual
 '
 
 test_done