summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--object-file.c6
-rw-r--r--read-cache.c3
-rwxr-xr-xt/t3700-add.sh26
-rwxr-xr-xt/t7400-submodule-basic.sh25
4 files changed, 56 insertions, 4 deletions
diff --git a/object-file.c b/object-file.c
index 4675c8ed6b..a7438b6205 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1661,7 +1661,11 @@ int index_path(struct index_state *istate, struct object_id *oid,
strbuf_release(&sb);
break;
case S_IFDIR:
- return repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid);
+ if (repo_resolve_gitlink_ref(istate->repo, path, "HEAD", oid))
+ return error(_("'%s' does not have a commit checked out"), path);
+ if (&hash_algos[oid->algo] != istate->repo->hash_algo)
+ return error(_("cannot add a submodule of a different hash algorithm"));
+ break;
default:
return error(_("%s: unsupported file type"), path);
}
diff --git a/read-cache.c b/read-cache.c
index 032480d0c7..990d4ead0d 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -706,7 +706,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
(intent_only ? ADD_CACHE_NEW_ONLY : 0));
unsigned hash_flags = pretend ? 0 : INDEX_WRITE_OBJECT;
- struct object_id oid;
if (flags & ADD_CACHE_RENORMALIZE)
hash_flags |= INDEX_RENORMALIZE;
@@ -716,8 +715,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
namelen = strlen(path);
if (S_ISDIR(st_mode)) {
- if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
- return error(_("'%s' does not have a commit checked out"), path);
while (namelen && path[namelen-1] == '/')
namelen--;
}
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index df580a5806..af93e53c12 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -388,6 +388,7 @@ test_expect_success 'error on a repository with no commits' '
test_must_fail git add empty >actual 2>&1 &&
cat >expect <<-EOF &&
error: '"'empty/'"' does not have a commit checked out
+ error: unable to index file '"'empty/'"'
fatal: adding files failed
EOF
test_cmp expect actual
@@ -541,6 +542,31 @@ test_expect_success 'all statuses changed in folder if . is given' '
)
'
+test_expect_success 'cannot add a submodule of a different algorithm' '
+ git init --object-format=sha256 sha256 &&
+ (
+ cd sha256 &&
+ test_commit abc &&
+ git init --object-format=sha1 submodule &&
+ test_commit -C submodule def &&
+ test_must_fail git add submodule 2>err &&
+ test_grep "cannot add a submodule of a different hash algorithm" err &&
+ git ls-files --stage >entries &&
+ test_grep ! ^160000 entries
+ ) &&
+ git init --object-format=sha1 sha1 &&
+ (
+ cd sha1 &&
+ test_commit abc &&
+ git init --object-format=sha256 submodule &&
+ test_commit -C submodule def &&
+ test_must_fail git add submodule 2>err &&
+ test_grep "cannot add a submodule of a different hash algorithm" err &&
+ git ls-files --stage >entries &&
+ test_grep ! ^160000 entries
+ )
+'
+
test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
path="$(pwd)/BLUB" &&
touch "$path" &&
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index fd3e7e355e..e6b551daad 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -407,6 +407,31 @@ test_expect_success 'submodule add in subdirectory with relative path should fai
test_grep toplevel output.err
'
+test_expect_success 'submodule add of a different algorithm fails' '
+ git init --object-format=sha256 sha256 &&
+ (
+ cd sha256 &&
+ test_commit abc &&
+ git init --object-format=sha1 submodule &&
+ test_commit -C submodule def &&
+ test_must_fail git submodule add "$submodurl" submodule 2>err &&
+ test_grep "cannot add a submodule of a different hash algorithm" err &&
+ git ls-files --stage >entries &&
+ test_grep ! ^160000 entries
+ ) &&
+ git init --object-format=sha1 sha1 &&
+ (
+ cd sha1 &&
+ test_commit abc &&
+ git init --object-format=sha256 submodule &&
+ test_commit -C submodule def &&
+ test_must_fail git submodule add "$submodurl" submodule 2>err &&
+ test_grep "cannot add a submodule of a different hash algorithm" err &&
+ git ls-files --stage >entries &&
+ test_grep ! ^160000 entries
+ )
+'
+
test_expect_success 'setup - add an example entry to .gitmodules' '
git config --file=.gitmodules submodule.example.url git://example.com/init.git
'