Skip to content

Commit eca4460

Browse files
committed
t4019 "grep" portability fix
Input to "grep" is supposed to be "text", but we deliberately feed output from "git diff --color" to sift it into two sets of lines (ones with errors, the other without). Some implementations of "grep" only report matches with the exit status, without showing the matched lines in their output (e.g. OpenBSD 4.6, which says "Binary file .. matches"). Fortunately, "grep -a" is often a way to force the command to treat its input as text. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15caa41 commit eca4460

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

t/t4019-diff-wserror.sh

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,27 @@ test_expect_success setup '
2020

2121
blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
2222

23+
printf "\033[%s" "$blue_grep" >check-grep
24+
if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
25+
then
26+
grep_a=grep
27+
elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
28+
then
29+
grep_a='grep -a'
30+
else
31+
grep_a=grep ;# expected to fail...
32+
fi
33+
rm -f check-grep
34+
35+
prepare_output () {
36+
git diff --color >output
37+
$grep_a "$blue_grep" output >error
38+
$grep_a -v "$blue_grep" output >normal
39+
}
40+
2341
test_expect_success default '
2442
25-
git diff --color >output
26-
grep "$blue_grep" output >error
27-
grep -v "$blue_grep" output >normal
43+
prepare_output
2844
2945
grep Eight normal >/dev/null &&
3046
grep HT error >/dev/null &&
@@ -37,9 +53,7 @@ test_expect_success default '
3753
test_expect_success 'without -trail' '
3854
3955
git config core.whitespace -trail
40-
git diff --color >output
41-
grep "$blue_grep" output >error
42-
grep -v "$blue_grep" output >normal
56+
prepare_output
4357
4458
grep Eight normal >/dev/null &&
4559
grep HT error >/dev/null &&
@@ -53,9 +67,7 @@ test_expect_success 'without -trail (attribute)' '
5367
5468
git config --unset core.whitespace
5569
echo "F whitespace=-trail" >.gitattributes
56-
git diff --color >output
57-
grep "$blue_grep" output >error
58-
grep -v "$blue_grep" output >normal
70+
prepare_output
5971
6072
grep Eight normal >/dev/null &&
6173
grep HT error >/dev/null &&
@@ -69,9 +81,7 @@ test_expect_success 'without -space' '
6981
7082
rm -f .gitattributes
7183
git config core.whitespace -space
72-
git diff --color >output
73-
grep "$blue_grep" output >error
74-
grep -v "$blue_grep" output >normal
84+
prepare_output
7585
7686
grep Eight normal >/dev/null &&
7787
grep HT normal >/dev/null &&
@@ -85,9 +95,7 @@ test_expect_success 'without -space (attribute)' '
8595
8696
git config --unset core.whitespace
8797
echo "F whitespace=-space" >.gitattributes
88-
git diff --color >output
89-
grep "$blue_grep" output >error
90-
grep -v "$blue_grep" output >normal
98+
prepare_output
9199
92100
grep Eight normal >/dev/null &&
93101
grep HT normal >/dev/null &&
@@ -101,9 +109,7 @@ test_expect_success 'with indent-non-tab only' '
101109
102110
rm -f .gitattributes
103111
git config core.whitespace indent,-trailing,-space
104-
git diff --color >output
105-
grep "$blue_grep" output >error
106-
grep -v "$blue_grep" output >normal
112+
prepare_output
107113
108114
grep Eight error >/dev/null &&
109115
grep HT normal >/dev/null &&
@@ -117,9 +123,7 @@ test_expect_success 'with indent-non-tab only (attribute)' '
117123
118124
git config --unset core.whitespace
119125
echo "F whitespace=indent,-trailing,-space" >.gitattributes
120-
git diff --color >output
121-
grep "$blue_grep" output >error
122-
grep -v "$blue_grep" output >normal
126+
prepare_output
123127
124128
grep Eight error >/dev/null &&
125129
grep HT normal >/dev/null &&
@@ -133,9 +137,7 @@ test_expect_success 'with cr-at-eol' '
133137
134138
rm -f .gitattributes
135139
git config core.whitespace cr-at-eol
136-
git diff --color >output
137-
grep "$blue_grep" output >error
138-
grep -v "$blue_grep" output >normal
140+
prepare_output
139141
140142
grep Eight normal >/dev/null &&
141143
grep HT error >/dev/null &&
@@ -149,9 +151,7 @@ test_expect_success 'with cr-at-eol (attribute)' '
149151
150152
git config --unset core.whitespace
151153
echo "F whitespace=trailing,cr-at-eol" >.gitattributes
152-
git diff --color >output
153-
grep "$blue_grep" output >error
154-
grep -v "$blue_grep" output >normal
154+
prepare_output
155155
156156
grep Eight normal >/dev/null &&
157157
grep HT error >/dev/null &&
@@ -195,7 +195,7 @@ test_expect_success 'color new trailing blank lines' '
195195
git add x &&
196196
{ echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
197197
git diff --color x >output &&
198-
cnt=$(grep "${blue_grep}" output | wc -l) &&
198+
cnt=$($grep_a "${blue_grep}" output | wc -l) &&
199199
test $cnt = 2
200200
'
201201

0 commit comments

Comments
 (0)