]> git.ipfire.org Git - thirdparty/git.git/commitdiff
submodule--helper: fix filesystem collisions by encoding gitdir paths
authorAdrian Ratiu <adrian.ratiu@collabora.com>
Sat, 20 Dec 2025 10:15:25 +0000 (12:15 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 21 Dec 2025 02:36:01 +0000 (11:36 +0900)
Fix nested filesystem collisions by url-encoding gitdir paths stored
in submodule.%s.gitdir, when extensions.submodulePathConfig is enabled.

Credit goes to Junio and Patrick for coming up with this design: the
encoding is only applied when necessary, to newly added submodules.

Existing modules don't need the encoding because git already errors
out when detecting nested gitdirs before this patch.

This commit adds the basic url-encoding and some tests. Next commits
extend the encode -> validate -> retry loop to fix more conflicts.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Suggested-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
submodule.c
t/t7425-submodule-gitdir-path-extension.sh

index 314c79d5c2021ebbe1aec1e826b616f4bafeace6..f19672f01a609cb5aa8f356d06b70d40712d2885 100644 (file)
@@ -34,6 +34,7 @@
 #include "list-objects-filter-options.h"
 #include "wildmatch.h"
 #include "strbuf.h"
+#include "url.h"
 
 #define OPT_QUIET (1 << 0)
 #define OPT_CACHED (1 << 1)
@@ -465,12 +466,23 @@ static void create_default_gitdir_config(const char *submodule_name)
 {
        struct strbuf gitdir_path = STRBUF_INIT;
 
+       /* Case 1: try the plain module name */
        repo_git_path_append(the_repository, &gitdir_path, "modules/%s", submodule_name);
        if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {
                strbuf_release(&gitdir_path);
                return;
        }
 
+       /* Case 2: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
+       strbuf_reset(&gitdir_path);
+       repo_git_path_append(the_repository, &gitdir_path, "modules/");
+       strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);
+       if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {
+               strbuf_release(&gitdir_path);
+               return;
+       }
+
+       /* Case 3: nothing worked, error out */
        die(_("failed to set a valid default config for 'submodule.%s.gitdir'. "
              "Please ensure it is set, for example by running something like: "
              "'git config submodule.%s.gitdir .git/modules/%s'"),
index 2e644ec2da2cd84e6a8db291a97e8b3a80653105..0dc1fedcae56c9526905210db83397d265325dc7 100644 (file)
@@ -31,6 +31,7 @@
 #include "commit-reach.h"
 #include "read-cache-ll.h"
 #include "setup.h"
+#include "url.h"
 
 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
 static int initialized_fetch_ref_tips;
@@ -2252,12 +2253,43 @@ out:
        return ret;
 }
 
-int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
+/*
+ * Encoded gitdir validation, only used when extensions.submodulePathConfig is enabled.
+ * This does not print errors like the non-encoded version, because encoding is supposed
+ * to mitigate / fix all these.
+ */
+static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name UNUSED)
+{
+       const char *modules_marker = "/modules/";
+       char *p = git_dir, *last_submodule_name = NULL;
+
+       if (!the_repository->repository_format_submodule_path_cfg)
+               BUG("validate_submodule_encoded_git_dir() must be called with "
+                   "extensions.submodulePathConfig enabled.");
+
+       /* Find the last submodule name in the gitdir path (modules can be nested). */
+       while ((p = strstr(p, modules_marker))) {
+               last_submodule_name = p + strlen(modules_marker);
+               p++;
+       }
+
+       /* Prevent the use of '/' in encoded names */
+       if (!last_submodule_name || strchr(last_submodule_name, '/'))
+               return -1;
+
+       return 0;
+}
+
+static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodule_name)
 {
        size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
        char *p;
        int ret = 0;
 
+       if (the_repository->repository_format_submodule_path_cfg)
+               BUG("validate_submodule_git_dir() must be called with "
+                   "extensions.submodulePathConfig disabled.");
+
        if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
            strcmp(p, submodule_name))
                BUG("submodule name '%s' not a suffix of git dir '%s'",
@@ -2293,6 +2325,14 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
        return 0;
 }
 
+int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
+{
+       if (!the_repository->repository_format_submodule_path_cfg)
+               return validate_submodule_legacy_git_dir(git_dir, submodule_name);
+
+       return validate_submodule_encoded_git_dir(git_dir, submodule_name);
+}
+
 int validate_submodule_path(const char *path)
 {
        char *p = xstrdup(path);
index 6ca9f13a598fef27648eb28782592145d91084a4..dbe18f2925dca35159b1dbeb129a7d91d0f64838 100755 (executable)
@@ -327,4 +327,61 @@ test_expect_success '`git clone --recurse-submodules` works after migration' '
        )
 '
 
+test_expect_success 'setup submodules with nested git dirs' '
+       git init nested &&
+       test_commit -C nested nested &&
+       (
+               cd nested &&
+               cat >.gitmodules <<-EOF &&
+               [submodule "hippo"]
+                       url = .
+                       path = thing1
+               [submodule "hippo/hooks"]
+                       url = .
+                       path = thing2
+               EOF
+               git clone . thing1 &&
+               git clone . thing2 &&
+               git add .gitmodules thing1 thing2 &&
+               test_tick &&
+               git commit -m nested
+       )
+'
+
+test_expect_success 'git dirs of encoded sibling submodules must not be nested' '
+       git clone -c extensions.submodulePathConfig=true --recurse-submodules nested clone_nested &&
+
+       verify_submodule_gitdir_path clone_nested hippo modules/hippo &&
+       git -C clone_nested config submodule.hippo.gitdir > actual &&
+       test_grep "\.git/modules/hippo$" actual &&
+
+       verify_submodule_gitdir_path clone_nested hippo/hooks modules/hippo%2fhooks &&
+       git -C clone_nested config submodule.hippo/hooks.gitdir > actual &&
+       test_grep "\.git/modules/hippo%2fhooks$" actual
+'
+
+test_expect_success 'submodule git dir nesting detection must work with parallel cloning' '
+       git clone -c extensions.submodulePathConfig=true --recurse-submodules --jobs=2 nested clone_parallel &&
+
+       verify_submodule_gitdir_path clone_parallel hippo modules/hippo &&
+       git -C clone_nested config submodule.hippo.gitdir > actual &&
+       test_grep "\.git/modules/hippo$" actual &&
+
+       verify_submodule_gitdir_path clone_parallel hippo/hooks modules/hippo%2fhooks &&
+       git -C clone_nested config submodule.hippo/hooks.gitdir > actual &&
+       test_grep "\.git/modules/hippo%2fhooks$" actual
+'
+
+test_expect_success 'disabling extensions.submodulePathConfig prevents nested submodules' '
+       (
+               cd clone_nested &&
+               # disable extension and verify failure
+               git config --replace-all extensions.submodulePathConfig false &&
+               test_must_fail git submodule add ./thing2 hippo/foobar &&
+               # re-enable extension and verify it works
+               git config --replace-all extensions.submodulePathConfig true &&
+               git submodule add ./thing2 hippo/foobar
+       )
+'
+
 test_done