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 <alisonatwork@outlook.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alison Winters committed Nov 21, 2022 at 00:26 UTC 9de31f7bd25029eb6e2292ede6df3a646f261de4
2 files changed +26 -3
contrib/completion/git-completion.bash
+11 -3
@@ -722,6 +722,7 @@ __git_refs ()
722 local format refs
723 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
724 local match="${4-}"
725 + local umatch="${4-}"
726 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
727
728 __git_find_repo_path
@@ -745,12 +746,19 @@ __git_refs ()
746 fi
747 fi
748
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 +
755 if [ "$list_refs_from" = path ]; then
756 if [[ "$cur_" == ^* ]]; then
757 pfx="$pfx^"
758 fer_pfx="$fer_pfx^"
759 cur_=${cur_#^}
760 match=${match#^}
761 + umatch=${umatch#^}
762 fi
763 case "$cur_" in
764 refs|refs/*)
@@ -761,7 +769,7 @@ __git_refs ()
769 *)
770 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD; do
771 case "$i" in
764 - $match*)
772 + $match*|$umatch*)
773 if [ -e "$dir/$i" ]; then
774 echo "$pfx$i$sfx"
775 fi
@@ -795,7 +803,7 @@ __git_refs ()
803 *)
804 if [ "$list_refs_from" = remote ]; then
805 case "HEAD" in
798 - $match*) echo "${pfx}HEAD$sfx" ;;
806 + $match*|$umatch*) echo "${pfx}HEAD$sfx" ;;
807 esac
808 __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
809 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
@@ -804,7 +812,7 @@ __git_refs ()
812 else
813 local query_symref
814 case "HEAD" in
807 - $match*) query_symref="HEAD" ;;
815 + $match*|$umatch*) query_symref="HEAD" ;;
816 esac
817 __git ls-remote "$remote" $query_symref \
818 "refs/tags/$match*" "refs/heads/$match*" \
t/t9902-completion.sh
+15
@@ -2270,6 +2270,21 @@ test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGN
2270 )
2271 '
2272
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 +
2288 test_expect_success 'git -C <path> checkout uses the right repo' '
2289 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2290 branch-in-other Z