diff options
| author | Junio C Hamano <gitster@pobox.com> | 2023-11-08 11:03:59 +0900 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2023-11-08 11:04:00 +0900 |
| commit | d8972a5abd77d1f50aaaa7be7d70e80c9d18cdef (patch) | |
| tree | 5e1e74ba07c7e57f3a16a8a6cdb6631a9afac712 /t/test-lib-functions.sh | |
| parent | 42b87f7ee60323f2e45a91233db80d44d3e33ad2 (diff) | |
| parent | 0497e6c61172faabf412e013f2fc30a05054a46d (diff) | |
| download | git-d8972a5abd77d1f50aaaa7be7d70e80c9d18cdef.tar.gz | |
Merge branch 'ps/show-ref'
Teach "git show-ref" a mode to check the existence of a ref.
* ps/show-ref:
t: use git-show-ref(1) to check for ref existence
builtin/show-ref: add new mode to check for reference existence
builtin/show-ref: explicitly spell out different modes in synopsis
builtin/show-ref: ensure mutual exclusiveness of subcommands
builtin/show-ref: refactor options for patterns subcommand
builtin/show-ref: stop using global vars for `show_one()`
builtin/show-ref: stop using global variable to count matches
builtin/show-ref: refactor `--exclude-existing` options
builtin/show-ref: fix dead code when passing patterns
builtin/show-ref: fix leaking string buffer
builtin/show-ref: split up different subcommands
builtin/show-ref: convert pattern to a local variable
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> |
