Skip to content

Commit 5ab5872

Browse files
nasamuffingitster
authored andcommitted
run-command: allow capturing of collated output
Some callers, for example server-side hooks which wish to relay hook output to clients across a transport, want to capture what would normally print to stderr and do something else with it. Allow that via a callback. By calling the callback regardless of whether there's output available, we allow clients to send e.g. a keepalive if necessary. Because we expose a strbuf, not a fd or FILE*, there's no need to create a temporary pipe or similar - we can just skip the print to stderr and instead hand it to the caller. 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 857f047 commit 5ab5872

4 files changed

Lines changed: 61 additions & 8 deletions

File tree

run-command.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,10 @@ static void pp_cleanup(struct parallel_processes *pp,
15951595
* When get_next_task added messages to the buffer in its last
15961596
* iteration, the buffered output is non empty.
15971597
*/
1598-
strbuf_write(&pp->buffered_output, stderr);
1598+
if (opts->consume_output)
1599+
opts->consume_output(&pp->buffered_output, opts->data);
1600+
else
1601+
strbuf_write(&pp->buffered_output, stderr);
15991602
strbuf_release(&pp->buffered_output);
16001603

16011604
sigchain_pop_common();
@@ -1734,13 +1737,17 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
17341737
}
17351738
}
17361739

1737-
static void pp_output(const struct parallel_processes *pp)
1740+
static void pp_output(const struct parallel_processes *pp,
1741+
const struct run_process_parallel_opts *opts)
17381742
{
17391743
size_t i = pp->output_owner;
17401744

17411745
if (child_is_working(&pp->children[i]) &&
17421746
pp->children[i].err.len) {
1743-
strbuf_write(&pp->children[i].err, stderr);
1747+
if (opts->consume_output)
1748+
opts->consume_output(&pp->children[i].err, opts->data);
1749+
else
1750+
strbuf_write(&pp->children[i].err, stderr);
17441751
strbuf_reset(&pp->children[i].err);
17451752
}
17461753
}
@@ -1788,11 +1795,15 @@ static int pp_collect_finished(struct parallel_processes *pp,
17881795
} else {
17891796
const size_t n = opts->processes;
17901797

1791-
strbuf_write(&pp->children[i].err, stderr);
1798+
/* Output errors, then all other finished child processes */
1799+
if (opts->consume_output) {
1800+
opts->consume_output(&pp->children[i].err, opts->data);
1801+
opts->consume_output(&pp->buffered_output, opts->data);
1802+
} else {
1803+
strbuf_write(&pp->children[i].err, stderr);
1804+
strbuf_write(&pp->buffered_output, stderr);
1805+
}
17921806
strbuf_reset(&pp->children[i].err);
1793-
1794-
/* Output all other finished child processes */
1795-
strbuf_write(&pp->buffered_output, stderr);
17961807
strbuf_reset(&pp->buffered_output);
17971808

17981809
/*
@@ -1829,7 +1840,7 @@ static void pp_handle_child_IO(struct parallel_processes *pp,
18291840
pp->children[i].state = GIT_CP_WAIT_CLEANUP;
18301841
} else {
18311842
pp_buffer_stderr(pp, opts, output_timeout);
1832-
pp_output(pp);
1843+
pp_output(pp, opts);
18331844
}
18341845
}
18351846

@@ -1852,6 +1863,9 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
18521863
"max:%"PRIuMAX,
18531864
(uintmax_t)opts->processes);
18541865

1866+
if (opts->ungroup && opts->consume_output)
1867+
BUG("ungroup and reading output are mutualy exclusive");
1868+
18551869
/*
18561870
* Child tasks might receive input via stdin, terminating early (or not), so
18571871
* ignore the default SIGPIPE which gets handled by each feed_pipe_fn which

run-command.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,17 @@ typedef int (*feed_pipe_fn)(int child_in,
435435
void *pp_cb,
436436
void *pp_task_cb);
437437

438+
/**
439+
* If this callback is provided, output is collated into a new pipe instead
440+
* of the process stderr. Then `consume_output_fn` will be called repeatedly
441+
* with output contained in the `output` arg. It will also be called with an
442+
* empty `output` to allow for keepalives or similar operations if necessary.
443+
*
444+
* pp_cb is the callback cookie as passed into run_processes_parallel.
445+
* No task cookie is provided because the callback receives collated output.
446+
*/
447+
typedef void (*consume_output_fn)(struct strbuf *output, void *pp_cb);
448+
438449
/**
439450
* This callback is called on every child process that finished processing.
440451
*
@@ -494,6 +505,12 @@ struct run_process_parallel_opts
494505
*/
495506
feed_pipe_fn feed_pipe;
496507

508+
/*
509+
* consume_output: see consume_output_fn() above. This can be NULL
510+
* to omit any special handling.
511+
*/
512+
consume_output_fn consume_output;
513+
497514
/**
498515
* task_finished: See task_finished_fn() above. This can be
499516
* NULL to omit any special handling.

t/helper/test-run-command.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ static int no_job(struct child_process *cp UNUSED,
5858
return 0;
5959
}
6060

61+
static void test_divert_output(struct strbuf *output, void *cb UNUSED)
62+
{
63+
FILE *output_file;
64+
65+
output_file = fopen("./output_file", "a");
66+
67+
strbuf_write(output, output_file);
68+
fclose(output_file);
69+
}
70+
6171
static int task_finished(int result UNUSED,
6272
struct strbuf *err,
6373
void *pp_cb UNUSED,
@@ -198,6 +208,7 @@ static int testsuite(int argc, const char **argv)
198208
.get_next_task = next_test,
199209
.start_failure = test_failed,
200210
.feed_pipe = test_stdin_pipe_feed,
211+
.consume_output = test_divert_output,
201212
.task_finished = test_finished,
202213
.data = &suite,
203214
};
@@ -514,6 +525,10 @@ int cmd__run_command(int argc, const char **argv)
514525
opts.get_next_task = parallel_next;
515526
opts.task_finished = task_finished_quiet;
516527
opts.feed_pipe = test_stdin_pipe_feed;
528+
} else if (!strcmp(argv[1], "run-command-divert-output")) {
529+
opts.get_next_task = parallel_next;
530+
opts.consume_output = test_divert_output;
531+
opts.task_finished = task_finished_quiet;
517532
} else {
518533
ret = 1;
519534
fprintf(stderr, "check usage\n");

t/t0061-run-command.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ test_expect_success 'run_command runs ungrouped in parallel with more tasks than
164164
test_line_count = 4 err
165165
'
166166

167+
test_expect_success 'run_command can divert output' '
168+
test_when_finished rm output_file &&
169+
test-tool run-command run-command-divert-output 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
170+
test_must_be_empty actual &&
171+
test_cmp expect output_file
172+
'
173+
167174
test_expect_success 'run_command listens to stdin' '
168175
cat >expect <<-\EOF &&
169176
preloaded output of a child

0 commit comments

Comments
 (0)