Skip to content

Commit 863135c

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 8a101e3 commit 863135c

6 files changed

Lines changed: 115 additions & 2 deletions

File tree

Documentation/config/extensions.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ in parallel to be grouped (de-interleaved) correctly.
127127
+
128128
Defaults to disabled. When disabled, `hook.jobs` has no effect for pre-push
129129
hooks, which will always be run sequentially.
130+
+
131+
The extension can also be enabled by setting `hook.forceStdoutToStderr`
132+
to `true` in the global configuration.
130133
131134
worktreeConfig:::
132135
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
@@ -65,3 +65,9 @@ This setting has no effect unless all configured hooks for the event have
6565
+
6666
This has no effect for hooks requiring separate output streams (like `pre-push`)
6767
unless `extensions.hookStdoutToStderr` is enabled.
68+
69+
hook.forceStdoutToStderr::
70+
A boolean that enables the `extensions.hookStdoutToStderr` behavior
71+
(merging stdout to stderr for all hooks) globally. This effectively
72+
forces all hooks to behave as if the extension was enabled, allowing
73+
parallel execution for hooks like `pre-push`.

hook.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct hook_config_cache_entry {
132132
* event_jobs: event-name to per-event jobs count (heap-allocated unsigned int *,
133133
* where NULL == unset).
134134
* jobs: value of the global hook.jobs key. Defaults to 0 if unset.
135+
* force_stdout_to_stderr: value of hook.forceStdoutToStderr. Defaults to 0.
135136
*/
136137
struct hook_all_config_cb {
137138
struct strmap commands;
@@ -140,6 +141,7 @@ struct hook_all_config_cb {
140141
struct strmap parallel_hooks;
141142
struct strmap event_jobs;
142143
unsigned int jobs;
144+
int force_stdout_to_stderr;
143145
};
144146

145147
/* repo_config() callback that collects all hook.* configuration in one pass. */
@@ -165,6 +167,10 @@ static int hook_config_lookup_all(const char *key, const char *value,
165167
warning(_("hook.jobs must be positive, ignoring: 0"));
166168
else
167169
data->jobs = v;
170+
} else if (!strcmp(subkey, "forcestdouttostderr") && value) {
171+
int v = git_parse_maybe_bool(value);
172+
if (v >= 0)
173+
data->force_stdout_to_stderr = v;
168174
}
169175
return 0;
170176
}
@@ -344,6 +350,7 @@ static void build_hook_config_map(struct repository *r,
344350

345351
cache->jobs = cb_data.jobs;
346352
cache->event_jobs = cb_data.event_jobs;
353+
cache->force_stdout_to_stderr = cb_data.force_stdout_to_stderr;
347354

348355
strmap_clear(&cb_data.commands, 1);
349356
strmap_clear(&cb_data.parallel_hooks, 0); /* values are uintptr_t, not heap ptrs */
@@ -573,6 +580,20 @@ static void run_hooks_opt_clear(struct run_hooks_opt *options)
573580
strvec_clear(&options->args);
574581
}
575582

583+
static void hook_force_apply_stdout_to_stderr(struct repository *r,
584+
struct run_hooks_opt *options)
585+
{
586+
int force = 0;
587+
588+
if (r && r->gitdir && r->hook_config_cache)
589+
force = r->hook_config_cache->force_stdout_to_stderr;
590+
else
591+
repo_config_get_bool(r, "hook.forceStdoutToStderr", &force);
592+
593+
if (force)
594+
options->stdout_to_stderr = 1;
595+
}
596+
576597
/* Determine how many jobs to use for hook execution. */
577598
static unsigned int get_hook_jobs(struct repository *r,
578599
struct run_hooks_opt *options,
@@ -582,9 +603,12 @@ static unsigned int get_hook_jobs(struct repository *r,
582603
unsigned int jobs;
583604

584605
/*
585-
* Hooks needing separate output streams must run sequentially. Next
586-
* commits will add an extension to allow parallelizing these as well.
606+
* Apply hook.forceStdoutToStderr before anything else: it affects
607+
* whether we can run in parallel or not.
587608
*/
609+
hook_force_apply_stdout_to_stderr(r, options);
610+
611+
/* Hooks needing separate output streams must run sequentially. */
588612
if (!options->stdout_to_stderr)
589613
return 1;
590614

hook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ struct hook_config_cache {
235235
struct strmap hooks; /* maps event name -> string_list of hooks */
236236
struct strmap event_jobs; /* maps event name -> heap-allocated unsigned int * */
237237
unsigned int jobs; /* hook.jobs config value; 0 if unset (defaults to serial) */
238+
int force_stdout_to_stderr; /* hook.forceStdoutToStderr config value */
238239
};
239240

240241
/**

setup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,7 @@ void initialize_repository_version(int hash_algo,
23622362
struct strbuf repo_version = STRBUF_INIT;
23632363
int target_version = GIT_REPO_VERSION;
23642364
int default_submodule_path_config = 0;
2365+
int default_hook_stdout_to_stderr = 0;
23652366

23662367
/*
23672368
* Note that we initialize the repository version to 1 when the ref
@@ -2419,6 +2420,15 @@ void initialize_repository_version(int hash_algo,
24192420
repo_config_set(the_repository, "extensions.submodulepathconfig", "true");
24202421
}
24212422

2423+
repo_config_get_bool(the_repository, "hook.forceStdoutToStderr",
2424+
&default_hook_stdout_to_stderr);
2425+
if (default_hook_stdout_to_stderr) {
2426+
/* extensions.hookstdouttostderr requires at least version 1 */
2427+
if (target_version == 0)
2428+
target_version = 1;
2429+
repo_config_set(the_repository, "extensions.hookstdouttostderr", "true");
2430+
}
2431+
24222432
strbuf_addf(&repo_version, "%d", target_version);
24232433
repo_config_set(the_repository, "core.repositoryformatversion", repo_version.buf);
24242434

t/t1800-hook.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,4 +989,73 @@ test_expect_success 'hook.<event>.jobs still requires hook.<name>.parallel=true'
989989
test_cmp expect hook.order
990990
'
991991

992+
test_expect_success '`git init` respects hook.forceStdoutToStderr' '
993+
test_when_finished "rm -rf repo-init" &&
994+
test_config_global hook.forceStdoutToStderr true &&
995+
git init repo-init &&
996+
git -C repo-init config extensions.hookStdoutToStderr >actual &&
997+
echo true >expect &&
998+
test_cmp expect actual
999+
'
1000+
1001+
test_expect_success '`git init` does not set extensions.hookStdoutToStderr by default' '
1002+
test_when_finished "rm -rf upstream" &&
1003+
git init upstream &&
1004+
test_must_fail git -C upstream config extensions.hookStdoutToStderr
1005+
'
1006+
1007+
test_expect_success '`git clone` does not set extensions.hookStdoutToStderr by default' '
1008+
test_when_finished "rm -rf upstream repo-clone-no-ext" &&
1009+
git init upstream &&
1010+
git clone upstream repo-clone-no-ext &&
1011+
test_must_fail git -C repo-clone-no-ext config extensions.hookStdoutToStderr
1012+
'
1013+
1014+
test_expect_success '`git clone` respects hook.forceStdoutToStderr' '
1015+
test_when_finished "rm -rf upstream repo-clone" &&
1016+
git init upstream &&
1017+
test_config_global hook.forceStdoutToStderr true &&
1018+
git clone upstream repo-clone &&
1019+
git -C repo-clone config extensions.hookStdoutToStderr >actual &&
1020+
echo true >expect &&
1021+
test_cmp expect actual
1022+
'
1023+
1024+
test_expect_success 'hook.forceStdoutToStderr enables extension for existing repos' '
1025+
test_when_finished "rm -rf remote-repo existing-repo" &&
1026+
git init --bare remote-repo &&
1027+
git init -b main existing-repo &&
1028+
# No local extensions.hookStdoutToStderr config set here
1029+
# so global config should apply
1030+
test_config_global hook.forceStdoutToStderr true &&
1031+
cd existing-repo &&
1032+
test_commit A &&
1033+
git remote add origin ../remote-repo &&
1034+
setup_hooks pre-push &&
1035+
git push origin HEAD >stdout.actual 2>stderr.actual &&
1036+
check_stdout_merged_to_stderr pre-push &&
1037+
cd ..
1038+
'
1039+
1040+
test_expect_success 'hook.forceStdoutToStderr enables pre-push parallel runs' '
1041+
test_when_finished "rm -rf repo-parallel remote-parallel" &&
1042+
git init --bare remote-parallel &&
1043+
git init repo-parallel &&
1044+
git -C repo-parallel remote add origin ../remote-parallel &&
1045+
test_commit -C repo-parallel A &&
1046+
1047+
write_sentinel_hook repo-parallel/.git/hooks/pre-push &&
1048+
git -C repo-parallel config hook.hook-2.event pre-push &&
1049+
git -C repo-parallel config hook.hook-2.command \
1050+
"$(sentinel_detector sentinel hook.order)" &&
1051+
git -C repo-parallel config hook.hook-2.parallel true &&
1052+
1053+
git -C repo-parallel config hook.jobs 2 &&
1054+
git -C repo-parallel config hook.forceStdoutToStderr true &&
1055+
1056+
git -C repo-parallel push origin HEAD >out 2>err &&
1057+
echo parallel >expect &&
1058+
test_cmp expect repo-parallel/hook.order
1059+
'
1060+
9921061
test_done

0 commit comments

Comments
 (0)