@@ -122,6 +122,38 @@ __git ()
122
122
${__git_dir: +--git-dir=" $__git_dir " } " $@ " 2> /dev/null
123
123
}
124
124
125
+ # Helper function to read the first line of a file into a variable.
126
+ # __git_eread requires 2 arguments, the file path and the name of the
127
+ # variable, in that order.
128
+ #
129
+ # This is taken from git-prompt.sh.
130
+ __git_eread ()
131
+ {
132
+ test -r " $1 " && IFS=$' \r\n ' read -r " $2 " < " $1 "
133
+ }
134
+
135
+ # Runs git in $__git_repo_path to determine whether a ref exists.
136
+ # 1: The ref to search
137
+ __git_ref_exists ()
138
+ {
139
+ local ref=$1
140
+
141
+ # If the reftable is in use, we have to shell out to 'git rev-parse'
142
+ # to determine whether the ref exists instead of looking directly in
143
+ # the filesystem to determine whether the ref exists. Otherwise, use
144
+ # Bash builtins since executing Git commands are expensive on some
145
+ # platforms.
146
+ if __git_eread " $__git_repo_path /HEAD" head; then
147
+ b=" ${head# ref: } "
148
+ if [ " $b " == " refs/heads/.invalid" ]; then
149
+ __git -C " $__git_repo_path " rev-parse --verify --quiet " $ref " 2> /dev/null
150
+ return $?
151
+ fi
152
+ fi
153
+
154
+ [ -f " $__git_repo_path /$ref " ]
155
+ }
156
+
125
157
# Removes backslash escaping, single quotes and double quotes from a word,
126
158
# stores the result in the variable $dequoted_word.
127
159
# 1: The word to dequote.
@@ -1625,7 +1657,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
1625
1657
_git_cherry_pick ()
1626
1658
{
1627
1659
__git_find_repo_path
1628
- if [ -f " $__git_repo_path " / CHERRY_PICK_HEAD ] ; then
1660
+ if __git_ref_exists CHERRY_PICK_HEAD; then
1629
1661
__gitcomp " $__git_cherry_pick_inprogress_options "
1630
1662
return
1631
1663
fi
@@ -2067,7 +2099,7 @@ _git_log ()
2067
2099
__git_find_repo_path
2068
2100
2069
2101
local merge=" "
2070
- if [ -f " $__git_repo_path / MERGE_HEAD" ] ; then
2102
+ if __git_ref_exists MERGE_HEAD; then
2071
2103
merge=" --merge"
2072
2104
fi
2073
2105
case " $prev ,$cur " in
@@ -2934,6 +2966,7 @@ _git_reset ()
2934
2966
2935
2967
_git_restore ()
2936
2968
{
2969
+ __git_find_repo_path
2937
2970
case " $prev " in
2938
2971
-s)
2939
2972
__git_complete_refs
@@ -2952,7 +2985,7 @@ _git_restore ()
2952
2985
__gitcomp_builtin restore
2953
2986
;;
2954
2987
* )
2955
- if __git rev-parse --verify --quiet HEAD > /dev/null ; then
2988
+ if __git_ref_exists HEAD; then
2956
2989
__git_complete_index_file " --modified"
2957
2990
fi
2958
2991
esac
@@ -2963,7 +2996,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
2963
2996
_git_revert ()
2964
2997
{
2965
2998
__git_find_repo_path
2966
- if [ -f " $__git_repo_path " / REVERT_HEAD ] ; then
2999
+ if __git_ref_exists REVERT_HEAD; then
2967
3000
__gitcomp " $__git_revert_inprogress_options "
2968
3001
return
2969
3002
fi
@@ -3709,7 +3742,7 @@ __gitk_main ()
3709
3742
__git_find_repo_path
3710
3743
3711
3744
local merge=" "
3712
- if [ -f " $__git_repo_path / MERGE_HEAD" ] ; then
3745
+ if __git_ref_exists MERGE_HEAD; then
3713
3746
merge=" --merge"
3714
3747
fi
3715
3748
case " $cur " in
0 commit comments