submodule: fix case-folding gitdir filesystem collisions

Add a new check when extension.submodulePathConfig is enabled, 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. 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>

Adrian Ratiu committed Jan 12, 2026 at 20:46 UTC 1685bba838ace8b4e325616ab914a6b01f18547f
5 files changed +126 -2
builtin/submodule--helper.c
+25 -1
@@ -473,7 +473,7 @@ static void create_default_gitdir_config(const char *submodule_name)
473 return;
474 }
475
476 - /* Case 2: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
476 + /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
477 strbuf_reset(&gitdir_path);
478 repo_git_path_append(the_repository, &gitdir_path, "modules/");
479 strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);
@@ -482,6 +482,30 @@ static void create_default_gitdir_config(const char *submodule_name)
482 return;
483 }
484
485 + /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */
486 + strbuf_reset(&gitdir_path);
487 + repo_git_path_append(the_repository, &gitdir_path, "modules/");
488 + strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);
489 + if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
490 + return;
491 +
492 + /* Case 2.3: Try some derived gitdir names, see if one sticks */
493 + for (char c = '0'; c <= '9'; c++) {
494 + strbuf_reset(&gitdir_path);
495 + repo_git_path_append(the_repository, &gitdir_path, "modules/");
496 + strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);
497 + strbuf_addch(&gitdir_path, c);
498 + if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
499 + return;
500 +
501 + strbuf_reset(&gitdir_path);
502 + repo_git_path_append(the_repository, &gitdir_path, "modules/");
503 + strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);
504 + strbuf_addch(&gitdir_path, c);
505 + if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
506 + return;
507 + }
508 +
509 /* Case 3: nothing worked, error out */
510 die(_("failed to set a valid default config for 'submodule.%s.gitdir'. "
511 "Please ensure it is set, for example by running something like: "
submodule.c
+52 -1
@@ -2254,15 +2254,58 @@ out:
2254 return ret;
2255 }
2256
2257 +static int check_casefolding_conflict(const char *git_dir,
2258 + const char *submodule_name,
2259 + const bool suffixes_match)
2260 +{
2261 + char *p, *modules_dir = xstrdup(git_dir);
2262 + struct dirent *de;
2263 + DIR *dir = NULL;
2264 + int ret = 0;
2265 +
2266 + if ((p = find_last_dir_sep(modules_dir)))
2267 + *p = '\0';
2268 +
2269 + /* No conflict is possible if modules_dir doesn't exist (first clone) */
2270 + if (!is_directory(modules_dir))
2271 + goto cleanup;
2272 +
2273 + dir = opendir(modules_dir);
2274 + if (!dir) {
2275 + ret = -1;
2276 + goto cleanup;
2277 + }
2278 +
2279 + /* Check for another directory under .git/modules that differs only in case. */
2280 + while ((de = readdir(dir))) {
2281 + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2282 + continue;
2283 +
2284 + if ((suffixes_match || is_git_directory(git_dir)) &&
2285 + !strcasecmp(de->d_name, submodule_name) &&
2286 + strcmp(de->d_name, submodule_name)) {
2287 + ret = -1; /* collision found */
2288 + break;
2289 + }
2290 + }
2291 +
2292 +cleanup:
2293 + if (dir)
2294 + closedir(dir);
2295 + free(modules_dir);
2296 + return ret;
2297 +}
2298 +
2299 /*
2300 * Encoded gitdir validation, only used when extensions.submodulePathConfig is enabled.
2301 * This does not print errors like the non-encoded version, because encoding is supposed
2302 * to mitigate / fix all these.
2303 */
2262 -static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name UNUSED)
2304 +static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name)
2305 {
2306 const char *modules_marker = "/modules/";
2307 char *p = git_dir, *last_submodule_name = NULL;
2308 + int config_ignorecase = 0;
2309
2310 if (!the_repository->repository_format_submodule_path_cfg)
2311 BUG("validate_submodule_encoded_git_dir() must be called with "
@@ -2278,6 +2321,14 @@ static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodu
2321 if (!last_submodule_name || strchr(last_submodule_name, '/'))
2322 return -1;
2323
2324 + /* Prevent conflicts on case-folding filesystems */
2325 + repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase);
2326 + if (ignore_case || config_ignorecase) {
2327 + bool suffixes_match = !strcmp(last_submodule_name, submodule_name);
2328 + return check_casefolding_conflict(git_dir, submodule_name,
2329 + suffixes_match);
2330 + }
2331 +
2332 return 0;
2333 }
2334
t/t7425-submodule-gitdir-path-extension.sh
+35
@@ -403,4 +403,39 @@ test_expect_success 'disabling extensions.submodulePathConfig prevents nested su
403 )
404 '
405
406 +test_expect_success CASE_INSENSITIVE_FS 'verify case-folding conflicts are correctly encoded' '
407 + git clone -c extensions.submodulePathConfig=true main cloned-folding &&
408 + (
409 + cd cloned-folding &&
410 +
411 + # conflict: the "folding" gitdir will already be taken
412 + git submodule add ../new-sub "folding" &&
413 + test_commit lowercase &&
414 + git submodule add ../new-sub "FoldinG" &&
415 + test_commit uppercase &&
416 +
417 + # conflict: the "foo" gitdir will already be taken
418 + git submodule add ../new-sub "FOO" &&
419 + test_commit uppercase-foo &&
420 + git submodule add ../new-sub "foo" &&
421 + test_commit lowercase-foo &&
422 +
423 + # create a multi conflict between foobar, fooBar and foo%42ar
424 + # the "foo" gitdir will already be taken
425 + git submodule add ../new-sub "foobar" &&
426 + test_commit lowercase-foobar &&
427 + git submodule add ../new-sub "foo%42ar" &&
428 + test_commit encoded-foo%42ar &&
429 + git submodule add ../new-sub "fooBar" &&
430 + test_commit mixed-fooBar
431 + ) &&
432 + verify_submodule_gitdir_path cloned-folding "folding" "modules/folding" &&
433 + verify_submodule_gitdir_path cloned-folding "FoldinG" "modules/%46oldin%47" &&
434 + verify_submodule_gitdir_path cloned-folding "FOO" "modules/FOO" &&
435 + verify_submodule_gitdir_path cloned-folding "foo" "modules/foo0" &&
436 + verify_submodule_gitdir_path cloned-folding "foobar" "modules/foobar" &&
437 + verify_submodule_gitdir_path cloned-folding "foo%42ar" "modules/foo%42ar" &&
438 + verify_submodule_gitdir_path cloned-folding "fooBar" "modules/fooBar0"
439 +'
440 +
441 test_done
url.c
+7
@@ -9,6 +9,13 @@ int is_rfc3986_unreserved(char ch)
9 ch == '-' || ch == '_' || ch == '.' || ch == '~';
10 }
11
12 +int is_casefolding_rfc3986_unreserved(char c)
13 +{
14 + return (c >= 'a' && c <= 'z') ||
15 + (c >= '0' && c <= '9') ||
16 + c == '-' || c == '.' || c == '_' || c == '~';
17 +}
18 +
19 int is_urlschemechar(int first_flag, int ch)
20 {
21 /*
url.h
+7
@@ -28,4 +28,11 @@ void str_end_url_with_slash(const char *url, char **dest);
28 */
29 int is_rfc3986_unreserved(char ch);
30
31 +/*
32 + * This is a variant of is_rfc3986_unreserved() that treats uppercase
33 + * letters as "reserved". This forces them to be percent-encoded, allowing
34 + * 'Foo' (%46oo) and 'foo' (foo) to be distinct on case-folding filesystems.
35 + */
36 +int is_casefolding_rfc3986_unreserved(char c);
37 +
38 #endif /* URL_H */