Skip to content

Commit 9de31f7

Browse files
alisonatworkgitster
authored andcommitted
completion: add case-insensitive match of pseudorefs
When GIT_COMPLETION_IGNORE_CASE is set, also allow lowercase completion text like "head" to match uppercase HEAD and other pseudorefs. Signed-off-by: Alison Winters <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bab766 commit 9de31f7

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

contrib/completion/git-completion.bash

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ __git_refs ()
722722
local format refs
723723
local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
724724
local match="${4-}"
725+
local umatch="${4-}"
725726
local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
726727

727728
__git_find_repo_path
@@ -745,12 +746,19 @@ __git_refs ()
745746
fi
746747
fi
747748

749+
if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
750+
then
751+
# uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
752+
umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
753+
fi
754+
748755
if [ "$list_refs_from" = path ]; then
749756
if [[ "$cur_" == ^* ]]; then
750757
pfx="$pfx^"
751758
fer_pfx="$fer_pfx^"
752759
cur_=${cur_#^}
753760
match=${match#^}
761+
umatch=${umatch#^}
754762
fi
755763
case "$cur_" in
756764
refs|refs/*)
@@ -761,7 +769,7 @@ __git_refs ()
761769
*)
762770
for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD; do
763771
case "$i" in
764-
$match*)
772+
$match*|$umatch*)
765773
if [ -e "$dir/$i" ]; then
766774
echo "$pfx$i$sfx"
767775
fi
@@ -795,7 +803,7 @@ __git_refs ()
795803
*)
796804
if [ "$list_refs_from" = remote ]; then
797805
case "HEAD" in
798-
$match*) echo "${pfx}HEAD$sfx" ;;
806+
$match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
799807
esac
800808
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
801809
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
@@ -804,7 +812,7 @@ __git_refs ()
804812
else
805813
local query_symref
806814
case "HEAD" in
807-
$match*) query_symref="HEAD" ;;
815+
$match*|$umatch*) query_symref="HEAD" ;;
808816
esac
809817
__git ls-remote "$remote" $query_symref \
810818
"refs/tags/$match*" "refs/heads/$match*" \

t/t9902-completion.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,21 @@ test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGN
22702270
)
22712271
'
22722272

2273+
test_expect_success 'checkout completes pseudo refs' '
2274+
test_completion "git checkout H" <<-\EOF
2275+
HEAD Z
2276+
EOF
2277+
'
2278+
2279+
test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2280+
(
2281+
GIT_COMPLETION_IGNORE_CASE=1 &&
2282+
test_completion "git checkout h" <<-\EOF
2283+
HEAD Z
2284+
EOF
2285+
)
2286+
'
2287+
22732288
test_expect_success 'git -C <path> checkout uses the right repo' '
22742289
test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
22752290
branch-in-other Z

0 commit comments

Comments
 (0)