]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7006-pager.sh
Merge branch 'ks/doc-checkout-previous'
[thirdparty/git.git] / t / t7006-pager.sh
CommitLineData
60b6e220
JN
1#!/bin/sh
2
3test_description='Test automatic use of a pager.'
4
5. ./test-lib.sh
678e484b 6. "$TEST_DIRECTORY"/lib-pager.sh
cc4e48fc 7. "$TEST_DIRECTORY"/lib-terminal.sh
60b6e220 8
60b6e220 9test_expect_success 'setup' '
00648ba0 10 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
8d68a6d5 11 test_unconfig core.pager &&
fdf1bc48
JN
12
13 PAGER="cat >paginated.out" &&
14 export PAGER &&
15
60b6e220
JN
16 test_commit initial
17'
18
60b6e220 19test_expect_success TTY 'some commands use a pager' '
0d164519 20 rm -f paginated.out &&
2d3ca216 21 test_terminal git log &&
60b6e220
JN
22 test -e paginated.out
23'
24
2cb60093
NTND
25test_expect_failure TTY 'pager runs from subdir' '
26 echo subdir/paginated.out >expected &&
27 mkdir -p subdir &&
28 rm -f paginated.out subdir/paginated.out &&
29 (
30 cd subdir &&
31 test_terminal git log
32 ) &&
33 {
34 ls paginated.out subdir/paginated.out ||
35 :
36 } >actual &&
37 test_cmp expected actual
38'
39
e54c1f2d
JN
40test_expect_success TTY 'LESS and LV envvars are set for pagination' '
41 (
42 sane_unset LESS LV &&
52c02f65 43 PAGER="env >pager-env.out; wc" &&
e54c1f2d
JN
44 export PAGER &&
45
46 test_terminal git log
47 ) &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
50'
51
995bc22d
EW
52test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
53 (
54 sane_unset LESS LV &&
55 PAGER="env >pager-env.out; wc" &&
56 export PAGER &&
57 PATH="$(git --exec-path):$PATH" &&
58 export PATH &&
59 test_terminal sh -c ". git-sh-setup && git_pager"
60 ) &&
61 grep ^LESS= pager-env.out &&
62 grep ^LV= pager-env.out
63'
64
60b6e220 65test_expect_success TTY 'some commands do not use a pager' '
0d164519 66 rm -f paginated.out &&
2d3ca216 67 test_terminal git rev-list HEAD &&
60b6e220
JN
68 ! test -e paginated.out
69'
70
60b6e220 71test_expect_success 'no pager when stdout is a pipe' '
0d164519 72 rm -f paginated.out &&
60b6e220
JN
73 git log | cat &&
74 ! test -e paginated.out
75'
76
60b6e220 77test_expect_success 'no pager when stdout is a regular file' '
0d164519 78 rm -f paginated.out &&
fdf1bc48 79 git log >file &&
60b6e220
JN
80 ! test -e paginated.out
81'
82
60b6e220 83test_expect_success TTY 'git --paginate rev-list uses a pager' '
0d164519 84 rm -f paginated.out &&
2d3ca216 85 test_terminal git --paginate rev-list HEAD &&
60b6e220
JN
86 test -e paginated.out
87'
88
60b6e220 89test_expect_success 'no pager even with --paginate when stdout is a pipe' '
0d164519 90 rm -f file paginated.out &&
60b6e220
JN
91 git --paginate log | cat &&
92 ! test -e paginated.out
93'
94
60b6e220 95test_expect_success TTY 'no pager with --no-pager' '
0d164519 96 rm -f paginated.out &&
2d3ca216 97 test_terminal git --no-pager log &&
60b6e220
JN
98 ! test -e paginated.out
99'
100
ff38d1a9
NTND
101test_expect_success TTY 'configuration can disable pager' '
102 rm -f paginated.out &&
8d68a6d5 103 test_unconfig pager.grep &&
ff38d1a9
NTND
104 test_terminal git grep initial &&
105 test -e paginated.out &&
106
107 rm -f paginated.out &&
8d68a6d5 108 test_config pager.grep false &&
ff38d1a9
NTND
109 test_terminal git grep initial &&
110 ! test -e paginated.out
111'
112
41bf3bc2 113test_expect_success TTY 'git config uses a pager if configured to' '
3ba7e6e2 114 rm -f paginated.out &&
8d68a6d5 115 test_config pager.config true &&
3ba7e6e2
NTND
116 test_terminal git config --list &&
117 test -e paginated.out
118'
119
41bf3bc2 120test_expect_success TTY 'configuration can enable pager (from subdir)' '
2cb60093
NTND
121 rm -f paginated.out &&
122 mkdir -p subdir &&
8d68a6d5 123 test_config pager.bundle true &&
2cb60093
NTND
124
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
127 (
128 cd subdir &&
129 test_terminal git bundle unbundle ../test.bundle
130 ) &&
131 {
132 test -e paginated.out ||
133 test -e subdir/paginated.out
134 }
135'
136
ff1e7248 137test_expect_success TTY 'git tag -l defaults to paging' '
b3ee740c
138 rm -f paginated.out &&
139 test_terminal git tag -l &&
ff1e7248 140 test -e paginated.out
b3ee740c
141'
142
143test_expect_success TTY 'git tag -l respects pager.tag' '
144 rm -f paginated.out &&
ff1e7248
145 test_terminal git -c pager.tag=false tag -l &&
146 ! test -e paginated.out
b3ee740c
147'
148
149test_expect_success TTY 'git tag -l respects --no-pager' '
150 rm -f paginated.out &&
151 test_terminal git -c pager.tag --no-pager tag -l &&
152 ! test -e paginated.out
153'
154
ff1e7248 155test_expect_success TTY 'git tag with no args defaults to paging' '
b3ee740c
156 # no args implies -l so this should page like -l
157 rm -f paginated.out &&
158 test_terminal git tag &&
ff1e7248 159 test -e paginated.out
b3ee740c
160'
161
162test_expect_success TTY 'git tag with no args respects pager.tag' '
163 # no args implies -l so this should page like -l
164 rm -f paginated.out &&
ff1e7248
165 test_terminal git -c pager.tag=false tag &&
166 ! test -e paginated.out
b3ee740c
167'
168
ff1e7248 169test_expect_success TTY 'git tag --contains defaults to paging' '
b3ee740c
170 # --contains implies -l so this should page like -l
171 rm -f paginated.out &&
172 test_terminal git tag --contains &&
ff1e7248 173 test -e paginated.out
b3ee740c
174'
175
176test_expect_success TTY 'git tag --contains respects pager.tag' '
177 # --contains implies -l so this should page like -l
178 rm -f paginated.out &&
ff1e7248
179 test_terminal git -c pager.tag=false tag --contains &&
180 ! test -e paginated.out
b3ee740c
181'
182
183test_expect_success TTY 'git tag -a defaults to not paging' '
184 test_when_finished "git tag -d newtag" &&
185 rm -f paginated.out &&
186 test_terminal git tag -am message newtag &&
187 ! test -e paginated.out
188'
189
de121ffe 190test_expect_success TTY 'git tag -a ignores pager.tag' '
b3ee740c
191 test_when_finished "git tag -d newtag" &&
192 rm -f paginated.out &&
193 test_terminal git -c pager.tag tag -am message newtag &&
194 ! test -e paginated.out
195'
196
197test_expect_success TTY 'git tag -a respects --paginate' '
198 test_when_finished "git tag -d newtag" &&
199 rm -f paginated.out &&
200 test_terminal git --paginate tag -am message newtag &&
201 test -e paginated.out
202'
203
595d59e2 204test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
de121ffe
205 test_when_finished "git tag -d newtag" &&
206 rm -f paginated.out &&
207 test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
208 ! test -e paginated.out
209'
210
211test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
212 rm -f paginated.out &&
ff1e7248
213 test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
214 ! test -e paginated.out
de121ffe
215'
216
0ae19de7 217test_expect_success TTY 'git branch defaults to paging' '
ed104fa9
218 rm -f paginated.out &&
219 test_terminal git branch &&
0ae19de7 220 test -e paginated.out
ed104fa9
221'
222
223test_expect_success TTY 'git branch respects pager.branch' '
224 rm -f paginated.out &&
0ae19de7
225 test_terminal git -c pager.branch=false branch &&
226 ! test -e paginated.out
ed104fa9
227'
228
229test_expect_success TTY 'git branch respects --no-pager' '
230 rm -f paginated.out &&
0ae19de7 231 test_terminal git --no-pager branch &&
ed104fa9
232 ! test -e paginated.out
233'
234
d74b541e 235test_expect_success TTY 'git branch --edit-description ignores pager.branch' '
ed104fa9
236 rm -f paginated.out editor.used &&
237 write_script editor <<-\EOF &&
238 echo "New description" >"$1"
239 touch editor.used
240 EOF
241 EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
242 ! test -e paginated.out &&
243 test -e editor.used
244'
245
d74b541e 246test_expect_success TTY 'git branch --set-upstream-to ignores pager.branch' '
ed104fa9
247 rm -f paginated.out &&
248 git branch other &&
249 test_when_finished "git branch -D other" &&
250 test_terminal git -c pager.branch branch --set-upstream-to=other &&
251 test_when_finished "git branch --unset-upstream" &&
d74b541e 252 ! test -e paginated.out
ed104fa9
253'
254
60b6e220
JN
255# A colored commit log will begin with an appropriate ANSI escape
256# for the first color; the text "commit" comes later.
257colorful() {
fdf1bc48 258 read firstline <$1
e0ae1e6f 259 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
60b6e220
JN
260}
261
60b6e220 262test_expect_success 'tests can detect color' '
0d164519 263 rm -f colorful.log colorless.log &&
fdf1bc48
JN
264 git log --no-color >colorless.log &&
265 git log --color >colorful.log &&
60b6e220
JN
266 ! colorful colorless.log &&
267 colorful colorful.log
268'
269
60b6e220 270test_expect_success 'no color when stdout is a regular file' '
fdf1bc48 271 rm -f colorless.log &&
0d164519 272 test_config color.ui auto &&
fdf1bc48 273 git log >colorless.log &&
60b6e220
JN
274 ! colorful colorless.log
275'
276
60b6e220 277test_expect_success TTY 'color when writing to a pager' '
fdf1bc48 278 rm -f paginated.out &&
0d164519 279 test_config color.ui auto &&
e433749d 280 test_terminal git log &&
60b6e220
JN
281 colorful paginated.out
282'
283
daa0c3d9
JK
284test_expect_success TTY 'colors are suppressed by color.pager' '
285 rm -f paginated.out &&
286 test_config color.ui auto &&
287 test_config color.pager false &&
e433749d 288 test_terminal git log &&
daa0c3d9
JK
289 ! colorful paginated.out
290'
291
60b6e220 292test_expect_success 'color when writing to a file intended for a pager' '
fdf1bc48 293 rm -f colorful.log &&
0d164519 294 test_config color.ui auto &&
fdf1bc48
JN
295 (
296 TERM=vt100 &&
297 GIT_PAGER_IN_USE=true &&
298 export TERM GIT_PAGER_IN_USE &&
299 git log >colorful.log
300 ) &&
60b6e220
JN
301 colorful colorful.log
302'
303
2e6c012e
JK
304test_expect_success TTY 'colors are sent to pager for external commands' '
305 test_config alias.externallog "!git log" &&
306 test_config color.ui auto &&
e433749d 307 test_terminal git -p externallog &&
2e6c012e
JK
308 colorful paginated.out
309'
310
3c7406d4
JN
311# Use this helper to make it easy for the caller of your
312# terminal-using function to specify whether it should fail.
313# If you write
314#
315# your_test() {
316# parse_args "$@"
317#
318# $test_expectation "$cmd - behaves well" "
319# ...
320# $full_command &&
321# ...
322# "
323# }
324#
325# then your test can be used like this:
326#
327# your_test expect_(success|failure) [test_must_fail] 'git foo'
328#
329parse_args() {
330 test_expectation="test_$1"
331 shift
332 if test "$1" = test_must_fail
333 then
334 full_command="test_must_fail test_terminal "
335 shift
336 else
337 full_command="test_terminal "
338 fi
339 cmd=$1
340 full_command="$full_command $1"
341}
fdf1bc48 342
8f81449e
JN
343test_default_pager() {
344 parse_args "$@"
345
996621eb 346 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
00648ba0 347 sane_unset PAGER GIT_PAGER &&
8d68a6d5 348 test_unconfig core.pager &&
0d164519 349 rm -f default_pager_used &&
8f81449e
JN
350 cat >\$less <<-\EOF &&
351 #!/bin/sh
352 wc >default_pager_used
353 EOF
354 chmod +x \$less &&
355 (
356 PATH=.:\$PATH &&
357 export PATH &&
358 $full_command
359 ) &&
360 test -e default_pager_used
361 "
362}
60b6e220 363
8f81449e
JN
364test_PAGER_overrides() {
365 parse_args "$@"
fdf1bc48 366
8f81449e 367 $test_expectation TTY "$cmd - PAGER overrides default pager" "
00648ba0 368 sane_unset GIT_PAGER &&
8d68a6d5 369 test_unconfig core.pager &&
0d164519 370 rm -f PAGER_used &&
8f81449e
JN
371 PAGER='wc >PAGER_used' &&
372 export PAGER &&
373 $full_command &&
374 test -e PAGER_used
375 "
376}
fdf1bc48 377
8f81449e 378test_core_pager_overrides() {
73e25e7c
NTND
379 if_local_config=
380 used_if_wanted='overrides PAGER'
381 test_core_pager "$@"
382}
60b6e220 383
73e25e7c
NTND
384test_local_config_ignored() {
385 if_local_config='! '
386 used_if_wanted='is not used'
387 test_core_pager "$@"
388}
fdf1bc48 389
73e25e7c 390test_core_pager() {
8f81449e
JN
391 parse_args "$@"
392
73e25e7c 393 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
00648ba0 394 sane_unset GIT_PAGER &&
0d164519 395 rm -f core.pager_used &&
8f81449e
JN
396 PAGER=wc &&
397 export PAGER &&
8d68a6d5 398 test_config core.pager 'wc >core.pager_used' &&
8f81449e 399 $full_command &&
73e25e7c 400 ${if_local_config}test -e core.pager_used
8f81449e
JN
401 "
402}
403
bce2c9ae 404test_core_pager_subdir() {
73e25e7c
NTND
405 if_local_config=
406 used_if_wanted='overrides PAGER'
407 test_pager_subdir_helper "$@"
408}
409
410test_no_local_config_subdir() {
411 if_local_config='! '
412 used_if_wanted='is not used'
413 test_pager_subdir_helper "$@"
414}
415
416test_pager_subdir_helper() {
bce2c9ae
JN
417 parse_args "$@"
418
73e25e7c 419 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
00648ba0 420 sane_unset GIT_PAGER &&
bce2c9ae 421 rm -f core.pager_used &&
0d164519 422 rm -fr sub &&
bce2c9ae
JN
423 PAGER=wc &&
424 stampname=\$(pwd)/core.pager_used &&
425 export PAGER stampname &&
8d68a6d5 426 test_config core.pager 'wc >\"\$stampname\"' &&
bce2c9ae
JN
427 mkdir sub &&
428 (
429 cd sub &&
430 $full_command
431 ) &&
73e25e7c 432 ${if_local_config}test -e core.pager_used
bce2c9ae
JN
433 "
434}
435
8f81449e
JN
436test_GIT_PAGER_overrides() {
437 parse_args "$@"
438
439 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
0d164519 440 rm -f GIT_PAGER_used &&
8d68a6d5 441 test_config core.pager wc &&
8f81449e
JN
442 GIT_PAGER='wc >GIT_PAGER_used' &&
443 export GIT_PAGER &&
444 $full_command &&
445 test -e GIT_PAGER_used
446 "
447}
fdf1bc48 448
73e25e7c
NTND
449test_doesnt_paginate() {
450 parse_args "$@"
451
452 $test_expectation TTY "no pager for '$cmd'" "
0d164519 453 rm -f GIT_PAGER_used &&
73e25e7c
NTND
454 GIT_PAGER='wc >GIT_PAGER_used' &&
455 export GIT_PAGER &&
456 $full_command &&
457 ! test -e GIT_PAGER_used
458 "
459}
460
030149a4
JN
461test_pager_choices() {
462 test_default_pager expect_success "$@"
463 test_PAGER_overrides expect_success "$@"
464 test_core_pager_overrides expect_success "$@"
465 test_core_pager_subdir expect_success "$@"
466 test_GIT_PAGER_overrides expect_success "$@"
467}
468
469test_expect_success 'setup: some aliases' '
470 git config alias.aliasedlog log &&
471 git config alias.true "!true"
60b6e220
JN
472'
473
030149a4
JN
474test_pager_choices 'git log'
475test_pager_choices 'git -p log'
476test_pager_choices 'git aliasedlog'
477
478test_default_pager expect_success 'git -p aliasedlog'
479test_PAGER_overrides expect_success 'git -p aliasedlog'
480test_core_pager_overrides expect_success 'git -p aliasedlog'
1a27409a 481test_core_pager_subdir expect_success 'git -p aliasedlog'
030149a4
JN
482test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
483
484test_default_pager expect_success 'git -p true'
485test_PAGER_overrides expect_success 'git -p true'
486test_core_pager_overrides expect_success 'git -p true'
1a27409a 487test_core_pager_subdir expect_success 'git -p true'
030149a4
JN
488test_GIT_PAGER_overrides expect_success 'git -p true'
489
490test_default_pager expect_success test_must_fail 'git -p request-pull'
491test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
492test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
1a27409a 493test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
030149a4 494test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
8f81449e
JN
495
496test_default_pager expect_success test_must_fail 'git -p'
497test_PAGER_overrides expect_success test_must_fail 'git -p'
73e25e7c 498test_local_config_ignored expect_failure test_must_fail 'git -p'
8f81449e
JN
499test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
500
1a27409a 501test_expect_success TTY 'core.pager in repo config works and retains cwd' '
551d535d
JS
502 sane_unset GIT_PAGER &&
503 test_config core.pager "cat >cwd-retained" &&
504 (
505 cd sub &&
506 rm -f cwd-retained &&
507 test_terminal git -p rev-parse HEAD &&
508 test_path_is_file cwd-retained
509 )
510'
511
a9bcf658 512test_expect_success TTY 'core.pager is found via alias in subdirectory' '
3f9c5dfb
JS
513 sane_unset GIT_PAGER &&
514 test_config core.pager "cat >via-alias" &&
515 (
516 cd sub &&
517 rm -f via-alias &&
518 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
519 test_path_is_file via-alias
520 )
521'
522
030149a4 523test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
60b6e220 524
773b69bf
NTND
525test_pager_choices 'git shortlog'
526test_expect_success 'setup: configure shortlog not to paginate' '
527 git config pager.shortlog false
528'
529test_doesnt_paginate expect_success 'git shortlog'
530test_no_local_config_subdir expect_success 'git shortlog'
531test_default_pager expect_success 'git -p shortlog'
532test_core_pager_subdir expect_success 'git -p shortlog'
533
d1ea8962
NTND
534test_core_pager_subdir expect_success test_must_fail \
535 'git -p apply </dev/null'
536
9bad7233 537test_expect_success TTY 'command-specific pager' '
212ad944 538 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
539 echo "foo:initial" >expect &&
540 >actual &&
8d68a6d5
JK
541 test_unconfig core.pager &&
542 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
543 test_terminal git log --format=%s -1 &&
544 test_cmp expect actual
545'
546
547test_expect_success TTY 'command-specific pager overrides core.pager' '
212ad944 548 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
549 echo "foo:initial" >expect &&
550 >actual &&
99094a7a 551 test_config core.pager "exit 1" &&
8d68a6d5 552 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
553 test_terminal git log --format=%s -1 &&
554 test_cmp expect actual
555'
556
557test_expect_success TTY 'command-specific pager overridden by environment' '
558 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
559 >actual &&
560 echo "foo:initial" >expect &&
8d68a6d5 561 test_config pager.log "exit 1" &&
9bad7233
JK
562 test_terminal git log --format=%s -1 &&
563 test_cmp expect actual
564'
565
92058e4d
JK
566test_expect_success 'setup external command' '
567 cat >git-external <<-\EOF &&
568 #!/bin/sh
569 git "$@"
570 EOF
571 chmod +x git-external
572'
573
574test_expect_success TTY 'command-specific pager works for external commands' '
575 sane_unset PAGER GIT_PAGER &&
576 echo "foo:initial" >expect &&
577 >actual &&
578 test_config pager.external "sed s/^/foo:/ >actual" &&
90ae5d27 579 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
92058e4d
JK
580 test_cmp expect actual
581'
582
583test_expect_success TTY 'sub-commands of externals use their own pager' '
584 sane_unset PAGER GIT_PAGER &&
585 echo "foo:initial" >expect &&
586 >actual &&
587 test_config pager.log "sed s/^/foo:/ >actual" &&
588 test_terminal git --exec-path=. external log --format=%s -1 &&
589 test_cmp expect actual
590'
591
592test_expect_success TTY 'external command pagers override sub-commands' '
593 sane_unset PAGER GIT_PAGER &&
594 >expect &&
595 >actual &&
596 test_config pager.external false &&
597 test_config pager.log "sed s/^/log:/ >actual" &&
598 test_terminal git --exec-path=. external log --format=%s -1 &&
599 test_cmp expect actual
600'
601
9e9de18f
JK
602test_expect_success 'command with underscores does not complain' '
603 write_script git-under_score <<-\EOF &&
604 echo ok
605 EOF
606 git --exec-path=. under_score >actual 2>&1 &&
607 echo ok >expect &&
608 test_cmp expect actual
609'
610
b2d3fd28
KD
611test_expect_success TTY 'git tag with auto-columns ' '
612 test_commit one &&
613 test_commit two &&
614 test_commit three &&
615 test_commit four &&
616 test_commit five &&
617 cat >expect <<-\EOF &&
618 initial one two three four five
619 EOF
620 test_terminal env PAGER="cat >actual" COLUMNS=80 \
621 git -c column.ui=auto tag --sort=authordate &&
622 test_cmp expect actual
623'
624
60b6e220 625test_done