Skip to content

Commit 2184d4b

Browse files
bmwillgitster
authored andcommitted
submodule: merge repo_read_gitmodules and gitmodules_config
Since 69aba53 (submodule: add repo_read_gitmodules) there have been two ways to load a repository's .gitmodules file: 'repo_read_gitmodules()' is used if you have a repository object you are working with or 'gitmodules_config()' if you are implicitly working with 'the_repository'. Merge the logic of these two functions to remove duplicate code. In addition, 'repo_read_gitmodules()' can segfault by passing in a NULL pointer to 'git_config_from_file()' if a repository doesn't have a worktree. Instead check for the existence of a worktree before attempting to load the .gitmodules file. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 34e2ba0 commit 2184d4b

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

submodule.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,6 @@ void load_submodule_cache(void)
230230
git_config(submodule_config, NULL);
231231
}
232232

233-
void gitmodules_config(void)
234-
{
235-
const char *work_tree = get_git_work_tree();
236-
if (work_tree) {
237-
struct strbuf gitmodules_path = STRBUF_INIT;
238-
strbuf_addstr(&gitmodules_path, work_tree);
239-
strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE);
240-
if (read_cache() < 0)
241-
die("index file corrupt");
242-
243-
if (!is_gitmodules_unmerged(&the_index))
244-
git_config_from_file(git_modules_config,
245-
gitmodules_path.buf, NULL);
246-
strbuf_release(&gitmodules_path);
247-
}
248-
}
249-
250233
static int gitmodules_cb(const char *var, const char *value, void *data)
251234
{
252235
struct repository *repo = data;
@@ -255,10 +238,24 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
255238

256239
void repo_read_gitmodules(struct repository *repo)
257240
{
258-
char *gitmodules_path = repo_worktree_path(repo, GITMODULES_FILE);
241+
if (repo->worktree) {
242+
char *gitmodules;
243+
244+
if (repo_read_index(repo) < 0)
245+
return;
259246

260-
git_config_from_file(gitmodules_cb, gitmodules_path, repo);
261-
free(gitmodules_path);
247+
gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
248+
249+
if (!is_gitmodules_unmerged(repo->index))
250+
git_config_from_file(gitmodules_cb, gitmodules, repo);
251+
252+
free(gitmodules);
253+
}
254+
}
255+
256+
void gitmodules_config(void)
257+
{
258+
repo_read_gitmodules(the_repository);
262259
}
263260

264261
void gitmodules_config_sha1(const unsigned char *commit_sha1)

0 commit comments

Comments
 (0)