t7006: add tests for how git branch paginates
[git.git] / t / t7006-pager.sh
blob12c72ef9672d1d14bbd4bce92175e32ced82781f
1 #!/bin/sh
3 test_description='Test automatic use of a pager.'
5 . ./test-lib.sh
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" &&
14 export PAGER &&
16 test_commit initial
19 test_expect_success TTY 'some commands use a pager' '
20 rm -f paginated.out &&
21 test_terminal git log &&
22 test -e paginated.out
25 test_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 &&
30 cd subdir &&
31 test_terminal git log
32 ) &&
34 ls paginated.out subdir/paginated.out ||
36 } >actual &&
37 test_cmp expected actual
40 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
42 sane_unset LESS LV &&
43 PAGER="env >pager-env.out; wc" &&
44 export PAGER &&
46 test_terminal git log
47 ) &&
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' '
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
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 &&
73 git log | cat &&
74 ! test -e paginated.out
77 test_expect_success 'no pager when stdout is a regular file' '
78 rm -f paginated.out &&
79 git log >file &&
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 &&
86 test -e paginated.out
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 &&
122 mkdir -p subdir &&
123 test_config pager.bundle true &&
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
128 cd subdir &&
129 test_terminal git bundle unbundle ../test.bundle
130 ) &&
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 test_expect_success TTY 'git branch defaults to not paging' '
218 rm -f paginated.out &&
219 test_terminal git branch &&
220 ! test -e paginated.out
223 test_expect_success TTY 'git branch respects pager.branch' '
224 rm -f paginated.out &&
225 test_terminal git -c pager.branch branch &&
226 test -e paginated.out
229 test_expect_success TTY 'git branch respects --no-pager' '
230 rm -f paginated.out &&
231 test_terminal git -c pager.branch --no-pager branch &&
232 ! test -e paginated.out
235 test_expect_failure TTY 'git branch --edit-description ignores pager.branch' '
236 rm -f paginated.out editor.used &&
237 write_script editor <<-\EOF &&
238 echo "New description" >"$1"
239 touch editor.used
241 EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
242 ! test -e paginated.out &&
243 test -e editor.used
246 test_expect_success TTY 'git branch --set-upstream-to respects pager.branch' '
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" &&
252 test -e paginated.out
255 # A colored commit log will begin with an appropriate ANSI escape
256 # for the first color; the text "commit" comes later.
257 colorful() {
258 read firstline <$1
259 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
262 test_expect_success 'tests can detect color' '
263 rm -f colorful.log colorless.log &&
264 git log --no-color >colorless.log &&
265 git log --color >colorful.log &&
266 ! colorful colorless.log &&
267 colorful colorful.log
270 test_expect_success 'no color when stdout is a regular file' '
271 rm -f colorless.log &&
272 test_config color.ui auto &&
273 git log >colorless.log &&
274 ! colorful colorless.log
277 test_expect_success TTY 'color when writing to a pager' '
278 rm -f paginated.out &&
279 test_config color.ui auto &&
280 test_terminal env TERM=vt100 git log &&
281 colorful paginated.out
284 test_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 &&
288 test_terminal env TERM=vt100 git log &&
289 ! colorful paginated.out
292 test_expect_success 'color when writing to a file intended for a pager' '
293 rm -f colorful.log &&
294 test_config color.ui auto &&
296 TERM=vt100 &&
297 GIT_PAGER_IN_USE=true &&
298 export TERM GIT_PAGER_IN_USE &&
299 git log >colorful.log
300 ) &&
301 colorful colorful.log
304 test_expect_success TTY 'colors are sent to pager for external commands' '
305 test_config alias.externallog "!git log" &&
306 test_config color.ui auto &&
307 test_terminal env TERM=vt100 git -p externallog &&
308 colorful paginated.out
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
315 # your_test() {
316 # parse_args "$@"
318 # $test_expectation "$cmd - behaves well" "
319 # ...
320 # $full_command &&
321 # ...
325 # then your test can be used like this:
327 # your_test expect_(success|failure) [test_must_fail] 'git foo'
329 parse_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 "
339 cmd=$1
340 full_command="$full_command $1"
343 test_default_pager() {
344 parse_args "$@"
346 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
347 sane_unset PAGER GIT_PAGER &&
348 test_unconfig core.pager &&
349 rm -f default_pager_used &&
350 cat >\$less <<-\EOF &&
351 #!/bin/sh
352 wc >default_pager_used
354 chmod +x \$less &&
356 PATH=.:\$PATH &&
357 export PATH &&
358 $full_command
359 ) &&
360 test -e default_pager_used
364 test_PAGER_overrides() {
365 parse_args "$@"
367 $test_expectation TTY "$cmd - PAGER overrides default pager" "
368 sane_unset GIT_PAGER &&
369 test_unconfig core.pager &&
370 rm -f PAGER_used &&
371 PAGER='wc >PAGER_used' &&
372 export PAGER &&
373 $full_command &&
374 test -e PAGER_used
378 test_core_pager_overrides() {
379 if_local_config=
380 used_if_wanted='overrides PAGER'
381 test_core_pager "$@"
384 test_local_config_ignored() {
385 if_local_config='! '
386 used_if_wanted='is not used'
387 test_core_pager "$@"
390 test_core_pager() {
391 parse_args "$@"
393 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
394 sane_unset GIT_PAGER &&
395 rm -f core.pager_used &&
396 PAGER=wc &&
397 export PAGER &&
398 test_config core.pager 'wc >core.pager_used' &&
399 $full_command &&
400 ${if_local_config}test -e core.pager_used
404 test_core_pager_subdir() {
405 if_local_config=
406 used_if_wanted='overrides PAGER'
407 test_pager_subdir_helper "$@"
410 test_no_local_config_subdir() {
411 if_local_config='! '
412 used_if_wanted='is not used'
413 test_pager_subdir_helper "$@"
416 test_pager_subdir_helper() {
417 parse_args "$@"
419 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
420 sane_unset GIT_PAGER &&
421 rm -f core.pager_used &&
422 rm -fr sub &&
423 PAGER=wc &&
424 stampname=\$(pwd)/core.pager_used &&
425 export PAGER stampname &&
426 test_config core.pager 'wc >\"\$stampname\"' &&
427 mkdir sub &&
429 cd sub &&
430 $full_command
431 ) &&
432 ${if_local_config}test -e core.pager_used
436 test_GIT_PAGER_overrides() {
437 parse_args "$@"
439 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
440 rm -f GIT_PAGER_used &&
441 test_config core.pager wc &&
442 GIT_PAGER='wc >GIT_PAGER_used' &&
443 export GIT_PAGER &&
444 $full_command &&
445 test -e GIT_PAGER_used
449 test_doesnt_paginate() {
450 parse_args "$@"
452 $test_expectation TTY "no pager for '$cmd'" "
453 rm -f GIT_PAGER_used &&
454 GIT_PAGER='wc >GIT_PAGER_used' &&
455 export GIT_PAGER &&
456 $full_command &&
457 ! test -e GIT_PAGER_used
461 test_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 "$@"
469 test_expect_success 'setup: some aliases' '
470 git config alias.aliasedlog log &&
471 git config alias.true "!true"
474 test_pager_choices 'git log'
475 test_pager_choices 'git -p log'
476 test_pager_choices 'git aliasedlog'
478 test_default_pager expect_success 'git -p aliasedlog'
479 test_PAGER_overrides expect_success 'git -p aliasedlog'
480 test_core_pager_overrides expect_success 'git -p aliasedlog'
481 test_core_pager_subdir expect_success 'git -p aliasedlog'
482 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
484 test_default_pager expect_success 'git -p true'
485 test_PAGER_overrides expect_success 'git -p true'
486 test_core_pager_overrides expect_success 'git -p true'
487 test_core_pager_subdir expect_success 'git -p true'
488 test_GIT_PAGER_overrides expect_success 'git -p true'
490 test_default_pager expect_success test_must_fail 'git -p request-pull'
491 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
492 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
493 test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
494 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
496 test_default_pager expect_success test_must_fail 'git -p'
497 test_PAGER_overrides expect_success test_must_fail 'git -p'
498 test_local_config_ignored expect_failure test_must_fail 'git -p'
499 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
501 test_expect_success TTY 'core.pager in repo config works and retains cwd' '
502 sane_unset GIT_PAGER &&
503 test_config core.pager "cat >cwd-retained" &&
505 cd sub &&
506 rm -f cwd-retained &&
507 test_terminal git -p rev-parse HEAD &&
508 test_path_is_file cwd-retained
512 test_expect_success TTY 'core.pager is found via alias in subdirectory' '
513 sane_unset GIT_PAGER &&
514 test_config core.pager "cat >via-alias" &&
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
523 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
525 test_pager_choices 'git shortlog'
526 test_expect_success 'setup: configure shortlog not to paginate' '
527 git config pager.shortlog false
529 test_doesnt_paginate expect_success 'git shortlog'
530 test_no_local_config_subdir expect_success 'git shortlog'
531 test_default_pager expect_success 'git -p shortlog'
532 test_core_pager_subdir expect_success 'git -p shortlog'
534 test_core_pager_subdir expect_success test_must_fail \
535 'git -p apply </dev/null'
537 test_expect_success TTY 'command-specific pager' '
538 sane_unset PAGER GIT_PAGER &&
539 echo "foo:initial" >expect &&
540 >actual &&
541 test_unconfig core.pager &&
542 test_config pager.log "sed s/^/foo:/ >actual" &&
543 test_terminal git log --format=%s -1 &&
544 test_cmp expect actual
547 test_expect_success TTY 'command-specific pager overrides core.pager' '
548 sane_unset PAGER GIT_PAGER &&
549 echo "foo:initial" >expect &&
550 >actual &&
551 test_config core.pager "exit 1" &&
552 test_config pager.log "sed s/^/foo:/ >actual" &&
553 test_terminal git log --format=%s -1 &&
554 test_cmp expect actual
557 test_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 &&
561 test_config pager.log "exit 1" &&
562 test_terminal git log --format=%s -1 &&
563 test_cmp expect actual
566 test_expect_success 'setup external command' '
567 cat >git-external <<-\EOF &&
568 #!/bin/sh
569 git "$@"
571 chmod +x git-external
574 test_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" &&
579 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
580 test_cmp expect actual
583 test_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
592 test_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
602 test_expect_success 'command with underscores does not complain' '
603 write_script git-under_score <<-\EOF &&
604 echo ok
606 git --exec-path=. under_score >actual 2>&1 &&
607 echo ok >expect &&
608 test_cmp expect actual
611 test_done