interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t1800-hook.sh

1282 lines · 42020 bytes Raw Blame History
1#!/bin/sh
2
3test_description='git-hook command and config-managed multihooks'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-terminal.sh
7
8setup_hooks () {
9 test_config hook.ghi.command "/path/ghi" &&
10 test_config hook.ghi.event pre-commit --add &&
11 test_config hook.ghi.event test-hook --add &&
12 test_config_global hook.def.command "/path/def" &&
13 test_config_global hook.def.event pre-commit --add
14}
15
16setup_hookdir () {
17 mkdir -p .git/hooks &&
18 write_script .git/hooks/pre-commit <<-EOF &&
19 echo \"Legacy Hook\"
20 EOF
21 test_when_finished rm -rf .git/hooks
22}
23
24# write_sentinel_hook <path> [sentinel]
25#
26# Writes a hook that marks itself as started, sleeps for a few seconds, then
27# marks itself done. The sleep must be long enough that sentinel_detector can
28# observe <sentinel>.started before <sentinel>.done appears when both hooks
29# run concurrently in parallel mode.
30write_sentinel_hook () {
31 sentinel="${2:-sentinel}"
32 write_script "$1" <<-EOF
33 touch ${sentinel}.started &&
34 sleep 2 &&
35 touch ${sentinel}.done
36 EOF
37}
38
39# sentinel_detector <sentinel> <output>
40#
41# Returns a shell command string suitable for use as hook.<name>.command.
42# The detector must be registered after the sentinel:
43# 1. In serial mode, the sentinel has completed (and <sentinel>.done exists)
44# before the detector starts.
45# 2. In parallel mode, both run concurrently so <sentinel>.done has not appeared
46# yet and the detector just sees <sentinel>.started.
47#
48# At start, poll until <sentinel>.started exists to absorb startup jitter, then
49# write to <output>:
50# 1. 'serial' if <sentinel>.done exists (sentinel finished before we started),
51# 2. 'parallel' if only <sentinel>.started exists (sentinel still running),
52# 3. 'timeout' if <sentinel>.started never appeared.
53#
54# The command ends with ':' so when git appends "$@" for hooks that receive
55# positional arguments (e.g. pre-push), the result ': "$@"' is valid shell
56# rather than a syntax error 'fi "$@"'.
57sentinel_detector () {
58 cat <<-EOF
59 i=0
60 while ! test -f ${1}.started && test \$i -lt 10; do
61 sleep 1
62 i=\$((i+1))
63 done
64 if test -f ${1}.done; then
65 echo serial >${2}
66 elif test -f ${1}.started; then
67 echo parallel >${2}
68 else
69 echo timeout >${2}
70 fi
71 :
72 EOF
73}
74
75test_expect_success 'git hook usage' '
76 test_expect_code 129 git hook &&
77 test_expect_code 129 git hook run &&
78 git hook run -h &&
79 test_expect_code 129 git hook run --unknown 2>err &&
80 test_expect_code 129 git hook list &&
81 git hook list -h &&
82 test_grep "unknown option" err
83'
84
85test_expect_success 'git hook list: unknown hook name is rejected' '
86 test_must_fail git hook list prereceive 2>err &&
87 test_grep "unknown hook event" err
88'
89
90test_expect_success 'git hook run: unknown hook name is rejected' '
91 test_must_fail git hook run prereceive 2>err &&
92 test_grep "unknown hook event" err
93'
94
95test_expect_success 'git hook list: known hook name is accepted' '
96 test_must_fail git hook list pre-receive 2>err &&
97 test_grep ! "unknown hook event" err
98'
99
100test_expect_success 'git hook run: known hook name is accepted' '
101 git hook run --ignore-missing pre-receive 2>err &&
102 test_grep ! "unknown hook event" err
103'
104
105test_expect_success 'git hook run: --allow-unknown-hook-name overrides rejection' '
106 git hook run --allow-unknown-hook-name --ignore-missing custom-hook 2>err &&
107 test_grep ! "unknown hook event" err
108'
109
110test_expect_success 'git hook list: --allow-unknown-hook-name overrides rejection' '
111 test_must_fail git hook list --allow-unknown-hook-name custom-hook 2>err &&
112 test_grep ! "unknown hook event" err
113'
114
115test_expect_success 'git hook list: nonexistent hook' '
116 cat >stderr.expect <<-\EOF &&
117 warning: no hooks found for event '\''test-hook'\''
118 EOF
119 test_expect_code 1 git hook list --allow-unknown-hook-name test-hook 2>stderr.actual &&
120 test_cmp stderr.expect stderr.actual
121'
122
123test_expect_success 'git hook list: traditional hook from hookdir' '
124 test_hook test-hook <<-EOF &&
125 echo Test hook
126 EOF
127
128 cat >expect <<-\EOF &&
129 hook from hookdir
130 EOF
131 git hook list --allow-unknown-hook-name test-hook >actual &&
132 test_cmp expect actual
133'
134
135test_expect_success 'git hook list: configured hook' '
136 test_config hook.myhook.command "echo Hello" &&
137 test_config hook.myhook.event test-hook --add &&
138
139 echo "myhook" >expect &&
140 git hook list --allow-unknown-hook-name test-hook >actual &&
141 test_cmp expect actual
142'
143
144test_expect_success 'git hook list: -z shows NUL-terminated output' '
145 test_hook test-hook <<-EOF &&
146 echo Test hook
147 EOF
148 test_config hook.myhook.command "echo Hello" &&
149 test_config hook.myhook.event test-hook --add &&
150
151 printf "myhookQhook from hookdirQ" >expect &&
152 git hook list --allow-unknown-hook-name -z test-hook >actual.raw &&
153 nul_to_q <actual.raw >actual &&
154 test_cmp expect actual
155'
156
157test_expect_success 'git hook run: nonexistent hook' '
158 cat >stderr.expect <<-\EOF &&
159 error: cannot find a hook named test-hook
160 EOF
161 test_expect_code 1 git hook run --allow-unknown-hook-name test-hook 2>stderr.actual &&
162 test_cmp stderr.expect stderr.actual
163'
164
165test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
166 git hook run --allow-unknown-hook-name --ignore-missing does-not-exist 2>stderr.actual &&
167 test_must_be_empty stderr.actual
168'
169
170test_expect_success 'git hook run: basic' '
171 test_hook test-hook <<-EOF &&
172 echo Test hook
173 EOF
174
175 cat >expect <<-\EOF &&
176 Test hook
177 EOF
178 git hook run --allow-unknown-hook-name test-hook 2>actual &&
179 test_cmp expect actual
180'
181
182test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
183 test_hook test-hook <<-EOF &&
184 echo >&1 Will end up on stderr
185 echo >&2 Will end up on stderr
186 EOF
187
188 cat >stderr.expect <<-\EOF &&
189 Will end up on stderr
190 Will end up on stderr
191 EOF
192 git hook run --allow-unknown-hook-name test-hook >stdout.actual 2>stderr.actual &&
193 test_cmp stderr.expect stderr.actual &&
194 test_must_be_empty stdout.actual
195'
196
197for code in 1 2 128 129
198do
199 test_expect_success "git hook run: exit code $code is passed along" '
200 test_hook test-hook <<-EOF &&
201 exit $code
202 EOF
203
204 test_expect_code $code git hook run --allow-unknown-hook-name test-hook
205 '
206done
207
208test_expect_success 'git hook run arg u ments without -- is not allowed' '
209 test_expect_code 129 git hook run --allow-unknown-hook-name test-hook arg u ments
210'
211
212test_expect_success 'git hook run -- pass arguments' '
213 test_hook test-hook <<-\EOF &&
214 echo $1
215 echo $2
216 EOF
217
218 cat >expect <<-EOF &&
219 arg
220 u ments
221 EOF
222
223 git hook run --allow-unknown-hook-name test-hook -- arg "u ments" 2>actual &&
224 test_cmp expect actual
225'
226
227test_expect_success 'git hook run: out-of-repo runs execute global hooks' '
228 test_config_global hook.global-hook.event test-hook --add &&
229 test_config_global hook.global-hook.command "echo no repo no problems" --add &&
230
231 echo "global-hook" >expect &&
232 nongit git hook list --allow-unknown-hook-name test-hook >actual &&
233 test_cmp expect actual &&
234
235 echo "no repo no problems" >expect &&
236
237 nongit git hook run --allow-unknown-hook-name test-hook 2>actual &&
238 test_cmp expect actual
239'
240
241test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
242 mkdir my-hooks &&
243 write_script my-hooks/test-hook <<-\EOF &&
244 echo Hook ran $1
245 EOF
246
247 cat >expect <<-\EOF &&
248 Test hook
249 Hook ran one
250 Hook ran two
251 Hook ran three
252 Hook ran four
253 EOF
254
255 test_hook test-hook <<-EOF &&
256 echo Test hook
257 EOF
258
259 # Test various ways of specifying the path. See also
260 # t1350-config-hooks-path.sh
261 >actual &&
262 git hook run --allow-unknown-hook-name test-hook -- ignored 2>>actual &&
263 git -c core.hooksPath=my-hooks hook run --allow-unknown-hook-name test-hook -- one 2>>actual &&
264 git -c core.hooksPath=my-hooks/ hook run --allow-unknown-hook-name test-hook -- two 2>>actual &&
265 git -c core.hooksPath="$PWD/my-hooks" hook run --allow-unknown-hook-name test-hook -- three 2>>actual &&
266 git -c core.hooksPath="$PWD/my-hooks/" hook run --allow-unknown-hook-name test-hook -- four 2>>actual &&
267 test_cmp expect actual
268'
269
270test_hook_tty () {
271 expect_tty=$1
272 shift
273
274 if test "$expect_tty" != "no_tty"; then
275 cat >expect <<-\EOF
276 STDOUT TTY
277 STDERR TTY
278 EOF
279 else
280 cat >expect <<-\EOF
281 STDOUT NO TTY
282 STDERR NO TTY
283 EOF
284 fi
285
286 test_when_finished "rm -rf repo" &&
287 git init repo &&
288
289 test_commit -C repo A &&
290 test_commit -C repo B &&
291 git -C repo reset --soft HEAD^ &&
292
293 test_hook -C repo pre-commit <<-EOF &&
294 test -t 1 && echo STDOUT TTY >>actual || echo STDOUT NO TTY >>actual &&
295 test -t 2 && echo STDERR TTY >>actual || echo STDERR NO TTY >>actual
296 EOF
297
298 test_terminal git -C repo "$@" &&
299 test_cmp expect repo/actual
300}
301
302test_expect_success TTY 'git hook run -j1: stdout and stderr are connected to a TTY' '
303 # hooks running sequentially (-j1) are always connected to the tty for
304 # optimum real-time performance.
305 test_hook_tty tty hook run -j1 pre-commit
306'
307
308test_expect_success TTY 'git hook run -jN: stdout and stderr are not connected to a TTY' '
309 # Hooks are not connected to the tty when run in parallel, instead they
310 # output to a pipe through which run-command collects and de-interlaces
311 # their outputs, which then gets passed either to the tty or a sideband.
312 test_hook_tty no_tty hook run -j2 pre-commit
313'
314
315test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' '
316 test_hook_tty tty commit -m"B.new"
317'
318
319test_expect_success 'git hook list orders by config order' '
320 setup_hooks &&
321
322 cat >expected <<-\EOF &&
323 def
324 ghi
325 EOF
326
327 git hook list pre-commit >actual &&
328 test_cmp expected actual
329'
330
331test_expect_success 'git hook list reorders on duplicate event declarations' '
332 setup_hooks &&
333
334 # 'def' is usually configured globally; move it to the end by
335 # configuring it locally.
336 test_config hook.def.event "pre-commit" --add &&
337
338 cat >expected <<-\EOF &&
339 ghi
340 def
341 EOF
342
343 git hook list pre-commit >actual &&
344 test_cmp expected actual
345'
346
347test_expect_success 'git hook list: empty event value resets events' '
348 setup_hooks &&
349
350 # ghi is configured for pre-commit; reset it with an empty value
351 test_config hook.ghi.event "" --add &&
352
353 # only def should remain for pre-commit
354 echo "def" >expected &&
355 git hook list pre-commit >actual &&
356 test_cmp expected actual
357'
358
359test_expect_success 'hook can be configured for multiple events' '
360 setup_hooks &&
361
362 # 'ghi' should be included in both 'pre-commit' and 'test-hook'
363 git hook list pre-commit >actual &&
364 test_grep "ghi" actual &&
365 git hook list --allow-unknown-hook-name test-hook >actual &&
366 test_grep "ghi" actual
367'
368
369test_expect_success 'git hook list shows hooks from the hookdir' '
370 setup_hookdir &&
371
372 cat >expected <<-\EOF &&
373 hook from hookdir
374 EOF
375
376 git hook list pre-commit >actual &&
377 test_cmp expected actual
378'
379
380test_expect_success 'inline hook definitions execute oneliners' '
381 test_config hook.oneliner.event "pre-commit" &&
382 test_config hook.oneliner.command "echo \"Hello World\"" &&
383
384 echo "Hello World" >expected &&
385
386 # hooks are run with stdout_to_stderr = 1
387 git hook run pre-commit 2>actual &&
388 test_cmp expected actual
389'
390
391test_expect_success 'inline hook definitions resolve paths' '
392 write_script sample-hook.sh <<-\EOF &&
393 echo \"Sample Hook\"
394 EOF
395
396 test_when_finished "rm sample-hook.sh" &&
397
398 test_config hook.sample-hook.event pre-commit &&
399 test_config hook.sample-hook.command "\"$(pwd)/sample-hook.sh\"" &&
400
401 echo \"Sample Hook\" >expected &&
402
403 # hooks are run with stdout_to_stderr = 1
404 git hook run pre-commit 2>actual &&
405 test_cmp expected actual
406'
407
408test_expect_success 'hookdir hook included in git hook run' '
409 setup_hookdir &&
410
411 echo \"Legacy Hook\" >expected &&
412
413 # hooks are run with stdout_to_stderr = 1
414 git hook run pre-commit 2>actual &&
415 test_cmp expected actual
416'
417
418test_expect_success 'stdin to multiple hooks' '
419 test_config hook.stdin-a.event "test-hook" &&
420 test_config hook.stdin-a.command "xargs -P1 -I% echo a%" &&
421 test_config hook.stdin-b.event "test-hook" &&
422 test_config hook.stdin-b.command "xargs -P1 -I% echo b%" &&
423
424 cat >input <<-\EOF &&
425 1
426 2
427 3
428 EOF
429
430 cat >expected <<-\EOF &&
431 a1
432 a2
433 a3
434 b1
435 b2
436 b3
437 EOF
438
439 git hook run --allow-unknown-hook-name --to-stdin=input test-hook 2>actual &&
440 test_cmp expected actual
441'
442
443test_expect_success 'rejects hooks with no commands configured' '
444 test_config hook.broken.event "test-hook" &&
445 test_must_fail git hook list --allow-unknown-hook-name test-hook 2>actual &&
446 test_grep "hook.broken.command" actual &&
447 test_must_fail git hook run --allow-unknown-hook-name test-hook 2>actual &&
448 test_grep "hook.broken.command" actual
449'
450
451test_expect_success 'disabled hook is not run' '
452 test_config hook.skipped.event "test-hook" &&
453 test_config hook.skipped.command "echo \"Should not run\"" &&
454 test_config hook.skipped.enabled false &&
455
456 git hook run --allow-unknown-hook-name --ignore-missing test-hook 2>actual &&
457 test_must_be_empty actual
458'
459
460test_expect_success 'disabled hook with no command warns' '
461 test_config hook.nocommand.event "pre-commit" &&
462 test_config hook.nocommand.enabled false &&
463
464 git hook list pre-commit 2>actual &&
465 test_grep "disabled hook.*nocommand.*no command configured" actual
466'
467
468test_expect_success 'disabled hook appears as disabled in git hook list' '
469 test_config hook.active.event "pre-commit" &&
470 test_config hook.active.command "echo active" &&
471 test_config hook.inactive.event "pre-commit" &&
472 test_config hook.inactive.command "echo inactive" &&
473 test_config hook.inactive.enabled false &&
474
475 git hook list pre-commit >actual &&
476 test_grep "^active$" actual &&
477 test_grep "^disabled inactive$" actual
478'
479
480test_expect_success 'disabled hook shows scope with --show-scope' '
481 test_config hook.myhook.event "pre-commit" &&
482 test_config hook.myhook.command "echo hi" &&
483 test_config hook.myhook.enabled false &&
484
485 git hook list --show-scope pre-commit >actual &&
486 test_grep "^local disabled myhook$" actual
487'
488
489test_expect_success 'disabled configured hook is not reported as existing by hook_exists' '
490 test_when_finished "rm -f git-bugreport-hook-exists-test.txt" &&
491 test_config hook.linter.event "pre-commit" &&
492 test_config hook.linter.command "echo lint" &&
493 test_config hook.linter.enabled false &&
494
495 git bugreport -s hook-exists-test &&
496 test_grep ! "pre-commit" git-bugreport-hook-exists-test.txt
497'
498
499test_expect_success 'event-disabled hookdir hook is not reported as existing by hook_exists' '
500 test_when_finished "rm -f git-bugreport-hook-exists-test.txt" &&
501 setup_hookdir &&
502 test_config hook.pre-commit.enabled false &&
503
504 git bugreport -s hook-exists-test &&
505 test_grep ! "pre-commit" git-bugreport-hook-exists-test.txt
506'
507
508test_expect_success 'globally disabled hook can be re-enabled locally' '
509 test_config_global hook.global-hook.event "test-hook" &&
510 test_config_global hook.global-hook.command "echo \"global-hook ran\"" &&
511 test_config_global hook.global-hook.enabled false &&
512 test_config hook.global-hook.enabled true &&
513
514 echo "global-hook ran" >expected &&
515 git hook run --allow-unknown-hook-name test-hook 2>actual &&
516 test_cmp expected actual
517'
518
519test_expect_success 'hook.<friendly-name>.enabled without a value means true' '
520 test_config_global hook.global-hook.event "test-hook" &&
521 test_config_global hook.global-hook.command "echo \"global-hook ran\"" &&
522 test_config_global hook.global-hook.enabled false &&
523
524 echo "global-hook ran" >expected &&
525 git -c hook.global-hook.enabled \
526 hook run --allow-unknown-hook-name test-hook 2>actual &&
527 test_cmp expected actual
528'
529
530test_expect_success 'configured hooks run before hookdir hook' '
531 setup_hookdir &&
532 test_config hook.first.event "pre-commit" &&
533 test_config hook.first.command "echo first" &&
534 test_config hook.second.event "pre-commit" &&
535 test_config hook.second.command "echo second" &&
536
537 cat >expected <<-\EOF &&
538 first
539 second
540 hook from hookdir
541 EOF
542
543 git hook list pre-commit >actual &&
544 test_cmp expected actual &&
545
546 # "Legacy Hook" is the output of the hookdir pre-commit script
547 # written by setup_hookdir() above.
548 cat >expected <<-\EOF &&
549 first
550 second
551 "Legacy Hook"
552 EOF
553
554 git hook run pre-commit 2>actual &&
555 test_cmp expected actual
556'
557
558test_expect_success 'git hook list --show-scope shows config scope' '
559 setup_hookdir &&
560 test_config_global hook.global-hook.command "echo global" &&
561 test_config_global hook.global-hook.event pre-commit --add &&
562 test_config hook.local-hook.command "echo local" &&
563 test_config hook.local-hook.event pre-commit --add &&
564
565 cat >expected <<-\EOF &&
566 global global-hook
567 local local-hook
568 hook from hookdir
569 EOF
570 git hook list --show-scope pre-commit >actual &&
571 test_cmp expected actual &&
572
573 # without --show-scope the scope must not appear
574 git hook list pre-commit >actual &&
575 test_grep ! "^global " actual &&
576 test_grep ! "^local " actual
577'
578
579test_expect_success 'git hook run a hook with a bad shebang' '
580 test_when_finished "rm -rf bad-hooks" &&
581 mkdir bad-hooks &&
582 write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
583
584 test_expect_code 1 git \
585 -c core.hooksPath=bad-hooks \
586 hook run --allow-unknown-hook-name test-hook >out 2>err &&
587 test_must_be_empty out &&
588
589 # TODO: We should emit the same (or at least a more similar)
590 # error on MINGW (essentially Git for Windows) and all other
591 # platforms.. See the OS-specific code in start_command()
592 test_grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err
593'
594
595test_expect_success 'stdin to hooks' '
596 mkdir -p .git/hooks &&
597 write_script .git/hooks/test-hook <<-\EOF &&
598 echo BEGIN stdin
599 cat
600 echo END stdin
601 EOF
602
603 cat >expect <<-EOF &&
604 BEGIN stdin
605 hello
606 END stdin
607 EOF
608
609 echo hello >input &&
610 git hook run --allow-unknown-hook-name --to-stdin=input test-hook 2>actual &&
611 test_cmp expect actual
612'
613
614check_stdout_separate_from_stderr () {
615 for hook in "$@"
616 do
617 # Ensure hook's stdout is only in stdout, not stderr
618 test_grep "Hook $hook stdout" stdout.actual || return 1
619 test_grep ! "Hook $hook stdout" stderr.actual || return 1
620
621 # Ensure hook's stderr is only in stderr, not stdout
622 test_grep "Hook $hook stderr" stderr.actual || return 1
623 test_grep ! "Hook $hook stderr" stdout.actual || return 1
624 done
625}
626
627check_stdout_merged_to_stderr () {
628 for hook in "$@"
629 do
630 # Ensure hook's stdout is only in stderr, not stdout
631 test_grep "Hook $hook stdout" stderr.actual || return 1
632 test_grep ! "Hook $hook stdout" stdout.actual || return 1
633
634 # Ensure hook's stderr is only in stderr, not stdout
635 test_grep "Hook $hook stderr" stderr.actual || return 1
636 test_grep ! "Hook $hook stderr" stdout.actual || return 1
637 done
638}
639
640setup_hooks () {
641 for hook in "$@"
642 do
643 test_hook $hook <<-EOF
644 echo >&1 Hook $hook stdout
645 echo >&2 Hook $hook stderr
646 EOF
647 done
648}
649
650test_expect_success 'client hooks: pre-push expects separate stdout and stderr' '
651 test_when_finished "rm -f stdout.actual stderr.actual" &&
652 git init --bare remote &&
653 git remote add origin remote &&
654 test_commit A &&
655 setup_hooks pre-push &&
656 git push origin HEAD:main >stdout.actual 2>stderr.actual &&
657 check_stdout_separate_from_stderr pre-push
658'
659
660test_expect_success 'client hooks: commit hooks expect stdout redirected to stderr' '
661 hooks="pre-commit prepare-commit-msg \
662 commit-msg post-commit \
663 reference-transaction" &&
664 setup_hooks $hooks &&
665 test_when_finished "rm -f stdout.actual stderr.actual" &&
666 git checkout -B main &&
667 git checkout -b branch-a &&
668 test_commit commit-on-branch-a &&
669 git commit --allow-empty -m "Test" >stdout.actual 2>stderr.actual &&
670 check_stdout_merged_to_stderr $hooks
671'
672
673test_expect_success 'client hooks: checkout hooks expect stdout redirected to stderr' '
674 setup_hooks post-checkout reference-transaction &&
675 test_when_finished "rm -f stdout.actual stderr.actual" &&
676 git checkout -b new-branch main >stdout.actual 2>stderr.actual &&
677 check_stdout_merged_to_stderr post-checkout reference-transaction
678'
679
680test_expect_success 'client hooks: merge hooks expect stdout redirected to stderr' '
681 setup_hooks pre-merge-commit post-merge reference-transaction &&
682 test_when_finished "rm -f stdout.actual stderr.actual" &&
683 test_commit new-branch-commit &&
684 git merge --no-ff branch-a >stdout.actual 2>stderr.actual &&
685 check_stdout_merged_to_stderr pre-merge-commit post-merge reference-transaction
686'
687
688test_expect_success 'client hooks: post-rewrite hooks expect stdout redirected to stderr' '
689 setup_hooks post-rewrite reference-transaction &&
690 test_when_finished "rm -f stdout.actual stderr.actual" &&
691 git commit --amend --allow-empty --no-edit >stdout.actual 2>stderr.actual &&
692 check_stdout_merged_to_stderr post-rewrite reference-transaction
693'
694
695test_expect_success 'client hooks: applypatch hooks expect stdout redirected to stderr' '
696 setup_hooks applypatch-msg pre-applypatch post-applypatch &&
697 test_when_finished "rm -f stdout.actual stderr.actual" &&
698 git checkout -b branch-b main &&
699 test_commit branch-b &&
700 git format-patch -1 --stdout >patch &&
701 git checkout -b branch-c main &&
702 git am patch >stdout.actual 2>stderr.actual &&
703 check_stdout_merged_to_stderr applypatch-msg pre-applypatch post-applypatch
704'
705
706test_expect_success 'client hooks: rebase hooks expect stdout redirected to stderr' '
707 setup_hooks pre-rebase &&
708 test_when_finished "rm -f stdout.actual stderr.actual" &&
709 git checkout -b branch-d main &&
710 test_commit branch-d &&
711 git checkout main &&
712 test_commit diverge-main &&
713 git checkout branch-d &&
714 git rebase main >stdout.actual 2>stderr.actual &&
715 check_stdout_merged_to_stderr pre-rebase
716'
717
718test_expect_success 'client hooks: post-index-change expects stdout redirected to stderr' '
719 setup_hooks post-index-change &&
720 test_when_finished "rm -f stdout.actual stderr.actual" &&
721 oid=$(git hash-object -w --stdin </dev/null) &&
722 git update-index --add --cacheinfo 100644 $oid new-file \
723 >stdout.actual 2>stderr.actual &&
724 check_stdout_merged_to_stderr post-index-change
725'
726
727test_expect_success 'server hooks expect stdout redirected to stderr' '
728 test_when_finished "rm -f stdout.actual stderr.actual" &&
729 git init --bare remote-server &&
730 git remote add origin-server remote-server &&
731 cd remote-server &&
732 setup_hooks pre-receive update post-receive post-update &&
733 cd .. &&
734 git push origin-server HEAD:new-branch >stdout.actual 2>stderr.actual &&
735 check_stdout_merged_to_stderr pre-receive update post-receive post-update
736'
737
738test_expect_success 'server push-to-checkout hook expects stdout redirected to stderr' '
739 test_when_finished "rm -f stdout.actual stderr.actual" &&
740 git init server &&
741 git -C server checkout -b main &&
742 test_config -C server receive.denyCurrentBranch updateInstead &&
743 git remote add origin-server-2 server &&
744 cd server &&
745 setup_hooks push-to-checkout &&
746 cd .. &&
747 git push origin-server-2 HEAD:main >stdout.actual 2>stderr.actual &&
748 check_stdout_merged_to_stderr push-to-checkout
749'
750
751test_expect_success 'parallel hook output is not interleaved' '
752 test_when_finished "rm -rf .git/hooks" &&
753
754 write_script .git/hooks/test-hook <<-EOF &&
755 echo "Hook 1 Start"
756 sleep 1
757 echo "Hook 1 End"
758 EOF
759
760 test_config hook.hook-2.event test-hook &&
761 test_config hook.hook-2.command \
762 "echo \"Hook 2 Start\"; sleep 2; echo \"Hook 2 End\"" &&
763 test_config hook.hook-2.parallel true &&
764 test_config hook.hook-3.event test-hook &&
765 test_config hook.hook-3.command \
766 "echo \"Hook 3 Start\"; sleep 3; echo \"Hook 3 End\"" &&
767 test_config hook.hook-3.parallel true &&
768
769 git hook run --allow-unknown-hook-name -j3 test-hook >out 2>err.parallel &&
770
771 # Verify Hook 1 output is grouped
772 sed -n "/Hook 1 Start/,/Hook 1 End/p" err.parallel >hook1_out &&
773 test_line_count = 2 hook1_out &&
774
775 # Verify Hook 2 output is grouped
776 sed -n "/Hook 2 Start/,/Hook 2 End/p" err.parallel >hook2_out &&
777 test_line_count = 2 hook2_out &&
778
779 # Verify Hook 3 output is grouped
780 sed -n "/Hook 3 Start/,/Hook 3 End/p" err.parallel >hook3_out &&
781 test_line_count = 2 hook3_out
782'
783
784test_expect_success 'git hook run -j1 runs hooks in series' '
785 test_when_finished "rm -rf .git/hooks" &&
786
787 test_config hook.series-1.event "test-hook" &&
788 test_config hook.series-1.command "echo 1" --add &&
789 test_config hook.series-2.event "test-hook" &&
790 test_config hook.series-2.command "echo 2" --add &&
791
792 mkdir -p .git/hooks &&
793 write_script .git/hooks/test-hook <<-EOF &&
794 echo 3
795 EOF
796
797 cat >expected <<-\EOF &&
798 1
799 2
800 3
801 EOF
802
803 git hook run --allow-unknown-hook-name -j1 test-hook 2>actual &&
804 test_cmp expected actual
805'
806
807test_expect_success 'git hook run -j2 runs hooks in parallel' '
808 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
809 test_when_finished "rm -rf .git/hooks" &&
810
811 mkdir -p .git/hooks &&
812 write_sentinel_hook .git/hooks/test-hook &&
813
814 test_config hook.hook-2.event test-hook &&
815 test_config hook.hook-2.command \
816 "$(sentinel_detector sentinel hook.order)" &&
817 test_config hook.hook-2.parallel true &&
818
819 git hook run --allow-unknown-hook-name -j2 test-hook >out 2>err &&
820 echo parallel >expect &&
821 test_cmp expect hook.order
822'
823
824test_expect_success 'git hook run -j2 overrides parallel=false' '
825 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
826 test_config hook.hook-1.event test-hook &&
827 test_config hook.hook-1.command \
828 "touch sentinel.started; sleep 2; touch sentinel.done" &&
829 # hook-1 intentionally has no parallel=true
830 test_config hook.hook-2.event test-hook &&
831 test_config hook.hook-2.command \
832 "$(sentinel_detector sentinel hook.order)" &&
833 # hook-2 also has no parallel=true
834
835 # -j2 overrides parallel=false; hooks run in parallel with a warning.
836 git hook run --allow-unknown-hook-name -j2 test-hook >out 2>err &&
837 echo parallel >expect &&
838 test_cmp expect hook.order
839'
840
841test_expect_success 'git hook run -j2 warns for hooks not marked parallel=true' '
842 test_config hook.hook-1.event test-hook &&
843 test_config hook.hook-1.command "true" &&
844 test_config hook.hook-2.event test-hook &&
845 test_config hook.hook-2.command "true" &&
846 # neither hook has parallel=true
847
848 git hook run --allow-unknown-hook-name -j2 test-hook >out 2>err &&
849 test_grep "hook .hook-1. is not marked as parallel=true" err &&
850 test_grep "hook .hook-2. is not marked as parallel=true" err
851'
852
853test_expect_success 'hook.jobs=1 config runs hooks in series' '
854 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
855
856 # Use two configured hooks so the execution order is deterministic:
857 # hook-1 (sentinel) is listed before hook-2 (detector), so hook-1
858 # always runs first even in serial mode.
859 test_config hook.hook-1.event test-hook &&
860 test_config hook.hook-1.command \
861 "touch sentinel.started; sleep 2; touch sentinel.done" &&
862 test_config hook.hook-2.event test-hook &&
863 test_config hook.hook-2.command \
864 "$(sentinel_detector sentinel hook.order)" &&
865
866 test_config hook.jobs 1 &&
867
868 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
869 echo serial >expect &&
870 test_cmp expect hook.order
871'
872
873test_expect_success 'hook.jobs=2 config runs hooks in parallel' '
874 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
875 test_when_finished "rm -rf .git/hooks" &&
876
877 mkdir -p .git/hooks &&
878 write_sentinel_hook .git/hooks/test-hook &&
879
880 test_config hook.hook-2.event test-hook &&
881 test_config hook.hook-2.command \
882 "$(sentinel_detector sentinel hook.order)" &&
883 test_config hook.hook-2.parallel true &&
884
885 test_config hook.jobs 2 &&
886
887 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
888 echo parallel >expect &&
889 test_cmp expect hook.order
890'
891
892test_expect_success 'hook.<name>.parallel=true enables parallel execution' '
893 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
894 test_config hook.hook-1.event test-hook &&
895 test_config hook.hook-1.command \
896 "touch sentinel.started; sleep 2; touch sentinel.done" &&
897 test_config hook.hook-1.parallel true &&
898 test_config hook.hook-2.event test-hook &&
899 test_config hook.hook-2.command \
900 "$(sentinel_detector sentinel hook.order)" &&
901 test_config hook.hook-2.parallel true &&
902
903 test_config hook.jobs 2 &&
904
905 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
906 echo parallel >expect &&
907 test_cmp expect hook.order
908'
909
910test_expect_success 'hook.<name>.parallel=false (default) forces serial execution' '
911 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
912 test_config hook.hook-1.event test-hook &&
913 test_config hook.hook-1.command \
914 "touch sentinel.started; sleep 2; touch sentinel.done" &&
915 test_config hook.hook-2.event test-hook &&
916 test_config hook.hook-2.command \
917 "$(sentinel_detector sentinel hook.order)" &&
918
919 test_config hook.jobs 2 &&
920
921 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
922 echo serial >expect &&
923 test_cmp expect hook.order
924'
925
926test_expect_success 'one non-parallel hook forces the whole event to run serially' '
927 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
928 test_config hook.hook-1.event test-hook &&
929 test_config hook.hook-1.command \
930 "touch sentinel.started; sleep 2; touch sentinel.done" &&
931 test_config hook.hook-1.parallel true &&
932 test_config hook.hook-2.event test-hook &&
933 test_config hook.hook-2.command \
934 "$(sentinel_detector sentinel hook.order)" &&
935 # hook-2 has no parallel=true: should force serial for all
936
937 test_config hook.jobs 2 &&
938
939 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
940 echo serial >expect &&
941 test_cmp expect hook.order
942'
943
944test_expect_success 'client hooks: pre-push parallel execution merges stdout to stderr' '
945 test_when_finished "rm -rf remote-par stdout.actual stderr.actual" &&
946 git init --bare remote-par &&
947 git remote add origin-par remote-par &&
948 test_commit par-commit &&
949 mkdir -p .git/hooks &&
950 setup_hooks pre-push &&
951 test_config hook.jobs 2 &&
952 git push origin-par HEAD:main >stdout.actual 2>stderr.actual &&
953 check_stdout_merged_to_stderr pre-push
954'
955
956test_expect_success 'client hooks: pre-push runs in parallel when hook.jobs > 1' '
957 test_when_finished "rm -rf repo-parallel remote-parallel" &&
958 git init --bare remote-parallel &&
959 git init repo-parallel &&
960 git -C repo-parallel remote add origin ../remote-parallel &&
961 test_commit -C repo-parallel A &&
962
963 write_sentinel_hook repo-parallel/.git/hooks/pre-push &&
964 git -C repo-parallel config hook.hook-2.event pre-push &&
965 git -C repo-parallel config hook.hook-2.command \
966 "$(sentinel_detector sentinel hook.order)" &&
967 git -C repo-parallel config hook.hook-2.parallel true &&
968
969 git -C repo-parallel config hook.jobs 2 &&
970
971 git -C repo-parallel push origin HEAD >out 2>err &&
972 echo parallel >expect &&
973 test_cmp expect repo-parallel/hook.order
974'
975
976test_expect_success 'hook.jobs=2 is ignored for force-serial hooks (pre-commit)' '
977 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
978 test_config hook.hook-1.event pre-commit &&
979 test_config hook.hook-1.command \
980 "touch sentinel.started; sleep 2; touch sentinel.done" &&
981 test_config hook.hook-1.parallel true &&
982 test_config hook.hook-2.event pre-commit &&
983 test_config hook.hook-2.command \
984 "$(sentinel_detector sentinel hook.order)" &&
985 test_config hook.hook-2.parallel true &&
986 test_config hook.jobs 2 &&
987 git commit --allow-empty -m "test: verify force-serial on pre-commit" &&
988 echo serial >expect &&
989 test_cmp expect hook.order
990'
991
992test_expect_success 'hook.<event>.jobs overrides hook.jobs for that event' '
993 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
994 test_config hook.hook-1.event test-hook &&
995 test_config hook.hook-1.command \
996 "touch sentinel.started; sleep 2; touch sentinel.done" &&
997 test_config hook.hook-1.parallel true &&
998 test_config hook.hook-2.event test-hook &&
999 test_config hook.hook-2.command \
1000 "$(sentinel_detector sentinel hook.order)" &&
1001 test_config hook.hook-2.parallel true &&
1002
1003 # Global hook.jobs=1 (serial), but per-event override allows parallel.
1004 test_config hook.jobs 1 &&
1005 test_config hook.test-hook.jobs 2 &&
1006
1007 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1008 echo parallel >expect &&
1009 test_cmp expect hook.order
1010'
1011
1012test_expect_success 'hook.<event>.jobs=1 forces serial even when hook.jobs>1' '
1013 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
1014 test_config hook.hook-1.event test-hook &&
1015 test_config hook.hook-1.command \
1016 "touch sentinel.started; sleep 2; touch sentinel.done" &&
1017 test_config hook.hook-1.parallel true &&
1018 test_config hook.hook-2.event test-hook &&
1019 test_config hook.hook-2.command \
1020 "$(sentinel_detector sentinel hook.order)" &&
1021 test_config hook.hook-2.parallel true &&
1022
1023 # Global hook.jobs=4 allows parallel, but per-event override forces serial.
1024 test_config hook.jobs 4 &&
1025 test_config hook.test-hook.jobs 1 &&
1026
1027 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1028 echo serial >expect &&
1029 test_cmp expect hook.order
1030'
1031
1032test_expect_success 'hook.<event>.jobs still requires hook.<name>.parallel=true' '
1033 test_when_finished "rm -f sentinel.started sentinel.done hook.order" &&
1034 test_config hook.hook-1.event test-hook &&
1035 test_config hook.hook-1.command \
1036 "touch sentinel.started; sleep 2; touch sentinel.done" &&
1037 # hook-1 intentionally has no parallel=true
1038 test_config hook.hook-2.event test-hook &&
1039 test_config hook.hook-2.command \
1040 "$(sentinel_detector sentinel hook.order)" &&
1041 # hook-2 also has no parallel=true
1042
1043 # Per-event jobs=2 but no hook has parallel=true: must still run serially.
1044 test_config hook.test-hook.jobs 2 &&
1045
1046 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1047 echo serial >expect &&
1048 test_cmp expect hook.order
1049'
1050
1051test_expect_success 'hook.<friendly-name>.jobs warns when name has .command' '
1052 test_config hook.my-hook.command "true" &&
1053 test_config hook.my-hook.jobs 2 &&
1054 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1055 test_grep "hook.my-hook.jobs.*friendly-name" err
1056'
1057
1058test_expect_success 'hook.<friendly-name>.jobs warns when name has .event' '
1059 test_config hook.my-hook.event test-hook &&
1060 test_config hook.my-hook.command "true" &&
1061 test_config hook.my-hook.jobs 2 &&
1062 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1063 test_grep "hook.my-hook.jobs.*friendly-name" err
1064'
1065
1066test_expect_success 'hook.<friendly-name>.jobs warns when name has .parallel' '
1067 test_config hook.my-hook.event test-hook &&
1068 test_config hook.my-hook.command "true" &&
1069 test_config hook.my-hook.parallel true &&
1070 test_config hook.my-hook.jobs 2 &&
1071 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1072 test_grep "hook.my-hook.jobs.*friendly-name" err
1073'
1074
1075test_expect_success 'hook.<event>.jobs does not warn for a real event name' '
1076 test_config hook.test-hook.jobs 2 &&
1077 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1078 test_grep ! "friendly-name" err
1079'
1080
1081test_expect_success 'hook.jobs=-1 resolves to online_cpus()' '
1082 test_config hook.hook-1.event test-hook &&
1083 test_config hook.hook-1.command "true" &&
1084 test_config hook.hook-1.parallel true &&
1085
1086 test_config hook.jobs -1 &&
1087
1088 cpus=$(test-tool online-cpus) &&
1089 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1090 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1091 test_grep "\"region_enter\".*\"hook\".*\"test-hook\".*\"max:$cpus\"" trace.txt
1092'
1093
1094test_expect_success 'hook.<friendly-name>.parallel without a value means true' '
1095 test_config hook.hook-1.event test-hook &&
1096 test_config hook.hook-1.command "true" &&
1097 test_config hook.jobs 2 &&
1098
1099 GIT_TRACE2_EVENT="$(pwd)/trace-parallel.txt" \
1100 git -c hook.hook-1.parallel \
1101 hook run --allow-unknown-hook-name test-hook >out 2>err &&
1102 test_grep "\"region_enter\".*\"hook\".*\"test-hook\".*\"max:2\"" trace-parallel.txt
1103'
1104
1105test_expect_success 'hook.<event>.jobs=-1 resolves to online_cpus()' '
1106 test_config hook.hook-1.event test-hook &&
1107 test_config hook.hook-1.command "true" &&
1108 test_config hook.hook-1.parallel true &&
1109
1110 test_config hook.test-hook.jobs -1 &&
1111
1112 cpus=$(test-tool online-cpus) &&
1113 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1114 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1115 test_grep "\"region_enter\".*\"hook\".*\"test-hook\".*\"max:$cpus\"" trace.txt
1116'
1117
1118test_expect_success 'git hook run -j-1 resolves to online_cpus()' '
1119 test_config hook.hook-1.event test-hook &&
1120 test_config hook.hook-1.command "true" &&
1121 test_config hook.hook-1.parallel true &&
1122
1123 cpus=$(test-tool online-cpus) &&
1124 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1125 git hook run --allow-unknown-hook-name -j-1 test-hook >out 2>err &&
1126 test_grep "\"region_enter\".*\"hook\".*\"test-hook\".*\"max:$cpus\"" trace.txt
1127'
1128
1129test_expect_success 'hook.jobs rejects values less than -1' '
1130 test_config hook.jobs -2 &&
1131 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1132 test_grep "hook.jobs must be a positive integer or -1" err
1133'
1134
1135test_expect_success 'hook.<event>.jobs rejects values less than -1' '
1136 test_config hook.test-hook.jobs -5 &&
1137 git hook run --allow-unknown-hook-name --ignore-missing test-hook >out 2>err &&
1138 test_grep "hook.test-hook.jobs must be a positive integer or -1" err
1139'
1140
1141test_expect_success 'hook.<event>.enabled=false skips all hooks for event' '
1142 test_config hook.hook-1.event test-hook &&
1143 test_config hook.hook-1.command "echo ran" &&
1144 test_hook test-hook <<-\EOF &&
1145 echo ran
1146 EOF
1147 test_config hook.test-hook.enabled false &&
1148 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1149 test_must_be_empty out &&
1150 test_must_be_empty err
1151'
1152
1153test_expect_success 'hook.<event>.enabled=true does not suppress hooks' '
1154 test_config hook.hook-1.event test-hook &&
1155 test_config hook.hook-1.command "echo ran" &&
1156 test_config hook.test-hook.enabled true &&
1157 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1158 test_grep "ran" err
1159'
1160
1161test_expect_success 'hook.<event>.enabled=false does not affect other events' '
1162 test_config hook.hook-1.event test-hook &&
1163 test_config hook.hook-1.command "echo ran" &&
1164 test_hook test-hook <<-\EOF &&
1165 echo hookdir ran
1166 EOF
1167 test_config hook.other-event.enabled false &&
1168 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1169 test_grep "^ran$" err &&
1170 test_grep "^hookdir ran$" err
1171'
1172
1173test_expect_success 'hook.<friendly-name>.enabled=false still disables that hook' '
1174 test_config hook.hook-1.event test-hook &&
1175 test_config hook.hook-1.command "echo hook-1" &&
1176 test_config hook.hook-2.event test-hook &&
1177 test_config hook.hook-2.command "echo hook-2" &&
1178 test_config hook.hook-1.enabled false &&
1179 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1180 test_grep ! "hook-1" err &&
1181 test_grep "hook-2" err
1182'
1183
1184test_expect_success 'git hook list shows event-disabled hooks as event-disabled' '
1185 test_config hook.hook-1.event test-hook &&
1186 test_config hook.hook-1.command "echo ran" &&
1187 test_config hook.hook-2.event test-hook &&
1188 test_config hook.hook-2.command "echo ran" &&
1189 test_hook test-hook <<-\EOF &&
1190 echo ran
1191 EOF
1192 test_config hook.test-hook.enabled false &&
1193 git hook list --allow-unknown-hook-name test-hook >actual &&
1194 test_grep "^event-disabled hook-1$" actual &&
1195 test_grep "^event-disabled hook-2$" actual &&
1196 test_grep "^event-disabled hook from hookdir$" actual
1197'
1198
1199test_expect_success 'git hook list shows scope with event-disabled' '
1200 test_config hook.hook-1.event test-hook &&
1201 test_config hook.hook-1.command "echo ran" &&
1202 test_config hook.test-hook.enabled false &&
1203 git hook list --allow-unknown-hook-name --show-scope test-hook >actual &&
1204 test_grep "^local event-disabled hook-1$" actual
1205'
1206
1207test_expect_success 'git hook list still shows hooks when event is disabled' '
1208 test_config hook.hook-1.event test-hook &&
1209 test_config hook.hook-1.command "echo ran" &&
1210 test_config hook.test-hook.enabled false &&
1211 git hook list --allow-unknown-hook-name test-hook >actual &&
1212 test_grep "event-disabled" actual
1213'
1214
1215test_expect_success 'commit skips the hookdir hook with hook.pre-commit.enabled=false' '
1216 test_hook pre-commit <<-\EOF &&
1217 exit 1
1218 EOF
1219 test_must_fail git commit --allow-empty -m blocked &&
1220 git -c hook.pre-commit.enabled=false commit --allow-empty -m allowed
1221'
1222
1223test_expect_success 'friendly-name matching known event name is rejected' '
1224 test_config hook.pre-commit.event pre-commit &&
1225 test_config hook.pre-commit.command "echo oops" &&
1226 test_must_fail git hook run pre-commit 2>err &&
1227 test_grep "collides with a known event name" err
1228'
1229
1230test_expect_success 'friendly-name matching known event name is rejected even for different event' '
1231 test_config hook.pre-commit.event post-commit &&
1232 test_config hook.pre-commit.command "echo oops" &&
1233 test_must_fail git hook run post-commit 2>err &&
1234 test_grep "collides with a known event name" err
1235'
1236
1237test_expect_success 'friendly-name matching unknown event warns' '
1238 test_config hook.test-hook.event test-hook &&
1239 test_config hook.test-hook.command "echo ran" &&
1240 git hook run --allow-unknown-hook-name test-hook >out 2>err &&
1241 test_grep "same as its event" err
1242'
1243
1244test_expect_success 'hooks in parallel that do not read input' '
1245 # Add this to our $PATH to avoid having to write the whole trash
1246 # directory into our config options, which would require quoting.
1247 mkdir bin &&
1248 PATH=$PWD/bin:$PATH &&
1249
1250 write_script bin/hook-fast <<-\EOF &&
1251 # This hook does not read its input, so the parent process
1252 # may see SIGPIPE if it is not ignored. It should happen
1253 # relatively quickly.
1254 exit 0
1255 EOF
1256
1257 write_script bin/hook-slow <<-\EOF &&
1258 # This hook is slow, so we expect it to still be running
1259 # when the other hook has exited (and the parent has a pipe error
1260 # writing to it).
1261 #
1262 # So we want to be slow enough that we expect this to happen, but not
1263 # so slow that the test takes forever. 1 second is probably enough
1264 # in practice (and if it is occasionally not on a loaded system, we
1265 # will err on the side of having the test pass).
1266 sleep 1
1267 exit 0
1268 EOF
1269
1270 git init --bare parallel.git &&
1271 git -C parallel.git config hook.fast.command "hook-fast" &&
1272 git -C parallel.git config hook.fast.event pre-receive &&
1273 git -C parallel.git config hook.fast.parallel true &&
1274 git -C parallel.git config hook.slow.command "hook-slow" &&
1275 git -C parallel.git config hook.slow.event pre-receive &&
1276 git -C parallel.git config hook.slow.parallel true &&
1277 git -C parallel.git config hook.jobs 2 &&
1278
1279 git push ./parallel.git "+refs/heads/*:refs/heads/*"
1280'
1281
1282test_done