Commit d26ab4c5 authored by Duy Nguyen's avatar Duy Nguyen

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.
parent b2e21eec
......@@ -19,6 +19,7 @@
#include "utf8.h"
#include "dir.h"
#include "color.h"
#include "worktree.h"
struct config_source {
struct config_source *prev;
......@@ -2137,6 +2138,39 @@ int repo_config_get_pathname(struct repository *repo,
return ret;
}
int repo_config_set_gently(struct repository *r,
const char *key, const char *value)
{
char *path = repo_git_path(r, "config");
int ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0);
free(path);
return ret;
}
void repo_config_set(struct repository *r, const char *key, const char *value)
{
if (!repo_config_set_gently(r, key, value))
return;
if (value)
die(_("could not set '%s' to '%s'"), key, value);
else
die(_("could not unset '%s'"), key);
}
int repo_config_set_worktree_gently(struct repository *r,
const char *key, const char *value)
{
char *path;
int ret;
path = get_worktree_config(r);
if (!path)
return CONFIG_INVALID_FILE;
ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0);
free(path);
return ret;
}
/* Functions used historically to read configuration from 'the_repository' */
void git_config(config_fn_t fn, void *data)
{
......@@ -2912,7 +2946,12 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
ret = 0;
/* Invalidate the config cache */
/*
* Invalidate the config cache
*
* NEEDSWORK: invalidate _all_ existing config caches, not
* just one from the_repository
*/
git_config_clear();
out_free:
......
......@@ -103,6 +103,9 @@ extern int git_config_color(char *, const char *, const char *);
extern int git_config_set_in_file_gently(const char *, const char *, const char *);
extern void git_config_set_in_file(const char *, const char *, const char *);
extern int git_config_set_gently(const char *, const char *);
extern int repo_config_set_gently(struct repository *, const char *, const char *);
extern void repo_config_set(struct repository *, const char *, const char *);
extern int repo_config_set_worktree_gently(struct repository *, const char *, const char *);
extern void git_config_set(const char *, const char *);
extern int git_config_parse_key(const char *, char **, int *);
extern int git_config_key_is_valid(const char *key);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment