reset/checkout/read-tree: unify config callback for submodule recursion

The callback function is essentially duplicated 3 times. Remove all of them and offer a new callback function, that lives in submodule.c By putting the callback function there, we no longer need the function 'set_config_update_recurse_submodules', nor duplicate the global variable in each builtin as well as submodule.c In the three builtins we have different 2 ways how to load the .gitmodules and config file, which are slightly different. git-checkout has to load the submodule config all the time due to 23b4c7bcc5 (checkout: Use submodule.*.ignore settings from .git/config and .gitmodules, 2010-08-28) git-reset and git-read-tree do not respect these diff settings, so loading the submodule configuration is optional. Also put that into submodule.c for code deduplication. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed May 26, 2017 at 12:10 UTC d7a3803f9e83242adac0f02af843ef0520c71f0a
5 files changed +38 -83
builtin/checkout.c
+1 -26
@@ -21,31 +21,12 @@
21 #include "submodule-config.h"
22 #include "submodule.h"
23
24 -static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
25 -
24 static const char * const checkout_usage[] = {
25 N_("git checkout [<options>] <branch>"),
26 N_("git checkout [<options>] [<branch>] -- <file>..."),
27 NULL,
28 };
29
32 -static int option_parse_recurse_submodules(const struct option *opt,
33 - const char *arg, int unset)
34 -{
35 - if (unset) {
36 - recurse_submodules = RECURSE_SUBMODULES_OFF;
37 - return 0;
38 - }
39 - if (arg)
40 - recurse_submodules =
41 - parse_update_recurse_submodules_arg(opt->long_name,
42 - arg);
43 - else
44 - recurse_submodules = RECURSE_SUBMODULES_ON;
45 -
46 - return 0;
47 -}
48 -
30 struct checkout_opts {
31 int patch_mode;
32 int quiet;
@@ -1184,7 +1165,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1165 N_("do not check if another worktree is holding the given ref")),
1166 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
1167 "checkout", "control recursive updating of submodules",
1187 - PARSE_OPT_OPTARG, option_parse_recurse_submodules },
1168 + PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
1169 OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
1170 OPT_END(),
1171 };
@@ -1215,12 +1196,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1196 git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
1197 }
1198
1218 - if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1219 - git_config(submodule_config, NULL);
1220 - if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT)
1221 - set_config_update_recurse_submodules(recurse_submodules);
1222 - }
1223 -
1199 if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
1200 die(_("-b, -B and --orphan are mutually exclusive"));
1201
builtin/read-tree.c
+3 -25
@@ -21,7 +21,6 @@
21 static int nr_trees;
22 static int read_empty;
23 static struct tree *trees[MAX_UNPACK_TREES];
24 -static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
24
25 static int list_tree(unsigned char *sha1)
26 {
@@ -99,23 +98,6 @@ static int debug_merge(const struct cache_entry * const *stages,
98 return 0;
99 }
100
102 -static int option_parse_recurse_submodules(const struct option *opt,
103 - const char *arg, int unset)
104 -{
105 - if (unset) {
106 - recurse_submodules = RECURSE_SUBMODULES_OFF;
107 - return 0;
108 - }
109 - if (arg)
110 - recurse_submodules =
111 - parse_update_recurse_submodules_arg(opt->long_name,
112 - arg);
113 - else
114 - recurse_submodules = RECURSE_SUBMODULES_ON;
115 -
116 - return 0;
117 -}
118 -
101 static struct lock_file lock_file;
102
103 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
@@ -159,7 +141,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
141 N_("debug unpack-trees")),
142 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
143 "checkout", "control recursive updating of submodules",
162 - PARSE_OPT_OPTARG, option_parse_recurse_submodules },
144 + PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
145 OPT_END()
146 };
147
@@ -173,13 +155,9 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
155 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
156 read_tree_usage, 0);
157
176 - hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
158 + load_submodule_cache();
159
178 - if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT) {
179 - gitmodules_config();
180 - git_config(submodule_config, NULL);
181 - set_config_update_recurse_submodules(RECURSE_SUBMODULES_ON);
182 - }
160 + hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
161
162 prefix_set = opts.prefix ? 1 : 0;
163 if (1 < opts.merge + opts.reset + prefix_set)
builtin/reset.c
+2 -25
@@ -24,25 +24,6 @@
24 #include "submodule.h"
25 #include "submodule-config.h"
26
27 -static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
28 -
29 -static int option_parse_recurse_submodules(const struct option *opt,
30 - const char *arg, int unset)
31 -{
32 - if (unset) {
33 - recurse_submodules = RECURSE_SUBMODULES_OFF;
34 - return 0;
35 - }
36 - if (arg)
37 - recurse_submodules =
38 - parse_update_recurse_submodules_arg(opt->long_name,
39 - arg);
40 - else
41 - recurse_submodules = RECURSE_SUBMODULES_ON;
42 -
43 - return 0;
44 -}
45 -
27 static const char * const git_reset_usage[] = {
28 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
29 N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
@@ -306,7 +287,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
287 N_("reset HEAD but keep local changes"), KEEP),
288 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
289 "reset", "control recursive updating of submodules",
309 - PARSE_OPT_OPTARG, option_parse_recurse_submodules },
290 + PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
291 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
292 OPT_BOOL('N', "intent-to-add", &intent_to_add,
293 N_("record only the fact that removed paths will be added later")),
@@ -319,11 +300,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
300 PARSE_OPT_KEEP_DASHDASH);
301 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
302
322 - if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT) {
323 - gitmodules_config();
324 - git_config(submodule_config, NULL);
325 - set_config_update_recurse_submodules(RECURSE_SUBMODULES_ON);
326 - }
303 + load_submodule_cache();
304
305 unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", oid.hash);
306 if (unborn) {
submodule.c
+27 -6
@@ -18,7 +18,7 @@
18 #include "worktree.h"
19
20 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
21 -static int config_update_recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
21 +static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
22 static int parallel_jobs = 1;
23 static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP;
24 static int initialized_fetch_ref_tips;
@@ -169,6 +169,32 @@ int submodule_config(const char *var, const char *value, void *cb)
169 return 0;
170 }
171
172 +int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
173 + const char *arg, int unset)
174 +{
175 + if (unset) {
176 + config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
177 + return 0;
178 + }
179 + if (arg)
180 + config_update_recurse_submodules =
181 + parse_update_recurse_submodules_arg(opt->long_name,
182 + arg);
183 + else
184 + config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
185 +
186 + return 0;
187 +}
188 +
189 +void load_submodule_cache(void)
190 +{
191 + if (config_update_recurse_submodules == RECURSE_SUBMODULES_OFF)
192 + return;
193 +
194 + gitmodules_config();
195 + git_config(submodule_config, NULL);
196 +}
197 +
198 void gitmodules_config(void)
199 {
200 const char *work_tree = get_git_work_tree();
@@ -596,11 +622,6 @@ void set_config_fetch_recurse_submodules(int value)
622 config_fetch_recurse_submodules = value;
623 }
624
599 -void set_config_update_recurse_submodules(int value)
600 -{
601 - config_update_recurse_submodules = value;
602 -}
603 -
625 int should_update_submodules(void)
626 {
627 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
submodule.h
+5 -1
@@ -39,6 +39,11 @@ extern void stage_updated_gitmodules(void);
39 extern void set_diffopt_flags_from_submodule_config(struct diff_options *,
40 const char *path);
41 extern int submodule_config(const char *var, const char *value, void *cb);
42 +
43 +struct option;
44 +int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
45 + const char *arg, int unset);
46 +void load_submodule_cache(void);
47 extern void gitmodules_config(void);
48 extern void gitmodules_config_sha1(const unsigned char *commit_sha1);
49 extern int is_submodule_initialized(const char *path);
@@ -65,7 +70,6 @@ extern void show_submodule_inline_diff(FILE *f, const char *path,
70 const char *del, const char *add, const char *reset,
71 const struct diff_options *opt);
72 extern void set_config_fetch_recurse_submodules(int value);
68 -extern void set_config_update_recurse_submodules(int value);
73 /* Check if we want to update any submodule.*/
74 extern int should_update_submodules(void);
75 /*