Skip to content

Commit ef9a774

Browse files
cjhoward92Carson Howard
authored andcommitted
submodule: update index check to check path before directory and fix tests
1 parent 9371149 commit ef9a774

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/submodule.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,39 +149,44 @@ static int find_by_path(const git_config_entry *entry, void *payload)
149149
return 0;
150150
}
151151

152-
static int can_add_submodule(git_repository *repo, const char *path) {
153-
int error;
152+
/*
153+
* Checks to see if the submodule shares its name with a file or directory that
154+
* already exists on the index. If so, the submodule cannot be added.
155+
*/
156+
static int can_add_submodule(git_repository *repo, const char *path)
157+
{
158+
int error = 0;
154159
git_index *index;
155160
git_buf dir = GIT_BUF_INIT;
156161

157-
if ((error = git_buf_sets(&dir, path)) < 0)
158-
return error;
159-
160-
if ((error = git_path_to_dir(&dir)) < 0)
161-
return error;
162-
163-
/* get the index for the repo */
164-
165162
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
166-
return error;
167-
168-
/* see if the submodule name exists as a file on the index */
163+
goto out;
169164

170165
if ((error = git_index_find(NULL, index, path)) == 0) {
171166
giterr_set(GITERR_SUBMODULE,
172-
"'%s' already exists in the index", path);
173-
return GIT_EEXISTS;
167+
"File '%s' already exists in the index", path);
168+
error = GIT_EEXISTS;
169+
goto out;
174170
}
171+
error = 0;
175172

176-
/* see if the submodule name exists as a directory on the index */
173+
if ((error = git_buf_sets(&dir, path)) < 0)
174+
goto out;
175+
176+
if ((error = git_path_to_dir(&dir)) < 0)
177+
goto out;
177178

178179
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) == 0) {
179180
giterr_set(GITERR_SUBMODULE,
180-
"'%s' already exists in the index", path);
181-
return GIT_EEXISTS;
181+
"Directory '%s' already exists in the index", path);
182+
error = GIT_EEXISTS;
183+
goto out;
182184
}
185+
error = 0;
183186

184-
return 0;
187+
out:
188+
git_buf_free(&dir);
189+
return error;
185190
}
186191

187192
/**

tests/submodule/add.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,21 @@ void test_submodule_add__path_exists_in_index(void)
136136
git_buf dirname = GIT_BUF_INIT;
137137
git_buf filename = GIT_BUF_INIT;
138138

139-
/* In this repo, HEAD (master) has no remote tracking branc h*/
140139
g_repo = cl_git_sandbox_init("testrepo");
141140

142-
cl_git_pass(git_buf_joinpath(&dirname, git_repository_workdir(g_repo), "TestGitRepository"));
141+
cl_git_pass(git_buf_joinpath(&dirname, git_repository_workdir(g_repo), "subdirectory"));
143142
cl_git_pass(git_buf_joinpath(&filename, dirname.ptr, "test.txt"));
144143

145144
cl_git_pass(p_mkdir(dirname.ptr, 0700));
146145
cl_git_mkfile(filename.ptr, "This is some content");
147146

148147
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
149-
cl_git_pass(git_index_add_bypath(index, "TestGitRepository/test.txt"));
148+
cl_git_pass(git_index_add_bypath(index, "subdirectory/test.txt"));
149+
150+
cl_git_pass(p_unlink(filename.ptr));
151+
cl_git_pass(p_rmdir(dirname.ptr));
150152

151-
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1), GIT_EEXISTS);
153+
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
152154

153155
git_submodule_free(sm);
154156
git_buf_free(&dirname);
@@ -161,17 +163,18 @@ void test_submodule_add__file_exists_in_index(void)
161163
git_submodule *sm;
162164
git_buf name = GIT_BUF_INIT;
163165

164-
/* In this repo, HEAD (master) has no remote tracking branc h*/
165166
g_repo = cl_git_sandbox_init("testrepo");
166167

167-
cl_git_pass(git_buf_joinpath(&name, git_repository_workdir(g_repo), "TestGitRepository"));
168+
cl_git_pass(git_buf_joinpath(&name, git_repository_workdir(g_repo), "subdirectory"));
168169

169170
cl_git_mkfile(name.ptr, "Test content");
170171

171172
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
172-
cl_git_pass(git_index_add_bypath(index, "TestGitRepository"));
173+
cl_git_pass(git_index_add_bypath(index, "subdirectory"));
174+
175+
cl_git_pass(p_unlink(name.ptr));
173176

174-
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1), GIT_EEXISTS);
177+
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
175178

176179
git_submodule_free(sm);
177180
git_buf_free(&name);

0 commit comments

Comments
 (0)