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

[PATCH 04/14] config: add kvi.path, use it to evaluate includes

From
Glen Choo via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 21, 2023, 19:13 UTC
Message-ID
<b407fe1ed1d9cf4d9d79f900c48864fe4eab9568.1682104398.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1497.git.git.1682104398.gitgitgadget@gmail.com>
From: Glen Choo <chooglen@google.com>

Include directives are evaluated using the path of the config file. To reduce the dependence on "config_reader.source", add a new "key_value_info.path" member and use that instead of "config_source.path".

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 40 ++++++++++++++++++++--------------------
 config.h |  1 +
 2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/config.c b/config.c
index 3369b32d065..b952a4b118d 100644
--- a/config.c
+++ b/config.c
@@ -199,7 +199,7 @@ static const char include_depth_advice[] = N_(
 "from\n"
 "	%s\n"
 "This might be due to circular includes.");
-static int handle_path_include(struct config_source *cs, const char *path,
+static int handle_path_include(struct key_value_info *kvi, const char *path,
 			       struct config_include_data *inc)
 {
 	int ret = 0;
@@ -221,14 +221,14 @@ static int handle_path_include(struct config_source *cs, const char *path,
 	if (!is_absolute_path(path)) {
 		char *slash;
 
-		if (!cs || !cs->path) {
+		if (!kvi || !kvi->path) {
 			ret = error(_("relative config includes must come from files"));
 			goto cleanup;
 		}
 
-		slash = find_last_dir_sep(cs->path);
+		slash = find_last_dir_sep(kvi->path);
 		if (slash)
-			strbuf_add(&buf, cs->path, slash - cs->path + 1);
+			strbuf_add(&buf, kvi->path, slash - kvi->path + 1);
 		strbuf_addstr(&buf, path);
 		path = buf.buf;
 	}
@@ -236,12 +236,11 @@ static int handle_path_include(struct config_source *cs, const char *path,
 	if (!access_or_die(path, R_OK, 0)) {
 		if (++inc->depth > MAX_INCLUDE_DEPTH)
 			die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
-			    !cs ? "<unknown>" :
-			    cs->name ? cs->name :
+			    !kvi ? "<unknown>" :
+			    kvi->filename ? kvi->filename :
 			    "the command line");
 		ret = git_config_from_file_with_options(git_config_include, path, inc,
-							current_config_scope(),
-							NULL);
+							kvi->scope, NULL);
 		inc->depth--;
 	}
 cleanup:
@@ -256,7 +255,7 @@ static void add_trailing_starstar_for_dir(struct strbuf *pat)
 		strbuf_addstr(pat, "**");
 }
 
-static int prepare_include_condition_pattern(struct config_source *cs,
+static int prepare_include_condition_pattern(struct key_value_info *kvi,
 					     struct strbuf *pat)
 {
 	struct strbuf path = STRBUF_INIT;
@@ -273,11 +272,11 @@ static int prepare_include_condition_pattern(struct config_source *cs,
 	if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
 		const char *slash;
 
-		if (!cs || !cs->path)
+		if (!kvi || !kvi->path)
 			return error(_("relative config include "
 				       "conditionals must come from files"));
 
-		strbuf_realpath(&path, cs->path, 1);
+		strbuf_realpath(&path, kvi->path, 1);
 		slash = find_last_dir_sep(path.buf);
 		if (!slash)
 			BUG("how is this possible?");
@@ -292,7 +291,7 @@ static int prepare_include_condition_pattern(struct config_source *cs,
 	return prefix;
 }
 
-static int include_by_gitdir(struct config_source *cs,
+static int include_by_gitdir(struct key_value_info *kvi,
 			     const struct config_options *opts,
 			     const char *cond, size_t cond_len, int icase)
 {
@@ -309,7 +308,7 @@ static int include_by_gitdir(struct config_source *cs,
 
 	strbuf_realpath(&text, git_dir, 1);
 	strbuf_add(&pattern, cond, cond_len);
-	prefix = prepare_include_condition_pattern(cs, &pattern);
+	prefix = prepare_include_condition_pattern(kvi, &pattern);
 
 again:
 	if (prefix < 0)
@@ -442,16 +441,16 @@ static int include_by_remote_url(struct config_include_data *inc,
 					     inc->remote_urls);
 }
 
-static int include_condition_is_true(struct config_source *cs,
+static int include_condition_is_true(struct key_value_info *kvi,
 				     struct config_include_data *inc,
 				     const char *cond, size_t cond_len)
 {
 	const struct config_options *opts = inc->opts;
 
 	if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
-		return include_by_gitdir(cs, opts, cond, cond_len, 0);
+		return include_by_gitdir(kvi, opts, cond, cond_len, 0);
 	else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
-		return include_by_gitdir(cs, opts, cond, cond_len, 1);
+		return include_by_gitdir(kvi, opts, cond, cond_len, 1);
 	else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
 		return include_by_branch(cond, cond_len);
 	else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
@@ -467,7 +466,6 @@ static int kvi_fn(config_fn_t fn, const char *key, const char *value,
 static int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
-	struct config_source *cs = inc->config_reader->source;
 	struct key_value_info *kvi = inc->config_reader->config_kvi;
 	const char *cond, *key;
 	size_t cond_len;
@@ -482,16 +480,16 @@ static int git_config_include(const char *var, const char *value, void *data)
 		return ret;
 
 	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(kvi, value, inc);
 
 	if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
-	    cond && include_condition_is_true(cs, inc, cond, cond_len) &&
+	    cond && include_condition_is_true(kvi, inc, cond, cond_len) &&
 	    !strcmp(key, "path")) {
 		config_fn_t old_fn = inc->fn;
 
 		if (inc->opts->unconditional_remote_url)
 			inc->fn = forbid_remote_url;
-		ret = handle_path_include(cs, value, inc);
+		ret = handle_path_include(kvi, value, inc);
 		inc->fn = old_fn;
 	}
 
@@ -683,6 +681,7 @@ static void kvi_from_param(struct key_value_info *out)
 	out->linenr = -1;
 	out->origin_type = CONFIG_ORIGIN_CMDLINE;
 	out->scope = CONFIG_SCOPE_COMMAND;
+	out->path = NULL;
 }
 
 int git_config_parse_parameter(const char *text,
@@ -1077,6 +1076,7 @@ static void kvi_from_source(struct config_source *cs,
 	out->origin_type = cs->origin_type;
 	out->linenr = cs->linenr;
 	out->scope = scope;
+	out->path = cs->path;
 }
 
 static int git_parse_source(struct config_source *cs, config_fn_t fn,
diff --git a/config.h b/config.h
index 27d1718ac7b..f8bab9fdef4 100644
--- a/config.h
+++ b/config.h
@@ -674,6 +674,7 @@ struct key_value_info {
 	int linenr;
 	enum config_origin_type origin_type;
 	enum config_scope scope;
+	const char *path;
 	struct key_value_info *prev;
 };
 
-- 
gitgitgadget
Previous: Glen Choo via GitGitGadgetNext: Glen Choo via GitGitGadget
Message 7 of 115 in “[RFC] config: remove global state from config iteration”
  1. 00/14 [RFC] config: remove global state from config iterationGlen Choo via GitGitGadget, Apr 21, 2023
  2. 01/14 config.c: introduce kvi_fn(), use it for configsetsGlen Choo via GitGitGadget, Apr 21, 2023
  3. 02/14 config.c: use kvi for CLI configGlen Choo via GitGitGadget, Apr 21, 2023
  4. Ævar Arnfjörð BjarmasonMay 1, 2023
  5. 06/14 config: inline git_color_default_configGlen Choo via GitGitGadget, Apr 21, 2023
  6. 03/14 config: use kvi for config filesGlen Choo via GitGitGadget, Apr 21, 2023
  7. 04/14 config: add kvi.path, use it to evaluate includesGlen Choo via GitGitGadget, Apr 21, 2023
  8. 05/14 config: pass source to config_parser_event_fn_tGlen Choo via GitGitGadget, Apr 21, 2023
  9. 07/14 urlmatch.h: use config_fn_t typeGlen Choo via GitGitGadget, Apr 21, 2023
  10. 08/14 (RFC-only) config: add kvi arg to config_fn_tGlen Choo via GitGitGadget, Apr 21, 2023
  11. 10/14 (RFC-only) config: finish config_fn_t refactorGlen Choo via GitGitGadget, Apr 21, 2023
  12. Ævar Arnfjörð BjarmasonMay 1, 2023
  13. Jonathan TanMay 5, 2023
  14. Glen ChooMay 9, 2023
  15. Jonathan TanMay 11, 2023
  16. Glen ChooMay 8, 2023
  17. 11/14 config: remove current_config_(line|name|origin_type)Glen Choo via GitGitGadget, Apr 21, 2023
  18. 14/14 config: remove config_reader from configset_add_valueGlen Choo via GitGitGadget, Apr 21, 2023
  19. 12/14 config: remove current_config_scope()Glen Choo via GitGitGadget, Apr 21, 2023
  20. 09/14 (RFC-only) config: apply cocci to config_fn_t implementationsGlen Choo via GitGitGadget, Apr 21, 2023
  21. 13/14 config: pass kvi to die_bad_number()Glen Choo via GitGitGadget, Apr 21, 2023
  22. 00/14 [RFC] config: remove global state from config iterationGlen Choo via GitGitGadget, May 30, 2023
  23. 01/14 config: inline git_color_default_configGlen Choo via GitGitGadget, May 30, 2023
  24. 02/14 urlmatch.h: use config_fn_t typeGlen Choo via GitGitGadget, May 30, 2023
  25. 03/14 (RFC-only) config: add kvi arg to config_fn_tGlen Choo via GitGitGadget, May 30, 2023
  26. Phillip WoodJun 1, 2023
  27. Glen ChooJun 1, 2023
  28. Phillip WoodJun 2, 2023
  29. Glen ChooJun 2, 2023
  30. Phillip WoodJun 5, 2023
  31. Glen ChooJun 9, 2023
  32. 08/14 builtin/config.c: test misuse of format_config()Glen Choo via GitGitGadget, May 30, 2023
  33. 06/14 config.c: pass kvi in configsetsGlen Choo via GitGitGadget, May 30, 2023
  34. Jonathan TanJun 1, 2023
  35. 05/14 (RFC-only) config: finish config_fn_t refactorGlen Choo via GitGitGadget, May 30, 2023
  36. Jonathan TanJun 1, 2023
  37. 07/14 config: provide kvi with config filesGlen Choo via GitGitGadget, May 30, 2023
  38. Jonathan TanJun 1, 2023
  39. Jonathan TanJun 1, 2023
  40. 04/14 (RFC-only) config: apply cocci to config_fn_t implementationsGlen Choo via GitGitGadget, May 30, 2023
  41. 09/14 config.c: provide kvi with CLI configGlen Choo via GitGitGadget, May 30, 2023
  42. Jonathan TanJun 1, 2023
  43. Glen ChooJun 2, 2023
  44. 10/14 trace2: plumb config kviGlen Choo via GitGitGadget, May 30, 2023
  45. Jonathan TanJun 1, 2023
  46. 12/14 config.c: remove config_reader from configsetsGlen Choo via GitGitGadget, May 30, 2023
  47. 13/14 config: add kvi.path, use it to evaluate includesGlen Choo via GitGitGadget, May 30, 2023
  48. Jonathan TanJun 2, 2023
  49. 14/14 config: pass source to config_parser_event_fn_tGlen Choo via GitGitGadget, May 30, 2023
  50. Jonathan TanJun 2, 2023
  51. Glen ChooJun 2, 2023
  52. 11/14 config: pass kvi to die_bad_number()Glen Choo via GitGitGadget, May 30, 2023
  53. Jonathan TanJun 1, 2023
  54. Glen ChooJun 2, 2023
  55. 00/12 config: remove global state from config iterationGlen Choo via GitGitGadget, Jun 20, 2023
  56. 02/12 urlmatch.h: use config_fn_t typeGlen Choo via GitGitGadget, Jun 20, 2023
  57. Junio C HamanoJun 20, 2023
  58. 01/12 config: inline git_color_default_configGlen Choo via GitGitGadget, Jun 20, 2023
  59. Junio C HamanoJun 20, 2023
  60. 04/12 config.c: pass ctx in configsetsGlen Choo via GitGitGadget, Jun 20, 2023
  61. Junio C HamanoJun 20, 2023
  62. 05/12 config: pass ctx with config filesGlen Choo via GitGitGadget, Jun 20, 2023
  63. 06/12 builtin/config.c: test misuse of format_config()Glen Choo via GitGitGadget, Jun 20, 2023
  64. Junio C HamanoJun 20, 2023
  65. Glen ChooJun 20, 2023
  66. Jonathan TanJun 23, 2023
  67. Jeff KingJun 24, 2023
  68. Glen ChooJun 28, 2023
  69. 07/12 config.c: pass ctx with CLI configGlen Choo via GitGitGadget, Jun 20, 2023
  70. Jonathan TanJun 23, 2023
  71. Glen ChooJun 23, 2023
  72. 08/12 trace2: plumb config kviGlen Choo via GitGitGadget, Jun 20, 2023
  73. Jonathan TanJun 23, 2023
  74. 10/12 config.c: remove config_reader from configsetsGlen Choo via GitGitGadget, Jun 20, 2023
  75. Jonathan TanJun 23, 2023
  76. Junio C HamanoJun 23, 2023
  77. 03/12 config: add ctx arg to config_fn_tGlen Choo via GitGitGadget, Jun 20, 2023
  78. 11/12 config: add kvi.path, use it to evaluate includesGlen Choo via GitGitGadget, Jun 20, 2023
  79. 12/12 config: pass source to config_parser_event_fn_tGlen Choo via GitGitGadget, Jun 20, 2023
  80. Junio C HamanoJun 20, 2023
  81. 09/12 config: pass kvi to die_bad_number()Glen Choo via GitGitGadget, Jun 20, 2023
  82. Junio C HamanoJun 21, 2023
  83. Glen ChooJun 21, 2023
  84. Jonathan TanJun 23, 2023
  85. Junio C HamanoJun 23, 2023
  86. Glen ChooJun 23, 2023
  87. 00/12 config: remove global state from config iterationGlen Choo via GitGitGadget, Jun 26, 2023
  88. 01/12 config: inline git_color_default_configGlen Choo via GitGitGadget, Jun 26, 2023
  89. 02/12 urlmatch.h: use config_fn_t typeGlen Choo via GitGitGadget, Jun 26, 2023
  90. 04/12 config.c: pass ctx in configsetsGlen Choo via GitGitGadget, Jun 26, 2023
  91. 06/12 builtin/config.c: test misuse of format_config()Glen Choo via GitGitGadget, Jun 26, 2023
  92. 05/12 config: pass ctx with config filesGlen Choo via GitGitGadget, Jun 26, 2023
  93. 07/12 config.c: pass ctx with CLI configGlen Choo via GitGitGadget, Jun 26, 2023
  94. 08/12 trace2: plumb config kviGlen Choo via GitGitGadget, Jun 26, 2023
  95. 03/12 config: add ctx arg to config_fn_tGlen Choo via GitGitGadget, Jun 26, 2023
  96. 11/12 config: add kvi.path, use it to evaluate includesGlen Choo via GitGitGadget, Jun 26, 2023
  97. 12/12 config: pass source to config_parser_event_fn_tGlen Choo via GitGitGadget, Jun 26, 2023
  98. 10/12 config.c: remove config_reader from configsetsGlen Choo via GitGitGadget, Jun 26, 2023
  99. 09/12 config: pass kvi to die_bad_number()Glen Choo via GitGitGadget, Jun 26, 2023
  100. Junio C HamanoJun 26, 2023
  101. 00/11 config: remove global state from config iterationGlen Choo via GitGitGadget, Jun 28, 2023
  102. 01/11 config: inline git_color_default_configGlen Choo via GitGitGadget, Jun 28, 2023
  103. 02/11 urlmatch.h: use config_fn_t typeGlen Choo via GitGitGadget, Jun 28, 2023
  104. 04/11 config.c: pass ctx in configsetsGlen Choo via GitGitGadget, Jun 28, 2023
  105. 05/11 config: pass ctx with config filesGlen Choo via GitGitGadget, Jun 28, 2023
  106. 06/11 config.c: pass ctx with CLI configGlen Choo via GitGitGadget, Jun 28, 2023
  107. 07/11 trace2: plumb config kviGlen Choo via GitGitGadget, Jun 28, 2023
  108. 03/11 config: add ctx arg to config_fn_tGlen Choo via GitGitGadget, Jun 28, 2023
  109. 09/11 config.c: remove config_reader from configsetsGlen Choo via GitGitGadget, Jun 28, 2023
  110. 10/11 config: add kvi.path, use it to evaluate includesGlen Choo via GitGitGadget, Jun 28, 2023
  111. 11/11 config: pass source to config_parser_event_fn_tGlen Choo via GitGitGadget, Jun 28, 2023
  112. 08/11 config: pass kvi to die_bad_number()Glen Choo via GitGitGadget, Jun 28, 2023
  113. Jonathan TanJun 28, 2023
  114. Junio C HamanoJun 28, 2023
  115. Phillip WoodJul 11, 2023

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.