Skip to content

Commit c59e51c

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bb5c624 commit c59e51c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

object-file.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,11 @@ int index_path(struct index_state *istate, struct object_id *oid,
12961296
strbuf_release(&sb);
12971297
break;
12981298
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;
13001304
default:
13011305
return error(_("%s: unsupported file type"), path);
13021306
}

t/t3700-add.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,33 @@ test_expect_success 'all statuses changed in folder if . is given' '
541541
)
542542
'
543543

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+
(
551+
cd submodule &&
552+
test_commit def
553+
) &&
554+
test_must_fail git add submodule &&
555+
test $(git ls-files --stage | grep ^160000 | wc -l) -eq 0
556+
) &&
557+
git init --object-format=sha1 sha1 &&
558+
(
559+
cd sha1 &&
560+
test_commit abc &&
561+
git init --object-format=sha256 submodule &&
562+
(
563+
cd submodule &&
564+
test_commit def
565+
) &&
566+
test_must_fail git add submodule &&
567+
test $(git ls-files --stage | grep ^160000 | wc -l) -eq 0
568+
)
569+
'
570+
544571
test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
545572
path="$(pwd)/BLUB" &&
546573
touch "$path" &&

t/t7400-submodule-basic.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,33 @@ test_expect_success 'submodule add in subdirectory with relative path should fai
407407
test_grep toplevel output.err
408408
'
409409

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+
(
417+
cd submodule &&
418+
test_commit def
419+
) &&
420+
test_must_fail git submodule add "$submodurl" submodule &&
421+
test $(git ls-files --stage | grep ^160000 | wc -l) -eq 0
422+
) &&
423+
git init --object-format=sha1 sha1 &&
424+
(
425+
cd sha1 &&
426+
test_commit abc &&
427+
git init --object-format=sha256 submodule &&
428+
(
429+
cd submodule &&
430+
test_commit def
431+
) &&
432+
test_must_fail git submodule add "$submodurl" submodule &&
433+
test $(git ls-files --stage | grep ^160000 | wc -l) -eq 0
434+
)
435+
'
436+
410437
test_expect_success 'setup - add an example entry to .gitmodules' '
411438
git config --file=.gitmodules submodule.example.url git://example.com/init.git
412439
'

0 commit comments

Comments
 (0)