Skip to content

Commit fd77714

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ecb590a commit fd77714

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

t/t0020-crlf.sh

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ has_cr() {
88
tr '\015' Q <"$1" | grep Q >/dev/null
99
}
1010

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+
1118
test_expect_success setup '
1219
1320
git config core.autocrlf false &&
@@ -100,14 +107,9 @@ test_expect_success 'update with autocrlf=input' '
100107
rm -f tmp one dir/two three &&
101108
git read-tree --reset -u HEAD &&
102109
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 &&
111113
differs=$(git diff-index --cached HEAD) &&
112114
verbose test -z "$differs"
113115
@@ -118,14 +120,9 @@ test_expect_success 'update with autocrlf=true' '
118120
rm -f tmp one dir/two three &&
119121
git read-tree --reset -u HEAD &&
120122
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 &&
129126
differs=$(git diff-index --cached HEAD) &&
130127
verbose test -z "$differs"
131128
@@ -136,13 +133,9 @@ test_expect_success 'checkout with autocrlf=true' '
136133
rm -f tmp one dir/two three &&
137134
git config core.autocrlf true &&
138135
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 &&
146139
test "$one" = $(git hash-object --stdin <one) &&
147140
test "$two" = $(git hash-object --stdin <dir/two) &&
148141
differs=$(git diff-index --cached HEAD) &&
@@ -154,18 +147,9 @@ test_expect_success 'checkout with autocrlf=input' '
154147
rm -f tmp one dir/two three &&
155148
git config core.autocrlf input &&
156149
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 &&
169153
test "$one" = $(git hash-object --stdin <one) &&
170154
test "$two" = $(git hash-object --stdin <dir/two) &&
171155
differs=$(git diff-index --cached HEAD) &&

0 commit comments

Comments
 (0)