Skip to content

Commit 91b8348

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

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void prepare_move_submodule(const char *src, int first,
8181
struct strbuf submodule_dotgit = STRBUF_INIT;
8282
if (!S_ISGITLINK(active_cache[first]->ce_mode))
8383
die(_("Directory %s is in index and no submodule?"), src);
84-
if (!is_staging_gitmodules_ok())
84+
if (!is_staging_gitmodules_ok(&the_index))
8585
die(_("Please stage your changes to .gitmodules or stash them to proceed"));
8686
strbuf_addf(&submodule_dotgit, "%s/.git", src);
8787
*submodule_gitfile = read_gitfile(submodule_dotgit.buf);

builtin/rm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
286286
list.entry[list.nr].name = xstrdup(ce->name);
287287
list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
288288
if (list.entry[list.nr++].is_submodule &&
289-
!is_staging_gitmodules_ok())
289+
!is_staging_gitmodules_ok(&the_index))
290290
die (_("Please stage your changes to .gitmodules or stash them to proceed"));
291291
}
292292

submodule.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@ static struct oid_array ref_tips_after_fetch;
3737
static int gitmodules_is_unmerged;
3838

3939
/*
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.
4646
*/
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)
5048
{
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;
5259
}
5360

5461
/*
@@ -231,11 +238,6 @@ void gitmodules_config(void)
231238
!memcmp(ce->name, GITMODULES_FILE, 11))
232239
gitmodules_is_unmerged = 1;
233240
}
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;
239241
}
240242

241243
if (!gitmodules_is_unmerged)

submodule.h

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

36-
extern int is_staging_gitmodules_ok(void);
36+
extern int is_staging_gitmodules_ok(const struct index_state *istate);
3737
extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
3838
extern int remove_path_from_gitmodules(const char *path);
3939
extern void stage_updated_gitmodules(void);

0 commit comments

Comments
 (0)