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

Re: [PATCH v4 3/9] config API: add and use a "git_config_get()" family of functions

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 2, 2023, 23:56 UTC
Message-ID
<xmqqv8kjpqoe.fsf@gitster.g>
In-Reply-To
<patch-v4-3.9-998b11ae4bc-20230202T131155Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
Show 12 quoted lines
> We already have the basic "git_config_get_value()" function and its
> "repo_*" and "configset" siblings to get a given "key" and assign the
> last key found to a provided "value".
>
> But some callers don't care about that value, but just want to use the
> return value of the "get_value()" function to check whether the key
> exist (or another non-zero return value).
>
> The immediate motivation for this is that a subsequent commit will
> need to change all callers of the "*_get_value_multi()" family of
> functions. In two cases here we (ab)used it to check whether we had
> any values for the given key, but didn't care about the return value.
So, the idea is that 
	if (!git_config_get_string(key, &discard))
		free(discard);
	else
		... the key is missing ...
becomes
	if (git_config_get(key))
		... the key is missing ...

In other words, git_config_get() returns 0 only when the key is used, and non-zero return signals that the key is not used?

Similarly, get_value_multi() was an interface to get to the value_list associated to the given key, and was abused like

	if (git_config_get_value_multi(key))
		... the key exists ...
which will become
	if (!git_config_get(key))
		... the key exists ...
right?
Show 6 quoted lines
>  	/* Set maintenance strategy, if unset */
> -	if (!git_config_get_string("maintenance.strategy", &config_value))
> -		free(config_value);
> -	else
> +	if (git_config_get("maintenance.strategy"))
>  		git_config_set("maintenance.strategy", "incremental");
OK.  config_get() says "true" meaning the key is missing.
> -	if (!argc && git_config_get_value_multi("submodule.active"))
> +	if (!argc && !git_config_get("submodule.active"))
>  		module_list_active(&list);
OK.
Show 7 quoted lines
> @@ -2743,7 +2743,7 @@ static int module_update(int argc, const char **argv, const char *prefix)
>  		 * If there are no path args and submodule.active is set then,
>  		 * by default, only initialize 'active' modules.
>  		 */
> -		if (!argc && git_config_get_value_multi("submodule.active"))
> +		if (!argc && !git_config_get("submodule.active"))
>  			module_list_active(&list);
OK.
Show 6 quoted lines
> @@ -3185,7 +3184,7 @@ static void configure_added_submodule(struct add_data *add_data)
>  	 * is_submodule_active(), since that function needs to find
>  	 * out the value of "submodule.active" again anyway.
>  	 */
> -	if (!git_config_get_string_tmp("submodule.active", &val)) {
> +	if (!git_config_get("submodule.active")) {

string_tmp() variant is to retrieve borrowed value, and it returns 0 when there is a value. If it is a valueless true, we get -1 back with an error message. What does the updated version do in the valueless true case?

Show 10 quoted lines
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index f51c40f1e1e..6ba42d4ad20 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -338,7 +337,7 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
>  				to_file, "core.bare", NULL, "true", 0))
>  			error(_("failed to unset '%s' in '%s'"),
>  				"core.bare", to_file);
> -		if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) &&
> +		if (!git_configset_get(&cs, "core.worktree") &&
OK.
Show 34 quoted lines
> diff --git a/config.c b/config.c
> index 00090a32fc3..b88da70c664 100644
> --- a/config.c
> +++ b/config.c
> @@ -2289,23 +2289,28 @@ void read_very_early_config(config_fn_t cb, void *data)
>  	config_with_options(cb, data, NULL, &opts);
>  }
>  
> -static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
> +static int configset_find_element(struct config_set *cs, const char *key,
> +				  struct config_set_element **dest)
>  {
>  	struct config_set_element k;
>  	struct config_set_element *found_entry;
>  	char *normalized_key;
> +	int ret;
> +
>  	/*
>  	 * `key` may come from the user, so normalize it before using it
>  	 * for querying entries from the hashmap.
>  	 */
> -	if (git_config_parse_key(key, &normalized_key, NULL))
> -		return NULL;
> +	ret = git_config_parse_key(key, &normalized_key, NULL);
> +	if (ret)
> +		return ret;
>  
>  	hashmap_entry_init(&k.ent, strhash(normalized_key));
>  	k.key = normalized_key;
>  	found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL);
>  	free(normalized_key);
> -	return found_entry;
> +	*dest = found_entry;
> +	return 0;

OK, so we used to return NULL when the key is not parseable, and otherwise we returned the config_set_element we found for the key, or NULL if there is no such element. Now we return error code as the return value and allow the caller to peek the element via *dest parameter.

So, from the caller's point of view (dest != NULL) is how it checks if the key is used. The function returning 0 is a sign that the key passed to it is healthy. OK.

Show 10 quoted lines
> @@ -2314,8 +2319,11 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
>  	struct string_list_item *si;
>  	struct configset_list_item *l_item;
>  	struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
> +	int ret;
>  
> -	e = configset_find_element(cs, key);
> +	ret = configset_find_element(cs, key, &e);
> +	if (ret)
> +		return ret;

The function never returned any meaningful error, so the callers may not be prepared to see such an error return. But now we at least notice an error at this level.

Show 14 quoted lines
> @@ -2425,8 +2433,25 @@ int git_configset_get_value(struct config_set *cs, const char *key, const char *
>  
>  const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
>  {
> -	struct config_set_element *e = configset_find_element(cs, key);
> -	return e ? &e->value_list : NULL;
> +	struct config_set_element *e;
> +
> +	if (configset_find_element(cs, key, &e))
> +		return NULL;
> +	else if (!e)
> +		return NULL;
> +	return &e->value_list;
> +}

OK. !e means "we got a healthy key and peeking into the hash table, there wasn't any entry for the key", and that is reported with NULL. Do we evern return a string list with .nr == 0, I wonder. Having to deal with such a list would make the caller's job more complex, but perhaps we are not allowing the code to shrink value_list.nr to avoid such a situation?

Show 11 quoted lines
> +int git_configset_get(struct config_set *cs, const char *key)
> +{
> +	struct config_set_element *e;
> +	int ret;
> +
> +	if ((ret = configset_find_element(cs, key, &e)))
> +		return ret;
> +	else if (!e)
> +		return 1;
> +	return 0;
>  }

OK. So 0 return from the function means there is a value (or more) for a given key. Good.

Show 11 quoted lines
> diff --git a/config.h b/config.h
> index ef9eade6414..04c5e594015 100644
> --- a/config.h
> +++ b/config.h
> @@ -471,9 +471,12 @@ void git_configset_clear(struct config_set *cs);
>  
>  /*
>   * These functions return 1 if not found, and 0 if found, leaving the found
> - * value in the 'dest' pointer.
> + * value in the 'dest' pointer (if any).
>   */

Now the returned non-zero values are no longer 1 alone, no? Whatever lower-level functions use to signal an error is propagated up with the

	if ((ret = func())
		return ret;
pattern.
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 66 of 134 in “config API: make "multi" safe, fix numerous segfaults”
  1. 00/10 config API: make "multi" safe, fix numerous segfaultsÆvar Arnfjörð Bjarmason, Oct 26, 2022
  2. 01/10 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Oct 26, 2022
  3. SZEDER GáborOct 26, 2022
  4. Ævar Arnfjörð BjarmasonOct 26, 2022
  5. Junio C HamanoOct 27, 2022
  6. 02/10 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Oct 26, 2022
  7. 03/10 config API: mark *_multi() with RESULT_MUST_BE_USEDÆvar Arnfjörð Bjarmason, Oct 26, 2022
  8. 04/10 string-list API: mark "struct_string_list" to "for_each_string_list" constÆvar Arnfjörð Bjarmason, Oct 26, 2022
  9. Junio C HamanoOct 27, 2022
  10. Ævar Arnfjörð BjarmasonOct 27, 2022
  11. 05/10 string-list API: make has_string() and list_lookup() "const"Ævar Arnfjörð Bjarmason, Oct 26, 2022
  12. 06/10 builtin/gc.c: use "unsorted_string_list_has_string()" where appropriateÆvar Arnfjörð Bjarmason, Oct 26, 2022
  13. Junio C HamanoOct 27, 2022
  14. Ævar Arnfjörð BjarmasonOct 27, 2022
  15. 08/10 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Oct 26, 2022
  16. Junio C HamanoOct 27, 2022
  17. 07/10 config API: add and use "lookup_value" functionsÆvar Arnfjörð Bjarmason, Oct 26, 2022
  18. Junio C HamanoOct 27, 2022
  19. 10/10 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Oct 26, 2022
  20. 09/10 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Oct 26, 2022
  21. Junio C HamanoOct 27, 2022
  22. Junio C HamanoOct 27, 2022
  23. Ævar Arnfjörð BjarmasonOct 27, 2022
  24. Junio C HamanoOct 28, 2022
  25. Ævar Arnfjörð BjarmasonOct 31, 2022
  26. Junio C HamanoOct 27, 2022
  27. 0/9 config API: make "multi" safe, fix numerous segfaultsÆvar Arnfjörð Bjarmason, Nov 1, 2022
  28. 1/9 for-each-repo tests: test bad --config keysÆvar Arnfjörð Bjarmason, Nov 1, 2022
  29. 2/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Nov 1, 2022
  30. 3/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Nov 1, 2022
  31. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Nov 1, 2022
  32. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Nov 1, 2022
  33. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Nov 1, 2022
  34. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Nov 1, 2022
  35. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Nov 1, 2022
  36. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Nov 1, 2022
  37. Taylor BlauNov 2, 2022
  38. 0/9 config API: make "multi" safe, fix numerous segfaultsÆvar Arnfjörð Bjarmason, Nov 25, 2022
  39. 1/9 for-each-repo tests: test bad --config keysÆvar Arnfjörð Bjarmason, Nov 25, 2022
  40. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Nov 25, 2022
  41. 3/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Nov 25, 2022
  42. Glen ChooJan 19, 2023
  43. 2/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Nov 25, 2022
  44. Glen ChooJan 19, 2023
  45. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Nov 25, 2022
  46. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Nov 25, 2022
  47. Glen ChooJan 19, 2023
  48. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Nov 25, 2022
  49. Glen ChooJan 19, 2023
  50. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Nov 25, 2022
  51. Glen ChooJan 19, 2023
  52. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Nov 25, 2022
  53. Glen ChooJan 19, 2023
  54. 0/9 config API: make "multi" safe, fix numerous segfaultsÆvar Arnfjörð Bjarmason, Feb 2, 2023
  55. 1/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Feb 2, 2023
  56. Junio C HamanoFeb 3, 2023
  57. Glen ChooFeb 6, 2023
  58. 2/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Feb 2, 2023
  59. Junio C HamanoFeb 2, 2023
  60. Glen ChooFeb 6, 2023
  61. Ævar Arnfjörð BjarmasonFeb 6, 2023
  62. Glen ChooFeb 6, 2023
  63. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Feb 2, 2023
  64. Junio C HamanoFeb 3, 2023
  65. 3/9 config API: add and use a "git_config_get()" family of functionsÆvar Arnfjörð Bjarmason, Feb 2, 2023
  66. Junio C HamanoFeb 2, 2023
  67. Ævar Arnfjörð BjarmasonFeb 7, 2023
  68. Glen ChooFeb 6, 2023
  69. Glen ChooFeb 6, 2023
  70. Ævar Arnfjörð BjarmasonFeb 7, 2023
  71. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Feb 2, 2023
  72. Glen ChooFeb 6, 2023
  73. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Feb 2, 2023
  74. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Feb 2, 2023
  75. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Feb 2, 2023
  76. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Feb 2, 2023
  77. Glen ChooFeb 6, 2023
  78. 00/10 config API: make "multi" safe, fix segfaults, propagate "ret"Ævar Arnfjörð Bjarmason, Feb 7, 2023
  79. 01/10 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Feb 7, 2023
  80. 02/10 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Feb 7, 2023
  81. Glen ChooFeb 9, 2023
  82. 04/10 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Feb 7, 2023
  83. 03/10 config API: add and use a "git_config_get()" family of functionsÆvar Arnfjörð Bjarmason, Feb 7, 2023
  84. Glen ChooFeb 9, 2023
  85. Ævar Arnfjörð BjarmasonFeb 9, 2023
  86. Ævar Arnfjörð BjarmasonFeb 9, 2023
  87. Glen ChooFeb 9, 2023
  88. 05/10 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Feb 7, 2023
  89. 06/10 config API: don't lose the git_*get*() return valuesÆvar Arnfjörð Bjarmason, Feb 7, 2023
  90. 07/10 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Feb 7, 2023
  91. 08/10 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Feb 7, 2023
  92. 09/10 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Feb 7, 2023
  93. 10/10 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Feb 7, 2023
  94. Junio C HamanoFeb 7, 2023
  95. 0/9 config API: make "multi" safe, fix segfaults, propagate "ret"Ævar Arnfjörð Bjarmason, Mar 7, 2023
  96. 1/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Mar 7, 2023
  97. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Mar 7, 2023
  98. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Mar 7, 2023
  99. 3/9 config API: add and use a "git_config_get()" family of functionsÆvar Arnfjörð Bjarmason, Mar 7, 2023
  100. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Mar 7, 2023
  101. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Mar 7, 2023
  102. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Mar 7, 2023
  103. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Mar 7, 2023
  104. 2/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Mar 7, 2023
  105. Glen ChooMar 8, 2023
  106. 0/9 config API: make "multi" safe, fix segfaults, propagate "ret"Ævar Arnfjörð Bjarmason, Mar 8, 2023
  107. 1/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Mar 8, 2023
  108. 2/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Mar 8, 2023
  109. 3/9 config API: add and use a "git_config_get()" family of functionsÆvar Arnfjörð Bjarmason, Mar 8, 2023
  110. Glen ChooMar 9, 2023
  111. Ævar Arnfjörð BjarmasonMar 14, 2023
  112. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Mar 8, 2023
  113. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Mar 8, 2023
  114. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Mar 8, 2023
  115. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Mar 8, 2023
  116. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Mar 8, 2023
  117. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Mar 8, 2023
  118. Glen ChooMar 9, 2023
  119. Glen ChooMar 9, 2023
  120. Junio C HamanoMar 9, 2023
  121. 0/9 config API: make "multi" safe, fix segfaults, propagate "ret"Ævar Arnfjörð Bjarmason, Mar 28, 2023
  122. 1/9 config tests: cover blind spots in git_die_config() testsÆvar Arnfjörð Bjarmason, Mar 28, 2023
  123. 2/9 config tests: add "NULL" tests for *_get_value_multi()Ævar Arnfjörð Bjarmason, Mar 28, 2023
  124. 3/9 config API: add and use a "git_config_get()" family of functionsÆvar Arnfjörð Bjarmason, Mar 28, 2023
  125. 4/9 versioncmp.c: refactor config reading next commitÆvar Arnfjörð Bjarmason, Mar 28, 2023
  126. 5/9 config API: have *_multi() return an "int" and take a "dest"Ævar Arnfjörð Bjarmason, Mar 28, 2023
  127. 6/9 for-each-repo: error on bad --configÆvar Arnfjörð Bjarmason, Mar 28, 2023
  128. 8/9 config API: add "string" version of *_value_multi(), fix segfaultsÆvar Arnfjörð Bjarmason, Mar 28, 2023
  129. 7/9 config API users: test for *_get_value_multi() segfaultsÆvar Arnfjörð Bjarmason, Mar 28, 2023
  130. 9/9 for-each-repo: with bad config, don't conflate <path> and <cmd>Ævar Arnfjörð Bjarmason, Mar 28, 2023
  131. SZEDER GáborApr 7, 2023
  132. Glen ChooMar 28, 2023
  133. Junio C HamanoMar 28, 2023
  134. Junio C HamanoMar 29, 2023

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.