config: add repo_config_set_worktree_gently()

Some config settings, such as those for sparse-checkout, are likely intended to only apply to one worktree at a time. To make this write easier, add a new config API method, repo_config_set_worktree_gently(). This method will attempt to write to the worktree-specific config, but will instead write to the common config file if worktree config is not enabled. The next change will introduce a consumer of this method. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Feb 7, 2022 at 21:33 UTC fe18733927c1d28ffcec3b52908989c591bc7b87
2 files changed +40 -3
config.c
+32 -3
@@ -21,6 +21,7 @@
21 #include "dir.h"
22 #include "color.h"
23 #include "refs.h"
24 +#include "worktree.h"
25
26 struct config_source {
27 struct config_source *prev;
@@ -2884,6 +2885,20 @@ int git_config_set_gently(const char *key, const char *value)
2885 return git_config_set_multivar_gently(key, value, NULL, 0);
2886 }
2887
2888 +int repo_config_set_worktree_gently(struct repository *r,
2889 + const char *key, const char *value)
2890 +{
2891 + /* Only use worktree-specific config if it is is already enabled. */
2892 + if (repository_format_worktree_config) {
2893 + char *file = repo_git_path(r, "config.worktree");
2894 + int ret = git_config_set_multivar_in_file_gently(
2895 + file, key, value, NULL, 0);
2896 + free(file);
2897 + return ret;
2898 + }
2899 + return repo_config_set_multivar_gently(r, key, value, NULL, 0);
2900 +}
2901 +
2902 void git_config_set(const char *key, const char *value)
2903 {
2904 git_config_set_multivar(key, value, NULL, 0);
@@ -3181,14 +3196,28 @@ void git_config_set_multivar_in_file(const char *config_filename,
3196 int git_config_set_multivar_gently(const char *key, const char *value,
3197 const char *value_pattern, unsigned flags)
3198 {
3184 - return git_config_set_multivar_in_file_gently(NULL, key, value, value_pattern,
3185 - flags);
3199 + return repo_config_set_multivar_gently(the_repository, key, value,
3200 + value_pattern, flags);
3201 +}
3202 +
3203 +int repo_config_set_multivar_gently(struct repository *r, const char *key,
3204 + const char *value,
3205 + const char *value_pattern, unsigned flags)
3206 +{
3207 + char *file = repo_git_path(r, "config");
3208 + int res = git_config_set_multivar_in_file_gently(file,
3209 + key, value,
3210 + value_pattern,
3211 + flags);
3212 + free(file);
3213 + return res;
3214 }
3215
3216 void git_config_set_multivar(const char *key, const char *value,
3217 const char *value_pattern, unsigned flags)
3218 {
3191 - git_config_set_multivar_in_file(NULL, key, value, value_pattern,
3219 + git_config_set_multivar_in_file(git_path("config"),
3220 + key, value, value_pattern,
3221 flags);
3222 }
3223
config.h
+8
@@ -253,6 +253,13 @@ void git_config_set_in_file(const char *, const char *, const char *);
253
254 int git_config_set_gently(const char *, const char *);
255
256 +/**
257 + * Write a config value that should apply to the current worktree. If
258 + * extensions.worktreeConfig is enabled, then the write will happen in the
259 + * current worktree's config. Otherwise, write to the common config file.
260 + */
261 +int repo_config_set_worktree_gently(struct repository *, const char *, const char *);
262 +
263 /**
264 * write config values to `.git/config`, takes a key/value pair as parameter.
265 */
@@ -281,6 +288,7 @@ int git_config_parse_key(const char *, char **, size_t *);
288
289 int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned);
290 void git_config_set_multivar(const char *, const char *, const char *, unsigned);
291 +int repo_config_set_multivar_gently(struct repository *, const char *, const char *, const char *, unsigned);
292 int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned);
293
294 /**