79
return error("hook fork failed");
80
case -ERR_RUN_COMMAND_EXEC:
81
return error("hook execute failed");
82
+ case -ERR_RUN_COMMAND_PIPE:
83
+ return error("hook pipe failed");
84
case -ERR_RUN_COMMAND_WAITPID:
85
return error("waitpid failed");
86
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
95
}
96
}
97
96
-static int run_hook(const char *hook_name,
97
- struct command *first_cmd,
98
- int single)
98
+static int run_hook(const char *hook_name)
99
{
100
+ static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
101
struct command *cmd;
101
- int argc, code;
102
- const char **argv;
102
+ struct child_process proc;
103
+ const char *argv[2];
104
+ int have_input = 0, code;
105
104
- for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
106
+ for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
107
if (!cmd->error_string)
106
- argc += 3;
107
- if (single)
108
- break;
108
+ have_input = 1;
109
}
110
111
- if (!argc || access(hook_name, X_OK) < 0)
111
+ if (!have_input || access(hook_name, X_OK) < 0)
112
return 0;
113
114
- argv = xmalloc(sizeof(*argv) * (2 + argc));
114
argv[0] = hook_name;
116
- for (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {
115
+ argv[1] = NULL;
116
+
117
+ memset(&proc, 0, sizeof(proc));
118
+ proc.argv = argv;
119
+ proc.in = -1;
120
+ proc.stdout_to_stderr = 1;
121
+
122
+ code = start_command(&proc);
123
+ if (code)
124
+ return hook_status(code, hook_name);
125
+ for (cmd = commands; cmd; cmd = cmd->next) {
126
if (!cmd->error_string) {
118
- argv[argc++] = xstrdup(cmd->ref_name);
119
- argv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));
120
- argv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));
127
+ size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
128
+ sha1_to_hex(cmd->old_sha1),
129
+ sha1_to_hex(cmd->new_sha1),
130
+ cmd->ref_name);
131
+ if (write_in_full(proc.in, buf, n) != n)
132
+ break;
133
}
122
- if (single)
123
- break;
134
}
125
- argv[argc] = NULL;
126
-
127
- code = run_command_v_opt(argv,
128
- RUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);
129
- while (--argc > 0)
130
- free((char*)argv[argc]);
131
- free(argv);
132
-
133
- return hook_status(code, hook_name);
135
+ return hook_status(finish_command(&proc), hook_name);
136
}
137
138
static int run_update_hook(struct command *cmd)
267
return;
268
}
269
268
- if (run_hook(pre_receive_hook, commands, 0)) {
270
+ if (run_hook(pre_receive_hook)) {
271
while (cmd) {
272
cmd->error_string = "pre-receive hook declined";
273
cmd = cmd->next;
522
unlink(pack_lockfile);
523
if (report_status)
524
report(unpack_status);
523
- run_hook(post_receive_hook, commands, 0);
525
+ run_hook(post_receive_hook);
526
run_update_post_hook(commands);
527
}
528
return 0;