run-command: add helper for pp child states

There is a recurring pattern of testing parallel process child states and file descriptors to determine if a child is running, receiving any input or if it's ready for cleanup. Name the pp_child structure and introduce a helper to make the checks more readable. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adrian Ratiu committed Jan 28, 2026 at 23:39 UTC dee82860688cdcd35cbf0ce7e74736e473c5b697
1 file changed +17 -10
run-command.c
+17 -10
@@ -1478,15 +1478,22 @@ enum child_state {
1478 GIT_CP_WAIT_CLEANUP,
1479 };
1480
1481 +struct parallel_child {
1482 + enum child_state state;
1483 + struct child_process process;
1484 + struct strbuf err;
1485 + void *data;
1486 +};
1487 +
1488 +static int child_is_working(const struct parallel_child *pp_child)
1489 +{
1490 + return pp_child->state == GIT_CP_WORKING;
1491 +}
1492 +
1493 struct parallel_processes {
1494 size_t nr_processes;
1495
1484 - struct {
1485 - enum child_state state;
1486 - struct child_process process;
1487 - struct strbuf err;
1488 - void *data;
1489 - } *children;
1496 + struct parallel_child *children;
1497 /*
1498 * The struct pollfd is logically part of *children,
1499 * but the system call expects it as its own array.
@@ -1509,7 +1516,7 @@ static void kill_children(const struct parallel_processes *pp,
1516 int signo)
1517 {
1518 for (size_t i = 0; i < opts->processes; i++)
1512 - if (pp->children[i].state == GIT_CP_WORKING)
1519 + if (child_is_working(&pp->children[i]))
1520 kill(pp->children[i].process.pid, signo);
1521 }
1522
@@ -1665,7 +1672,7 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
1672
1673 /* Buffer output from all pipes. */
1674 for (size_t i = 0; i < opts->processes; i++) {
1668 - if (pp->children[i].state == GIT_CP_WORKING &&
1675 + if (child_is_working(&pp->children[i]) &&
1676 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1677 int n = strbuf_read_once(&pp->children[i].err,
1678 pp->children[i].process.err, 0);
@@ -1683,7 +1690,7 @@ static void pp_output(const struct parallel_processes *pp)
1690 {
1691 size_t i = pp->output_owner;
1692
1686 - if (pp->children[i].state == GIT_CP_WORKING &&
1693 + if (child_is_working(&pp->children[i]) &&
1694 pp->children[i].err.len) {
1695 strbuf_write(&pp->children[i].err, stderr);
1696 strbuf_reset(&pp->children[i].err);
@@ -1748,7 +1755,7 @@ static int pp_collect_finished(struct parallel_processes *pp,
1755 * running process time.
1756 */
1757 for (i = 0; i < n; i++)
1751 - if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1758 + if (child_is_working(&pp->children[(pp->output_owner + i) % n]))
1759 break;
1760 pp->output_owner = (pp->output_owner + i) % n;
1761 }