##// END OF EJS Templates
bash_completion: don't complete export with "garbage" when mq is not around...
Alexis S. L. Carvalho -
r3480:03932425 default
parent child Browse files
Show More
@@ -1,390 +1,395 b''
1 # bash completion for the Mercurial distributed SCM
1 # bash completion for the Mercurial distributed SCM
2
2
3 # Docs:
3 # Docs:
4 #
4 #
5 # If you source this file from your .bashrc, bash should be able to
5 # If you source this file from your .bashrc, bash should be able to
6 # complete a command line that uses hg with all the available commands
6 # complete a command line that uses hg with all the available commands
7 # and options and sometimes even arguments.
7 # and options and sometimes even arguments.
8 #
8 #
9 # Mercurial allows you to define additional commands through extensions.
9 # Mercurial allows you to define additional commands through extensions.
10 # Bash should be able to automatically figure out the name of these new
10 # Bash should be able to automatically figure out the name of these new
11 # commands and their options. If you also want to tell it how to
11 # commands and their options. If you also want to tell it how to
12 # complete non-option arguments, see below for how to define an
12 # complete non-option arguments, see below for how to define an
13 # _hg_cmd_foo function.
13 # _hg_cmd_foo function.
14 #
14 #
15 #
15 #
16 # Notes about completion for specific commands:
16 # Notes about completion for specific commands:
17 #
17 #
18 # - the completion function for the email command from the patchbomb
18 # - the completion function for the email command from the patchbomb
19 # extension will try to call _hg_emails to get a list of e-mail
19 # extension will try to call _hg_emails to get a list of e-mail
20 # addresses. It's up to the user to define this function. For
20 # addresses. It's up to the user to define this function. For
21 # example, put the addresses of the lists that you usually patchbomb
21 # example, put the addresses of the lists that you usually patchbomb
22 # in ~/.patchbomb-to and the addresses that you usually use to send
22 # in ~/.patchbomb-to and the addresses that you usually use to send
23 # the patchbombs in ~/.patchbomb-from and use something like this:
23 # the patchbombs in ~/.patchbomb-from and use something like this:
24 #
24 #
25 # _hg_emails()
25 # _hg_emails()
26 # {
26 # {
27 # if [ -r ~/.patchbomb-$1 ]; then
27 # if [ -r ~/.patchbomb-$1 ]; then
28 # cat ~/.patchbomb-$1
28 # cat ~/.patchbomb-$1
29 # fi
29 # fi
30 # }
30 # }
31 #
31 #
32 #
32 #
33 # Writing completion functions for additional commands:
33 # Writing completion functions for additional commands:
34 #
34 #
35 # If it exists, the function _hg_cmd_foo will be called without
35 # If it exists, the function _hg_cmd_foo will be called without
36 # arguments to generate the completion candidates for the hg command
36 # arguments to generate the completion candidates for the hg command
37 # "foo".
37 # "foo".
38 #
38 #
39 # In addition to the regular completion variables provided by bash,
39 # In addition to the regular completion variables provided by bash,
40 # the following variables are also set:
40 # the following variables are also set:
41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
42 # - $cmd - the name of the hg command being completed
42 # - $cmd - the name of the hg command being completed
43 # - $cmd_index - the index of $cmd in $COMP_WORDS
43 # - $cmd_index - the index of $cmd in $COMP_WORDS
44 # - $cur - the current argument being completed
44 # - $cur - the current argument being completed
45 # - $prev - the argument before $cur
45 # - $prev - the argument before $cur
46 # - $global_args - "|"-separated list of global options that accept
46 # - $global_args - "|"-separated list of global options that accept
47 # an argument (e.g. '--cwd|-R|--repository')
47 # an argument (e.g. '--cwd|-R|--repository')
48 # - $canonical - 1 if we canonicalized $cmd before calling the function
48 # - $canonical - 1 if we canonicalized $cmd before calling the function
49 # 0 otherwise
49 # 0 otherwise
50 #
50 #
51
51
52 shopt -s extglob
52 shopt -s extglob
53
53
54 _hg_commands()
54 _hg_commands()
55 {
55 {
56 local commands
56 local commands
57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
59 }
59 }
60
60
61 _hg_paths()
61 _hg_paths()
62 {
62 {
63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
65 }
65 }
66
66
67 _hg_repos()
67 _hg_repos()
68 {
68 {
69 local i
69 local i
70 for i in $(compgen -d -- "$cur"); do
70 for i in $(compgen -d -- "$cur"); do
71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
72 done
72 done
73 }
73 }
74
74
75 _hg_status()
75 _hg_status()
76 {
76 {
77 local files="$("$hg" status -n$1 . 2>/dev/null)"
77 local files="$("$hg" status -n$1 . 2>/dev/null)"
78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
79 }
79 }
80
80
81 _hg_tags()
81 _hg_tags()
82 {
82 {
83 local tags="$("$hg" tags -q 2>/dev/null)"
83 local tags="$("$hg" tags -q 2>/dev/null)"
84 local IFS=$'\n'
84 local IFS=$'\n'
85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
86 }
86 }
87
87
88 # this is "kind of" ugly...
88 # this is "kind of" ugly...
89 _hg_count_non_option()
89 _hg_count_non_option()
90 {
90 {
91 local i count=0
91 local i count=0
92 local filters="$1"
92 local filters="$1"
93
93
94 for ((i=1; $i<=$COMP_CWORD; i++)); do
94 for ((i=1; $i<=$COMP_CWORD; i++)); do
95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
97 continue
97 continue
98 fi
98 fi
99 count=$(($count + 1))
99 count=$(($count + 1))
100 fi
100 fi
101 done
101 done
102
102
103 echo $(($count - 1))
103 echo $(($count - 1))
104 }
104 }
105
105
106 _hg()
106 _hg()
107 {
107 {
108 local cur prev cmd cmd_index opts i
108 local cur prev cmd cmd_index opts i
109 # global options that receive an argument
109 # global options that receive an argument
110 local global_args='--cwd|-R|--repository'
110 local global_args='--cwd|-R|--repository'
111 local hg="$1"
111 local hg="$1"
112
112
113 COMPREPLY=()
113 COMPREPLY=()
114 cur="$2"
114 cur="$2"
115 prev="$3"
115 prev="$3"
116
116
117 # searching for the command
117 # searching for the command
118 # (first non-option argument that doesn't follow a global option that
118 # (first non-option argument that doesn't follow a global option that
119 # receives an argument)
119 # receives an argument)
120 for ((i=1; $i<=$COMP_CWORD; i++)); do
120 for ((i=1; $i<=$COMP_CWORD; i++)); do
121 if [[ ${COMP_WORDS[i]} != -* ]]; then
121 if [[ ${COMP_WORDS[i]} != -* ]]; then
122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
123 cmd="${COMP_WORDS[i]}"
123 cmd="${COMP_WORDS[i]}"
124 cmd_index=$i
124 cmd_index=$i
125 break
125 break
126 fi
126 fi
127 fi
127 fi
128 done
128 done
129
129
130 if [[ "$cur" == -* ]]; then
130 if [[ "$cur" == -* ]]; then
131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
132
132
133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
134 return
134 return
135 fi
135 fi
136
136
137 # global options
137 # global options
138 case "$prev" in
138 case "$prev" in
139 -R|--repository)
139 -R|--repository)
140 _hg_repos
140 _hg_repos
141 return
141 return
142 ;;
142 ;;
143 --cwd)
143 --cwd)
144 # Stick with default bash completion
144 # Stick with default bash completion
145 return
145 return
146 ;;
146 ;;
147 esac
147 esac
148
148
149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
150 _hg_commands
150 _hg_commands
151 return
151 return
152 fi
152 fi
153
153
154 # try to generate completion candidates for whatever command the user typed
154 # try to generate completion candidates for whatever command the user typed
155 local help
155 local help
156 local canonical=0
156 local canonical=0
157 if _hg_command_specific; then
157 if _hg_command_specific; then
158 return
158 return
159 fi
159 fi
160
160
161 # canonicalize the command name and try again
161 # canonicalize the command name and try again
162 help=$("$hg" help "$cmd" 2>/dev/null)
162 help=$("$hg" help "$cmd" 2>/dev/null)
163 if [ $? -ne 0 ]; then
163 if [ $? -ne 0 ]; then
164 # Probably either the command doesn't exist or it's ambiguous
164 # Probably either the command doesn't exist or it's ambiguous
165 return
165 return
166 fi
166 fi
167 cmd=${help#hg }
167 cmd=${help#hg }
168 cmd=${cmd%%[$' \n']*}
168 cmd=${cmd%%[$' \n']*}
169 canonical=1
169 canonical=1
170 _hg_command_specific
170 _hg_command_specific
171 }
171 }
172
172
173 _hg_command_specific()
173 _hg_command_specific()
174 {
174 {
175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
176 "_hg_cmd_$cmd"
176 "_hg_cmd_$cmd"
177 return 0
177 return 0
178 fi
178 fi
179
179
180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
181 if [ $canonical = 1 ]; then
181 if [ $canonical = 1 ]; then
182 _hg_tags
182 _hg_tags
183 return 0
183 return 0
184 elif [[ status != "$cmd"* ]]; then
184 elif [[ status != "$cmd"* ]]; then
185 _hg_tags
185 _hg_tags
186 return 0
186 return 0
187 else
187 else
188 return 1
188 return 1
189 fi
189 fi
190 fi
190 fi
191
191
192 case "$cmd" in
192 case "$cmd" in
193 help)
193 help)
194 _hg_commands
194 _hg_commands
195 ;;
195 ;;
196 export|manifest|update)
196 export|manifest|update)
197 _hg_tags
197 _hg_tags
198 ;;
198 ;;
199 pull|push|outgoing|incoming)
199 pull|push|outgoing|incoming)
200 _hg_paths
200 _hg_paths
201 _hg_repos
201 _hg_repos
202 ;;
202 ;;
203 paths)
203 paths)
204 _hg_paths
204 _hg_paths
205 ;;
205 ;;
206 add)
206 add)
207 _hg_status "u"
207 _hg_status "u"
208 ;;
208 ;;
209 commit)
209 commit)
210 _hg_status "mar"
210 _hg_status "mar"
211 ;;
211 ;;
212 remove)
212 remove)
213 _hg_status "d"
213 _hg_status "d"
214 ;;
214 ;;
215 forget)
215 forget)
216 _hg_status "a"
216 _hg_status "a"
217 ;;
217 ;;
218 diff)
218 diff)
219 _hg_status "mar"
219 _hg_status "mar"
220 ;;
220 ;;
221 revert)
221 revert)
222 _hg_status "mard"
222 _hg_status "mard"
223 ;;
223 ;;
224 clone)
224 clone)
225 local count=$(_hg_count_non_option)
225 local count=$(_hg_count_non_option)
226 if [ $count = 1 ]; then
226 if [ $count = 1 ]; then
227 _hg_paths
227 _hg_paths
228 fi
228 fi
229 _hg_repos
229 _hg_repos
230 ;;
230 ;;
231 debugindex|debugindexdot)
231 debugindex|debugindexdot)
232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
233 ;;
233 ;;
234 debugdata)
234 debugdata)
235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
236 ;;
236 ;;
237 *)
237 *)
238 return 1
238 return 1
239 ;;
239 ;;
240 esac
240 esac
241
241
242 return 0
242 return 0
243 }
243 }
244
244
245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
246 || complete -o default -F _hg hg
246 || complete -o default -F _hg hg
247
247
248
248
249 # Completion for commands provided by extensions
249 # Completion for commands provided by extensions
250
250
251 # mq
251 # mq
252 _hg_ext_mq_patchlist()
252 _hg_ext_mq_patchlist()
253 {
253 {
254 local patches=$("$hg" $1 2>/dev/null)
254 local patches
255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
255 patches=$("$hg" $1 2>/dev/null)
256 if [ $? -eq 0 ] && [ "$patches" ]; then
257 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
258 return 0
259 fi
260 return 1
256 }
261 }
257
262
258 _hg_ext_mq_queues()
263 _hg_ext_mq_queues()
259 {
264 {
260 local root=$("$hg" root 2>/dev/null)
265 local root=$("$hg" root 2>/dev/null)
261 local n
266 local n
262 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
267 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
263 # I think we're usually not interested in the regular "patches" queue
268 # I think we're usually not interested in the regular "patches" queue
264 # so just filter it.
269 # so just filter it.
265 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
270 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
266 COMPREPLY=(${COMPREPLY[@]:-} "$n")
271 COMPREPLY=(${COMPREPLY[@]:-} "$n")
267 fi
272 fi
268 done
273 done
269 }
274 }
270
275
271 _hg_cmd_qpop()
276 _hg_cmd_qpop()
272 {
277 {
273 if [[ "$prev" = @(-n|--name) ]]; then
278 if [[ "$prev" = @(-n|--name) ]]; then
274 _hg_ext_mq_queues
279 _hg_ext_mq_queues
275 return
280 return
276 fi
281 fi
277 _hg_ext_mq_patchlist qapplied
282 _hg_ext_mq_patchlist qapplied
278 }
283 }
279
284
280 _hg_cmd_qpush()
285 _hg_cmd_qpush()
281 {
286 {
282 if [[ "$prev" = @(-n|--name) ]]; then
287 if [[ "$prev" = @(-n|--name) ]]; then
283 _hg_ext_mq_queues
288 _hg_ext_mq_queues
284 return
289 return
285 fi
290 fi
286 _hg_ext_mq_patchlist qunapplied
291 _hg_ext_mq_patchlist qunapplied
287 }
292 }
288
293
289 _hg_cmd_qdelete()
294 _hg_cmd_qdelete()
290 {
295 {
291 _hg_ext_mq_patchlist qunapplied
296 _hg_ext_mq_patchlist qunapplied
292 }
297 }
293
298
294 _hg_cmd_qsave()
299 _hg_cmd_qsave()
295 {
300 {
296 if [[ "$prev" = @(-n|--name) ]]; then
301 if [[ "$prev" = @(-n|--name) ]]; then
297 _hg_ext_mq_queues
302 _hg_ext_mq_queues
298 return
303 return
299 fi
304 fi
300 }
305 }
301
306
302 _hg_cmd_strip()
307 _hg_cmd_strip()
303 {
308 {
304 _hg_tags
309 _hg_tags
305 }
310 }
306
311
307 _hg_cmd_qcommit()
312 _hg_cmd_qcommit()
308 {
313 {
309 local root=$("$hg" root 2>/dev/null)
314 local root=$("$hg" root 2>/dev/null)
310 # this is run in a sub-shell, so we can't use _hg_status
315 # this is run in a sub-shell, so we can't use _hg_status
311 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
316 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
312 "$hg" status -nmar 2>/dev/null)
317 "$hg" status -nmar 2>/dev/null)
313 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
318 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
314 }
319 }
315
320
316 _hg_cmd_export()
321 _hg_cmd_export()
317 {
322 {
318 _hg_ext_mq_patchlist qapplied
323 _hg_ext_mq_patchlist qapplied
319 }
324 }
320
325
321
326
322 # hbisect
327 # hbisect
323 _hg_cmd_bisect()
328 _hg_cmd_bisect()
324 {
329 {
325 local i subcmd
330 local i subcmd
326
331
327 # find the sub-command
332 # find the sub-command
328 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
333 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
329 if [[ ${COMP_WORDS[i]} != -* ]]; then
334 if [[ ${COMP_WORDS[i]} != -* ]]; then
330 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
335 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
331 subcmd="${COMP_WORDS[i]}"
336 subcmd="${COMP_WORDS[i]}"
332 break
337 break
333 fi
338 fi
334 fi
339 fi
335 done
340 done
336
341
337 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
342 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
338 COMPREPLY=(${COMPREPLY[@]:-}
343 COMPREPLY=(${COMPREPLY[@]:-}
339 $(compgen -W 'bad good help init next reset' -- "$cur"))
344 $(compgen -W 'bad good help init next reset' -- "$cur"))
340 return
345 return
341 fi
346 fi
342
347
343 case "$subcmd" in
348 case "$subcmd" in
344 good|bad)
349 good|bad)
345 _hg_tags
350 _hg_tags
346 ;;
351 ;;
347 esac
352 esac
348
353
349 return
354 return
350 }
355 }
351
356
352
357
353 # patchbomb
358 # patchbomb
354 _hg_cmd_email()
359 _hg_cmd_email()
355 {
360 {
356 case "$prev" in
361 case "$prev" in
357 -c|--cc|-t|--to|-f|--from)
362 -c|--cc|-t|--to|-f|--from)
358 # we need an e-mail address. let the user provide a function
363 # we need an e-mail address. let the user provide a function
359 # to get them
364 # to get them
360 if [ "$(type -t _hg_emails)" = function ]; then
365 if [ "$(type -t _hg_emails)" = function ]; then
361 local arg=to
366 local arg=to
362 if [[ "$prev" == @(-f|--from) ]]; then
367 if [[ "$prev" == @(-f|--from) ]]; then
363 arg=from
368 arg=from
364 fi
369 fi
365 local addresses=$(_hg_emails $arg)
370 local addresses=$(_hg_emails $arg)
366 COMPREPLY=(${COMPREPLY[@]:-}
371 COMPREPLY=(${COMPREPLY[@]:-}
367 $(compgen -W '$addresses' -- "$cur"))
372 $(compgen -W '$addresses' -- "$cur"))
368 fi
373 fi
369 return
374 return
370 ;;
375 ;;
371 -m|--mbox)
376 -m|--mbox)
372 # fallback to standard filename completion
377 # fallback to standard filename completion
373 return
378 return
374 ;;
379 ;;
375 -s|--subject)
380 -s|--subject)
376 # free form string
381 # free form string
377 return
382 return
378 ;;
383 ;;
379 esac
384 esac
380
385
381 _hg_tags
386 _hg_tags
382 return
387 return
383 }
388 }
384
389
385
390
386 # gpg
391 # gpg
387 _hg_cmd_sign()
392 _hg_cmd_sign()
388 {
393 {
389 _hg_tags
394 _hg_tags
390 }
395 }
General Comments 0
You need to be logged in to leave comments. Login now