All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clone: use submodules.recurse option for automatically clone submodules
@ 2020-01-31 21:11 Markus Klein via GitGitGadget
  2020-02-01 21:19 ` Johannes Schindelin
  2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
  0 siblings, 2 replies; 11+ messages in thread
From: Markus Klein via GitGitGadget @ 2020-01-31 21:11 UTC (permalink / raw)
  To: git; +Cc: Markus Klein, Markus

From: Markus <masmiseim@gmx.de>

Simplify cloning repositories with submodules when the option
submodules.recurse is set by the user. This makes it transparent to the
user if submodules are used. The user doesn’t have to know if he has to add
an extra parameter to get the full project including the used submodules.
This makes clone behave identical to other commands like fetch, pull,
checkout, ... which include the submodules automatically if this option is
set.

It is implemented analog to the pull command by using an own config
function instead of using just the default config. In contrast to the pull
command, the submodule.recurse state is saved as an array of strings as it
can take an optionally pathspec argument which describes which submodules
should be recursively initialized and cloned. To recursively initialize and
clone all submodules a pathspec of "." has to be used.
The regression test is simplified compared to the test for "git clone
--recursive" as the general functionality is already checked there.

Signed-off-by: Markus Klein <masmiseim@gmx.de>
---
    Add the usage of the submodules.recurse parameter on clone
    
    I try to finish the pullrequest #573 from Maddimax. This adds the usage
    of the submodules.recurse parameter on clone

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v1
Pull-Request: https://github.com/git/git/pull/695

 builtin/clone.c              | 16 +++++++++++++++-
 t/t7407-submodule-foreach.sh | 11 +++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 0fc89ae2b9..21b9d927a2 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -26,6 +26,8 @@
 #include "dir-iterator.h"
 #include "iterator.h"
 #include "sigchain.h"
+#include "submodule-config.h"
+#include "submodule.h"
 #include "branch.h"
 #include "remote.h"
 #include "run-command.h"
@@ -929,6 +931,18 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+/**
+ * Read config variables.
+ */
+static int git_clone_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
+		string_list_append(&option_recurse_submodules, "true");
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1103,7 +1117,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	write_config(&option_config);
 
-	git_config(git_default_config, NULL);
+	git_config(git_clone_config, NULL);
 
 	if (option_bare) {
 		if (option_mirror)
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 6b2aa917e1..44b32f7b27 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -383,6 +383,17 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
 		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 	)
 '
+test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
+	git clone -c submodule.recurse=true super clone7 &&
+	(
+		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
 
 test_expect_success 'command passed to foreach retains notion of stdin' '
 	(

base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] clone: use submodules.recurse option for automatically clone submodules
  2020-01-31 21:11 [PATCH] clone: use submodules.recurse option for automatically clone submodules Markus Klein via GitGitGadget
@ 2020-02-01 21:19 ` Johannes Schindelin
  2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
  1 sibling, 0 replies; 11+ messages in thread
From: Johannes Schindelin @ 2020-02-01 21:19 UTC (permalink / raw)
  To: Markus Klein via GitGitGadget; +Cc: git, Markus Klein, Markus

[-- Attachment #1: Type: text/plain, Size: 4304 bytes --]

Hi Markus,


On Fri, 31 Jan 2020, Markus Klein via GitGitGadget wrote:

> From: Markus <masmiseim@gmx.de>

This line should probably match...

>
> Simplify cloning repositories with submodules when the option
> submodules.recurse is set by the user. This makes it transparent to the
> user if submodules are used. The user doesn’t have to know if he has to add
> an extra parameter to get the full project including the used submodules.
> This makes clone behave identical to other commands like fetch, pull,
> checkout, ... which include the submodules automatically if this option is
> set.
>
> It is implemented analog to the pull command by using an own config
> function instead of using just the default config. In contrast to the pull
> command, the submodule.recurse state is saved as an array of strings as it
> can take an optionally pathspec argument which describes which submodules
> should be recursively initialized and cloned. To recursively initialize and
> clone all submodules a pathspec of "." has to be used.
> The regression test is simplified compared to the test for "git clone
> --recursive" as the general functionality is already checked there.
>
> Signed-off-by: Markus Klein <masmiseim@gmx.de>

... this line. I.e. you will want to run `git config --global user.name
"Markus Klein"` and then `git commit --amend --reset-author`.

Ciao,
Johannes

> ---
>     Add the usage of the submodules.recurse parameter on clone
>
>     I try to finish the pullrequest #573 from Maddimax. This adds the usage
>     of the submodules.recurse parameter on clone
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v1
> Pull-Request: https://github.com/git/git/pull/695
>
>  builtin/clone.c              | 16 +++++++++++++++-
>  t/t7407-submodule-foreach.sh | 11 +++++++++++
>  2 files changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/builtin/clone.c b/builtin/clone.c
> index 0fc89ae2b9..21b9d927a2 100644
> --- a/builtin/clone.c
> +++ b/builtin/clone.c
> @@ -26,6 +26,8 @@
>  #include "dir-iterator.h"
>  #include "iterator.h"
>  #include "sigchain.h"
> +#include "submodule-config.h"
> +#include "submodule.h"
>  #include "branch.h"
>  #include "remote.h"
>  #include "run-command.h"
> @@ -929,6 +931,18 @@ static int path_exists(const char *path)
>  	return !stat(path, &sb);
>  }
>
> +/**
> + * Read config variables.
> + */
> +static int git_clone_config(const char *var, const char *value, void *cb)
> +{
> +	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
> +		string_list_append(&option_recurse_submodules, "true");
> +		return 0;
> +	}
> +	return git_default_config(var, value, cb);
> +}
> +
>  int cmd_clone(int argc, const char **argv, const char *prefix)
>  {
>  	int is_bundle = 0, is_local;
> @@ -1103,7 +1117,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>
>  	write_config(&option_config);
>
> -	git_config(git_default_config, NULL);
> +	git_config(git_clone_config, NULL);
>
>  	if (option_bare) {
>  		if (option_mirror)
> diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
> index 6b2aa917e1..44b32f7b27 100755
> --- a/t/t7407-submodule-foreach.sh
> +++ b/t/t7407-submodule-foreach.sh
> @@ -383,6 +383,17 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
>  		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
>  	)
>  '
> +test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
> +	git clone -c submodule.recurse=true super clone7 &&
> +	(
> +		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
> +		cat >expect <<-EOF &&
> +		.git
> +		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
> +		EOF
> +		test_cmp expect actual
> +	)
> +'
>
>  test_expect_success 'command passed to foreach retains notion of stdin' '
>  	(
>
> base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-01-31 21:11 [PATCH] clone: use submodules.recurse option for automatically clone submodules Markus Klein via GitGitGadget
  2020-02-01 21:19 ` Johannes Schindelin
@ 2020-02-04 21:32 ` Markus Klein via GitGitGadget
  2020-02-05 10:37   ` Johannes Schindelin
                     ` (2 more replies)
  1 sibling, 3 replies; 11+ messages in thread
From: Markus Klein via GitGitGadget @ 2020-02-04 21:32 UTC (permalink / raw)
  To: git; +Cc: Markus Klein, Markus Klein

From: Markus Klein <masmiseim@gmx.de>

Simplify cloning repositories with submodules when the option
submodules.recurse is set by the user. This makes it transparent to the
user if submodules are used. The user doesn’t have to know if he has to add
an extra parameter to get the full project including the used submodules.
This makes clone behave identical to other commands like fetch, pull,
checkout, ... which include the submodules automatically if this option is
set.

It is implemented analog to the pull command by using an own config
function instead of using just the default config. In contrast to the pull
command, the submodule.recurse state is saved as an array of strings as it
can take an optionally pathspec argument which describes which submodules
should be recursively initialized and cloned. To recursively initialize and
clone all submodules a pathspec of "." has to be used.
The regression test is simplified compared to the test for "git clone
--recursive" as the general functionality is already checked there.

Changes since v1:
* Fixed the commit author to match the Signed-off-by line

Signed-off-by: Markus Klein <masmiseim@gmx.de>
---
    Add the usage of the submodules.recurse parameter on clone
    
    I try to finish the pullrequest #573 from Maddimax. This adds the usage
    of the submodules.recurse parameter on clone

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v2
Pull-Request: https://github.com/git/git/pull/695

Range-diff vs v1:

 1:  7fa8d19faf ! 1:  c75835268a clone: use submodules.recurse option for automatically clone submodules
     @@ -1,4 +1,4 @@
     -Author: Markus <masmiseim@gmx.de>
     +Author: Markus Klein <masmiseim@gmx.de>
      
          clone: use submodules.recurse option for automatically clone submodules
      
     @@ -19,6 +19,9 @@
          The regression test is simplified compared to the test for "git clone
          --recursive" as the general functionality is already checked there.
      
     +    Changes since v1:
     +    * Fixed the commit author to match the Signed-off-by line
     +
          Signed-off-by: Markus Klein <masmiseim@gmx.de>
      
       diff --git a/builtin/clone.c b/builtin/clone.c


 builtin/clone.c              | 16 +++++++++++++++-
 t/t7407-submodule-foreach.sh | 11 +++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 0fc89ae2b9..21b9d927a2 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -26,6 +26,8 @@
 #include "dir-iterator.h"
 #include "iterator.h"
 #include "sigchain.h"
+#include "submodule-config.h"
+#include "submodule.h"
 #include "branch.h"
 #include "remote.h"
 #include "run-command.h"
@@ -929,6 +931,18 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+/**
+ * Read config variables.
+ */
+static int git_clone_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
+		string_list_append(&option_recurse_submodules, "true");
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1103,7 +1117,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	write_config(&option_config);
 
-	git_config(git_default_config, NULL);
+	git_config(git_clone_config, NULL);
 
 	if (option_bare) {
 		if (option_mirror)
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 6b2aa917e1..44b32f7b27 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -383,6 +383,17 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
 		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 	)
 '
+test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
+	git clone -c submodule.recurse=true super clone7 &&
+	(
+		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
 
 test_expect_success 'command passed to foreach retains notion of stdin' '
 	(

base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
@ 2020-02-05 10:37   ` Johannes Schindelin
  2020-02-06 19:03   ` Junio C Hamano
  2020-02-24 23:06   ` [PATCH v3] clone: use submodule.recurse " Markus Klein via GitGitGadget
  2 siblings, 0 replies; 11+ messages in thread
From: Johannes Schindelin @ 2020-02-05 10:37 UTC (permalink / raw)
  To: Markus Klein via GitGitGadget; +Cc: git, Markus Klein, Markus Klein

[-- Attachment #1: Type: text/plain, Size: 5154 bytes --]

Hi Markus,

On Tue, 4 Feb 2020, Markus Klein via GitGitGadget wrote:

> From: Markus Klein <masmiseim@gmx.de>
>
> Simplify cloning repositories with submodules when the option
> submodules.recurse is set by the user. This makes it transparent to the
> user if submodules are used. The user doesn’t have to know if he has to add
> an extra parameter to get the full project including the used submodules.
> This makes clone behave identical to other commands like fetch, pull,
> checkout, ... which include the submodules automatically if this option is
> set.
>
> It is implemented analog to the pull command by using an own config
> function instead of using just the default config. In contrast to the pull
> command, the submodule.recurse state is saved as an array of strings as it
> can take an optionally pathspec argument which describes which submodules
> should be recursively initialized and cloned. To recursively initialize and
> clone all submodules a pathspec of "." has to be used.
> The regression test is simplified compared to the test for "git clone
> --recursive" as the general functionality is already checked there.
>
> Changes since v1:
> * Fixed the commit author to match the Signed-off-by line

This changelog should go...

>
> Signed-off-by: Markus Klein <masmiseim@gmx.de>
> ---

... after the `---`. I.e. it should go into the PR description (which is
the first comment on the PR) instead of the commit message.

Ciao,
Johannes

>     Add the usage of the submodules.recurse parameter on clone
>
>     I try to finish the pullrequest #573 from Maddimax. This adds the usage
>     of the submodules.recurse parameter on clone
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v2
> Pull-Request: https://github.com/git/git/pull/695
>
> Range-diff vs v1:
>
>  1:  7fa8d19faf ! 1:  c75835268a clone: use submodules.recurse option for automatically clone submodules
>      @@ -1,4 +1,4 @@
>      -Author: Markus <masmiseim@gmx.de>
>      +Author: Markus Klein <masmiseim@gmx.de>
>
>           clone: use submodules.recurse option for automatically clone submodules
>
>      @@ -19,6 +19,9 @@
>           The regression test is simplified compared to the test for "git clone
>           --recursive" as the general functionality is already checked there.
>
>      +    Changes since v1:
>      +    * Fixed the commit author to match the Signed-off-by line
>      +
>           Signed-off-by: Markus Klein <masmiseim@gmx.de>
>
>        diff --git a/builtin/clone.c b/builtin/clone.c
>
>
>  builtin/clone.c              | 16 +++++++++++++++-
>  t/t7407-submodule-foreach.sh | 11 +++++++++++
>  2 files changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/builtin/clone.c b/builtin/clone.c
> index 0fc89ae2b9..21b9d927a2 100644
> --- a/builtin/clone.c
> +++ b/builtin/clone.c
> @@ -26,6 +26,8 @@
>  #include "dir-iterator.h"
>  #include "iterator.h"
>  #include "sigchain.h"
> +#include "submodule-config.h"
> +#include "submodule.h"
>  #include "branch.h"
>  #include "remote.h"
>  #include "run-command.h"
> @@ -929,6 +931,18 @@ static int path_exists(const char *path)
>  	return !stat(path, &sb);
>  }
>
> +/**
> + * Read config variables.
> + */
> +static int git_clone_config(const char *var, const char *value, void *cb)
> +{
> +	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
> +		string_list_append(&option_recurse_submodules, "true");
> +		return 0;
> +	}
> +	return git_default_config(var, value, cb);
> +}
> +
>  int cmd_clone(int argc, const char **argv, const char *prefix)
>  {
>  	int is_bundle = 0, is_local;
> @@ -1103,7 +1117,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>
>  	write_config(&option_config);
>
> -	git_config(git_default_config, NULL);
> +	git_config(git_clone_config, NULL);
>
>  	if (option_bare) {
>  		if (option_mirror)
> diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
> index 6b2aa917e1..44b32f7b27 100755
> --- a/t/t7407-submodule-foreach.sh
> +++ b/t/t7407-submodule-foreach.sh
> @@ -383,6 +383,17 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
>  		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
>  	)
>  '
> +test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
> +	git clone -c submodule.recurse=true super clone7 &&
> +	(
> +		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
> +		cat >expect <<-EOF &&
> +		.git
> +		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
> +		EOF
> +		test_cmp expect actual
> +	)
> +'
>
>  test_expect_success 'command passed to foreach retains notion of stdin' '
>  	(
>
> base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
  2020-02-05 10:37   ` Johannes Schindelin
@ 2020-02-06 19:03   ` Junio C Hamano
  2020-02-07 15:45     ` Johannes Schindelin
                       ` (2 more replies)
  2020-02-24 23:06   ` [PATCH v3] clone: use submodule.recurse " Markus Klein via GitGitGadget
  2 siblings, 3 replies; 11+ messages in thread
From: Junio C Hamano @ 2020-02-06 19:03 UTC (permalink / raw)
  To: Markus Klein via GitGitGadget; +Cc: git, Markus Klein

"Markus Klein via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Markus Klein <masmiseim@gmx.de>
>
> Simplify cloning repositories with submodules when the option
> submodules.recurse is set by the user. This makes it transparent to the
> user if submodules are used. The user doesn’t have to know if he has to add
> an extra parameter to get the full project including the used submodules.
> This makes clone behave identical to other commands like fetch, pull,
> checkout, ... which include the submodules automatically if this option is
> set.

I am not sure if it is even a good idea to make clone behave
identically to fetch and pull.  We cannot escape from the fact that
the initial cloning of the top-level superproject is a special
event---we do not even have a place to put the configuration
specific to that superproject (e.g. which submodules are good ones
to clone by default) before that happens.

You misspelt "submodule.recurse" everywhere in the log message, by
the way, even though the code seems to react to the right variable.

> It is implemented analog to the pull command by using an own config
> function instead of using just the default config. 

I am not sure if this is worth saying, but it is not incorrect per-se.

> In contrast to the pull
> command, the submodule.recurse state is saved as an array of strings as it
> can take an optionally pathspec argument which describes which submodules
> should be recursively initialized and cloned.

Sorry, but I do not think I get this part at all.  Your callback
seems to add a fixed string "true" to option_recurse_submodules
string list as many times as submodule.recurse variable is defined
in various configuration files.  Does anybody count how many and
react differently?  You mention "pathspec" here, but how does one
specify a pathspec beforehand (remember, this is clone and there is
no superproject repository or its per-repository configuration file
yet before we clone it)?

> To recursively initialize and
> clone all submodules a pathspec of "." has to be used.
> The regression test is simplified compared to the test for "git clone
> --recursive" as the general functionality is already checked there.

Documentation/config/submodule.txt says submodule.recurse says

    Specifies if commands recurse into submodules by default. This
    applies to all commands that have a `--recurse-submodules`
    option, except `clone`.  Defaults to false.

so I take that the value must be a boolean.  So I am lost what
pathspec you are talking about here.

> +/**
> + * Read config variables.
> + */

That's a fairly useless comment that does not say more than what the
name of the function already tells us X-<.

> +static int git_clone_config(const char *var, const char *value, void *cb)
> +{
> +	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
> +		string_list_append(&option_recurse_submodules, "true");
> +		return 0;

The breakage of this is not apparent, but this is misleading.  If
submodule.recurse is set to a value that git_config_bool() would say
"false", the if statement is skipped, and you end up calling
git_default_config() with "submodule.recurse", even though you are
supposed to have already dealt with the setting.

	if (!strcmp(var, "submodule.recurse")) {
		if (git_config_bool(var, value))
			...
		return 0; /* done with the variable either way */
	}

is more appropriate.  I still do not know what this code is trying
to do by appending "true" as many times as submodule.recurse appears
in the configuration file(s), though.

When given from the command line, i.e.

	git clone --no-recurse-submodules ...
	git clone --recurse-submodules    ...
	git clone --recurse-submodules=<something> ...

recurse_submodules_cb() reacts to them by

 (1) clearing what have been accumulated so far,
 (2) appending the match-all "." pathspec, and
 (3) appending the <something> string 

to option_recurse_submodules string list.  But given that
submodule.recurse is not (and will not be without an involved
transition plan) a pathspec but merely a boolean, I would think
appending hardcoded string constant "true" makes little sense.
After sorting the list, these values become values of the
submodule.active configuration variable whose values are pathspec
elements in cmd_clone(); see the part of the code before it makes a
call to init_db().

So, I would sort-of understand if you pretend --recurse-submodules
was given from the command line when submodule.recurse is set to
true (which would mean that you'd append "." to the string list).
But I do not understand why appending "true" is a good thing at all
here.

Another thing I noticed.

If you have "[submodule] recurse" in your $HOME/.gitconfig, you'd
want to be able to countermand from the command line with

    git clone --no-recurse-submodules ...

so that the clone would not go recursive.  And that should be
tested.  

You'd also want the opposite, i.e. with "[submodule] recurse=no" in
your $HOME/.gitconfig and running

    git clone --recurse-submodules ...

should countermand the configuration.

Thanks.

> +test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
> +	git clone -c submodule.recurse=true super clone7 &&
> +	(
> +		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
> +		cat >expect <<-EOF &&
> +		.git
> +		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
> +		EOF
> +		test_cmp expect actual
> +	)
> +'


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-06 19:03   ` Junio C Hamano
@ 2020-02-07 15:45     ` Johannes Schindelin
  2020-02-07 18:17       ` Junio C Hamano
  2020-02-07 18:33     ` Junio C Hamano
  2020-02-24 22:19     ` AW: " masmiseim
  2 siblings, 1 reply; 11+ messages in thread
From: Johannes Schindelin @ 2020-02-07 15:45 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Markus Klein via GitGitGadget, git, Markus Klein

Hi Junio,

On Thu, 6 Feb 2020, Junio C Hamano wrote:

> "Markus Klein via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > +static int git_clone_config(const char *var, const char *value, void *cb)
> > +{
> > +	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
> > +		string_list_append(&option_recurse_submodules, "true");
> > +		return 0;
>
> The breakage of this is not apparent, but this is misleading.  If
> submodule.recurse is set to a value that git_config_bool() would say
> "false", the if statement is skipped, and you end up calling
> git_default_config() with "submodule.recurse", even though you are
> supposed to have already dealt with the setting.
>
> 	if (!strcmp(var, "submodule.recurse")) {
> 		if (git_config_bool(var, value))
> 			...
> 		return 0; /* done with the variable either way */
> 	}
>
> is more appropriate.

Good catch, and I think you will have to do even more: in the "else" case,
it is possible that the user overrode a `submodule.recurse` from the
system config in their user-wide config, so we must _undo_ the
`string_list_append().

Further, it is probably not a good idea to append "true" _twice_ if
multiple configs in the chain specify `submodule.recurse = true`.

> I still do not know what this code is trying to do by appending "true"
> as many times as submodule.recurse appears in the configuration file(s),
> though.
>
> When given from the command line, i.e.
>
> 	git clone --no-recurse-submodules ...
> 	git clone --recurse-submodules    ...
> 	git clone --recurse-submodules=<something> ...
>
> recurse_submodules_cb() reacts to them by
>
>  (1) clearing what have been accumulated so far,
>  (2) appending the match-all "." pathspec, and
>  (3) appending the <something> string
>
> to option_recurse_submodules string list.  But given that
> submodule.recurse is not (and will not be without an involved
> transition plan) a pathspec but merely a boolean, I would think
> appending hardcoded string constant "true" makes little sense.
> After sorting the list, these values become values of the
> submodule.active configuration variable whose values are pathspec
> elements in cmd_clone(); see the part of the code before it makes a
> call to init_db().

Indeed, I think I even pointed out that "true" is not an appropriate value
to use here: https://github.com/git/git/pull/695/#discussion_r367866708

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-07 15:45     ` Johannes Schindelin
@ 2020-02-07 18:17       ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2020-02-07 18:17 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Markus Klein via GitGitGadget, git, Markus Klein

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> 	if (!strcmp(var, "submodule.recurse")) {
>> 		if (git_config_bool(var, value))
>> 			...
>> 		return 0; /* done with the variable either way */
>> 	}
>>
>> is more appropriate.
>
> Good catch, and I think you will have to do even more: in the "else" case,
> it is possible that the user overrode a `submodule.recurse` from the
> system config in their user-wide config, so we must _undo_ the
> `string_list_append().

Yeah, I tend to agree that submodule.recurse should not be made into
a multi-valued fields with this change; it should stay to be the
usual last-one-wins single boolean.

> Further, it is probably not a good idea to append "true" _twice_ if
> multiple configs in the chain specify `submodule.recurse = true`.

The user of this list in cmd_clone() first sorts and dedups, so
appending the same is OK, even though it may appear sloppy.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-06 19:03   ` Junio C Hamano
  2020-02-07 15:45     ` Johannes Schindelin
@ 2020-02-07 18:33     ` Junio C Hamano
  2020-02-24 22:19     ` AW: " masmiseim
  2 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2020-02-07 18:33 UTC (permalink / raw)
  To: Markus Klein, Johannes Schindelin; +Cc: git, Markus Klein via GitGitGadget

Junio C Hamano <gitster@pobox.com> writes:

> So, I would sort-of understand if you pretend --recurse-submodules
> was given from the command line when submodule.recurse is set to
> true (which would mean that you'd append "." to the string list).
> But I do not understand why appending "true" is a good thing at all
> here.
>
> Another thing I noticed.
>
> If you have "[submodule] recurse" in your $HOME/.gitconfig, you'd
> want to be able to countermand from the command line with
>
>     git clone --no-recurse-submodules ...
>
> so that the clone would not go recursive.  And that should be
> tested.  
>
> You'd also want the opposite, i.e. with "[submodule] recurse=no" in
> your $HOME/.gitconfig and running
>
>     git clone --recurse-submodules ...
>
> should countermand the configuration.


Totally untested, but just to illustrate the approach, here is a
sample patch to implement "Pretend --recurse-submodules=. is set on
the command line when submodule.recurse is set (in the 'last one
wins' sense) and there is no --recurse-submodules command line
option."  It should outline the right interactions between the
command line options and configuration variable, like allowing "git
clone --no-recurse-submodules" to defeat submodule.recurse
configuration.

Not that I agree that "[submodules] recurse" set in the
$HOME/.gitconfig should affect "git clone".  It is merely to
illustrate how it could be done, if it were a good idea.

 builtin/clone.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 0fc89ae2b9..163803d89e 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -32,6 +32,7 @@
 #include "connected.h"
 #include "packfile.h"
 #include "list-objects-filter-options.h"
+#include "submodule.h"
 
 /*
  * Overall FIXMEs:
@@ -71,6 +72,8 @@ static struct list_objects_filter_options filter_options;
 static struct string_list server_options = STRING_LIST_INIT_NODUP;
 static int option_remote_submodules;
 
+static int recurse_submodules_option_given;
+
 static int recurse_submodules_cb(const struct option *opt,
 				 const char *arg, int unset)
 {
@@ -81,7 +84,7 @@ static int recurse_submodules_cb(const struct option *opt,
 	else
 		string_list_append((struct string_list *)opt->value,
 				   (const char *)opt->defval);
-
+	recurse_submodules_option_given = 1;
 	return 0;
 }
 
@@ -929,6 +932,13 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+static int git_clone_config(const char *var, const char *value, void *cb)
+{
+	if (starts_with(var, "submodule."))
+		return git_default_submodule_config(var, value, NULL);
+	return git_default_config(var, value, cb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1103,7 +1113,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	write_config(&option_config);
 
-	git_config(git_default_config, NULL);
+	git_config(git_clone_config, NULL);
+	if (!recurse_submodules_option_given && should_update_submodules())
+		string_list_append(&option_recurse_submodules, ".");
 
 	if (option_bare) {
 		if (option_mirror)

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* AW: [PATCH v2] clone: use submodules.recurse option for automatically clone submodules
  2020-02-06 19:03   ` Junio C Hamano
  2020-02-07 15:45     ` Johannes Schindelin
  2020-02-07 18:33     ` Junio C Hamano
@ 2020-02-24 22:19     ` masmiseim
  2 siblings, 0 replies; 11+ messages in thread
From: masmiseim @ 2020-02-24 22:19 UTC (permalink / raw)
  To: 'Junio C Hamano', 'Markus Klein via GitGitGadget'; +Cc: git

> "Markus Klein via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
> > From: Markus Klein <masmiseim@gmx.de>
> >
> > Simplify cloning repositories with submodules when the option 
> > submodules.recurse is set by the user. This makes it transparent to 
> > the user if submodules are used. The user doesn’t have to know if he 
> > has to add an extra parameter to get the full project including the used submodules.
> > This makes clone behave identical to other commands like fetch, pull, 
> > checkout, ... which include the submodules automatically if this 
> > option is set.
> 
> I am not sure if it is even a good idea to make clone behave identically to fetch and pull. 
> We cannot escape from the fact that the initial cloning of the top-level superproject is a special
> event---we do not even have a place to put the configuration specific to that superproject
> (e.g. which submodules are good ones to clone by default) before that happens.

It behaves only identical if the option "submodule.recurse" is set in the global .gitconfig. So, it is optional for people who know what they do. For people which use submodules heavily, this is very useful.

For the case where you don't like to get all submodules but have this option set, you can disable it via --no-recurse-submodules

> 
> You misspelt "submodule.recurse" everywhere in the log message, by the way, even though the code seems
> to react to the right variable.
> 
> > It is implemented analog to the pull command by using an own config 
> > function instead of using just the default config.
> 
> I am not sure if this is worth saying, but it is not incorrect per-se.
> 
> > In contrast to the pull
> > command, the submodule.recurse state is saved as an array of strings 
> > as it can take an optionally pathspec argument which describes which 
> > submodules should be recursively initialized and cloned.
> 
> Sorry, but I do not think I get this part at all.  Your callback seems to add a fixed string "true"
> to option_recurse_submodules string list as many times as submodule.recurse variable is defined in
> various configuration files.  Does anybody count how many and react differently?  You mention "pathspec"
> here, but how does one specify a pathspec beforehand (remember, this is clone and there is no superproject
> repository or its per-repository configuration file yet before we clone it)?

I'm so sorry for the confusing with the true. This is definitely wrong. Johannes already pointed this out to me and I had already fixed it. Shame on me, as I had uploaded an old version :-(

> 
> > To recursively initialize and
> > clone all submodules a pathspec of "." has to be used.
> > The regression test is simplified compared to the test for "git clone 
> > --recursive" as the general functionality is already checked there.
> 
> Documentation/config/submodule.txt says submodule.recurse says
> 
>     Specifies if commands recurse into submodules by default. This
>     applies to all commands that have a `--recurse-submodules`
>     option, except `clone`.  Defaults to false.
> 
> so I take that the value must be a boolean.  So I am lost what pathspec you are talking about here.
> 
> > +/**
> > + * Read config variables.
> > + */
> 
> That's a fairly useless comment that does not say more than what the name of the function already tells us X-<.

True, this was copy'pasted from the pull implementation. So it should be useless there also.

> 
> > +static int git_clone_config(const char *var, const char *value, void 
> > +*cb) {
> > +	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
> > +		string_list_append(&option_recurse_submodules, "true");
> > +		return 0;
> 
> The breakage of this is not apparent, but this is misleading.  If submodule.recurse is set to a value
> that git_config_bool() would say "false", the if statement is skipped, and you end up calling
> git_default_config() with "submodule.recurse", even though you are supposed to have already dealt with
> the setting.
> 
> 	if (!strcmp(var, "submodule.recurse")) {
> 		if (git_config_bool(var, value))
> 			...
> 		return 0; /* done with the variable either way */
> 	}
> 
> is more appropriate.  I still do not know what this code is trying to do by appending "true" as many
> times as submodule.recurse appears in the configuration file(s), though.
> 
> When given from the command line, i.e.
> 
> 	git clone --no-recurse-submodules ...
> 	git clone --recurse-submodules    ...
> 	git clone --recurse-submodules=<something> ...
> 
> recurse_submodules_cb() reacts to them by
> 
>  (1) clearing what have been accumulated so far,
>  (2) appending the match-all "." pathspec, and
>  (3) appending the <something> string 
> 
> to option_recurse_submodules string list.  But given that submodule.recurse is not (and will not be without
> an involved transition plan) a pathspec but merely a boolean, I would think appending hardcoded string
> constant "true" makes little sense.
> After sorting the list, these values become values of the submodule.active configuration variable whose
> values are pathspec elements in cmd_clone(); see the part of the code before it makes a call to init_db().
> 
> So, I would sort-of understand if you pretend --recurse-submodules was given from the command line when
> submodule.recurse is set to true (which would mean that you'd append "." to the string list).
> But I do not understand why appending "true" is a good thing at all here.
> 
> Another thing I noticed.
> 
> If you have "[submodule] recurse" in your $HOME/.gitconfig, you'd want to be able to countermand
> from the command line with
> 
>     git clone --no-recurse-submodules ...
> 
> so that the clone would not go recursive.  And that should be tested.  
> 
> You'd also want the opposite, i.e. with "[submodule] recurse=no" in your $HOME/.gitconfig and running
> 
>     git clone --recurse-submodules ...
> 
> should countermand the configuration.

Thanks for the hint. I added this tests, and it was very helpful, as it pointed out, that the disabling via --no-recurse-submodules was not working.

> 
> Thanks.
> 
> > +test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
> > +	git clone -c submodule.recurse=true super clone7 &&
> > +	(
> > +		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
> > +		cat >expect <<-EOF &&
> > +		.git
> > +		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
> > +		EOF
> > +		test_cmp expect actual
> > +	)
> > +'

Thanks for the feedback



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] clone: use submodule.recurse option for automatically clone submodules
  2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
  2020-02-05 10:37   ` Johannes Schindelin
  2020-02-06 19:03   ` Junio C Hamano
@ 2020-02-24 23:06   ` Markus Klein via GitGitGadget
  2020-05-01 13:54     ` [PATCH v4] " Markus Klein via GitGitGadget
  2 siblings, 1 reply; 11+ messages in thread
From: Markus Klein via GitGitGadget @ 2020-02-24 23:06 UTC (permalink / raw)
  To: git; +Cc: Markus Klein, Markus Klein

From: Markus Klein <masmiseim@gmx.de>

Simplify cloning repositories with submodules when the option
submodule.recurse is set by the user. This makes it transparent to the
user if submodules are used. The user doesn’t have to know if he has to add
an extra parameter to get the full project including the used submodules.
This makes clone behave identical to other commands like fetch, pull,
checkout, ... which include the submodules automatically if this option is
set.

It is implemented analog to the pull command by using an own config
function instead of using just the default config. In contrast to the pull
command, the submodule.recurse state is saved in addition as an array of
strings as it can take an optionally pathspec argument which describes
which submodules should be recursively initialized and cloned. To
recursively initialize and clone all submodules a pathspec of "." has to
be used.
The regression test is simplified compared to the test for "git clone
--recursive" as the general functionality is already checked there.

Signed-off-by: Markus Klein <masmiseim@gmx.de>
---
    Add the usage of the submodules.recurse parameter on clone
    
    This adds the usage of the submodule.recurse parameter on clone
    
    Changes since v1:
    
     * Fixed the commit author to match the Signed-off-by line
    
    Changes since v2:
    
     * Added additional regression tests for checking the
       --no-recurse-submodules parameter
     * fixed not working --no-recurse-submodules parameter when
       submodule.recurse option is set
     * fixed invalid setting of “true” to the pathspec

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v3
Pull-Request: https://github.com/git/git/pull/695

Range-diff vs v2:

 1:  c75835268a2 ! 1:  f71ca264885 clone: use submodules.recurse option for automatically clone submodules
     @@ -1,9 +1,9 @@
      Author: Markus Klein <masmiseim@gmx.de>
      
     -    clone: use submodules.recurse option for automatically clone submodules
     +    clone: use submodule.recurse option for automatically clone submodules
      
          Simplify cloning repositories with submodules when the option
     -    submodules.recurse is set by the user. This makes it transparent to the
     +    submodule.recurse is set by the user. This makes it transparent to the
          user if submodules are used. The user doesn’t have to know if he has to add
          an extra parameter to get the full project including the used submodules.
          This makes clone behave identical to other commands like fetch, pull,
     @@ -12,16 +12,14 @@
      
          It is implemented analog to the pull command by using an own config
          function instead of using just the default config. In contrast to the pull
     -    command, the submodule.recurse state is saved as an array of strings as it
     -    can take an optionally pathspec argument which describes which submodules
     -    should be recursively initialized and cloned. To recursively initialize and
     -    clone all submodules a pathspec of "." has to be used.
     +    command, the submodule.recurse state is saved in addition as an array of
     +    strings as it can take an optionally pathspec argument which describes
     +    which submodules should be recursively initialized and cloned. To
     +    recursively initialize and clone all submodules a pathspec of "." has to
     +    be used.
          The regression test is simplified compared to the test for "git clone
          --recursive" as the general functionality is already checked there.
      
     -    Changes since v1:
     -    * Fixed the commit author to match the Signed-off-by line
     -
          Signed-off-by: Markus Klein <masmiseim@gmx.de>
      
       diff --git a/builtin/clone.c b/builtin/clone.c
     @@ -36,17 +34,156 @@
       #include "branch.h"
       #include "remote.h"
       #include "run-command.h"
     +@@
     + 	NULL
     + };
     + 
     ++struct option_submodules
     ++{
     ++	struct string_list option;
     ++	int status;
     ++};
     ++
     + static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
     + static int option_local = -1, option_no_hardlinks, option_shared;
     + static int option_no_tags;
     +@@
     + 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 struct option_submodules option_recurse_submodules = {STRING_LIST_INIT_NODUP, RECURSE_SUBMODULES_DEFAULT};
     + static struct list_objects_filter_options filter_options;
     + static struct string_list server_options = STRING_LIST_INIT_NODUP;
     + static int option_remote_submodules;
     +@@
     + 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);
     ++	struct option_submodules *list = (struct option_submodules *)(opt->value);
     ++
     ++	if (unset) {
     ++		string_list_clear(&list->option, 0);
     ++		list->status = RECURSE_SUBMODULES_OFF;
     ++	}
     ++	else if (arg) {
     ++		string_list_append(&list->option, arg);
     ++		list->status = RECURSE_SUBMODULES_ON_DEMAND;
     ++	}
     ++	else {
     ++		string_list_append(&list->option, (const char *)opt->defval);
     ++		list->status = RECURSE_SUBMODULES_ON;
     ++	}
     + 
     + 	return 0;
     + }
     +@@
     + static struct option builtin_clone_options[] = {
     + 	OPT__VERBOSITY(&option_verbosity),
     + 	OPT_BOOL(0, "progress", &option_progress,
     +-		 N_("force progress reporting")),
     ++		N_("force progress reporting")),
     + 	OPT_BOOL('n', "no-checkout", &option_no_checkout,
     +-		 N_("don't create a checkout")),
     ++		N_("don't create a checkout")),
     + 	OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
     + 	OPT_HIDDEN_BOOL(0, "naked", &option_bare,
     + 			N_("create a bare repository")),
     +@@
     + 	OPT_BOOL('l', "local", &option_local,
     + 		N_("to clone from a local repository")),
     + 	OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
     +-		    N_("don't use local hardlinks, always copy")),
     ++			N_("don't use local hardlinks, always copy")),
     + 	OPT_BOOL('s', "shared", &option_shared,
     +-		    N_("setup as shared repository")),
     ++			N_("setup as shared repository")),
     + 	OPT_ALIAS(0, "recursive", "recurse-submodules"),
     + 	{ 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")),
     ++			N_("number of submodules cloned in parallel")),
     + 	OPT_STRING(0, "template", &option_template, N_("template-directory"),
     +-		   N_("directory from which templates will be used")),
     ++			N_("directory from which templates will be used")),
     + 	OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
     + 			N_("reference repository")),
     + 	OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
     + 			N_("repo"), N_("reference repository")),
     + 	OPT_BOOL(0, "dissociate", &option_dissociate,
     +-		 N_("use --reference only while cloning")),
     ++			N_("use --reference only while cloning")),
     + 	OPT_STRING('o', "origin", &option_origin, N_("name"),
     +-		   N_("use <name> instead of 'origin' to track upstream")),
     ++			N_("use <name> instead of 'origin' to track upstream")),
     + 	OPT_STRING('b', "branch", &option_branch, N_("branch"),
     +-		   N_("checkout <branch> instead of the remote's HEAD")),
     ++			N_("checkout <branch> instead of the remote's HEAD")),
     + 	OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
     +-		   N_("path to git-upload-pack on the remote")),
     ++			N_("path to git-upload-pack on the remote")),
     + 	OPT_STRING(0, "depth", &option_depth, N_("depth"),
     +-		    N_("create a shallow clone of that depth")),
     ++			N_("create a shallow clone of that depth")),
     + 	OPT_STRING(0, "shallow-since", &option_since, N_("time"),
     +-		    N_("create a shallow clone since a specific time")),
     ++			N_("create a shallow clone since a specific time")),
     + 	OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
     + 			N_("deepen history of shallow clone, excluding rev")),
     + 	OPT_BOOL(0, "single-branch", &option_single_branch,
     +-		    N_("clone only one branch, HEAD or --branch")),
     ++			N_("clone only one branch, HEAD or --branch")),
     + 	OPT_BOOL(0, "no-tags", &option_no_tags,
     +-		 N_("don't clone any tags, and make later fetches not to follow them")),
     ++			N_("don't clone any tags, and make later fetches not to follow them")),
     + 	OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
     +-		    N_("any cloned submodules will be shallow")),
     ++			N_("any cloned submodules will be shallow")),
     + 	OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
     +-		   N_("separate git dir from working tree")),
     ++			N_("separate git dir from working tree")),
     + 	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
     + 			N_("set config inside the new repository")),
     + 	OPT_STRING_LIST(0, "server-option", &server_options,
     +@@
     + 			TRANSPORT_FAMILY_IPV6),
     + 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
     + 	OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
     +-		    N_("any cloned submodules will use their remote-tracking branch")),
     ++			N_("any cloned submodules will use their remote-tracking branch")),
     + 	OPT_BOOL(0, "sparse", &option_sparse_checkout,
     +-		    N_("initialize sparse-checkout file to include only files at root")),
     ++			N_("initialize sparse-checkout file to include only files at root")),
     + 	OPT_END()
     + };
     + 
     +@@
     + 	err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
     + 			   oid_to_hex(&oid), "1", NULL);
     + 
     +-	if (!err && (option_recurse_submodules.nr > 0)) {
     ++	if (!err && (option_recurse_submodules.option.nr > 0)) {
     + 		struct argv_array args = ARGV_ARRAY_INIT;
     + 		argv_array_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
     + 
      @@
       	return !stat(path, &sb);
       }
       
     -+/**
     -+ * Read config variables.
     -+ */
      +static int git_clone_config(const char *var, const char *value, void *cb)
      +{
     -+	if (!strcmp(var, "submodule.recurse") && git_config_bool(var, value)) {
     -+		string_list_append(&option_recurse_submodules, "true");
     ++	if (!strcmp(var, "submodule.recurse")){
     ++		if (git_config_bool(var, value) && option_recurse_submodules.status != RECURSE_SUBMODULES_OFF)
     ++			string_list_append(&option_recurse_submodules.option, ".");
      +		return 0;
      +	}
      +	return git_default_config(var, value, cb);
     @@ -56,6 +193,30 @@
       {
       	int is_bundle = 0, is_local;
      @@
     + 			fprintf(stderr, _("Cloning into '%s'...\n"), dir);
     + 	}
     + 
     +-	if (option_recurse_submodules.nr > 0) {
     ++	if (option_recurse_submodules.option.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);
     ++		string_list_sort(&option_recurse_submodules.option);
     ++		string_list_remove_duplicates(&option_recurse_submodules.option, 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) {
     ++		for_each_string_list_item(item, &option_recurse_submodules.option) {
     + 			strbuf_addf(&sb, "submodule.active=%s",
     + 				    item->string);
     + 			string_list_append(&option_config,
     +@@
       
       	write_config(&option_config);
       
     @@ -82,6 +243,25 @@
      +		EOF
      +		test_cmp expect actual
      +	)
     ++'
     ++
     ++test_expect_success 'use "git clone" with submodule.recurse=false and --recurse-submodules to checkout all submodules' '
     ++	git clone -c submodule.recurse=false --recurse-submodules super clone8 &&
     ++	(
     ++		git -C clone8 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
     ++		cat >expect <<-EOF &&
     ++		.git
     ++		$(pwd)/clone8/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
     ++		EOF
     ++		test_cmp expect actual
     ++	)
     ++'
     ++
     ++test_expect_success 'use "git clone" with submodule.recurse=true and --no-recurse-submodules to checkout no submodules' '
     ++	git clone -c submodule.recurse=true --no-recurse-submodules super clone9 &&
     ++	(
     ++		test_path_is_missing clone9/nested1/nested2/nested3/submodule
     ++	)
      +'
       
       test_expect_success 'command passed to foreach retains notion of stdin' '


 builtin/clone.c              | 89 +++++++++++++++++++++++-------------
 t/t7407-submodule-foreach.sh | 30 ++++++++++++
 2 files changed, 87 insertions(+), 32 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 0fc89ae2b9c..dd45b6bb6a0 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -26,6 +26,8 @@
 #include "dir-iterator.h"
 #include "iterator.h"
 #include "sigchain.h"
+#include "submodule-config.h"
+#include "submodule.h"
 #include "branch.h"
 #include "remote.h"
 #include "run-command.h"
@@ -46,6 +48,12 @@ static const char * const builtin_clone_usage[] = {
 	NULL
 };
 
+struct option_submodules
+{
+	struct string_list option;
+	int status;
+};
+
 static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
 static int option_local = -1, option_no_hardlinks, option_shared;
 static int option_no_tags;
@@ -66,7 +74,7 @@ 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 struct option_submodules option_recurse_submodules = {STRING_LIST_INIT_NODUP, RECURSE_SUBMODULES_DEFAULT};
 static struct list_objects_filter_options filter_options;
 static struct string_list server_options = STRING_LIST_INIT_NODUP;
 static int option_remote_submodules;
@@ -74,13 +82,20 @@ static int option_remote_submodules;
 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);
+	struct option_submodules *list = (struct option_submodules *)(opt->value);
+
+	if (unset) {
+		string_list_clear(&list->option, 0);
+		list->status = RECURSE_SUBMODULES_OFF;
+	}
+	else if (arg) {
+		string_list_append(&list->option, arg);
+		list->status = RECURSE_SUBMODULES_ON_DEMAND;
+	}
+	else {
+		string_list_append(&list->option, (const char *)opt->defval);
+		list->status = RECURSE_SUBMODULES_ON;
+	}
 
 	return 0;
 }
@@ -88,9 +103,9 @@ static int recurse_submodules_cb(const struct option *opt,
 static struct option builtin_clone_options[] = {
 	OPT__VERBOSITY(&option_verbosity),
 	OPT_BOOL(0, "progress", &option_progress,
-		 N_("force progress reporting")),
+		N_("force progress reporting")),
 	OPT_BOOL('n', "no-checkout", &option_no_checkout,
-		 N_("don't create a checkout")),
+		N_("don't create a checkout")),
 	OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
 	OPT_HIDDEN_BOOL(0, "naked", &option_bare,
 			N_("create a bare repository")),
@@ -99,43 +114,43 @@ static struct option builtin_clone_options[] = {
 	OPT_BOOL('l', "local", &option_local,
 		N_("to clone from a local repository")),
 	OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
-		    N_("don't use local hardlinks, always copy")),
+			N_("don't use local hardlinks, always copy")),
 	OPT_BOOL('s', "shared", &option_shared,
-		    N_("setup as shared repository")),
+			N_("setup as shared repository")),
 	OPT_ALIAS(0, "recursive", "recurse-submodules"),
 	{ 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")),
+			N_("number of submodules cloned in parallel")),
 	OPT_STRING(0, "template", &option_template, N_("template-directory"),
-		   N_("directory from which templates will be used")),
+			N_("directory from which templates will be used")),
 	OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
 			N_("reference repository")),
 	OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
 			N_("repo"), N_("reference repository")),
 	OPT_BOOL(0, "dissociate", &option_dissociate,
-		 N_("use --reference only while cloning")),
+			N_("use --reference only while cloning")),
 	OPT_STRING('o', "origin", &option_origin, N_("name"),
-		   N_("use <name> instead of 'origin' to track upstream")),
+			N_("use <name> instead of 'origin' to track upstream")),
 	OPT_STRING('b', "branch", &option_branch, N_("branch"),
-		   N_("checkout <branch> instead of the remote's HEAD")),
+			N_("checkout <branch> instead of the remote's HEAD")),
 	OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
-		   N_("path to git-upload-pack on the remote")),
+			N_("path to git-upload-pack on the remote")),
 	OPT_STRING(0, "depth", &option_depth, N_("depth"),
-		    N_("create a shallow clone of that depth")),
+			N_("create a shallow clone of that depth")),
 	OPT_STRING(0, "shallow-since", &option_since, N_("time"),
-		    N_("create a shallow clone since a specific time")),
+			N_("create a shallow clone since a specific time")),
 	OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
 			N_("deepen history of shallow clone, excluding rev")),
 	OPT_BOOL(0, "single-branch", &option_single_branch,
-		    N_("clone only one branch, HEAD or --branch")),
+			N_("clone only one branch, HEAD or --branch")),
 	OPT_BOOL(0, "no-tags", &option_no_tags,
-		 N_("don't clone any tags, and make later fetches not to follow them")),
+			N_("don't clone any tags, and make later fetches not to follow them")),
 	OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
-		    N_("any cloned submodules will be shallow")),
+			N_("any cloned submodules will be shallow")),
 	OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
-		   N_("separate git dir from working tree")),
+			N_("separate git dir from working tree")),
 	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
 			N_("set config inside the new repository")),
 	OPT_STRING_LIST(0, "server-option", &server_options,
@@ -146,9 +161,9 @@ static struct option builtin_clone_options[] = {
 			TRANSPORT_FAMILY_IPV6),
 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
 	OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
-		    N_("any cloned submodules will use their remote-tracking branch")),
+			N_("any cloned submodules will use their remote-tracking branch")),
 	OPT_BOOL(0, "sparse", &option_sparse_checkout,
-		    N_("initialize sparse-checkout file to include only files at root")),
+			N_("initialize sparse-checkout file to include only files at root")),
 	OPT_END()
 };
 
@@ -811,7 +826,7 @@ static int checkout(int submodule_progress)
 	err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
 			   oid_to_hex(&oid), "1", NULL);
 
-	if (!err && (option_recurse_submodules.nr > 0)) {
+	if (!err && (option_recurse_submodules.option.nr > 0)) {
 		struct argv_array args = ARGV_ARRAY_INIT;
 		argv_array_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
 
@@ -929,6 +944,16 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+static int git_clone_config(const char *var, const char *value, void *cb)
+{
+	if (!strcmp(var, "submodule.recurse")){
+		if (git_config_bool(var, value) && option_recurse_submodules.status != RECURSE_SUBMODULES_OFF)
+			string_list_append(&option_recurse_submodules.option, ".");
+		return 0;
+	}
+	return git_default_config(var, value, cb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1060,19 +1085,19 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			fprintf(stderr, _("Cloning into '%s'...\n"), dir);
 	}
 
-	if (option_recurse_submodules.nr > 0) {
+	if (option_recurse_submodules.option.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);
+		string_list_sort(&option_recurse_submodules.option);
+		string_list_remove_duplicates(&option_recurse_submodules.option, 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) {
+		for_each_string_list_item(item, &option_recurse_submodules.option) {
 			strbuf_addf(&sb, "submodule.active=%s",
 				    item->string);
 			string_list_append(&option_config,
@@ -1103,7 +1128,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	write_config(&option_config);
 
-	git_config(git_default_config, NULL);
+	git_config(git_clone_config, NULL);
 
 	if (option_bare) {
 		if (option_mirror)
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 6b2aa917e11..e6667d8e9b0 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -383,6 +383,36 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
 		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 	)
 '
+test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
+	git clone -c submodule.recurse=true super clone7 &&
+	(
+		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'use "git clone" with submodule.recurse=false and --recurse-submodules to checkout all submodules' '
+	git clone -c submodule.recurse=false --recurse-submodules super clone8 &&
+	(
+		git -C clone8 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone8/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'use "git clone" with submodule.recurse=true and --no-recurse-submodules to checkout no submodules' '
+	git clone -c submodule.recurse=true --no-recurse-submodules super clone9 &&
+	(
+		test_path_is_missing clone9/nested1/nested2/nested3/submodule
+	)
+'
 
 test_expect_success 'command passed to foreach retains notion of stdin' '
 	(

base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4] clone: use submodule.recurse option for automatically clone submodules
  2020-02-24 23:06   ` [PATCH v3] clone: use submodule.recurse " Markus Klein via GitGitGadget
@ 2020-05-01 13:54     ` Markus Klein via GitGitGadget
  0 siblings, 0 replies; 11+ messages in thread
From: Markus Klein via GitGitGadget @ 2020-05-01 13:54 UTC (permalink / raw)
  To: git; +Cc: Markus Klein, Markus Klein

From: Markus Klein <masmiseim@gmx.de>

Simplify cloning repositories with submodules when the option
submodule.recurse is set by the user. This makes it transparent to the
user if submodules are used. The user doesn’t have to know if he has to add
an extra parameter to get the full project including the used submodules.
This makes clone behave identical to other commands like fetch, pull,
checkout, ... which include the submodules automatically if this option is
set.

It is implemented analog to the pull command by using an own config
function instead of using just the default config. In contrast to the pull
command, the submodule.recurse state is saved in addition as an array of
strings as it can take an optionally pathspec argument which describes
which submodules should be recursively initialized and cloned. To
recursively initialize and clone all submodules a pathspec of "." has to
be used.
The regression test is simplified compared to the test for "git clone
--recursive" as the general functionality is already checked there.

Signed-off-by: Markus Klein <masmiseim@gmx.de>
---
    Add the usage of the submodules.recurse parameter on clone
    
    This adds the usage of the submodule.recurse parameter on clone
    
    Changes since v1:
    
     * Fixed the commit author to match the Signed-off-by line
    
    Changes since v2:
    
     * Added additional regression tests for checking the
       --no-recurse-submodules parameter
     * fixed not working --no-recurse-submodules parameter when
       submodule.recurse option is set
     * fixed invalid setting of “true” to the pathspec
    
    Changes since v3:
    
     * Using the solution from Junio C Hamano which is tested with the
       regression tests introduced in V3

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-695%2FMasmiseim36%2Fdev%2FCloneWithSubmodule-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-695/Masmiseim36/dev/CloneWithSubmodule-v4
Pull-Request: https://github.com/git/git/pull/695

Range-diff vs v3:

 1:  f71ca264885 ! 1:  e502f370b04 clone: use submodule.recurse option for automatically clone submodules
     @@ Commit message
      
       ## builtin/clone.c ##
      @@
     - #include "dir-iterator.h"
     - #include "iterator.h"
     - #include "sigchain.h"
     -+#include "submodule-config.h"
     + #include "connected.h"
     + #include "packfile.h"
     + #include "list-objects-filter-options.h"
      +#include "submodule.h"
     - #include "branch.h"
     - #include "remote.h"
     - #include "run-command.h"
     -@@ builtin/clone.c: static const char * const builtin_clone_usage[] = {
     - 	NULL
     - };
       
     -+struct option_submodules
     -+{
     -+	struct string_list option;
     -+	int status;
     -+};
     -+
     - static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
     - static int option_local = -1, option_no_hardlinks, option_shared;
     - static int option_no_tags;
     -@@ builtin/clone.c: 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 struct option_submodules option_recurse_submodules = {STRING_LIST_INIT_NODUP, RECURSE_SUBMODULES_DEFAULT};
     - static struct list_objects_filter_options filter_options;
     + /*
     +  * Overall FIXMEs:
     +@@ builtin/clone.c: static struct list_objects_filter_options filter_options;
       static struct string_list server_options = STRING_LIST_INIT_NODUP;
       static int option_remote_submodules;
     -@@ builtin/clone.c: static int option_remote_submodules;
     + 
     ++static int recurse_submodules_option_given;
     ++
       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);
     -+	struct option_submodules *list = (struct option_submodules *)(opt->value);
     -+
     -+	if (unset) {
     -+		string_list_clear(&list->option, 0);
     -+		list->status = RECURSE_SUBMODULES_OFF;
     -+	}
     -+	else if (arg) {
     -+		string_list_append(&list->option, arg);
     -+		list->status = RECURSE_SUBMODULES_ON_DEMAND;
     -+	}
     -+	else {
     -+		string_list_append(&list->option, (const char *)opt->defval);
     -+		list->status = RECURSE_SUBMODULES_ON;
     -+	}
     - 
     +@@ builtin/clone.c: static int recurse_submodules_cb(const struct option *opt,
     + 	else
     + 		string_list_append((struct string_list *)opt->value,
     + 				   (const char *)opt->defval);
     +-
     ++	recurse_submodules_option_given = 1;
       	return 0;
       }
     -@@ builtin/clone.c: static int recurse_submodules_cb(const struct option *opt,
     - static struct option builtin_clone_options[] = {
     - 	OPT__VERBOSITY(&option_verbosity),
     - 	OPT_BOOL(0, "progress", &option_progress,
     --		 N_("force progress reporting")),
     -+		N_("force progress reporting")),
     - 	OPT_BOOL('n', "no-checkout", &option_no_checkout,
     --		 N_("don't create a checkout")),
     -+		N_("don't create a checkout")),
     - 	OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
     - 	OPT_HIDDEN_BOOL(0, "naked", &option_bare,
     - 			N_("create a bare repository")),
     -@@ builtin/clone.c: static struct option builtin_clone_options[] = {
     - 	OPT_BOOL('l', "local", &option_local,
     - 		N_("to clone from a local repository")),
     - 	OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
     --		    N_("don't use local hardlinks, always copy")),
     -+			N_("don't use local hardlinks, always copy")),
     - 	OPT_BOOL('s', "shared", &option_shared,
     --		    N_("setup as shared repository")),
     -+			N_("setup as shared repository")),
     - 	OPT_ALIAS(0, "recursive", "recurse-submodules"),
     - 	{ 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")),
     -+			N_("number of submodules cloned in parallel")),
     - 	OPT_STRING(0, "template", &option_template, N_("template-directory"),
     --		   N_("directory from which templates will be used")),
     -+			N_("directory from which templates will be used")),
     - 	OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
     - 			N_("reference repository")),
     - 	OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
     - 			N_("repo"), N_("reference repository")),
     - 	OPT_BOOL(0, "dissociate", &option_dissociate,
     --		 N_("use --reference only while cloning")),
     -+			N_("use --reference only while cloning")),
     - 	OPT_STRING('o', "origin", &option_origin, N_("name"),
     --		   N_("use <name> instead of 'origin' to track upstream")),
     -+			N_("use <name> instead of 'origin' to track upstream")),
     - 	OPT_STRING('b', "branch", &option_branch, N_("branch"),
     --		   N_("checkout <branch> instead of the remote's HEAD")),
     -+			N_("checkout <branch> instead of the remote's HEAD")),
     - 	OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
     --		   N_("path to git-upload-pack on the remote")),
     -+			N_("path to git-upload-pack on the remote")),
     - 	OPT_STRING(0, "depth", &option_depth, N_("depth"),
     --		    N_("create a shallow clone of that depth")),
     -+			N_("create a shallow clone of that depth")),
     - 	OPT_STRING(0, "shallow-since", &option_since, N_("time"),
     --		    N_("create a shallow clone since a specific time")),
     -+			N_("create a shallow clone since a specific time")),
     - 	OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
     - 			N_("deepen history of shallow clone, excluding rev")),
     - 	OPT_BOOL(0, "single-branch", &option_single_branch,
     --		    N_("clone only one branch, HEAD or --branch")),
     -+			N_("clone only one branch, HEAD or --branch")),
     - 	OPT_BOOL(0, "no-tags", &option_no_tags,
     --		 N_("don't clone any tags, and make later fetches not to follow them")),
     -+			N_("don't clone any tags, and make later fetches not to follow them")),
     - 	OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
     --		    N_("any cloned submodules will be shallow")),
     -+			N_("any cloned submodules will be shallow")),
     - 	OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
     --		   N_("separate git dir from working tree")),
     -+			N_("separate git dir from working tree")),
     - 	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
     - 			N_("set config inside the new repository")),
     - 	OPT_STRING_LIST(0, "server-option", &server_options,
     -@@ builtin/clone.c: static struct option builtin_clone_options[] = {
     - 			TRANSPORT_FAMILY_IPV6),
     - 	OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
     - 	OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
     --		    N_("any cloned submodules will use their remote-tracking branch")),
     -+			N_("any cloned submodules will use their remote-tracking branch")),
     - 	OPT_BOOL(0, "sparse", &option_sparse_checkout,
     --		    N_("initialize sparse-checkout file to include only files at root")),
     -+			N_("initialize sparse-checkout file to include only files at root")),
     - 	OPT_END()
     - };
     - 
     -@@ builtin/clone.c: static int checkout(int submodule_progress)
     - 	err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
     - 			   oid_to_hex(&oid), "1", NULL);
     - 
     --	if (!err && (option_recurse_submodules.nr > 0)) {
     -+	if (!err && (option_recurse_submodules.option.nr > 0)) {
     - 		struct argv_array args = ARGV_ARRAY_INIT;
     - 		argv_array_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
       
      @@ builtin/clone.c: static int path_exists(const char *path)
       	return !stat(path, &sb);
     @@ builtin/clone.c: static int path_exists(const char *path)
       
      +static int git_clone_config(const char *var, const char *value, void *cb)
      +{
     -+	if (!strcmp(var, "submodule.recurse")){
     -+		if (git_config_bool(var, value) && option_recurse_submodules.status != RECURSE_SUBMODULES_OFF)
     -+			string_list_append(&option_recurse_submodules.option, ".");
     -+		return 0;
     -+	}
     ++	if (starts_with(var, "submodule."))
     ++		return git_default_submodule_config(var, value, NULL);
      +	return git_default_config(var, value, cb);
      +}
      +
     @@ builtin/clone.c: static int path_exists(const char *path)
       {
       	int is_bundle = 0, is_local;
      @@ builtin/clone.c: int cmd_clone(int argc, const char **argv, const char *prefix)
     - 			fprintf(stderr, _("Cloning into '%s'...\n"), dir);
     - 	}
     - 
     --	if (option_recurse_submodules.nr > 0) {
     -+	if (option_recurse_submodules.option.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);
     -+		string_list_sort(&option_recurse_submodules.option);
     -+		string_list_remove_duplicates(&option_recurse_submodules.option, 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) {
     -+		for_each_string_list_item(item, &option_recurse_submodules.option) {
     - 			strbuf_addf(&sb, "submodule.active=%s",
     - 				    item->string);
     - 			string_list_append(&option_config,
     -@@ builtin/clone.c: int cmd_clone(int argc, const char **argv, const char *prefix)
       
       	write_config(&option_config);
       
      -	git_config(git_default_config, NULL);
      +	git_config(git_clone_config, NULL);
     ++	if (!recurse_submodules_option_given && should_update_submodules())
     ++		string_list_append(&option_recurse_submodules, ".");
       
       	if (option_bare) {
       		if (option_mirror)


 builtin/clone.c              | 16 ++++++++++++++--
 t/t7407-submodule-foreach.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 0fc89ae2b9c..163803d89e5 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -32,6 +32,7 @@
 #include "connected.h"
 #include "packfile.h"
 #include "list-objects-filter-options.h"
+#include "submodule.h"
 
 /*
  * Overall FIXMEs:
@@ -71,6 +72,8 @@ static struct list_objects_filter_options filter_options;
 static struct string_list server_options = STRING_LIST_INIT_NODUP;
 static int option_remote_submodules;
 
+static int recurse_submodules_option_given;
+
 static int recurse_submodules_cb(const struct option *opt,
 				 const char *arg, int unset)
 {
@@ -81,7 +84,7 @@ static int recurse_submodules_cb(const struct option *opt,
 	else
 		string_list_append((struct string_list *)opt->value,
 				   (const char *)opt->defval);
-
+	recurse_submodules_option_given = 1;
 	return 0;
 }
 
@@ -929,6 +932,13 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+static int git_clone_config(const char *var, const char *value, void *cb)
+{
+	if (starts_with(var, "submodule."))
+		return git_default_submodule_config(var, value, NULL);
+	return git_default_config(var, value, cb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1103,7 +1113,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
 	write_config(&option_config);
 
-	git_config(git_default_config, NULL);
+	git_config(git_clone_config, NULL);
+	if (!recurse_submodules_option_given && should_update_submodules())
+		string_list_append(&option_recurse_submodules, ".");
 
 	if (option_bare) {
 		if (option_mirror)
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 6b2aa917e11..e6667d8e9b0 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -383,6 +383,36 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
 		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 	)
 '
+test_expect_success 'use "git clone" with submodule.recurse=true to checkout all submodules' '
+	git clone -c submodule.recurse=true super clone7 &&
+	(
+		git -C clone7 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone7/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'use "git clone" with submodule.recurse=false and --recurse-submodules to checkout all submodules' '
+	git clone -c submodule.recurse=false --recurse-submodules super clone8 &&
+	(
+		git -C clone8 rev-parse --resolve-git-dir .git --resolve-git-dir nested1/nested2/nested3/submodule/.git >actual &&
+		cat >expect <<-EOF &&
+		.git
+		$(pwd)/clone8/.git/modules/nested1/modules/nested2/modules/nested3/modules/submodule
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'use "git clone" with submodule.recurse=true and --no-recurse-submodules to checkout no submodules' '
+	git clone -c submodule.recurse=true --no-recurse-submodules super clone9 &&
+	(
+		test_path_is_missing clone9/nested1/nested2/nested3/submodule
+	)
+'
 
 test_expect_success 'command passed to foreach retains notion of stdin' '
 	(

base-commit: d0654dc308b0ba76dd8ed7bbb33c8d8f7aacd783
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-05-01 13:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-31 21:11 [PATCH] clone: use submodules.recurse option for automatically clone submodules Markus Klein via GitGitGadget
2020-02-01 21:19 ` Johannes Schindelin
2020-02-04 21:32 ` [PATCH v2] " Markus Klein via GitGitGadget
2020-02-05 10:37   ` Johannes Schindelin
2020-02-06 19:03   ` Junio C Hamano
2020-02-07 15:45     ` Johannes Schindelin
2020-02-07 18:17       ` Junio C Hamano
2020-02-07 18:33     ` Junio C Hamano
2020-02-24 22:19     ` AW: " masmiseim
2020-02-24 23:06   ` [PATCH v3] clone: use submodule.recurse " Markus Klein via GitGitGadget
2020-05-01 13:54     ` [PATCH v4] " Markus Klein via GitGitGadget

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.