Skip to content

Commit cba29b8

Browse files
committed
Merge branch 'bc/submodule-force-same-hash' into seen
Adding a repository that uses a different hash function is a no-no, but "git submodule add" did nt prevent it, which has been corrected. * bc/submodule-force-same-hash: object-file: disallow adding submodules of different hash algo
2 parents 133bf92 + c59e51c commit cba29b8

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
@@ -1620,7 +1620,11 @@ int index_path(struct index_state *istate, struct object_id *oid,
16201620
strbuf_release(&sb);
16211621
break;
16221622
case S_IFDIR:
1623-
return repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid);
1623+
if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
1624+
return -1;
1625+
if (&hash_algos[oid->algo] != istate->repo->hash_algo)
1626+
return error(_("cannot add a submodule of a different hash algorithm"));
1627+
break;
16241628
default:
16251629
return error(_("%s: unsupported file type"), path);
16261630
}

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)