diff options
| author | Patrick Steinhardt <ps@pks.im> | 2023-10-31 09:16:59 +0100 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2023-11-01 12:09:01 +0900 |
| commit | 0497e6c61172faabf412e013f2fc30a05054a46d (patch) | |
| tree | 25d1588270a35a905b41a181037533c0873a28ad /t/test-lib-functions.sh | |
| parent | builtin/show-ref: add new mode to check for reference existence (diff) | |
| download | git-0497e6c61172faabf412e013f2fc30a05054a46d.tar.gz git-0497e6c61172faabf412e013f2fc30a05054a46d.zip | |
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/test-lib-functions.sh')
| -rw-r--r-- | t/test-lib-functions.sh | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 2f8868caa1..56b33536ed 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -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> |
