Skip to content

Commit 9244d69

Browse files
szedergitster
authored andcommitted
completion: don't modify the $cur variable in completion functions
Since v1.7.4-rc0~11^2~2 (bash: get --pretty=m<tab> completion to work with bash v4, 2010-12-02) we use _get_comp_words_by_ref() to access completion-related variables, and the $cur variable holds the word containing the current cursor position in all completion functions. This $cur variable is left unchanged in most completion functions; there are only four functions modifying its value, namely __gitcomp(), __git_complete_revlist_file(), __git_complete_remote_or_refspec(), and _git_config(). If this variable were never modified, then it would allow us a nice optimisation and cleanup. Therefore, this patch assigns $cur to an other local variable and uses that for later modifications in those four functions. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e839fe6 commit 9244d69

File tree

1 file changed

+50
-57
lines changed

1 file changed

+50
-57
lines changed

contrib/completion/git-completion.bash

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -491,18 +491,20 @@ __gitcomp ()
491491
{
492492
local cur
493493
_get_comp_words_by_ref -n =: cur
494+
local cur_="$cur"
495+
494496
if [ $# -gt 2 ]; then
495-
cur="$3"
497+
cur_="$3"
496498
fi
497-
case "$cur" in
499+
case "$cur_" in
498500
--*=)
499501
COMPREPLY=()
500502
;;
501503
*)
502504
local IFS=$'\n'
503505
COMPREPLY=($(compgen -P "${2-}" \
504506
-W "$(__gitcomp_1 "${1-}" "${4-}")" \
505-
-- "$cur"))
507+
-- "$cur_"))
506508
;;
507509
esac
508510
}
@@ -668,17 +670,18 @@ __git_complete_revlist_file ()
668670
{
669671
local pfx ls ref cur
670672
_get_comp_words_by_ref -n =: cur
671-
case "$cur" in
673+
local cur_="$cur"
674+
case "$cur_" in
672675
*..?*:*)
673676
return
674677
;;
675678
?*:*)
676-
ref="${cur%%:*}"
677-
cur="${cur#*:}"
678-
case "$cur" in
679+
ref="${cur_%%:*}"
680+
cur_="${cur_#*:}"
681+
case "$cur_" in
679682
?*/*)
680-
pfx="${cur%/*}"
681-
cur="${cur##*/}"
683+
pfx="${cur_%/*}"
684+
cur_="${cur_##*/}"
682685
ls="$ref:$pfx"
683686
pfx="$pfx/"
684687
;;
@@ -708,17 +711,17 @@ __git_complete_revlist_file ()
708711
s,$,/,
709712
}
710713
s/^.* //')" \
711-
-- "$cur"))
714+
-- "$cur_"))
712715
;;
713716
*...*)
714-
pfx="${cur%...*}..."
715-
cur="${cur#*...}"
716-
__gitcomp "$(__git_refs)" "$pfx" "$cur"
717+
pfx="${cur_%...*}..."
718+
cur_="${cur_#*...}"
719+
__gitcomp "$(__git_refs)" "$pfx" "$cur_"
717720
;;
718721
*..*)
719-
pfx="${cur%..*}.."
720-
cur="${cur#*..}"
721-
__gitcomp "$(__git_refs)" "$pfx" "$cur"
722+
pfx="${cur_%..*}.."
723+
cur_="${cur_#*..}"
724+
__gitcomp "$(__git_refs)" "$pfx" "$cur_"
722725
;;
723726
*)
724727
__gitcomp "$(__git_refs)"
@@ -741,7 +744,7 @@ __git_complete_remote_or_refspec ()
741744
{
742745
local cur words cword
743746
_get_comp_words_by_ref -n =: cur words cword
744-
local cmd="${words[1]}"
747+
local cur_="$cur" cmd="${words[1]}"
745748
local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
746749
while [ $c -lt $cword ]; do
747750
i="${words[c]}"
@@ -771,40 +774,40 @@ __git_complete_remote_or_refspec ()
771774
return
772775
fi
773776
[ "$remote" = "." ] && remote=
774-
case "$cur" in
777+
case "$cur_" in
775778
*:*)
776779
case "$COMP_WORDBREAKS" in
777780
*:*) : great ;;
778-
*) pfx="${cur%%:*}:" ;;
781+
*) pfx="${cur_%%:*}:" ;;
779782
esac
780-
cur="${cur#*:}"
783+
cur_="${cur_#*:}"
781784
lhs=0
782785
;;
783786
+*)
784787
pfx="+"
785-
cur="${cur#+}"
788+
cur_="${cur_#+}"
786789
;;
787790
esac
788791
case "$cmd" in
789792
fetch)
790793
if [ $lhs = 1 ]; then
791-
__gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
794+
__gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur_"
792795
else
793-
__gitcomp "$(__git_refs)" "$pfx" "$cur"
796+
__gitcomp "$(__git_refs)" "$pfx" "$cur_"
794797
fi
795798
;;
796799
pull)
797800
if [ $lhs = 1 ]; then
798-
__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
801+
__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur_"
799802
else
800-
__gitcomp "$(__git_refs)" "$pfx" "$cur"
803+
__gitcomp "$(__git_refs)" "$pfx" "$cur_"
801804
fi
802805
;;
803806
push)
804807
if [ $lhs = 1 ]; then
805-
__gitcomp "$(__git_refs)" "$pfx" "$cur"
808+
__gitcomp "$(__git_refs)" "$pfx" "$cur_"
806809
else
807-
__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
810+
__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur_"
808811
fi
809812
;;
810813
esac
@@ -2012,70 +2015,60 @@ _git_config ()
20122015
return
20132016
;;
20142017
branch.*.*)
2015-
local pfx="${cur%.*}."
2016-
cur="${cur##*.}"
2017-
__gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur"
2018+
local pfx="${cur%.*}." cur_="${cur##*.}"
2019+
__gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur_"
20182020
return
20192021
;;
20202022
branch.*)
2021-
local pfx="${cur%.*}."
2022-
cur="${cur#*.}"
2023-
__gitcomp "$(__git_heads)" "$pfx" "$cur" "."
2023+
local pfx="${cur%.*}." cur_="${cur#*.}"
2024+
__gitcomp "$(__git_heads)" "$pfx" "$cur_" "."
20242025
return
20252026
;;
20262027
guitool.*.*)
2027-
local pfx="${cur%.*}."
2028-
cur="${cur##*.}"
2028+
local pfx="${cur%.*}." cur_="${cur##*.}"
20292029
__gitcomp "
20302030
argprompt cmd confirm needsfile noconsole norescan
20312031
prompt revprompt revunmerged title
2032-
" "$pfx" "$cur"
2032+
" "$pfx" "$cur_"
20332033
return
20342034
;;
20352035
difftool.*.*)
2036-
local pfx="${cur%.*}."
2037-
cur="${cur##*.}"
2038-
__gitcomp "cmd path" "$pfx" "$cur"
2036+
local pfx="${cur%.*}." cur_="${cur##*.}"
2037+
__gitcomp "cmd path" "$pfx" "$cur_"
20392038
return
20402039
;;
20412040
man.*.*)
2042-
local pfx="${cur%.*}."
2043-
cur="${cur##*.}"
2044-
__gitcomp "cmd path" "$pfx" "$cur"
2041+
local pfx="${cur%.*}." cur_="${cur##*.}"
2042+
__gitcomp "cmd path" "$pfx" "$cur_"
20452043
return
20462044
;;
20472045
mergetool.*.*)
2048-
local pfx="${cur%.*}."
2049-
cur="${cur##*.}"
2050-
__gitcomp "cmd path trustExitCode" "$pfx" "$cur"
2046+
local pfx="${cur%.*}." cur_="${cur##*.}"
2047+
__gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
20512048
return
20522049
;;
20532050
pager.*)
2054-
local pfx="${cur%.*}."
2055-
cur="${cur#*.}"
2051+
local pfx="${cur%.*}." cur_="${cur#*.}"
20562052
__git_compute_all_commands
2057-
__gitcomp "$__git_all_commands" "$pfx" "$cur"
2053+
__gitcomp "$__git_all_commands" "$pfx" "$cur_"
20582054
return
20592055
;;
20602056
remote.*.*)
2061-
local pfx="${cur%.*}."
2062-
cur="${cur##*.}"
2057+
local pfx="${cur%.*}." cur_="${cur##*.}"
20632058
__gitcomp "
20642059
url proxy fetch push mirror skipDefaultUpdate
20652060
receivepack uploadpack tagopt pushurl
2066-
" "$pfx" "$cur"
2061+
" "$pfx" "$cur_"
20672062
return
20682063
;;
20692064
remote.*)
2070-
local pfx="${cur%.*}."
2071-
cur="${cur#*.}"
2072-
__gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
2065+
local pfx="${cur%.*}." cur_="${cur#*.}"
2066+
__gitcomp "$(__git_remotes)" "$pfx" "$cur_" "."
20732067
return
20742068
;;
20752069
url.*.*)
2076-
local pfx="${cur%.*}."
2077-
cur="${cur##*.}"
2078-
__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur"
2070+
local pfx="${cur%.*}." cur_="${cur##*.}"
2071+
__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
20792072
return
20802073
;;
20812074
esac

0 commit comments

Comments
 (0)