Skip to content

Commit 917e080

Browse files
nasamuffingitster
authored andcommitted
hook API: support passing stdin to hooks, convert am's 'post-rewrite'
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 <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5402673 commit 917e080

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

builtin/am.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -495,24 +495,12 @@ static int run_applypatch_msg_hook(struct am_state *state)
495495
*/
496496
static int run_post_rewrite_hook(const struct am_state *state)
497497
{
498-
struct child_process cp = CHILD_PROCESS_INIT;
499-
const char *hook = find_hook("post-rewrite");
500-
int ret;
501-
502-
if (!hook)
503-
return 0;
498+
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
504499

505-
strvec_push(&cp.args, hook);
506-
strvec_push(&cp.args, "rebase");
500+
strvec_push(&opt.args, "rebase");
501+
opt.path_to_stdin = am_path(state, "rewritten");
507502

508-
cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
509-
cp.stdout_to_stderr = 1;
510-
cp.trace2_hook_name = "post-rewrite";
511-
512-
ret = run_command(&cp);
513-
514-
close(cp.in);
515-
return ret;
503+
return run_hooks_opt("post-rewrite", &opt);
516504
}
517505

518506
/**

hook.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ static int pick_next_hook(struct child_process *cp,
5555

5656
cp->no_stdin = 1;
5757
strvec_pushv(&cp->env, hook_cb->options->env.v);
58+
/* reopen the file for stdin; run_command closes it. */
59+
if (hook_cb->options->path_to_stdin) {
60+
cp->no_stdin = 0;
61+
cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY);
62+
}
5863
cp->stdout_to_stderr = 1;
5964
cp->trace2_hook_name = hook_cb->hook_name;
6065
cp->dir = hook_cb->options->dir;

hook.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ struct run_hooks_opt
3030
* was invoked.
3131
*/
3232
int *invoked_hook;
33+
34+
/**
35+
* Path to file which should be piped to stdin for each hook.
36+
*/
37+
const char *path_to_stdin;
3338
};
3439

3540
#define RUN_HOOKS_OPT_INIT { \

0 commit comments

Comments
 (0)