Commit | Line | Data |
---|---|---|
8ebae56b KH |
1 | # bash completion support for StGIT -*- shell-script -*- |
2 | # | |
3 | # Copyright (C) 2006, Karl Hasselström <kha@treskal.com> | |
4 | # Based on git-completion.sh | |
5 | # | |
6 | # To use these routines: | |
7 | # | |
8 | # 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash). | |
9 | # | |
10 | # 2. Add the following line to your .bashrc: | |
11 | # . ~/.stgit-completion.bash | |
12 | ||
1aaf55c9 CM |
13 | _stg_commands=" |
14 | add | |
15 | applied | |
16 | assimilate | |
17 | branch | |
18 | delete | |
19 | diff | |
20 | clean | |
21 | clone | |
22 | commit | |
d5ae2173 | 23 | cp |
1aaf55c9 CM |
24 | export |
25 | files | |
26 | float | |
27 | fold | |
28 | goto | |
841c7b2a | 29 | hide |
1aaf55c9 CM |
30 | id |
31 | import | |
32 | init | |
33 | log | |
34 | ||
35 | new | |
36 | patches | |
37 | pick | |
38 | pop | |
39 | pull | |
40 | push | |
22037590 | 41 | rebase |
1aaf55c9 CM |
42 | refresh |
43 | rename | |
44 | resolved | |
45 | rm | |
46 | series | |
47 | show | |
6f1c5e3c | 48 | sink |
1aaf55c9 | 49 | status |
06848fab | 50 | sync |
1aaf55c9 CM |
51 | top |
52 | unapplied | |
53 | uncommit | |
841c7b2a | 54 | unhide |
1aaf55c9 CM |
55 | " |
56 | ||
8ebae56b KH |
57 | # The path to .git, or empty if we're not in a repository. |
58 | _gitdir () | |
59 | { | |
60 | echo "$(git rev-parse --git-dir 2>/dev/null)" | |
61 | } | |
62 | ||
63 | # Name of the current branch, or empty if there isn't one. | |
64 | _current_branch () | |
65 | { | |
66 | local b=$(git symbolic-ref HEAD 2>/dev/null) | |
67 | echo ${b#refs/heads/} | |
68 | } | |
69 | ||
70 | # List of all applied patches. | |
71 | _applied_patches () | |
72 | { | |
73 | local g=$(_gitdir) | |
74 | [ "$g" ] && cat "$g/patches/$(_current_branch)/applied" | |
75 | } | |
76 | ||
77 | # List of all unapplied patches. | |
78 | _unapplied_patches () | |
79 | { | |
80 | local g=$(_gitdir) | |
81 | [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied" | |
82 | } | |
83 | ||
84 | # List of all patches. | |
85 | _all_patches () | |
86 | { | |
87 | local b=$(_current_branch) | |
88 | local g=$(_gitdir) | |
89 | [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" | |
90 | } | |
91 | ||
92 | # List of all patches except the current patch. | |
93 | _all_other_patches () | |
94 | { | |
95 | local b=$(_current_branch) | |
96 | local g=$(_gitdir) | |
97 | [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \ | |
7ae2e706 | 98 | | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$" |
8ebae56b KH |
99 | } |
100 | ||
450ae43a YD |
101 | _all_branches () |
102 | { | |
103 | local g=$(_gitdir) | |
104 | [ "$g" ] && (cd .git/patches/ && echo *) | |
105 | } | |
106 | ||
1aaf55c9 CM |
107 | # List the command options |
108 | _cmd_options () | |
109 | { | |
21b1cc55 | 110 | stg $1 --help 2>/dev/null | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/" |
1aaf55c9 CM |
111 | } |
112 | ||
8ebae56b KH |
113 | # Generate completions for patches and patch ranges from the given |
114 | # patch list function, and options from the given list. | |
115 | _complete_patch_range () | |
116 | { | |
117 | local patchlist="$1" options="$2" | |
118 | local pfx cur="${COMP_WORDS[COMP_CWORD]}" | |
119 | case "$cur" in | |
120 | *..*) | |
121 | pfx="${cur%..*}.." | |
122 | cur="${cur#*..}" | |
123 | COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur")) | |
124 | ;; | |
125 | *) | |
126 | COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur")) | |
127 | ;; | |
128 | esac | |
129 | } | |
130 | ||
ebbd6f00 CM |
131 | _complete_patch_range_options () |
132 | { | |
133 | local patchlist="$1" options="$2" patch_options="$3" | |
134 | local prev="${COMP_WORDS[COMP_CWORD-1]}" | |
135 | local cur="${COMP_WORDS[COMP_CWORD]}" | |
136 | local popt | |
137 | for popt in $patch_options; do | |
138 | if [ $prev == $popt ]; then | |
139 | _complete_patch_range $patchlist | |
140 | return | |
141 | fi | |
142 | done | |
143 | COMPREPLY=($(compgen -W "$options" -- "$cur")) | |
144 | } | |
145 | ||
450ae43a YD |
146 | _complete_branch () |
147 | { | |
148 | COMPREPLY=($(compgen -W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}")) | |
149 | } | |
150 | ||
8ebae56b KH |
151 | # Generate completions for options from the given list. |
152 | _complete_options () | |
153 | { | |
154 | local options="$1" | |
155 | COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}")) | |
156 | } | |
157 | ||
1aaf55c9 | 158 | _stg_common () |
8ebae56b | 159 | { |
1aaf55c9 | 160 | _complete_options "$(_cmd_options $1)" |
8ebae56b KH |
161 | } |
162 | ||
ebbd6f00 | 163 | _stg_patches () |
8ebae56b | 164 | { |
ebbd6f00 | 165 | _complete_patch_range "$2" "$(_cmd_options $1)" |
8ebae56b KH |
166 | } |
167 | ||
ebbd6f00 | 168 | _stg_patches_options () |
8ebae56b | 169 | { |
ebbd6f00 | 170 | _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3" |
8ebae56b KH |
171 | } |
172 | ||
1aaf55c9 | 173 | _stg_help () |
8ebae56b | 174 | { |
1aaf55c9 | 175 | _complete_options "$_stg_commands" |
8ebae56b KH |
176 | } |
177 | ||
178 | _stg () | |
179 | { | |
180 | local i c=1 command | |
181 | ||
182 | while [ $c -lt $COMP_CWORD ]; do | |
183 | if [ $c == 1 ]; then | |
184 | command="${COMP_WORDS[c]}" | |
185 | fi | |
186 | c=$((++c)) | |
187 | done | |
188 | ||
189 | # Complete name of subcommand. | |
190 | if [ $c -eq $COMP_CWORD -a -z "$command" ]; then | |
191 | COMPREPLY=($(compgen \ | |
1aaf55c9 | 192 | -W "--help --version copyright help $_stg_commands" \ |
8ebae56b KH |
193 | -- "${COMP_WORDS[COMP_CWORD]}")) |
194 | return; | |
195 | fi | |
196 | ||
197 | # Complete arguments to subcommands. | |
198 | case "$command" in | |
1aaf55c9 CM |
199 | # generic commands |
200 | help) _stg_help ;; | |
201 | # repository commands | |
ebbd6f00 | 202 | id) _stg_patches $command _all_patches ;; |
1aaf55c9 | 203 | # stack commands |
ebbd6f00 CM |
204 | float) _stg_patches $command _all_patches ;; |
205 | goto) _stg_patches $command _all_other_patches ;; | |
841c7b2a | 206 | hide) _stg_patches $command _all_patches ;; |
ebbd6f00 CM |
207 | pop) _stg_patches $command _applied_patches ;; |
208 | push) _stg_patches $command _unapplied_patches ;; | |
0aebdb85 | 209 | series) _stg_patches $command _all_patches ;; |
6f1c5e3c | 210 | sink) _stg_patches $command _all_patches ;; |
841c7b2a | 211 | unhide) _stg_patches $command _all_patches ;; |
1aaf55c9 | 212 | # patch commands |
ebbd6f00 | 213 | delete) _stg_patches $command _all_patches ;; |
8560c67b | 214 | export) _stg_patches $command _applied_patches ;; |
ebbd6f00 CM |
215 | files) _stg_patches $command _all_patches ;; |
216 | log) _stg_patches $command _all_patches ;; | |
b4f656f0 | 217 | mail) _stg_patches $command _all_patches ;; |
ebbd6f00 CM |
218 | pick) _stg_patches $command _unapplied_patches ;; |
219 | refresh)_stg_patches_options $command _applied_patches "-p --patch" ;; | |
220 | rename) _stg_patches $command _all_patches ;; | |
221 | show) _stg_patches $command _all_patches ;; | |
06848fab | 222 | sync) _stg_patches $command _applied_patches ;; |
ebbd6f00 CM |
223 | # working-copy commands |
224 | diff) _stg_patches_options $command _applied_patches "-r --range" ;; | |
450ae43a YD |
225 | # commands that usually raher accept branches |
226 | branch) _complete_branch $command _all_branches ;; | |
227 | rebase) _complete_branch $command _all_branches ;; | |
1aaf55c9 CM |
228 | # all the other commands |
229 | *) _stg_common $command ;; | |
8ebae56b KH |
230 | esac |
231 | } | |
232 | ||
233 | complete -o default -F _stg stg |