aboutsummaryrefslogtreecommitdiff
path: root/t/t7811-grep-open.sh
blob: aedf484feeb9da05054f18ab2f4913865048fe35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/sh

test_description='git grep --open-files-in-pager
'

. ./test-lib.sh
. "$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 C_LOCALE_OUTPUT 'git grep -O --cached' '
	test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
	grep 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_config() {
	git config "$1" "$2" &&
	test_when_finished "git config --unset $1"
}

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