Merge branch 'tz/doc-show-defaults-to-head'
[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         sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
11         test_unconfig core.pager &&
12
13         PAGER="cat >paginated.out" &&
14         export PAGER &&
15
16         test_commit initial
17 '
18
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
23 '
24
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 &&
29         (
30                 cd subdir &&
31                 test_terminal git log
32         ) &&
33         {
34                 ls paginated.out subdir/paginated.out ||
35                 :
36         } >actual &&
37         test_cmp expected actual
38 '
39
40 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
41         (
42                 sane_unset LESS LV &&
43                 PAGER="env >pager-env.out; wc" &&
44                 export PAGER &&
45
46                 test_terminal git log
47         ) &&
48         grep ^LESS= pager-env.out &&
49         grep ^LV= pager-env.out
50 '
51
52 test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
53         (
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
63 '
64
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
69 '
70
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
75 '
76
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
81 '
82
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
87 '
88
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
93 '
94
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
99 '
100
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 &&
106
107         rm -f paginated.out &&
108         test_config pager.grep false &&
109         test_terminal git grep initial &&
110         ! test -e paginated.out
111 '
112
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
118 '
119
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 &&
124
125         git bundle create test.bundle --all &&
126         rm -f paginated.out subdir/paginated.out &&
127         (
128                 cd subdir &&
129                 test_terminal git bundle unbundle ../test.bundle
130         ) &&
131         {
132                 test -e paginated.out ||
133                 test -e subdir/paginated.out
134         }
135 '
136
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
141 '
142
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
147 '
148
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
153 '
154
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
160 '
161
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
167 '
168
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
174 '
175
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
181 '
182
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
188 '
189
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
195 '
196
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
202 '
203
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
209 '
210
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
215 '
216
217 test_expect_success TTY 'git branch defaults to paging' '
218         rm -f paginated.out &&
219         test_terminal git branch &&
220         test -e paginated.out
221 '
222
223 test_expect_success TTY 'git branch respects pager.branch' '
224         rm -f paginated.out &&
225         test_terminal git -c pager.branch=false branch &&
226         ! test -e paginated.out
227 '
228
229 test_expect_success TTY 'git branch respects --no-pager' '
230         rm -f paginated.out &&
231         test_terminal git --no-pager branch &&
232         ! test -e paginated.out
233 '
234
235 test_expect_success 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
240         EOF
241         EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
242         ! test -e paginated.out &&
243         test -e editor.used
244 '
245
246 test_expect_success TTY 'git branch --set-upstream-to ignores 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
253 '
254
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
260 }
261
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
268 '
269
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
275 '
276
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 git log &&
281         colorful paginated.out
282 '
283
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 git log &&
289         ! colorful paginated.out
290 '
291
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 &&
295         (
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
302 '
303
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 git -p externallog &&
308         colorful paginated.out
309 '
310
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
314 #
315 #       your_test() {
316 #               parse_args "$@"
317 #
318 #               $test_expectation "$cmd - behaves well" "
319 #                       ...
320 #                       $full_command &&
321 #                       ...
322 #               "
323 #       }
324 #
325 # then your test can be used like this:
326 #
327 #       your_test expect_(success|failure) [test_must_fail] 'git foo'
328 #
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 "
338         fi
339         cmd=$1
340         full_command="$full_command $1"
341 }
342
343 test_default_pager() {
344         parse_args "$@"
345
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
353                 EOF
354                 chmod +x \$less &&
355                 (
356                         PATH=.:\$PATH &&
357                         export PATH &&
358                         $full_command
359                 ) &&
360                 test -e default_pager_used
361         "
362 }
363
364 test_PAGER_overrides() {
365         parse_args "$@"
366
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
375         "
376 }
377
378 test_core_pager_overrides() {
379         if_local_config=
380         used_if_wanted='overrides PAGER'
381         test_core_pager "$@"
382 }
383
384 test_local_config_ignored() {
385         if_local_config='! '
386         used_if_wanted='is not used'
387         test_core_pager "$@"
388 }
389
390 test_core_pager() {
391         parse_args "$@"
392
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
401         "
402 }
403
404 test_core_pager_subdir() {
405         if_local_config=
406         used_if_wanted='overrides PAGER'
407         test_pager_subdir_helper "$@"
408 }
409
410 test_no_local_config_subdir() {
411         if_local_config='! '
412         used_if_wanted='is not used'
413         test_pager_subdir_helper "$@"
414 }
415
416 test_pager_subdir_helper() {
417         parse_args "$@"
418
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 &&
428                 (
429                         cd sub &&
430                         $full_command
431                 ) &&
432                 ${if_local_config}test -e core.pager_used
433         "
434 }
435
436 test_GIT_PAGER_overrides() {
437         parse_args "$@"
438
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
446         "
447 }
448
449 test_doesnt_paginate() {
450         parse_args "$@"
451
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
458         "
459 }
460
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 "$@"
467 }
468
469 test_expect_success 'setup: some aliases' '
470         git config alias.aliasedlog log &&
471         git config alias.true "!true"
472 '
473
474 test_pager_choices                       'git log'
475 test_pager_choices                       'git -p log'
476 test_pager_choices                       'git aliasedlog'
477
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'
483
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'
489
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'
495
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'
500
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" &&
504         (
505                 cd sub &&
506                 rm -f cwd-retained &&
507                 test_terminal git -p rev-parse HEAD &&
508                 test_path_is_file cwd-retained
509         )
510 '
511
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" &&
515         (
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
520         )
521 '
522
523 test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
524
525 test_pager_choices                       'git shortlog'
526 test_expect_success 'setup: configure shortlog not to paginate' '
527         git config pager.shortlog false
528 '
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'
533
534 test_core_pager_subdir    expect_success test_must_fail \
535                                          'git -p apply </dev/null'
536
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
545 '
546
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
555 '
556
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
564 '
565
566 test_expect_success 'setup external command' '
567         cat >git-external <<-\EOF &&
568         #!/bin/sh
569         git "$@"
570         EOF
571         chmod +x git-external
572 '
573
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
581 '
582
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
590 '
591
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
600 '
601
602 test_expect_success 'command with underscores does not complain' '
603         write_script git-under_score <<-\EOF &&
604         echo ok
605         EOF
606         git --exec-path=. under_score >actual 2>&1 &&
607         echo ok >expect &&
608         test_cmp expect actual
609 '
610
611 test_expect_success TTY 'git tag with auto-columns ' '
612         test_commit one &&
613         test_commit two &&
614         test_commit three &&
615         test_commit four &&
616         test_commit five &&
617         cat >expect <<-\EOF &&
618         initial  one      two      three    four     five
619         EOF
620         test_terminal env PAGER="cat >actual" COLUMNS=80 \
621                 git -c column.ui=auto tag --sort=authordate &&
622         test_cmp expect actual
623 '
624
625 test_done