Skip to content

Commit 8f7c7f5

Browse files
pcloudsgitster
authored andcommitted
config.c: add repo_config_set_worktree_gently()
This is C equivalent of "git config --set --worktree". In other words, it will - write to $GIT_DIR/config in single-worktree setup - write to $GIT_COMMON_DIR/worktrees/<x>/config.worktree or $GIT_COMMON_DIR/config.worktree (for main worktree) if extensions.worktreeConfig is enabled - return error in multiple-worktree setup if extensions.worktreeConfig is not enabled. While at there, also add repo_config_set*() for writing to $GIT_COMMON_DIR/config. Note, since git_config_set_multivar_in_file_gently() only invalidates the config set of the_repository, anybody who uses these functions on submodules must do additional invalidation if needed. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ec5633 commit 8f7c7f5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

config.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "utf8.h"
2020
#include "dir.h"
2121
#include "color.h"
22+
#include "worktree.h"
2223

2324
struct config_source {
2425
struct config_source *prev;
@@ -2137,6 +2138,39 @@ int repo_config_get_pathname(struct repository *repo,
21372138
return ret;
21382139
}
21392140

2141+
int repo_config_set_gently(struct repository *r,
2142+
const char *key, const char *value)
2143+
{
2144+
char *path = repo_git_path(r, "config");
2145+
int ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0);
2146+
free(path);
2147+
return ret;
2148+
}
2149+
2150+
void repo_config_set(struct repository *r, const char *key, const char *value)
2151+
{
2152+
if (!repo_config_set_gently(r, key, value))
2153+
return;
2154+
if (value)
2155+
die(_("could not set '%s' to '%s'"), key, value);
2156+
else
2157+
die(_("could not unset '%s'"), key);
2158+
}
2159+
2160+
int repo_config_set_worktree_gently(struct repository *r,
2161+
const char *key, const char *value)
2162+
{
2163+
char *path;
2164+
int ret;
2165+
2166+
path = get_worktree_config(r);
2167+
if (!path)
2168+
return CONFIG_INVALID_FILE;
2169+
ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0);
2170+
free(path);
2171+
return ret;
2172+
}
2173+
21402174
/* Functions used historically to read configuration from 'the_repository' */
21412175
void git_config(config_fn_t fn, void *data)
21422176
{
@@ -2912,7 +2946,12 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
29122946

29132947
ret = 0;
29142948

2915-
/* Invalidate the config cache */
2949+
/*
2950+
* Invalidate the config cache
2951+
*
2952+
* NEEDSWORK: invalidate _all_ existing config caches, not
2953+
* just one from the_repository
2954+
*/
29162955
git_config_clear();
29172956

29182957
out_free:

config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ extern int git_config_color(char *, const char *, const char *);
103103
extern int git_config_set_in_file_gently(const char *, const char *, const char *);
104104
extern void git_config_set_in_file(const char *, const char *, const char *);
105105
extern int git_config_set_gently(const char *, const char *);
106+
extern int repo_config_set_gently(struct repository *, const char *, const char *);
107+
extern void repo_config_set(struct repository *, const char *, const char *);
108+
extern int repo_config_set_worktree_gently(struct repository *, const char *, const char *);
106109
extern void git_config_set(const char *, const char *);
107110
extern int git_config_parse_key(const char *, char **, int *);
108111
extern int git_config_key_is_valid(const char *key);

0 commit comments

Comments
 (0)