Skip to content

Commit 66c78e0

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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bb5c624 commit 66c78e0

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,31 @@ 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+
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+
544569
test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
545570
path="$(pwd)/BLUB" &&
546571
touch "$path" &&

t/t7400-submodule-basic.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,31 @@ 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+
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+
410435
test_expect_success 'setup - add an example entry to .gitmodules' '
411436
git config --file=.gitmodules submodule.example.url git://example.com/init.git
412437
'

0 commit comments

Comments
 (0)