summaryrefslogtreecommitdiff
path: root/src/submodule.c
diff options
context:
space:
mode:
authorCarson Howard <carsonh@axosoft.com>2017-10-16 15:30:47 -0700
committerCarson Howard <tylerw+systemtest@axosoft.com>2018-03-27 07:29:04 -0700
commitad1c435054cd9c1f6fd0eeaff497651cbd1e5d30 (patch)
tree1955d25933260fb33db6805dec5e9229e1eb7c64 /src/submodule.c
parent217add94008135035f19a7076ba20de4338d8220 (diff)
downloadlibgit2-ad1c435054cd9c1f6fd0eeaff497651cbd1e5d30.tar.gz
submodule: check index for prefix before adding submodule
submodule: check path and prefix before adding submodule submodule: fix test errors
Diffstat (limited to 'src/submodule.c')
-rw-r--r--src/submodule.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/submodule.c b/src/submodule.c
index 3ec0307b3..147d3aed4 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -660,7 +660,10 @@ int git_submodule_add_setup(
int use_gitlink)
{
int error = 0;
+ size_t path_len;
+ const char *dir;
git_config_backend *mods = NULL;
+ git_index *index;
git_submodule *sm = NULL;
git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
git_repository *subrepo = NULL;
@@ -688,6 +691,34 @@ int git_submodule_add_setup(
goto cleanup;
}
+ /* get the index for the repo */
+
+ if ((error = git_repository_index__weakptr(&index, repo)) < 0)
+ goto cleanup;
+
+ /* see if the submodule name exists as a file on the index */
+
+ if ((error = git_index_find(NULL, index, path)) == 0) {
+ giterr_set(GITERR_SUBMODULE,
+ "'%s' already exists in the index", path);
+ return GIT_EEXISTS;
+ }
+
+ /* We need the path to end with '/' so we can check it as a directory prefix */
+
+ path_len = strlen(path);
+ dir = git__malloc(path_len + 1);
+ strcpy(dir, path);
+ git_path_string_to_dir(dir, path_len + 1);
+
+ /* see if the submodule name exists as a directory on the index */
+
+ if ((error = git_index_find_prefix(NULL, index, dir)) == 0) {
+ giterr_set(GITERR_SUBMODULE,
+ "'%s' already exists in the index", path);
+ return GIT_EEXISTS;
+ }
+
/* update .gitmodules */
if (!(mods = open_gitmodules(repo, GITMODULES_CREATE))) {