Skip to content

Commit fe18733

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 615a84a commit fe18733

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

config.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "dir.h"
2222
#include "color.h"
2323
#include "refs.h"
24+
#include "worktree.h"
2425

2526
struct config_source {
2627
struct config_source *prev;
@@ -2884,6 +2885,20 @@ int git_config_set_gently(const char *key, const char *value)
28842885
return git_config_set_multivar_gently(key, value, NULL, 0);
28852886
}
28862887

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+
28872902
void git_config_set(const char *key, const char *value)
28882903
{
28892904
git_config_set_multivar(key, value, NULL, 0);
@@ -3181,14 +3196,28 @@ void git_config_set_multivar_in_file(const char *config_filename,
31813196
int git_config_set_multivar_gently(const char *key, const char *value,
31823197
const char *value_pattern, unsigned flags)
31833198
{
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;
31863214
}
31873215

31883216
void git_config_set_multivar(const char *key, const char *value,
31893217
const char *value_pattern, unsigned flags)
31903218
{
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,
31923221
flags);
31933222
}
31943223

config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ void git_config_set_in_file(const char *, const char *, const char *);
253253

254254
int git_config_set_gently(const char *, const char *);
255255

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+
256263
/**
257264
* write config values to `.git/config`, takes a key/value pair as parameter.
258265
*/
@@ -281,6 +288,7 @@ int git_config_parse_key(const char *, char **, size_t *);
281288

282289
int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned);
283290
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);
284292
int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned);
285293

286294
/**

0 commit comments

Comments
 (0)