Skip to content

Commit 34e2ba0

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 91b8348 commit 34e2ba0

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

submodule.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ static struct oid_array ref_tips_before_fetch;
2727
static struct oid_array ref_tips_after_fetch;
2828

2929
/*
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.
3633
*/
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+
}
3849

3950
/*
4051
* 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)
7182
if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
7283
return -1;
7384

74-
if (gitmodules_is_unmerged)
85+
if (is_gitmodules_unmerged(&the_index))
7586
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
7687

7788
submodule = submodule_from_path(null_sha1, oldpath);
@@ -105,7 +116,7 @@ int remove_path_from_gitmodules(const char *path)
105116
if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
106117
return -1;
107118

108-
if (gitmodules_is_unmerged)
119+
if (is_gitmodules_unmerged(&the_index))
109120
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
110121

111122
submodule = submodule_from_path(null_sha1, path);
@@ -156,7 +167,7 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
156167
if (submodule) {
157168
if (submodule->ignore)
158169
handle_ignore_submodules_arg(diffopt, submodule->ignore);
159-
else if (gitmodules_is_unmerged)
170+
else if (is_gitmodules_unmerged(&the_index))
160171
DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
161172
}
162173
}
@@ -224,23 +235,12 @@ void gitmodules_config(void)
224235
const char *work_tree = get_git_work_tree();
225236
if (work_tree) {
226237
struct strbuf gitmodules_path = STRBUF_INIT;
227-
int pos;
228238
strbuf_addstr(&gitmodules_path, work_tree);
229239
strbuf_addstr(&gitmodules_path, "/" GITMODULES_FILE);
230240
if (read_cache() < 0)
231241
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-
}
242242

243-
if (!gitmodules_is_unmerged)
243+
if (!is_gitmodules_unmerged(&the_index))
244244
git_config_from_file(git_modules_config,
245245
gitmodules_path.buf, NULL);
246246
strbuf_release(&gitmodules_path);
@@ -1198,8 +1198,7 @@ static int get_next_submodule(struct child_process *cp,
11981198
default_argv = "on-demand";
11991199
}
12001200
} else {
1201-
if ((spf->default_option == RECURSE_SUBMODULES_OFF) ||
1202-
gitmodules_is_unmerged)
1201+
if (spf->default_option == RECURSE_SUBMODULES_OFF)
12031202
continue;
12041203
if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) {
12051204
if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))

submodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct submodule_update_strategy {
3333
};
3434
#define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
3535

36+
extern int is_gitmodules_unmerged(const struct index_state *istate);
3637
extern int is_staging_gitmodules_ok(const struct index_state *istate);
3738
extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
3839
extern int remove_path_from_gitmodules(const char *path);

0 commit comments

Comments
 (0)