Skip to content

Commit c7c4bde

Browse files
avargitster
authored andcommitted
run-command API: remove "env" member, always use "env_array"
Remove the "env" member from "struct child_process" in favor of always using the "env_array". As with the preceding removal of "argv" in favor of "args" this gets rid of current and future oddities around memory management at the API boundary (see the amended API docs). For some of the conversions we can replace patterns like: child.env = env->v; With: strvec_pushv(&child.env_array, env->v); But for others we need to guard the strvec_pushv() with a NULL check, since we're not passing in the "v" member of a "struct strvec", e.g. in the case of tmp_objdir_env()'s return value. Ideally we'd rename the "env_array" member to simply "env" as a follow-up, since it and "args" are now inconsistent in not having an "_array" suffix, and seemingly without any good reason, unless we look at the history of how they came to be. But as we've currently got 122 in-tree hits for a "git grep env_array" let's leave that for now (and possibly forever). Doing that rename would be too disruptive. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 26a1535 commit c7c4bde

File tree

8 files changed

+40
-42
lines changed

8 files changed

+40
-42
lines changed

builtin/receive-pack.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ static const char *push_to_deploy(unsigned char *sha1,
13671367

13681368
strvec_pushl(&child.args, "update-index", "-q", "--ignore-submodules",
13691369
"--refresh", NULL);
1370-
child.env = env->v;
1370+
strvec_pushv(&child.env_array, env->v);
13711371
child.dir = work_tree;
13721372
child.no_stdin = 1;
13731373
child.stdout_to_stderr = 1;
@@ -1379,7 +1379,7 @@ static const char *push_to_deploy(unsigned char *sha1,
13791379
child_process_init(&child);
13801380
strvec_pushl(&child.args, "diff-files", "--quiet",
13811381
"--ignore-submodules", "--", NULL);
1382-
child.env = env->v;
1382+
strvec_pushv(&child.env_array, env->v);
13831383
child.dir = work_tree;
13841384
child.no_stdin = 1;
13851385
child.stdout_to_stderr = 1;
@@ -1393,7 +1393,7 @@ static const char *push_to_deploy(unsigned char *sha1,
13931393
/* diff-index with either HEAD or an empty tree */
13941394
head_has_history() ? "HEAD" : empty_tree_oid_hex(),
13951395
"--", NULL);
1396-
child.env = env->v;
1396+
strvec_pushv(&child.env_array, env->v);
13971397
child.no_stdin = 1;
13981398
child.no_stdout = 1;
13991399
child.stdout_to_stderr = 0;
@@ -1404,7 +1404,7 @@ static const char *push_to_deploy(unsigned char *sha1,
14041404
child_process_init(&child);
14051405
strvec_pushl(&child.args, "read-tree", "-u", "-m", hash_to_hex(sha1),
14061406
NULL);
1407-
child.env = env->v;
1407+
strvec_pushv(&child.env_array, env->v);
14081408
child.dir = work_tree;
14091409
child.no_stdin = 1;
14101410
child.no_stdout = 1;
@@ -2202,7 +2202,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
22022202
close(err_fd);
22032203
return "unable to create temporary object directory";
22042204
}
2205-
child.env = tmp_objdir_env(tmp_objdir);
2205+
if (tmp_objdir)
2206+
strvec_pushv(&child.env_array, tmp_objdir_env(tmp_objdir));
22062207

22072208
/*
22082209
* Normally we just pass the tmp_objdir environment to the child

builtin/worktree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static int add_worktree(const char *path, const char *refname,
349349
strvec_push(&cp.args, "--quiet");
350350
}
351351

352-
cp.env = child_env.v;
352+
strvec_pushv(&cp.env_array, child_env.v);
353353
ret = run_command(&cp);
354354
if (ret)
355355
goto done;
@@ -360,7 +360,7 @@ static int add_worktree(const char *path, const char *refname,
360360
strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
361361
if (opts->quiet)
362362
strvec_push(&cp.args, "--quiet");
363-
cp.env = child_env.v;
363+
strvec_pushv(&cp.env_array, child_env.v);
364364
ret = run_command(&cp);
365365
if (ret)
366366
goto done;
@@ -389,7 +389,7 @@ static int add_worktree(const char *path, const char *refname,
389389
cp.no_stdin = 1;
390390
cp.stdout_to_stderr = 1;
391391
cp.dir = path;
392-
cp.env = env;
392+
strvec_pushv(&cp.env_array, env);
393393
cp.trace2_hook_name = "post-checkout";
394394
strvec_pushl(&cp.args, absolute_path(hook),
395395
oid_to_hex(null_oid()),

connected.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
109109
_("Checking connectivity"));
110110

111111
rev_list.git_cmd = 1;
112-
rev_list.env = opt->env;
112+
if (opt->env)
113+
strvec_pushv(&rev_list.env_array, opt->env);
113114
rev_list.in = -1;
114115
rev_list.no_stdout = 1;
115116
if (opt->err_fd)

editor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "config.h"
33
#include "strbuf.h"
4+
#include "strvec.h"
45
#include "run-command.h"
56
#include "sigchain.h"
67

@@ -78,7 +79,8 @@ static int launch_specified_editor(const char *editor, const char *path,
7879
strbuf_realpath(&realpath, path, 1);
7980

8081
strvec_pushl(&p.args, editor, realpath.buf, NULL);
81-
p.env = env;
82+
if (env)
83+
strvec_pushv(&p.env_array, (const char **)env);
8284
p.use_shell = 1;
8385
p.trace2_child_class = "editor";
8486
if (start_command(&p) < 0) {

object-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ static void fill_alternate_refs_command(struct child_process *cmd,
797797
}
798798
}
799799

800-
cmd->env = local_repo_env;
800+
strvec_pushv(&cmd->env_array, (const char **)local_repo_env);
801801
cmd->out = -1;
802802
}
803803

run-command.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,7 @@ static void trace_run_command(const struct child_process *cp)
655655
sq_quote_buf_pretty(&buf, cp->dir);
656656
strbuf_addch(&buf, ';');
657657
}
658-
/*
659-
* The caller is responsible for initializing cp->env from
660-
* cp->env_array if needed. We only check one place.
661-
*/
662-
if (cp->env)
663-
trace_add_env(&buf, cp->env);
658+
trace_add_env(&buf, cp->env_array.v);
664659
if (cp->git_cmd)
665660
strbuf_addstr(&buf, " git");
666661
sq_quote_argv_pretty(&buf, cp->args.v);
@@ -676,9 +671,6 @@ int start_command(struct child_process *cmd)
676671
int failed_errno;
677672
char *str;
678673

679-
if (!cmd->env)
680-
cmd->env = cmd->env_array.v;
681-
682674
/*
683675
* In case of errors we must keep the promise to close FDs
684676
* that have been passed in via ->in and ->out.
@@ -768,7 +760,7 @@ int start_command(struct child_process *cmd)
768760
set_cloexec(null_fd);
769761
}
770762

771-
childenv = prep_childenv(cmd->env);
763+
childenv = prep_childenv(cmd->env_array.v);
772764
atfork_prepare(&as);
773765

774766
/*
@@ -931,7 +923,7 @@ int start_command(struct child_process *cmd)
931923
else if (cmd->use_shell)
932924
cmd->args.v = prepare_shell_cmd(&nargv, sargv);
933925

934-
cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v, (char**) cmd->env,
926+
cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v, (char**) cmd->env_array.v,
935927
cmd->dir, fhin, fhout, fherr);
936928
failed_errno = errno;
937929
if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
@@ -1047,7 +1039,8 @@ int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir,
10471039
cmd.wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0;
10481040
cmd.close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0;
10491041
cmd.dir = dir;
1050-
cmd.env = env;
1042+
if (env)
1043+
strvec_pushv(&cmd.env_array, (const char **)env);
10511044
cmd.trace2_child_class = tr2_class;
10521045
return run_command(&cmd);
10531046
}
@@ -1333,7 +1326,8 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
13331326
strvec_push(&hook.args, p);
13341327
while ((p = va_arg(args, const char *)))
13351328
strvec_push(&hook.args, p);
1336-
hook.env = env;
1329+
if (env)
1330+
strvec_pushv(&hook.env_array, (const char **)env);
13371331
hook.no_stdin = 1;
13381332
hook.stdout_to_stderr = 1;
13391333
hook.trace2_hook_name = name;

run-command.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ struct child_process {
5656
* `finish_command` (or during `start_command` when it is unsuccessful).
5757
*/
5858
struct strvec args;
59+
60+
/**
61+
* Like .args the .env_array is a `struct strvec'.
62+
*
63+
* To modify the environment of the sub-process, specify an array of
64+
* environment settings. Each string in the array manipulates the
65+
* environment.
66+
*
67+
* - If the string is of the form "VAR=value", i.e. it contains '='
68+
* the variable is added to the child process's environment.
69+
*
70+
* - If the string does not contain '=', it names an environment
71+
* variable that will be removed from the child process's environment.
72+
*
73+
* The memory in .env_array will be cleaned up automatically during
74+
* `finish_command` (or during `start_command` when it is unsuccessful).
75+
*/
5976
struct strvec env_array;
6077
pid_t pid;
6178

@@ -92,23 +109,6 @@ struct child_process {
92109
*/
93110
const char *dir;
94111

95-
/**
96-
* To modify the environment of the sub-process, specify an array of
97-
* string pointers (NULL terminated) in .env:
98-
*
99-
* - If the string is of the form "VAR=value", i.e. it contains '='
100-
* the variable is added to the child process's environment.
101-
*
102-
* - If the string does not contain '=', it names an environment
103-
* variable that will be removed from the child process's environment.
104-
*
105-
* If the .env member is NULL, `start_command` will point it at the
106-
* .env_array `strvec` (so you may use one or the other, but not both).
107-
* The memory in .env_array will be cleaned up automatically during
108-
* `finish_command` (or during `start_command` when it is unsuccessful).
109-
*/
110-
const char *const *env;
111-
112112
unsigned no_stdin:1;
113113
unsigned no_stdout:1;
114114
unsigned no_stderr:1;

trailer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static char *apply_command(struct conf_info *conf, const char *arg)
236236
strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
237237
strvec_push(&cp.args, cmd.buf);
238238
}
239-
cp.env = local_repo_env;
239+
strvec_pushv(&cp.env_array, (const char **)local_repo_env);
240240
cp.no_stdin = 1;
241241
cp.use_shell = 1;
242242

0 commit comments

Comments
 (0)