submodule: add more robust error handling when a submodule path is found on add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
diff --git a/src/submodule.c b/src/submodule.c
index cfde81a..3743466 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -163,10 +163,12 @@ static int is_path_occupied(bool *occupied, git_repository *repo, const char *pa
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
goto out;
- if ((error = git_index_find(NULL, index, path)) == 0) {
- giterr_set(GITERR_SUBMODULE,
- "File '%s' already exists in the index", path);
- *occupied = true;
+ if ((error = git_index_find(NULL, index, path)) != GIT_ENOTFOUND) {
+ if (!error) {
+ giterr_set(GITERR_SUBMODULE,
+ "File '%s' already exists in the index", path);
+ *occupied = true;
+ }
goto out;
}
@@ -176,14 +178,15 @@ static int is_path_occupied(bool *occupied, git_repository *repo, const char *pa
if ((error = git_path_to_dir(&dir)) < 0)
goto out;
- if ((error = git_index_find_prefix(NULL, index, dir.ptr)) < 0 && error != GIT_ENOTFOUND)
- goto out;
-
- if (!error) {
- giterr_set(GITERR_SUBMODULE,
- "Directory '%s' already exists in the index", path);
- *occupied = true;
+ if ((error = git_index_find_prefix(NULL, index, dir.ptr)) != GIT_ENOTFOUND) {
+ if (!error) {
+ giterr_set(GITERR_SUBMODULE,
+ "Directory '%s' already exists in the index", path);
+ *occupied = true;
+ }
+ goto out;
}
+
error = 0;
out:
diff --git a/tests/submodule/add.c b/tests/submodule/add.c
index 383b5e5..ebb9d62 100644
--- a/tests/submodule/add.c
+++ b/tests/submodule/add.c
@@ -156,7 +156,7 @@ void test_submodule_add__path_exists_in_index(void)
cl_git_pass(git_buf_joinpath(&filename, "subdirectory", "test.txt"));
- cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_repository_index__weakptr(&index, g_repo));
test_add_entry(index, valid_blob_id, filename.ptr, GIT_FILEMODE_BLOB);
@@ -174,7 +174,7 @@ void test_submodule_add__file_exists_in_index(void)
g_repo = cl_git_sandbox_init("testrepo");
- cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_repository_index__weakptr(&index, g_repo));
test_add_entry(index, valid_blob_id, "subdirectory", GIT_FILEMODE_BLOB);