Skip to content

Commit 187968b

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 f86f9a7 commit 187968b

File tree

8 files changed

+69
-5
lines changed

8 files changed

+69
-5
lines changed

Documentation/config/extensions.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ relativeWorktrees:::
7373
repaired with either the `--relative-paths` option or with the
7474
`worktree.useRelativePaths` config set to `true`.
7575
76+
hookStdoutToStderr:::
77+
If enabled, the stdout of all hooks is redirected to stderr. This
78+
enforces consistency, since by default most hooks already behave
79+
this way, with pre-push being the only known exception.
80+
+
81+
This is useful for parallel hook execution (see the `hook.jobs` config in
82+
linkgit:git-config[1]), as it allows the output of multiple hooks running
83+
in parallel to be grouped (de-interleaved) correctly.
84+
+
85+
Defaults to disabled. When disabled, `hook.jobs` has no effect for pre-push
86+
hooks, which will always be run sequentially.
87+
7688
worktreeConfig:::
7789
If enabled, then worktrees will load config settings from the
7890
`$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
@@ -20,3 +20,6 @@ hook.jobs::
2020
Specifies how many hooks can be run simultaneously during parallelized
2121
hook execution. If unspecified, defaults to the number of processors on
2222
the current system.
23+
+
24+
This has no effect for hooks requiring separate output streams (like `pre-push`)
25+
unless `extensions.hookStdoutToStderr` is enabled.

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ int repo_init(struct repository *repo,
281281
repo->repository_format_worktree_config = format.worktree_config;
282282
repo->repository_format_relative_worktrees = format.relative_worktrees;
283283
repo->repository_format_precious_objects = format.precious_objects;
284+
repo->repository_format_hook_stdout_to_stderr = format.hook_stdout_to_stderr;
284285

285286
/* take ownership of format.partial_clone */
286287
repo->repository_format_partial_clone = format.partial_clone;

repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ struct repository {
165165
int repository_format_worktree_config;
166166
int repository_format_relative_worktrees;
167167
int repository_format_precious_objects;
168+
int repository_format_hook_stdout_to_stderr;
168169

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

setup.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,9 @@ static enum extension_result handle_extension(const char *var,
686686
} else if (!strcmp(ext, "relativeworktrees")) {
687687
data->relative_worktrees = git_config_bool(var, value);
688688
return EXTENSION_OK;
689+
} else if (!strcmp(ext, "hookstdouttostderr")) {
690+
data->hook_stdout_to_stderr = git_config_bool(var, value);
691+
return EXTENSION_OK;
689692
}
690693
return EXTENSION_UNKNOWN;
691694
}
@@ -1947,6 +1950,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
19471950
repo_fmt.worktree_config;
19481951
the_repository->repository_format_relative_worktrees =
19491952
repo_fmt.relative_worktrees;
1953+
the_repository->repository_format_hook_stdout_to_stderr =
1954+
repo_fmt.hook_stdout_to_stderr;
19501955
/* take ownership of repo_fmt.partial_clone */
19511956
the_repository->repository_format_partial_clone =
19521957
repo_fmt.partial_clone;
@@ -2047,6 +2052,8 @@ void check_repository_format(struct repository_format *fmt)
20472052
fmt->worktree_config;
20482053
the_repository->repository_format_relative_worktrees =
20492054
fmt->relative_worktrees;
2055+
the_repository->repository_format_hook_stdout_to_stderr =
2056+
fmt->hook_stdout_to_stderr;
20502057
the_repository->repository_format_partial_clone =
20512058
xstrdup_or_null(fmt->partial_clone);
20522059
clear_repository_format(&repo_fmt);

setup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ struct repository_format {
167167
char *partial_clone; /* value of extensions.partialclone */
168168
int worktree_config;
169169
int relative_worktrees;
170+
int hook_stdout_to_stderr;
170171
int is_bare;
171172
int hash_algo;
172173
int compat_hash_algo;

t/t1800-hook.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,48 @@ test_expect_success 'client hooks: pre-push expects separate stdout and stderr'
395395
check_stdout_separate_from_stderr pre-push
396396
'
397397

398+
test_expect_success 'client hooks: extension makes pre-push merge stdout to stderr' '
399+
test_when_finished "rm -f stdout.actual stderr.actual" &&
400+
git init --bare remote2 &&
401+
git remote add origin2 remote2 &&
402+
test_commit B &&
403+
# repositoryformatversion=1 might be already set (eg default sha256)
404+
# so check before using test_config to set it
405+
{ test "$(git config core.repositoryformatversion)" = 1 ||
406+
test_config core.repositoryformatversion 1; } &&
407+
git config set core.repositoryformatversion 1 &&
408+
test_config extensions.hookStdoutToStderr true &&
409+
setup_hooks pre-push &&
410+
git push origin2 HEAD:main >stdout.actual 2>stderr.actual &&
411+
check_stdout_merged_to_stderr pre-push
412+
'
413+
414+
test_expect_success 'client hooks: pre-push defaults to serial execution' '
415+
test_when_finished "rm -rf repo-serial" &&
416+
git init --bare remote-serial &&
417+
git init repo-serial &&
418+
git -C repo-serial remote add origin ../remote-serial &&
419+
test_commit -C repo-serial A &&
420+
421+
# Setup 2 pre-push hooks
422+
write_script repo-serial/.git/hooks/pre-push <<-EOF &&
423+
sleep 2
424+
echo "Hook 1" >&2
425+
EOF
426+
git -C repo-serial config hook.hook-2.event pre-push &&
427+
git -C repo-serial config hook.hook-2.command "sleep 2; echo Hook 2 >&2" &&
428+
429+
git -C repo-serial config hook.jobs 2 &&
430+
431+
start=$(date +%s) &&
432+
git -C repo-serial push origin HEAD >out 2>err &&
433+
end=$(date +%s) &&
434+
435+
duration=$((end - start)) &&
436+
# Serial >= 4s, parallel < 4s.
437+
test $duration -ge 4
438+
'
439+
398440
test_expect_success 'client hooks: commit hooks expect stdout redirected to stderr' '
399441
hooks="pre-commit prepare-commit-msg \
400442
commit-msg post-commit \

transport.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,11 +1394,8 @@ static int run_pre_push_hook(struct transport *transport,
13941394
opt.copy_feed_pipe_cb_data = copy_pre_push_hook_data;
13951395
opt.free_feed_pipe_cb_data = free_pre_push_hook_data;
13961396

1397-
/*
1398-
* pre-push hooks expect stdout & stderr to be separate, so don't merge
1399-
* them to keep backwards compatibility with existing hooks.
1400-
*/
1401-
opt.stdout_to_stderr = 0;
1397+
/* merge stdout to stderr only when extensions.StdoutToStderr is enabled */
1398+
opt.stdout_to_stderr = the_repository->repository_format_hook_stdout_to_stderr;
14021399

14031400
ret = run_hooks_opt(the_repository, "pre-push", &opt);
14041401

0 commit comments

Comments
 (0)