Skip to content

Commit aa765fe

Browse files
committed
Merge branch 'bc/submodule-force-same-hash' into next
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: read-cache: drop submodule check from add_to_cache() object-file: disallow adding submodules of different hash algo
2 parents c4e4a73 + 6fe288b commit aa765fe

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

object-file.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,11 @@ int index_path(struct index_state *istate, struct object_id *oid,
16611661
strbuf_release(&sb);
16621662
break;
16631663
case S_IFDIR:
1664-
return repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid);
1664+
if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
1665+
return error(_("'%s' does not have a commit checked out"), path);
1666+
if (&hash_algos[oid->algo] != istate->repo->hash_algo)
1667+
return error(_("cannot add a submodule of a different hash algorithm"));
1668+
break;
16651669
default:
16661670
return error(_("%s: unsupported file type"), path);
16671671
}

read-cache.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
706706
int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
707707
(intent_only ? ADD_CACHE_NEW_ONLY : 0));
708708
unsigned hash_flags = pretend ? 0 : INDEX_WRITE_OBJECT;
709-
struct object_id oid;
710709

711710
if (flags & ADD_CACHE_RENORMALIZE)
712711
hash_flags |= INDEX_RENORMALIZE;
@@ -716,8 +715,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
716715

717716
namelen = strlen(path);
718717
if (S_ISDIR(st_mode)) {
719-
if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
720-
return error(_("'%s' does not have a commit checked out"), path);
721718
while (namelen && path[namelen-1] == '/')
722719
namelen--;
723720
}

t/t3700-add.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ test_expect_success 'error on a repository with no commits' '
388388
test_must_fail git add empty >actual 2>&1 &&
389389
cat >expect <<-EOF &&
390390
error: '"'empty/'"' does not have a commit checked out
391+
error: unable to index file '"'empty/'"'
391392
fatal: adding files failed
392393
EOF
393394
test_cmp expect actual
@@ -541,6 +542,31 @@ test_expect_success 'all statuses changed in folder if . is given' '
541542
)
542543
'
543544

545+
test_expect_success 'cannot add a submodule of a different algorithm' '
546+
git init --object-format=sha256 sha256 &&
547+
(
548+
cd sha256 &&
549+
test_commit abc &&
550+
git init --object-format=sha1 submodule &&
551+
test_commit -C submodule def &&
552+
test_must_fail git add submodule 2>err &&
553+
test_grep "cannot add a submodule of a different hash algorithm" err &&
554+
git ls-files --stage >entries &&
555+
test_grep ! ^160000 entries
556+
) &&
557+
git init --object-format=sha1 sha1 &&
558+
(
559+
cd sha1 &&
560+
test_commit abc &&
561+
git init --object-format=sha256 submodule &&
562+
test_commit -C submodule def &&
563+
test_must_fail git add submodule 2>err &&
564+
test_grep "cannot add a submodule of a different hash algorithm" err &&
565+
git ls-files --stage >entries &&
566+
test_grep ! ^160000 entries
567+
)
568+
'
569+
544570
test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
545571
path="$(pwd)/BLUB" &&
546572
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)