Skip to content

Commit 5fee995

Browse files
jlehmanngitster
authored andcommitted
submodule.c: add .gitmodules staging helper functions
Add the new is_staging_gitmodules_ok() and stage_updated_gitmodules() functions to submodule.c. The first makes it possible for call sites to see if the .gitmodules file did contain any unstaged modifications they would accidentally stage in addition to those they intend to stage themselves. The second function stages all modifications to the .gitmodules file, both will be used by subsequent patches for the mv and rm commands. Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a88c915 commit 5fee995

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

submodule.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "string-list.h"
1111
#include "sha1-array.h"
1212
#include "argv-array.h"
13+
#include "blob.h"
1314

1415
static struct string_list config_name_for_path;
1516
static struct string_list config_fetch_recurse_submodules_for_name;
@@ -30,6 +31,51 @@ static struct sha1_array ref_tips_after_fetch;
3031
*/
3132
static int gitmodules_is_unmerged;
3233

34+
/*
35+
* This flag is set if the .gitmodules file had unstaged modifications on
36+
* startup. This must be checked before allowing modifications to the
37+
* .gitmodules file with the intention to stage them later, because when
38+
* continuing we would stage the modifications the user didn't stage herself
39+
* too. That might change in a future version when we learn to stage the
40+
* changes we do ourselves without staging any previous modifications.
41+
*/
42+
static int gitmodules_is_modified;
43+
44+
45+
int is_staging_gitmodules_ok(void)
46+
{
47+
return !gitmodules_is_modified;
48+
}
49+
50+
void stage_updated_gitmodules(void)
51+
{
52+
struct strbuf buf = STRBUF_INIT;
53+
struct stat st;
54+
int pos;
55+
struct cache_entry *ce;
56+
int namelen = strlen(".gitmodules");
57+
58+
pos = cache_name_pos(".gitmodules", namelen);
59+
if (pos < 0) {
60+
warning(_("could not find .gitmodules in index"));
61+
return;
62+
}
63+
ce = active_cache[pos];
64+
ce->ce_flags = namelen;
65+
if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
66+
die(_("reading updated .gitmodules failed"));
67+
if (lstat(".gitmodules", &st) < 0)
68+
die_errno(_("unable to stat updated .gitmodules"));
69+
fill_stat_cache_info(ce, &st);
70+
ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
71+
if (remove_cache_entry_at(pos) < 0)
72+
die(_("unable to remove .gitmodules from index"));
73+
if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
74+
die(_("adding updated .gitmodules failed"));
75+
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
76+
die(_("staging updated .gitmodules failed"));
77+
}
78+
3379
static int add_submodule_odb(const char *path)
3480
{
3581
struct strbuf objects_directory = STRBUF_INIT;
@@ -116,6 +162,11 @@ void gitmodules_config(void)
116162
!memcmp(ce->name, ".gitmodules", 11))
117163
gitmodules_is_unmerged = 1;
118164
}
165+
} else if (pos < active_nr) {
166+
struct stat st;
167+
if (lstat(".gitmodules", &st) == 0 &&
168+
ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
169+
gitmodules_is_modified = 1;
119170
}
120171

121172
if (!gitmodules_is_unmerged)

submodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ enum {
1111
RECURSE_SUBMODULES_ON = 2
1212
};
1313

14+
int is_staging_gitmodules_ok(void);
15+
void stage_updated_gitmodules(void);
1416
void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
1517
const char *path);
1618
int submodule_config(const char *var, const char *value, void *cb);

0 commit comments

Comments
 (0)