summaryrefslogtreecommitdiff
path: root/submodule.c
diff options
context:
space:
mode:
authorBrandon Williams <bmwill@google.com>2017-08-02 19:49:22 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-08-02 21:26:46 (GMT)
commit2184d4ba0cb86a7f40153cd46b03d3fa75b247d9 (patch)
tree316c452a7128bcd7a37321349871e61145144db8 /submodule.c
parent34e2ba04be4f8e11d91b2508aa8ca84148fe63f1 (diff)
downloadgit-2184d4ba0cb86a7f40153cd46b03d3fa75b247d9.zip
git-2184d4ba0cb86a7f40153cd46b03d3fa75b247d9.tar.gz
git-2184d4ba0cb86a7f40153cd46b03d3fa75b247d9.tar.bz2
submodule: merge repo_read_gitmodules and gitmodules_config
Since 69aba5329 (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 <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'submodule.c')
-rw-r--r--submodule.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/submodule.c b/submodule.c
index 3b0e70c..9d5eaca 100644
--- a/submodule.c
+++ b/submodule.c
@@ -230,23 +230,6 @@ void load_submodule_cache(void)
git_config(submodule_config, NULL);
}
-void gitmodules_config(void)
-{
- const char *work_tree = get_git_work_tree();
- if (work_tree) {
- struct strbuf gitmodules_path = STRBUF_INIT;
- strbuf_addstr(&gitmodules_path, work_tree);
- strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE);
- if (read_cache() < 0)
- die("index file corrupt");
-
- if (!is_gitmodules_unmerged(&the_index))
- git_config_from_file(git_modules_config,
- gitmodules_path.buf, NULL);
- strbuf_release(&gitmodules_path);
- }
-}
-
static int gitmodules_cb(const char *var, const char *value, void *data)
{
struct repository *repo = data;
@@ -255,10 +238,24 @@ static int gitmodules_cb(const char *var, const char *value, void *data)
void repo_read_gitmodules(struct repository *repo)
{
- char *gitmodules_path = repo_worktree_path(repo, GITMODULES_FILE);
+ if (repo->worktree) {
+ char *gitmodules;
+
+ if (repo_read_index(repo) < 0)
+ return;
- git_config_from_file(gitmodules_cb, gitmodules_path, repo);
- free(gitmodules_path);
+ gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
+
+ if (!is_gitmodules_unmerged(repo->index))
+ git_config_from_file(gitmodules_cb, gitmodules, repo);
+
+ free(gitmodules);
+ }
+}
+
+void gitmodules_config(void)
+{
+ repo_read_gitmodules(the_repository);
}
void gitmodules_config_sha1(const unsigned char *commit_sha1)