diff options
| author | Adrian Ratiu <adrian.ratiu@collabora.com> | 2025-11-07 17:05:47 +0200 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2025-11-07 09:01:11 -0800 |
| commit | 12e839a497f0eaf69b2ab016ad03a53e44aefe4d (patch) | |
| tree | f5ab55e847a136d7df394ba65daa85d6d6de9de1 | |
| parent | submodule: add extension to encode gitdir paths (diff) | |
| download | git-12e839a497f0eaf69b2ab016ad03a53e44aefe4d.tar.gz git-12e839a497f0eaf69b2ab016ad03a53e44aefe4d.zip | |
submodule: fix case-folding gitdir filesystem colisions
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
| -rw-r--r-- | submodule.c | 47 | ||||
| -rwxr-xr-x | t/t7425-submodule-encoding.sh | 15 | ||||
| -rw-r--r-- | url.c | 12 | ||||
| -rw-r--r-- | 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 @@ -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) { /* @@ -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 */ |
