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