Skip to content

Commit 3e487c3

Browse files
10ne1gitster
authored andcommitted
hook: allow runtime enabling extensions.hookStdoutToStderr
Add a new config `hook.forceStdoutToStderr` which allows enabling extensions.hookStdoutToStderr by default at runtime, both for new and existing repositories. This makes it easier for users to enable hook parallelization for hooks like pre-push by enforcing output consistency. See previous commit for a more in-depth explanation & alternatives considered. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 187968b commit 3e487c3

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

Documentation/config/extensions.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ in parallel to be grouped (de-interleaved) correctly.
8484
+
8585
Defaults to disabled. When disabled, `hook.jobs` has no effect for pre-push
8686
hooks, which will always be run sequentially.
87+
+
88+
The extension can also be enabled by setting `hook.forceStdoutToStderr`
89+
to `true` in the global configuration.
8790
8891
worktreeConfig:::
8992
If enabled, then worktrees will load config settings from the

Documentation/config/hook.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ hook.jobs::
2323
+
2424
This has no effect for hooks requiring separate output streams (like `pre-push`)
2525
unless `extensions.hookStdoutToStderr` is enabled.
26+
27+
hook.forceStdoutToStderr::
28+
A boolean that enables the `extensions.hookStdoutToStderr` behavior
29+
(merging stdout to stderr for all hooks) globally. This effectively
30+
forces all hooks to behave as if the extension was enabled, allowing
31+
parallel execution for hooks like `pre-push`.

hook.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ static unsigned int get_hook_jobs(struct repository *r, struct run_hooks_opt *op
264264
{
265265
unsigned int jobs = options->jobs;
266266

267+
/*
268+
* Allow hook.forceStdoutToStderr to enable extensions.hookStdoutToStderr
269+
* for existing repositories (runtime override).
270+
*/
271+
if (!options->stdout_to_stderr) {
272+
int v = 0;
273+
repo_config_get_bool(r, "hook.forceStdoutToStderr", &v);
274+
options->stdout_to_stderr = v;
275+
}
276+
267277
/*
268278
* Hooks which configure stdout_to_stderr=0 (like pre-push), expect separate
269279
* output streams. Unless extensions.StdoutToStderr is enabled (which forces

setup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,7 @@ void initialize_repository_version(int hash_algo,
23102310
{
23112311
struct strbuf repo_version = STRBUF_INIT;
23122312
int target_version = GIT_REPO_VERSION;
2313+
int default_hook_stdout_to_stderr = 0;
23132314

23142315
/*
23152316
* Note that we initialize the repository version to 1 when the ref
@@ -2348,6 +2349,15 @@ void initialize_repository_version(int hash_algo,
23482349
clear_repository_format(&repo_fmt);
23492350
}
23502351

2352+
repo_config_get_bool(the_repository, "hook.forceStdoutToStderr",
2353+
&default_hook_stdout_to_stderr);
2354+
if (default_hook_stdout_to_stderr) {
2355+
/* extensions.hookstdouttostderr requires at least version 1 */
2356+
if (target_version == 0)
2357+
target_version = 1;
2358+
repo_config_set(the_repository, "extensions.hookstdouttostderr", "true");
2359+
}
2360+
23512361
strbuf_addf(&repo_version, "%d", target_version);
23522362
repo_config_set(the_repository, "core.repositoryformatversion", repo_version.buf);
23532363

t/t1800-hook.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,4 +649,78 @@ test_expect_success 'hook.jobs=2 config runs hooks in parallel' '
649649
test $duration -lt 4
650650
'
651651

652+
test_expect_success '`git init` respects hook.forceStdoutToStderr' '
653+
test_when_finished "rm -rf repo-init" &&
654+
test_config_global hook.forceStdoutToStderr true &&
655+
git init repo-init &&
656+
git -C repo-init config extensions.hookStdoutToStderr >actual &&
657+
echo true >expect &&
658+
test_cmp expect actual
659+
'
660+
661+
test_expect_success '`git init` does not set extensions.hookStdoutToStderr by default' '
662+
test_when_finished "rm -rf upstream" &&
663+
git init upstream &&
664+
test_must_fail git -C upstream config extensions.hookStdoutToStderr
665+
'
666+
667+
test_expect_success '`git clone` does not set extensions.hookStdoutToStderr by default' '
668+
test_when_finished "rm -rf upstream repo-clone-no-ext" &&
669+
git init upstream &&
670+
git clone upstream repo-clone-no-ext &&
671+
test_must_fail git -C repo-clone-no-ext config extensions.hookStdoutToStderr
672+
'
673+
674+
test_expect_success '`git clone` respects hook.forceStdoutToStderr' '
675+
test_when_finished "rm -rf upstream repo-clone" &&
676+
git init upstream &&
677+
test_config_global hook.forceStdoutToStderr true &&
678+
git clone upstream repo-clone &&
679+
git -C repo-clone config extensions.hookStdoutToStderr >actual &&
680+
echo true >expect &&
681+
test_cmp expect actual
682+
'
683+
684+
test_expect_success 'hook.forceStdoutToStderr enables extension for existing repos' '
685+
test_when_finished "rm -rf remote-repo existing-repo" &&
686+
git init --bare remote-repo &&
687+
git init -b main existing-repo &&
688+
# No local extensions.hookStdoutToStderr config set here
689+
# so global config should apply
690+
test_config_global hook.forceStdoutToStderr true &&
691+
cd existing-repo &&
692+
test_commit A &&
693+
git remote add origin ../remote-repo &&
694+
setup_hooks pre-push &&
695+
git push origin HEAD >stdout.actual 2>stderr.actual &&
696+
check_stdout_merged_to_stderr pre-push &&
697+
cd ..
698+
'
699+
700+
test_expect_success 'hook.forceStdoutToStderr enables pre-push parallel runs' '
701+
test_when_finished "rm -rf repo-parallel" &&
702+
git init --bare remote-parallel &&
703+
git init repo-parallel &&
704+
git -C repo-parallel remote add origin ../remote-parallel &&
705+
test_commit -C repo-parallel A &&
706+
707+
write_script repo-parallel/.git/hooks/pre-push <<-EOF &&
708+
sleep 2
709+
echo "Hook 1" >&2
710+
EOF
711+
git -C repo-parallel config hook.hook-2.event pre-push &&
712+
git -C repo-parallel config hook.hook-2.command "sleep 2; echo Hook 2 >&2" &&
713+
714+
git -C repo-parallel config hook.jobs 2 &&
715+
git -C repo-parallel config hook.forceStdoutToStderr true &&
716+
717+
start=$(date +%s) &&
718+
git -C repo-parallel push origin HEAD >out 2>err &&
719+
end=$(date +%s) &&
720+
721+
duration=$((end - start)) &&
722+
# Serial >= 4s, parallel < 4s.
723+
test $duration -lt 4
724+
'
725+
652726
test_done

0 commit comments

Comments
 (0)