Skip to content

Commit 69a282d

Browse files
author
Carson Howard
committed
submodule: add more robust error handling when a submodule path is found on add
1 parent c07abd6 commit 69a282d

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/submodule.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ static int is_path_occupied(bool *occupied, git_repository *repo, const char *pa
163163
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
164164
goto out;
165165

166-
if ((error = git_index_find(NULL, index, path)) == 0) {
167-
giterr_set(GITERR_SUBMODULE,
168-
"File '%s' already exists in the index", path);
169-
*occupied = true;
166+
if ((error = git_index_find(NULL, index, path)) != GIT_ENOTFOUND) {
167+
if (!error) {
168+
giterr_set(GITERR_SUBMODULE,
169+
"File '%s' already exists in the index", path);
170+
*occupied = true;
171+
}
170172
goto out;
171173
}
172174

@@ -176,14 +178,15 @@ static int is_path_occupied(bool *occupied, git_repository *repo, const char *pa
176178
if ((error = git_path_to_dir(&dir)) < 0)
177179
goto out;
178180

179-
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) < 0 && error != GIT_ENOTFOUND)
180-
goto out;
181-
182-
if (!error) {
183-
giterr_set(GITERR_SUBMODULE,
184-
"Directory '%s' already exists in the index", path);
185-
*occupied = true;
181+
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) != GIT_ENOTFOUND) {
182+
if (!error) {
183+
giterr_set(GITERR_SUBMODULE,
184+
"Directory '%s' already exists in the index", path);
185+
*occupied = true;
186+
}
187+
goto out;
186188
}
189+
187190
error = 0;
188191

189192
out:

tests/submodule/add.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void test_submodule_add__path_exists_in_index(void)
156156

157157
cl_git_pass(git_buf_joinpath(&filename, "subdirectory", "test.txt"));
158158

159-
cl_git_pass(git_repository_index(&index, g_repo));
159+
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
160160

161161
test_add_entry(index, valid_blob_id, filename.ptr, GIT_FILEMODE_BLOB);
162162

@@ -174,7 +174,7 @@ void test_submodule_add__file_exists_in_index(void)
174174

175175
g_repo = cl_git_sandbox_init("testrepo");
176176

177-
cl_git_pass(git_repository_index(&index, g_repo));
177+
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
178178

179179
test_add_entry(index, valid_blob_id, "subdirectory", GIT_FILEMODE_BLOB);
180180

0 commit comments

Comments
 (0)