submodule: decouple url and submodule interest

Currently the submodule.<name>.url config option is used to determine if a given submodule is of interest to the user. This ends up being cumbersome in a world where we want to have different submodules checked out in different worktrees or a more generalized mechanism to select which submodules are of interest. In a future with worktree support for submodules, there will be multiple working trees, each of which may only need a subset of the submodules checked out. The URL (which is where the submodule repository can be obtained) should not differ between different working trees. It may also be convenient for users to more easily specify groups of submodules they are interested in as opposed to running "git submodule init <path>" on each submodule they want checked out in their working tree. To this end two config options are introduced, submodule.active and submodule.<name>.active. The submodule.active config holds a pathspec that specifies which submodules should exist in the working tree. The submodule.<name>.active config is a boolean flag used to indicate if that particular submodule should exist in the working tree. Its important to note that submodule.active functions differently than the other configuration options since it takes a pathspec. This allows users to adopt at least two new workflows: 1. Submodules can be grouped with a leading directory, such that a pathspec e.g. 'lib/' would cover all library-ish modules to allow those who are interested in library-ish modules to set "submodule.active = lib/" just once to say any and all modules in 'lib/' are interesting. 2. Once the pathspec-attribute feature is invented, users can label submodules with attributes to group them, so that a broad pathspec with attribute requirements, e.g. ':(attr:lib)', can be used to say any and all modules with the 'lib' attribute are interesting. Since the .gitattributes file, just like the .gitmodules file, is tracked by the superproject, when a submodule moves in the superproject tree, the project can adjust which path gets the attribute in .gitattributes, just like it can adjust which path has the submodule in .gitmodules. Neither of these two additional configuration options solve the problem of wanting different submodules checked out in different worktrees because multiple worktrees share .git/config. Only once per-worktree configurations become a reality can this be solved, but this is a necessary preparatory step for that future. Given these multiple ways to check if a submodule is of interest, the more fine-grained submodule.<name>.active option has the highest order of precedence followed by the pathspec check against submodule.active. To ensure backwards compatibility, if neither of these options are set, git falls back to checking the submodule.<name>.url option to determine if a submodule is interesting. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 17, 2017 at 15:38 UTC a086f921a725319a6f0c2c3aacf676c890b3ce3e
3 files changed +110 -10
Documentation/config.txt
+13 -2
@@ -2920,8 +2920,9 @@ submodule.<name>.url::
2920 The URL for a submodule. This variable is copied from the .gitmodules
2921 file to the git config via 'git submodule init'. The user can change
2922 the configured URL before obtaining the submodule via 'git submodule
2923 - update'. After obtaining the submodule, the presence of this variable
2924 - is used as a sign whether the submodule is of interest to git commands.
2923 + update'. If neither submodule.<name>.active or submodule.active are
2924 + set, the presence of this variable is used as a fallback to indicate
2925 + whether the submodule is of interest to git commands.
2926 See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
2927
2928 submodule.<name>.update::
@@ -2959,6 +2960,16 @@ submodule.<name>.ignore::
2960 "--ignore-submodules" option. The 'git submodule' commands are not
2961 affected by this setting.
2962
2963 +submodule.<name>.active::
2964 + Boolean value indicating if the submodule is of interest to git
2965 + commands. This config option takes precedence over the
2966 + submodule.active config option.
2967 +
2968 +submodule.active::
2969 + A repeated field which contains a pathspec used to match against a
2970 + submodule's path to determine if the submodule is of interest to git
2971 + commands.
2972 +
2973 submodule.fetchJobs::
2974 Specifies how many submodules are fetched/cloned at the same time.
2975 A positive integer allows up to that number of submodules fetched
submodule.c
+42 -8
@@ -212,25 +212,59 @@ void gitmodules_config_sha1(const unsigned char *commit_sha1)
212 }
213
214 /*
215 + * NEEDSWORK: With the addition of different configuration options to determine
216 + * if a submodule is of interests, the validity of this function's name comes
217 + * into question. Once the dust has settled and more concrete terminology is
218 + * decided upon, come up with a more proper name for this function. One
219 + * potential candidate could be 'is_submodule_active()'.
220 + *
221 * Determine if a submodule has been initialized at a given 'path'
222 */
223 int is_submodule_initialized(const char *path)
224 {
225 int ret = 0;
220 - const struct submodule *module = NULL;
226 + char *key = NULL;
227 + char *value = NULL;
228 + const struct string_list *sl;
229 + const struct submodule *module = submodule_from_path(null_sha1, path);
230
222 - module = submodule_from_path(null_sha1, path);
231 + /* early return if there isn't a path->module mapping */
232 + if (!module)
233 + return 0;
234
224 - if (module) {
225 - char *key = xstrfmt("submodule.%s.url", module->name);
226 - char *value = NULL;
235 + /* submodule.<name>.active is set */
236 + key = xstrfmt("submodule.%s.active", module->name);
237 + if (!git_config_get_bool(key, &ret)) {
238 + free(key);
239 + return ret;
240 + }
241 + free(key);
242
228 - ret = !git_config_get_string(key, &value);
243 + /* submodule.active is set */
244 + sl = git_config_get_value_multi("submodule.active");
245 + if (sl) {
246 + struct pathspec ps;
247 + struct argv_array args = ARGV_ARRAY_INIT;
248 + const struct string_list_item *item;
249
230 - free(value);
231 - free(key);
250 + for_each_string_list_item(item, sl) {
251 + argv_array_push(&args, item->string);
252 + }
253 +
254 + parse_pathspec(&ps, 0, 0, NULL, args.argv);
255 + ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
256 +
257 + argv_array_clear(&args);
258 + clear_pathspec(&ps);
259 + return ret;
260 }
261
262 + /* fallback to checking if the URL is set */
263 + key = xstrfmt("submodule.%s.url", module->name);
264 + ret = !git_config_get_string(key, &value);
265 +
266 + free(value);
267 + free(key);
268 return ret;
269 }
270
t/t7413-submodule-is-active.sh
+55
@@ -28,4 +28,59 @@ test_expect_success 'is-active works with urls' '
28 git -C super submodule--helper is-active sub1
29 '
30
31 +test_expect_success 'is-active works with submodule.<name>.active config' '
32 + test_when_finished "git -C super config --unset submodule.sub1.active" &&
33 + test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
34 +
35 + git -C super config --bool submodule.sub1.active "false" &&
36 + test_must_fail git -C super submodule--helper is-active sub1 &&
37 +
38 + git -C super config --bool submodule.sub1.active "true" &&
39 + git -C super config --unset submodule.sub1.URL &&
40 + git -C super submodule--helper is-active sub1
41 +'
42 +
43 +test_expect_success 'is-active works with basic submodule.active config' '
44 + test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
45 + test_when_finished "git -C super config --unset-all submodule.active" &&
46 +
47 + git -C super config --add submodule.active "." &&
48 + git -C super config --unset submodule.sub1.URL &&
49 +
50 + git -C super submodule--helper is-active sub1 &&
51 + git -C super submodule--helper is-active sub2
52 +'
53 +
54 +test_expect_success 'is-active correctly works with paths that are not submodules' '
55 + test_when_finished "git -C super config --unset-all submodule.active" &&
56 +
57 + test_must_fail git -C super submodule--helper is-active not-a-submodule &&
58 +
59 + git -C super config --add submodule.active "." &&
60 + test_must_fail git -C super submodule--helper is-active not-a-submodule
61 +'
62 +
63 +test_expect_success 'is-active works with exclusions in submodule.active config' '
64 + test_when_finished "git -C super config --unset-all submodule.active" &&
65 +
66 + git -C super config --add submodule.active "." &&
67 + git -C super config --add submodule.active ":(exclude)sub1" &&
68 +
69 + test_must_fail git -C super submodule--helper is-active sub1 &&
70 + git -C super submodule--helper is-active sub2
71 +'
72 +
73 +test_expect_success 'is-active with submodule.active and submodule.<name>.active' '
74 + test_when_finished "git -C super config --unset-all submodule.active" &&
75 + test_when_finished "git -C super config --unset submodule.sub1.active" &&
76 + test_when_finished "git -C super config --unset submodule.sub2.active" &&
77 +
78 + git -C super config --add submodule.active "sub1" &&
79 + git -C super config --bool submodule.sub1.active "false" &&
80 + git -C super config --bool submodule.sub2.active "true" &&
81 +
82 + test_must_fail git -C super submodule--helper is-active sub1 &&
83 + git -C super submodule--helper is-active sub2
84 +'
85 +
86 test_done