Skip to content

Commit 44dbb3b

Browse files
stanhugitster
authored andcommitted
completion: support pseudoref existence checks for reftables
In contrib/completion/git-completion.bash, there are a bunch of instances where we read pseudorefs, such as HEAD, MERGE_HEAD, REVERT_HEAD, and others via the filesystem. However, the upcoming reftable refs backend won't use '.git/HEAD' at all but instead will write an invalid refname as placeholder for backwards compatibility, which will break the git-completion script. Update the '__git_pseudoref_exists' function to: 1. Recognize the placeholder '.git/HEAD' written by the reftable backend (its content is specified in the reftable specs). 2. If reftable is in use, use 'git rev-parse' to determine whether the given ref exists. 3. Otherwise, continue to use 'test -f' to check for the ref's filename. Signed-off-by: Stan Hu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 666270a commit 44dbb3b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,35 @@ __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+
125135
# Runs git in $__git_repo_path to determine whether a pseudoref exists.
126136
# 1: The pseudo-ref to search
127137
__git_pseudoref_exists ()
128138
{
129139
local ref=$1
130140

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+
131154
[ -f "$__git_repo_path/$ref" ]
132155
}
133156

0 commit comments

Comments
 (0)