completion: add new __git_complete helper
This simplifies the completions, and would make it easier to define aliases in the future. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Felipe Contreras committed
May 14, 2012 at 17:35 UTC
6b179adfe93d13c2f993668cd12eec52860486ac
2 files changed
+34
-38
contrib/completion/git-completion.bash
+33
-37
@@ -2603,21 +2603,6 @@ _git ()
2603
{
2604
local i c=1 command __git_dir
2605
2606
- if [[ -n ${ZSH_VERSION-} ]]; then
2607
- emulate -L bash
2608
- setopt KSH_TYPESET
2609
-
2610
- # workaround zsh's bug that leaves 'words' as a special
2611
- # variable in versions < 4.3.12
2612
- typeset -h words
2613
-
2614
- # workaround zsh's bug that quotes spaces in the COMPREPLY
2615
- # array if IFS doesn't contain spaces.
2616
- typeset -h IFS
2617
- fi
2618
-
2619
- local cur words cword prev
2620
- _get_comp_words_by_ref -n =: cur words cword prev
2606
while [ $c -lt $cword ]; do
2607
i="${words[c]}"
2608
case "$i" in
@@ -2667,22 +2652,6 @@ _git ()
2652
2653
_gitk ()
2654
{
2670
- if [[ -n ${ZSH_VERSION-} ]]; then
2671
- emulate -L bash
2672
- setopt KSH_TYPESET
2673
-
2674
- # workaround zsh's bug that leaves 'words' as a special
2675
- # variable in versions < 4.3.12
2676
- typeset -h words
2677
-
2678
- # workaround zsh's bug that quotes spaces in the COMPREPLY
2679
- # array if IFS doesn't contain spaces.
2680
- typeset -h IFS
2681
- fi
2682
-
2683
- local cur words cword prev
2684
- _get_comp_words_by_ref -n =: cur words cword prev
2685
-
2655
__git_has_doubledash && return
2656
2657
local g="$(__gitdir)"
@@ -2703,16 +2672,43 @@ _gitk ()
2672
__git_complete_revlist
2673
}
2674
2706
-complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
2707
- || complete -o default -o nospace -F _git git
2708
-complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
2709
- || complete -o default -o nospace -F _gitk gitk
2675
+__git_func_wrap ()
2676
+{
2677
+ if [[ -n ${ZSH_VERSION-} ]]; then
2678
+ emulate -L bash
2679
+ setopt KSH_TYPESET
2680
+
2681
+ # workaround zsh's bug that leaves 'words' as a special
2682
+ # variable in versions < 4.3.12
2683
+ typeset -h words
2684
+
2685
+ # workaround zsh's bug that quotes spaces in the COMPREPLY
2686
+ # array if IFS doesn't contain spaces.
2687
+ typeset -h IFS
2688
+ fi
2689
+ local cur words cword prev
2690
+ _get_comp_words_by_ref -n =: cur words cword prev
2691
+ $1
2692
+}
2693
+
2694
+# Setup completion for certain functions defined above by setting common
2695
+# variables and workarounds.
2696
+# This is NOT a public function; use at your own risk.
2697
+__git_complete ()
2698
+{
2699
+ local wrapper="__git_wrap${2}"
2700
+ eval "$wrapper () { __git_func_wrap $2 ; }"
2701
+ complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
2702
+ || complete -o default -o nospace -F $wrapper $1
2703
+}
2704
+
2705
+__git_complete git _git
2706
+__git_complete gitk _gitk
2707
2708
# The following are necessary only for Cygwin, and only are needed
2709
# when the user has tab-completed the executable name and consequently
2710
# included the '.exe' suffix.
2711
#
2712
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2716
-complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
2717
- || complete -o default -o nospace -F _git git.exe
2713
+__git_complete git.exe _git
2714
fi
t/t9902-completion.sh
+1
-1
@@ -63,7 +63,7 @@ run_completion ()
63
local _cword
64
_words=( $1 )
65
(( _cword = ${#_words[@]} - 1 ))
66
- _git && print_comp
66
+ __git_wrap_git && print_comp
67
}
68
69
test_completion ()