7976fa7bcce8ed11c403a4923d9ce3e7155765a0
[git/git.git] / t / t7006-pager.sh
1 #!/bin/sh
2
3 test_description='Test automatic use of a pager.'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
8
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 &&
14
15 PAGER="cat >paginated.out" &&
16 export PAGER &&
17
18 test_commit initial
19 '
20
21 test_expect_success TTY 'some commands use a pager' '
22 rm -f paginated.out &&
23 test_terminal git log &&
24 test -e paginated.out
25 '
26
27 test_expect_failure TTY 'pager runs from subdir' '
28 echo subdir/paginated.out >expected &&
29 mkdir -p subdir &&
30 rm -f paginated.out subdir/paginated.out &&
31 (
32 cd subdir &&
33 test_terminal git log
34 ) &&
35 {
36 ls paginated.out subdir/paginated.out ||
37 :
38 } >actual &&
39 test_cmp expected actual
40 '
41
42 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
43 (
44 sane_unset LESS LV &&
45 PAGER="env >pager-env.out; wc" &&
46 export PAGER &&
47
48 test_terminal git log
49 ) &&
50 grep ^LESS= pager-env.out &&
51 grep ^LV= pager-env.out
52 '
53
54 test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
55 (
56 sane_unset LESS LV &&
57 PAGER="env >pager-env.out; wc" &&
58 export PAGER &&
59 PATH="$(git --exec-path):$PATH" &&
60 export PATH &&
61 test_terminal sh -c ". git-sh-setup && git_pager"
62 ) &&
63 grep ^LESS= pager-env.out &&
64 grep ^LV= pager-env.out
65 '
66
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
71 '
72
73 test_expect_success 'no pager when stdout is a pipe' '
74 rm -f paginated.out &&
75 git log | cat &&
76 ! test -e paginated.out
77 '
78
79 test_expect_success 'no pager when stdout is a regular file' '
80 rm -f paginated.out &&
81 git log >file &&
82 ! test -e paginated.out
83 '
84
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 &&
88 test -e paginated.out
89 '
90
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
95 '
96
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
101 '
102
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 &&
108
109 rm -f paginated.out &&
110 test_config pager.grep false &&
111 test_terminal git grep initial &&
112 ! test -e paginated.out
113 '
114
115 test_expect_success TTY 'configuration can enable pager (from subdir)' '
116 rm -f paginated.out &&
117 mkdir -p subdir &&
118 test_config pager.bundle true &&
119
120 git bundle create test.bundle --all &&
121 rm -f paginated.out subdir/paginated.out &&
122 (
123 cd subdir &&
124 test_terminal git bundle unbundle ../test.bundle
125 ) &&
126 {
127 test -e paginated.out ||
128 test -e subdir/paginated.out
129 }
130 '
131
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
136 '
137
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
142 '
143
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
148 '
149
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
155 '
156
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
162 '
163
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
169 '
170
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
176 '
177
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
183 '
184
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
190 '
191
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
197 '
198
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
204 '
205
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
210 '
211
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
216 '
217
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
222 '
223
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
228 '
229
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"
234 touch editor.used
235 EOF
236 EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
237 ! test -e paginated.out &&
238 test -e editor.used
239 '
240
241 test_expect_success TTY 'git branch --set-upstream-to ignores pager.branch' '
242 rm -f paginated.out &&
243 git branch other &&
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
248 '
249
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
254 '
255
256 test_expect_success TTY 'git config --edit ignores pager.config' '
257 rm -f paginated.out editor.used &&
258 write_script editor <<-\EOF &&
259 touch editor.used
260 EOF
261 EDITOR=./editor test_terminal git -c pager.config config --edit &&
262 ! test -e paginated.out &&
263 test -e editor.used
264 '
265
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
270 '
271
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
277 '
278
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
283 '
284
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
289 '
290
291
292 # A colored commit log will begin with an appropriate ANSI escape
293 # for the first color; the text "commit" comes later.
294 colorful() {
295 read firstline <$1
296 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
297 }
298
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
305 '
306
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
312 '
313
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
319 '
320
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
327 '
328
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 &&
332 (
333 TERM=vt100 &&
334 GIT_PAGER_IN_USE=true &&
335 export TERM GIT_PAGER_IN_USE &&
336 git log >colorful.log
337 ) &&
338 colorful colorful.log
339 '
340
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
346 '
347
348 # Use this helper to make it easy for the caller of your
349 # terminal-using function to specify whether it should fail.
350 # If you write
351 #
352 # your_test() {
353 # parse_args "$@"
354 #
355 # $test_expectation "$cmd - behaves well" "
356 # ...
357 # $full_command &&
358 # ...
359 # "
360 # }
361 #
362 # then your test can be used like this:
363 #
364 # your_test expect_(success|failure) [test_must_fail] 'git foo'
365 #
366 parse_args() {
367 test_expectation="test_$1"
368 shift
369 if test "$1" = test_must_fail
370 then
371 full_command="test_must_fail test_terminal "
372 shift
373 else
374 full_command="test_terminal "
375 fi
376 cmd=$1
377 full_command="$full_command $1"
378 }
379
380 test_default_pager() {
381 parse_args "$@"
382
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 &&
388 #!/bin/sh
389 wc >default_pager_used
390 EOF
391 chmod +x \$less &&
392 (
393 PATH=.:\$PATH &&
394 export PATH &&
395 $full_command
396 ) &&
397 test -e default_pager_used
398 "
399 }
400
401 test_PAGER_overrides() {
402 parse_args "$@"
403
404 $test_expectation TTY "$cmd - PAGER overrides default pager" "
405 sane_unset GIT_PAGER &&
406 test_unconfig core.pager &&
407 rm -f PAGER_used &&
408 PAGER='wc >PAGER_used' &&
409 export PAGER &&
410 $full_command &&
411 test -e PAGER_used
412 "
413 }
414
415 test_core_pager_overrides() {
416 if_local_config=
417 used_if_wanted='overrides PAGER'
418 test_core_pager "$@"
419 }
420
421 test_local_config_ignored() {
422 if_local_config='! '
423 used_if_wanted='is not used'
424 test_core_pager "$@"
425 }
426
427 test_core_pager() {
428 parse_args "$@"
429
430 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
431 sane_unset GIT_PAGER &&
432 rm -f core.pager_used &&
433 PAGER=wc &&
434 export PAGER &&
435 test_config core.pager 'wc >core.pager_used' &&
436 $full_command &&
437 ${if_local_config}test -e core.pager_used
438 "
439 }
440
441 test_core_pager_subdir() {
442 if_local_config=
443 used_if_wanted='overrides PAGER'
444 test_pager_subdir_helper "$@"
445 }
446
447 test_no_local_config_subdir() {
448 if_local_config='! '
449 used_if_wanted='is not used'
450 test_pager_subdir_helper "$@"
451 }
452
453 test_pager_subdir_helper() {
454 parse_args "$@"
455
456 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
457 sane_unset GIT_PAGER &&
458 rm -f core.pager_used &&
459 rm -fr sub &&
460 PAGER=wc &&
461 stampname=\$(pwd)/core.pager_used &&
462 export PAGER stampname &&
463 test_config core.pager 'wc >\"\$stampname\"' &&
464 mkdir sub &&
465 (
466 cd sub &&
467 $full_command
468 ) &&
469 ${if_local_config}test -e core.pager_used
470 "
471 }
472
473 test_GIT_PAGER_overrides() {
474 parse_args "$@"
475
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' &&
480 export GIT_PAGER &&
481 $full_command &&
482 test -e GIT_PAGER_used
483 "
484 }
485
486 test_doesnt_paginate() {
487 parse_args "$@"
488
489 $test_expectation TTY "no pager for '$cmd'" "
490 rm -f GIT_PAGER_used &&
491 GIT_PAGER='wc >GIT_PAGER_used' &&
492 export GIT_PAGER &&
493 $full_command &&
494 ! test -e GIT_PAGER_used
495 "
496 }
497
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 "$@"
504 }
505
506 test_expect_success 'setup: some aliases' '
507 git config alias.aliasedlog log &&
508 git config alias.true "!true"
509 '
510
511 test_pager_choices 'git log'
512 test_pager_choices 'git -p log'
513 test_pager_choices 'git aliasedlog'
514
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'
520
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'
526
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'
532
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'
537
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" &&
541 (
542 cd sub &&
543 rm -f cwd-retained &&
544 test_terminal git -p rev-parse HEAD &&
545 test_path_is_file cwd-retained
546 )
547 '
548
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" &&
552 (
553 cd sub &&
554 rm -f via-alias &&
555 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
556 test_path_is_file via-alias
557 )
558 '
559
560 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
561
562 test_pager_choices 'git shortlog'
563 test_expect_success 'setup: configure shortlog not to paginate' '
564 git config pager.shortlog false
565 '
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'
570
571 test_core_pager_subdir expect_success test_must_fail \
572 'git -p apply </dev/null'
573
574 test_expect_success TTY 'command-specific pager' '
575 sane_unset PAGER GIT_PAGER &&
576 echo "foo:initial" >expect &&
577 >actual &&
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
582 '
583
584 test_expect_success TTY 'command-specific pager overrides core.pager' '
585 sane_unset PAGER GIT_PAGER &&
586 echo "foo:initial" >expect &&
587 >actual &&
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
592 '
593
594 test_expect_success TTY 'command-specific pager overridden by environment' '
595 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
596 >actual &&
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
601 '
602
603 test_expect_success 'setup external command' '
604 cat >git-external <<-\EOF &&
605 #!/bin/sh
606 git "$@"
607 EOF
608 chmod +x git-external
609 '
610
611 test_expect_success TTY 'command-specific pager works for external commands' '
612 sane_unset PAGER GIT_PAGER &&
613 echo "foo:initial" >expect &&
614 >actual &&
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
618 '
619
620 test_expect_success TTY 'sub-commands of externals use their own pager' '
621 sane_unset PAGER GIT_PAGER &&
622 echo "foo:initial" >expect &&
623 >actual &&
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
627 '
628
629 test_expect_success TTY 'external command pagers override sub-commands' '
630 sane_unset PAGER GIT_PAGER &&
631 >actual &&
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
636 '
637
638 test_expect_success 'command with underscores does not complain' '
639 write_script git-under_score <<-\EOF &&
640 echo ok
641 EOF
642 git --exec-path=. under_score >actual 2>&1 &&
643 echo ok >expect &&
644 test_cmp expect actual
645 '
646
647 test_expect_success TTY 'git tag with auto-columns ' '
648 test_commit one &&
649 test_commit two &&
650 test_commit three &&
651 test_commit four &&
652 test_commit five &&
653 cat >expect <<-\EOF &&
654 initial one two three four five
655 EOF
656 test_terminal env PAGER="cat >actual" COLUMNS=80 \
657 git -c column.ui=auto tag --sort=authordate &&
658 test_cmp expect actual
659 '
660
661 test_done