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 <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed Jul 30, 2013 at 21:50 UTC 5fee995244e13fd59500425d49038b00499396f8
2 files changed +53
submodule.c
+51
@@ -10,6 +10,7 @@
10 #include "string-list.h"
11 #include "sha1-array.h"
12 #include "argv-array.h"
13 +#include "blob.h"
14
15 static struct string_list config_name_for_path;
16 static struct string_list config_fetch_recurse_submodules_for_name;
@@ -30,6 +31,51 @@ static struct sha1_array ref_tips_after_fetch;
31 */
32 static int gitmodules_is_unmerged;
33
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 +
79 static int add_submodule_odb(const char *path)
80 {
81 struct strbuf objects_directory = STRBUF_INIT;
@@ -116,6 +162,11 @@ void gitmodules_config(void)
162 !memcmp(ce->name, ".gitmodules", 11))
163 gitmodules_is_unmerged = 1;
164 }
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;
170 }
171
172 if (!gitmodules_is_unmerged)
submodule.h
+2
@@ -11,6 +11,8 @@ enum {
11 RECURSE_SUBMODULES_ON = 2
12 };
13
14 +int is_staging_gitmodules_ok(void);
15 +void stage_updated_gitmodules(void);
16 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
17 const char *path);
18 int submodule_config(const char *var, const char *value, void *cb);