8db4fc8b10893b95b4fd8c44de4bbf78d639d6d9
   1#!/bin/sh
   2
   3test_description='git grep --open-files-in-pager
   4'
   5
   6. ./test-lib.sh
   7. "$TEST_DIRECTORY"/lib-pager.sh
   8unset PAGER GIT_PAGER
   9
  10test_expect_success 'setup' '
  11        test_commit initial grep.h "
  12enum grep_pat_token {
  13        GREP_PATTERN,
  14        GREP_PATTERN_HEAD,
  15        GREP_PATTERN_BODY,
  16        GREP_AND,
  17        GREP_OPEN_PAREN,
  18        GREP_CLOSE_PAREN,
  19        GREP_NOT,
  20        GREP_OR,
  21};" &&
  22
  23        test_commit add-user revision.c "
  24        }
  25        if (seen_dashdash)
  26                read_pathspec_from_stdin(revs, &sb, prune);
  27        strbuf_release(&sb);
  28}
  29
  30static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
  31{
  32        append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what);
  33" &&
  34
  35        mkdir subdir &&
  36        test_commit subdir subdir/grep.c "enum grep_pat_token" &&
  37
  38        test_commit uninteresting unrelated "hello, world" &&
  39
  40        echo GREP_PATTERN >untracked
  41'
  42
  43test_expect_success SIMPLEPAGER 'git grep -O' '
  44        cat >$less <<-\EOF &&
  45        #!/bin/sh
  46        printf "%s\n" "$@" >pager-args
  47        EOF
  48        chmod +x $less &&
  49        cat >expect.less <<-\EOF &&
  50        +/*GREP_PATTERN
  51        grep.h
  52        EOF
  53        echo grep.h >expect.notless &&
  54        >empty &&
  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        } &&
  61        test_cmp empty out
  62'
  63
  64test_expect_success 'git grep -O --cached' '
  65        test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
  66        grep open-files-in-pager msg
  67'
  68
  69test_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
  75        >empty &&
  76
  77        (
  78                GIT_PAGER='\''printf "%s\n" >pager-args'\'' &&
  79                export GIT_PAGER &&
  80                git grep --no-index -O GREP_PATTERN >out
  81        ) &&
  82        test_cmp expect pager-args &&
  83        test_cmp empty out
  84'
  85
  86test_expect_success 'setup: fake "less"' '
  87        cat >less <<-\EOF
  88        #!/bin/sh
  89        printf "%s\n" "$@" >actual
  90        EOF
  91'
  92
  93test_expect_success 'git grep -O jumps to line in less' '
  94        cat >expect <<-\EOF &&
  95        +/*GREP_PATTERN
  96        grep.h
  97        EOF
  98        >empty &&
  99
 100        GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
 101        test_cmp expect actual &&
 102        test_cmp empty out &&
 103
 104        git grep -O./less GREP_PATTERN >out2 &&
 105        test_cmp expect actual &&
 106        test_cmp empty out2
 107'
 108
 109test_expect_success 'modified file' '
 110        rm -f actual &&
 111        cat >less <<-\EOF &&
 112        #!/bin/sh
 113        printf "%s\n" "$@" >actual
 114        EOF
 115        chmod +x $less &&
 116        cat >expect <<-\EOF &&
 117        +/*enum grep_pat_token
 118        grep.h
 119        revision.c
 120        subdir/grep.c
 121        unrelated
 122        EOF
 123        >empty &&
 124
 125        echo "enum grep_pat_token" >unrelated &&
 126        test_when_finished "git checkout HEAD unrelated" &&
 127        GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
 128        test_cmp expect actual &&
 129        test_cmp empty out
 130'
 131
 132test_expect_success 'run from subdir' '
 133        rm -f actual &&
 134        echo grep.c >expect &&
 135        >empty &&
 136
 137        (
 138                cd subdir &&
 139                export GIT_PAGER &&
 140                GIT_PAGER='\''printf "%s\n" >../args'\'' &&
 141                git grep -O "enum grep_pat_token" >../out &&
 142                git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
 143        ) &&
 144        case $(cat dir) in
 145        *subdir)
 146                : good
 147                ;;
 148        *)
 149                false
 150                ;;
 151        esac &&
 152        test_cmp expect args &&
 153        test_cmp empty out &&
 154        test_cmp empty out2
 155'
 156
 157test_done