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 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
11 test_unconfig core.pager &&
13 PAGER="cat >paginated.out" &&
19 test_expect_success TTY 'some commands use a pager' '
20 rm -f paginated.out &&
21 test_terminal git log &&
25 test_expect_failure TTY 'pager runs from subdir' '
26 echo subdir/paginated.out >expected &&
28 rm -f paginated.out subdir/paginated.out &&
34 ls paginated.out subdir/paginated.out ||
37 test_cmp expected actual
40 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
43 PAGER="env >pager-env.out; wc" &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
52 test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
55 PAGER="env >pager-env.out; wc" &&
57 PATH="$(git --exec-path):$PATH" &&
59 test_terminal sh -c ". git-sh-setup && git_pager"
61 grep ^LESS= pager-env.out &&
62 grep ^LV= pager-env.out
65 test_expect_success TTY 'some commands do not use a pager' '
66 rm -f paginated.out &&
67 test_terminal git rev-list HEAD &&
68 ! test -e paginated.out
71 test_expect_success 'no pager when stdout is a pipe' '
72 rm -f paginated.out &&
74 ! test -e paginated.out
77 test_expect_success 'no pager when stdout is a regular file' '
78 rm -f paginated.out &&
80 ! test -e paginated.out
83 test_expect_success TTY 'git --paginate rev-list uses a pager' '
84 rm -f paginated.out &&
85 test_terminal git --paginate rev-list HEAD &&
89 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
90 rm -f file paginated.out &&
91 git --paginate log | cat &&
92 ! test -e paginated.out
95 test_expect_success TTY 'no pager with --no-pager' '
96 rm -f paginated.out &&
97 test_terminal git --no-pager log &&
98 ! test -e paginated.out
101 test_expect_success TTY 'configuration can disable pager' '
102 rm -f paginated.out &&
103 test_unconfig pager.grep &&
104 test_terminal git grep initial &&
105 test -e paginated.out &&
107 rm -f paginated.out &&
108 test_config pager.grep false &&
109 test_terminal git grep initial &&
110 ! test -e paginated.out
113 test_expect_success TTY 'git config uses a pager if configured to' '
114 rm -f paginated.out &&
115 test_config pager.config true &&
116 test_terminal git config --list &&
117 test -e paginated.out
120 test_expect_success TTY 'configuration can enable pager (from subdir)' '
121 rm -f paginated.out &&
123 test_config pager.bundle true &&
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
129 test_terminal git bundle unbundle ../test.bundle
132 test -e paginated.out ||
133 test -e subdir/paginated.out
137 test_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
143 test_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
149 test_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
155 test_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
162 test_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
169 test_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
176 test_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
183 test_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
190 test_expect_failure TTY 'git tag -a ignores pager.tag' '
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
197 test_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
204 # A colored commit log will begin with an appropriate ANSI escape
205 # for the first color; the text "commit" comes later.
208 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
211 test_expect_success 'tests can detect color' '
212 rm -f colorful.log colorless.log &&
213 git log --no-color >colorless.log &&
214 git log --color >colorful.log &&
215 ! colorful colorless.log &&
216 colorful colorful.log
219 test_expect_success 'no color when stdout is a regular file' '
220 rm -f colorless.log &&
221 test_config color.ui auto &&
222 git log >colorless.log &&
223 ! colorful colorless.log
226 test_expect_success TTY 'color when writing to a pager' '
227 rm -f paginated.out &&
228 test_config color.ui auto &&
229 test_terminal env TERM=vt100 git log &&
230 colorful paginated.out
233 test_expect_success TTY 'colors are suppressed by color.pager' '
234 rm -f paginated.out &&
235 test_config color.ui auto &&
236 test_config color.pager false &&
237 test_terminal env TERM=vt100 git log &&
238 ! colorful paginated.out
241 test_expect_success 'color when writing to a file intended for a pager' '
242 rm -f colorful.log &&
243 test_config color.ui auto &&
246 GIT_PAGER_IN_USE=true &&
247 export TERM GIT_PAGER_IN_USE &&
248 git log >colorful.log
250 colorful colorful.log
253 test_expect_success TTY 'colors are sent to pager for external commands' '
254 test_config alias.externallog "!git log" &&
255 test_config color.ui auto &&
256 test_terminal env TERM=vt100 git -p externallog &&
257 colorful paginated.out
260 # Use this helper to make it easy for the caller of your
261 # terminal-using function to specify whether it should fail.
267 # $test_expectation "$cmd - behaves well" "
274 # then your test can be used like this:
276 # your_test expect_(success|failure) [test_must_fail] 'git foo'
279 test_expectation="test_$1"
281 if test "$1" = test_must_fail
283 full_command="test_must_fail test_terminal "
286 full_command="test_terminal "
289 full_command="$full_command $1"
292 test_default_pager() {
295 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
296 sane_unset PAGER GIT_PAGER &&
297 test_unconfig core.pager &&
298 rm -f default_pager_used &&
299 cat >\$less <<-\EOF &&
301 wc >default_pager_used
309 test -e default_pager_used
313 test_PAGER_overrides() {
316 $test_expectation TTY "$cmd - PAGER overrides default pager" "
317 sane_unset GIT_PAGER &&
318 test_unconfig core.pager &&
320 PAGER='wc >PAGER_used' &&
327 test_core_pager_overrides() {
329 used_if_wanted='overrides PAGER'
333 test_local_config_ignored() {
335 used_if_wanted='is not used'
342 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
343 sane_unset GIT_PAGER &&
344 rm -f core.pager_used &&
347 test_config core.pager 'wc >core.pager_used' &&
349 ${if_local_config}test -e core.pager_used
353 test_core_pager_subdir() {
355 used_if_wanted='overrides PAGER'
356 test_pager_subdir_helper "$@"
359 test_no_local_config_subdir() {
361 used_if_wanted='is not used'
362 test_pager_subdir_helper "$@"
365 test_pager_subdir_helper() {
368 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
369 sane_unset GIT_PAGER &&
370 rm -f core.pager_used &&
373 stampname=\$(pwd)/core.pager_used &&
374 export PAGER stampname &&
375 test_config core.pager 'wc >\"\$stampname\"' &&
381 ${if_local_config}test -e core.pager_used
385 test_GIT_PAGER_overrides() {
388 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
389 rm -f GIT_PAGER_used &&
390 test_config core.pager wc &&
391 GIT_PAGER='wc >GIT_PAGER_used' &&
394 test -e GIT_PAGER_used
398 test_doesnt_paginate() {
401 $test_expectation TTY "no pager for '$cmd'" "
402 rm -f GIT_PAGER_used &&
403 GIT_PAGER='wc >GIT_PAGER_used' &&
406 ! test -e GIT_PAGER_used
410 test_pager_choices() {
411 test_default_pager expect_success "$@"
412 test_PAGER_overrides expect_success "$@"
413 test_core_pager_overrides expect_success "$@"
414 test_core_pager_subdir expect_success "$@"
415 test_GIT_PAGER_overrides expect_success "$@"
418 test_expect_success 'setup: some aliases' '
419 git config alias.aliasedlog log &&
420 git config alias.true "!true"
423 test_pager_choices 'git log'
424 test_pager_choices 'git -p log'
425 test_pager_choices 'git aliasedlog'
427 test_default_pager expect_success 'git -p aliasedlog'
428 test_PAGER_overrides expect_success 'git -p aliasedlog'
429 test_core_pager_overrides expect_success 'git -p aliasedlog'
430 test_core_pager_subdir expect_success 'git -p aliasedlog'
431 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
433 test_default_pager expect_success 'git -p true'
434 test_PAGER_overrides expect_success 'git -p true'
435 test_core_pager_overrides expect_success 'git -p true'
436 test_core_pager_subdir expect_success 'git -p true'
437 test_GIT_PAGER_overrides expect_success 'git -p true'
439 test_default_pager expect_success test_must_fail 'git -p request-pull'
440 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
441 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
442 test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
443 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
445 test_default_pager expect_success test_must_fail 'git -p'
446 test_PAGER_overrides expect_success test_must_fail 'git -p'
447 test_local_config_ignored expect_failure test_must_fail 'git -p'
448 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
450 test_expect_success TTY 'core.pager in repo config works and retains cwd' '
451 sane_unset GIT_PAGER &&
452 test_config core.pager "cat >cwd-retained" &&
455 rm -f cwd-retained &&
456 test_terminal git -p rev-parse HEAD &&
457 test_path_is_file cwd-retained
461 test_expect_success TTY 'core.pager is found via alias in subdirectory' '
462 sane_unset GIT_PAGER &&
463 test_config core.pager "cat >via-alias" &&
467 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
468 test_path_is_file via-alias
472 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
474 test_pager_choices 'git shortlog'
475 test_expect_success 'setup: configure shortlog not to paginate' '
476 git config pager.shortlog false
478 test_doesnt_paginate expect_success 'git shortlog'
479 test_no_local_config_subdir expect_success 'git shortlog'
480 test_default_pager expect_success 'git -p shortlog'
481 test_core_pager_subdir expect_success 'git -p shortlog'
483 test_core_pager_subdir expect_success test_must_fail \
484 'git -p apply </dev/null'
486 test_expect_success TTY 'command-specific pager' '
487 sane_unset PAGER GIT_PAGER &&
488 echo "foo:initial" >expect &&
490 test_unconfig core.pager &&
491 test_config pager.log "sed s/^/foo:/ >actual" &&
492 test_terminal git log --format=%s -1 &&
493 test_cmp expect actual
496 test_expect_success TTY 'command-specific pager overrides core.pager' '
497 sane_unset PAGER GIT_PAGER &&
498 echo "foo:initial" >expect &&
500 test_config core.pager "exit 1" &&
501 test_config pager.log "sed s/^/foo:/ >actual" &&
502 test_terminal git log --format=%s -1 &&
503 test_cmp expect actual
506 test_expect_success TTY 'command-specific pager overridden by environment' '
507 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
509 echo "foo:initial" >expect &&
510 test_config pager.log "exit 1" &&
511 test_terminal git log --format=%s -1 &&
512 test_cmp expect actual
515 test_expect_success 'setup external command' '
516 cat >git-external <<-\EOF &&
520 chmod +x git-external
523 test_expect_success TTY 'command-specific pager works for external commands' '
524 sane_unset PAGER GIT_PAGER &&
525 echo "foo:initial" >expect &&
527 test_config pager.external "sed s/^/foo:/ >actual" &&
528 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
529 test_cmp expect actual
532 test_expect_success TTY 'sub-commands of externals use their own pager' '
533 sane_unset PAGER GIT_PAGER &&
534 echo "foo:initial" >expect &&
536 test_config pager.log "sed s/^/foo:/ >actual" &&
537 test_terminal git --exec-path=. external log --format=%s -1 &&
538 test_cmp expect actual
541 test_expect_success TTY 'external command pagers override sub-commands' '
542 sane_unset PAGER GIT_PAGER &&
545 test_config pager.external false &&
546 test_config pager.log "sed s/^/log:/ >actual" &&
547 test_terminal git --exec-path=. external log --format=%s -1 &&
548 test_cmp expect actual
551 test_expect_success 'command with underscores does not complain' '
552 write_script git-under_score <<-\EOF &&
555 git --exec-path=. under_score >actual 2>&1 &&
557 test_cmp expect actual