diff mbox series

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

Message ID 20181227155611.10585-4-pclouds@gmail.com (mailing list archive)
State New, archived
Headers show
Series Add "git config --move-to" | expand

Commit Message

Duy Nguyen Dec. 27, 2018, 3:56 p.m. UTC
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 <pclouds@gmail.com>
---
 config.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 config.h |  3 +++
 2 files changed, 43 insertions(+), 1 deletion(-)

Comments

Derrick Stolee April 30, 2019, 4:40 p.m. UTC | #1
On 12/27/2018 10:56 AM, Nguyễn Thái Ngọc Duy wrote:
> 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);

I know this is an old thread, but the patch is still in 'pu'. These methods
do not appear to have any callers. Perhaps this patch should be dropped
until there is a reason to include it?

Thanks,
-Stolee
Duy Nguyen May 1, 2019, 9:55 a.m. UTC | #2
On Tue, Apr 30, 2019 at 11:40 PM Derrick Stolee <stolee@gmail.com> wrote:
>
> On 12/27/2018 10:56 AM, Nguyễn Thái Ngọc Duy wrote:
> > 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);
>
> I know this is an old thread, but the patch is still in 'pu'. These methods
> do not appear to have any callers. Perhaps this patch should be dropped
> until there is a reason to include it?

That series was broken down to test the water before the whole
sumodule support on multiple worktree series enters. But since this
series is not going anyway, don't worry I don't think it will ever
enter 'master'.

> Thanks,
> -Stolee
diff mbox series

Patch

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);