t0020: fix ignored exit code inside loops
A loop like: for f in one two; do something $f || break done will correctly break out of the loop when we see a failure of one item, but the resulting exit code will always be zero. We can fix that by putting the loop into a function or subshell, but in this case it is simpler still to just unroll the loop. We do add a helper function, which hopefully makes the end result even more readable (in addition to being shorter). Reported-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 25, 2015 at 01:28 UTC
fd7771415b9447c8e53dd99d7946e00ee212d70d
1 file changed
+19
-35
t/t0020-crlf.sh
+19
-35
@@ -8,6 +8,13 @@ has_cr() {
8
tr '\015' Q <"$1" | grep Q >/dev/null
9
}
10
11
+# add or remove CRs to disk file in-place
12
+# usage: munge_cr <append|remove> <file>
13
+munge_cr () {
14
+ "${1}_cr" <"$2" >tmp &&
15
+ mv tmp "$2"
16
+}
17
+
18
test_expect_success setup '
19
20
git config core.autocrlf false &&
@@ -100,14 +107,9 @@ test_expect_success 'update with autocrlf=input' '
107
rm -f tmp one dir/two three &&
108
git read-tree --reset -u HEAD &&
109
git config core.autocrlf input &&
103
-
104
- for f in one dir/two
105
- do
106
- append_cr <$f >tmp && mv -f tmp $f &&
107
- git update-index -- $f ||
108
- break
109
- done &&
110
-
110
+ munge_cr append one &&
111
+ munge_cr append dir/two &&
112
+ git update-index -- one dir/two &&
113
differs=$(git diff-index --cached HEAD) &&
114
verbose test -z "$differs"
115
@@ -118,14 +120,9 @@ test_expect_success 'update with autocrlf=true' '
120
rm -f tmp one dir/two three &&
121
git read-tree --reset -u HEAD &&
122
git config core.autocrlf true &&
121
-
122
- for f in one dir/two
123
- do
124
- append_cr <$f >tmp && mv -f tmp $f &&
125
- git update-index -- $f ||
126
- break
127
- done &&
128
-
123
+ munge_cr append one &&
124
+ munge_cr append dir/two &&
125
+ git update-index -- one dir/two &&
126
differs=$(git diff-index --cached HEAD) &&
127
verbose test -z "$differs"
128
@@ -136,13 +133,9 @@ test_expect_success 'checkout with autocrlf=true' '
133
rm -f tmp one dir/two three &&
134
git config core.autocrlf true &&
135
git read-tree --reset -u HEAD &&
139
-
140
- for f in one dir/two
141
- do
142
- remove_cr <"$f" >tmp && mv -f tmp $f &&
143
- verbose git update-index -- $f ||
144
- break
145
- done &&
136
+ munge_cr remove one &&
137
+ munge_cr remove dir/two &&
138
+ git update-index -- one dir/two &&
139
test "$one" = $(git hash-object --stdin <one) &&
140
test "$two" = $(git hash-object --stdin <dir/two) &&
141
differs=$(git diff-index --cached HEAD) &&
@@ -154,18 +147,9 @@ test_expect_success 'checkout with autocrlf=input' '
147
rm -f tmp one dir/two three &&
148
git config core.autocrlf input &&
149
git read-tree --reset -u HEAD &&
157
-
158
- for f in one dir/two
159
- do
160
- if has_cr "$f"
161
- then
162
- echo "Eh? $f"
163
- false
164
- break
165
- else
166
- git update-index -- $f
167
- fi
168
- done &&
150
+ test_must_fail has_cr one &&
151
+ test_must_fail has_cr two &&
152
+ git update-index -- one dir/two &&
153
test "$one" = $(git hash-object --stdin <one) &&
154
test "$two" = $(git hash-object --stdin <dir/two) &&
155
differs=$(git diff-index --cached HEAD) &&