Subject: [PATCH 3/6] config.c: add repo_config_set_worktree_gently()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 27 Dec 2018 16:56:08 +0100

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-masked]>
---
 config.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 config.h |  3 +++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/config.c b/config.c
index 79fbe65da8..151d28664e 100644
--- a/config.c
+++ b/config.c
@@ -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:
diff --git a/config.h b/config.h
index ee5d3fa7b4..62204dc252 100644
--- a/config.h
+++ b/config.h
@@ -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);
-- 
2.20.0.482.g66447595a7