Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git grep --open-files-in-pager |
| 4 | ' |
| 5 | |
Ævar Arnfjörð Bjarmason | b202e51 | 2021-10-22 10:55:41 +0200 | [diff] [blame] | 6 | TEST_PASSES_SANITIZE_LEAK=true |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 7 | . ./test-lib.sh |
| 8 | . "$TEST_DIRECTORY"/lib-pager.sh |
| 9 | unset PAGER GIT_PAGER |
| 10 | |
| 11 | test_expect_success 'setup' ' |
| 12 | test_commit initial grep.h " |
| 13 | enum grep_pat_token { |
| 14 | GREP_PATTERN, |
| 15 | GREP_PATTERN_HEAD, |
| 16 | GREP_PATTERN_BODY, |
| 17 | GREP_AND, |
| 18 | GREP_OPEN_PAREN, |
| 19 | GREP_CLOSE_PAREN, |
| 20 | GREP_NOT, |
| 21 | GREP_OR, |
| 22 | };" && |
| 23 | |
| 24 | test_commit add-user revision.c " |
| 25 | } |
| 26 | if (seen_dashdash) |
| 27 | read_pathspec_from_stdin(revs, &sb, prune); |
| 28 | strbuf_release(&sb); |
| 29 | } |
| 30 | |
| 31 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
| 32 | { |
| 33 | append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what); |
| 34 | " && |
| 35 | |
| 36 | mkdir subdir && |
| 37 | test_commit subdir subdir/grep.c "enum grep_pat_token" && |
| 38 | |
| 39 | test_commit uninteresting unrelated "hello, world" && |
| 40 | |
| 41 | echo GREP_PATTERN >untracked |
| 42 | ' |
| 43 | |
| 44 | test_expect_success SIMPLEPAGER 'git grep -O' ' |
| 45 | cat >$less <<-\EOF && |
| 46 | #!/bin/sh |
| 47 | printf "%s\n" "$@" >pager-args |
| 48 | EOF |
| 49 | chmod +x $less && |
| 50 | cat >expect.less <<-\EOF && |
| 51 | +/*GREP_PATTERN |
| 52 | grep.h |
| 53 | EOF |
| 54 | echo grep.h >expect.notless && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 55 | |
| 56 | PATH=.:$PATH git grep -O GREP_PATTERN >out && |
| 57 | { |
| 58 | test_cmp expect.less pager-args || |
| 59 | test_cmp expect.notless pager-args |
| 60 | } && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 61 | test_must_be_empty out |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 62 | ' |
| 63 | |
Junio C Hamano | c9ea118 | 2011-04-14 14:36:14 -0700 | [diff] [blame] | 64 | test_expect_success 'git grep -O --cached' ' |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 65 | test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg && |
Junio C Hamano | c9ea118 | 2011-04-14 14:36:14 -0700 | [diff] [blame] | 66 | test_i18ngrep open-files-in-pager msg |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 67 | ' |
| 68 | |
| 69 | test_expect_success 'git grep -O --no-index' ' |
| 70 | rm -f expect.less pager-args out && |
| 71 | cat >expect <<-\EOF && |
| 72 | grep.h |
| 73 | untracked |
| 74 | EOF |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 75 | |
| 76 | ( |
| 77 | GIT_PAGER='\''printf "%s\n" >pager-args'\'' && |
| 78 | export GIT_PAGER && |
| 79 | git grep --no-index -O GREP_PATTERN >out |
| 80 | ) && |
| 81 | test_cmp expect pager-args && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 82 | test_must_be_empty out |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 83 | ' |
| 84 | |
| 85 | test_expect_success 'setup: fake "less"' ' |
Brandon Casey | 6a1b3fc | 2010-06-21 12:37:14 -0500 | [diff] [blame] | 86 | cat >less <<-\EOF && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 87 | #!/bin/sh |
| 88 | printf "%s\n" "$@" >actual |
| 89 | EOF |
Brandon Casey | 6a1b3fc | 2010-06-21 12:37:14 -0500 | [diff] [blame] | 90 | chmod +x less |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 91 | ' |
| 92 | |
| 93 | test_expect_success 'git grep -O jumps to line in less' ' |
| 94 | cat >expect <<-\EOF && |
| 95 | +/*GREP_PATTERN |
| 96 | grep.h |
| 97 | EOF |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 98 | |
| 99 | GIT_PAGER=./less git grep -O GREP_PATTERN >out && |
| 100 | test_cmp expect actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 101 | test_must_be_empty out && |
Johannes Schindelin | 0af88c1 | 2010-06-12 11:39:46 -0500 | [diff] [blame] | 102 | |
| 103 | git grep -O./less GREP_PATTERN >out2 && |
| 104 | test_cmp expect actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 105 | test_must_be_empty out2 |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 106 | ' |
| 107 | |
| 108 | test_expect_success 'modified file' ' |
| 109 | rm -f actual && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 110 | cat >expect <<-\EOF && |
| 111 | +/*enum grep_pat_token |
| 112 | grep.h |
| 113 | revision.c |
| 114 | subdir/grep.c |
| 115 | unrelated |
| 116 | EOF |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 117 | |
Ævar Arnfjörð Bjarmason | b202e51 | 2021-10-22 10:55:41 +0200 | [diff] [blame] | 118 | test_when_finished "git reset --hard" && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 119 | echo "enum grep_pat_token" >unrelated && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 120 | GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out && |
| 121 | test_cmp expect actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 122 | test_must_be_empty out |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 123 | ' |
| 124 | |
Nazri Ramliy | e7b082a | 2010-07-02 21:55:06 -0500 | [diff] [blame] | 125 | test_expect_success 'copes with color settings' ' |
| 126 | rm -f actual && |
| 127 | echo grep.h >expect && |
| 128 | test_config color.grep always && |
| 129 | test_config color.grep.filename yellow && |
| 130 | test_config color.grep.separator green && |
| 131 | git grep -O'\''printf "%s\n" >actual'\'' GREP_AND && |
| 132 | test_cmp expect actual |
| 133 | ' |
| 134 | |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 135 | test_expect_success 'run from subdir' ' |
| 136 | rm -f actual && |
| 137 | echo grep.c >expect && |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 138 | |
| 139 | ( |
| 140 | cd subdir && |
| 141 | export GIT_PAGER && |
| 142 | GIT_PAGER='\''printf "%s\n" >../args'\'' && |
| 143 | git grep -O "enum grep_pat_token" >../out && |
Johannes Schindelin | 0af88c1 | 2010-06-12 11:39:46 -0500 | [diff] [blame] | 144 | git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2 |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 145 | ) && |
| 146 | case $(cat dir) in |
| 147 | *subdir) |
| 148 | : good |
| 149 | ;; |
| 150 | *) |
| 151 | false |
| 152 | ;; |
| 153 | esac && |
| 154 | test_cmp expect args && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 155 | test_must_be_empty out && |
| 156 | test_must_be_empty out2 |
Johannes Schindelin | 678e484 | 2010-06-12 11:36:51 -0500 | [diff] [blame] | 157 | ' |
| 158 | |
| 159 | test_done |