Merge branch 'bw/submodule-is-active'

"what URL do we want to update this submodule?" and "are we
interested in this submodule?" are split into two distinct
concepts, and then the way used to express the latter got extended,
paving a way to make it easier to manage a project with many
submodules and make it possible to later extend use of multiple
worktrees for a project with submodules.

* bw/submodule-is-active:
  submodule add: respect submodule.active and submodule.<name>.active
  submodule--helper init: set submodule.<name>.active
  clone: teach --recurse-submodules to optionally take a pathspec
  submodule init: initialize active submodules
  submodule: decouple url and submodule interest
  submodule--helper clone: check for configured submodules using helper
  submodule sync: use submodule--helper is-active
  submodule sync: skip work for inactive submodules
  submodule status: use submodule--helper is-active
  submodule--helper: add is-active subcommand
This commit is contained in:
Junio C Hamano 2017-03-30 14:07:14 -07:00
commit a93dcb0a56
9 changed files with 446 additions and 55 deletions

View File

@ -3013,8 +3013,9 @@ submodule.<name>.url::
The URL for a submodule. This variable is copied from the .gitmodules
file to the git config via 'git submodule init'. The user can change
the configured URL before obtaining the submodule via 'git submodule
update'. After obtaining the submodule, the presence of this variable
is used as a sign whether the submodule is of interest to git commands.
update'. If neither submodule.<name>.active or submodule.active are
set, the presence of this variable is used as a fallback to indicate
whether the submodule is of interest to git commands.
See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
submodule.<name>.update::
@ -3052,6 +3053,16 @@ submodule.<name>.ignore::
"--ignore-submodules" option. The 'git submodule' commands are not
affected by this setting.
submodule.<name>.active::
Boolean value indicating if the submodule is of interest to git
commands. This config option takes precedence over the
submodule.active config option.
submodule.active::
A repeated field which contains a pathspec used to match against a
submodule's path to determine if the submodule is of interest to git
commands.
submodule.fetchJobs::
Specifies how many submodules are fetched/cloned at the same time.
A positive integer allows up to that number of submodules fetched

View File

@ -14,7 +14,7 @@ SYNOPSIS
[-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
[--dissociate] [--separate-git-dir <git dir>]
[--depth <depth>] [--[no-]single-branch]
[--recursive | --recurse-submodules] [--[no-]shallow-submodules]
[--recurse-submodules] [--[no-]shallow-submodules]
[--jobs <n>] [--] <repository> [<directory>]
DESCRIPTION
@ -215,10 +215,14 @@ objects from the source repository into a pack in the cloned repository.
branch when `--single-branch` clone was made, no remote-tracking
branch is created.
--recursive::
--recurse-submodules::
After the clone is created, initialize all submodules within,
using their default settings. This is equivalent to running
--recurse-submodules[=<pathspec]::
After the clone is created, initialize and clone submodules
within based on the provided pathspec. If no pathspec is
provided, all submodules are initialized and cloned.
Submodules are initialized and cloned using their default
settings. The resulting clone has `submodule.active` set to
the provided pathspec, or "." (meaning all submodules) if no
pathspec is provided. This is equivalent to running
`git submodule update --init --recursive` immediately after
the clone is finished. This option is ignored if the cloned
repository does not have a worktree/checkout (i.e. if any of

View File

@ -129,7 +129,9 @@ init [--] [<path>...]::
repository will be assumed to be upstream.
+
Optional <path> arguments limit which submodules will be initialized.
If no path is specified, all submodules are initialized.
If no path is specified and submodule.active has been configured, submodules
configured to be active will be initialized, otherwise all submodules are
initialized.
+
When present, it will also copy the value of `submodule.$name.update`.
This command does not alter existing information in .git/config.

View File

@ -39,7 +39,7 @@ static const char * const builtin_clone_usage[] = {
};
static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
static int option_local = -1, option_no_hardlinks, option_shared;
static int option_shallow_submodules;
static int deepen;
static char *option_template, *option_depth, *option_since;
@ -56,6 +56,21 @@ static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
static int option_dissociate;
static int max_jobs = -1;
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
static int recurse_submodules_cb(const struct option *opt,
const char *arg, int unset)
{
if (unset)
string_list_clear((struct string_list *)opt->value, 0);
else if (arg)
string_list_append((struct string_list *)opt->value, arg);
else
string_list_append((struct string_list *)opt->value,
(const char *)opt->defval);
return 0;
}
static struct option builtin_clone_options[] = {
OPT__VERBOSITY(&option_verbosity),
@ -74,10 +89,13 @@ static struct option builtin_clone_options[] = {
N_("don't use local hardlinks, always copy")),
OPT_BOOL('s', "shared", &option_shared,
N_("setup as shared repository")),
OPT_BOOL(0, "recursive", &option_recursive,
N_("initialize submodules in the clone")),
OPT_BOOL(0, "recurse-submodules", &option_recursive,
N_("initialize submodules in the clone")),
{ OPTION_CALLBACK, 0, "recursive", &option_recurse_submodules,
N_("pathspec"), N_("initialize submodules in the clone"),
PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, recurse_submodules_cb,
(intptr_t)"." },
{ OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
N_("pathspec"), N_("initialize submodules in the clone"),
PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
OPT_INTEGER('j', "jobs", &max_jobs,
N_("number of submodules cloned in parallel")),
OPT_STRING(0, "template", &option_template, N_("template-directory"),
@ -733,7 +751,7 @@ static int checkout(int submodule_progress)
err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
oid_to_hex(&oid), "1", NULL);
if (!err && option_recursive) {
if (!err && (option_recurse_submodules.nr > 0)) {
struct argv_array args = ARGV_ARRAY_INIT;
argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL);
@ -957,7 +975,25 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
fprintf(stderr, _("Cloning into '%s'...\n"), dir);
}
if (option_recursive) {
if (option_recurse_submodules.nr > 0) {
struct string_list_item *item;
struct strbuf sb = STRBUF_INIT;
/* remove duplicates */
string_list_sort(&option_recurse_submodules);
string_list_remove_duplicates(&option_recurse_submodules, 0);
/*
* NEEDSWORK: In a multi-working-tree world, this needs to be
* set in the per-worktree config.
*/
for_each_string_list_item(item, &option_recurse_submodules) {
strbuf_addf(&sb, "submodule.active=%s",
item->string);
string_list_append(&option_config,
strbuf_detach(&sb, NULL));
}
if (option_required_reference.nr &&
option_optional_reference.nr)
die(_("clone --recursive is not compatible with "

View File

@ -270,6 +270,29 @@ static int module_list_compute(int argc, const char **argv,
return result;
}
static void module_list_active(struct module_list *list)
{
int i;
struct module_list active_modules = MODULE_LIST_INIT;
gitmodules_config();
for (i = 0; i < list->nr; i++) {
const struct cache_entry *ce = list->entries[i];
if (!is_submodule_initialized(ce->name))
continue;
ALLOC_GROW(active_modules.entries,
active_modules.nr + 1,
active_modules.alloc);
active_modules.entries[active_modules.nr++] = ce;
}
free(list->entries);
*list = active_modules;
}
static int module_list(int argc, const char **argv, const char *prefix)
{
int i;
@ -333,6 +356,18 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath);
/*
* NEEDSWORK: In a multi-working-tree world, this needs to be
* set in the per-worktree config.
*
* Set active flag for the submodule being initialized
*/
if (!is_submodule_initialized(path)) {
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.active", sub->name);
git_config_set_gently(sb.buf, "true");
}
/*
* Copy url setting when it is not set yet.
* To look up the url in .git/config, we must not fall back to
@ -420,6 +455,13 @@ static int module_init(int argc, const char **argv, const char *prefix)
if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
return 1;
/*
* If there are no path args and submodule.active is set then,
* by default, only initialize 'active' modules.
*/
if (!argc && git_config_get_value_multi("submodule.active"))
module_list_active(&list);
for (i = 0; i < list.nr; i++)
init_submodule(list.entries[i]->name, prefix, quiet);
@ -741,7 +783,6 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
struct strbuf displaypath_sb = STRBUF_INIT;
struct strbuf sb = STRBUF_INIT;
const char *displaypath = NULL;
char *url = NULL;
int needs_cloning = 0;
if (ce_stage(ce)) {
@ -775,15 +816,8 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
goto cleanup;
}
/*
* Looking up the url in .git/config.
* We must not fall back to .gitmodules as we only want
* to process configured submodules.
*/
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
git_config_get_string(sb.buf, &url);
if (!url) {
/* Check if the submodule has been initialized. */
if (!is_submodule_initialized(ce->name)) {
next_submodule_warn_missing(suc, out, displaypath);
goto cleanup;
}
@ -817,7 +851,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
argv_array_push(&child->args, "--depth=1");
argv_array_pushl(&child->args, "--path", sub->path, NULL);
argv_array_pushl(&child->args, "--name", sub->name, NULL);
argv_array_pushl(&child->args, "--url", url, NULL);
argv_array_pushl(&child->args, "--url", sub->url, NULL);
if (suc->references.nr) {
struct string_list_item *item;
for_each_string_list_item(item, &suc->references)
@ -827,7 +861,6 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
argv_array_push(&child->args, suc->depth);
cleanup:
free(url);
strbuf_reset(&displaypath_sb);
strbuf_reset(&sb);
@ -1109,6 +1142,16 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
return 0;
}
static int is_active(int argc, const char **argv, const char *prefix)
{
if (argc != 2)
die("submodule--helper is-active takes exactly 1 arguments");
gitmodules_config();
return !is_submodule_initialized(argv[1]);
}
#define SUPPORT_SUPER_PREFIX (1<<0)
struct cmd_struct {
@ -1128,6 +1171,7 @@ static struct cmd_struct commands[] = {
{"init", module_init, SUPPORT_SUPER_PREFIX},
{"remote-branch", resolve_remote_submodule_branch, 0},
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
{"is-active", is_active, 0},
};
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)

View File

@ -278,6 +278,20 @@ or you are unsure what this means choose another name with the '--name' option."
fi &&
git add --force .gitmodules ||
die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
# NEEDSWORK: In a multi-working-tree world, this needs to be
# set in the per-worktree config.
if git config --get submodule.active >/dev/null
then
# If the submodule being adding isn't already covered by the
# current configured pathspec, set the submodule's active flag
if ! git submodule--helper is-active "$sm_path"
then
git config submodule."$sm_name".active "true"
fi
else
git config submodule."$sm_name".active "true"
fi
}
#
@ -1010,14 +1024,13 @@ cmd_status()
do
die_if_unmatched "$mode" "$sha1"
name=$(git submodule--helper name "$sm_path") || exit
url=$(git config submodule."$name".url)
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
if test "$stage" = U
then
say "U$sha1 $displaypath"
continue
fi
if test -z "$url" ||
if ! git submodule--helper is-active "$sm_path" ||
{
! test -d "$sm_path"/.git &&
! test -f "$sm_path"/.git
@ -1090,6 +1103,13 @@ cmd_sync()
while read mode sha1 stage sm_path
do
die_if_unmatched "$mode" "$sha1"
# skip inactive submodules
if ! git submodule--helper is-active "$sm_path"
then
continue
fi
name=$(git submodule--helper name "$sm_path")
url=$(git config -f .gitmodules --get submodule."$name".url)
@ -1112,27 +1132,24 @@ cmd_sync()
;;
esac
if git config "submodule.$name.url" >/dev/null 2>/dev/null
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
git config submodule."$name".url "$super_config_url"
if test -e "$sm_path"/.git
then
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
git config submodule."$name".url "$super_config_url"
(
sanitize_submodule_env
cd "$sm_path"
remote=$(get_default_remote)
git config remote."$remote".url "$sub_origin_url"
if test -e "$sm_path"/.git
if test -n "$recursive"
then
(
sanitize_submodule_env
cd "$sm_path"
remote=$(get_default_remote)
git config remote."$remote".url "$sub_origin_url"
if test -n "$recursive"
then
prefix="$prefix$sm_path/"
eval cmd_sync
fi
)
prefix="$prefix$sm_path/"
eval cmd_sync
fi
)
fi
done
}

View File

@ -213,25 +213,59 @@ void gitmodules_config_sha1(const unsigned char *commit_sha1)
}
/*
* NEEDSWORK: With the addition of different configuration options to determine
* if a submodule is of interests, the validity of this function's name comes
* into question. Once the dust has settled and more concrete terminology is
* decided upon, come up with a more proper name for this function. One
* potential candidate could be 'is_submodule_active()'.
*
* Determine if a submodule has been initialized at a given 'path'
*/
int is_submodule_initialized(const char *path)
{
int ret = 0;
const struct submodule *module = NULL;
char *key = NULL;
char *value = NULL;
const struct string_list *sl;
const struct submodule *module = submodule_from_path(null_sha1, path);
module = submodule_from_path(null_sha1, path);
/* early return if there isn't a path->module mapping */
if (!module)
return 0;
if (module) {
char *key = xstrfmt("submodule.%s.url", module->name);
char *value = NULL;
ret = !git_config_get_string(key, &value);
free(value);
/* submodule.<name>.active is set */
key = xstrfmt("submodule.%s.active", module->name);
if (!git_config_get_bool(key, &ret)) {
free(key);
return ret;
}
free(key);
/* submodule.active is set */
sl = git_config_get_value_multi("submodule.active");
if (sl) {
struct pathspec ps;
struct argv_array args = ARGV_ARRAY_INIT;
const struct string_list_item *item;
for_each_string_list_item(item, sl) {
argv_array_push(&args, item->string);
}
parse_pathspec(&ps, 0, 0, NULL, args.argv);
ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
argv_array_clear(&args);
clear_pathspec(&ps);
return ret;
}
/* fallback to checking if the URL is set */
key = xstrfmt("submodule.%s.url", module->name);
ret = !git_config_get_string(key, &value);
free(value);
free(key);
return ret;
}

View File

@ -1130,5 +1130,141 @@ test_expect_success 'submodule helper list is not confused by common prefixes' '
test_cmp expect actual
'
test_expect_success 'setup superproject with submodules' '
git init sub1 &&
test_commit -C sub1 test &&
test_commit -C sub1 test2 &&
git init multisuper &&
git -C multisuper submodule add ../sub1 sub0 &&
git -C multisuper submodule add ../sub1 sub1 &&
git -C multisuper submodule add ../sub1 sub2 &&
git -C multisuper submodule add ../sub1 sub3 &&
git -C multisuper commit -m "add some submodules"
'
cat >expect <<-EOF
-sub0
sub1 (test2)
sub2 (test2)
sub3 (test2)
EOF
test_expect_success 'submodule update --init with a specification' '
test_when_finished "rm -rf multisuper_clone" &&
pwd=$(pwd) &&
git clone file://"$pwd"/multisuper multisuper_clone &&
git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
test_cmp expect actual
'
test_expect_success 'submodule update --init with submodule.active set' '
test_when_finished "rm -rf multisuper_clone" &&
pwd=$(pwd) &&
git clone file://"$pwd"/multisuper multisuper_clone &&
git -C multisuper_clone config submodule.active "." &&
git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
git -C multisuper_clone submodule update --init &&
git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
test_cmp expect actual
'
test_expect_success 'submodule update and setting submodule.<name>.active' '
test_when_finished "rm -rf multisuper_clone" &&
pwd=$(pwd) &&
git clone file://"$pwd"/multisuper multisuper_clone &&
git -C multisuper_clone config --bool submodule.sub0.active "true" &&
git -C multisuper_clone config --bool submodule.sub1.active "false" &&
git -C multisuper_clone config --bool submodule.sub2.active "true" &&
cat >expect <<-\EOF &&
sub0 (test2)
-sub1
sub2 (test2)
-sub3
EOF
git -C multisuper_clone submodule update &&
git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
test_cmp expect actual
'
test_expect_success 'clone --recurse-submodules with a pathspec works' '
test_when_finished "rm -rf multisuper_clone" &&
cat >expected <<-\EOF &&
sub0 (test2)
-sub1
-sub2
-sub3
EOF
git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
git -C multisuper_clone submodule status |cut -c1,43- >actual &&
test_cmp actual expected
'
test_expect_success 'clone with multiple --recurse-submodules options' '
test_when_finished "rm -rf multisuper_clone" &&
cat >expect <<-\EOF &&
-sub0
sub1 (test2)
-sub2
sub3 (test2)
EOF
git clone --recurse-submodules="." \
--recurse-submodules=":(exclude)sub0" \
--recurse-submodules=":(exclude)sub2" \
multisuper multisuper_clone &&
git -C multisuper_clone submodule status |cut -c1,43- >actual &&
test_cmp expect actual
'
test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
test_when_finished "rm -rf multisuper_clone" &&
cat <<-\EOF >expect &&
-sub0
sub1 (test2)
-sub2
sub3 (test2)
EOF
cat <<-\EOF >expect2 &&
-sub0
sub1 (test2)
-sub2
sub3 (test2)
-sub4
sub5 (test2)
EOF
git clone --recurse-submodules="." \
--recurse-submodules=":(exclude)sub0" \
--recurse-submodules=":(exclude)sub2" \
--recurse-submodules=":(exclude)sub4" \
multisuper multisuper_clone &&
git -C multisuper_clone submodule status |cut -c1,43- >actual &&
test_cmp expect actual &&
git -C multisuper submodule add ../sub1 sub4 &&
git -C multisuper submodule add ../sub1 sub5 &&
git -C multisuper commit -m "add more submodules" &&
# obtain the new superproject
git -C multisuper_clone pull &&
git -C multisuper_clone submodule update --init &&
git -C multisuper_clone submodule status |cut -c1,43- >actual &&
test_cmp expect2 actual
'
test_expect_success 'init properly sets the config' '
test_when_finished "rm -rf multisuper_clone" &&
git clone --recurse-submodules="." \
--recurse-submodules=":(exclude)sub0" \
multisuper multisuper_clone &&
git -C multisuper_clone submodule init -- sub0 sub1 &&
git -C multisuper_clone config --get submodule.sub0.active &&
test_must_fail git -C multisuper_clone config --get submodule.sub1.active
'
test_done

107
t/t7413-submodule-is-active.sh Executable file
View File

@ -0,0 +1,107 @@
#!/bin/sh
test_description='Test submodule--helper is-active
This test verifies that `git submodue--helper is-active` correclty identifies
submodules which are "active" and interesting to the user.
'
. ./test-lib.sh
test_expect_success 'setup' '
git init sub &&
test_commit -C sub initial &&
git init super &&
test_commit -C super initial &&
git -C super submodule add ../sub sub1 &&
git -C super submodule add ../sub sub2 &&
# Remove submodule.<name>.active entries in order to test in an
# environment where only URLs are present in the conifg
git -C super config --unset submodule.sub1.active &&
git -C super config --unset submodule.sub2.active &&
git -C super commit -a -m "add 2 submodules at sub{1,2}"
'
test_expect_success 'is-active works with urls' '
git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2 &&
git -C super config --unset submodule.sub1.URL &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super config submodule.sub1.URL ../sub &&
git -C super submodule--helper is-active sub1
'
test_expect_success 'is-active works with submodule.<name>.active config' '
test_when_finished "git -C super config --unset submodule.sub1.active" &&
test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
git -C super config --bool submodule.sub1.active "false" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super config --bool submodule.sub1.active "true" &&
git -C super config --unset submodule.sub1.URL &&
git -C super submodule--helper is-active sub1
'
test_expect_success 'is-active works with basic submodule.active config' '
test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
test_when_finished "git -C super config --unset-all submodule.active" &&
git -C super config --add submodule.active "." &&
git -C super config --unset submodule.sub1.URL &&
git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
'
test_expect_success 'is-active correctly works with paths that are not submodules' '
test_when_finished "git -C super config --unset-all submodule.active" &&
test_must_fail git -C super submodule--helper is-active not-a-submodule &&
git -C super config --add submodule.active "." &&
test_must_fail git -C super submodule--helper is-active not-a-submodule
'
test_expect_success 'is-active works with exclusions in submodule.active config' '
test_when_finished "git -C super config --unset-all submodule.active" &&
git -C super config --add submodule.active "." &&
git -C super config --add submodule.active ":(exclude)sub1" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
'
test_expect_success 'is-active with submodule.active and submodule.<name>.active' '
test_when_finished "git -C super config --unset-all submodule.active" &&
test_when_finished "git -C super config --unset submodule.sub1.active" &&
test_when_finished "git -C super config --unset submodule.sub2.active" &&
git -C super config --add submodule.active "sub1" &&
git -C super config --bool submodule.sub1.active "false" &&
git -C super config --bool submodule.sub2.active "true" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
'
test_expect_success 'is-active, submodule.active and submodule add' '
test_when_finished "rm -rf super2" &&
git init super2 &&
test_commit -C super2 initial &&
git -C super2 config --add submodule.active "sub*" &&
# submodule add should only add submodule.<name>.active
# to the config if not matched by the pathspec
git -C super2 submodule add ../sub sub1 &&
test_must_fail git -C super2 config --get submodule.sub1.active &&
git -C super2 submodule add ../sub mod &&
git -C super2 config --get submodule.mod.active
'
test_done