Skip to content

Commit a086f92

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

File tree

3 files changed

+110
-10
lines changed

3 files changed

+110
-10
lines changed

Documentation/config.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,8 +2920,9 @@ submodule.<name>.url::
29202920
The URL for a submodule. This variable is copied from the .gitmodules
29212921
file to the git config via 'git submodule init'. The user can change
29222922
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.
29252926
See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
29262927

29272928
submodule.<name>.update::
@@ -2959,6 +2960,16 @@ submodule.<name>.ignore::
29592960
"--ignore-submodules" option. The 'git submodule' commands are not
29602961
affected by this setting.
29612962

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+
29622973
submodule.fetchJobs::
29632974
Specifies how many submodules are fetched/cloned at the same time.
29642975
A positive integer allows up to that number of submodules fetched

submodule.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,59 @@ void gitmodules_config_sha1(const unsigned char *commit_sha1)
212212
}
213213

214214
/*
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+
*
215221
* Determine if a submodule has been initialized at a given 'path'
216222
*/
217223
int is_submodule_initialized(const char *path)
218224
{
219225
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);
221230

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;
223234

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);
227242

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;
229249

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;
232260
}
233261

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);
234268
return ret;
235269
}
236270

t/t7413-submodule-is-active.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,59 @@ test_expect_success 'is-active works with urls' '
2828
git -C super submodule--helper is-active sub1
2929
'
3030

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+
3186
test_done

0 commit comments

Comments
 (0)