git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Emily Shaffer" <emilyshaffer@google.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Felipe Contreras" <felipe.contreras@gmail.com>,
	"Taylor Blau" <me@ttaylorr.com>,
	"Michael Strawbridge" <michael.strawbridge@amd.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 3/5] hook API: support passing stdin to hooks, convert am's 'post-rewrite'
Date: Mon, 23 Jan 2023 18:15:07 +0100	[thread overview]
Message-ID: <patch-3.5-c6b9b69c516-20230123T170551Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-0.5-00000000000-20230123T170550Z-avarab@gmail.com>

From: Emily Shaffer <emilyshaffer@google.com>

Convert the invocation of the 'post-rewrite' hook run by 'git am' to
use the hook.h library. To do this we need to add a "path_to_stdin"
member to "struct run_hooks_opt".

In our API this is supported by asking for a file path, rather
than by reading stdin. Reading directly from stdin would involve caching
the entire stdin (to memory or to disk) once the hook API is made to
support "jobs" larger than 1, along with support for executing N hooks
at a time (i.e. the upcoming config-based hooks).

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/am.c | 20 ++++----------------
 hook.c       |  8 +++++++-
 hook.h       |  5 +++++
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 82a41cbfc4e..8be91617fef 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -495,24 +495,12 @@ static int run_applypatch_msg_hook(struct am_state *state)
  */
 static int run_post_rewrite_hook(const struct am_state *state)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-	const char *hook = find_hook("post-rewrite");
-	int ret;
-
-	if (!hook)
-		return 0;
+	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
 
-	strvec_push(&cp.args, hook);
-	strvec_push(&cp.args, "rebase");
+	strvec_push(&opt.args, "rebase");
+	opt.path_to_stdin = am_path(state, "rewritten");
 
-	cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
-	cp.stdout_to_stderr = 1;
-	cp.trace2_hook_name = "post-rewrite";
-
-	ret = run_command(&cp);
-
-	close(cp.in);
-	return ret;
+	return run_hooks_opt("post-rewrite", &opt);
 }
 
 /**
diff --git a/hook.c b/hook.c
index a4fa1031f28..86c6dc1fe70 100644
--- a/hook.c
+++ b/hook.c
@@ -53,8 +53,14 @@ static int pick_next_hook(struct child_process *cp,
 	if (!hook_path)
 		return 0;
 
-	cp->no_stdin = 1;
 	strvec_pushv(&cp->env, hook_cb->options->env.v);
+	/* reopen the file for stdin; run_command closes it. */
+	if (hook_cb->options->path_to_stdin) {
+		cp->no_stdin = 0;
+		cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY);
+	} else {
+		cp->no_stdin = 1;
+	}
 	cp->stdout_to_stderr = 1;
 	cp->trace2_hook_name = hook_cb->hook_name;
 	cp->dir = hook_cb->options->dir;
diff --git a/hook.h b/hook.h
index 4258b13da0d..19ab9a5806e 100644
--- a/hook.h
+++ b/hook.h
@@ -30,6 +30,11 @@ struct run_hooks_opt
 	 * was invoked.
 	 */
 	int *invoked_hook;
+
+	/**
+	 * Path to file which should be piped to stdin for each hook.
+	 */
+	const char *path_to_stdin;
 };
 
 #define RUN_HOOKS_OPT_INIT { \
-- 
2.39.1.1301.gffb37c08dee


  parent reply	other threads:[~2023-01-23 17:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-23 17:15 [PATCH 0/5] hook API: support stdin, convert post-rewrite Ævar Arnfjörð Bjarmason
2023-01-23 17:15 ` [PATCH 1/5] run-command.c: remove dead assignment in while-loop Ævar Arnfjörð Bjarmason
2023-01-23 22:48   ` Junio C Hamano
2023-01-23 17:15 ` [PATCH 2/5] run-command: allow stdin for run_processes_parallel Ævar Arnfjörð Bjarmason
2023-01-23 22:52   ` Junio C Hamano
2023-01-23 17:15 ` Ævar Arnfjörð Bjarmason [this message]
2023-01-23 23:10   ` [PATCH 3/5] hook API: support passing stdin to hooks, convert am's 'post-rewrite' Junio C Hamano
2023-01-23 23:13   ` Junio C Hamano
2023-01-23 17:15 ` [PATCH 4/5] sequencer: use the new hook API for the simpler "post-rewrite" call Ævar Arnfjörð Bjarmason
2023-01-24 14:46   ` Phillip Wood
2023-01-27 15:08     ` Phillip Wood
2023-01-23 17:15 ` [PATCH 5/5] hook: support a --to-stdin=<path> option for testing Ævar Arnfjörð Bjarmason
2023-01-24  0:55   ` Junio C Hamano
2023-01-24  0:59     ` Michael Strawbridge
2023-02-08 19:21 ` [PATCH v2 0/5] hook API: support stdin, convert post-rewrite Ævar Arnfjörð Bjarmason
2023-02-08 19:21   ` [PATCH v2 1/5] run-command.c: remove dead assignment in while-loop Ævar Arnfjörð Bjarmason
2023-02-08 21:03     ` Junio C Hamano
2023-02-08 19:21   ` [PATCH v2 2/5] run-command: allow stdin for run_processes_parallel Ævar Arnfjörð Bjarmason
2023-02-08 21:09     ` Junio C Hamano
2023-02-08 19:21   ` [PATCH v2 3/5] hook API: support passing stdin to hooks, convert am's 'post-rewrite' Ævar Arnfjörð Bjarmason
2023-02-08 21:12     ` Junio C Hamano
2023-02-08 19:21   ` [PATCH v2 4/5] sequencer: use the new hook API for the simpler "post-rewrite" call Ævar Arnfjörð Bjarmason
2023-02-08 21:17     ` Junio C Hamano
2023-02-08 19:21   ` [PATCH v2 5/5] hook: support a --to-stdin=<path> option Ævar Arnfjörð Bjarmason
2023-02-08 21:22     ` Junio C Hamano
2023-02-09  1:56       ` Ævar Arnfjörð Bjarmason
2023-02-08 21:23   ` [PATCH v2 0/5] hook API: support stdin, convert post-rewrite Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=patch-3.5-c6b9b69c516-20230123T170551Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    --cc=michael.strawbridge@amd.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).