Skip to content

Commit 9371149

Browse files
Carson HowardCarson Howard
authored andcommitted
submodule: fix styling errors
1 parent 3e500fc commit 9371149

File tree

2 files changed

+50
-61
lines changed

2 files changed

+50
-61
lines changed

src/submodule.c

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,41 @@ 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;
154+
git_index *index;
155+
git_buf dir = GIT_BUF_INIT;
156+
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+
165+
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 */
169+
170+
if ((error = git_index_find(NULL, index, path)) == 0) {
171+
giterr_set(GITERR_SUBMODULE,
172+
"'%s' already exists in the index", path);
173+
return GIT_EEXISTS;
174+
}
175+
176+
/* see if the submodule name exists as a directory on the index */
177+
178+
if ((error = git_index_find_prefix(NULL, index, dir.ptr)) == 0) {
179+
giterr_set(GITERR_SUBMODULE,
180+
"'%s' already exists in the index", path);
181+
return GIT_EEXISTS;
182+
}
183+
184+
return 0;
185+
}
186+
152187
/**
153188
* Release the name map returned by 'load_submodule_names'.
154189
*/
@@ -660,10 +695,7 @@ int git_submodule_add_setup(
660695
int use_gitlink)
661696
{
662697
int error = 0;
663-
size_t path_len;
664-
const char *dir;
665698
git_config_backend *mods = NULL;
666-
git_index *index;
667699
git_submodule *sm = NULL;
668700
git_buf name = GIT_BUF_INIT, real_url = GIT_BUF_INIT;
669701
git_repository *subrepo = NULL;
@@ -691,34 +723,9 @@ int git_submodule_add_setup(
691723
goto cleanup;
692724
}
693725

694-
/* get the index for the repo */
695-
696-
if ((error = git_repository_index__weakptr(&index, repo)) < 0)
726+
if ((error = can_add_submodule(repo, path)) < 0)
697727
goto cleanup;
698728

699-
/* see if the submodule name exists as a file on the index */
700-
701-
if ((error = git_index_find(NULL, index, path)) == 0) {
702-
giterr_set(GITERR_SUBMODULE,
703-
"'%s' already exists in the index", path);
704-
return GIT_EEXISTS;
705-
}
706-
707-
/* We need the path to end with '/' so we can check it as a directory prefix */
708-
709-
path_len = strlen(path);
710-
dir = git__malloc(path_len + 1);
711-
strcpy(dir, path);
712-
git_path_string_to_dir(dir, path_len + 1);
713-
714-
/* see if the submodule name exists as a directory on the index */
715-
716-
if ((error = git_index_find_prefix(NULL, index, dir)) == 0) {
717-
giterr_set(GITERR_SUBMODULE,
718-
"'%s' already exists in the index", path);
719-
return GIT_EEXISTS;
720-
}
721-
722729
/* update .gitmodules */
723730

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

tests/submodule/add.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,22 @@ void test_submodule_add__path_exists_in_index(void)
135135
git_submodule *sm;
136136
git_buf dirname = GIT_BUF_INIT;
137137
git_buf filename = GIT_BUF_INIT;
138-
FILE *fd;
139138

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

143-
git_buf_joinpath(&dirname, git_repository_workdir(g_repo), "TestGitRepository");
144-
git_buf_joinpath(&filename, dirname.ptr, "test.txt");
142+
cl_git_pass(git_buf_joinpath(&dirname, git_repository_workdir(g_repo), "TestGitRepository"));
143+
cl_git_pass(git_buf_joinpath(&filename, dirname.ptr, "test.txt"));
145144

146-
p_mkdir(dirname.ptr, 0700);
147-
fd = fopen(filename.ptr, "w");
148-
fclose(fd);
145+
cl_git_pass(p_mkdir(dirname.ptr, 0700));
146+
cl_git_mkfile(filename.ptr, "This is some content");
149147

150-
cl_git_pass(
151-
git_repository_index__weakptr(&index, g_repo)
152-
);
148+
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
149+
cl_git_pass(git_index_add_bypath(index, "TestGitRepository/test.txt"));
153150

154-
cl_git_pass(
155-
git_index_add_bypath(index, "TestGitRepository/test.txt")
156-
);
157-
158-
cl_git_fail_with(
159-
git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1),
160-
GIT_EEXISTS
161-
);
151+
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1), GIT_EEXISTS);
162152

153+
git_submodule_free(sm);
163154
git_buf_free(&dirname);
164155
git_buf_free(&filename);
165156
}
@@ -169,28 +160,19 @@ void test_submodule_add__file_exists_in_index(void)
169160
git_index *index;
170161
git_submodule *sm;
171162
git_buf name = GIT_BUF_INIT;
172-
FILE *fd;
173163

174164
/* In this repo, HEAD (master) has no remote tracking branc h*/
175165
g_repo = cl_git_sandbox_init("testrepo");
176166

177-
git_buf_joinpath(&name, git_repository_workdir(g_repo), "TestGitRepository");
167+
cl_git_pass(git_buf_joinpath(&name, git_repository_workdir(g_repo), "TestGitRepository"));
178168

179-
fd = fopen(name.ptr, "w");
180-
fclose(fd);
169+
cl_git_mkfile(name.ptr, "Test content");
181170

182-
cl_git_pass(
183-
git_repository_index__weakptr(&index, g_repo)
184-
);
171+
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
172+
cl_git_pass(git_index_add_bypath(index, "TestGitRepository"));
185173

186-
cl_git_pass(
187-
git_index_add_bypath(index, "TestGitRepository")
188-
);
189-
190-
cl_git_fail_with(
191-
git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1),
192-
GIT_EEXISTS
193-
);
174+
cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1), GIT_EEXISTS);
194175

176+
git_submodule_free(sm);
195177
git_buf_free(&name);
196178
}

0 commit comments

Comments
 (0)