Skip to content

Commit ba41396

Browse files
10ne1gitster
authored andcommitted
hook: introduce extensions.hookStdoutToStderr
All hooks already redirect stdout to stderr with the exception of pre-push which has a known user who depends on the separate stdout versus stderr outputs (the git-lfs project). The pre-push behavior was a surprise which we found out about after causing a regression for git-lfs. Notably, it might not be the only exception (it's the one we know about). There might be more. This presents a challenge because stdout_to_stderr is required for hook parallelization, so run-command can buffer and de-interleave the hook outputs using ungroup=0, when hook.jobs > 1. Introduce an extension to enforce consistency: all hooks merge stdout into stderr and can be safely parallelized. This provides a clean separation and avoids breaking existing stdout vs stderr behavior. When this extension is disabled, the `hook.jobs` config has no effect for pre-push, to prevent garbled (interleaved) parallel output, so it runs sequentially like before. Alternatives I've considered to this extension include: 1. Allowing pre-push to run in parallel with interleaved output. 2. Always running pre-push sequentially (no parallel jobs for it). 3. Making users (only git-lfs? maybe more?) fix their hooks to read stderr not stdout. Out of all these alternatives, I think this extension is the most reasonable compromise, to not break existing users, allow pre-push parallel jobs for those who need it (with correct outputs) and also future-proofing in case there are any more exceptions to be added. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a333477 commit ba41396

8 files changed

Lines changed: 64 additions & 5 deletions

File tree

Documentation/config/extensions.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ The extension can be enabled automatically for new repositories by setting
102102
`init.defaultSubmodulePathConfig` to `true`, for example by running
103103
`git config --global init.defaultSubmodulePathConfig true`.
104104
105+
hookStdoutToStderr:::
106+
If enabled, the stdout of all hooks is redirected to stderr. This
107+
enforces consistency, since by default most hooks already behave
108+
this way, with pre-push being the only known exception.
109+
+
110+
This is useful for parallel hook execution (see the `hook.jobs` config in
111+
linkgit:git-config[1]), as it allows the output of multiple hooks running
112+
in parallel to be grouped (de-interleaved) correctly.
113+
+
114+
Defaults to disabled. When disabled, `hook.jobs` has no effect for pre-push
115+
hooks, which will always be run sequentially.
116+
105117
worktreeConfig:::
106118
If enabled, then worktrees will load config settings from the
107119
`$GIT_DIR/config.worktree` file in addition to the

Documentation/config/hook.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ hook.jobs::
6262
+
6363
This setting has no effect unless all configured hooks for the event have
6464
`hook.<name>.parallel` set to `true`.
65+
+
66+
This has no effect for hooks requiring separate output streams (like `pre-push`)
67+
unless `extensions.hookStdoutToStderr` is enabled.

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ int repo_init(struct repository *repo,
283283
repo->repository_format_relative_worktrees = format.relative_worktrees;
284284
repo->repository_format_precious_objects = format.precious_objects;
285285
repo->repository_format_submodule_path_cfg = format.submodule_path_cfg;
286+
repo->repository_format_hook_stdout_to_stderr = format.hook_stdout_to_stderr;
286287

287288
/* take ownership of format.partial_clone */
288289
repo->repository_format_partial_clone = format.partial_clone;

repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ struct repository {
173173
int repository_format_relative_worktrees;
174174
int repository_format_precious_objects;
175175
int repository_format_submodule_path_cfg;
176+
int repository_format_hook_stdout_to_stderr;
176177

177178
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
178179
unsigned different_commondir:1;

setup.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ static enum extension_result handle_extension(const char *var,
688688
} else if (!strcmp(ext, "submodulepathconfig")) {
689689
data->submodule_path_cfg = git_config_bool(var, value);
690690
return EXTENSION_OK;
691+
} else if (!strcmp(ext, "hookstdouttostderr")) {
692+
data->hook_stdout_to_stderr = git_config_bool(var, value);
693+
return EXTENSION_OK;
691694
}
692695
return EXTENSION_UNKNOWN;
693696
}
@@ -1951,6 +1954,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
19511954
repo_fmt.relative_worktrees;
19521955
the_repository->repository_format_submodule_path_cfg =
19531956
repo_fmt.submodule_path_cfg;
1957+
the_repository->repository_format_hook_stdout_to_stderr =
1958+
repo_fmt.hook_stdout_to_stderr;
19541959
/* take ownership of repo_fmt.partial_clone */
19551960
the_repository->repository_format_partial_clone =
19561961
repo_fmt.partial_clone;
@@ -2053,6 +2058,8 @@ void check_repository_format(struct repository_format *fmt)
20532058
fmt->submodule_path_cfg;
20542059
the_repository->repository_format_relative_worktrees =
20552060
fmt->relative_worktrees;
2061+
the_repository->repository_format_hook_stdout_to_stderr =
2062+
fmt->hook_stdout_to_stderr;
20562063
the_repository->repository_format_partial_clone =
20572064
xstrdup_or_null(fmt->partial_clone);
20582065
clear_repository_format(&repo_fmt);

setup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ struct repository_format {
168168
int worktree_config;
169169
int relative_worktrees;
170170
int submodule_path_cfg;
171+
int hook_stdout_to_stderr;
171172
int is_bare;
172173
int hash_algo;
173174
int compat_hash_algo;

t/t1800-hook.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,43 @@ test_expect_success 'client hooks: pre-push expects separate stdout and stderr'
532532
check_stdout_separate_from_stderr pre-push
533533
'
534534

535+
test_expect_success 'client hooks: extension makes pre-push merge stdout to stderr' '
536+
test_when_finished "rm -rf remote2 stdout.actual stderr.actual" &&
537+
git init --bare remote2 &&
538+
git remote add origin2 remote2 &&
539+
test_commit B &&
540+
git config set core.repositoryformatversion 1 &&
541+
test_config extensions.hookStdoutToStderr true &&
542+
setup_hooks pre-push &&
543+
git push origin2 HEAD:main >stdout.actual 2>stderr.actual &&
544+
check_stdout_merged_to_stderr pre-push
545+
'
546+
547+
test_expect_success 'client hooks: pre-push defaults to serial execution' '
548+
test_when_finished "rm -rf remote-serial repo-serial" &&
549+
git init --bare remote-serial &&
550+
git init repo-serial &&
551+
git -C repo-serial remote add origin ../remote-serial &&
552+
test_commit -C repo-serial A &&
553+
554+
# Setup 2 pre-push hooks; no parallel=true so they must run serially.
555+
# Use sentinel/detector pattern: hook-1 (sentinel, configured) runs first
556+
# because configured hooks precede traditional hooks in list order; hook-2
557+
# (detector) runs second and checks whether hook-1 has finished.
558+
git -C repo-serial config hook.hook-1.event pre-push &&
559+
git -C repo-serial config hook.hook-1.command \
560+
"touch sentinel.started; sleep 2; touch sentinel.done" &&
561+
git -C repo-serial config hook.hook-2.event pre-push &&
562+
git -C repo-serial config hook.hook-2.command \
563+
"$(sentinel_detector sentinel hook.order)" &&
564+
565+
git -C repo-serial config hook.jobs 2 &&
566+
567+
git -C repo-serial push origin HEAD >out 2>err &&
568+
echo serial >expect &&
569+
test_cmp expect repo-serial/hook.order
570+
'
571+
535572
test_expect_success 'client hooks: commit hooks expect stdout redirected to stderr' '
536573
hooks="pre-commit prepare-commit-msg \
537574
commit-msg post-commit \

transport.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,11 +1388,8 @@ static int run_pre_push_hook(struct transport *transport,
13881388
opt.feed_pipe_cb_data_alloc = pre_push_hook_data_alloc;
13891389
opt.feed_pipe_cb_data_free = pre_push_hook_data_free;
13901390

1391-
/*
1392-
* pre-push hooks expect stdout & stderr to be separate, so don't merge
1393-
* them to keep backwards compatibility with existing hooks.
1394-
*/
1395-
opt.stdout_to_stderr = 0;
1391+
/* merge stdout to stderr only when extensions.hookStdoutToStderr is enabled */
1392+
opt.stdout_to_stderr = the_repository->repository_format_hook_stdout_to_stderr;
13961393

13971394
ret = run_hooks_opt(the_repository, "pre-push", &opt);
13981395

0 commit comments

Comments
 (0)