t5510: prefer "git -C" to subshell for followRemoteHEAD tests
These tests set config within a sub-repo using (cd two && git config), and then a separate test_when_finished outside the subshell to clean it up. We can't use test_config to do this, because the cleanup command it registers inside the subshell would be lost. Nor can we do it before entering the subshell, because the config has to be set after some other commands are run. Let's switch these tests to use "git -C" for each command instead of a subshell. That lets us use test_config (with -C also) at the appropriate part of the test. And we no longer need the manual cleanup command. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 19, 2025 at 15:27 UTC
f1c2a42eacd272f7aa28ea8d017ae84547ee9ab1
1 file changed
+83
-119
t/t5510-fetch.sh
+83
-119
@@ -123,149 +123,113 @@ test_expect_success "fetch test remote HEAD change" '
123
'
124
125
test_expect_success "fetch test followRemoteHEAD never" '
126
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
127
- (
128
- cd two &&
129
- git update-ref --no-deref -d refs/remotes/origin/HEAD &&
130
- git config set remote.origin.followRemoteHEAD "never" &&
131
- GIT_TRACE_PACKET=$PWD/trace.out git fetch &&
132
- # Confirm that we do not even ask for HEAD when we are
133
- # not going to act on it.
134
- test_grep ! "ref-prefix HEAD" trace.out &&
135
- test_must_fail git rev-parse --verify refs/remotes/origin/HEAD
136
- )
126
+ git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
127
+ test_config -C two remote.origin.followRemoteHEAD "never" &&
128
+ GIT_TRACE_PACKET=$PWD/trace.out git -C two fetch &&
129
+ # Confirm that we do not even ask for HEAD when we are
130
+ # not going to act on it.
131
+ test_grep ! "ref-prefix HEAD" trace.out &&
132
+ test_must_fail git -C two rev-parse --verify refs/remotes/origin/HEAD
133
'
134
135
test_expect_success "fetch test followRemoteHEAD warn no change" '
140
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
141
- (
142
- cd two &&
143
- git rev-parse --verify refs/remotes/origin/other &&
144
- git remote set-head origin other &&
145
- git rev-parse --verify refs/remotes/origin/HEAD &&
146
- git rev-parse --verify refs/remotes/origin/main &&
147
- git config set remote.origin.followRemoteHEAD "warn" &&
148
- git fetch >output &&
149
- echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
150
- "but we have ${SQ}other${SQ} locally." >expect &&
151
- test_cmp expect output &&
152
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
153
- branch=$(git rev-parse refs/remotes/origin/other) &&
154
- test "z$head" = "z$branch"
155
- )
136
+ git -C two rev-parse --verify refs/remotes/origin/other &&
137
+ git -C two remote set-head origin other &&
138
+ git -C two rev-parse --verify refs/remotes/origin/HEAD &&
139
+ git -C two rev-parse --verify refs/remotes/origin/main &&
140
+ test_config -C two remote.origin.followRemoteHEAD "warn" &&
141
+ git -C two fetch >output &&
142
+ echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
143
+ "but we have ${SQ}other${SQ} locally." >expect &&
144
+ test_cmp expect output &&
145
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
146
+ branch=$(git -C two rev-parse refs/remotes/origin/other) &&
147
+ test "z$head" = "z$branch"
148
'
149
150
test_expect_success "fetch test followRemoteHEAD warn create" '
159
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
160
- (
161
- cd two &&
162
- git update-ref --no-deref -d refs/remotes/origin/HEAD &&
163
- git config set remote.origin.followRemoteHEAD "warn" &&
164
- git rev-parse --verify refs/remotes/origin/main &&
165
- output=$(git fetch) &&
166
- test "z" = "z$output" &&
167
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
168
- branch=$(git rev-parse refs/remotes/origin/main) &&
169
- test "z$head" = "z$branch"
170
- )
151
+ git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
152
+ test_config -C two remote.origin.followRemoteHEAD "warn" &&
153
+ git -C two rev-parse --verify refs/remotes/origin/main &&
154
+ output=$(git -C two fetch) &&
155
+ test "z" = "z$output" &&
156
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
157
+ branch=$(git -C two rev-parse refs/remotes/origin/main) &&
158
+ test "z$head" = "z$branch"
159
'
160
161
test_expect_success "fetch test followRemoteHEAD warn detached" '
174
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
175
- (
176
- cd two &&
177
- git update-ref --no-deref -d refs/remotes/origin/HEAD &&
178
- git update-ref refs/remotes/origin/HEAD HEAD &&
179
- HEAD=$(git log --pretty="%H") &&
180
- git config set remote.origin.followRemoteHEAD "warn" &&
181
- git fetch >output &&
182
- echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
183
- "but we have a detached HEAD pointing to" \
184
- "${SQ}${HEAD}${SQ} locally." >expect &&
185
- test_cmp expect output
186
- )
162
+ git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
163
+ git -C two update-ref refs/remotes/origin/HEAD HEAD &&
164
+ HEAD=$(git -C two log --pretty="%H") &&
165
+ test_config -C two remote.origin.followRemoteHEAD "warn" &&
166
+ git -C two fetch >output &&
167
+ echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
168
+ "but we have a detached HEAD pointing to" \
169
+ "${SQ}${HEAD}${SQ} locally." >expect &&
170
+ test_cmp expect output
171
'
172
173
test_expect_success "fetch test followRemoteHEAD warn quiet" '
190
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
191
- (
192
- cd two &&
193
- git rev-parse --verify refs/remotes/origin/other &&
194
- git remote set-head origin other &&
195
- git rev-parse --verify refs/remotes/origin/HEAD &&
196
- git rev-parse --verify refs/remotes/origin/main &&
197
- git config set remote.origin.followRemoteHEAD "warn" &&
198
- output=$(git fetch --quiet) &&
199
- test "z" = "z$output" &&
200
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
201
- branch=$(git rev-parse refs/remotes/origin/other) &&
202
- test "z$head" = "z$branch"
203
- )
174
+ git -C two rev-parse --verify refs/remotes/origin/other &&
175
+ git -C two remote set-head origin other &&
176
+ git -C two rev-parse --verify refs/remotes/origin/HEAD &&
177
+ git -C two rev-parse --verify refs/remotes/origin/main &&
178
+ test_config -C two remote.origin.followRemoteHEAD "warn" &&
179
+ output=$(git -C two fetch --quiet) &&
180
+ test "z" = "z$output" &&
181
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
182
+ branch=$(git -C two rev-parse refs/remotes/origin/other) &&
183
+ test "z$head" = "z$branch"
184
'
185
186
test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is same" '
207
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
208
- (
209
- cd two &&
210
- git rev-parse --verify refs/remotes/origin/other &&
211
- git remote set-head origin other &&
212
- git rev-parse --verify refs/remotes/origin/HEAD &&
213
- git rev-parse --verify refs/remotes/origin/main &&
214
- git config set remote.origin.followRemoteHEAD "warn-if-not-main" &&
215
- actual=$(git fetch) &&
216
- test "z" = "z$actual" &&
217
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
218
- branch=$(git rev-parse refs/remotes/origin/other) &&
219
- test "z$head" = "z$branch"
220
- )
187
+ git -C two rev-parse --verify refs/remotes/origin/other &&
188
+ git -C two remote set-head origin other &&
189
+ git -C two rev-parse --verify refs/remotes/origin/HEAD &&
190
+ git -C two rev-parse --verify refs/remotes/origin/main &&
191
+ test_config -C two remote.origin.followRemoteHEAD "warn-if-not-main" &&
192
+ actual=$(git -C two fetch) &&
193
+ test "z" = "z$actual" &&
194
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
195
+ branch=$(git -C two rev-parse refs/remotes/origin/other) &&
196
+ test "z$head" = "z$branch"
197
'
198
199
test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is different" '
224
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
225
- (
226
- cd two &&
227
- git rev-parse --verify refs/remotes/origin/other &&
228
- git remote set-head origin other &&
229
- git rev-parse --verify refs/remotes/origin/HEAD &&
230
- git rev-parse --verify refs/remotes/origin/main &&
231
- git config set remote.origin.followRemoteHEAD "warn-if-not-some/different-branch" &&
232
- git fetch >actual &&
233
- echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
234
- "but we have ${SQ}other${SQ} locally." >expect &&
235
- test_cmp expect actual &&
236
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
237
- branch=$(git rev-parse refs/remotes/origin/other) &&
238
- test "z$head" = "z$branch"
239
- )
200
+ git -C two rev-parse --verify refs/remotes/origin/other &&
201
+ git -C two remote set-head origin other &&
202
+ git -C two rev-parse --verify refs/remotes/origin/HEAD &&
203
+ git -C two rev-parse --verify refs/remotes/origin/main &&
204
+ test_config -C two remote.origin.followRemoteHEAD "warn-if-not-some/different-branch" &&
205
+ git -C two fetch >actual &&
206
+ echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
207
+ "but we have ${SQ}other${SQ} locally." >expect &&
208
+ test_cmp expect actual &&
209
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
210
+ branch=$(git -C two rev-parse refs/remotes/origin/other) &&
211
+ test "z$head" = "z$branch"
212
'
213
214
test_expect_success "fetch test followRemoteHEAD always" '
243
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
244
- (
245
- cd two &&
246
- git rev-parse --verify refs/remotes/origin/other &&
247
- git remote set-head origin other &&
248
- git rev-parse --verify refs/remotes/origin/HEAD &&
249
- git rev-parse --verify refs/remotes/origin/main &&
250
- git config set remote.origin.followRemoteHEAD "always" &&
251
- git fetch &&
252
- head=$(git rev-parse refs/remotes/origin/HEAD) &&
253
- branch=$(git rev-parse refs/remotes/origin/main) &&
254
- test "z$head" = "z$branch"
255
- )
215
+ git -C two rev-parse --verify refs/remotes/origin/other &&
216
+ git -C two remote set-head origin other &&
217
+ git -C two rev-parse --verify refs/remotes/origin/HEAD &&
218
+ git -C two rev-parse --verify refs/remotes/origin/main &&
219
+ test_config -C two remote.origin.followRemoteHEAD "always" &&
220
+ git -C two fetch &&
221
+ head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
222
+ branch=$(git -C two rev-parse refs/remotes/origin/main) &&
223
+ test "z$head" = "z$branch"
224
'
225
226
test_expect_success 'followRemoteHEAD does not kick in with refspecs' '
259
- test_when_finished "git -C two config unset remote.origin.followRemoteHEAD" &&
260
- (
261
- cd two &&
262
- git remote set-head origin other &&
263
- git config set remote.origin.followRemoteHEAD always &&
264
- git fetch origin refs/heads/main:refs/remotes/origin/main &&
265
- echo refs/remotes/origin/other >expect &&
266
- git symbolic-ref refs/remotes/origin/HEAD >actual &&
267
- test_cmp expect actual
268
- )
227
+ git -C two remote set-head origin other &&
228
+ test_config -C two remote.origin.followRemoteHEAD always &&
229
+ git -C two fetch origin refs/heads/main:refs/remotes/origin/main &&
230
+ echo refs/remotes/origin/other >expect &&
231
+ git -C two symbolic-ref refs/remotes/origin/HEAD >actual &&
232
+ test_cmp expect actual
233
'
234
235
test_expect_success 'fetch --prune on its own works as expected' '