object-file: disallow adding submodules of different hash algo

The design of the hash algorithm transition plan is that objects stored must be entirely in one algorithm since we lack any way to indicate a mix of algorithms. This also includes submodules, but we have traditionally not enforced this, which leads to various problems when trying to clone or check out the the submodule from the remote. Since this cannot work in the general case, restrict adding a submodule of a different algorithm to the index. Add tests for git add and git submodule add that these are rejected. Note that we cannot check this in git fsck because the malformed submodule is stored in the tree as an object ID which is either truncated (when a SHA-256 submodule is added to a SHA-1 repository) or padded with zeros (when a SHA-1 submodule is added to a SHA-256 repository). We cannot detect even the latter case because someone could have an actual submodule that actually ends in 24 zeros, which would be a false positive. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Nov 15, 2025 at 00:58 UTC 66c78e0653a4e60c625b8400da31da0ba5bd1286
3 files changed +55 -1
object-file.c
+5 -1
@@ -1296,7 +1296,11 @@ int index_path(struct index_state *istate, struct object_id *oid,
1296 strbuf_release(&sb);
1297 break;
1298 case S_IFDIR:
1299 - return repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid);
1299 + if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
1300 + return -1;
1301 + if (&hash_algos[oid->algo] != istate->repo->hash_algo)
1302 + return error(_("cannot add a submodule of a different hash algorithm"));
1303 + break;
1304 default:
1305 return error(_("%s: unsupported file type"), path);
1306 }
t/t3700-add.sh
+25
@@ -541,6 +541,31 @@ test_expect_success 'all statuses changed in folder if . is given' '
541 )
542 '
543
544 +test_expect_success 'cannot add a submodule of a different algorithm' '
545 + git init --object-format=sha256 sha256 &&
546 + (
547 + cd sha256 &&
548 + test_commit abc &&
549 + git init --object-format=sha1 submodule &&
550 + test_commit -C submodule def &&
551 + test_must_fail git add submodule 2>err &&
552 + test_grep "cannot add a submodule of a different hash algorithm" err &&
553 + git ls-files --stage >entries &&
554 + test_grep ! ^160000 entries
555 + ) &&
556 + git init --object-format=sha1 sha1 &&
557 + (
558 + cd sha1 &&
559 + test_commit abc &&
560 + git init --object-format=sha256 submodule &&
561 + test_commit -C submodule def &&
562 + test_must_fail git add submodule 2>err &&
563 + test_grep "cannot add a submodule of a different hash algorithm" err &&
564 + git ls-files --stage >entries &&
565 + test_grep ! ^160000 entries
566 + )
567 +'
568 +
569 test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
570 path="$(pwd)/BLUB" &&
571 touch "$path" &&
t/t7400-submodule-basic.sh
+25
@@ -407,6 +407,31 @@ test_expect_success 'submodule add in subdirectory with relative path should fai
407 test_grep toplevel output.err
408 '
409
410 +test_expect_success 'submodule add of a different algorithm fails' '
411 + git init --object-format=sha256 sha256 &&
412 + (
413 + cd sha256 &&
414 + test_commit abc &&
415 + git init --object-format=sha1 submodule &&
416 + test_commit -C submodule def &&
417 + test_must_fail git submodule add "$submodurl" submodule 2>err &&
418 + test_grep "cannot add a submodule of a different hash algorithm" err &&
419 + git ls-files --stage >entries &&
420 + test_grep ! ^160000 entries
421 + ) &&
422 + git init --object-format=sha1 sha1 &&
423 + (
424 + cd sha1 &&
425 + test_commit abc &&
426 + git init --object-format=sha256 submodule &&
427 + test_commit -C submodule def &&
428 + test_must_fail git submodule add "$submodurl" submodule 2>err &&
429 + test_grep "cannot add a submodule of a different hash algorithm" err &&
430 + git ls-files --stage >entries &&
431 + test_grep ! ^160000 entries
432 + )
433 +'
434 +
435 test_expect_success 'setup - add an example entry to .gitmodules' '
436 git config --file=.gitmodules submodule.example.url git://example.com/init.git
437 '