use env_array member of struct child_process
Convert users of struct child_process to using the managed env_array for specifying environment variables instead of supplying an array on the stack or bringing their own argv_array. This shortens and simplifies the code and ensures automatically that the allocated memory is freed after use. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 19, 2014 at 13:14 UTC
a915459097b72da9cc058172a54029352b684b0f
5 files changed
+25
-38
builtin/receive-pack.c
+14
-9
@@ -453,7 +453,6 @@ leave:
453
static void prepare_push_cert_sha1(struct child_process *proc)
454
{
455
static int already_done;
456
- struct argv_array env = ARGV_ARRAY_INIT;
456
457
if (!push_cert.len)
458
return;
@@ -487,20 +486,26 @@ static void prepare_push_cert_sha1(struct child_process *proc)
486
nonce_status = check_nonce(push_cert.buf, bogs);
487
}
488
if (!is_null_sha1(push_cert_sha1)) {
490
- argv_array_pushf(&env, "GIT_PUSH_CERT=%s", sha1_to_hex(push_cert_sha1));
491
- argv_array_pushf(&env, "GIT_PUSH_CERT_SIGNER=%s",
489
+ argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT=%s",
490
+ sha1_to_hex(push_cert_sha1));
491
+ argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s",
492
sigcheck.signer ? sigcheck.signer : "");
493
- argv_array_pushf(&env, "GIT_PUSH_CERT_KEY=%s",
493
+ argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s",
494
sigcheck.key ? sigcheck.key : "");
495
- argv_array_pushf(&env, "GIT_PUSH_CERT_STATUS=%c", sigcheck.result);
495
+ argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_STATUS=%c",
496
+ sigcheck.result);
497
if (push_cert_nonce) {
497
- argv_array_pushf(&env, "GIT_PUSH_CERT_NONCE=%s", push_cert_nonce);
498
- argv_array_pushf(&env, "GIT_PUSH_CERT_NONCE_STATUS=%s", nonce_status);
498
+ argv_array_pushf(&proc->env_array,
499
+ "GIT_PUSH_CERT_NONCE=%s",
500
+ push_cert_nonce);
501
+ argv_array_pushf(&proc->env_array,
502
+ "GIT_PUSH_CERT_NONCE_STATUS=%s",
503
+ nonce_status);
504
if (nonce_status == NONCE_SLOP)
500
- argv_array_pushf(&env, "GIT_PUSH_CERT_NONCE_SLOP=%ld",
505
+ argv_array_pushf(&proc->env_array,
506
+ "GIT_PUSH_CERT_NONCE_SLOP=%ld",
507
nonce_stamp_slop);
508
}
503
- proc->env = env.argv;
509
}
510
}
511
http-backend.c
+3
-6
@@ -314,7 +314,6 @@ static void run_service(const char **argv)
314
const char *encoding = getenv("HTTP_CONTENT_ENCODING");
315
const char *user = getenv("REMOTE_USER");
316
const char *host = getenv("REMOTE_ADDR");
317
- struct argv_array env = ARGV_ARRAY_INIT;
317
int gzipped_request = 0;
318
struct child_process cld = CHILD_PROCESS_INIT;
319
@@ -329,13 +328,12 @@ static void run_service(const char **argv)
328
host = "(none)";
329
330
if (!getenv("GIT_COMMITTER_NAME"))
332
- argv_array_pushf(&env, "GIT_COMMITTER_NAME=%s", user);
331
+ argv_array_pushf(&cld.env_array, "GIT_COMMITTER_NAME=%s", user);
332
if (!getenv("GIT_COMMITTER_EMAIL"))
334
- argv_array_pushf(&env, "GIT_COMMITTER_EMAIL=%s@http.%s",
335
- user, host);
333
+ argv_array_pushf(&cld.env_array,
334
+ "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
335
336
cld.argv = argv;
338
- cld.env = env.argv;
337
if (gzipped_request)
338
cld.in = -1;
339
cld.git_cmd = 1;
@@ -350,7 +348,6 @@ static void run_service(const char **argv)
348
349
if (finish_command(&cld))
350
exit(1);
353
- argv_array_clear(&env);
351
}
352
353
static int show_text_ref(const char *name, const unsigned char *sha1,
pager.c
+4
-11
@@ -74,17 +74,10 @@ void setup_pager(void)
74
pager_process.use_shell = 1;
75
pager_process.argv = pager_argv;
76
pager_process.in = -1;
77
- if (!getenv("LESS") || !getenv("LV")) {
78
- static const char *env[3];
79
- int i = 0;
80
-
81
- if (!getenv("LESS"))
82
- env[i++] = "LESS=FRX";
83
- if (!getenv("LV"))
84
- env[i++] = "LV=-c";
85
- env[i] = NULL;
86
- pager_process.env = env;
87
- }
77
+ if (!getenv("LESS"))
78
+ argv_array_push(&pager_process.env_array, "LESS=FRX");
79
+ if (!getenv("LV"))
80
+ argv_array_push(&pager_process.env_array, "LV=-c");
81
if (start_command(&pager_process))
82
return;
83
transport-helper.c
+2
-8
@@ -108,12 +108,6 @@ static struct child_process *get_helper(struct transport *transport)
108
int refspec_alloc = 0;
109
int duped;
110
int code;
111
- char git_dir_buf[sizeof(GIT_DIR_ENVIRONMENT) + PATH_MAX + 1];
112
- const char *helper_env[] = {
113
- git_dir_buf,
114
- NULL
115
- };
116
-
111
112
if (data->helper)
113
return data->helper;
@@ -129,8 +123,8 @@ static struct child_process *get_helper(struct transport *transport)
123
helper->git_cmd = 0;
124
helper->silent_exec_failure = 1;
125
132
- snprintf(git_dir_buf, sizeof(git_dir_buf), "%s=%s", GIT_DIR_ENVIRONMENT, get_git_dir());
133
- helper->env = helper_env;
126
+ argv_array_pushf(&helper->env_array, "%s=%s", GIT_DIR_ENVIRONMENT,
127
+ get_git_dir());
128
129
code = start_command(helper);
130
if (code < 0 && errno == ENOENT)
wt-status.c
+2
-4
@@ -726,14 +726,14 @@ static void wt_status_print_changed(struct wt_status *s)
726
static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
727
{
728
struct child_process sm_summary = CHILD_PROCESS_INIT;
729
- struct argv_array env = ARGV_ARRAY_INIT;
729
struct argv_array argv = ARGV_ARRAY_INIT;
730
struct strbuf cmd_stdout = STRBUF_INIT;
731
struct strbuf summary = STRBUF_INIT;
732
char *summary_content;
733
size_t len;
734
736
- argv_array_pushf(&env, "GIT_INDEX_FILE=%s", s->index_file);
735
+ argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
736
+ s->index_file);
737
738
argv_array_push(&argv, "submodule");
739
argv_array_push(&argv, "summary");
@@ -745,14 +745,12 @@ static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitt
745
argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
746
747
sm_summary.argv = argv.argv;
748
- sm_summary.env = env.argv;
748
sm_summary.git_cmd = 1;
749
sm_summary.no_stdin = 1;
750
fflush(s->fp);
751
sm_summary.out = -1;
752
753
run_command(&sm_summary);
755
- argv_array_clear(&env);
754
argv_array_clear(&argv);
755
756
len = strbuf_read(&cmd_stdout, sm_summary.out, 1024);