Skip to content

Commit c549a40

Browse files
10ne1gitster
authored andcommitted
hook: add jobs option
Allow the API callers to specify the number of jobs across which hook execution can be parallelized. It defaults to 1 and no hook currently changes it, so all hooks run sequentially as before. This allows us to both pave the way for parallel hook execution (that will be a follow-up patch series building upon this) and to finish the API conversion of builtin/receive-pack.c, keeping the output async sideband thread ("muxer") design as Peff suggested. When .jobs==1 nothing changes, the "copy_to_sideband" async thread still outputs directly via sideband channel 2, keeping the current (mostly) real-time output characteristics, avoids unnecessary poll delays or deadlock risks. When .jobs > 1, a more complex muxer is needed to buffer the hook output and avoid interleaving. After working on this mux I quickly realized I was re-implementing run-command with ungroup=0 so that idea was dropped in favor of run-command which outputs to stderr. In other words, run-command itself already can buffer/deinterleave pp child outputs (ungroup=0), so we can just connect its stderr to the sideband async task when jobs > 1. Maybe it helps to illustrate how it works with ascii graphics: [ Sequential (jobs = 1) ] [ Parallel (jobs > 1) ] +--------------+ +--------+ +--------+ | Hook Process | | Hook 1 | | Hook 2 | +--------------+ +--------+ +--------+ | | | | stderr (inherited) | stderr pipe | | | (captured) | v v v +-------------------------------------------------------------+ | Parent Process | | | | (direct write) [run-command (buffered)] | | | | | | | | writes | | v v | | +-------------------------------------------+ | | | stderr (FD 2) | | | +-------------------------------------------+ | | | | | | (dup2'd to pipe) | | v | | +-----------------------+ | | | sideband async thread | | | +-----------------------+ | +-------------------------------------------------------------+ When use_sideband == 0, the sideband async thread is missing, so this same architecture just outputs via the parent stderr stream. See the following commits for the hook API conversions doing this, using pre-existing sideband thread logic from `copy_to_sideband`. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b7b2157 commit c549a40

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

hook.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
152152
.tr2_category = "hook",
153153
.tr2_label = hook_name,
154154

155-
.processes = 1,
156-
.ungroup = 1,
155+
.processes = options->jobs,
156+
.ungroup = options->jobs == 1,
157157

158158
.get_next_task = pick_next_hook,
159159
.start_failure = notify_start_failure,
@@ -169,6 +169,9 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
169169
if (options->path_to_stdin && options->feed_pipe)
170170
BUG("options path_to_stdin and feed_pipe are mutually exclusive");
171171

172+
if (!options->jobs)
173+
BUG("run_hooks_opt must be called with options.jobs >= 1");
174+
172175
if (options->invoked_hook)
173176
*options->invoked_hook = 0;
174177

hook.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ struct run_hooks_opt
1616
/* Emit an error if the hook is missing */
1717
unsigned int error_if_missing:1;
1818

19+
/**
20+
* Number of processes to parallelize across.
21+
*
22+
* If > 1, output will be buffered and de-interleaved (ungroup=0).
23+
* If == 1, output will be real-time (ungroup=1).
24+
*/
25+
unsigned int jobs;
26+
1927
/**
2028
* An optional initial working directory for the hook,
2129
* translates to "struct child_process"'s "dir" member.
@@ -90,6 +98,7 @@ struct run_hooks_opt
9098
.env = STRVEC_INIT, \
9199
.args = STRVEC_INIT, \
92100
.stdout_to_stderr = 1, \
101+
.jobs = 1, \
93102
}
94103

95104
struct hook_cb_data {

0 commit comments

Comments
 (0)