]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t: use git-show-ref(1) to check for ref existence
authorPatrick Steinhardt <ps@pks.im>
Tue, 31 Oct 2023 08:16:59 +0000 (09:16 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Nov 2023 03:09:01 +0000 (12:09 +0900)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t1430-bad-ref-name.sh
t/t3200-branch.sh
t/t5521-pull-options.sh
t/t5605-clone-local.sh
t/test-lib-functions.sh

index ff1c967d550318e0b17ad3c9d1e08654c51e7506..7b7d6953c62d1be63c7f16e0c561b46b5e678e23 100755 (executable)
@@ -205,8 +205,9 @@ test_expect_success 'update-ref --no-deref -d can delete symref to broken name'
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
        test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+       test_ref_exists refs/heads/badname &&
        git update-ref --no-deref -d refs/heads/badname >output 2>error &&
-       test_path_is_missing .git/refs/heads/badname &&
+       test_ref_missing refs/heads/badname &&
        test_must_be_empty output &&
        test_must_be_empty error
 '
@@ -216,8 +217,9 @@ test_expect_success 'branch -d can delete symref to broken name' '
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
        test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+       test_ref_exists refs/heads/badname &&
        git branch -d badname >output 2>error &&
-       test_path_is_missing .git/refs/heads/badname &&
+       test_ref_missing refs/heads/badname &&
        test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
        test_must_be_empty error
 '
@@ -225,8 +227,9 @@ test_expect_success 'branch -d can delete symref to broken name' '
 test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
        test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+       test_ref_exists refs/heads/badname &&
        git update-ref --no-deref -d refs/heads/badname >output 2>error &&
-       test_path_is_missing .git/refs/heads/badname &&
+       test_ref_missing refs/heads/badname &&
        test_must_be_empty output &&
        test_must_be_empty error
 '
@@ -234,8 +237,9 @@ test_expect_success 'update-ref --no-deref -d can delete dangling symref to brok
 test_expect_success 'branch -d can delete dangling symref to broken name' '
        test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+       test_ref_exists refs/heads/badname &&
        git branch -d badname >output 2>error &&
-       test_path_is_missing .git/refs/heads/badname &&
+       test_ref_missing refs/heads/badname &&
        test_i18ngrep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
        test_must_be_empty error
 '
@@ -245,8 +249,9 @@ test_expect_success 'update-ref -d can delete broken name through symref' '
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
        test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+       test_ref_exists refs/heads/broken...ref &&
        git update-ref -d refs/heads/badname >output 2>error &&
-       test_path_is_missing .git/refs/heads/broken...ref &&
+       test_ref_missing refs/heads/broken...ref &&
        test_must_be_empty output &&
        test_must_be_empty error
 '
@@ -254,8 +259,9 @@ test_expect_success 'update-ref -d can delete broken name through symref' '
 test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
        printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+       test_ref_exists refs/heads/broken...symref &&
        git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
-       test_path_is_missing .git/refs/heads/broken...symref &&
+       test_ref_missing refs/heads/broken...symref &&
        test_must_be_empty output &&
        test_must_be_empty error
 '
@@ -263,8 +269,9 @@ test_expect_success 'update-ref --no-deref -d can delete symref with broken name
 test_expect_success 'branch -d can delete symref with broken name' '
        printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+       test_ref_exists refs/heads/broken...symref &&
        git branch -d broken...symref >output 2>error &&
-       test_path_is_missing .git/refs/heads/broken...symref &&
+       test_ref_missing refs/heads/broken...symref &&
        test_i18ngrep "Deleted branch broken...symref (was refs/heads/main)" output &&
        test_must_be_empty error
 '
@@ -272,8 +279,9 @@ test_expect_success 'branch -d can delete symref with broken name' '
 test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
        printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+       test_ref_exists refs/heads/broken...symref &&
        git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
-       test_path_is_missing .git/refs/heads/broken...symref &&
+       test_ref_missing refs/heads/broken...symref &&
        test_must_be_empty output &&
        test_must_be_empty error
 '
@@ -281,8 +289,9 @@ test_expect_success 'update-ref --no-deref -d can delete dangling symref with br
 test_expect_success 'branch -d can delete dangling symref with broken name' '
        printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
        test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+       test_ref_exists refs/heads/broken...symref &&
        git branch -d broken...symref >output 2>error &&
-       test_path_is_missing .git/refs/heads/broken...symref &&
+       test_ref_missing refs/heads/broken...symref &&
        test_i18ngrep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
        test_must_be_empty error
 '
index 080e4f24a6e2b1a238cfc7f340579b4eec2066d3..bde4f1485b76c5ad1afb1654c831539ad39c69c8 100755 (executable)
@@ -25,7 +25,7 @@ test_expect_success 'prepare a trivial repository' '
 
 test_expect_success 'git branch --help should not have created a bogus branch' '
        test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
-       test_path_is_missing .git/refs/heads/--help
+       test_ref_missing refs/heads/--help
 '
 
 test_expect_success 'branch -h in broken repository' '
@@ -40,7 +40,8 @@ test_expect_success 'branch -h in broken repository' '
 '
 
 test_expect_success 'git branch abc should create a branch' '
-       git branch abc && test_path_is_file .git/refs/heads/abc
+       git branch abc &&
+       test_ref_exists refs/heads/abc
 '
 
 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' '
 '
 
 test_expect_success 'git branch a/b/c should create a branch' '
-       git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
+       git branch a/b/c &&
+       test_ref_exists refs/heads/a/b/c
 '
 
 test_expect_success 'git branch mb main... should create a branch' '
-       git branch mb main... && test_path_is_file .git/refs/heads/mb
+       git branch mb main... &&
+       test_ref_exists refs/heads/mb
 '
 
 test_expect_success 'git branch HEAD should fail' '
@@ -78,14 +81,14 @@ EOF
 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
        GIT_COMMITTER_DATE="2005-05-26 23:30" \
        git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
-       test_path_is_file .git/refs/heads/d/e/f &&
+       test_ref_exists refs/heads/d/e/f &&
        test_path_is_file .git/logs/refs/heads/d/e/f &&
        test_cmp expect .git/logs/refs/heads/d/e/f
 '
 
 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
        git branch -d d/e/f &&
-       test_path_is_missing .git/refs/heads/d/e/f &&
+       test_ref_missing refs/heads/d/e/f &&
        test_must_fail git reflog exists refs/heads/d/e/f
 '
 
@@ -213,7 +216,7 @@ test_expect_success 'git branch -M should leave orphaned HEAD alone' '
                test_commit initial &&
                git checkout --orphan lonely &&
                grep lonely .git/HEAD &&
-               test_path_is_missing .git/refs/head/lonely &&
+               test_ref_missing refs/head/lonely &&
                git branch -M main mistress &&
                grep lonely .git/HEAD
        )
@@ -799,8 +802,8 @@ test_expect_success 'deleting a symref' '
        git symbolic-ref refs/heads/symref refs/heads/target &&
        echo "Deleted branch symref (was refs/heads/target)." >expect &&
        git branch -d symref >actual &&
-       test_path_is_file .git/refs/heads/target &&
-       test_path_is_missing .git/refs/heads/symref &&
+       test_ref_exists refs/heads/target &&
+       test_ref_missing refs/heads/symref &&
        test_cmp expect actual
 '
 
@@ -809,16 +812,16 @@ test_expect_success 'deleting a dangling symref' '
        test_path_is_file .git/refs/heads/dangling-symref &&
        echo "Deleted branch dangling-symref (was nowhere)." >expect &&
        git branch -d dangling-symref >actual &&
-       test_path_is_missing .git/refs/heads/dangling-symref &&
+       test_ref_missing refs/heads/dangling-symref &&
        test_cmp expect actual
 '
 
 test_expect_success 'deleting a self-referential symref' '
        git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
-       test_path_is_file .git/refs/heads/self-reference &&
+       test_ref_exists refs/heads/self-reference &&
        echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
        git branch -d self-reference >actual &&
-       test_path_is_missing .git/refs/heads/self-reference &&
+       test_ref_missing refs/heads/self-reference &&
        test_cmp expect actual
 '
 
@@ -826,8 +829,8 @@ test_expect_success 'renaming a symref is not allowed' '
        git symbolic-ref refs/heads/topic refs/heads/main &&
        test_must_fail git branch -m topic new-topic &&
        git symbolic-ref refs/heads/topic &&
-       test_path_is_file .git/refs/heads/main &&
-       test_path_is_missing .git/refs/heads/new-topic
+       test_ref_exists refs/heads/main &&
+       test_ref_missing refs/heads/new-topic
 '
 
 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
@@ -1142,7 +1145,7 @@ EOF
 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
        GIT_COMMITTER_DATE="2005-05-26 23:30" \
        git checkout -b g/h/i -l main &&
-       test_path_is_file .git/refs/heads/g/h/i &&
+       test_ref_exists refs/heads/g/h/i &&
        test_path_is_file .git/logs/refs/heads/g/h/i &&
        test_cmp expect .git/logs/refs/heads/g/h/i
 '
index 079b2f2536e6974c90b152226d8ea2189016a6f6..3681859f98337504d4be02fa19248c7fc4d09a61 100755 (executable)
@@ -143,7 +143,7 @@ test_expect_success 'git pull --dry-run' '
                cd clonedry &&
                git pull --dry-run ../parent &&
                test_path_is_missing .git/FETCH_HEAD &&
-               test_path_is_missing .git/refs/heads/main &&
+               test_ref_missing refs/heads/main &&
                test_path_is_missing .git/index &&
                test_path_is_missing file
        )
@@ -157,7 +157,7 @@ test_expect_success 'git pull --all --dry-run' '
                git remote add origin ../parent &&
                git pull --all --dry-run &&
                test_path_is_missing .git/FETCH_HEAD &&
-               test_path_is_missing .git/refs/remotes/origin/main &&
+               test_ref_missing refs/remotes/origin/main &&
                test_path_is_missing .git/index &&
                test_path_is_missing file
        )
index 1d7b1abda1a4870971efb8408c11a4dde2e7564e..946c5751885671859ae309458c34106b79d67966 100755 (executable)
@@ -69,7 +69,7 @@ test_expect_success 'local clone of repo with nonexistent ref in HEAD' '
        git clone a d &&
        (cd d &&
        git fetch &&
-       test ! -e .git/refs/remotes/origin/HEAD)
+       test_ref_missing refs/remotes/origin/HEAD)
 '
 
 test_expect_success 'bundle clone without .bundle suffix' '
index 2f8868caa171d2d3ce78f3672e937ab7f8a3db97..56b33536ed1d4929f66b8572587e0b8ea1b79fb0 100644 (file)
@@ -251,6 +251,61 @@ debug () {
        done
 }
 
+# Usage: test_ref_exists [options] <ref>
+#
+#   -C <dir>:
+#      Run all git commands in directory <dir>
+#
+# This helper function checks whether a reference exists. Symrefs or object IDs
+# will not be resolved. Can be used to check references with bad names.
+test_ref_exists () {
+       local indir=
+
+       while test $# != 0
+       do
+               case "$1" in
+               -C)
+                       indir="$2"
+                       shift
+                       ;;
+               *)
+                       break
+                       ;;
+               esac
+               shift
+       done &&
+
+       indir=${indir:+"$indir"/} &&
+
+       if test "$#" != 1
+       then
+               BUG "expected exactly one reference"
+       fi &&
+
+       git ${indir:+ -C "$indir"} show-ref --exists "$1"
+}
+
+# Behaves the same as test_ref_exists, except that it checks for the absence of
+# a reference. This is preferable to `! test_ref_exists` as this function is
+# able to distinguish actually-missing references from other, generic errors.
+test_ref_missing () {
+       test_ref_exists "$@"
+       case "$?" in
+       2)
+               # This is the good case.
+               return 0
+               ;;
+       0)
+               echo >&4 "test_ref_missing: reference exists"
+               return 1
+               ;;
+       *)
+               echo >&4 "test_ref_missing: generic error"
+               return 1
+               ;;
+       esac
+}
+
 # Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]
 #   -C <dir>:
 #      Run all git commands in directory <dir>