Skip to content

Commit

Permalink
run-command: add env_array, an optional argv_array for env
Browse files Browse the repository at this point in the history
Similar to args, add a struct argv_array member to struct child_process
that simplifies specifying the environment for children.  It is freed
automatically by finish_command() or if start_command() encounters an
error.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Oct 19, 2014
1 parent 98349e5 commit 19a583d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Documentation/technical/api-run-command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ string pointers (NULL terminated) in .env:
. If the string does not contain '=', it names an environment
variable that will be removed from the child process's environment.

If the .env member is NULL, `start_command` will point it at the
.env_array `argv_array` (so you may use one or the other, but not both).
The memory in .env_array will be cleaned up automatically during
`finish_command` (or during `start_command` when it is unsuccessful).

To specify a new initial working directory for the sub-process,
specify it in the .dir member.

Expand Down
6 changes: 6 additions & 0 deletions run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void child_process_init(struct child_process *child)
{
memset(child, 0, sizeof(*child));
argv_array_init(&child->args);
argv_array_init(&child->env_array);
}

struct child_to_clean {
Expand Down Expand Up @@ -287,6 +288,8 @@ int start_command(struct child_process *cmd)

if (!cmd->argv)
cmd->argv = cmd->args.argv;
if (!cmd->env)
cmd->env = cmd->env_array.argv;

/*
* In case of errors we must keep the promise to close FDs
Expand Down Expand Up @@ -338,6 +341,7 @@ int start_command(struct child_process *cmd)
error("cannot create %s pipe for %s: %s",
str, cmd->argv[0], strerror(failed_errno));
argv_array_clear(&cmd->args);
argv_array_clear(&cmd->env_array);
errno = failed_errno;
return -1;
}
Expand Down Expand Up @@ -524,6 +528,7 @@ int start_command(struct child_process *cmd)
else if (cmd->err)
close(cmd->err);
argv_array_clear(&cmd->args);
argv_array_clear(&cmd->env_array);
errno = failed_errno;
return -1;
}
Expand All @@ -550,6 +555,7 @@ int finish_command(struct child_process *cmd)
{
int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
argv_array_clear(&cmd->args);
argv_array_clear(&cmd->env_array);
return ret;
}

Expand Down
3 changes: 2 additions & 1 deletion run-command.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
struct child_process {
const char **argv;
struct argv_array args;
struct argv_array env_array;
pid_t pid;
/*
* Using .in, .out, .err:
Expand Down Expand Up @@ -44,7 +45,7 @@ struct child_process {
unsigned clean_on_exit:1;
};

#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT }
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
void child_process_init(struct child_process *);

int start_command(struct child_process *);
Expand Down

0 comments on commit 19a583d

Please sign in to comment.