Skip to content

Commit 0497e6c

Browse files
pks-tgitster
authored andcommitted
t: use git-show-ref(1) to check for ref existence
Convert tests that use `test_path_is_file` and `test_path_is_missing` to instead use a set of helpers `test_ref_exists` and `test_ref_missing`. These helpers are implemented via the newly introduced `git show-ref --exists` command. Thus, we can avoid intimate knowledge of how the ref backend stores references on disk. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9080a7f commit 0497e6c

File tree

5 files changed

+94
-27
lines changed

5 files changed

+94
-27
lines changed

t/t1430-bad-ref-name.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ test_expect_success 'update-ref --no-deref -d can delete symref to broken name'
205205
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
206206
test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
207207
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
208+
test_ref_exists refs/heads/badname &&
208209
git update-ref --no-deref -d refs/heads/badname >output 2>error &&
209-
test_path_is_missing .git/refs/heads/badname &&
210+
test_ref_missing refs/heads/badname &&
210211
test_must_be_empty output &&
211212
test_must_be_empty error
212213
'
@@ -216,26 +217,29 @@ test_expect_success 'branch -d can delete symref to broken name' '
216217
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
217218
test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
218219
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
220+
test_ref_exists refs/heads/badname &&
219221
git branch -d badname >output 2>error &&
220-
test_path_is_missing .git/refs/heads/badname &&
222+
test_ref_missing refs/heads/badname &&
221223
test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
222224
test_must_be_empty error
223225
'
224226

225227
test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
226228
test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
227229
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
230+
test_ref_exists refs/heads/badname &&
228231
git update-ref --no-deref -d refs/heads/badname >output 2>error &&
229-
test_path_is_missing .git/refs/heads/badname &&
232+
test_ref_missing refs/heads/badname &&
230233
test_must_be_empty output &&
231234
test_must_be_empty error
232235
'
233236

234237
test_expect_success 'branch -d can delete dangling symref to broken name' '
235238
test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
236239
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
240+
test_ref_exists refs/heads/badname &&
237241
git branch -d badname >output 2>error &&
238-
test_path_is_missing .git/refs/heads/badname &&
242+
test_ref_missing refs/heads/badname &&
239243
test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
240244
test_must_be_empty error
241245
'
@@ -245,44 +249,49 @@ test_expect_success 'update-ref -d can delete broken name through symref' '
245249
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
246250
test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
247251
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
252+
test_ref_exists refs/heads/broken...ref &&
248253
git update-ref -d refs/heads/badname >output 2>error &&
249-
test_path_is_missing .git/refs/heads/broken...ref &&
254+
test_ref_missing refs/heads/broken...ref &&
250255
test_must_be_empty output &&
251256
test_must_be_empty error
252257
'
253258

254259
test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
255260
printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
256261
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
262+
test_ref_exists refs/heads/broken...symref &&
257263
git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
258-
test_path_is_missing .git/refs/heads/broken...symref &&
264+
test_ref_missing refs/heads/broken...symref &&
259265
test_must_be_empty output &&
260266
test_must_be_empty error
261267
'
262268

263269
test_expect_success 'branch -d can delete symref with broken name' '
264270
printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
265271
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
272+
test_ref_exists refs/heads/broken...symref &&
266273
git branch -d broken...symref >output 2>error &&
267-
test_path_is_missing .git/refs/heads/broken...symref &&
274+
test_ref_missing refs/heads/broken...symref &&
268275
test_i18ngrep "Deleted branch broken...symref (was refs/heads/main)" output &&
269276
test_must_be_empty error
270277
'
271278

272279
test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
273280
printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
274281
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
282+
test_ref_exists refs/heads/broken...symref &&
275283
git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
276-
test_path_is_missing .git/refs/heads/broken...symref &&
284+
test_ref_missing refs/heads/broken...symref &&
277285
test_must_be_empty output &&
278286
test_must_be_empty error
279287
'
280288

281289
test_expect_success 'branch -d can delete dangling symref with broken name' '
282290
printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
283291
test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
292+
test_ref_exists refs/heads/broken...symref &&
284293
git branch -d broken...symref >output 2>error &&
285-
test_path_is_missing .git/refs/heads/broken...symref &&
294+
test_ref_missing refs/heads/broken...symref &&
286295
test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
287296
test_must_be_empty error
288297
'

t/t3200-branch.sh

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test_expect_success 'prepare a trivial repository' '
2525

2626
test_expect_success 'git branch --help should not have created a bogus branch' '
2727
test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
28-
test_path_is_missing .git/refs/heads/--help
28+
test_ref_missing refs/heads/--help
2929
'
3030

3131
test_expect_success 'branch -h in broken repository' '
@@ -40,7 +40,8 @@ test_expect_success 'branch -h in broken repository' '
4040
'
4141

4242
test_expect_success 'git branch abc should create a branch' '
43-
git branch abc && test_path_is_file .git/refs/heads/abc
43+
git branch abc &&
44+
test_ref_exists refs/heads/abc
4445
'
4546

4647
test_expect_success 'git branch abc should fail when abc exists' '
@@ -61,11 +62,13 @@ test_expect_success 'git branch --force abc should succeed when abc exists' '
6162
'
6263

6364
test_expect_success 'git branch a/b/c should create a branch' '
64-
git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
65+
git branch a/b/c &&
66+
test_ref_exists refs/heads/a/b/c
6567
'
6668

6769
test_expect_success 'git branch mb main... should create a branch' '
68-
git branch mb main... && test_path_is_file .git/refs/heads/mb
70+
git branch mb main... &&
71+
test_ref_exists refs/heads/mb
6972
'
7073

7174
test_expect_success 'git branch HEAD should fail' '
@@ -78,14 +81,14 @@ EOF
7881
test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
7982
GIT_COMMITTER_DATE="2005-05-26 23:30" \
8083
git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
81-
test_path_is_file .git/refs/heads/d/e/f &&
84+
test_ref_exists refs/heads/d/e/f &&
8285
test_path_is_file .git/logs/refs/heads/d/e/f &&
8386
test_cmp expect .git/logs/refs/heads/d/e/f
8487
'
8588

8689
test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
8790
git branch -d d/e/f &&
88-
test_path_is_missing .git/refs/heads/d/e/f &&
91+
test_ref_missing refs/heads/d/e/f &&
8992
test_must_fail git reflog exists refs/heads/d/e/f
9093
'
9194

@@ -213,7 +216,7 @@ test_expect_success 'git branch -M should leave orphaned HEAD alone' '
213216
test_commit initial &&
214217
git checkout --orphan lonely &&
215218
grep lonely .git/HEAD &&
216-
test_path_is_missing .git/refs/head/lonely &&
219+
test_ref_missing refs/head/lonely &&
217220
git branch -M main mistress &&
218221
grep lonely .git/HEAD
219222
)
@@ -799,8 +802,8 @@ test_expect_success 'deleting a symref' '
799802
git symbolic-ref refs/heads/symref refs/heads/target &&
800803
echo "Deleted branch symref (was refs/heads/target)." >expect &&
801804
git branch -d symref >actual &&
802-
test_path_is_file .git/refs/heads/target &&
803-
test_path_is_missing .git/refs/heads/symref &&
805+
test_ref_exists refs/heads/target &&
806+
test_ref_missing refs/heads/symref &&
804807
test_cmp expect actual
805808
'
806809

@@ -809,25 +812,25 @@ test_expect_success 'deleting a dangling symref' '
809812
test_path_is_file .git/refs/heads/dangling-symref &&
810813
echo "Deleted branch dangling-symref (was nowhere)." >expect &&
811814
git branch -d dangling-symref >actual &&
812-
test_path_is_missing .git/refs/heads/dangling-symref &&
815+
test_ref_missing refs/heads/dangling-symref &&
813816
test_cmp expect actual
814817
'
815818

816819
test_expect_success 'deleting a self-referential symref' '
817820
git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
818-
test_path_is_file .git/refs/heads/self-reference &&
821+
test_ref_exists refs/heads/self-reference &&
819822
echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
820823
git branch -d self-reference >actual &&
821-
test_path_is_missing .git/refs/heads/self-reference &&
824+
test_ref_missing refs/heads/self-reference &&
822825
test_cmp expect actual
823826
'
824827

825828
test_expect_success 'renaming a symref is not allowed' '
826829
git symbolic-ref refs/heads/topic refs/heads/main &&
827830
test_must_fail git branch -m topic new-topic &&
828831
git symbolic-ref refs/heads/topic &&
829-
test_path_is_file .git/refs/heads/main &&
830-
test_path_is_missing .git/refs/heads/new-topic
832+
test_ref_exists refs/heads/main &&
833+
test_ref_missing refs/heads/new-topic
831834
'
832835

833836
test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
@@ -1142,7 +1145,7 @@ EOF
11421145
test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
11431146
GIT_COMMITTER_DATE="2005-05-26 23:30" \
11441147
git checkout -b g/h/i -l main &&
1145-
test_path_is_file .git/refs/heads/g/h/i &&
1148+
test_ref_exists refs/heads/g/h/i &&
11461149
test_path_is_file .git/logs/refs/heads/g/h/i &&
11471150
test_cmp expect .git/logs/refs/heads/g/h/i
11481151
'

t/t5521-pull-options.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ test_expect_success 'git pull --dry-run' '
143143
cd clonedry &&
144144
git pull --dry-run ../parent &&
145145
test_path_is_missing .git/FETCH_HEAD &&
146-
test_path_is_missing .git/refs/heads/main &&
146+
test_ref_missing refs/heads/main &&
147147
test_path_is_missing .git/index &&
148148
test_path_is_missing file
149149
)
@@ -157,7 +157,7 @@ test_expect_success 'git pull --all --dry-run' '
157157
git remote add origin ../parent &&
158158
git pull --all --dry-run &&
159159
test_path_is_missing .git/FETCH_HEAD &&
160-
test_path_is_missing .git/refs/remotes/origin/main &&
160+
test_ref_missing refs/remotes/origin/main &&
161161
test_path_is_missing .git/index &&
162162
test_path_is_missing file
163163
)

t/t5605-clone-local.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test_expect_success 'local clone of repo with nonexistent ref in HEAD' '
6969
git clone a d &&
7070
(cd d &&
7171
git fetch &&
72-
test ! -e .git/refs/remotes/origin/HEAD)
72+
test_ref_missing refs/remotes/origin/HEAD)
7373
'
7474

7575
test_expect_success 'bundle clone without .bundle suffix' '

t/test-lib-functions.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,61 @@ debug () {
251251
done
252252
}
253253

254+
# Usage: test_ref_exists [options] <ref>
255+
#
256+
# -C <dir>:
257+
# Run all git commands in directory <dir>
258+
#
259+
# This helper function checks whether a reference exists. Symrefs or object IDs
260+
# will not be resolved. Can be used to check references with bad names.
261+
test_ref_exists () {
262+
local indir=
263+
264+
while test $# != 0
265+
do
266+
case "$1" in
267+
-C)
268+
indir="$2"
269+
shift
270+
;;
271+
*)
272+
break
273+
;;
274+
esac
275+
shift
276+
done &&
277+
278+
indir=${indir:+"$indir"/} &&
279+
280+
if test "$#" != 1
281+
then
282+
BUG "expected exactly one reference"
283+
fi &&
284+
285+
git ${indir:+ -C "$indir"} show-ref --exists "$1"
286+
}
287+
288+
# Behaves the same as test_ref_exists, except that it checks for the absence of
289+
# a reference. This is preferable to `! test_ref_exists` as this function is
290+
# able to distinguish actually-missing references from other, generic errors.
291+
test_ref_missing () {
292+
test_ref_exists "$@"
293+
case "$?" in
294+
2)
295+
# This is the good case.
296+
return 0
297+
;;
298+
0)
299+
echo >&4 "test_ref_missing: reference exists"
300+
return 1
301+
;;
302+
*)
303+
echo >&4 "test_ref_missing: generic error"
304+
return 1
305+
;;
306+
esac
307+
}
308+
254309
# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]
255310
# -C <dir>:
256311
# Run all git commands in directory <dir>

0 commit comments

Comments
 (0)