Submodules: Don't parse .gitmodules when it contains, merge conflicts

Commands like "git status", "git diff" and "git fetch" would fail when the .gitmodules file contained merge conflicts because the config parser would call die() when hitting the conflict markers: "fatal: bad config file line <n> in <path>/.gitmodules" While this behavior was on the safe side, it is really unhelpful to the user to have commands like status and diff fail, as these are needed to find out what's going on. And the error message is only mildly helpful, as it points to the right file but doesn't mention that it is unmerged. Users of git gui were not shown any conflicts at all when this happened. Improve the situation by checking if the index records .gitmodules as unmerged. When that is the case we can't make any assumptions about the configuration to be found there after the merge conflict is resolved by the user, so assume that all recursion is disabled unless .git/config or the global config say otherwise. As soon as the merge conflict is resolved and the .gitmodules file has been staged subsequent commands again honor any configuration done there. Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed May 14, 2011 at 18:26 UTC d4e98b581b5b3b70ca58cb6677ec02bee4ae7007
2 files changed +32 -5
submodule.c
+29 -2
@@ -14,6 +14,15 @@ static struct string_list config_fetch_recurse_submodules_for_name;
14 static struct string_list config_ignore_for_name;
15 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
16 static struct string_list changed_submodule_paths;
17 +/*
18 + * The following flag is set if the .gitmodules file is unmerged. We then
19 + * disable recursion for all submodules where .git/config doesn't have a
20 + * matching config entry because we can't guess what might be configured in
21 + * .gitmodules unless the user resolves the conflict. When a command line
22 + * option is given (which always overrides configuration) this flag will be
23 + * ignored.
24 + */
25 +static int gitmodules_is_unmerged;
26
27 static int add_submodule_odb(const char *path)
28 {
@@ -63,6 +72,8 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
72 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
73 if (ignore_option)
74 handle_ignore_submodules_arg(diffopt, ignore_option->util);
75 + else if (gitmodules_is_unmerged)
76 + DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
77 }
78 }
79
@@ -82,9 +93,24 @@ void gitmodules_config(void)
93 const char *work_tree = get_git_work_tree();
94 if (work_tree) {
95 struct strbuf gitmodules_path = STRBUF_INIT;
96 + int pos;
97 strbuf_addstr(&gitmodules_path, work_tree);
98 strbuf_addstr(&gitmodules_path, "/.gitmodules");
87 - git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
99 + if (read_cache() < 0)
100 + die("index file corrupt");
101 + pos = cache_name_pos(".gitmodules", 11);
102 + if (pos < 0) { /* .gitmodules not found or isn't merged */
103 + pos = -1 - pos;
104 + if (active_nr > pos) { /* there is a .gitmodules */
105 + const struct cache_entry *ce = active_cache[pos];
106 + if (ce_namelen(ce) == 11 &&
107 + !memcmp(ce->name, ".gitmodules", 11))
108 + gitmodules_is_unmerged = 1;
109 + }
110 + }
111 +
112 + if (!gitmodules_is_unmerged)
113 + git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
114 strbuf_release(&gitmodules_path);
115 }
116 }
@@ -434,7 +460,8 @@ int fetch_populated_submodules(int num_options, const char **options,
460 default_argv = "on-demand";
461 }
462 } else {
437 - if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF)
463 + if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
464 + gitmodules_is_unmerged)
465 continue;
466 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
467 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
t/t7506-status-submodule.sh
+3 -3
@@ -196,7 +196,7 @@ AA .gitmodules
196 A sub1
197 EOF
198
199 -test_expect_failure 'status with merge conflict in .gitmodules' '
199 +test_expect_success 'status with merge conflict in .gitmodules' '
200 git clone . super &&
201 test_create_repo_with_commit sub1 &&
202 test_tick &&
@@ -255,7 +255,7 @@ index badaa4c,44f999a..0000000
255 ++>>>>>>> add_sub1
256 EOF
257
258 -test_expect_failure 'diff with merge conflict in .gitmodules' '
258 +test_expect_success 'diff with merge conflict in .gitmodules' '
259 (
260 cd super &&
261 git diff >../diff_actual 2>&1
@@ -263,7 +263,7 @@ test_expect_failure 'diff with merge conflict in .gitmodules' '
263 test_cmp diff_actual diff_expect
264 '
265
266 -test_expect_failure 'diff --submodule with merge conflict in .gitmodules' '
266 +test_expect_success 'diff --submodule with merge conflict in .gitmodules' '
267 (
268 cd super &&
269 git diff --submodule >../diff_submodule_actual 2>&1