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 <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Aug 2, 2017 at 12:49 UTC 34e2ba04be4f8e11d91b2508aa8ca84148fe63f1
2 files changed +24 -24
submodule.c
+23 -24
@@ -27,14 +27,25 @@ static struct oid_array ref_tips_before_fetch;
27 static struct oid_array ref_tips_after_fetch;
28
29 /*
30 - * The following flag is set if the .gitmodules file is unmerged. We then
31 - * disable recursion for all submodules where .git/config doesn't have a
32 - * matching config entry because we can't guess what might be configured in
33 - * .gitmodules unless the user resolves the conflict. When a command line
34 - * option is given (which always overrides configuration) this flag will be
35 - * ignored.
30 + * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
31 + * will be disabled because we can't guess what might be configured in
32 + * .gitmodules unless the user resolves the conflict.
33 */
37 -static int gitmodules_is_unmerged;
34 +int is_gitmodules_unmerged(const struct index_state *istate)
35 +{
36 + int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
37 + if (pos < 0) { /* .gitmodules not found or isn't merged */
38 + pos = -1 - pos;
39 + if (istate->cache_nr > pos) { /* there is a .gitmodules */
40 + const struct cache_entry *ce = istate->cache[pos];
41 + if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
42 + !strcmp(ce->name, GITMODULES_FILE))
43 + return 1;
44 + }
45 + }
46 +
47 + return 0;
48 +}
49
50 /*
51 * 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)
82 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
83 return -1;
84
74 - if (gitmodules_is_unmerged)
85 + if (is_gitmodules_unmerged(&the_index))
86 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
87
88 submodule = submodule_from_path(null_sha1, oldpath);
@@ -105,7 +116,7 @@ int remove_path_from_gitmodules(const char *path)
116 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
117 return -1;
118
108 - if (gitmodules_is_unmerged)
119 + if (is_gitmodules_unmerged(&the_index))
120 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
121
122 submodule = submodule_from_path(null_sha1, path);
@@ -156,7 +167,7 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
167 if (submodule) {
168 if (submodule->ignore)
169 handle_ignore_submodules_arg(diffopt, submodule->ignore);
159 - else if (gitmodules_is_unmerged)
170 + else if (is_gitmodules_unmerged(&the_index))
171 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
172 }
173 }
@@ -224,23 +235,12 @@ void gitmodules_config(void)
235 const char *work_tree = get_git_work_tree();
236 if (work_tree) {
237 struct strbuf gitmodules_path = STRBUF_INIT;
227 - int pos;
238 strbuf_addstr(&gitmodules_path, work_tree);
239 strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE);
240 if (read_cache() < 0)
241 die("index file corrupt");
232 - pos = cache_name_pos(GITMODULES_FILE, 11);
233 - if (pos < 0) { /* .gitmodules not found or isn't merged */
234 - pos = -1 - pos;
235 - if (active_nr > pos) { /* there is a .gitmodules */
236 - const struct cache_entry *ce = active_cache[pos];
237 - if (ce_namelen(ce) == 11 &&
238 - !memcmp(ce->name, GITMODULES_FILE, 11))
239 - gitmodules_is_unmerged = 1;
240 - }
241 - }
242
243 - if (!gitmodules_is_unmerged)
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);
@@ -1198,8 +1198,7 @@ static int get_next_submodule(struct child_process *cp,
1198 default_argv = "on-demand";
1199 }
1200 } else {
1201 - if ((spf->default_option == RECURSE_SUBMODULES_OFF) ||
1202 - gitmodules_is_unmerged)
1201 + if (spf->default_option == RECURSE_SUBMODULES_OFF)
1202 continue;
1203 if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) {
1204 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
submodule.h
+1
@@ -33,6 +33,7 @@ struct submodule_update_strategy {
33 };
34 #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
35
36 +extern int is_gitmodules_unmerged(const struct index_state *istate);
37 extern int is_staging_gitmodules_ok(const struct index_state *istate);
38 extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
39 extern int remove_path_from_gitmodules(const char *path);