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 <gitster@pobox.com>
Junio C Hamano committed
Dec 26, 2009 at 13:53 UTC
eca4460eeb18b895cb6ca590490073f75fd010a6
1 file changed
+28
-28
t/t4019-diff-wserror.sh
+28
-28
@@ -20,11 +20,27 @@ test_expect_success setup '
20
21
blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
22
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
+
41
test_expect_success default '
42
25
- git diff --color >output
26
- grep "$blue_grep" output >error
27
- grep -v "$blue_grep" output >normal
43
+ prepare_output
44
45
grep Eight normal >/dev/null &&
46
grep HT error >/dev/null &&
@@ -37,9 +53,7 @@ test_expect_success default '
53
test_expect_success 'without -trail' '
54
55
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
57
58
grep Eight normal >/dev/null &&
59
grep HT error >/dev/null &&
@@ -53,9 +67,7 @@ test_expect_success 'without -trail (attribute)' '
67
68
git config --unset core.whitespace
69
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
71
72
grep Eight normal >/dev/null &&
73
grep HT error >/dev/null &&
@@ -69,9 +81,7 @@ test_expect_success 'without -space' '
81
82
rm -f .gitattributes
83
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
85
86
grep Eight normal >/dev/null &&
87
grep HT normal >/dev/null &&
@@ -85,9 +95,7 @@ test_expect_success 'without -space (attribute)' '
95
96
git config --unset core.whitespace
97
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
99
100
grep Eight normal >/dev/null &&
101
grep HT normal >/dev/null &&
@@ -101,9 +109,7 @@ test_expect_success 'with indent-non-tab only' '
109
110
rm -f .gitattributes
111
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
113
114
grep Eight error >/dev/null &&
115
grep HT normal >/dev/null &&
@@ -117,9 +123,7 @@ test_expect_success 'with indent-non-tab only (attribute)' '
123
124
git config --unset core.whitespace
125
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
127
128
grep Eight error >/dev/null &&
129
grep HT normal >/dev/null &&
@@ -133,9 +137,7 @@ test_expect_success 'with cr-at-eol' '
137
138
rm -f .gitattributes
139
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
141
142
grep Eight normal >/dev/null &&
143
grep HT error >/dev/null &&
@@ -149,9 +151,7 @@ test_expect_success 'with cr-at-eol (attribute)' '
151
152
git config --unset core.whitespace
153
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
155
156
grep Eight normal >/dev/null &&
157
grep HT error >/dev/null &&
@@ -195,7 +195,7 @@ test_expect_success 'color new trailing blank lines' '
195
git add x &&
196
{ echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
197
git diff --color x >output &&
198
- cnt=$(grep "${blue_grep}" output | wc -l) &&
198
+ cnt=$($grep_a "${blue_grep}" output | wc -l) &&
199
test $cnt = 2
200
'
201