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

Re: [PATCH v5 2/4] submodule--helper: introduce for_each_listed_submodule()

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 25, 2017, 03:43 UTC
Message-ID
<xmqqmv5jfnod.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170924120858.26813-3-pc44800@gmail.com>
Prathamesh Chavan <pc44800@gmail.com> writes:
Show 12 quoted lines
> Introduce function for_each_listed_submodule() and replace a loop
> in module_init() with a call to it.
>
> The new function will also be used in other parts of the
> system in later patches.
>
> Mentored-by: Christian Couder <christian.couder@gmail.com>
> Mentored-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com>
> ---
>  builtin/submodule--helper.c | 36 ++++++++++++++++++++++++++++--------
>  1 file changed, 28 insertions(+), 8 deletions(-)

This looks more or less perfectly done, but I say this basing it on an assumption that nobody ever would want to initialize a single submodule without going through module_init() interface.

If there will be a caller that would have made a direct call to init_submodule() if this patch did not exist, then I think this patch is better done by keeping the external interface to the init_submodule() function intact, and introducing an intermediate helper init_submodule_cb() function that is to be used as the callback used in for_each_listed_submodule() API. In general, we should make sure that these callback functions that take "void *cb_data" parameters are called _only_ by these iterators that defined the callback (in this case, for_each_listed_submodule()) and not other functions.

Show 91 quoted lines
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index d24ac9028..d12790b5c 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -14,6 +14,9 @@
>  #include "refs.h"
>  #include "connect.h"
>  
> +typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
> +				  void *cb_data);
> +
>  static char *get_default_remote(void)
>  {
>  	char *dest = NULL, *ret;
> @@ -352,15 +355,30 @@ static int module_list(int argc, const char **argv, const char *prefix)
>  	return 0;
>  }
>  
> -static void init_submodule(const char *path, const char *prefix, int quiet)
> +static void for_each_listed_submodule(const struct module_list *list,
> +				      each_submodule_fn fn, void *cb_data)
> +{
> +	int i;
> +	for (i = 0; i < list->nr; i++)
> +		fn(list->entries[i], cb_data);
> +}
> +
> +struct init_cb {
> +	const char *prefix;
> +	unsigned int quiet: 1;
> +};
> +#define INIT_CB_INIT { NULL, 0 }
> +
> +static void init_submodule(const struct cache_entry *list_item, void *cb_data)
>  {
> +	struct init_cb *info = cb_data;
>  	const struct submodule *sub;
>  	struct strbuf sb = STRBUF_INIT;
>  	char *upd = NULL, *url = NULL, *displaypath;
>  
> -	displaypath = get_submodule_displaypath(path, prefix);
> +	displaypath = get_submodule_displaypath(list_item->name, info->prefix);
>  
> -	sub = submodule_from_path(&null_oid, path);
> +	sub = submodule_from_path(&null_oid, list_item->name);
>  
>  	if (!sub)
>  		die(_("No url found for submodule path '%s' in .gitmodules"),
> @@ -372,7 +390,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
>  	 *
>  	 * Set active flag for the submodule being initialized
>  	 */
> -	if (!is_submodule_active(the_repository, path)) {
> +	if (!is_submodule_active(the_repository, list_item->name)) {
>  		strbuf_addf(&sb, "submodule.%s.active", sub->name);
>  		git_config_set_gently(sb.buf, "true");
>  		strbuf_reset(&sb);
> @@ -414,7 +432,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
>  		if (git_config_set_gently(sb.buf, url))
>  			die(_("Failed to register url for submodule path '%s'"),
>  			    displaypath);
> -		if (!quiet)
> +		if (!info->quiet)
>  			fprintf(stderr,
>  				_("Submodule '%s' (%s) registered for path '%s'\n"),
>  				sub->name, url, displaypath);
> @@ -443,10 +461,10 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
>  
>  static int module_init(int argc, const char **argv, const char *prefix)
>  {
> +	struct init_cb info = INIT_CB_INIT;
>  	struct pathspec pathspec;
>  	struct module_list list = MODULE_LIST_INIT;
>  	int quiet = 0;
> -	int i;
>  
>  	struct option module_init_options[] = {
>  		OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
> @@ -471,8 +489,10 @@ static int module_init(int argc, const char **argv, const char *prefix)
>  	if (!argc && git_config_get_value_multi("submodule.active"))
>  		module_list_active(&list);
>  
> -	for (i = 0; i < list.nr; i++)
> -		init_submodule(list.entries[i]->name, prefix, quiet);
> +	info.prefix = prefix;
> +	info.quiet = !!quiet;
> +
> +	for_each_listed_submodule(&list, init_submodule, &info);
>  
>  	return 0;
>  }
Previous: Prathamesh ChavanNext: Prathamesh Chavan
Message 36 of 59 in “submodule--helper: introduce get_submodule_displaypath()”
  1. Prathamesh ChavanAug 21, 2017
  2. [GSoC][PATCH 2/4] submodule--helper: introduce for_each_submodule_list()Prathamesh Chavan, Aug 21, 2017
  3. Junio C HamanoAug 22, 2017
  4. [GSoC][PATCH 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 21, 2017
  5. Heiko VoigtAug 21, 2017
  6. Prathamesh ChavanAug 21, 2017
  7. [GSoC][PATCH 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 21, 2017
  8. Junio C HamanoAug 22, 2017
  9. [GSoC][PATCH v2 0/4] submodule: Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 23, 2017
  10. [GSoC][PATCH v2 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 23, 2017
  11. [GSoC][PATCH v2 2/4] submodule--helper: introduce for_each_submodule()Prathamesh Chavan, Aug 23, 2017
  12. Junio C HamanoAug 23, 2017
  13. Stefan BellerAug 23, 2017
  14. Junio C HamanoAug 23, 2017
  15. [GSoC][PATCH v3 0/4] Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 24, 2017
  16. [GSoC][PATCH v3 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 24, 2017
  17. [GSoC][PATCH v3 2/4] submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Aug 24, 2017
  18. [GSoC][PATCH v3 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 24, 2017
  19. [GSoC][PATCH v3 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 24, 2017
  20. Junio C HamanoAug 25, 2017
  21. Stefan BellerAug 25, 2017
  22. Junio C HamanoAug 25, 2017
  23. Prathamesh ChavanAug 27, 2017
  24. [GSoC][PATCH v4 0/4] Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 28, 2017
  25. [GSoC][PATCH v4 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 28, 2017
  26. [GSoC][PATCH v4 1/4] submodule--helper: introduce get_submodule_displaypath()Han-Wen Nienhuys, Sep 21, 2017
  27. [GSoC][PATCH v4 2/4] submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Aug 28, 2017
  28. [GSoC][PATCH v4 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 28, 2017
  29. [GSoC][PATCH v4 3/4] submodule: port set_name_rev() from shell to CHan-Wen Nienhuys, Sep 21, 2017
  30. [GSoC][PATCH v4 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 28, 2017
  31. [GSoC][PATCH v4 4/4] submodule: port submodule subcommand 'status' from shell to CHan-Wen Nienhuys, Sep 21, 2017
  32. 0/4 Incremental rewrite of git-submodulesPrathamesh Chavan, Sep 24, 2017
  33. 1/4 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Sep 24, 2017
  34. Junio C HamanoSep 25, 2017
  35. 2/4 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Sep 24, 2017
  36. Junio C HamanoSep 25, 2017
  37. 3/4 submodule: port set_name_rev() from shell to CPrathamesh Chavan, Sep 24, 2017
  38. Junio C HamanoSep 25, 2017
  39. Junio C HamanoSep 25, 2017
  40. 4/4 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Sep 24, 2017
  41. Junio C HamanoSep 25, 2017
  42. 0/3 Incremental rewrite of git-submodulesPrathamesh Chavan, Sep 29, 2017
  43. 1/3 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Sep 29, 2017
  44. Junio C HamanoOct 2, 2017
  45. 2/3 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Sep 29, 2017
  46. Junio C HamanoOct 2, 2017
  47. 3/3 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Sep 29, 2017
  48. Junio C HamanoOct 2, 2017
  49. 0/3 Incremental rewrite of git-submodulesPrathamesh Chavan, Oct 6, 2017
  50. 1/3 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Oct 6, 2017
  51. Eric SunshineOct 6, 2017
  52. 2/3 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Oct 6, 2017
  53. Eric SunshineOct 6, 2017
  54. 3/3 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Oct 6, 2017
  55. Junio C HamanoOct 7, 2017
  56. Eric SunshineOct 7, 2017
  57. Junio C HamanoOct 2, 2017
  58. [GSoC][PATCH v2 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 23, 2017
  59. [GSoC][PATCH v2 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 23, 2017

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.