Add option to recurse into submodules
v2 cover Add option to recurse into submodules
v2:
* this applies on sb/reset-recurse-submodules
* grep and push are also respecting submodule.recurse now.
* Brandon seemed to disagree with the first patch as it may hurt with his 'RFC repo object' series [1], but I explained a possible way out in [2] and Brandon seemed to agree that this makes sense offlist.
That first patch was changed to include loading the gitmodules file in the call to loading the config.
[1] https://public-inbox.org/git/[email protected]/
[2] https://public-inbox.org/git/CAGZ79kb+31h6f_9fuUpbfeY7xxjP-kbvqz6J5K5sbPEmdPNvpw@mail.gmail.com/
v1:
In a submodule heavy workflow it becomes tedious to pass in --recurse-submodules all the time, so make an option for it.
Thanks,
Stefan
Stefan Beller (6):
submodule.c: add has_submodules to check if we have any submodules submodule test invocation: only pass additional arguments Introduce submodule.recurse option for worktree manipulators builtin/fetch.c: respect 'submodule.recurse' option builtin/grep.c: respect 'submodule.recurse' option builtin/push.c: respect 'submodule.recurse' option
Documentation/config.txt | 5 +++ builtin/checkout.c | 9 ++-- builtin/fetch.c | 3 +- builtin/grep.c | 3 ++ builtin/push.c | 4 ++ builtin/read-tree.c | 3 +- builtin/reset.c | 3 +- builtin/submodule--helper.c | 10 ++--- submodule.c | 85 +++++++++++++++++++++++++++++++------- submodule.h | 8 +++- t/lib-submodule-update.sh | 18 +++++++- t/t1013-read-tree-submodule.sh | 4 +- t/t2013-checkout-submodule.sh | 4 +- t/t5526-fetch-submodules.sh | 10 +++++ t/t5531-deep-submodule-push.sh | 21 ++++++++++ t/t7112-reset-submodule.sh | 4 +- t/t7814-grep-recurse-submodules.sh | 18 ++++++++ unpack-trees.c | 3 +- 18 files changed, 169 insertions(+), 46 deletions(-)
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 3/6 Introduce submodule.recurse option for worktree manipulators
Any command that understands '--recurse-submodules' can have its default changed to true, by setting the submodule.recurse option.
This patch includes read-tree/checkout/reset for working tree manipulating commands. Later patches will cover other commands.
--- Documentation/config.txt | 5 +++++ builtin/checkout.c | 9 +++------ submodule.c | 6 +++++- t/lib-submodule-update.sh | 12 ++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-)
Documentation/config.txt +5 -0
@@ -3063,6 +3063,11 @@ submodule.active:: submodule's path to determine if the submodule is of interest to git commands. +submodule.recurse:: + Specifies if commands recurse into submodules by default. This + applies to all commands that have a `--recurse-submodules` option. + Defaults to false. + submodule.fetchJobs:: Specifies how many submodules are fetched/cloned at the same time. A positive integer allows up to that number of submodules fetched
builtin/checkout.c +3 -6
@@ -1194,7 +1194,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) opts.prefix = prefix; opts.show_progress =-1 ; -gitmodules_config (); +load_submodule_config ();git_config (git_checkout_config, &opts); opts.track =BRANCH_TRACK_UNSPECIFIED ;
@@ -1214,11 +1214,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)git_xmerge_config ("merge.conflictstyle" , conflict_style,NULL ); } -if (recurse_submodules !=RECURSE_SUBMODULES_OFF ) { -load_submodule_config (); -if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) -set_config_update_recurse_submodules (recurse_submodules); - } +if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) +set_config_update_recurse_submodules (recurse_submodules);if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) >1 )die (_ ("-b, -B and --orphan are mutually exclusive" ));
submodule.c +5 -1
@@ -91,7 +91,11 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)static int submodule_config (const char *var,const char *value,void *cb) { -if (!strcmp (var,"submodule.fetchjobs" )) { +if (!strcmp (var,"submodule.recurse" )) { +int v =git_config_bool (var, value) ? +RECURSE_SUBMODULES_ON :RECURSE_SUBMODULES_OFF ; + config_update_recurse_submodules = v; + }else if (!strcmp (var,"submodule.fetchjobs" )) { submodule_config_reading =SUBMODULE_CONFIG_EXISTS ; parallel_jobs =git_config_int (var, value);if (parallel_jobs <0 )
t/lib-submodule-update.sh +12 -0
@@ -990,6 +990,18 @@ test_submodule_switch_recursing () { ) ' + test_expect_success "git -c submodule.recurse=true $cmd_args --recurse-submodules: modified submodule updates submodule work tree" ' + prolog && + reset_work_tree_to_interested add_sub1 && + ( + cd submodule_update && + git branch -t modify_sub1 origin/modify_sub1 && + git -c submodule.recurse=true $cmd_args --recurse-submodules modify_sub1 && + test_superproject_content origin/modify_sub1 && + test_submodule_content sub1 origin/modify_sub1 + ) + ' + # Updating a submodule to an invalid sha1 doesn't update the # superproject nor the submodule's work tree. test_expect_success "$command: updating to a missing submodule commit fails" '
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 2/6 submodule test invocation: only pass additional arguments
In reply to Stefan Beller--- t/lib-submodule-update.sh | 6 ++++-- t/t1013-read-tree-submodule.sh | 4 ++-- t/t2013-checkout-submodule.sh | 4 ++-- t/t7112-reset-submodule.sh | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-)
t/lib-submodule-update.sh +4 -2
@@ -782,7 +782,8 @@ test_submodule_forced_switch () { # git directory first into the superproject. test_submodule_switch_recursing () { - command="$1" + cmd_args="$1" + command="git $cmd_args --recurse-submodules" RESULTDS=success if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1 then
@@ -1022,7 +1023,8 @@ test_submodule_switch_recursing () { # that change a submodule, but throwing away local changes in # the superproject as well as the submodule is allowed. test_submodule_forced_switch_recursing () { - command="$1" + cmd_args="$1" + command="git $cmd_args --recurse-submodules" RESULT=success if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1 then
t/t1013-read-tree-submodule.sh +2 -2
@@ -9,9 +9,9 @@ KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1 -test_submodule_switch_recursing "git read-tree --recurse-submodules -u -m" +test_submodule_switch_recursing "read-tree -u -m" -test_submodule_forced_switch_recursing "git read-tree --recurse-submodules -u --reset" +test_submodule_forced_switch_recursing "read-tree -u --reset" test_submodule_switch "git read-tree -u -m"
t/t2013-checkout-submodule.sh +2 -2
@@ -65,9 +65,9 @@ test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/ KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 -test_submodule_switch_recursing "git checkout --recurse-submodules" +test_submodule_switch_recursing "checkout" -test_submodule_forced_switch_recursing "git checkout -f --recurse-submodules" +test_submodule_forced_switch_recursing "checkout -f" test_submodule_switch "git checkout"
t/t7112-reset-submodule.sh +2 -2
@@ -9,9 +9,9 @@ KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1 -test_submodule_switch_recursing "git reset --recurse-submodules --keep" +test_submodule_switch_recursing "reset --keep" -test_submodule_forced_switch_recursing "git reset --hard --recurse-submodules" +test_submodule_forced_switch_recursing "reset --hard" test_submodule_switch "git reset --keep"
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 5/6 builtin/grep.c: respect 'submodule.recurse' option
In reply to Stefan BellerIn builtin/grep.c we parse the config before evaluating the command line options. This makes the task of teaching grep to respect the new config option 'submodule.recurse' very easy by just parsing that option.
As an alternative I had implemented a similar structure to treat submodules as the fetch/push command have, including
* aligning the meaning of the 'recurse_submodules' to possible submodule values RECURSE_SUBMODULES_* as defined in submodule.h.
* having a callback to parse the value and
* reacting to the RECURSE_SUBMODULES_DEFAULT state that was the initial state.
However all this is not needed for a true boolean value, so let's keep it simple. However this adds another place where "submodule.recurse" is parsed.
--- builtin/grep.c | 3 +++ t/t7814-grep-recurse-submodules.sh | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+)
builtin/grep.c +3 -0
@@ -291,6 +291,9 @@ static int grep_cmd_config(const char *var, const char *value, void *cb) num_threads, var); } +if (!strcmp (var,"submodule.recurse" )) + recurse_submodules =git_config_bool (var, value); +return st; }
t/t7814-grep-recurse-submodules.sh +18 -0
@@ -33,6 +33,24 @@ test_expect_success 'grep correctly finds patterns in a submodule' ' test_cmp expect actual ' +test_expect_success 'grep finds patterns in a submodule via config' ' + test_config submodule.recurse true && + # expect from previous test + git grep -e "bar" >actual && + test_cmp expect actual +' + +test_expect_success 'grep --no-recurse-submodules overrides config' ' + test_config submodule.recurse true && + cat >expect <<-\EOF && + a:foobar + b/b:bar + EOF + + git grep -e "bar" --no-recurse-submodules >actual && + test_cmp expect actual +' + test_expect_success 'grep and basic pathspecs' ' cat >expect <<-\EOF && submodule/a:foobar
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 4/6 builtin/fetch.c: respect 'submodule.recurse' option
In reply to Stefan Beller--- submodule.c | 1 + t/t5526-fetch-submodules.sh | 10 ++++++++++ 2 files changed, 11 insertions(+)
submodule.c +1 -0
@@ -94,6 +94,7 @@ static int submodule_config(const char *var, const char *value, void *cb)if (!strcmp (var,"submodule.recurse" )) {int v =git_config_bool (var, value) ?RECURSE_SUBMODULES_ON :RECURSE_SUBMODULES_OFF ; + config_fetch_recurse_submodules = v; config_update_recurse_submodules = v; }else if (!strcmp (var,"submodule.fetchjobs" )) { submodule_config_reading =SUBMODULE_CONFIG_EXISTS ;
t/t5526-fetch-submodules.sh +10 -0
@@ -71,6 +71,16 @@ test_expect_success "fetch --recurse-submodules recurses into submodules" ' test_i18ncmp expect.err actual.err ' +test_expect_success "submodule.recurse option triggers recursive fetch" ' + add_upstream_commit && + ( + cd downstream && + git -c submodule.recurse fetch >../actual.out 2>../actual.err + ) && + test_must_be_empty actual.out && + test_i18ncmp expect.err actual.err +' + test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" ' add_upstream_commit && (
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 6/6 builtin/push.c: respect 'submodule.recurse' option
In reply to Stefan BellerThe closest mapping from the boolean 'submodule.recurse' set to "yes" to the variety of submodule push modes is "on-demand", so implement that.
--- builtin/push.c | 4 ++++ t/t5531-deep-submodule-push.sh | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+)
builtin/push.c +4 -0
@@ -498,6 +498,10 @@ static int git_push_config(const char *k, const char *v, void *cb)const char *value;if (!git_config_get_value ("push.recursesubmodules" , &value)) recurse_submodules =parse_push_recurse_submodules_arg (k, value); + }else if (!strcmp (k,"submodule.recurse" )) { +int val =git_config_bool (k, v) ? +RECURSE_SUBMODULES_ON_DEMAND :RECURSE_SUBMODULES_OFF ; + recurse_submodules = val; }return git_default_config (k, v,NULL );
t/t5531-deep-submodule-push.sh +21 -0
@@ -126,6 +126,27 @@ test_expect_success 'push succeeds if submodule commit not on remote but using o ) ' +test_expect_success 'push succeeds if submodule commit not on remote but using auto-on-demand via submodule.recurse config' ' + ( + cd work/gar/bage && + >recurse-on-demand-from-submodule-recurse-config && + git add recurse-on-demand-from-submodule-recurse-config && + git commit -m "Recurse submodule.recurse from config junk" + ) && + ( + cd work && + git add gar/bage && + git commit -m "Recurse submodule.recurse from config for gar/bage" && + git -c submodule.recurse push ../pub.git master && + # Check that the supermodule commit got there + git fetch ../pub.git && + git diff --quiet FETCH_HEAD master && + # Check that the submodule commit got there too + cd gar/bage && + git diff --quiet origin/master master + ) +' + test_expect_success 'push recurse-submodules on command line overrides config' ' ( cd work/gar/bage &&
Signature · 1 line
2.13.0.18.g7d86cc8ba0
PATCH v2 1/6 submodule.c: add has_submodules to check if we have any submodules
In reply to Stefan BellerWhen submodules are involved, it often slows down the process, as most submodule related handling is either done via a child process or by iterating over the index finding all gitlinks.
For most commands that may interact with submodules, we need have a quick check if we do have any submodules at all, such that we can be fast in the case when no submodules are in use. For this quick check introduce a function that checks with different heuristics if we do have submodules around, checking if
* anything related to submodules is configured,
* absorbed git dirs for submodules are present,
* the '.gitmodules' file exists
* gitlinks are recorded in the index.
Each heuristic has advantages and disadvantages.
For example in a later patch, when we first use this function in git-clone, we'll just check for the existence of the '.gitmodules' file, because at the time of running the clone command there will be no absorbed git dirs or submodule configuration around.
Checking for any configuration related to submodules would be useful in a later stage (after cloning) to see if the submodules are actually in use.
Checking for absorbed git directories is good to see if the user has actually cloned submodules already (i.e. not just initialized them by configuring them).
The heuristic for checking the configuration requires this patch to have have a global state, whether the submodule config has already been read, and if there were any submodule related keys. Make 'submodule_config' private to the submodule code, and introduce 'load_submodule_config' that will take care of this global state.
--- builtin/checkout.c | 2 +- builtin/fetch.c | 3 +- builtin/read-tree.c | 3 +- builtin/reset.c | 3 +- builtin/submodule--helper.c | 10 ++---- submodule.c | 80 ++++++++++++++++++++++++++++++++++++--------- submodule.h | 8 ++++- unpack-trees.c | 3 +- 8 files changed, 79 insertions(+), 33 deletions(-)
builtin/checkout.c +1 -1
@@ -1215,7 +1215,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) }if (recurse_submodules !=RECURSE_SUBMODULES_OFF ) { -git_config (submodule_config,NULL ); +load_submodule_config ();if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT )set_config_update_recurse_submodules (recurse_submodules); }
builtin/fetch.c +1 -2
@@ -1343,8 +1343,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)int arg =parse_fetch_recurse_submodules_arg ("--recurse-submodules-default" , recurse_submodules_default);set_config_fetch_recurse_submodules (arg); } -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); }if (all) {
builtin/read-tree.c +1 -2
@@ -176,8 +176,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)hold_locked_index (&lock_file,LOCK_DIE_ON_ERROR );if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) { -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();set_config_update_recurse_submodules (RECURSE_SUBMODULES_ON ); }
builtin/reset.c +1 -2
@@ -320,8 +320,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)parse_args (&pathspec, argv, prefix, patch_mode, &rev);if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) { -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();set_config_update_recurse_submodules (RECURSE_SUBMODULES_ON ); }
builtin/submodule--helper.c +3 -7
@@ -1013,9 +1013,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)if (pathspec.nr) suc.warn_if_uninitialized =1 ; -/* Overlay the parsed .gitmodules file with .git/config */ -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();if (max_jobs <0 ) max_jobs =parallel_submodules ();
@@ -1057,9 +1055,8 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefixstatic const char *remote_submodule_branch (const char *path) {const struct submodule *sub; -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); sub =submodule_from_path (null_sha1, path);if (!sub)return NULL ;
@@ -1129,8 +1126,7 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, embed_gitdir_options, git_submodule_helper_usage,0 ); -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();if (module_list_compute (argc, argv, prefix, &pathspec, &list) <0 )return 1 ;
submodule.c +64 -16
98 lines not on this page · raw message
submodule.h +7 -1
@@ -1,6 +1,12 @@#ifndef SUBMODULE_H #define SUBMODULE_H +#define SUBMODULE_CHECK_ANY_CONFIG (1<<0) +#define SUBMODULE_CHECK_ABSORBED_GIT_DIRS (1<<1) +#define SUBMODULE_CHECK_GITMODULES_IN_WT (1<<2) +#define SUBMODULE_CHECK_GITLINKS_IN_TREE (1<<3) +int has_submodules (unsigned what_to_check); +struct diff_options ;struct argv_array ;struct sha1_array ;
@@ -37,7 +43,7 @@ extern int remove_path_from_gitmodules(const char *path);extern void stage_updated_gitmodules (void );extern void set_diffopt_flags_from_submodule_config (struct diff_options *,const char *path); -extern int submodule_config (const char *var,const char *value,void *cb); +extern void load_submodule_config (void );extern void gitmodules_config (void );extern void gitmodules_config_sha1 (const unsigned char *commit_sha1);extern int is_submodule_initialized (const char *path);
unpack-trees.c +1 -2
@@ -291,8 +291,7 @@ static void reload_gitmodules_file(struct index_state *index,else if (r ==0 ) {submodule_free ();checkout_entry (ce, state,NULL ); -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); }else break ; }
Signature · 1 line
2.13.0.18.g7d86cc8ba0
Re: [PATCHv2 2/6] submodule test invocation: only pass additional arguments
In reply to Stefan BellerQuoted 5 lines · Stefan Beller
t/t2013-checkout-submodule.sh +0
-0
t/t2013-checkout-submodule.sh test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/ · line 72
Quoted 10 lines
t/t2013-checkout-submodule.sh +2
-2@@ -65,9 +65,9 @@ test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/ KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 -test_submodule_switch_recursing "git checkout --recurse-submodules" +test_submodule_switch_recursing "checkout" -test_submodule_forced_switch_recursing "git checkout -f --recurse-submodules" +test_submodule_forced_switch_recursing "checkout -f" test_submodule_switch "git checkout"
Doesn't the above look crazy to you?
It is hostile to other people (and those who need to make merges) who have to work with test_submodule_switch_recursing that older one used to take the full command but its definition suddenly changes so that the caller now must omit the leading "git". Even worse, another helper with a similar-sounding name, test_submodule_switch, still must be called with the leading "git".
The same comment applies to the one we can see below.
Quoted 15 lines
t/t7112-reset-submodule.sh +2
-2@@ -9,9 +9,9 @@ KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1 -test_submodule_switch_recursing "git reset --recurse-submodules --keep" +test_submodule_switch_recursing "reset --keep" -test_submodule_forced_switch_recursing "git reset --hard --recurse-submodules" +test_submodule_forced_switch_recursing "reset --hard" test_submodule_switch "git reset --keep"
Re: [PATCHv2 2/6] submodule test invocation: only pass additional arguments
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Stefan Beller
t/t2013-checkout-submodule.sh +1
-1@@ -65,9 +65,9 @@ test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/ KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1 -test_submodule_switch_recursing "git checkout --recurse-submodules" +test_submodule_switch_recursing "checkout"
Quoted 6 lines
t/t2013-checkout-submodule.sh test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/ · line 72
Quoted 4 lines
t/t2013-checkout-submodule.sh +1
-1-test_submodule_forced_switch_recursing "git checkout -f --recurse-submodules" +test_submodule_forced_switch_recursing "checkout -f" test_submodule_switch "git checkout"Doesn't the above look crazy to you?
Oh well. The commit message doesn't explain why the craziness is required here (really!).
submodule test invocation: only pass additional arguments
In a later patch we want to introduce a config option to trigger the submodule recursing by default. As this option should be available and uniform across all commands that deal with submodules we'd want to test for this option in the submodule update library.
So instead of calling the whole test set again for "git -c submodule.recurse foo" instead of
"git foo --recurse-submodules", we'd only want to introduce one basic test that tests if the option is recognized and respected to not overload the test suite.
Quoted 5 lines
It is hostile to other people (and those who need to make merges) who have to work with
test_submodule_switch_recursingthat older one used to take the full command but its definition suddenly changes so that the caller now must omit the leading "git".
I am not aware of other people (or other series in flight by myself) that use one of the switches currently.
Quoted 3 lines
Even worse,
another helper with a similar-sounding name,test_submodule_switch, still must be called with the leading "git".
Oh, yeah that is a real issue. I will migrate all of them.
Quoted 2 lines
The same comment applies to the one we can see below.
An alternative would be to come up with a slightly different name to ensure we do not have issues with other series in flight. The function name is already pretty long, so encoding even more information in it may be not a good idea. But the argument is shorter, so maybe:
- test_submodule_switch_recursing "git reset --hard --recurse-submodules"
+ test_submodule_switch_recursing_args_only "reset --hard"
Thanks,
Stefan
Re: [PATCHv2 1/6] submodule.c: add has_submodules to check if we have any submodules
In reply to Stefan BellerQuoted 28 lines · Stefan Beller
When submodules are involved, it often slows down the process, as most submodule related handling is either done via a child process or by iterating over the index finding all gitlinks.
For most commands that may interact with submodules, we need have a quick check if we do have any submodules at all, such that we can be fast in the case when no submodules are in use. For this quick check introduce a function that checks with different heuristics if we do have submodules around, checking if
* anything related to submodules is configured,
* absorbed git dirs for submodules are present,
* the '.gitmodules' file exists
* gitlinks are recorded in the index.Each heuristic has advantages and disadvantages.
For example in a later patch, when we first use this function in git-clone, we'll just check for the existence of the '.gitmodules' file, because at the time of running the clone command there will be no absorbed git dirs or submodule configuration around.Checking for any configuration related to submodules would be useful in a later stage (after cloning) to see if the submodules are actually in use.
Checking for absorbed git directories is good to see if the user has actually cloned submodules already (i.e. not just initialized them by configuring them).
Quoted 5 lines
The heuristic for checking the configuration requires this patch to have have a global state, whether the submodule config has already been read, and if there were any submodule related keys. Make '
submodule_config' private to the submodule code, and introduce 'load_submodule_config' that will take care of this global state.
It doesn't look like any patches actually use this helper, is this intended?
Quoted 252 lines
Signed-off-by Stefan Beller <[email protected]>--- builtin/checkout.c | 2 +- builtin/fetch.c | 3 +- builtin/read-tree.c | 3 +- builtin/reset.c | 3 +- builtin/submodule--helper.c | 10 ++---- submodule.c | 80 ++++++++++++++++++++++++++++++++++++--------- submodule.h | 8 ++++- unpack-trees.c | 3 +- 8 files changed, 79 insertions(+), 33 deletions(-)builtin/checkout.c +1
-1@@ -1215,7 +1215,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) }if (recurse_submodules !=RECURSE_SUBMODULES_OFF ) { -git_config (submodule_config,NULL ); +load_submodule_config ();if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT )set_config_update_recurse_submodules (recurse_submodules); }builtin/fetch.c +1
-2@@ -1343,8 +1343,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)int arg =parse_fetch_recurse_submodules_arg ("--recurse-submodules-default" , recurse_submodules_default);set_config_fetch_recurse_submodules (arg); } -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); }if (all) {builtin/read-tree.c +1
-2@@ -176,8 +176,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)hold_locked_index (&lock_file,LOCK_DIE_ON_ERROR );if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) { -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();set_config_update_recurse_submodules (RECURSE_SUBMODULES_ON ); }builtin/reset.c +1
-2@@ -320,8 +320,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)parse_args (&pathspec, argv, prefix, patch_mode, &rev);if (recurse_submodules !=RECURSE_SUBMODULES_DEFAULT ) { -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();set_config_update_recurse_submodules (RECURSE_SUBMODULES_ON ); }builtin/submodule--helper.c +3
-7@@ -1013,9 +1013,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)if (pathspec.nr) suc.warn_if_uninitialized =1 ; -/* Overlay the parsed .gitmodules file with .git/config */ -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();if (max_jobs <0 ) max_jobs =parallel_submodules ();@@ -1057,9 +1055,8 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefixstatic const char *remote_submodule_branch (const char *path) {const struct submodule *sub; -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); sub =submodule_from_path (null_sha1, path);if (!sub)return NULL ;@@ -1129,8 +1126,7 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, embed_gitdir_options, git_submodule_helper_usage,0 ); -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config ();if (module_list_compute (argc, argv, prefix, &pathspec, &list) <0 )return 1 ;submodule.c +64
-16@@ -24,6 +24,12 @@ static int initialized_fetch_ref_tips;static struct sha1_array ref_tips_before_fetch;static struct sha1_array ref_tips_after_fetch; +static enum { +SUBMODULE_CONFIG_NOT_READ =0 , +SUBMODULE_CONFIG_NO_CONFIG , +SUBMODULE_CONFIG_EXISTS , +} submodule_config_reading; +/* * The following flag is set if the .gitmodules file is unmerged. We then * disable recursion for all submodules where .git/config doesn't have a @@ -83,6 +89,64 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)return 0 ; } +static int submodule_config (const char *var,const char *value,void *cb) +{ +if (!strcmp (var,"submodule.fetchjobs" )) { + submodule_config_reading =SUBMODULE_CONFIG_EXISTS ; + parallel_jobs =git_config_int (var, value); +if (parallel_jobs <0 ) +die (_ ("negative values not allowed for submodule.fetchJobs" )); +return 0 ; + }else if (starts_with (var,"submodule." )) { + submodule_config_reading =SUBMODULE_CONFIG_EXISTS ; +return parse_submodule_config_option (var, value); + }else if (!strcmp (var,"fetch.recursesubmodules" )) { + submodule_config_reading =SUBMODULE_CONFIG_EXISTS ; + config_fetch_recurse_submodules =parse_fetch_recurse_submodules_arg (var, value); +return 0 ; + } +return 0 ; +} + +void load_submodule_config (void ) +{ + submodule_config_reading =SUBMODULE_CONFIG_NO_CONFIG ; + +gitmodules_config (); +git_config (submodule_config,NULL ); +} + +int has_submodules (unsigned what_to_check) +{ +if (what_to_check &SUBMODULE_CHECK_ANY_CONFIG ) { +if (submodule_config_reading ==SUBMODULE_CONFIG_NOT_READ ) +load_submodule_config (); +if (submodule_config_reading ==SUBMODULE_CONFIG_EXISTS ) +return 1 ; + } + +if ((what_to_check &SUBMODULE_CHECK_ABSORBED_GIT_DIRS ) && +file_exists (git_path ("modules" ))) +return 1 ; + +if ((what_to_check &SUBMODULE_CHECK_GITMODULES_IN_WT ) && + (!is_bare_repository () &&file_exists (".gitmodules" ))) +return 1 ; + +if (what_to_check &SUBMODULE_CHECK_GITLINKS_IN_TREE ) { +int i; + +if (read_cache () <0 ) +die (_ ("index file corrupt" )); + +for (i =0 ; i < active_nr; i++) +if (S_ISGITLINK (active_cache[i]->ce_mode)) +return 1 ; + } + +return 0 ; +} +/* * Try to remove the "submodule.<name>" section from .gitmodules where the given * path is configured. Return 0 only if a .gitmodules file was found, a section @@ -152,22 +216,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, } } -int submodule_config (const char *var,const char *value,void *cb) -{ -if (!strcmp (var,"submodule.fetchjobs" )) { - parallel_jobs =git_config_int (var, value); -if (parallel_jobs <0 ) -die (_ ("negative values not allowed for submodule.fetchJobs" )); -return 0 ; - }else if (starts_with (var,"submodule." )) -return parse_submodule_config_option (var, value); -else if (!strcmp (var,"fetch.recursesubmodules" )) { - config_fetch_recurse_submodules =parse_fetch_recurse_submodules_arg (var, value); -return 0 ; - } -return 0 ; -} -void gitmodules_config (void ) {const char *work_tree =get_git_work_tree ();submodule.h +7
-1@@ -1,6 +1,12 @@#ifndef SUBMODULE_H #define SUBMODULE_H +#define SUBMODULE_CHECK_ANY_CONFIG (1<<0) +#define SUBMODULE_CHECK_ABSORBED_GIT_DIRS (1<<1) +#define SUBMODULE_CHECK_GITMODULES_IN_WT (1<<2) +#define SUBMODULE_CHECK_GITLINKS_IN_TREE (1<<3) +int has_submodules (unsigned what_to_check); +struct diff_options ;struct argv_array ;struct sha1_array ;@@ -37,7 +43,7 @@ extern int remove_path_from_gitmodules(const char *path);extern void stage_updated_gitmodules (void );extern void set_diffopt_flags_from_submodule_config (struct diff_options *,const char *path); -extern int submodule_config (const char *var,const char *value,void *cb); +extern void load_submodule_config (void );extern void gitmodules_config (void );extern void gitmodules_config_sha1 (const unsigned char *commit_sha1);extern int is_submodule_initialized (const char *path);unpack-trees.c +1
-2@@ -291,8 +291,7 @@ static void reload_gitmodules_file(struct index_state *index,else if (r ==0 ) {submodule_free ();checkout_entry (ce, state,NULL ); -gitmodules_config (); -git_config (submodule_config,NULL ); +load_submodule_config (); }else break ; }Signature · 1 line
2.13.0.18.g7d86cc8ba0
Signature · 1 line
Brandon Williams
Re: [PATCHv2 1/6] submodule.c: add has_submodules to check if we have any submodules
Quoted 2 lines · Brandon Williams
It doesn't look like any patches actually use this helper, is this intended?
It was needed for
https://public-inbox.org/git/[email protected]/
which we do not have in this series any more. Will drop this patch.