submodule: change can_add_submodule to is_path_occupied
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
diff --git a/src/submodule.c b/src/submodule.c
index d1a0c82..b9cdb19 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -153,11 +153,12 @@ static int find_by_path(const git_config_entry *entry, void *payload)
* Checks to see if the submodule shares its name with a file or directory that
* already exists on the index. If so, the submodule cannot be added.
*/
-static int can_add_submodule(git_repository *repo, const char *path)
+static int is_path_occupied(bool *occupied, git_repository *repo, const char *path)
{
int error = 0;
git_index *index;
git_buf dir = GIT_BUF_INIT;
+ *occupied = false;
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
goto out;
@@ -165,10 +166,9 @@ static int can_add_submodule(git_repository *repo, const char *path)
if ((error = git_index_find(NULL, index, path)) == 0) {
giterr_set(GITERR_SUBMODULE,
"File '%s' already exists in the index", path);
- error = GIT_EEXISTS;
+ *occupied = true;
goto out;
}
- error = 0;
if ((error = git_buf_sets(&dir, path)) < 0)
goto out;
@@ -179,8 +179,7 @@ static int can_add_submodule(git_repository *repo, const char *path)
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) == 0) {
giterr_set(GITERR_SUBMODULE,
"Directory '%s' already exists in the index", path);
- error = GIT_EEXISTS;
- goto out;
+ *occupied = true;
}
error = 0;
@@ -704,6 +703,7 @@ int git_submodule_add_setup(
git_submodule *sm = NULL;
git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
git_repository *subrepo = NULL;
+ bool path_occupied;
assert(repo && url && path);
@@ -728,9 +728,14 @@ int git_submodule_add_setup(
goto cleanup;
}
- if ((error = can_add_submodule(repo, path)) < 0)
+ if ((error = is_path_occupied(&path_occupied, repo, path)) < 0)
goto cleanup;
+ if (path_occupied) {
+ error = GIT_EEXISTS;
+ goto cleanup;
+ }
+
/* update .gitmodules */
if (!(mods = open_gitmodules(repo, GITMODULES_CREATE))) {