#!/bin/sh global test_description := ''git grep --open-files-in-pager '' source ./test-lib.sh source "$TEST_DIRECTORY"/lib-pager.sh unset PAGER GIT_PAGER test_expect_success 'setup' ' test_commit initial grep.h " enum grep_pat_token { GREP_PATTERN, GREP_PATTERN_HEAD, GREP_PATTERN_BODY, GREP_AND, GREP_OPEN_PAREN, GREP_CLOSE_PAREN, GREP_NOT, GREP_OR, };" && test_commit add-user revision.c " } if (seen_dashdash) read_pathspec_from_stdin(revs, &sb, prune); strbuf_release(&sb); } static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) { append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what); " && mkdir subdir && test_commit subdir subdir/grep.c "enum grep_pat_token" && test_commit uninteresting unrelated "hello, world" && echo GREP_PATTERN >untracked ' test_expect_success SIMPLEPAGER 'git grep -O' ' cat >$less <<-\EOF && #!/bin/sh printf "%s\n" "$@" >pager-args EOF chmod +x $less && cat >expect.less <<-\EOF && +/*GREP_PATTERN grep.h EOF echo grep.h >expect.notless && >empty && PATH=.:$PATH git grep -O GREP_PATTERN >out && { test_cmp expect.less pager-args || test_cmp expect.notless pager-args } && test_cmp empty out ' test_expect_success 'git grep -O --cached' ' test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg && test_i18ngrep open-files-in-pager msg ' test_expect_success 'git grep -O --no-index' ' rm -f expect.less pager-args out && cat >expect <<-\EOF && grep.h untracked EOF >empty && ( GIT_PAGER='''''printf "%s\n" >pager-args''''' && export GIT_PAGER && git grep --no-index -O GREP_PATTERN >out ) && test_cmp expect pager-args && test_cmp empty out ' test_expect_success 'setup: fake "less"' ' cat >less <<-\EOF && #!/bin/sh printf "%s\n" "$@" >actual EOF chmod +x less ' test_expect_success 'git grep -O jumps to line in less' ' cat >expect <<-\EOF && +/*GREP_PATTERN grep.h EOF >empty && GIT_PAGER=./less git grep -O GREP_PATTERN >out && test_cmp expect actual && test_cmp empty out && git grep -O./less GREP_PATTERN >out2 && test_cmp expect actual && test_cmp empty out2 ' test_expect_success 'modified file' ' rm -f actual && cat >expect <<-\EOF && +/*enum grep_pat_token grep.h revision.c subdir/grep.c unrelated EOF >empty && echo "enum grep_pat_token" >unrelated && test_when_finished "git checkout HEAD unrelated" && GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out && test_cmp expect actual && test_cmp empty out ' test_expect_success 'copes with color settings' ' rm -f actual && echo grep.h >expect && test_config color.grep always && test_config color.grep.filename yellow && test_config color.grep.separator green && git grep -O'''''printf "%s\n" >actual''''' GREP_AND && test_cmp expect actual ' test_expect_success 'run from subdir' ' rm -f actual && echo grep.c >expect && >empty && ( cd subdir && export GIT_PAGER && GIT_PAGER='''''printf "%s\n" >../args''''' && git grep -O "enum grep_pat_token" >../out && git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2 ) && case $(cat dir) in *subdir) : good ;; *) false ;; esac && test_cmp expect args && test_cmp empty out && test_cmp empty out2 ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"git grep --open-files-in-pager\n">)} spids: [4] ) ] spids: [4] ) (C {(.)} {(./test-lib.sh)}) (C {(.)} {(DQ ($ VSub_Name "$TEST_DIRECTORY")) (/lib-pager.sh)}) (C {(unset)} {(PAGER)} {(GIT_PAGER)}) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\ttest_commit initial grep.h \"\n"> <"enum grep_pat_token {\n"> <"\tGREP_PATTERN,\n"> <"\tGREP_PATTERN_HEAD,\n"> <"\tGREP_PATTERN_BODY,\n"> <"\tGREP_AND,\n"> <"\tGREP_OPEN_PAREN,\n"> <"\tGREP_CLOSE_PAREN,\n"> <"\tGREP_NOT,\n"> <"\tGREP_OR,\n"> <"};\" &&\n"> <"\n"> <"\ttest_commit add-user revision.c \"\n"> <"\t}\n"> <"\tif (seen_dashdash)\n"> <"\t\tread_pathspec_from_stdin(revs, &sb, prune);\n"> <"\tstrbuf_release(&sb);\n"> <"}\n"> <"\n"> < "static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)\n" > <"{\n"> <"\tappend_grep_pattern(&revs->grep_filter, ptn, \\\"command line\\\", 0, what);\n"> <"\" &&\n"> <"\n"> <"\tmkdir subdir &&\n"> <"\ttest_commit subdir subdir/grep.c \"enum grep_pat_token\" &&\n"> <"\n"> <"\ttest_commit uninteresting unrelated \"hello, world\" &&\n"> <"\n"> <"\techo GREP_PATTERN >untracked\n"> ) } ) (C {(test_expect_success)} {(SIMPLEPAGER)} {(SQ <"git grep -O">)} { (SQ <"\n"> <"\tcat >$less <<-\\EOF &&\n"> <"\t#!/bin/sh\n"> <"\tprintf \"%s\\n\" \"$@\" >pager-args\n"> <"\tEOF\n"> <"\tchmod +x $less &&\n"> <"\tcat >expect.less <<-\\EOF &&\n"> <"\t+/*GREP_PATTERN\n"> <"\tgrep.h\n"> <"\tEOF\n"> <"\techo grep.h >expect.notless &&\n"> <"\t>empty &&\n"> <"\n"> <"\tPATH=.:$PATH git grep -O GREP_PATTERN >out &&\n"> <"\t{\n"> <"\t\ttest_cmp expect.less pager-args ||\n"> <"\t\ttest_cmp expect.notless pager-args\n"> <"\t} &&\n"> <"\ttest_cmp empty out\n"> ) } ) (C {(test_expect_success)} {(SQ <"git grep -O --cached">)} { (SQ <"\n"> <"\ttest_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&\n"> <"\ttest_i18ngrep open-files-in-pager msg\n"> ) } ) (C {(test_expect_success)} {(SQ <"git grep -O --no-index">)} { (SQ <"\n"> <"\trm -f expect.less pager-args out &&\n"> <"\tcat >expect <<-\\EOF &&\n"> <"\tgrep.h\n"> <"\tuntracked\n"> <"\tEOF\n"> <"\t>empty &&\n"> <"\n"> <"\t(\n"> <"\t\tGIT_PAGER="> ) (EscapedLiteralPart token:) (SQ <"printf \"%s\\n\" >pager-args">) (EscapedLiteralPart token:) (SQ <" &&\n"> <"\t\texport GIT_PAGER &&\n"> <"\t\tgit grep --no-index -O GREP_PATTERN >out\n"> <"\t) &&\n"> <"\ttest_cmp expect pager-args &&\n"> <"\ttest_cmp empty out\n"> ) } ) (C {(test_expect_success)} {(SQ <"setup: fake \"less\"">)} { (SQ <"\n"> <"\tcat >less <<-\\EOF &&\n"> <"\t#!/bin/sh\n"> <"\tprintf \"%s\\n\" \"$@\" >actual\n"> <"\tEOF\n"> <"\tchmod +x less\n"> ) } ) (C {(test_expect_success)} {(SQ <"git grep -O jumps to line in less">)} { (SQ <"\n"> <"\tcat >expect <<-\\EOF &&\n"> <"\t+/*GREP_PATTERN\n"> <"\tgrep.h\n"> <"\tEOF\n"> <"\t>empty &&\n"> <"\n"> <"\tGIT_PAGER=./less git grep -O GREP_PATTERN >out &&\n"> <"\ttest_cmp expect actual &&\n"> <"\ttest_cmp empty out &&\n"> <"\n"> <"\tgit grep -O./less GREP_PATTERN >out2 &&\n"> <"\ttest_cmp expect actual &&\n"> <"\ttest_cmp empty out2\n"> ) } ) (C {(test_expect_success)} {(SQ <"modified file">)} { (SQ <"\n"> <"\trm -f actual &&\n"> <"\tcat >expect <<-\\EOF &&\n"> <"\t+/*enum grep_pat_token\n"> <"\tgrep.h\n"> <"\trevision.c\n"> <"\tsubdir/grep.c\n"> <"\tunrelated\n"> <"\tEOF\n"> <"\t>empty &&\n"> <"\n"> <"\techo \"enum grep_pat_token\" >unrelated &&\n"> <"\ttest_when_finished \"git checkout HEAD unrelated\" &&\n"> <"\tGIT_PAGER=./less git grep -F -O \"enum grep_pat_token\" >out &&\n"> <"\ttest_cmp expect actual &&\n"> <"\ttest_cmp empty out\n"> ) } ) (C {(test_expect_success)} {(SQ <"copes with color settings">)} { (SQ <"\n"> <"\trm -f actual &&\n"> <"\techo grep.h >expect &&\n"> <"\ttest_config color.grep always &&\n"> <"\ttest_config color.grep.filename yellow &&\n"> <"\ttest_config color.grep.separator green &&\n"> <"\tgit grep -O"> ) (EscapedLiteralPart token:) (SQ <"printf \"%s\\n\" >actual">) (EscapedLiteralPart token:) (SQ <" GREP_AND &&\n"> <"\ttest_cmp expect actual\n">) } ) (C {(test_expect_success)} {(SQ <"run from subdir">)} { (SQ <"\n"> <"\trm -f actual &&\n"> <"\techo grep.c >expect &&\n"> <"\t>empty &&\n"> <"\n"> <"\t(\n"> <"\t\tcd subdir &&\n"> <"\t\texport GIT_PAGER &&\n"> <"\t\tGIT_PAGER="> ) (EscapedLiteralPart token:) (SQ <"printf \"%s\\n\" >../args">) (EscapedLiteralPart token:) (SQ <" &&\n"> <"\t\tgit grep -O \"enum grep_pat_token\" >../out &&\n"> <"\t\tgit grep -O\"pwd >../dir; :\" \"enum grep_pat_token\" >../out2\n"> <"\t) &&\n"> <"\tcase $(cat dir) in\n"> <"\t*subdir)\n"> <"\t\t: good\n"> <"\t\t;;\n"> <"\t*)\n"> <"\t\tfalse\n"> <"\t\t;;\n"> <"\tesac &&\n"> <"\ttest_cmp expect args &&\n"> <"\ttest_cmp empty out &&\n"> <"\ttest_cmp empty out2\n"> ) } ) (C {(test_done)}) ] )