##// END OF EJS Templates
bash_completion: make export fall back to tags when there's no mq patch applied
Alexis S. L. Carvalho -
r3481:13a9a213 default
parent child Browse files
Show More
@@ -1,395 +1,396 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)
197 if _hg_ext_mq_patchlist qapplied && [ "${COMPREPLY[*]}" ]; then
198 return 0
199 fi
200 _hg_tags
201 ;;
202 manifest|update)
197 _hg_tags
203 _hg_tags
198 ;;
204 ;;
199 pull|push|outgoing|incoming)
205 pull|push|outgoing|incoming)
200 _hg_paths
206 _hg_paths
201 _hg_repos
207 _hg_repos
202 ;;
208 ;;
203 paths)
209 paths)
204 _hg_paths
210 _hg_paths
205 ;;
211 ;;
206 add)
212 add)
207 _hg_status "u"
213 _hg_status "u"
208 ;;
214 ;;
209 commit)
215 commit)
210 _hg_status "mar"
216 _hg_status "mar"
211 ;;
217 ;;
212 remove)
218 remove)
213 _hg_status "d"
219 _hg_status "d"
214 ;;
220 ;;
215 forget)
221 forget)
216 _hg_status "a"
222 _hg_status "a"
217 ;;
223 ;;
218 diff)
224 diff)
219 _hg_status "mar"
225 _hg_status "mar"
220 ;;
226 ;;
221 revert)
227 revert)
222 _hg_status "mard"
228 _hg_status "mard"
223 ;;
229 ;;
224 clone)
230 clone)
225 local count=$(_hg_count_non_option)
231 local count=$(_hg_count_non_option)
226 if [ $count = 1 ]; then
232 if [ $count = 1 ]; then
227 _hg_paths
233 _hg_paths
228 fi
234 fi
229 _hg_repos
235 _hg_repos
230 ;;
236 ;;
231 debugindex|debugindexdot)
237 debugindex|debugindexdot)
232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
238 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
233 ;;
239 ;;
234 debugdata)
240 debugdata)
235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
241 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
236 ;;
242 ;;
237 *)
243 *)
238 return 1
244 return 1
239 ;;
245 ;;
240 esac
246 esac
241
247
242 return 0
248 return 0
243 }
249 }
244
250
245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
251 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
246 || complete -o default -F _hg hg
252 || complete -o default -F _hg hg
247
253
248
254
249 # Completion for commands provided by extensions
255 # Completion for commands provided by extensions
250
256
251 # mq
257 # mq
252 _hg_ext_mq_patchlist()
258 _hg_ext_mq_patchlist()
253 {
259 {
254 local patches
260 local patches
255 patches=$("$hg" $1 2>/dev/null)
261 patches=$("$hg" $1 2>/dev/null)
256 if [ $? -eq 0 ] && [ "$patches" ]; then
262 if [ $? -eq 0 ] && [ "$patches" ]; then
257 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
263 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
258 return 0
264 return 0
259 fi
265 fi
260 return 1
266 return 1
261 }
267 }
262
268
263 _hg_ext_mq_queues()
269 _hg_ext_mq_queues()
264 {
270 {
265 local root=$("$hg" root 2>/dev/null)
271 local root=$("$hg" root 2>/dev/null)
266 local n
272 local n
267 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
273 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
268 # I think we're usually not interested in the regular "patches" queue
274 # I think we're usually not interested in the regular "patches" queue
269 # so just filter it.
275 # so just filter it.
270 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
276 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
271 COMPREPLY=(${COMPREPLY[@]:-} "$n")
277 COMPREPLY=(${COMPREPLY[@]:-} "$n")
272 fi
278 fi
273 done
279 done
274 }
280 }
275
281
276 _hg_cmd_qpop()
282 _hg_cmd_qpop()
277 {
283 {
278 if [[ "$prev" = @(-n|--name) ]]; then
284 if [[ "$prev" = @(-n|--name) ]]; then
279 _hg_ext_mq_queues
285 _hg_ext_mq_queues
280 return
286 return
281 fi
287 fi
282 _hg_ext_mq_patchlist qapplied
288 _hg_ext_mq_patchlist qapplied
283 }
289 }
284
290
285 _hg_cmd_qpush()
291 _hg_cmd_qpush()
286 {
292 {
287 if [[ "$prev" = @(-n|--name) ]]; then
293 if [[ "$prev" = @(-n|--name) ]]; then
288 _hg_ext_mq_queues
294 _hg_ext_mq_queues
289 return
295 return
290 fi
296 fi
291 _hg_ext_mq_patchlist qunapplied
297 _hg_ext_mq_patchlist qunapplied
292 }
298 }
293
299
294 _hg_cmd_qdelete()
300 _hg_cmd_qdelete()
295 {
301 {
296 _hg_ext_mq_patchlist qunapplied
302 _hg_ext_mq_patchlist qunapplied
297 }
303 }
298
304
299 _hg_cmd_qsave()
305 _hg_cmd_qsave()
300 {
306 {
301 if [[ "$prev" = @(-n|--name) ]]; then
307 if [[ "$prev" = @(-n|--name) ]]; then
302 _hg_ext_mq_queues
308 _hg_ext_mq_queues
303 return
309 return
304 fi
310 fi
305 }
311 }
306
312
307 _hg_cmd_strip()
313 _hg_cmd_strip()
308 {
314 {
309 _hg_tags
315 _hg_tags
310 }
316 }
311
317
312 _hg_cmd_qcommit()
318 _hg_cmd_qcommit()
313 {
319 {
314 local root=$("$hg" root 2>/dev/null)
320 local root=$("$hg" root 2>/dev/null)
315 # this is run in a sub-shell, so we can't use _hg_status
321 # this is run in a sub-shell, so we can't use _hg_status
316 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
322 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
317 "$hg" status -nmar 2>/dev/null)
323 "$hg" status -nmar 2>/dev/null)
318 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
324 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
319 }
325 }
320
326
321 _hg_cmd_export()
322 {
323 _hg_ext_mq_patchlist qapplied
324 }
325
326
327
327 # hbisect
328 # hbisect
328 _hg_cmd_bisect()
329 _hg_cmd_bisect()
329 {
330 {
330 local i subcmd
331 local i subcmd
331
332
332 # find the sub-command
333 # find the sub-command
333 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
334 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
334 if [[ ${COMP_WORDS[i]} != -* ]]; then
335 if [[ ${COMP_WORDS[i]} != -* ]]; then
335 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
336 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
336 subcmd="${COMP_WORDS[i]}"
337 subcmd="${COMP_WORDS[i]}"
337 break
338 break
338 fi
339 fi
339 fi
340 fi
340 done
341 done
341
342
342 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
343 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
343 COMPREPLY=(${COMPREPLY[@]:-}
344 COMPREPLY=(${COMPREPLY[@]:-}
344 $(compgen -W 'bad good help init next reset' -- "$cur"))
345 $(compgen -W 'bad good help init next reset' -- "$cur"))
345 return
346 return
346 fi
347 fi
347
348
348 case "$subcmd" in
349 case "$subcmd" in
349 good|bad)
350 good|bad)
350 _hg_tags
351 _hg_tags
351 ;;
352 ;;
352 esac
353 esac
353
354
354 return
355 return
355 }
356 }
356
357
357
358
358 # patchbomb
359 # patchbomb
359 _hg_cmd_email()
360 _hg_cmd_email()
360 {
361 {
361 case "$prev" in
362 case "$prev" in
362 -c|--cc|-t|--to|-f|--from)
363 -c|--cc|-t|--to|-f|--from)
363 # we need an e-mail address. let the user provide a function
364 # we need an e-mail address. let the user provide a function
364 # to get them
365 # to get them
365 if [ "$(type -t _hg_emails)" = function ]; then
366 if [ "$(type -t _hg_emails)" = function ]; then
366 local arg=to
367 local arg=to
367 if [[ "$prev" == @(-f|--from) ]]; then
368 if [[ "$prev" == @(-f|--from) ]]; then
368 arg=from
369 arg=from
369 fi
370 fi
370 local addresses=$(_hg_emails $arg)
371 local addresses=$(_hg_emails $arg)
371 COMPREPLY=(${COMPREPLY[@]:-}
372 COMPREPLY=(${COMPREPLY[@]:-}
372 $(compgen -W '$addresses' -- "$cur"))
373 $(compgen -W '$addresses' -- "$cur"))
373 fi
374 fi
374 return
375 return
375 ;;
376 ;;
376 -m|--mbox)
377 -m|--mbox)
377 # fallback to standard filename completion
378 # fallback to standard filename completion
378 return
379 return
379 ;;
380 ;;
380 -s|--subject)
381 -s|--subject)
381 # free form string
382 # free form string
382 return
383 return
383 ;;
384 ;;
384 esac
385 esac
385
386
386 _hg_tags
387 _hg_tags
387 return
388 return
388 }
389 }
389
390
390
391
391 # gpg
392 # gpg
392 _hg_cmd_sign()
393 _hg_cmd_sign()
393 {
394 {
394 _hg_tags
395 _hg_tags
395 }
396 }
General Comments 0
You need to be logged in to leave comments. Login now