From: Junio C Hamano Date: Mon, 22 Dec 2014 20:27:10 +0000 (-0800) Subject: Merge branch 'jc/hook-cleanup' X-Git-Tag: v2.3.0-rc0~62 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/77a801d23788991a752a19a45a7474fa8b707082?hp=-c Merge branch 'jc/hook-cleanup' Remove unused code. * jc/hook-cleanup: run-command.c: retire unused run_hook_with_custom_index() --- 77a801d23788991a752a19a45a7474fa8b707082 diff --combined run-command.c index a47699966c,ae36852404..0b432cc971 --- a/run-command.c +++ b/run-command.c @@@ -8,13 -8,6 +8,13 @@@ # define SHELL_PATH "/bin/sh" #endif +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 { pid_t pid; struct child_to_clean *next; @@@ -288,8 -281,6 +288,8 @@@ int start_command(struct child_process 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 @@@ -341,7 -332,6 +341,7 @@@ fail_pipe 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; } @@@ -528,7 -518,6 +528,7 @@@ else if (cmd->err) close(cmd->err); argv_array_clear(&cmd->args); + argv_array_clear(&cmd->env_array); errno = failed_errno; return -1; } @@@ -555,7 -544,6 +555,7 @@@ int finish_command(struct child_proces { int ret = wait_or_whine(cmd->pid, cmd->argv[0]); argv_array_clear(&cmd->args); + argv_array_clear(&cmd->env_array); return ret; } @@@ -567,21 -555,31 +567,21 @@@ int run_command(struct child_process *c return finish_command(cmd); } -static void prepare_run_command_v_opt(struct child_process *cmd, - const char **argv, - int opt) -{ - memset(cmd, 0, sizeof(*cmd)); - cmd->argv = argv; - cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; - cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0; - cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; - cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; - cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0; - cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; -} - int run_command_v_opt(const char **argv, int opt) { - struct child_process cmd; - prepare_run_command_v_opt(&cmd, argv, opt); - return run_command(&cmd); + return run_command_v_opt_cd_env(argv, opt, NULL, NULL); } int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) { - struct child_process cmd; - prepare_run_command_v_opt(&cmd, argv, opt); + struct child_process cmd = CHILD_PROCESS_INIT; + cmd.argv = argv; + cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; + cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0; + cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; + cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; + cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0; + cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; cmd.dir = dir; cmd.env = env; return run_command(&cmd); @@@ -626,45 -624,6 +626,45 @@@ static int async_die_is_recursing(void return ret != NULL; } +#else + +static struct { + void (**handlers)(void); + size_t nr; + size_t alloc; +} git_atexit_hdlrs; + +static int git_atexit_installed; + +static void git_atexit_dispatch(void) +{ + size_t i; + + for (i=git_atexit_hdlrs.nr ; i ; i--) + git_atexit_hdlrs.handlers[i-1](); +} + +static void git_atexit_clear(void) +{ + free(git_atexit_hdlrs.handlers); + memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs)); + git_atexit_installed = 0; +} + +#undef atexit +int git_atexit(void (*handler)(void)) +{ + ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc); + git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler; + if (!git_atexit_installed) { + if (atexit(&git_atexit_dispatch)) + return -1; + git_atexit_installed = 1; + } + return 0; +} +#define atexit git_atexit + #endif int start_async(struct async *async) @@@ -723,7 -682,6 +723,7 @@@ close(fdin[1]); if (need_out) close(fdout[0]); + git_atexit_clear(); exit(!!async->proc(proc_in, proc_out, async->data)); } @@@ -805,13 -763,14 +805,13 @@@ char *find_hook(const char *name int run_hook_ve(const char *const *env, const char *name, va_list args) { - struct child_process hook; + struct child_process hook = CHILD_PROCESS_INIT; const char *p; p = find_hook(name); if (!p) return 0; - memset(&hook, 0, sizeof(hook)); argv_array_push(&hook.args, p); while ((p = va_arg(args, const char *))) argv_array_push(&hook.args, p); @@@ -833,20 -792,3 +833,3 @@@ int run_hook_le(const char *const *env return ret; } - - int run_hook_with_custom_index(const char *index_file, const char *name, ...) - { - const char *hook_env[3] = { NULL }; - char index[PATH_MAX]; - va_list args; - int ret; - - snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); - hook_env[0] = index; - - va_start(args, name); - ret = run_hook_ve(hook_env, name, args); - va_end(args); - - return ret; - } diff --combined run-command.h index 2137315ee4,9e8cd9c4bf..d6868dc8c8 --- a/run-command.h +++ b/run-command.h @@@ -10,7 -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: @@@ -45,9 -44,6 +45,9 @@@ unsigned clean_on_exit:1; }; +#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT } +void child_process_init(struct child_process *); + int start_command(struct child_process *); int finish_command(struct child_process *); int run_command(struct child_process *); @@@ -57,10 -53,6 +57,6 @@@ LAST_ARG_MUST_BE_NUL extern int run_hook_le(const char *const *env, const char *name, ...); extern int run_hook_ve(const char *const *env, const char *name, va_list args); - LAST_ARG_MUST_BE_NULL - __attribute__((deprecated)) - extern int run_hook_with_custom_index(const char *index_file, const char *name, ...); - #define RUN_COMMAND_NO_STDIN 1 #define RUN_GIT_CMD 2 /*If this is to be git sub-command */ #define RUN_COMMAND_STDOUT_TO_STDERR 4