From 4c0eeafe4755345b0f4636bf09904cf689703e11 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:16 -0700 Subject: cache.h: add GITMODULES_FILE macro Add a macro to be used when specifying the '.gitmodules' file and convert any existing hard coded '.gitmodules' file strings to use the new macro. Signed-off-by: Brandon Williams Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- submodule.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index 6531c5d609..64ad5c12d8 100644 --- a/submodule.c +++ b/submodule.c @@ -63,7 +63,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) struct strbuf entry = STRBUF_INIT; const struct submodule *submodule; - if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ + if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ return -1; if (gitmodules_is_unmerged) @@ -77,7 +77,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) strbuf_addstr(&entry, "submodule."); strbuf_addstr(&entry, submodule->name); strbuf_addstr(&entry, ".path"); - if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) { + if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) { /* Maybe the user already did that, don't error out here */ warning(_("Could not update .gitmodules entry %s"), entry.buf); strbuf_release(&entry); @@ -97,7 +97,7 @@ int remove_path_from_gitmodules(const char *path) struct strbuf sect = STRBUF_INIT; const struct submodule *submodule; - if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ + if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ return -1; if (gitmodules_is_unmerged) @@ -110,7 +110,7 @@ int remove_path_from_gitmodules(const char *path) } strbuf_addstr(§, "submodule."); strbuf_addstr(§, submodule->name); - if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { + if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) { /* Maybe the user already did that, don't error out here */ warning(_("Could not remove .gitmodules entry for %s"), path); strbuf_release(§); @@ -122,7 +122,7 @@ int remove_path_from_gitmodules(const char *path) void stage_updated_gitmodules(void) { - if (add_file_to_cache(".gitmodules", 0)) + if (add_file_to_cache(GITMODULES_FILE, 0)) die(_("staging updated .gitmodules failed")); } @@ -230,21 +230,21 @@ void gitmodules_config(void) struct strbuf gitmodules_path = STRBUF_INIT; int pos; strbuf_addstr(&gitmodules_path, work_tree); - strbuf_addstr(&gitmodules_path, "/.gitmodules"); + strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE); if (read_cache() < 0) die("index file corrupt"); - pos = cache_name_pos(".gitmodules", 11); + pos = cache_name_pos(GITMODULES_FILE, 11); if (pos < 0) { /* .gitmodules not found or isn't merged */ pos = -1 - pos; if (active_nr > pos) { /* there is a .gitmodules */ const struct cache_entry *ce = active_cache[pos]; if (ce_namelen(ce) == 11 && - !memcmp(ce->name, ".gitmodules", 11)) + !memcmp(ce->name, GITMODULES_FILE, 11)) gitmodules_is_unmerged = 1; } } else if (pos < active_nr) { struct stat st; - if (lstat(".gitmodules", &st) == 0 && + if (lstat(GITMODULES_FILE, &st) == 0 && ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED) gitmodules_is_modified = 1; } @@ -264,7 +264,7 @@ 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"); + char *gitmodules_path = repo_worktree_path(repo, GITMODULES_FILE); git_config_from_file(gitmodules_cb, gitmodules_path, repo); free(gitmodules_path); -- cgit v1.3 From f20e7c1ea2459d9b8c12f8ed1f1546665841b643 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:18 -0700 Subject: submodule: remove submodule.fetchjobs from submodule-config parsing The '.gitmodules' file should only contain information pertinent to configuring individual submodules (name to path mapping, URL where to obtain the submodule, etc.) while other configuration like the number of jobs to use when fetching submodules should be a part of the repository's config. Remove the 'submodule.fetchjobs' configuration option from the general submodule-config parsing and instead rely on using the 'config_from_gitmodules()' in order to maintain backwards compatibility with this config being placed in the '.gitmodules' file. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/fetch.c | 18 +++++++++++++++++- builtin/submodule--helper.c | 17 +++++++++++++---- submodule-config.c | 8 ++++++++ submodule-config.h | 1 + submodule.c | 16 +--------------- submodule.h | 1 - 6 files changed, 40 insertions(+), 21 deletions(-) (limited to 'submodule.c') diff --git a/builtin/fetch.c b/builtin/fetch.c index c87e59f3b1..ade092bf8d 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -39,7 +39,7 @@ static int prune = -1; /* unspecified */ static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative; static int progress = -1; static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen; -static int max_children = -1; +static int max_children = 1; static enum transport_family family; static const char *depth; static const char *deepen_since; @@ -68,9 +68,24 @@ static int git_fetch_config(const char *k, const char *v, void *cb) recurse_submodules = r; } + if (!strcmp(k, "submodule.fetchjobs")) { + max_children = parse_submodule_fetchjobs(k, v); + return 0; + } + return git_default_config(k, v, cb); } +static int gitmodules_fetch_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "submodule.fetchjobs")) { + max_children = parse_submodule_fetchjobs(var, value); + return 0; + } + + return 0; +} + static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) { ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc); @@ -1311,6 +1326,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) for (i = 1; i < argc; i++) strbuf_addf(&default_rla, " %s", argv[i]); + config_from_gitmodules(gitmodules_fetch_config, NULL); git_config(git_fetch_config, NULL); argc = parse_options(argc, argv, prefix, diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 6abdad3294..6d9600d4fb 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -960,10 +960,19 @@ static int update_clone_task_finished(int result, return 0; } +static int gitmodules_update_clone_config(const char *var, const char *value, + void *cb) +{ + int *max_jobs = cb; + if (!strcmp(var, "submodule.fetchjobs")) + *max_jobs = parse_submodule_fetchjobs(var, value); + return 0; +} + static int update_clone(int argc, const char **argv, const char *prefix) { const char *update = NULL; - int max_jobs = -1; + int max_jobs = 1; struct string_list_item *item; struct pathspec pathspec; struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; @@ -1000,6 +1009,9 @@ static int update_clone(int argc, const char **argv, const char *prefix) }; suc.prefix = prefix; + config_from_gitmodules(gitmodules_update_clone_config, &max_jobs); + git_config(gitmodules_update_clone_config, &max_jobs); + argc = parse_options(argc, argv, prefix, module_update_clone_options, git_submodule_helper_usage, 0); @@ -1017,9 +1029,6 @@ static int update_clone(int argc, const char **argv, const char *prefix) gitmodules_config(); git_config(submodule_config, NULL); - if (max_jobs < 0) - max_jobs = parallel_submodules(); - run_processes_parallel(max_jobs, update_clone_get_next_task, update_clone_start_failure, diff --git a/submodule-config.c b/submodule-config.c index 5fe2d07877..70400f553a 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -248,6 +248,14 @@ static int parse_fetch_recurse(const char *opt, const char *arg, } } +int parse_submodule_fetchjobs(const char *var, const char *value) +{ + int fetchjobs = git_config_int(var, value); + if (fetchjobs < 0) + die(_("negative values not allowed for submodule.fetchjobs")); + return fetchjobs; +} + int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) { return parse_fetch_recurse(opt, arg, 1); diff --git a/submodule-config.h b/submodule-config.h index 233bfcb7ff..995d404f88 100644 --- a/submodule-config.h +++ b/submodule-config.h @@ -27,6 +27,7 @@ struct repository; extern void submodule_cache_free(struct submodule_cache *cache); +extern int parse_submodule_fetchjobs(const char *var, const char *value); extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg); struct option; extern int option_fetch_parse_recurse_submodules(const struct option *opt, diff --git a/submodule.c b/submodule.c index 64ad5c12d8..aa4fb1eaa2 100644 --- a/submodule.c +++ b/submodule.c @@ -22,7 +22,6 @@ static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; -static int parallel_jobs = 1; static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP; static int initialized_fetch_ref_tips; static struct oid_array ref_tips_before_fetch; @@ -159,12 +158,7 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, /* For loading from the .gitmodules file. */ static int git_modules_config(const char *var, const char *value, void *cb) { - if (!strcmp(var, "submodule.fetchjobs")) { - parallel_jobs = git_config_int(var, value); - if (parallel_jobs < 0) - die(_("negative values not allowed for submodule.fetchJobs")); - return 0; - } else if (starts_with(var, "submodule.")) + if (starts_with(var, "submodule.")) return parse_submodule_config_option(var, value); else if (!strcmp(var, "fetch.recursesubmodules")) { config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); @@ -1303,9 +1297,6 @@ int fetch_populated_submodules(const struct argv_array *options, argv_array_push(&spf.args, "--recurse-submodules-default"); /* default value, "--submodule-prefix" and its value are added later */ - if (max_parallel_jobs < 0) - max_parallel_jobs = parallel_jobs; - calculate_changed_submodule_paths(); run_processes_parallel(max_parallel_jobs, get_next_submodule, @@ -1825,11 +1816,6 @@ int merge_submodule(struct object_id *result, const char *path, return 0; } -int parallel_submodules(void) -{ - return parallel_jobs; -} - /* * Embeds a single submodules git directory into the superprojects git dir, * non recursively. diff --git a/submodule.h b/submodule.h index e85b144863..c8164a3b29 100644 --- a/submodule.h +++ b/submodule.h @@ -112,7 +112,6 @@ extern int push_unpushed_submodules(struct oid_array *commits, const struct string_list *push_options, int dry_run); extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir); -extern int parallel_submodules(void); /* * Given a submodule path (as in the index), return the repository * path of that submodule in 'buf'. Return -1 on error or when the -- cgit v1.3 From 8fa2915971e5032e6a32f5096452db81ab8795eb Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:19 -0700 Subject: submodule: remove fetch.recursesubmodules from submodule-config parsing Remove the 'fetch.recursesubmodules' configuration option from the general submodule-config parsing and instead rely on using 'config_from_gitmodules()' in order to maintain backwards compatibility with this config being placed in the '.gitmodules' file. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/fetch.c | 8 +++++++- submodule.c | 19 ++++++------------- submodule.h | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'submodule.c') diff --git a/builtin/fetch.c b/builtin/fetch.c index ade092bf8d..d84c26391c 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -71,6 +71,9 @@ static int git_fetch_config(const char *k, const char *v, void *cb) if (!strcmp(k, "submodule.fetchjobs")) { max_children = parse_submodule_fetchjobs(k, v); return 0; + } else if (!strcmp(k, "fetch.recursesubmodules")) { + recurse_submodules = parse_fetch_recurse_submodules_arg(k, v); + return 0; } return git_default_config(k, v, cb); @@ -81,6 +84,9 @@ static int gitmodules_fetch_config(const char *var, const char *value, void *cb) if (!strcmp(var, "submodule.fetchjobs")) { max_children = parse_submodule_fetchjobs(var, value); return 0; + } else if (!strcmp(var, "fetch.recursesubmodules")) { + recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); + return 0; } return 0; @@ -1355,7 +1361,6 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) deepen = 1; if (recurse_submodules != RECURSE_SUBMODULES_OFF) { - set_config_fetch_recurse_submodules(recurse_submodules_default); gitmodules_config(); git_config(submodule_config, NULL); } @@ -1399,6 +1404,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) result = fetch_populated_submodules(&options, submodule_prefix, recurse_submodules, + recurse_submodules_default, verbosity < 0, max_children); argv_array_clear(&options); diff --git a/submodule.c b/submodule.c index aa4fb1eaa2..1d9d2ce094 100644 --- a/submodule.c +++ b/submodule.c @@ -20,7 +20,6 @@ #include "worktree.h" #include "parse-options.h" -static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP; static int initialized_fetch_ref_tips; @@ -160,10 +159,6 @@ static int git_modules_config(const char *var, const char *value, void *cb) { if (starts_with(var, "submodule.")) return parse_submodule_config_option(var, value); - else if (!strcmp(var, "fetch.recursesubmodules")) { - config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); - return 0; - } return 0; } @@ -714,11 +709,6 @@ done: clear_commit_marks(right, ~0); } -void set_config_fetch_recurse_submodules(int value) -{ - config_fetch_recurse_submodules = value; -} - int should_update_submodules(void) { return config_update_recurse_submodules == RECURSE_SUBMODULES_ON; @@ -1164,10 +1154,11 @@ struct submodule_parallel_fetch { const char *work_tree; const char *prefix; int command_line_option; + int default_option; int quiet; int result; }; -#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0} +#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0} static int get_next_submodule(struct child_process *cp, struct strbuf *err, void *data, void **task_cb) @@ -1205,10 +1196,10 @@ static int get_next_submodule(struct child_process *cp, default_argv = "on-demand"; } } else { - if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || + if ((spf->default_option == RECURSE_SUBMODULES_OFF) || gitmodules_is_unmerged) continue; - if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { + if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; @@ -1275,6 +1266,7 @@ static int fetch_finish(int retvalue, struct strbuf *err, int fetch_populated_submodules(const struct argv_array *options, const char *prefix, int command_line_option, + int default_option, int quiet, int max_parallel_jobs) { int i; @@ -1282,6 +1274,7 @@ int fetch_populated_submodules(const struct argv_array *options, spf.work_tree = get_git_work_tree(); spf.command_line_option = command_line_option; + spf.default_option = default_option; spf.quiet = quiet; spf.prefix = prefix; diff --git a/submodule.h b/submodule.h index c8164a3b29..29a1ecd19a 100644 --- a/submodule.h +++ b/submodule.h @@ -76,7 +76,6 @@ extern void show_submodule_inline_diff(FILE *f, const char *path, unsigned dirty_submodule, const char *meta, const char *del, const char *add, const char *reset, const struct diff_options *opt); -extern void set_config_fetch_recurse_submodules(int value); /* Check if we want to update any submodule.*/ extern int should_update_submodules(void); /* @@ -87,6 +86,7 @@ extern const struct submodule *submodule_from_ce(const struct cache_entry *ce); extern void check_for_new_submodule_commits(struct object_id *oid); extern int fetch_populated_submodules(const struct argv_array *options, const char *prefix, int command_line_option, + int default_option, int quiet, int max_parallel_jobs); extern unsigned is_submodule_modified(const char *path, int ignore_untracked); extern int submodule_uses_gitfile(const char *path); -- cgit v1.3 From 91b834807b98b620050fe534b6de93e223dbcbcf Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:20 -0700 Subject: submodule: check for unstaged .gitmodules outside of config parsing Teach 'is_staging_gitmodules_ok()' to be able to determine in the '.gitmodules' file has unstaged changes based on the passed in index instead of relying on a global variable which is set during the submodule-config parsing. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- builtin/mv.c | 2 +- builtin/rm.c | 2 +- submodule.c | 32 +++++++++++++++++--------------- submodule.h | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) (limited to 'submodule.c') diff --git a/builtin/mv.c b/builtin/mv.c index dcf6736b5b..94fbaaa5da 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -81,7 +81,7 @@ static void prepare_move_submodule(const char *src, int first, struct strbuf submodule_dotgit = STRBUF_INIT; if (!S_ISGITLINK(active_cache[first]->ce_mode)) die(_("Directory %s is in index and no submodule?"), src); - if (!is_staging_gitmodules_ok()) + if (!is_staging_gitmodules_ok(&the_index)) die(_("Please stage your changes to .gitmodules or stash them to proceed")); strbuf_addf(&submodule_dotgit, "%s/.git", src); *submodule_gitfile = read_gitfile(submodule_dotgit.buf); diff --git a/builtin/rm.c b/builtin/rm.c index 52826d1379..4057e73fa0 100644 --- a/builtin/rm.c +++ b/builtin/rm.c @@ -286,7 +286,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix) list.entry[list.nr].name = xstrdup(ce->name); list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); if (list.entry[list.nr++].is_submodule && - !is_staging_gitmodules_ok()) + !is_staging_gitmodules_ok(&the_index)) die (_("Please stage your changes to .gitmodules or stash them to proceed")); } diff --git a/submodule.c b/submodule.c index 1d9d2ce094..677b5c4015 100644 --- a/submodule.c +++ b/submodule.c @@ -37,18 +37,25 @@ static struct oid_array ref_tips_after_fetch; static int gitmodules_is_unmerged; /* - * This flag is set if the .gitmodules file had unstaged modifications on - * startup. This must be checked before allowing modifications to the - * .gitmodules file with the intention to stage them later, because when - * continuing we would stage the modifications the user didn't stage herself - * too. That might change in a future version when we learn to stage the - * changes we do ourselves without staging any previous modifications. + * Check if the .gitmodules file has unstaged modifications. This must be + * checked before allowing modifications to the .gitmodules file with the + * intention to stage them later, because when continuing we would stage the + * modifications the user didn't stage herself too. That might change in a + * future version when we learn to stage the changes we do ourselves without + * staging any previous modifications. */ -static int gitmodules_is_modified; - -int is_staging_gitmodules_ok(void) +int is_staging_gitmodules_ok(const struct index_state *istate) { - return !gitmodules_is_modified; + int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); + + if ((pos >= 0) && (pos < istate->cache_nr)) { + struct stat st; + if (lstat(GITMODULES_FILE, &st) == 0 && + ce_match_stat(istate->cache[pos], &st, 0) & DATA_CHANGED) + return 0; + } + + return 1; } /* @@ -231,11 +238,6 @@ void gitmodules_config(void) !memcmp(ce->name, GITMODULES_FILE, 11)) gitmodules_is_unmerged = 1; } - } else if (pos < active_nr) { - struct stat st; - if (lstat(GITMODULES_FILE, &st) == 0 && - ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED) - gitmodules_is_modified = 1; } if (!gitmodules_is_unmerged) diff --git a/submodule.h b/submodule.h index 29a1ecd19a..b14660585f 100644 --- a/submodule.h +++ b/submodule.h @@ -33,7 +33,7 @@ struct submodule_update_strategy { }; #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL} -extern int is_staging_gitmodules_ok(void); +extern int is_staging_gitmodules_ok(const struct index_state *istate); extern int update_path_in_gitmodules(const char *oldpath, const char *newpath); extern int remove_path_from_gitmodules(const char *path); extern void stage_updated_gitmodules(void); -- cgit v1.3 From 34e2ba04be4f8e11d91b2508aa8ca84148fe63f1 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:21 -0700 Subject: submodule: check for unmerged .gitmodules outside of config parsing Add 'is_gitmodules_unmerged()' function which can be used to determine in the '.gitmodules' file is unmerged based on the passed in index instead of relying on a global variable which is set during the submodule-config parsing. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- submodule.c | 47 +++++++++++++++++++++++------------------------ submodule.h | 1 + 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index 677b5c4015..3b0e70c51d 100644 --- a/submodule.c +++ b/submodule.c @@ -27,14 +27,25 @@ static struct oid_array ref_tips_before_fetch; static struct oid_array ref_tips_after_fetch; /* - * The following flag is set if the .gitmodules file is unmerged. We then - * disable recursion for all submodules where .git/config doesn't have a - * matching config entry because we can't guess what might be configured in - * .gitmodules unless the user resolves the conflict. When a command line - * option is given (which always overrides configuration) this flag will be - * ignored. + * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file + * will be disabled because we can't guess what might be configured in + * .gitmodules unless the user resolves the conflict. */ -static int gitmodules_is_unmerged; +int is_gitmodules_unmerged(const struct index_state *istate) +{ + int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); + if (pos < 0) { /* .gitmodules not found or isn't merged */ + pos = -1 - pos; + if (istate->cache_nr > pos) { /* there is a .gitmodules */ + const struct cache_entry *ce = istate->cache[pos]; + if (ce_namelen(ce) == strlen(GITMODULES_FILE) && + !strcmp(ce->name, GITMODULES_FILE)) + return 1; + } + } + + return 0; +} /* * Check if the .gitmodules file has unstaged modifications. This must be @@ -71,7 +82,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ return -1; - if (gitmodules_is_unmerged) + if (is_gitmodules_unmerged(&the_index)) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); submodule = submodule_from_path(null_sha1, oldpath); @@ -105,7 +116,7 @@ int remove_path_from_gitmodules(const char *path) if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ return -1; - if (gitmodules_is_unmerged) + if (is_gitmodules_unmerged(&the_index)) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); submodule = submodule_from_path(null_sha1, path); @@ -156,7 +167,7 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, if (submodule) { if (submodule->ignore) handle_ignore_submodules_arg(diffopt, submodule->ignore); - else if (gitmodules_is_unmerged) + else if (is_gitmodules_unmerged(&the_index)) DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); } } @@ -224,23 +235,12 @@ void gitmodules_config(void) const char *work_tree = get_git_work_tree(); if (work_tree) { struct strbuf gitmodules_path = STRBUF_INIT; - int pos; strbuf_addstr(&gitmodules_path, work_tree); strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE); if (read_cache() < 0) die("index file corrupt"); - pos = cache_name_pos(GITMODULES_FILE, 11); - if (pos < 0) { /* .gitmodules not found or isn't merged */ - pos = -1 - pos; - if (active_nr > pos) { /* there is a .gitmodules */ - const struct cache_entry *ce = active_cache[pos]; - if (ce_namelen(ce) == 11 && - !memcmp(ce->name, GITMODULES_FILE, 11)) - gitmodules_is_unmerged = 1; - } - } - if (!gitmodules_is_unmerged) + if (!is_gitmodules_unmerged(&the_index)) git_config_from_file(git_modules_config, gitmodules_path.buf, NULL); strbuf_release(&gitmodules_path); @@ -1198,8 +1198,7 @@ static int get_next_submodule(struct child_process *cp, default_argv = "on-demand"; } } else { - if ((spf->default_option == RECURSE_SUBMODULES_OFF) || - gitmodules_is_unmerged) + if (spf->default_option == RECURSE_SUBMODULES_OFF) continue; if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) diff --git a/submodule.h b/submodule.h index b14660585f..8022faa591 100644 --- a/submodule.h +++ b/submodule.h @@ -33,6 +33,7 @@ struct submodule_update_strategy { }; #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL} +extern int is_gitmodules_unmerged(const struct index_state *istate); extern int is_staging_gitmodules_ok(const struct index_state *istate); extern int update_path_in_gitmodules(const char *oldpath, const char *newpath); extern int remove_path_from_gitmodules(const char *path); -- cgit v1.3 From 2184d4ba0cb86a7f40153cd46b03d3fa75b247d9 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 2 Aug 2017 12:49:22 -0700 Subject: 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 Signed-off-by: Junio C Hamano --- submodule.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index 3b0e70c51d..9d5eacaf9f 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) -- cgit v1.3