Skip to content

Commit ae2a382

Browse files
sunshinecogitster
authored andcommitted
worktree: elucidate environment variables intended for child processes
Take advantage of 'struct child_process.env' to make it obvious that environment variables set by add_worktree() are intended specifically for sub-commands it invokes to operate in the new worktree. We assign a local 'struct argv_array' to child_process.env, rather than utilizing the child_process.env_array 'struct argv_array', because future patches will make add_worktree() invoke additional sub-commands, and it's simpler to populate the environment array just once, whereas child_process.env_array gets cleared after each invocation, thus would require re-population for each sub-command. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c284243 commit ae2a382

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

builtin/worktree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static int add_worktree(const char *path, const char **child_argv,
186186
const char *name;
187187
struct stat st;
188188
struct child_process cp;
189+
struct argv_array child_env = ARGV_ARRAY_INIT;
189190
int counter = 0, len, ret;
190191
unsigned char rev[20];
191192

@@ -256,11 +257,12 @@ static int add_worktree(const char *path, const char **child_argv,
256257
fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
257258

258259
setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
259-
setenv(GIT_DIR_ENVIRONMENT, sb_git.buf, 1);
260-
setenv(GIT_WORK_TREE_ENVIRONMENT, path, 1);
260+
argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
261+
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
261262
memset(&cp, 0, sizeof(cp));
262263
cp.git_cmd = 1;
263264
cp.argv = child_argv;
265+
cp.env = child_env.argv;
264266
ret = run_command(&cp);
265267
if (!ret) {
266268
is_junk = 0;
@@ -272,6 +274,7 @@ static int add_worktree(const char *path, const char **child_argv,
272274
strbuf_reset(&sb);
273275
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
274276
unlink_or_warn(sb.buf);
277+
argv_array_clear(&child_env);
275278
strbuf_release(&sb);
276279
strbuf_release(&sb_repo);
277280
strbuf_release(&sb_git);

0 commit comments

Comments
 (0)