3 test_description='Test automatic use of a pager.'
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
9 test_expect_success 'setup' '
10 : squelch advice messages during the transition &&
11 git config --global log.mailmap false &&
12 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
13 test_unconfig core.pager &&
15 PAGER="cat >paginated.out" &&
21 test_expect_success TTY 'some commands use a pager' '
22 rm -f paginated.out &&
23 test_terminal git log &&
27 test_expect_failure TTY 'pager runs from subdir' '
28 echo subdir/paginated.out >expected &&
30 rm -f paginated.out subdir/paginated.out &&
36 ls paginated.out subdir/paginated.out ||
39 test_cmp expected actual
42 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
45 PAGER="env >pager-env.out; wc" &&
50 grep ^LESS= pager-env.out &&
51 grep ^LV= pager-env.out
54 test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
57 PAGER="env >pager-env.out; wc" &&
59 PATH="$(git --exec-path):$PATH" &&
61 test_terminal sh -c ". git-sh-setup && git_pager"
63 grep ^LESS= pager-env.out &&
64 grep ^LV= pager-env.out
67 test_expect_success TTY 'some commands do not use a pager' '
68 rm -f paginated.out &&
69 test_terminal git rev-list HEAD &&
70 ! test -e paginated.out
73 test_expect_success 'no pager when stdout is a pipe' '
74 rm -f paginated.out &&
76 ! test -e paginated.out
79 test_expect_success 'no pager when stdout is a regular file' '
80 rm -f paginated.out &&
82 ! test -e paginated.out
85 test_expect_success TTY 'git --paginate rev-list uses a pager' '
86 rm -f paginated.out &&
87 test_terminal git --paginate rev-list HEAD &&
91 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
92 rm -f file paginated.out &&
93 git --paginate log | cat &&
94 ! test -e paginated.out
97 test_expect_success TTY 'no pager with --no-pager' '
98 rm -f paginated.out &&
99 test_terminal git --no-pager log &&
100 ! test -e paginated.out
103 test_expect_success TTY 'configuration can disable pager' '
104 rm -f paginated.out &&
105 test_unconfig pager.grep &&
106 test_terminal git grep initial &&
107 test -e paginated.out &&
109 rm -f paginated.out &&
110 test_config pager.grep false &&
111 test_terminal git grep initial &&
112 ! test -e paginated.out
115 test_expect_success TTY 'configuration can enable pager (from subdir)' '
116 rm -f paginated.out &&
118 test_config pager.bundle true &&
120 git bundle create test.bundle --all &&
121 rm -f paginated.out subdir/paginated.out &&
124 test_terminal git bundle unbundle ../test.bundle
127 test -e paginated.out ||
128 test -e subdir/paginated.out
132 test_expect_success TTY 'git tag -l defaults to paging' '
133 rm -f paginated.out &&
134 test_terminal git tag -l &&
135 test -e paginated.out
138 test_expect_success TTY 'git tag -l respects pager.tag' '
139 rm -f paginated.out &&
140 test_terminal git -c pager.tag=false tag -l &&
141 ! test -e paginated.out
144 test_expect_success TTY 'git tag -l respects --no-pager' '
145 rm -f paginated.out &&
146 test_terminal git -c pager.tag --no-pager tag -l &&
147 ! test -e paginated.out
150 test_expect_success TTY 'git tag with no args defaults to paging' '
151 # no args implies -l so this should page like -l
152 rm -f paginated.out &&
153 test_terminal git tag &&
154 test -e paginated.out
157 test_expect_success TTY 'git tag with no args respects pager.tag' '
158 # no args implies -l so this should page like -l
159 rm -f paginated.out &&
160 test_terminal git -c pager.tag=false tag &&
161 ! test -e paginated.out
164 test_expect_success TTY 'git tag --contains defaults to paging' '
165 # --contains implies -l so this should page like -l
166 rm -f paginated.out &&
167 test_terminal git tag --contains &&
168 test -e paginated.out
171 test_expect_success TTY 'git tag --contains respects pager.tag' '
172 # --contains implies -l so this should page like -l
173 rm -f paginated.out &&
174 test_terminal git -c pager.tag=false tag --contains &&
175 ! test -e paginated.out
178 test_expect_success TTY 'git tag -a defaults to not paging' '
179 test_when_finished "git tag -d newtag" &&
180 rm -f paginated.out &&
181 test_terminal git tag -am message newtag &&
182 ! test -e paginated.out
185 test_expect_success TTY 'git tag -a ignores pager.tag' '
186 test_when_finished "git tag -d newtag" &&
187 rm -f paginated.out &&
188 test_terminal git -c pager.tag tag -am message newtag &&
189 ! test -e paginated.out
192 test_expect_success TTY 'git tag -a respects --paginate' '
193 test_when_finished "git tag -d newtag" &&
194 rm -f paginated.out &&
195 test_terminal git --paginate tag -am message newtag &&
196 test -e paginated.out
199 test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
200 test_when_finished "git tag -d newtag" &&
201 rm -f paginated.out &&
202 test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
203 ! test -e paginated.out
206 test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
207 rm -f paginated.out &&
208 test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
209 ! test -e paginated.out
212 test_expect_success TTY 'git branch defaults to paging' '
213 rm -f paginated.out &&
214 test_terminal git branch &&
215 test -e paginated.out
218 test_expect_success TTY 'git branch respects pager.branch' '
219 rm -f paginated.out &&
220 test_terminal git -c pager.branch=false branch &&
221 ! test -e paginated.out
224 test_expect_success TTY 'git branch respects --no-pager' '
225 rm -f paginated.out &&
226 test_terminal git --no-pager branch &&
227 ! test -e paginated.out
230 test_expect_success TTY 'git branch --edit-description ignores pager.branch' '
231 rm -f paginated.out editor.used &&
232 write_script editor <<-\EOF &&
233 echo "New description" >"$1"
236 EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
237 ! test -e paginated.out &&
241 test_expect_success TTY 'git branch --set-upstream-to ignores pager.branch' '
242 rm -f paginated.out &&
244 test_when_finished "git branch -D other" &&
245 test_terminal git -c pager.branch branch --set-upstream-to=other &&
246 test_when_finished "git branch --unset-upstream" &&
247 ! test -e paginated.out
250 test_expect_success TTY 'git config ignores pager.config when setting' '
251 rm -f paginated.out &&
252 test_terminal git -c pager.config config foo.bar bar &&
253 ! test -e paginated.out
256 test_expect_success TTY 'git config --edit ignores pager.config' '
257 rm -f paginated.out editor.used &&
258 write_script editor <<-\EOF &&
261 EDITOR=./editor test_terminal git -c pager.config config --edit &&
262 ! test -e paginated.out &&
266 test_expect_success TTY 'git config --get ignores pager.config' '
267 rm -f paginated.out &&
268 test_terminal git -c pager.config config --get foo.bar &&
269 ! test -e paginated.out
272 test_expect_success TTY 'git config --get-urlmatch defaults to paging' '
273 rm -f paginated.out &&
274 test_terminal git -c http."https://foo.com/".bar=foo \
275 config --get-urlmatch http https://foo.com &&
276 test -e paginated.out
279 test_expect_success TTY 'git config --get-all respects pager.config' '
280 rm -f paginated.out &&
281 test_terminal git -c pager.config=false config --get-all foo.bar &&
282 ! test -e paginated.out
285 test_expect_success TTY 'git config --list defaults to paging' '
286 rm -f paginated.out &&
287 test_terminal git config --list &&
288 test -e paginated.out
292 # A colored commit log will begin with an appropriate ANSI escape
293 # for the first color; the text "commit" comes later.
296 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
299 test_expect_success 'tests can detect color' '
300 rm -f colorful.log colorless.log &&
301 git log --no-color >colorless.log &&
302 git log --color >colorful.log &&
303 ! colorful colorless.log &&
304 colorful colorful.log
307 test_expect_success 'no color when stdout is a regular file' '
308 rm -f colorless.log &&
309 test_config color.ui auto &&
310 git log >colorless.log &&
311 ! colorful colorless.log
314 test_expect_success TTY 'color when writing to a pager' '
315 rm -f paginated.out &&
316 test_config color.ui auto &&
317 test_terminal git log &&
318 colorful paginated.out
321 test_expect_success TTY 'colors are suppressed by color.pager' '
322 rm -f paginated.out &&
323 test_config color.ui auto &&
324 test_config color.pager false &&
325 test_terminal git log &&
326 ! colorful paginated.out
329 test_expect_success 'color when writing to a file intended for a pager' '
330 rm -f colorful.log &&
331 test_config color.ui auto &&
334 GIT_PAGER_IN_USE=true &&
335 export TERM GIT_PAGER_IN_USE &&
336 git log >colorful.log
338 colorful colorful.log
341 test_expect_success TTY 'colors are sent to pager for external commands' '
342 test_config alias.externallog "!git log" &&
343 test_config color.ui auto &&
344 test_terminal git -p externallog &&
345 colorful paginated.out
348 # Use this helper to make it easy for the caller of your
349 # terminal-using function to specify whether it should fail.
355 # $test_expectation "$cmd - behaves well" "
362 # then your test can be used like this:
364 # your_test expect_(success|failure) [test_must_fail] 'git foo'
367 test_expectation="test_$1"
369 if test "$1" = test_must_fail
371 full_command="test_must_fail test_terminal "
374 full_command="test_terminal "
377 full_command="$full_command $1"
380 test_default_pager() {
383 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
384 sane_unset PAGER GIT_PAGER &&
385 test_unconfig core.pager &&
386 rm -f default_pager_used &&
387 cat >\$less <<-\EOF &&
389 wc >default_pager_used
397 test -e default_pager_used
401 test_PAGER_overrides() {
404 $test_expectation TTY "$cmd - PAGER overrides default pager" "
405 sane_unset GIT_PAGER &&
406 test_unconfig core.pager &&
408 PAGER='wc >PAGER_used' &&
415 test_core_pager_overrides() {
417 used_if_wanted='overrides PAGER'
421 test_local_config_ignored() {
423 used_if_wanted='is not used'
430 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
431 sane_unset GIT_PAGER &&
432 rm -f core.pager_used &&
435 test_config core.pager 'wc >core.pager_used' &&
437 ${if_local_config}test -e core.pager_used
441 test_core_pager_subdir() {
443 used_if_wanted='overrides PAGER'
444 test_pager_subdir_helper "$@"
447 test_no_local_config_subdir() {
449 used_if_wanted='is not used'
450 test_pager_subdir_helper "$@"
453 test_pager_subdir_helper() {
456 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
457 sane_unset GIT_PAGER &&
458 rm -f core.pager_used &&
461 stampname=\$(pwd)/core.pager_used &&
462 export PAGER stampname &&
463 test_config core.pager 'wc >\"\$stampname\"' &&
469 ${if_local_config}test -e core.pager_used
473 test_GIT_PAGER_overrides() {
476 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
477 rm -f GIT_PAGER_used &&
478 test_config core.pager wc &&
479 GIT_PAGER='wc >GIT_PAGER_used' &&
482 test -e GIT_PAGER_used
486 test_doesnt_paginate() {
489 $test_expectation TTY "no pager for '$cmd'" "
490 rm -f GIT_PAGER_used &&
491 GIT_PAGER='wc >GIT_PAGER_used' &&
494 ! test -e GIT_PAGER_used
498 test_pager_choices() {
499 test_default_pager expect_success "$@"
500 test_PAGER_overrides expect_success "$@"
501 test_core_pager_overrides expect_success "$@"
502 test_core_pager_subdir expect_success "$@"
503 test_GIT_PAGER_overrides expect_success "$@"
506 test_expect_success 'setup: some aliases' '
507 git config alias.aliasedlog log &&
508 git config alias.true "!true"
511 test_pager_choices 'git log'
512 test_pager_choices 'git -p log'
513 test_pager_choices 'git aliasedlog'
515 test_default_pager expect_success 'git -p aliasedlog'
516 test_PAGER_overrides expect_success 'git -p aliasedlog'
517 test_core_pager_overrides expect_success 'git -p aliasedlog'
518 test_core_pager_subdir expect_success 'git -p aliasedlog'
519 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
521 test_default_pager expect_success 'git -p true'
522 test_PAGER_overrides expect_success 'git -p true'
523 test_core_pager_overrides expect_success 'git -p true'
524 test_core_pager_subdir expect_success 'git -p true'
525 test_GIT_PAGER_overrides expect_success 'git -p true'
527 test_default_pager expect_success test_must_fail 'git -p request-pull'
528 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
529 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
530 test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
531 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
533 test_default_pager expect_success test_must_fail 'git -p'
534 test_PAGER_overrides expect_success test_must_fail 'git -p'
535 test_local_config_ignored expect_failure test_must_fail 'git -p'
536 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
538 test_expect_success TTY 'core.pager in repo config works and retains cwd' '
539 sane_unset GIT_PAGER &&
540 test_config core.pager "cat >cwd-retained" &&
543 rm -f cwd-retained &&
544 test_terminal git -p rev-parse HEAD &&
545 test_path_is_file cwd-retained
549 test_expect_success TTY 'core.pager is found via alias in subdirectory' '
550 sane_unset GIT_PAGER &&
551 test_config core.pager "cat >via-alias" &&
555 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
556 test_path_is_file via-alias
560 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
562 test_pager_choices 'git shortlog'
563 test_expect_success 'setup: configure shortlog not to paginate' '
564 git config pager.shortlog false
566 test_doesnt_paginate expect_success 'git shortlog'
567 test_no_local_config_subdir expect_success 'git shortlog'
568 test_default_pager expect_success 'git -p shortlog'
569 test_core_pager_subdir expect_success 'git -p shortlog'
571 test_core_pager_subdir expect_success test_must_fail \
572 'git -p apply </dev/null'
574 test_expect_success TTY 'command-specific pager' '
575 sane_unset PAGER GIT_PAGER &&
576 echo "foo:initial" >expect &&
578 test_unconfig core.pager &&
579 test_config pager.log "sed s/^/foo:/ >actual" &&
580 test_terminal git log --format=%s -1 &&
581 test_cmp expect actual
584 test_expect_success TTY 'command-specific pager overrides core.pager' '
585 sane_unset PAGER GIT_PAGER &&
586 echo "foo:initial" >expect &&
588 test_config core.pager "exit 1" &&
589 test_config pager.log "sed s/^/foo:/ >actual" &&
590 test_terminal git log --format=%s -1 &&
591 test_cmp expect actual
594 test_expect_success TTY 'command-specific pager overridden by environment' '
595 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
597 echo "foo:initial" >expect &&
598 test_config pager.log "exit 1" &&
599 test_terminal git log --format=%s -1 &&
600 test_cmp expect actual
603 test_expect_success 'setup external command' '
604 cat >git-external <<-\EOF &&
608 chmod +x git-external
611 test_expect_success TTY 'command-specific pager works for external commands' '
612 sane_unset PAGER GIT_PAGER &&
613 echo "foo:initial" >expect &&
615 test_config pager.external "sed s/^/foo:/ >actual" &&
616 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
617 test_cmp expect actual
620 test_expect_success TTY 'sub-commands of externals use their own pager' '
621 sane_unset PAGER GIT_PAGER &&
622 echo "foo:initial" >expect &&
624 test_config pager.log "sed s/^/foo:/ >actual" &&
625 test_terminal git --exec-path=. external log --format=%s -1 &&
626 test_cmp expect actual
629 test_expect_success TTY 'external command pagers override sub-commands' '
630 sane_unset PAGER GIT_PAGER &&
632 test_config pager.external false &&
633 test_config pager.log "sed s/^/log:/ >actual" &&
634 test_terminal git --exec-path=. external log --format=%s -1 &&
635 test_must_be_empty actual
638 test_expect_success 'command with underscores does not complain' '
639 write_script git-under_score <<-\EOF &&
642 git --exec-path=. under_score >actual 2>&1 &&
644 test_cmp expect actual
647 test_expect_success TTY 'git tag with auto-columns ' '
653 cat >expect <<-\EOF &&
654 initial one two three four five
656 test_terminal env PAGER="cat >actual" COLUMNS=80 \
657 git -c column.ui=auto tag --sort=authordate &&
658 test_cmp expect actual