Skip to content

Commit b282ca7

Browse files
Carson HowardCarson Howard
authored andcommitted
submodule: change can_add_submodule to is_path_occupied
1 parent 677d393 commit b282ca7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/submodule.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ static int find_by_path(const git_config_entry *entry, void *payload)
153153
* Checks to see if the submodule shares its name with a file or directory that
154154
* already exists on the index. If so, the submodule cannot be added.
155155
*/
156-
static int can_add_submodule(git_repository *repo, const char *path)
156+
static int is_path_occupied(bool *occupied, git_repository *repo, const char *path)
157157
{
158158
int error = 0;
159159
git_index *index;
160160
git_buf dir = GIT_BUF_INIT;
161+
*occupied = false;
161162

162163
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
163164
goto out;
164165

165166
if ((error = git_index_find(NULL, index, path)) == 0) {
166167
giterr_set(GITERR_SUBMODULE,
167168
"File '%s' already exists in the index", path);
168-
error = GIT_EEXISTS;
169+
*occupied = true;
169170
goto out;
170171
}
171-
error = 0;
172172

173173
if ((error = git_buf_sets(&dir, path)) < 0)
174174
goto out;
@@ -179,8 +179,7 @@ static int can_add_submodule(git_repository *repo, const char *path)
179179
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) == 0) {
180180
giterr_set(GITERR_SUBMODULE,
181181
"Directory '%s' already exists in the index", path);
182-
error = GIT_EEXISTS;
183-
goto out;
182+
*occupied = true;
184183
}
185184
error = 0;
186185

@@ -704,6 +703,7 @@ int git_submodule_add_setup(
704703
git_submodule *sm = NULL;
705704
git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
706705
git_repository *subrepo = NULL;
706+
bool path_occupied;
707707

708708
assert(repo && url && path);
709709

@@ -728,9 +728,14 @@ int git_submodule_add_setup(
728728
goto cleanup;
729729
}
730730

731-
if ((error = can_add_submodule(repo, path)) < 0)
731+
if ((error = is_path_occupied(&path_occupied, repo, path)) < 0)
732732
goto cleanup;
733733

734+
if (path_occupied) {
735+
error = GIT_EEXISTS;
736+
goto cleanup;
737+
}
738+
734739
/* update .gitmodules */
735740

736741
if (!(mods = open_gitmodules(repo, GITMODULES_CREATE))) {

0 commit comments

Comments
 (0)