Subject: [PATCH v3 5/6] config: select .git/common/config with --repo

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 26 Jan 2016 18:44:44 +0700

This new option allows the user to write to or read from
.git/common/config in worktree v1. In worktree v0, --repo is an alias
of --local.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Documentation/git-config.txt           | 14 +++++++++++++-
 Documentation/git-worktree.txt         |  4 ++++
 Documentation/gitrepository-layout.txt | 10 +++++-----
 builtin/config.c                       | 19 ++++++++++++++-----
 t/t2028-worktree-config.sh             | 23 ++++++++++++++++++++++-
 5 files changed, 58 insertions(+), 12 deletions(-)

diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 2608ca7..79fd453 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -47,7 +47,7 @@ checks or transformations are performed on the value.
 
 When reading, the values are read from the system, global and
 repository local configuration files by default, and options
-'--system', '--global', '--local' and '--file <filename>' can be
+'--system', '--global', '--repo', '--local' and '--file <filename>' can be
 used to tell the command to read from only that location (see <<FILES>>).
 
 When writing, the new value is written to the repository local
@@ -125,6 +125,18 @@ rather than from all available files.
 +
 See also <<FILES>>.
 
+--repo::
+
+	For writing options: write to the repository file
+	`.git/config` if the configuration variable extensions.worktree
+	is not specified or has the value zero, `.git/worktrees/config`
+	otherwise.
++
+For reading options: read only from the same file rather than from all
+available files.
++
+See also <<FILES>>.
+
 --local::
 	For writing options: write to the repository `.git/config` file.
 	This is	the default behavior.
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 0846f2a..6082d4d 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -119,6 +119,10 @@ In this version, the repository config file `.git/config` is not
 shared anymore. A new file, `.git/common/config`, read for all
 worktrees. Shared configuration should be stored here.
 
+Use "git config --repo" to store shared configuration variables. Use
+"git config --local" to store per-worktree ones. This works even in
+single-worktree mode.
+
 Version 0
 ~~~~~~~~~
 This is the first release. Version 0 is implied if extensions.worktree
diff --git a/Documentation/gitrepository-layout.txt b/Documentation/gitrepository-layout.txt
index d65345d..56175f0 100644
--- a/Documentation/gitrepository-layout.txt
+++ b/Documentation/gitrepository-layout.txt
@@ -254,6 +254,11 @@ common::
 	This directory is seen from all working directories. It is
 	meant to share files that all working directories can see.
 
+common/config::
+	Repository specific configuration file. Note that if this file
+	is present, it must contain the variable extensions.worktree
+	whose value must be one or above.
+
 worktrees::
 	Contains administrative data for linked
 	working trees. Each subdirectory contains the working tree-related
@@ -261,11 +266,6 @@ worktrees::
 	$GIT_COMMON_DIR is set, in which case
 	"$GIT_COMMON_DIR/worktrees" will be used instead.
 
-common/config::
-	Repository specific configuration file. Note that if this file
-	is present, it must contain the variable extensions.worktree
-	whose value must be one or above.
-
 worktrees/<id>/gitdir::
 	A text file containing the absolute path back to the .git file
 	that points to here. This is used to check if the linked
diff --git a/builtin/config.c b/builtin/config.c
index adc7727..6aecd13 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -21,7 +21,7 @@ static char delim = '=';
 static char key_delim = ' ';
 static char term = '\n';
 
-static int use_global_config, use_system_config, use_local_config;
+static int use_global_config, use_system_config, use_local_config, use_repo_config;
 static struct git_config_source given_config_source;
 static int actions, types;
 static const char *get_color_slot, *get_colorbool_slot;
@@ -54,7 +54,8 @@ static struct option builtin_config_options[] = {
 	OPT_GROUP(N_("Config file location")),
 	OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
 	OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
-	OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
+	OPT_BOOL(0, "repo", &use_repo_config, N_("use per-repository config file")),
+	OPT_BOOL(0, "local", &use_local_config, N_("use per-worktree config file")),
 	OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
 	OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
 	OPT_GROUP(N_("Action")),
@@ -460,7 +461,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
 	if (use_global_config + use_system_config + use_local_config +
-	    !!given_config_source.file + !!given_config_source.blob > 1) {
+	    !!given_config_source.file + !!given_config_source.blob > 1 +
+	    use_repo_config) {
 		error("only one config file at a time.");
 		usage_with_options(builtin_config_usage, builtin_config_options);
 	}
@@ -492,9 +494,16 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 	}
 	else if (use_system_config)
 		given_config_source.file = git_etc_gitconfig();
-	else if (use_local_config)
+	else if (use_local_config ||
+		 (use_repo_config &&
+		  repository_format_worktree_version == 0))
 		given_config_source.file = git_pathdup("config");
-	else if (given_config_source.file) {
+	else if (use_repo_config) {
+		struct strbuf sb = STRBUF_INIT;
+
+		strbuf_addf(&sb, "%s/common/config", get_git_common_dir());
+		given_config_source.file = strbuf_detach(&sb, NULL);
+	} else if (given_config_source.file) {
 		if (!is_absolute_path(given_config_source.file) && prefix)
 			given_config_source.file =
 				xstrdup(prefix_filename(prefix,
diff --git a/t/t2028-worktree-config.sh b/t/t2028-worktree-config.sh
index 5561788..d11b2ce 100755
--- a/t/t2028-worktree-config.sh
+++ b/t/t2028-worktree-config.sh
@@ -19,6 +19,15 @@ test_expect_success 'main config is shared in version 0' '
 	test_cmp expected actual
 '
 
+test_expect_success 'config --repo on v0' '
+	git config --global new.var old-value &&
+	git config --repo new.var new-value &&
+	test_path_is_missing .git/common/config &&
+	git config --repo new.var >actual &&
+	echo new-value >expected &&
+	test_cmp expected actual
+'
+
 test_expect_success 'main config is for main worktree only (v1)' '
 	mkdir .git/common &&
 	git config -f .git/common/config extensions.worktree 1 &&
@@ -28,7 +37,7 @@ test_expect_success 'main config is for main worktree only (v1)' '
 	test_must_fail git -C wt1 config wt.name
 '
 
-test_expect_success 'worktrees/config is shared (v1)' '
+test_expect_success 'common/config is shared (v1)' '
 	git config -f .git/common/config some.thing is-shared &&
 	echo is-shared >expected &&
 	git config some.thing >actual &&
@@ -37,4 +46,16 @@ test_expect_success 'worktrees/config is shared (v1)' '
 	test_cmp expected actual
 '
 
+test_expect_success 'config --repo on v1' '
+	git config --global new.var1 old-value &&
+	git config --repo new.var1 new-value &&
+	grep var1 .git/common/config >/dev/null &&
+	git config --repo new.var1 >actual &&
+	echo new-value >expected &&
+	test_cmp expected actual &&
+	git -C wt2 config --repo new.var1 >actual &&
+	echo new-value >expected &&
+	test_cmp expected actual
+'
+
 test_done
-- 
2.7.0.288.g1d8ad15