Skip to content

Commit 6b35fa6

Browse files
committed
Merge branch 'sh/completion-with-reftable' into seen
Command line completion script (in contrib/) learned to work better with the reftable backend. * sh/completion-with-reftable: completion: stop checking for reference existence via `test -f` completion: refactor existence checks for special refs
2 parents 6a4b0e1 + b1f48c6 commit 6b35fa6

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

contrib/completion/git-completion.bash

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,38 @@ __git ()
122122
${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
123123
}
124124

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+
125157
# Removes backslash escaping, single quotes and double quotes from a word,
126158
# stores the result in the variable $dequoted_word.
127159
# 1: The word to dequote.
@@ -1625,7 +1657,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
16251657
_git_cherry_pick ()
16261658
{
16271659
__git_find_repo_path
1628-
if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1660+
if __git_ref_exists CHERRY_PICK_HEAD; then
16291661
__gitcomp "$__git_cherry_pick_inprogress_options"
16301662
return
16311663
fi
@@ -2067,7 +2099,7 @@ _git_log ()
20672099
__git_find_repo_path
20682100

20692101
local merge=""
2070-
if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
2102+
if __git_ref_exists MERGE_HEAD; then
20712103
merge="--merge"
20722104
fi
20732105
case "$prev,$cur" in
@@ -2934,6 +2966,7 @@ _git_reset ()
29342966

29352967
_git_restore ()
29362968
{
2969+
__git_find_repo_path
29372970
case "$prev" in
29382971
-s)
29392972
__git_complete_refs
@@ -2952,7 +2985,7 @@ _git_restore ()
29522985
__gitcomp_builtin restore
29532986
;;
29542987
*)
2955-
if __git rev-parse --verify --quiet HEAD >/dev/null; then
2988+
if __git_ref_exists HEAD; then
29562989
__git_complete_index_file "--modified"
29572990
fi
29582991
esac
@@ -2963,7 +2996,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
29632996
_git_revert ()
29642997
{
29652998
__git_find_repo_path
2966-
if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2999+
if __git_ref_exists REVERT_HEAD; then
29673000
__gitcomp "$__git_revert_inprogress_options"
29683001
return
29693002
fi
@@ -3709,7 +3742,7 @@ __gitk_main ()
37093742
__git_find_repo_path
37103743

37113744
local merge=""
3712-
if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3745+
if __git_ref_exists MERGE_HEAD; then
37133746
merge="--merge"
37143747
fi
37153748
case "$cur" in

0 commit comments

Comments
 (0)