Subject: [PATCH 3/4] run-command: prep_childenv on all platforms

From: "D. Ben Knoble" <[email-masked]>
Date: Tue, 20 May 2025 15:34:57 -0400

We only prepare the child environment on non-Windows platforms, but
prep_childenv is the natural interposition point for our subprocess
system to adjust the environment as needed. Use it for Windows
platforms, also. In subsequent commits we'll use this interposition
point to modify the environment on all platforms.

Signed-off-by: D. Ben Knoble <[email-masked]>
---
 run-command.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/run-command.c b/run-command.c
index 8833b23367..dee6ae3e62 100644
--- a/run-command.c
+++ b/run-command.c
@@ -680,6 +680,7 @@ int start_command(struct child_process *cmd)
 	int fdin[2], fdout[2], fderr[2];
 	int failed_errno;
 	const char *str;
+	char **childenv;
 
 	/*
 	 * In case of errors we must keep the promise to close FDs
@@ -745,11 +746,12 @@ int start_command(struct child_process *cmd)
 	if (cmd->close_object_store)
 		close_object_store(the_repository->objects);
 
+	childenv = prep_childenv(cmd->env.v);
+
 #ifndef GIT_WINDOWS_NATIVE
 {
 	int notify_pipe[2];
 	int null_fd = -1;
-	char **childenv;
 	struct strvec argv = STRVEC_INIT;
 	struct child_err cerr;
 	struct atfork_state as;
@@ -772,7 +774,6 @@ int start_command(struct child_process *cmd)
 		set_cloexec(null_fd);
 	}
 
-	childenv = prep_childenv(cmd->env.v);
 	atfork_prepare(&as);
 
 	/*
@@ -893,7 +894,6 @@ int start_command(struct child_process *cmd)
 	if (null_fd >= 0)
 		close(null_fd);
 	strvec_clear(&argv);
-	free(childenv);
 }
 end_of_spawn:
 
@@ -933,7 +933,7 @@ int start_command(struct child_process *cmd)
 
 	trace_argv_printf(cmd->args.v, "trace: start_command:");
 	cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v,
-				  (char**) cmd->env.v,
+				  childenv,
 				  cmd->dir, fhin, fhout, fherr);
 	failed_errno = errno;
 	if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
@@ -952,6 +952,8 @@ int start_command(struct child_process *cmd)
 }
 #endif
 
+	free(childenv);
+
 	if (cmd->pid < 0) {
 		trace2_child_exit(cmd, -1);
 
-- 
2.48.1