tag: respect `pager.tag` in list-mode only
[git/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
b3ee740c
137test_expect_success TTY 'git tag -l defaults to not paging' '
138 rm -f paginated.out &&
139 test_terminal git tag -l &&
140 ! test -e paginated.out
141'
142
143test_expect_success TTY 'git tag -l respects pager.tag' '
144 rm -f paginated.out &&
145 test_terminal git -c pager.tag tag -l &&
146 test -e paginated.out
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
155test_expect_success TTY 'git tag with no args defaults to not paging' '
156 # no args implies -l so this should page like -l
157 rm -f paginated.out &&
158 test_terminal git tag &&
159 ! test -e paginated.out
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 &&
165 test_terminal git -c pager.tag tag &&
166 test -e paginated.out
167'
168
169test_expect_success TTY 'git tag --contains defaults to not paging' '
170 # --contains implies -l so this should page like -l
171 rm -f paginated.out &&
172 test_terminal git tag --contains &&
173 ! test -e paginated.out
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 &&
179 test_terminal git -c pager.tag tag --contains &&
180 test -e paginated.out
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
de121ffe
204test_expect_failure TTY 'git tag as alias ignores pager.tag with -a' '
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 &&
213 test_terminal git -c pager.tag -c alias.t=tag t -l &&
214 test -e paginated.out
215'
216
60b6e220
JN
217# A colored commit log will begin with an appropriate ANSI escape
218# for the first color; the text "commit" comes later.
219colorful() {
fdf1bc48 220 read firstline <$1
e0ae1e6f 221 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
60b6e220
JN
222}
223
60b6e220 224test_expect_success 'tests can detect color' '
0d164519 225 rm -f colorful.log colorless.log &&
fdf1bc48
JN
226 git log --no-color >colorless.log &&
227 git log --color >colorful.log &&
60b6e220
JN
228 ! colorful colorless.log &&
229 colorful colorful.log
230'
231
60b6e220 232test_expect_success 'no color when stdout is a regular file' '
fdf1bc48 233 rm -f colorless.log &&
0d164519 234 test_config color.ui auto &&
fdf1bc48 235 git log >colorless.log &&
60b6e220
JN
236 ! colorful colorless.log
237'
238
60b6e220 239test_expect_success TTY 'color when writing to a pager' '
fdf1bc48 240 rm -f paginated.out &&
0d164519 241 test_config color.ui auto &&
512477b1 242 test_terminal env TERM=vt100 git log &&
60b6e220
JN
243 colorful paginated.out
244'
245
daa0c3d9
JK
246test_expect_success TTY 'colors are suppressed by color.pager' '
247 rm -f paginated.out &&
248 test_config color.ui auto &&
249 test_config color.pager false &&
512477b1 250 test_terminal env TERM=vt100 git log &&
daa0c3d9
JK
251 ! colorful paginated.out
252'
253
60b6e220 254test_expect_success 'color when writing to a file intended for a pager' '
fdf1bc48 255 rm -f colorful.log &&
0d164519 256 test_config color.ui auto &&
fdf1bc48
JN
257 (
258 TERM=vt100 &&
259 GIT_PAGER_IN_USE=true &&
260 export TERM GIT_PAGER_IN_USE &&
261 git log >colorful.log
262 ) &&
60b6e220
JN
263 colorful colorful.log
264'
265
2e6c012e
JK
266test_expect_success TTY 'colors are sent to pager for external commands' '
267 test_config alias.externallog "!git log" &&
268 test_config color.ui auto &&
512477b1 269 test_terminal env TERM=vt100 git -p externallog &&
2e6c012e
JK
270 colorful paginated.out
271'
272
3c7406d4
JN
273# Use this helper to make it easy for the caller of your
274# terminal-using function to specify whether it should fail.
275# If you write
276#
277# your_test() {
278# parse_args "$@"
279#
280# $test_expectation "$cmd - behaves well" "
281# ...
282# $full_command &&
283# ...
284# "
285# }
286#
287# then your test can be used like this:
288#
289# your_test expect_(success|failure) [test_must_fail] 'git foo'
290#
291parse_args() {
292 test_expectation="test_$1"
293 shift
294 if test "$1" = test_must_fail
295 then
296 full_command="test_must_fail test_terminal "
297 shift
298 else
299 full_command="test_terminal "
300 fi
301 cmd=$1
302 full_command="$full_command $1"
303}
fdf1bc48 304
8f81449e
JN
305test_default_pager() {
306 parse_args "$@"
307
996621eb 308 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
00648ba0 309 sane_unset PAGER GIT_PAGER &&
8d68a6d5 310 test_unconfig core.pager &&
0d164519 311 rm -f default_pager_used &&
8f81449e
JN
312 cat >\$less <<-\EOF &&
313 #!/bin/sh
314 wc >default_pager_used
315 EOF
316 chmod +x \$less &&
317 (
318 PATH=.:\$PATH &&
319 export PATH &&
320 $full_command
321 ) &&
322 test -e default_pager_used
323 "
324}
60b6e220 325
8f81449e
JN
326test_PAGER_overrides() {
327 parse_args "$@"
fdf1bc48 328
8f81449e 329 $test_expectation TTY "$cmd - PAGER overrides default pager" "
00648ba0 330 sane_unset GIT_PAGER &&
8d68a6d5 331 test_unconfig core.pager &&
0d164519 332 rm -f PAGER_used &&
8f81449e
JN
333 PAGER='wc >PAGER_used' &&
334 export PAGER &&
335 $full_command &&
336 test -e PAGER_used
337 "
338}
fdf1bc48 339
8f81449e 340test_core_pager_overrides() {
73e25e7c
NTND
341 if_local_config=
342 used_if_wanted='overrides PAGER'
343 test_core_pager "$@"
344}
60b6e220 345
73e25e7c
NTND
346test_local_config_ignored() {
347 if_local_config='! '
348 used_if_wanted='is not used'
349 test_core_pager "$@"
350}
fdf1bc48 351
73e25e7c 352test_core_pager() {
8f81449e
JN
353 parse_args "$@"
354
73e25e7c 355 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
00648ba0 356 sane_unset GIT_PAGER &&
0d164519 357 rm -f core.pager_used &&
8f81449e
JN
358 PAGER=wc &&
359 export PAGER &&
8d68a6d5 360 test_config core.pager 'wc >core.pager_used' &&
8f81449e 361 $full_command &&
73e25e7c 362 ${if_local_config}test -e core.pager_used
8f81449e
JN
363 "
364}
365
bce2c9ae 366test_core_pager_subdir() {
73e25e7c
NTND
367 if_local_config=
368 used_if_wanted='overrides PAGER'
369 test_pager_subdir_helper "$@"
370}
371
372test_no_local_config_subdir() {
373 if_local_config='! '
374 used_if_wanted='is not used'
375 test_pager_subdir_helper "$@"
376}
377
378test_pager_subdir_helper() {
bce2c9ae
JN
379 parse_args "$@"
380
73e25e7c 381 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
00648ba0 382 sane_unset GIT_PAGER &&
bce2c9ae 383 rm -f core.pager_used &&
0d164519 384 rm -fr sub &&
bce2c9ae
JN
385 PAGER=wc &&
386 stampname=\$(pwd)/core.pager_used &&
387 export PAGER stampname &&
8d68a6d5 388 test_config core.pager 'wc >\"\$stampname\"' &&
bce2c9ae
JN
389 mkdir sub &&
390 (
391 cd sub &&
392 $full_command
393 ) &&
73e25e7c 394 ${if_local_config}test -e core.pager_used
bce2c9ae
JN
395 "
396}
397
8f81449e
JN
398test_GIT_PAGER_overrides() {
399 parse_args "$@"
400
401 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
0d164519 402 rm -f GIT_PAGER_used &&
8d68a6d5 403 test_config core.pager wc &&
8f81449e
JN
404 GIT_PAGER='wc >GIT_PAGER_used' &&
405 export GIT_PAGER &&
406 $full_command &&
407 test -e GIT_PAGER_used
408 "
409}
fdf1bc48 410
73e25e7c
NTND
411test_doesnt_paginate() {
412 parse_args "$@"
413
414 $test_expectation TTY "no pager for '$cmd'" "
0d164519 415 rm -f GIT_PAGER_used &&
73e25e7c
NTND
416 GIT_PAGER='wc >GIT_PAGER_used' &&
417 export GIT_PAGER &&
418 $full_command &&
419 ! test -e GIT_PAGER_used
420 "
421}
422
030149a4
JN
423test_pager_choices() {
424 test_default_pager expect_success "$@"
425 test_PAGER_overrides expect_success "$@"
426 test_core_pager_overrides expect_success "$@"
427 test_core_pager_subdir expect_success "$@"
428 test_GIT_PAGER_overrides expect_success "$@"
429}
430
431test_expect_success 'setup: some aliases' '
432 git config alias.aliasedlog log &&
433 git config alias.true "!true"
60b6e220
JN
434'
435
030149a4
JN
436test_pager_choices 'git log'
437test_pager_choices 'git -p log'
438test_pager_choices 'git aliasedlog'
439
440test_default_pager expect_success 'git -p aliasedlog'
441test_PAGER_overrides expect_success 'git -p aliasedlog'
442test_core_pager_overrides expect_success 'git -p aliasedlog'
1a27409a 443test_core_pager_subdir expect_success 'git -p aliasedlog'
030149a4
JN
444test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
445
446test_default_pager expect_success 'git -p true'
447test_PAGER_overrides expect_success 'git -p true'
448test_core_pager_overrides expect_success 'git -p true'
1a27409a 449test_core_pager_subdir expect_success 'git -p true'
030149a4
JN
450test_GIT_PAGER_overrides expect_success 'git -p true'
451
452test_default_pager expect_success test_must_fail 'git -p request-pull'
453test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
454test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
1a27409a 455test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
030149a4 456test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
8f81449e
JN
457
458test_default_pager expect_success test_must_fail 'git -p'
459test_PAGER_overrides expect_success test_must_fail 'git -p'
73e25e7c 460test_local_config_ignored expect_failure test_must_fail 'git -p'
8f81449e
JN
461test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
462
1a27409a 463test_expect_success TTY 'core.pager in repo config works and retains cwd' '
551d535d
JS
464 sane_unset GIT_PAGER &&
465 test_config core.pager "cat >cwd-retained" &&
466 (
467 cd sub &&
468 rm -f cwd-retained &&
469 test_terminal git -p rev-parse HEAD &&
470 test_path_is_file cwd-retained
471 )
472'
473
a9bcf658 474test_expect_success TTY 'core.pager is found via alias in subdirectory' '
3f9c5dfb
JS
475 sane_unset GIT_PAGER &&
476 test_config core.pager "cat >via-alias" &&
477 (
478 cd sub &&
479 rm -f via-alias &&
480 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
481 test_path_is_file via-alias
482 )
483'
484
030149a4 485test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
60b6e220 486
773b69bf
NTND
487test_pager_choices 'git shortlog'
488test_expect_success 'setup: configure shortlog not to paginate' '
489 git config pager.shortlog false
490'
491test_doesnt_paginate expect_success 'git shortlog'
492test_no_local_config_subdir expect_success 'git shortlog'
493test_default_pager expect_success 'git -p shortlog'
494test_core_pager_subdir expect_success 'git -p shortlog'
495
d1ea8962
NTND
496test_core_pager_subdir expect_success test_must_fail \
497 'git -p apply </dev/null'
498
9bad7233 499test_expect_success TTY 'command-specific pager' '
212ad944 500 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
501 echo "foo:initial" >expect &&
502 >actual &&
8d68a6d5
JK
503 test_unconfig core.pager &&
504 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
505 test_terminal git log --format=%s -1 &&
506 test_cmp expect actual
507'
508
509test_expect_success TTY 'command-specific pager overrides core.pager' '
212ad944 510 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
511 echo "foo:initial" >expect &&
512 >actual &&
99094a7a 513 test_config core.pager "exit 1" &&
8d68a6d5 514 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
515 test_terminal git log --format=%s -1 &&
516 test_cmp expect actual
517'
518
519test_expect_success TTY 'command-specific pager overridden by environment' '
520 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
521 >actual &&
522 echo "foo:initial" >expect &&
8d68a6d5 523 test_config pager.log "exit 1" &&
9bad7233
JK
524 test_terminal git log --format=%s -1 &&
525 test_cmp expect actual
526'
527
92058e4d
JK
528test_expect_success 'setup external command' '
529 cat >git-external <<-\EOF &&
530 #!/bin/sh
531 git "$@"
532 EOF
533 chmod +x git-external
534'
535
536test_expect_success TTY 'command-specific pager works for external commands' '
537 sane_unset PAGER GIT_PAGER &&
538 echo "foo:initial" >expect &&
539 >actual &&
540 test_config pager.external "sed s/^/foo:/ >actual" &&
90ae5d27 541 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
92058e4d
JK
542 test_cmp expect actual
543'
544
545test_expect_success TTY 'sub-commands of externals use their own pager' '
546 sane_unset PAGER GIT_PAGER &&
547 echo "foo:initial" >expect &&
548 >actual &&
549 test_config pager.log "sed s/^/foo:/ >actual" &&
550 test_terminal git --exec-path=. external log --format=%s -1 &&
551 test_cmp expect actual
552'
553
554test_expect_success TTY 'external command pagers override sub-commands' '
555 sane_unset PAGER GIT_PAGER &&
556 >expect &&
557 >actual &&
558 test_config pager.external false &&
559 test_config pager.log "sed s/^/log:/ >actual" &&
560 test_terminal git --exec-path=. external log --format=%s -1 &&
561 test_cmp expect actual
562'
563
9e9de18f
JK
564test_expect_success 'command with underscores does not complain' '
565 write_script git-under_score <<-\EOF &&
566 echo ok
567 EOF
568 git --exec-path=. under_score >actual 2>&1 &&
569 echo ok >expect &&
570 test_cmp expect actual
571'
572
60b6e220 573test_done