Skip to content

Commit 877b7bb

Browse files
nasamuffingitster
authored andcommitted
hook: convert 'post-rewrite' hook in sequencer.c to hook API
Replace the custom run-command calls used by post-rewrite with the newer and simpler hook_run_opt(), which does not need to create a custom 'struct child_process' or call find_hook(). Another benefit of using the hook API is that hook_run_opt() handles the SIGPIPE toggle logic. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bc0afba commit 877b7bb

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

sequencer.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,32 +1292,40 @@ int update_head_with_reflog(const struct commit *old_head,
12921292
return ret;
12931293
}
12941294

1295+
static int pipe_from_strbuf(int hook_stdin_fd, void *pp_cb, void *pp_task_cb UNUSED)
1296+
{
1297+
struct hook_cb_data *hook_cb = pp_cb;
1298+
struct strbuf *to_pipe = hook_cb->options->feed_pipe_ctx;
1299+
int ret;
1300+
1301+
if (!to_pipe)
1302+
BUG("pipe_from_strbuf called without feed_pipe_ctx");
1303+
1304+
ret = write_in_full(hook_stdin_fd, to_pipe->buf, to_pipe->len);
1305+
if (ret < 0 && errno != EPIPE)
1306+
return ret;
1307+
1308+
return 1; /* done writing */
1309+
}
1310+
12951311
static int run_rewrite_hook(const struct object_id *oldoid,
12961312
const struct object_id *newoid)
12971313
{
1298-
struct child_process proc = CHILD_PROCESS_INIT;
1314+
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
12991315
int code;
13001316
struct strbuf sb = STRBUF_INIT;
1301-
const char *hook_path = find_hook(the_repository, "post-rewrite");
13021317

1303-
if (!hook_path)
1304-
return 0;
1318+
strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
13051319

1306-
strvec_pushl(&proc.args, hook_path, "amend", NULL);
1307-
proc.in = -1;
1308-
proc.stdout_to_stderr = 1;
1309-
proc.trace2_hook_name = "post-rewrite";
1320+
opt.feed_pipe_ctx = &sb;
1321+
opt.feed_pipe = pipe_from_strbuf;
1322+
1323+
strvec_push(&opt.args, "amend");
1324+
1325+
code = run_hooks_opt(the_repository, "post-rewrite", &opt);
13101326

1311-
code = start_command(&proc);
1312-
if (code)
1313-
return code;
1314-
strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
1315-
sigchain_push(SIGPIPE, SIG_IGN);
1316-
write_in_full(proc.in, sb.buf, sb.len);
1317-
close(proc.in);
13181327
strbuf_release(&sb);
1319-
sigchain_pop(SIGPIPE);
1320-
return finish_command(&proc);
1328+
return code;
13211329
}
13221330

13231331
void commit_post_rewrite(struct repository *r,

0 commit comments

Comments
 (0)