Skip to content

Commit 3b413bd

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 ba41396 commit 3b413bd

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
@@ -113,6 +113,9 @@ in parallel to be grouped (de-interleaved) correctly.
113113
+
114114
Defaults to disabled. When disabled, `hook.jobs` has no effect for pre-push
115115
hooks, which will always be run sequentially.
116+
+
117+
The extension can also be enabled by setting `hook.forceStdoutToStderr`
118+
to `true` in the global configuration.
116119
117120
worktreeConfig:::
118121
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
@@ -136,6 +136,7 @@ struct hook_config_cache_entry {
136136
* event_jobs: event-name to per-event jobs count (heap-allocated unsigned int *,
137137
* where NULL == unset).
138138
* jobs: value of the global hook.jobs key. Defaults to 0 if unset.
139+
* force_stdout_to_stderr: value of hook.forceStdoutToStderr. Defaults to 0.
139140
*/
140141
struct hook_all_config_cb {
141142
struct strmap commands;
@@ -144,6 +145,7 @@ struct hook_all_config_cb {
144145
struct strmap parallel_hooks;
145146
struct strmap event_jobs;
146147
unsigned int jobs;
148+
int force_stdout_to_stderr;
147149
};
148150

149151
/* repo_config() callback that collects all hook.* configuration in one pass. */
@@ -169,6 +171,10 @@ static int hook_config_lookup_all(const char *key, const char *value,
169171
warning(_("hook.jobs must be positive, ignoring: 0"));
170172
else
171173
data->jobs = v;
174+
} else if (!strcmp(subkey, "forcestdouttostderr") && value) {
175+
int v = git_parse_maybe_bool(value);
176+
if (v >= 0)
177+
data->force_stdout_to_stderr = v;
172178
}
173179
return 0;
174180
}
@@ -325,6 +331,7 @@ static void build_hook_config_map(struct repository *r,
325331

326332
cache->jobs = cb_data.jobs;
327333
cache->event_jobs = cb_data.event_jobs;
334+
cache->force_stdout_to_stderr = cb_data.force_stdout_to_stderr;
328335

329336
strmap_clear(&cb_data.commands, 1);
330337
strmap_clear(&cb_data.parallel_hooks, 0); /* values are uintptr_t, not heap ptrs */
@@ -530,6 +537,20 @@ static void run_hooks_opt_clear(struct run_hooks_opt *options)
530537
strvec_clear(&options->args);
531538
}
532539

540+
static void hook_force_apply_stdout_to_stderr(struct repository *r,
541+
struct run_hooks_opt *options)
542+
{
543+
int force = 0;
544+
545+
if (r && r->gitdir && r->hook_config_cache)
546+
force = r->hook_config_cache->force_stdout_to_stderr;
547+
else
548+
repo_config_get_bool(r, "hook.forceStdoutToStderr", &force);
549+
550+
if (force)
551+
options->stdout_to_stderr = 1;
552+
}
553+
533554
/* Determine how many jobs to use for hook execution. */
534555
static unsigned int get_hook_jobs(struct repository *r,
535556
struct run_hooks_opt *options,
@@ -539,9 +560,12 @@ static unsigned int get_hook_jobs(struct repository *r,
539560
unsigned int jobs;
540561

541562
/*
542-
* Hooks needing separate output streams must run sequentially. Next
543-
* commits will add an extension to allow parallelizing these as well.
563+
* Apply hook.forceStdoutToStderr before anything else: it affects
564+
* whether we can run in parallel or not.
544565
*/
566+
hook_force_apply_stdout_to_stderr(r, options);
567+
568+
/* Hooks needing separate output streams must run sequentially. */
545569
if (!options->stdout_to_stderr)
546570
return 1;
547571

hook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct hook_config_cache {
224224
struct strmap hooks; /* maps event name -> string_list of hooks */
225225
struct strmap event_jobs; /* maps event name -> heap-allocated unsigned int * */
226226
unsigned int jobs; /* hook.jobs config value; 0 if unset (defaults to serial) */
227+
int force_stdout_to_stderr; /* hook.forceStdoutToStderr config value */
227228
};
228229

229230
/**

setup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,7 @@ void initialize_repository_version(int hash_algo,
23172317
struct strbuf repo_version = STRBUF_INIT;
23182318
int target_version = GIT_REPO_VERSION;
23192319
int default_submodule_path_config = 0;
2320+
int default_hook_stdout_to_stderr = 0;
23202321

23212322
/*
23222323
* Note that we initialize the repository version to 1 when the ref
@@ -2364,6 +2365,15 @@ void initialize_repository_version(int hash_algo,
23642365
repo_config_set(the_repository, "extensions.submodulepathconfig", "true");
23652366
}
23662367

2368+
repo_config_get_bool(the_repository, "hook.forceStdoutToStderr",
2369+
&default_hook_stdout_to_stderr);
2370+
if (default_hook_stdout_to_stderr) {
2371+
/* extensions.hookstdouttostderr requires at least version 1 */
2372+
if (target_version == 0)
2373+
target_version = 1;
2374+
repo_config_set(the_repository, "extensions.hookstdouttostderr", "true");
2375+
}
2376+
23672377
strbuf_addf(&repo_version, "%d", target_version);
23682378
repo_config_set(the_repository, "core.repositoryformatversion", repo_version.buf);
23692379

t/t1800-hook.sh

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

919+
test_expect_success '`git init` respects hook.forceStdoutToStderr' '
920+
test_when_finished "rm -rf repo-init" &&
921+
test_config_global hook.forceStdoutToStderr true &&
922+
git init repo-init &&
923+
git -C repo-init config extensions.hookStdoutToStderr >actual &&
924+
echo true >expect &&
925+
test_cmp expect actual
926+
'
927+
928+
test_expect_success '`git init` does not set extensions.hookStdoutToStderr by default' '
929+
test_when_finished "rm -rf upstream" &&
930+
git init upstream &&
931+
test_must_fail git -C upstream config extensions.hookStdoutToStderr
932+
'
933+
934+
test_expect_success '`git clone` does not set extensions.hookStdoutToStderr by default' '
935+
test_when_finished "rm -rf upstream repo-clone-no-ext" &&
936+
git init upstream &&
937+
git clone upstream repo-clone-no-ext &&
938+
test_must_fail git -C repo-clone-no-ext config extensions.hookStdoutToStderr
939+
'
940+
941+
test_expect_success '`git clone` respects hook.forceStdoutToStderr' '
942+
test_when_finished "rm -rf upstream repo-clone" &&
943+
git init upstream &&
944+
test_config_global hook.forceStdoutToStderr true &&
945+
git clone upstream repo-clone &&
946+
git -C repo-clone config extensions.hookStdoutToStderr >actual &&
947+
echo true >expect &&
948+
test_cmp expect actual
949+
'
950+
951+
test_expect_success 'hook.forceStdoutToStderr enables extension for existing repos' '
952+
test_when_finished "rm -rf remote-repo existing-repo" &&
953+
git init --bare remote-repo &&
954+
git init -b main existing-repo &&
955+
# No local extensions.hookStdoutToStderr config set here
956+
# so global config should apply
957+
test_config_global hook.forceStdoutToStderr true &&
958+
cd existing-repo &&
959+
test_commit A &&
960+
git remote add origin ../remote-repo &&
961+
setup_hooks pre-push &&
962+
git push origin HEAD >stdout.actual 2>stderr.actual &&
963+
check_stdout_merged_to_stderr pre-push &&
964+
cd ..
965+
'
966+
967+
test_expect_success 'hook.forceStdoutToStderr enables pre-push parallel runs' '
968+
test_when_finished "rm -rf repo-parallel remote-parallel" &&
969+
git init --bare remote-parallel &&
970+
git init repo-parallel &&
971+
git -C repo-parallel remote add origin ../remote-parallel &&
972+
test_commit -C repo-parallel A &&
973+
974+
write_sentinel_hook repo-parallel/.git/hooks/pre-push &&
975+
git -C repo-parallel config hook.hook-2.event pre-push &&
976+
git -C repo-parallel config hook.hook-2.command \
977+
"$(sentinel_detector sentinel hook.order)" &&
978+
git -C repo-parallel config hook.hook-2.parallel true &&
979+
980+
git -C repo-parallel config hook.jobs 2 &&
981+
git -C repo-parallel config hook.forceStdoutToStderr true &&
982+
983+
git -C repo-parallel push origin HEAD >out 2>err &&
984+
echo parallel >expect &&
985+
test_cmp expect repo-parallel/hook.order
986+
'
987+
919988
test_done

0 commit comments

Comments
 (0)