Skip to content

Commit 1fc9744

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 2b45cf1 commit 1fc9744

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
@@ -30,3 +30,9 @@ hook.jobs::
3030
+
3131
This has no effect for hooks requiring separate output streams (like `pre-push`)
3232
unless `extensions.hookStdoutToStderr` is enabled.
33+
34+
hook.forceStdoutToStderr::
35+
A boolean that enables the `extensions.hookStdoutToStderr` behavior
36+
(merging stdout to stderr for all hooks) globally. This effectively
37+
forces all hooks to behave as if the extension was enabled, allowing
38+
parallel execution for hooks like `pre-push`.

hook.c

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

481+
/*
482+
* Allow hook.forceStdoutToStderr to enable extensions.hookStdoutToStderr
483+
* for existing repositories (runtime override).
484+
*/
485+
if (!options->stdout_to_stderr) {
486+
int v = 0;
487+
repo_config_get_bool(r, "hook.forceStdoutToStderr", &v);
488+
options->stdout_to_stderr = v;
489+
}
490+
481491
/*
482492
* Hooks which configure stdout_to_stderr=0 (like pre-push), expect separate
483493
* 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
@@ -735,4 +735,78 @@ test_expect_success 'hook.jobs=2 config runs hooks in parallel' '
735735
test $duration -lt 4
736736
'
737737

738+
test_expect_success '`git init` respects hook.forceStdoutToStderr' '
739+
test_when_finished "rm -rf repo-init" &&
740+
test_config_global hook.forceStdoutToStderr true &&
741+
git init repo-init &&
742+
git -C repo-init config extensions.hookStdoutToStderr >actual &&
743+
echo true >expect &&
744+
test_cmp expect actual
745+
'
746+
747+
test_expect_success '`git init` does not set extensions.hookStdoutToStderr by default' '
748+
test_when_finished "rm -rf upstream" &&
749+
git init upstream &&
750+
test_must_fail git -C upstream config extensions.hookStdoutToStderr
751+
'
752+
753+
test_expect_success '`git clone` does not set extensions.hookStdoutToStderr by default' '
754+
test_when_finished "rm -rf upstream repo-clone-no-ext" &&
755+
git init upstream &&
756+
git clone upstream repo-clone-no-ext &&
757+
test_must_fail git -C repo-clone-no-ext config extensions.hookStdoutToStderr
758+
'
759+
760+
test_expect_success '`git clone` respects hook.forceStdoutToStderr' '
761+
test_when_finished "rm -rf upstream repo-clone" &&
762+
git init upstream &&
763+
test_config_global hook.forceStdoutToStderr true &&
764+
git clone upstream repo-clone &&
765+
git -C repo-clone config extensions.hookStdoutToStderr >actual &&
766+
echo true >expect &&
767+
test_cmp expect actual
768+
'
769+
770+
test_expect_success 'hook.forceStdoutToStderr enables extension for existing repos' '
771+
test_when_finished "rm -rf remote-repo existing-repo" &&
772+
git init --bare remote-repo &&
773+
git init -b main existing-repo &&
774+
# No local extensions.hookStdoutToStderr config set here
775+
# so global config should apply
776+
test_config_global hook.forceStdoutToStderr true &&
777+
cd existing-repo &&
778+
test_commit A &&
779+
git remote add origin ../remote-repo &&
780+
setup_hooks pre-push &&
781+
git push origin HEAD >stdout.actual 2>stderr.actual &&
782+
check_stdout_merged_to_stderr pre-push &&
783+
cd ..
784+
'
785+
786+
test_expect_success 'hook.forceStdoutToStderr enables pre-push parallel runs' '
787+
test_when_finished "rm -rf repo-parallel" &&
788+
git init --bare remote-parallel &&
789+
git init repo-parallel &&
790+
git -C repo-parallel remote add origin ../remote-parallel &&
791+
test_commit -C repo-parallel A &&
792+
793+
write_script repo-parallel/.git/hooks/pre-push <<-EOF &&
794+
sleep 2
795+
echo "Hook 1" >&2
796+
EOF
797+
git -C repo-parallel config hook.hook-2.event pre-push &&
798+
git -C repo-parallel config hook.hook-2.command "sleep 2; echo Hook 2 >&2" &&
799+
800+
git -C repo-parallel config hook.jobs 2 &&
801+
git -C repo-parallel config hook.forceStdoutToStderr true &&
802+
803+
start=$(date +%s) &&
804+
git -C repo-parallel push origin HEAD >out 2>err &&
805+
end=$(date +%s) &&
806+
807+
duration=$((end - start)) &&
808+
# Serial >= 4s, parallel < 4s.
809+
test $duration -lt 4
810+
'
811+
738812
test_done

0 commit comments

Comments
 (0)