[PATCH v4 4/4] submodule: fix case-folding gitdir filesystem colisions
- From
Adrian Ratiu <adrian.ratiu@collabora.com>
- Date
- Nov 7, 2025, 15:05 UTC
- Message-ID
- <20251107150547.3272180-5-adrian.ratiu@collabora.com>
- In-Reply-To
- <20251107150547.3272180-1-adrian.ratiu@collabora.com>
Add a new check in validate_submodule_git_dir() to detect and prevent case-folding filesystem colisions. When this new check is triggered, a stricter casefolding aware URI encoding is used to percent-encode uppercase characters, e.g. Foo becomes %46oo.
By using this check/retry mechanism the uppercase encoding is only applied when necessary, so case-sensitive filesystems are not affected.
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> --- submodule.c | 47 +++++++++++++++++++++++++++++++++-- t/t7425-submodule-encoding.sh | 15 +++++++++++ url.c | 12 +++++++++ url.h | 1 + 4 files changed, 73 insertions(+), 2 deletions(-)
diff --git a/submodule.c b/submodule.c index ceaff0c1aa..ecbffac2c6 100644 --- a/submodule.c +++ b/submodule.c @@ -2280,7 +2280,7 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name) size_t len = strlen(git_dir), suffix_len = strlen(submodule_name); char *p = git_dir + len - suffix_len; bool suffixes_match = !strcmp(p, submodule_name); - int ret = 0; + int ret = 0, config_ignorecase = 0; /* * We prevent the contents of sibling submodules' git directories to @@ -2318,6 +2318,42 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name) if (p && strchr(p, '/') != NULL) return error("submodule gitdir name '%s' contains unexpected '/'", p); + /* Prevent conflicts on case-folding filesystems */ + repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase); + if (ignore_case || config_ignorecase) { + char *lower_gitdir = xstrdup(git_dir); + char *module_name = find_last_submodule_name(lower_gitdir); + + if (module_name) { + for (p = module_name; *p; p++) + *p = tolower(*p); + + /* + * If lower path is different and already exists, check for collision. + * Intentionally double-check to eliminate false-positives. + */ + if (strcmp(lower_gitdir, git_dir) && is_git_directory(lower_gitdir)) { + char *canonical = real_pathdup(git_dir, 0); + if (canonical) { + struct strbuf norm_git_dir = STRBUF_INIT; + strbuf_addstr(&norm_git_dir, git_dir); + strbuf_normalize_path(&norm_git_dir); + + if (strcmp(canonical, norm_git_dir.buf)) + ret = error(_("submodule git dir '%s' " + "collides with '%s'"), + canonical, norm_git_dir.buf); + + strbuf_release(&norm_git_dir); + FREE_AND_NULL(canonical); + } + } + } + + FREE_AND_NULL(lower_gitdir); + return ret; + } + return 0; } @@ -2653,13 +2689,20 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, if (!validate_and_set_submodule_gitdir(buf, submodule_name)) return; - /* Case 2: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ + /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ strbuf_reset(buf); repo_git_path_append(r, buf, "modules/"); strbuf_addstr_urlencode(buf, submodule_name, is_rfc3986_unreserved); if (!validate_and_set_submodule_gitdir(buf, submodule_name)) return; + /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */ + strbuf_reset(buf); + repo_git_path_append(r, buf, "modules/"); + strbuf_addstr_urlencode(buf, submodule_name, is_casefolding_rfc3986_unreserved); + if (!validate_and_set_submodule_gitdir(buf, submodule_name)) + return; + /* Case 3: error out */ die(_("Cannot construct a valid gitdir path for submodule '%s': " "please set a unique git config for 'submodule.%s.gitdir'."), diff --git a/t/t7425-submodule-encoding.sh b/t/t7425-submodule-encoding.sh index a42d358f5b..f92b3e6338 100755 --- a/t/t7425-submodule-encoding.sh +++ b/t/t7425-submodule-encoding.sh @@ -143,4 +143,19 @@ test_expect_success 'submodule git dir nesting detection must work with parallel verify_submodule_gitdir_path clone_parallel hippo/hooks modules/hippo%2fhooks ' +test_expect_success 'verify case-folding conflict is correctly encoded' ' + git clone -c extensions.submoduleEncoding=true -c core.ignoreCase=true main cloned-folding && + ( + cd cloned-folding && + + git submodule add ../new-sub "folding" && + test_commit lowercase && + + git submodule add ../new-sub "FoldinG" && + test_commit uppercase + ) && + verify_submodule_gitdir_path cloned-folding "folding" "modules/folding" && + verify_submodule_gitdir_path cloned-folding "FoldinG" "modules/%46oldin%47" +' + test_done diff --git a/url.c b/url.c index 0fb1859b28..057e6e5c6e 100644 --- a/url.c +++ b/url.c @@ -14,6 +14,18 @@ int is_rfc3986_unreserved(char ch) ch == '-' || ch == '_' || ch == '.' || ch == '~'; } +/* + * This is a variant of is_rfc3986_unreserved() that treats uppercase + * letters as "reserved". This forces them to be percent-encoded, allowing + * 'Foo' (%46oo) and 'foo' (foo) to be distinct on case-folding filesystems. + */ +int is_casefolding_rfc3986_unreserved(char c) +{ + return (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + c == '-' || c == '.' || c == '_' || c == '~'; +} + int is_urlschemechar(int first_flag, int ch) { /* diff --git a/url.h b/url.h index 131a262066..92e3c63514 100644 --- a/url.h +++ b/url.h @@ -22,5 +22,6 @@ void end_url_with_slash(struct strbuf *buf, const char *url); void str_end_url_with_slash(const char *url, char **dest); int is_rfc3986_unreserved(char ch); +int is_casefolding_rfc3986_unreserved(char c); #endif /* URL_H */
-- 2.51.0