Skip to content

Commit 9bab766

Browse files
alisonatworkgitster
authored andcommitted
completion: add optional ignore-case when matching refs
If GIT_COMPLETION_IGNORE_CASE is set, --ignore-case will be added to git for-each-ref calls so that refs can be matched case insensitively, even when running on case sensitive filesystems. Signed-off-by: Alison Winters <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b08839 commit 9bab766

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
#
5959
# When set to "1" suggest all options, including options which are
6060
# typically hidden (e.g. '--allow-empty' for 'git commit').
61+
#
62+
# GIT_COMPLETION_IGNORE_CASE
63+
#
64+
# When set, uses for-each-ref '--ignore-case' to find refs that match
65+
# case insensitively, even on systems with case sensitive file systems
66+
# (e.g., completing tag name "FOO" on "git checkout f<TAB>").
6167

6268
case "$COMP_WORDBREAKS" in
6369
*:*) : great ;;
@@ -646,6 +652,7 @@ __git_heads ()
646652
local pfx="${1-}" cur_="${2-}" sfx="${3-}"
647653

648654
__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
655+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
649656
"refs/heads/$cur_*" "refs/heads/$cur_*/**"
650657
}
651658

@@ -659,6 +666,7 @@ __git_remote_heads ()
659666
local pfx="${1-}" cur_="${2-}" sfx="${3-}"
660667

661668
__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
669+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
662670
"refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
663671
}
664672

@@ -669,6 +677,7 @@ __git_tags ()
669677
local pfx="${1-}" cur_="${2-}" sfx="${3-}"
670678

671679
__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
680+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
672681
"refs/tags/$cur_*" "refs/tags/$cur_*/**"
673682
}
674683

@@ -688,6 +697,7 @@ __git_dwim_remote_heads ()
688697
# but only output if the branch name is unique
689698
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
690699
--sort="refname:strip=3" \
700+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
691701
"refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
692702
uniq -u
693703
}
@@ -765,6 +775,7 @@ __git_refs ()
765775
;;
766776
esac
767777
__git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
778+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
768779
"${refs[@]}"
769780
if [ -n "$track" ]; then
770781
__git_dwim_remote_heads "$pfx" "$match" "$sfx"
@@ -787,6 +798,7 @@ __git_refs ()
787798
$match*) echo "${pfx}HEAD$sfx" ;;
788799
esac
789800
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
801+
${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
790802
"refs/remotes/$remote/$match*" \
791803
"refs/remotes/$remote/$match*/**"
792804
else

t/t9902-completion.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,21 @@ test_expect_success 'checkout completes ref names' '
22552255
EOF
22562256
'
22572257

2258+
test_expect_success 'checkout does not match ref names of a different case' '
2259+
test_completion "git checkout M" ""
2260+
'
2261+
2262+
test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2263+
(
2264+
GIT_COMPLETION_IGNORE_CASE=1 &&
2265+
test_completion "git checkout M" <<-\EOF
2266+
main Z
2267+
mybranch Z
2268+
mytag Z
2269+
EOF
2270+
)
2271+
'
2272+
22582273
test_expect_success 'git -C <path> checkout uses the right repo' '
22592274
test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
22602275
branch-in-other Z

0 commit comments

Comments
 (0)