Skip to content

Commit

Permalink
run-command: introduce CHILD_PROCESS_INIT
Browse files Browse the repository at this point in the history
Most struct child_process variables are cleared using memset first after
declaration.  Provide a macro, CHILD_PROCESS_INIT, that can be used to
initialize them statically instead.  That's shorter, doesn't require a
function call and is slightly more readable (especially given that we
already have STRBUF_INIT, ARGV_ARRAY_INIT etc.).

Helped-by: Johannes Sixt <[email protected]>
Signed-off-by: Rene Scharfe <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
rscharfe authored and gitster committed Aug 20, 2014
1 parent 6c4ab27 commit d318027
Show file tree
Hide file tree
Showing 40 changed files with 60 additions and 107 deletions.
4 changes: 2 additions & 2 deletions Documentation/technical/api-run-command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ command to run in a sub-process.

The caller:

1. allocates and clears (memset(&chld, 0, sizeof(chld));) a
struct child_process variable;
1. allocates and clears (memset(&chld, 0, sizeof(chld)); or
using CHILD_PROCESS_INIT) a struct child_process variable;
2. initializes the members;
3. calls start_command();
4. processes the data;
Expand Down
3 changes: 1 addition & 2 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
struct archiver_args *args)
{
struct strbuf cmd = STRBUF_INIT;
struct child_process filter;
struct child_process filter = CHILD_PROCESS_INIT;
const char *argv[2];
int r;

Expand All @@ -406,7 +406,6 @@ static int write_tar_filter_archive(const struct archiver *ar,
if (args->compression_level >= 0)
strbuf_addf(&cmd, " -%d", args->compression_level);

memset(&filter, 0, sizeof(filter));
argv[0] = cmd.buf;
argv[1] = NULL;
filter.argv = argv;
Expand Down
3 changes: 1 addition & 2 deletions builtin/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
char *file = git_pathdup("ADD_EDIT.patch");
const char *apply_argv[] = { "apply", "--recount", "--cached",
NULL, NULL };
struct child_process child;
struct child_process child = CHILD_PROCESS_INIT;
struct rev_info rev;
int out;
struct stat st;
Expand Down Expand Up @@ -214,7 +214,6 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
if (!st.st_size)
die(_("Empty patch. Aborted."));

memset(&child, 0, sizeof(child));
child.git_cmd = 1;
child.argv = apply_argv;
if (run_command(&child))
Expand Down
3 changes: 1 addition & 2 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
{
/* oldsha1 SP newsha1 LF NUL */
static char buf[2*40 + 3];
struct child_process proc;
struct child_process proc = CHILD_PROCESS_INIT;
const char *argv[3];
int code;
size_t n;
Expand All @@ -1520,7 +1520,6 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
argv[1] = "amend";
argv[2] = NULL;

memset(&proc, 0, sizeof(proc));
proc.argv = argv;
proc.in = -1;
proc.stdout_to_stderr = 1;
Expand Down
3 changes: 1 addition & 2 deletions builtin/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ static const char *get_man_viewer_info(const char *name)
static int check_emacsclient_version(void)
{
struct strbuf buffer = STRBUF_INIT;
struct child_process ec_process;
struct child_process ec_process = CHILD_PROCESS_INIT;
const char *argv_ec[] = { "emacsclient", "--version", NULL };
int version;

/* emacsclient prints its version number on stderr */
memset(&ec_process, 0, sizeof(ec_process));
ec_process.argv = argv_ec;
ec_process.err = -1;
ec_process.stdout_to_stderr = 1;
Expand Down
3 changes: 1 addition & 2 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,10 @@ static void drop_save(void)
static int save_state(unsigned char *stash)
{
int len;
struct child_process cp;
struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf buffer = STRBUF_INIT;
const char *argv[] = {"stash", "create", NULL};

memset(&cp, 0, sizeof(cp));
cp.argv = argv;
cp.out = -1;
cp.git_cmd = 1;
Expand Down
3 changes: 1 addition & 2 deletions builtin/notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ static void write_commented_object(int fd, const unsigned char *object)
{
const char *show_args[5] =
{"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
struct child_process show;
struct child_process show = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
struct strbuf cbuf = STRBUF_INIT;

/* Invoke "git show --stat --no-notes $object" */
memset(&show, 0, sizeof(show));
show.argv = show_args;
show.no_stdin = 1;
show.out = -1;
Expand Down
12 changes: 4 additions & 8 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static int copy_to_sideband(int in, int out, void *arg)
typedef int (*feed_fn)(void *, const char **, size_t *);
static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
{
struct child_process proc;
struct child_process proc = CHILD_PROCESS_INIT;
struct async muxer;
const char *argv[2];
int code;
Expand All @@ -266,7 +266,6 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_sta

argv[1] = NULL;

memset(&proc, 0, sizeof(proc));
proc.argv = argv;
proc.in = -1;
proc.stdout_to_stderr = 1;
Expand Down Expand Up @@ -350,7 +349,7 @@ static int run_receive_hook(struct command *commands, const char *hook_name,
static int run_update_hook(struct command *cmd)
{
const char *argv[5];
struct child_process proc;
struct child_process proc = CHILD_PROCESS_INIT;
int code;

argv[0] = find_hook("update");
Expand All @@ -362,7 +361,6 @@ static int run_update_hook(struct command *cmd)
argv[3] = sha1_to_hex(cmd->new_sha1);
argv[4] = NULL;

memset(&proc, 0, sizeof(proc));
proc.no_stdin = 1;
proc.stdout_to_stderr = 1;
proc.err = use_sideband ? -1 : 0;
Expand Down Expand Up @@ -598,7 +596,7 @@ static void run_update_post_hook(struct command *commands)
struct command *cmd;
int argc;
const char **argv;
struct child_process proc;
struct child_process proc = CHILD_PROCESS_INIT;
char *hook;

hook = find_hook("post-update");
Expand All @@ -621,7 +619,6 @@ static void run_update_post_hook(struct command *commands)
}
argv[argc] = NULL;

memset(&proc, 0, sizeof(proc));
proc.no_stdin = 1;
proc.stdout_to_stderr = 1;
proc.err = use_sideband ? -1 : 0;
Expand Down Expand Up @@ -911,7 +908,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
const char *hdr_err;
int status;
char hdr_arg[38];
struct child_process child;
struct child_process child = CHILD_PROCESS_INIT;
int fsck_objects = (receive_fsck_objects >= 0
? receive_fsck_objects
: transfer_fsck_objects >= 0
Expand All @@ -933,7 +930,6 @@ static const char *unpack(int err_fd, struct shallow_info *si)
argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
}

memset(&child, 0, sizeof(child));
if (ntohl(hdr.hdr_entries) < unpack_limit) {
argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
if (quiet)
Expand Down
3 changes: 1 addition & 2 deletions builtin/remote-ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ static void send_git_request(int stdin_fd, const char *serv, const char *repo,
static int run_child(const char *arg, const char *service)
{
int r;
struct child_process child;
struct child_process child = CHILD_PROCESS_INIT;

memset(&child, 0, sizeof(child));
child.in = -1;
child.out = -1;
child.err = 0;
Expand Down
3 changes: 1 addition & 2 deletions builtin/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
{".idx"},
{".bitmap", 1},
};
struct child_process cmd;
struct child_process cmd = CHILD_PROCESS_INIT;
struct string_list_item *item;
struct argv_array cmd_args = ARGV_ARRAY_INIT;
struct string_list names = STRING_LIST_INIT_DUP;
Expand Down Expand Up @@ -250,7 +250,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)

argv_array_push(&cmd_args, packtmp);

memset(&cmd, 0, sizeof(cmd));
cmd.argv = cmd_args.argv;
cmd.git_cmd = 1;
cmd.out = -1;
Expand Down
4 changes: 2 additions & 2 deletions builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
static void export_object(const unsigned char *sha1, enum object_type type,
int raw, const char *filename)
{
struct child_process cmd = { NULL };
struct child_process cmd = CHILD_PROCESS_INIT;
int fd;

fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Expand Down Expand Up @@ -234,7 +234,7 @@ static void import_object(unsigned char *sha1, enum object_type type,

if (!raw && type == OBJ_TREE) {
const char *argv[] = { "mktree", NULL };
struct child_process cmd = { argv };
struct child_process cmd = CHILD_PROCESS_INIT;
struct strbuf result = STRBUF_INIT;

cmd.argv = argv;
Expand Down
3 changes: 1 addition & 2 deletions builtin/verify-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

static int verify_one_pack(const char *path, unsigned int flags)
{
struct child_process index_pack;
struct child_process index_pack = CHILD_PROCESS_INIT;
const char *argv[] = {"index-pack", NULL, NULL, NULL };
struct strbuf arg = STRBUF_INIT;
int verbose = flags & VERIFY_PACK_VERBOSE;
Expand All @@ -32,7 +32,6 @@ static int verify_one_pack(const char *path, unsigned int flags)
strbuf_addstr(&arg, ".pack");
argv[2] = arg.buf;

memset(&index_pack, 0, sizeof(index_pack));
index_pack.argv = argv;
index_pack.git_cmd = 1;

Expand Down
6 changes: 2 additions & 4 deletions bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ int create_bundle(struct bundle_header *header, const char *path,
int i, ref_count = 0;
struct strbuf buf = STRBUF_INIT;
struct rev_info revs;
struct child_process rls;
struct child_process rls = CHILD_PROCESS_INIT;
FILE *rls_fout;

bundle_to_stdout = !strcmp(path, "-");
Expand All @@ -258,7 +258,6 @@ int create_bundle(struct bundle_header *header, const char *path,
init_revisions(&revs, NULL);

/* write prerequisites */
memset(&rls, 0, sizeof(rls));
argv_array_pushl(&rls.args,
"rev-list", "--boundary", "--pretty=oneline",
NULL);
Expand Down Expand Up @@ -417,14 +416,13 @@ int unbundle(struct bundle_header *header, int bundle_fd, int flags)
{
const char *argv_index_pack[] = {"index-pack",
"--fix-thin", "--stdin", NULL, NULL};
struct child_process ip;
struct child_process ip = CHILD_PROCESS_INIT;

if (flags & BUNDLE_VERBOSE)
argv_index_pack[3] = "-v";

if (verify_bundle(header, 0))
return -1;
memset(&ip, 0, sizeof(ip));
ip.argv = argv_index_pack;
ip.in = bundle_fd;
ip.no_stdout = 1;
Expand Down
2 changes: 1 addition & 1 deletion column.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ int parseopt_column_callback(const struct option *opt,
}

static int fd_out = -1;
static struct child_process column_process;
static struct child_process column_process = CHILD_PROCESS_INIT;

int run_column_filter(int colopts, const struct column_options *opts)
{
Expand Down
2 changes: 1 addition & 1 deletion connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
return protocol;
}

static struct child_process no_fork;
static struct child_process no_fork = CHILD_PROCESS_INIT;

/*
* This returns a dummy child_process if the transport protocol does not
Expand Down
3 changes: 1 addition & 2 deletions connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
struct transport *transport,
const char *shallow_file)
{
struct child_process rev_list;
struct child_process rev_list = CHILD_PROCESS_INIT;
const char *argv[9];
char commit[41];
unsigned char sha1[20];
Expand Down Expand Up @@ -60,7 +60,6 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
argv[ac++] = "--quiet";
argv[ac] = NULL;

memset(&rev_list, 0, sizeof(rev_list));
rev_list.argv = argv;
rev_list.git_cmd = 1;
rev_list.in = -1;
Expand Down
3 changes: 1 addition & 2 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ static int filter_buffer(int in, int out, void *data)
/*
* Spawn cmd and feed the buffer contents through its stdin.
*/
struct child_process child_process;
struct child_process child_process = CHILD_PROCESS_INIT;
struct filter_params *params = (struct filter_params *)data;
int write_err, status;
const char *argv[] = { NULL, NULL };
Expand All @@ -344,7 +344,6 @@ static int filter_buffer(int in, int out, void *data)

argv[0] = cmd.buf;

memset(&child_process, 0, sizeof(child_process));
child_process.argv = argv;
child_process.use_shell = 1;
child_process.in = -1;
Expand Down
3 changes: 1 addition & 2 deletions credential-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ static int send_request(const char *socket, const struct strbuf *out)

static void spawn_daemon(const char *socket)
{
struct child_process daemon;
struct child_process daemon = CHILD_PROCESS_INIT;
const char *argv[] = { NULL, NULL, NULL };
char buf[128];
int r;

memset(&daemon, 0, sizeof(daemon));
argv[0] = "git-credential-cache--daemon";
argv[1] = socket;
daemon.argv = argv;
Expand Down
3 changes: 1 addition & 2 deletions credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,10 @@ static int run_credential_helper(struct credential *c,
const char *cmd,
int want_output)
{
struct child_process helper;
struct child_process helper = CHILD_PROCESS_INIT;
const char *argv[] = { NULL, NULL };
FILE *fp;

memset(&helper, 0, sizeof(helper));
argv[0] = cmd;
helper.argv = argv;
helper.use_shell = 1;
Expand Down
8 changes: 3 additions & 5 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static const char *access_hook;

static int run_access_hook(struct daemon_service *service, const char *dir, const char *path)
{
struct child_process child;
struct child_process child = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
const char *argv[8];
const char **arg = argv;
Expand All @@ -277,7 +277,6 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
*arg = NULL;
#undef STRARG

memset(&child, 0, sizeof(child));
child.use_shell = 1;
child.argv = argv;
child.no_stdin = 1;
Expand Down Expand Up @@ -406,9 +405,8 @@ static void copy_to_log(int fd)

static int run_service_command(const char **argv)
{
struct child_process cld;
struct child_process cld = CHILD_PROCESS_INIT;

memset(&cld, 0, sizeof(cld));
cld.argv = argv;
cld.git_cmd = 1;
cld.err = -1;
Expand Down Expand Up @@ -733,7 +731,7 @@ static void check_dead_children(void)
static char **cld_argv;
static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
{
struct child_process cld = { NULL };
struct child_process cld = CHILD_PROCESS_INIT;
char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
char *env[] = { addrbuf, portbuf, NULL };

Expand Down
3 changes: 1 addition & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -4931,7 +4931,7 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
struct diff_tempfile *temp;
const char *argv[3];
const char **arg = argv;
struct child_process child;
struct child_process child = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
int err = 0;

Expand All @@ -4940,7 +4940,6 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
*arg++ = temp->name;
*arg = NULL;

memset(&child, 0, sizeof(child));
child.use_shell = 1;
child.argv = argv;
child.out = -1;
Expand Down
Loading

0 comments on commit d318027

Please sign in to comment.