Thread (95 messages) 95 messages, 6 authors, 2021-05-19
STALE1873d

[PATCH v4 5/6] config: refactor parsing of GIT_CONFIG_PARAMETERS

From: Patrick Steinhardt <hidden>
Date: 2020-12-09 11:54:41
Subsystem: the rest · Maintainer: Linus Torvalds

We're about to introduce a new way of passing parameters via environment
variables to git, which will require us to change the way we parse
config entries from parameters. Currently, `git_config_from_parameters`
is written in a way which makes it rather hard to extend.

Refactor the function to make it ready for the new logic as a
preparatory step in order to avoid reindenting code and adding new logic
in the same step, which would be much harder to reason about. This
refactoring is not expected to change any behaviour.

Signed-off-by: Patrick Steinhardt <redacted>
---
 config.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
diff --git a/config.c b/config.c
index 151980e5c9..8162f3cec8 100644
--- a/config.c
+++ b/config.c
@@ -505,35 +505,36 @@ int git_config_parse_parameter(const char *text,
 
 int git_config_from_parameters(config_fn_t fn, void *data)
 {
-	const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
+	const char *env;
 	int ret = 0;
-	char *envw;
+	char *envw = NULL;
 	const char **argv = NULL;
-	int nr = 0, alloc = 0;
 	int i;
 	struct config_source source;
 
-	if (!env)
-		return 0;
-
 	memset(&source, 0, sizeof(source));
 	source.prev = cf;
 	source.origin_type = CONFIG_ORIGIN_CMDLINE;
 	cf = &source;
 
-	/* sq_dequote will write over it */
-	envw = xstrdup(env);
+	env = getenv(CONFIG_DATA_ENVIRONMENT);
+	if (env) {
+		int nr = 0, alloc = 0;
 
-	if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
-		ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT);
-		goto out;
-	}
+		/* sq_dequote will write over it */
+		envw = xstrdup(env);
 
-	for (i = 0; i < nr; i++) {
-		if (git_config_parse_parameter(argv[i], fn, data) < 0) {
-			ret = -1;
+		if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
+			ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT);
 			goto out;
 		}
+
+		for (i = 0; i < nr; i++) {
+			if (git_config_parse_parameter(argv[i], fn, data) < 0) {
+				ret = -1;
+				goto out;
+			}
+		}
 	}
 
 out:
-- 
2.29.2

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help