]> git.ipfire.org Git - thirdparty/git.git/commitdiff
completion: refactor existence checks for pseudorefs
authorStan Hu <stanhu@gmail.com>
Tue, 19 Dec 2023 22:14:17 +0000 (14:14 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 Dec 2023 23:11:56 +0000 (15:11 -0800)
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>
contrib/completion/git-completion.bash

index 13a39ebd2e73f9b34de743b797577a5e8e98cbae..8edd002eed07c17871e2e7eee2fdad56d288a43e 100644 (file)
@@ -122,6 +122,15 @@ __git ()
                ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
 }
 
+# Runs git in $__git_repo_path to determine whether a pseudoref exists.
+# 1: The pseudo-ref to search
+__git_pseudoref_exists ()
+{
+       local ref=$1
+
+       [ -f "$__git_repo_path/$ref" ]
+}
+
 # Removes backslash escaping, single quotes and double quotes from a word,
 # stores the result in the variable $dequoted_word.
 # 1: The word to dequote.
@@ -1625,7 +1634,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
 _git_cherry_pick ()
 {
        __git_find_repo_path
-       if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
+       if __git_pseudoref_exists CHERRY_PICK_HEAD; then
                __gitcomp "$__git_cherry_pick_inprogress_options"
                return
        fi
@@ -2067,7 +2076,7 @@ _git_log ()
        __git_find_repo_path
 
        local merge=""
-       if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
+       if __git_pseudoref_exists MERGE_HEAD; then
                merge="--merge"
        fi
        case "$prev,$cur" in
@@ -2934,6 +2943,7 @@ _git_reset ()
 
 _git_restore ()
 {
+       __git_find_repo_path
        case "$prev" in
        -s)
                __git_complete_refs
@@ -2952,7 +2962,7 @@ _git_restore ()
                __gitcomp_builtin restore
                ;;
        *)
-               if __git rev-parse --verify --quiet HEAD >/dev/null; then
+               if __git_pseudoref_exists HEAD; then
                        __git_complete_index_file "--modified"
                fi
        esac
@@ -2963,7 +2973,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
 _git_revert ()
 {
        __git_find_repo_path
-       if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
+       if __git_pseudoref_exists REVERT_HEAD; then
                __gitcomp "$__git_revert_inprogress_options"
                return
        fi
@@ -3592,7 +3602,7 @@ __gitk_main ()
        __git_find_repo_path
 
        local merge=""
-       if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
+       if __git_pseudoref_exists MERGE_HEAD; then
                merge="--merge"
        fi
        case "$cur" in