]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hook: convert 'post-rewrite' hook in sequencer.c to hook API
authorEmily Shaffer <emilyshaffer@google.com>
Thu, 18 Dec 2025 17:11:18 +0000 (19:11 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Dec 2025 04:46:26 +0000 (13:46 +0900)
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>
sequencer.c

index 5476d39ba9b0970d6a8d1ef80ece8f11b88279b1..71ed31c774068862104efe5faaa1eb02870861e6 100644 (file)
@@ -1292,32 +1292,40 @@ int update_head_with_reflog(const struct commit *old_head,
        return ret;
 }
 
+static int pipe_from_strbuf(int hook_stdin_fd, void *pp_cb, void *pp_task_cb UNUSED)
+{
+       struct hook_cb_data *hook_cb = pp_cb;
+       struct strbuf *to_pipe = hook_cb->options->feed_pipe_ctx;
+       int ret;
+
+       if (!to_pipe)
+               BUG("pipe_from_strbuf called without feed_pipe_ctx");
+
+       ret = write_in_full(hook_stdin_fd, to_pipe->buf, to_pipe->len);
+       if (ret < 0 && errno != EPIPE)
+               return ret;
+
+       return 1; /* done writing */
+}
+
 static int run_rewrite_hook(const struct object_id *oldoid,
                            const struct object_id *newoid)
 {
-       struct child_process proc = CHILD_PROCESS_INIT;
+       struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
        int code;
        struct strbuf sb = STRBUF_INIT;
-       const char *hook_path = find_hook(the_repository, "post-rewrite");
 
-       if (!hook_path)
-               return 0;
+       strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
 
-       strvec_pushl(&proc.args, hook_path, "amend", NULL);
-       proc.in = -1;
-       proc.stdout_to_stderr = 1;
-       proc.trace2_hook_name = "post-rewrite";
+       opt.feed_pipe_ctx = &sb;
+       opt.feed_pipe = pipe_from_strbuf;
+
+       strvec_push(&opt.args, "amend");
+
+       code = run_hooks_opt(the_repository, "post-rewrite", &opt);
 
-       code = start_command(&proc);
-       if (code)
-               return code;
-       strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
-       sigchain_push(SIGPIPE, SIG_IGN);
-       write_in_full(proc.in, sb.buf, sb.len);
-       close(proc.in);
        strbuf_release(&sb);
-       sigchain_pop(SIGPIPE);
-       return finish_command(&proc);
+       return code;
 }
 
 void commit_post_rewrite(struct repository *r,