git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 3/3] submodule--helper: introduce add-config subcommand

From
Rafael Silva <rafaeloliveira.cs@gmail.com>
Date
Jun 14, 2021, 19:51 UTC
Message-ID
<gohp6kh7i0xm08.fsf@cpm12071.fritz.box>
In-Reply-To
<20210614125157.99426-4-raykar.ath@gmail.com>
Atharva Raykar <raykar.ath@gmail.com> writes:
Show 5 quoted lines
> ---
>  builtin/submodule--helper.c | 119 ++++++++++++++++++++++++++++++++++++
>  git-submodule.sh            |  28 +--------
>  2 files changed, 120 insertions(+), 27 deletions(-)
>

I do not have enough expertise to judge the entire content of this patch. I would like, however, to propose a slight code change for the sake of readability.

Show 32 quoted lines
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 6dffaeb6cb..c4b2aa6537 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -2935,6 +2935,124 @@ static int add_clone(int argc, const char **argv, const char *prefix)
>  	return 0;
>  }
>  
> +static void configure_added_submodule(struct add_data *add_data)
> +{
> +	char *key, *submod_pathspec = NULL;
> +	struct child_process add_submod = CHILD_PROCESS_INIT;
> +	struct child_process add_gitmodules = CHILD_PROCESS_INIT;
> +	int pathspec_key_exists, activate = 0;
> +
> +	key = xstrfmt("submodule.%s.url", add_data->sm_name);
> +	git_config_set_gently(key, add_data->realrepo);
> +	free(key);
> +
> +	add_submod.git_cmd = 1;
> +	strvec_pushl(&add_submod.args, "add",
> +		     "--no-warn-embedded-repo", NULL);
> +	if (add_data->force)
> +		strvec_push(&add_submod.args, "--force");
> +	strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
> +
> +	if (run_command(&add_submod))
> +		die(_("Failed to add submodule '%s'"), add_data->sm_path);
> +
> +	key = xstrfmt("submodule.%s.path", add_data->sm_name);
> +	config_set_in_gitmodules_file_gently(key, add_data->sm_path);
> +	free(key);

This above three lines of code is very similar to the two operations that follows (including the one inside the `if (add_data->branch)` condition. So [ ... ]

Show 9 quoted lines
> +	key = xstrfmt("submodule.%s.url", add_data->sm_name);
> +	config_set_in_gitmodules_file_gently(key, add_data->repo);
> +	free(key);
> +	if (add_data->branch) {
> +		key = xstrfmt("submodule.%s.branch", add_data->sm_path);
> +		config_set_in_gitmodules_file_gently(key, add_data->branch);
> +		free(key);
> +	}
> +

[ ... ] it might be worth to write a small wrapper that will perform: (1) `xstrfmt()` on the specified config section, (2) set the configuration in the file and (3) free()'ing the variable inside the wrapper. Thus, most of these code will become one liners that is easier to read (given the function is properly named :) ).

After abstracting the code on the wrapper, this code will become something like:

    function_properly_named("submodule.%s.path", add_data->sm_name, add_data->sm_path);
    function_properly_named("submodule.%s.url", add_data->sm_name, add_data->repo);
    if (add_data->branch)
         function_properly_named("submodule.%s.branch", add_data->sm_path, add_data->branch);
Just as an example, here's a diff to demonstrate the argument:
-- >8 --
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 65f79fbd53..48ea909f51 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2934,6 +2934,14 @@ static int add_clone(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+void add_config_in_submodules_file(const char *keyfmt, const char *submodule,
+			  const char *value)
+{
+	char *key = xstrfmt(keyfmt, submodule);
+	config_set_in_gitmodules_file_gently(key, value);
+	free(key);
+}
+
 static void configure_added_submodule(struct add_data *add_data)
 {
 	char *key, *submod_pathspec = NULL;
@@ -2955,17 +2963,10 @@ static void configure_added_submodule(struct add_data *add_data)
 	if (run_command(&add_submod))
 		die(_("Failed to add submodule '%s'"), add_data->sm_path);
 
-	key = xstrfmt("submodule.%s.path", add_data->sm_name);
-	config_set_in_gitmodules_file_gently(key, add_data->sm_path);
-	free(key);
-	key = xstrfmt("submodule.%s.url", add_data->sm_name);
-	config_set_in_gitmodules_file_gently(key, add_data->repo);
-	free(key);
-	if (add_data->branch) {
-		key = xstrfmt("submodule.%s.branch", add_data->sm_path);
-		config_set_in_gitmodules_file_gently(key, add_data->branch);
-		free(key);
-	}
+	add_config_in_submodules_file("submodule.%s.path", add_data->sm_name, add_data->sm_path);
+	add_config_in_submodules_file("submodule.%s.url", add_data->sm_name, add_data->repo);
+	if (add_data->branch)
+		add_config_in_submodules_file("submodule.%s.branch", add_data->sm_path, add_data->branch);
 
 	add_gitmodules.git_cmd = 1;
 	strvec_pushl(&add_gitmodules.args,

-- >8 --

A proper name than "add_config_in_submodules_file" should be considered - I'm
not very good in naming things.

These change does (should) not change the behavior of code, even though
I believe it make the code simpler to read, I do not have strong
opinions about it. So, take this proposal as you wish. 
-- 
Thanks
Rafael
Previous: Atharva RaykarNext: Eric Sunshine
Message 28 of 40 in “submodule--helper: introduce subcommands for sh to C conversion”
  1. Atharva RaykarJun 5, 2021
  2. [GSoC] [PATCH 2/2] submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 5, 2021
  3. Christian CouderJun 7, 2021
  4. Atharva RaykarJun 7, 2021
  5. [GSoC] [PATCH 1/2] submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 5, 2021
  6. Bagas SanjayaJun 6, 2021
  7. Christian CouderJun 6, 2021
  8. [GSoC] [PATCH v2 0/2] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 8, 2021
  9. [GSoC] [PATCH v2 1/2] submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 8, 2021
  10. Đoàn Trần Công DanhJun 8, 2021
  11. Atharva RaykarJun 9, 2021
  12. Đoàn Trần Công DanhJun 9, 2021
  13. Atharva RaykarJun 9, 2021
  14. Junio C HamanoJun 9, 2021
  15. Atharva RaykarJun 9, 2021
  16. [GSoC] [PATCH v2 2/2] submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 8, 2021
  17. [GSoC] [PATCH v3 0/2] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 10, 2021
  18. 1/2 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 10, 2021
  19. Junio C HamanoJun 11, 2021
  20. Atharva RaykarJun 11, 2021
  21. Junio C HamanoJun 11, 2021
  22. 2/2 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 10, 2021
  23. [GSoC] [PATCH v4 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 14, 2021
  24. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 14, 2021
  25. Junio C HamanoJun 15, 2021
  26. Atharva RaykarJun 15, 2021
  27. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 14, 2021
  28. Rafael SilvaJun 14, 2021
  29. Eric SunshineJun 14, 2021
  30. Rafael SilvaJun 15, 2021
  31. Atharva RaykarJun 15, 2021
  32. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 14, 2021
  33. [GSoC] [PATCH v5 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 15, 2021
  34. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 15, 2021
  35. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 15, 2021
  36. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 15, 2021
  37. [GSoC] [PATCH v6 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 15, 2021
  38. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 15, 2021
  39. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 15, 2021
  40. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 15, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.