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 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=false 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 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=false tag &&
166 ! test -e paginated.out
169 test_expect_success TTY
'git tag --contains defaults to 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=false 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_success 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 test_expect_success 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
211 test_expect_success TTY
'git tag as alias respects pager.tag with -l' '
212 rm -f paginated.out &&
213 test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
214 ! test -e paginated.out
217 # A colored commit log will begin with an appropriate ANSI escape
218 # for the first color; the text "commit" comes later.
221 ! expr "$firstline" : "[a-zA-Z]" >/dev
/null
224 test_expect_success
'tests can detect color' '
225 rm -f colorful.log colorless.log &&
226 git log --no-color >colorless.log &&
227 git log --color >colorful.log &&
228 ! colorful colorless.log &&
229 colorful colorful.log
232 test_expect_success
'no color when stdout is a regular file' '
233 rm -f colorless.log &&
234 test_config color.ui auto &&
235 git log >colorless.log &&
236 ! colorful colorless.log
239 test_expect_success TTY
'color when writing to a pager' '
240 rm -f paginated.out &&
241 test_config color.ui auto &&
242 test_terminal git log &&
243 colorful paginated.out
246 test_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 &&
250 test_terminal git log &&
251 ! colorful paginated.out
254 test_expect_success
'color when writing to a file intended for a pager' '
255 rm -f colorful.log &&
256 test_config color.ui auto &&
259 GIT_PAGER_IN_USE=true &&
260 export TERM GIT_PAGER_IN_USE &&
261 git log >colorful.log
263 colorful colorful.log
266 test_expect_success TTY
'colors are sent to pager for external commands' '
267 test_config alias.externallog "!git log" &&
268 test_config color.ui auto &&
269 test_terminal git -p externallog &&
270 colorful paginated.out
273 # Use this helper to make it easy for the caller of your
274 # terminal-using function to specify whether it should fail.
280 # $test_expectation "$cmd - behaves well" "
287 # then your test can be used like this:
289 # your_test expect_(success|failure) [test_must_fail] 'git foo'
292 test_expectation
="test_$1"
294 if test "$1" = test_must_fail
296 full_command
="test_must_fail test_terminal "
299 full_command
="test_terminal "
302 full_command
="$full_command $1"
305 test_default_pager
() {
308 $test_expectation SIMPLEPAGER
,TTY
"$cmd - default pager is used by default" "
309 sane_unset PAGER GIT_PAGER &&
310 test_unconfig core.pager &&
311 rm -f default_pager_used &&
312 cat >\$less <<-\EOF &&
314 wc >default_pager_used
322 test -e default_pager_used
326 test_PAGER_overrides
() {
329 $test_expectation TTY
"$cmd - PAGER overrides default pager" "
330 sane_unset GIT_PAGER &&
331 test_unconfig core.pager &&
333 PAGER='wc >PAGER_used' &&
340 test_core_pager_overrides
() {
342 used_if_wanted
='overrides PAGER'
346 test_local_config_ignored
() {
348 used_if_wanted
='is not used'
355 $test_expectation TTY
"$cmd - repository-local core.pager setting $used_if_wanted" "
356 sane_unset GIT_PAGER &&
357 rm -f core.pager_used &&
360 test_config core.pager 'wc >core.pager_used' &&
362 ${if_local_config}test -e core.pager_used
366 test_core_pager_subdir
() {
368 used_if_wanted
='overrides PAGER'
369 test_pager_subdir_helper
"$@"
372 test_no_local_config_subdir
() {
374 used_if_wanted
='is not used'
375 test_pager_subdir_helper
"$@"
378 test_pager_subdir_helper
() {
381 $test_expectation TTY
"$cmd - core.pager $used_if_wanted from subdirectory" "
382 sane_unset GIT_PAGER &&
383 rm -f core.pager_used &&
386 stampname=\$(pwd)/core.pager_used &&
387 export PAGER stampname &&
388 test_config core.pager 'wc >\"\$stampname\"' &&
394 ${if_local_config}test -e core.pager_used
398 test_GIT_PAGER_overrides
() {
401 $test_expectation TTY
"$cmd - GIT_PAGER overrides core.pager" "
402 rm -f GIT_PAGER_used &&
403 test_config core.pager wc &&
404 GIT_PAGER='wc >GIT_PAGER_used' &&
407 test -e GIT_PAGER_used
411 test_doesnt_paginate
() {
414 $test_expectation TTY
"no pager for '$cmd'" "
415 rm -f GIT_PAGER_used &&
416 GIT_PAGER='wc >GIT_PAGER_used' &&
419 ! test -e GIT_PAGER_used
423 test_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
"$@"
431 test_expect_success
'setup: some aliases' '
432 git config alias.aliasedlog log &&
433 git config alias.true "!true"
436 test_pager_choices
'git log'
437 test_pager_choices
'git -p log'
438 test_pager_choices
'git aliasedlog'
440 test_default_pager expect_success
'git -p aliasedlog'
441 test_PAGER_overrides expect_success
'git -p aliasedlog'
442 test_core_pager_overrides expect_success
'git -p aliasedlog'
443 test_core_pager_subdir expect_success
'git -p aliasedlog'
444 test_GIT_PAGER_overrides expect_success
'git -p aliasedlog'
446 test_default_pager expect_success
'git -p true'
447 test_PAGER_overrides expect_success
'git -p true'
448 test_core_pager_overrides expect_success
'git -p true'
449 test_core_pager_subdir expect_success
'git -p true'
450 test_GIT_PAGER_overrides expect_success
'git -p true'
452 test_default_pager expect_success test_must_fail
'git -p request-pull'
453 test_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
454 test_core_pager_overrides expect_success test_must_fail
'git -p request-pull'
455 test_core_pager_subdir expect_success test_must_fail
'git -p request-pull'
456 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
458 test_default_pager expect_success test_must_fail
'git -p'
459 test_PAGER_overrides expect_success test_must_fail
'git -p'
460 test_local_config_ignored expect_failure test_must_fail
'git -p'
461 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p'
463 test_expect_success TTY
'core.pager in repo config works and retains cwd' '
464 sane_unset GIT_PAGER &&
465 test_config core.pager "cat >cwd-retained" &&
468 rm -f cwd-retained &&
469 test_terminal git -p rev-parse HEAD &&
470 test_path_is_file cwd-retained
474 test_expect_success TTY
'core.pager is found via alias in subdirectory' '
475 sane_unset GIT_PAGER &&
476 test_config core.pager "cat >via-alias" &&
480 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
481 test_path_is_file via-alias
485 test_doesnt_paginate expect_failure test_must_fail
'git -p nonsense'
487 test_pager_choices
'git shortlog'
488 test_expect_success
'setup: configure shortlog not to paginate' '
489 git config pager.shortlog false
491 test_doesnt_paginate expect_success
'git shortlog'
492 test_no_local_config_subdir expect_success
'git shortlog'
493 test_default_pager expect_success
'git -p shortlog'
494 test_core_pager_subdir expect_success
'git -p shortlog'
496 test_core_pager_subdir expect_success test_must_fail \
497 'git -p apply </dev/null'
499 test_expect_success TTY
'command-specific pager' '
500 sane_unset PAGER GIT_PAGER &&
501 echo "foo:initial" >expect &&
503 test_unconfig core.pager &&
504 test_config pager.log "sed s/^/foo:/ >actual" &&
505 test_terminal git log --format=%s -1 &&
506 test_cmp expect actual
509 test_expect_success TTY
'command-specific pager overrides core.pager' '
510 sane_unset PAGER GIT_PAGER &&
511 echo "foo:initial" >expect &&
513 test_config core.pager "exit 1" &&
514 test_config pager.log "sed s/^/foo:/ >actual" &&
515 test_terminal git log --format=%s -1 &&
516 test_cmp expect actual
519 test_expect_success TTY
'command-specific pager overridden by environment' '
520 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
522 echo "foo:initial" >expect &&
523 test_config pager.log "exit 1" &&
524 test_terminal git log --format=%s -1 &&
525 test_cmp expect actual
528 test_expect_success
'setup external command' '
529 cat >git-external <<-\EOF &&
533 chmod +x git-external
536 test_expect_success TTY
'command-specific pager works for external commands' '
537 sane_unset PAGER GIT_PAGER &&
538 echo "foo:initial" >expect &&
540 test_config pager.external "sed s/^/foo:/ >actual" &&
541 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
542 test_cmp expect actual
545 test_expect_success TTY
'sub-commands of externals use their own pager' '
546 sane_unset PAGER GIT_PAGER &&
547 echo "foo:initial" >expect &&
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
554 test_expect_success TTY
'external command pagers override sub-commands' '
555 sane_unset PAGER GIT_PAGER &&
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
564 test_expect_success
'command with underscores does not complain' '
565 write_script git-under_score <<-\EOF &&
568 git --exec-path=. under_score >actual 2>&1 &&
570 test_cmp expect actual
573 test_expect_success TTY
'git tag with auto-columns ' '
579 cat >expect <<-\EOF &&
580 initial one two three four five
582 test_terminal env PAGER="cat >actual" COLUMNS=80 \
583 git -c column.ui=auto tag --sort=authordate &&
584 test_cmp expect actual