@matheusbsbs / matheus-bagliano / commits / 9244d69b96
Fork 0

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 <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Apr 28, 2011 at 18:01 UTC 9244d69b96c209116390804c9af6bb1f5271ae18
1 file changed +50 -57
contrib/completion/git-completion.bash
+50 -57
@@ -491,10 +491,12 @@ __gitcomp ()
491 {
492 local cur
493 _get_comp_words_by_ref -n =: cur
494 + local cur_="$cur"
495 +
496 if [ $# -gt 2 ]; then
495 - cur="$3"
497 + cur_="$3"
498 fi
497 - case "$cur" in
499 + case "$cur_" in
500 --*=)
501 COMPREPLY=()
502 ;;
@@ -502,7 +504,7 @@ __gitcomp ()
504 local IFS=$'\n'
505 COMPREPLY=($(compgen -P "${2-}" \
506 -W "$(__gitcomp_1 "${1-}" "${4-}")" \
505 - -- "$cur"))
507 + -- "$cur_"))
508 ;;
509 esac
510 }
@@ -668,17 +670,18 @@ __git_complete_revlist_file ()
670 {
671 local pfx ls ref cur
672 _get_comp_words_by_ref -n =: cur
671 - case "$cur" in
673 + local cur_="$cur"
674 + case "$cur_" in
675 *..?*:*)
676 return
677 ;;
678 ?*:*)
676 - ref="${cur%%:*}"
677 - cur="${cur#*:}"
678 - case "$cur" in
679 + ref="${cur_%%:*}"
680 + cur_="${cur_#*:}"
681 + case "$cur_" in
682 ?*/*)
680 - pfx="${cur%/*}"
681 - cur="${cur##*/}"
683 + pfx="${cur_%/*}"
684 + cur_="${cur_##*/}"
685 ls="$ref:$pfx"
686 pfx="$pfx/"
687 ;;
@@ -708,17 +711,17 @@ __git_complete_revlist_file ()
711 s,$,/,
712 }
713 s/^.* //')" \
711 - -- "$cur"))
714 + -- "$cur_"))
715 ;;
716 *...*)
714 - pfx="${cur%...*}..."
715 - cur="${cur#*...}"
716 - __gitcomp "$(__git_refs)" "$pfx" "$cur"
717 + pfx="${cur_%...*}..."
718 + cur_="${cur_#*...}"
719 + __gitcomp "$(__git_refs)" "$pfx" "$cur_"
720 ;;
721 *..*)
719 - pfx="${cur%..*}.."
720 - cur="${cur#*..}"
721 - __gitcomp "$(__git_refs)" "$pfx" "$cur"
722 + pfx="${cur_%..*}.."
723 + cur_="${cur_#*..}"
724 + __gitcomp "$(__git_refs)" "$pfx" "$cur_"
725 ;;
726 *)
727 __gitcomp "$(__git_refs)"
@@ -741,7 +744,7 @@ __git_complete_remote_or_refspec ()
744 {
745 local cur words cword
746 _get_comp_words_by_ref -n =: cur words cword
744 - local cmd="${words[1]}"
747 + local cur_="$cur" cmd="${words[1]}"
748 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
749 while [ $c -lt $cword ]; do
750 i="${words[c]}"
@@ -771,40 +774,40 @@ __git_complete_remote_or_refspec ()
774 return
775 fi
776 [ "$remote" = "." ] && remote=
774 - case "$cur" in
777 + case "$cur_" in
778 *:*)
779 case "$COMP_WORDBREAKS" in
780 *:*) : great ;;
778 - *) pfx="${cur%%:*}:" ;;
781 + *) pfx="${cur_%%:*}:" ;;
782 esac
780 - cur="${cur#*:}"
783 + cur_="${cur_#*:}"
784 lhs=0
785 ;;
786 +*)
787 pfx="+"
785 - cur="${cur#+}"
788 + cur_="${cur_#+}"
789 ;;
790 esac
791 case "$cmd" in
792 fetch)
793 if [ $lhs = 1 ]; then
791 - __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
794 + __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur_"
795 else
793 - __gitcomp "$(__git_refs)" "$pfx" "$cur"
796 + __gitcomp "$(__git_refs)" "$pfx" "$cur_"
797 fi
798 ;;
799 pull)
800 if [ $lhs = 1 ]; then
798 - __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
801 + __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur_"
802 else
800 - __gitcomp "$(__git_refs)" "$pfx" "$cur"
803 + __gitcomp "$(__git_refs)" "$pfx" "$cur_"
804 fi
805 ;;
806 push)
807 if [ $lhs = 1 ]; then
805 - __gitcomp "$(__git_refs)" "$pfx" "$cur"
808 + __gitcomp "$(__git_refs)" "$pfx" "$cur_"
809 else
807 - __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
810 + __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur_"
811 fi
812 ;;
813 esac
@@ -2012,70 +2015,60 @@ _git_config ()
2015 return
2016 ;;
2017 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_"
2020 return
2021 ;;
2022 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_" "."
2025 return
2026 ;;
2027 guitool.*.*)
2027 - local pfx="${cur%.*}."
2028 - cur="${cur##*.}"
2028 + local pfx="${cur%.*}." cur_="${cur##*.}"
2029 __gitcomp "
2030 argprompt cmd confirm needsfile noconsole norescan
2031 prompt revprompt revunmerged title
2032 - " "$pfx" "$cur"
2032 + " "$pfx" "$cur_"
2033 return
2034 ;;
2035 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_"
2038 return
2039 ;;
2040 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_"
2043 return
2044 ;;
2045 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_"
2048 return
2049 ;;
2050 pager.*)
2054 - local pfx="${cur%.*}."
2055 - cur="${cur#*.}"
2051 + local pfx="${cur%.*}." cur_="${cur#*.}"
2052 __git_compute_all_commands
2057 - __gitcomp "$__git_all_commands" "$pfx" "$cur"
2053 + __gitcomp "$__git_all_commands" "$pfx" "$cur_"
2054 return
2055 ;;
2056 remote.*.*)
2061 - local pfx="${cur%.*}."
2062 - cur="${cur##*.}"
2057 + local pfx="${cur%.*}." cur_="${cur##*.}"
2058 __gitcomp "
2059 url proxy fetch push mirror skipDefaultUpdate
2060 receivepack uploadpack tagopt pushurl
2066 - " "$pfx" "$cur"
2061 + " "$pfx" "$cur_"
2062 return
2063 ;;
2064 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_" "."
2067 return
2068 ;;
2069 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_"
2072 return
2073 ;;
2074 esac