git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/6] config: factor out set_config_source_file()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 27, 2018, 15:56 UTC
Message-ID
<20181227155611.10585-6-pclouds@gmail.com>
In-Reply-To
<20181227155611.10585-1-pclouds@gmail.com>

In the next patch, "config" is taught to move some config variables from one file to another. We need to specify two locations, source and destination, and the plan is reusing the same --global/--local/... option. Factor this code out for reuse later.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/config.c | 113 +++++++++++++++++++++++++----------------------
 1 file changed, 59 insertions(+), 54 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index c22d25de12..61a6a5a0e1 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -71,6 +71,64 @@ static int show_origin;
 
 static NORETURN void usage_builtin_config(void);
 
+static void set_config_source_file(void)
+{
+	int nongit = !startup_info->have_repository;
+
+	if (use_global_config + use_system_config + use_local_config +
+	    use_worktree_config +
+	    !!given_config_source.file + !!given_config_source.blob > 1)
+		die(_("only one config file at a time"));
+
+	if (use_local_config && nongit)
+		die(_("--local can only be used inside a git repository"));
+
+	if (given_config_source.blob && nongit)
+		die(_("--blob can only be used inside a git repository"));
+
+	if (given_config_source.file &&
+			!strcmp(given_config_source.file, "-")) {
+		given_config_source.file = NULL;
+		given_config_source.use_stdin = 1;
+	}
+
+	if (use_global_config) {
+		char *user_config = expand_user_path("~/.gitconfig", 0);
+		char *xdg_config = xdg_config_home("config");
+
+		if (!user_config)
+			/*
+			 * It is unknown if HOME/.gitconfig exists, so
+			 * we do not know if we should write to XDG
+			 * location; error out even if XDG_CONFIG_HOME
+			 * is set and points at a sane location.
+			 */
+			die(_("$HOME not set"));
+
+		if (access_or_warn(user_config, R_OK, 0) &&
+		    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
+			given_config_source.file = xdg_config;
+			free(user_config);
+		} else {
+			given_config_source.file = user_config;
+			free(xdg_config);
+		}
+	}
+	else if (use_system_config)
+		given_config_source.file = git_etc_gitconfig();
+	else if (use_local_config)
+		given_config_source.file = git_pathdup("config");
+	else if (use_worktree_config) {
+		given_config_source.file = get_worktree_config(the_repository);
+		if (!given_config_source.file)
+			die(_("--worktree cannot be used with multiple "
+			      "working trees unless the config\n"
+			      "extension worktreeConfig is enabled. "
+			      "Please read \"CONFIGURATION FILE\"\n"
+			      "section in \"git help worktree\" for details"));
+	}
+}
+
 static int option_parse_type(const struct option *opt, const char *arg,
 			     int unset)
 {
@@ -604,60 +662,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			     builtin_config_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
-	if (use_global_config + use_system_config + use_local_config +
-	    use_worktree_config +
-	    !!given_config_source.file + !!given_config_source.blob > 1) {
-		error(_("only one config file at a time"));
-		usage_builtin_config();
-	}
-
-	if (use_local_config && nongit)
-		die(_("--local can only be used inside a git repository"));
-
-	if (given_config_source.blob && nongit)
-		die(_("--blob can only be used inside a git repository"));
-
-	if (given_config_source.file &&
-			!strcmp(given_config_source.file, "-")) {
-		given_config_source.file = NULL;
-		given_config_source.use_stdin = 1;
-	}
-
-	if (use_global_config) {
-		char *user_config = expand_user_path("~/.gitconfig", 0);
-		char *xdg_config = xdg_config_home("config");
-
-		if (!user_config)
-			/*
-			 * It is unknown if HOME/.gitconfig exists, so
-			 * we do not know if we should write to XDG
-			 * location; error out even if XDG_CONFIG_HOME
-			 * is set and points at a sane location.
-			 */
-			die(_("$HOME not set"));
-
-		if (access_or_warn(user_config, R_OK, 0) &&
-		    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
-			given_config_source.file = xdg_config;
-			free(user_config);
-		} else {
-			given_config_source.file = user_config;
-			free(xdg_config);
-		}
-	}
-	else if (use_system_config)
-		given_config_source.file = git_etc_gitconfig();
-	else if (use_local_config)
-		given_config_source.file = git_pathdup("config");
-	else if (use_worktree_config) {
-		given_config_source.file = get_worktree_config(the_repository);
-		if (!given_config_source.file)
-			die(_("--worktree cannot be used with multiple "
-			      "working trees unless the config\n"
-			      "extension worktreeConfig is enabled. "
-			      "Please read \"CONFIGURATION FILE\"\n"
-			      "section in \"git help worktree\" for details"));
-	}
+	set_config_source_file();
 
 	if (respect_includes_opt == -1)
 		config_options.respect_includes = !given_config_source.file;
-- 
2.20.0.482.g66447595a7
Previous: Eric SunshineNext: Nguyễn Thái Ngọc Duy
Message 11 of 12 in “Add "git config --move-to"”
  1. 0/6 Add "git config --move-to"Nguyễn Thái Ngọc Duy, Dec 27, 2018
  2. 1/6 config.c: avoid git_path() in do_git_config_sequence()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  3. Junio C HamanoJan 10, 2019
  4. 2/6 worktree.c: add get_worktree_config()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  5. Eric SunshineDec 27, 2018
  6. 3/6 config.c: add repo_config_set_worktree_gently()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  7. Derrick StoleeApr 30, 2019
  8. Duy NguyenMay 1, 2019
  9. 4/6 config: use OPT_FILENAME()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  10. Eric SunshineDec 27, 2018
  11. 5/6 config: factor out set_config_source_file()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  12. 6/6 config: add --move-toNguyễn Thái Ngọc Duy, Dec 27, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.