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

Brandon Williams committed Aug 2, 2017 at 12:49 UTC 91b834807b98b620050fe534b6de93e223dbcbcf
4 files changed +20 -18
builtin/mv.c
+1 -1
@@ -81,7 +81,7 @@ static void prepare_move_submodule(const char *src, int first,
81 struct strbuf submodule_dotgit = STRBUF_INIT;
82 if (!S_ISGITLINK(active_cache[first]->ce_mode))
83 die(_("Directory %s is in index and no submodule?"), src);
84 - if (!is_staging_gitmodules_ok())
84 + if (!is_staging_gitmodules_ok(&the_index))
85 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
86 strbuf_addf(&submodule_dotgit, "%s/.git", src);
87 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
builtin/rm.c
+1 -1
@@ -286,7 +286,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
286 list.entry[list.nr].name = xstrdup(ce->name);
287 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
288 if (list.entry[list.nr++].is_submodule &&
289 - !is_staging_gitmodules_ok())
289 + !is_staging_gitmodules_ok(&the_index))
290 die (_("Please stage your changes to .gitmodules or stash them to proceed"));
291 }
292
submodule.c
+17 -15
@@ -37,18 +37,25 @@ static struct oid_array ref_tips_after_fetch;
37 static int gitmodules_is_unmerged;
38
39 /*
40 - * This flag is set if the .gitmodules file had unstaged modifications on
41 - * startup. This must be checked before allowing modifications to the
42 - * .gitmodules file with the intention to stage them later, because when
43 - * continuing we would stage the modifications the user didn't stage herself
44 - * too. That might change in a future version when we learn to stage the
45 - * changes we do ourselves without staging any previous modifications.
40 + * Check if the .gitmodules file has unstaged modifications. This must be
41 + * checked before allowing modifications to the .gitmodules file with the
42 + * intention to stage them later, because when continuing we would stage the
43 + * modifications the user didn't stage herself too. That might change in a
44 + * future version when we learn to stage the changes we do ourselves without
45 + * staging any previous modifications.
46 */
47 -static int gitmodules_is_modified;
48 -
49 -int is_staging_gitmodules_ok(void)
47 +int is_staging_gitmodules_ok(const struct index_state *istate)
48 {
51 - return !gitmodules_is_modified;
49 + int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
50 +
51 + if ((pos >= 0) && (pos < istate->cache_nr)) {
52 + struct stat st;
53 + if (lstat(GITMODULES_FILE, &st) == 0 &&
54 + ce_match_stat(istate->cache[pos], &st, 0) & DATA_CHANGED)
55 + return 0;
56 + }
57 +
58 + return 1;
59 }
60
61 /*
@@ -231,11 +238,6 @@ void gitmodules_config(void)
238 !memcmp(ce->name, GITMODULES_FILE, 11))
239 gitmodules_is_unmerged = 1;
240 }
234 - } else if (pos < active_nr) {
235 - struct stat st;
236 - if (lstat(GITMODULES_FILE, &st) == 0 &&
237 - ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
238 - gitmodules_is_modified = 1;
241 }
242
243 if (!gitmodules_is_unmerged)
submodule.h
+1 -1
@@ -33,7 +33,7 @@ struct submodule_update_strategy {
33 };
34 #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
35
36 -extern int is_staging_gitmodules_ok(void);
36 +extern int is_staging_gitmodules_ok(const struct index_state *istate);
37 extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
38 extern int remove_path_from_gitmodules(const char *path);
39 extern void stage_updated_gitmodules(void);