]> git.madduck.net Git - code/vcsh.git/blob - _vcsh_bash

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #215 from lyda/commit-fix
[code/vcsh.git] / _vcsh_bash
1 # bash completion for vcsh.
2
3 # run git command
4 #   based on bash_completion:_command_offset()
5 _vcsh_git_command () {
6         local word_offset=$1
7         for (( i=0; i < $word_offset; i++ )); do
8                 for (( j=0; j <= ${#COMP_LINE}; j++ )); do
9                         [[ "$COMP_LINE" == "${COMP_WORDS[i]}"* ]] && break
10                         COMP_LINE=${COMP_LINE:1}
11                         ((COMP_POINT--))
12                 done
13                 COMP_LINE=${COMP_LINE#"${COMP_WORDS[i]}"}
14                 ((COMP_POINT-=${#COMP_WORDS[i]}))
15         done
16         COMP_LINE="git $COMP_LINE"
17         ((COMP_POINT+=4))
18
19         # shift COMP_WORDS elements and adjust COMP_CWORD
20         for (( i=1; i <= COMP_CWORD - $word_offset + 1; i++ )); do
21                 COMP_WORDS[i]=${COMP_WORDS[i+$word_offset-1]}
22         done
23         for (( i; i <= COMP_CWORD; i++ )); do
24                 unset 'COMP_WORDS[i]'
25         done
26         COMP_WORDS[0]=git
27         ((COMP_CWORD -= $word_offset - 1))
28
29         local cspec=$( complete -p git 2>/dev/null )
30         if [[ -n $cspec ]]; then
31                 if [[ ${cspec#* -F } != $cspec ]]; then
32                         local func=${cspec#*-F }
33                         func=${func%% *}
34
35                         if [[ ${#COMP_WORDS[@]} -ge 2 ]]; then
36                                 $func git "${COMP_WORDS[${#COMP_WORDS[@]}-1]}" "${COMP_WORDS[${#COMP_WORDS[@]}-2]}"
37                         else
38                                 $func git "${COMP_WORDS[${#COMP_WORDS[@]}-1]}"
39                         fi
40
41                         # restore initial compopts
42                         local opt
43                         while [[ $cspec == *" -o "* ]]; do
44                                 # FIXME: should we take "+o opt" into account?
45                                 cspec=${cspec#*-o }
46                                 opt=${cspec%% *}
47                                 compopt -o $opt
48                                 cspec=${cspec#$opt}
49                         done
50                 fi
51         fi
52 }
53
54 _vcsh () {
55         local cur prev words cword OPTS
56         _init_completion -n = || return
57
58         local repos cmds
59         repos=( $(command vcsh list) )
60         cmds="clone delete enter foreach help init list list-tracked list-untracked
61                   pull push rename run status upgrade version which write-gitignore"
62
63         local subcword cmd subcmd
64         for (( subcword=1; subcword < ${#words[@]}-1; subcword++ )); do
65                 [[ -n $cmd && ${words[subcword]} != -* ]] && subcmd=${words[subcword]} && break
66                 [[ ${words[subcword]} != -* ]] && cmd=${words[subcword]}
67         done
68
69         if [[ -z $cmd ]]; then
70                 case $prev in
71                         -c)
72                                 COMPREPLY=( $(compgen -f -- $cur) )
73                                 return
74                                 ;;
75                 esac
76
77                 case $cur in
78                         -*)
79                                 OPTS='-c -d -h -v'
80                                 COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
81                                 return
82                                 ;;
83                 esac
84                 COMPREPLY=( $(compgen -W "${repos[*]} ${cmds[*]}" -- $cur) )
85                 return 0
86         fi
87
88         case $cmd in
89                 help|init|list|pull|push|version|which)
90                         return
91                         ;;
92
93                 list-untracked)
94                         [[ $cur == -* ]] && \
95                                 COMPREPLY=( $(compgen -W '-a -r' -- $cur) ) && return
96                         ;;&
97
98                 run)
99                         if [[ -n $subcmd && -n "${repos[$subcmd]}" ]]; then
100                                 _command_offset $(( $subcword+1 ))
101                                 return
102                         fi
103                         ;;&
104
105                 delete|enter|list-tracked|list-untracked|rename|run|status|upgrade|write-gitignore)
106                         # return repos
107                         if [[ -z $subcmd ]]; then
108                                 COMPREPLY=( $(compgen -W "${repos[*]}" -- $cur) )
109                                 return
110                         fi
111                         return
112                         ;;
113
114                 clone)
115                         [[ $cur == -* ]] && \
116                                 COMPREPLY=( $(compgen -W '-b' -- $cur) )
117                         return
118                         ;;
119
120                 foreach)
121                         [[ $cur == -* ]] \
122                                 && COMPREPLY=( $(compgen -W "-g" -- $cur) ) && return
123                         _vcsh_git_command $subcword
124                         return
125                         ;;
126
127         esac
128
129         # git command on repository
130         if [[ -n "${repos[$cmd]}" ]]; then
131                 _vcsh_git_command $subcword
132         fi
133         return 0
134 }
135
136 complete -F _vcsh vcsh
137
138 # vim: ft=sh: