completion: refactor existence checks for pseudorefs

In preparation for the reftable backend, this commit introduces a '__git_pseudoref_exists' function that continues to use 'test -f' to determine whether a given pseudoref exists in the local filesystem. Signed-off-by: Stan Hu <stanhu@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stan Hu committed Dec 19, 2023 at 14:14 UTC 666270a2df49132c683f53b5efde240dd9bf5c5e
1 file changed +15 -5
contrib/completion/git-completion.bash
+15 -5
@@ -122,6 +122,15 @@ __git ()
122 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
123 }
124
125 +# Runs git in $__git_repo_path to determine whether a pseudoref exists.
126 +# 1: The pseudo-ref to search
127 +__git_pseudoref_exists ()
128 +{
129 + local ref=$1
130 +
131 + [ -f "$__git_repo_path/$ref" ]
132 +}
133 +
134 # Removes backslash escaping, single quotes and double quotes from a word,
135 # stores the result in the variable $dequoted_word.
136 # 1: The word to dequote.
@@ -1625,7 +1634,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1634 _git_cherry_pick ()
1635 {
1636 __git_find_repo_path
1628 - if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1637 + if __git_pseudoref_exists CHERRY_PICK_HEAD; then
1638 __gitcomp "$__git_cherry_pick_inprogress_options"
1639 return
1640 fi
@@ -2067,7 +2076,7 @@ _git_log ()
2076 __git_find_repo_path
2077
2078 local merge=""
2070 - if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2079 + if __git_pseudoref_exists MERGE_HEAD; then
2080 merge="--merge"
2081 fi
2082 case "$prev,$cur" in
@@ -2934,6 +2943,7 @@ _git_reset ()
2943
2944 _git_restore ()
2945 {
2946 + __git_find_repo_path
2947 case "$prev" in
2948 -s)
2949 __git_complete_refs
@@ -2952,7 +2962,7 @@ _git_restore ()
2962 __gitcomp_builtin restore
2963 ;;
2964 *)
2955 - if __git rev-parse --verify --quiet HEAD >/dev/null; then
2965 + if __git_pseudoref_exists HEAD; then
2966 __git_complete_index_file "--modified"
2967 fi
2968 esac
@@ -2963,7 +2973,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2973 _git_revert ()
2974 {
2975 __git_find_repo_path
2966 - if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2976 + if __git_pseudoref_exists REVERT_HEAD; then
2977 __gitcomp "$__git_revert_inprogress_options"
2978 return
2979 fi
@@ -3592,7 +3602,7 @@ __gitk_main ()
3602 __git_find_repo_path
3603
3604 local merge=""
3595 - if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3605 + if __git_pseudoref_exists MERGE_HEAD; then
3606 merge="--merge"
3607 fi
3608 case "$cur" in