Mercurial > hg
comparison contrib/bash_completion @ 2039:0c438fd25e6e
bash_completion: small optimization
Right now we always call "hg help $cmd" to get the canonical name of $cmd
(i.e. to go from "co" to "update").
This patch optimistically assumes that $cmd is already the canonical form
and tries to generate completions for it. If that fails, it falls back
to canonicalizing $cmd and trying again.
This means that:
- if a command or alias is explicitly handled by the
_hg_command_specific function, things get somewhat faster
- as long as the canonical $cmd is handled by _hg_command_specific, all
its aliases and abbreviations are also handled.
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Sun, 02 Apr 2006 18:20:52 +0200 |
parents | 107dc72880f8 |
children | 077a2da7f1de |
comparison
equal
deleted
inserted
replaced
2038:5c4496ed152d | 2039:0c438fd25e6e |
---|---|
97 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then | 97 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then |
98 _hg_commands | 98 _hg_commands |
99 return | 99 return |
100 fi | 100 fi |
101 | 101 |
102 # canonicalize command name | 102 # try to generate completion candidates for whatever command the user typed |
103 cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q') | 103 local help |
104 local canonical=0 | |
105 if _hg_command_specific; then | |
106 return | |
107 fi | |
104 | 108 |
105 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then | 109 # canonicalize the command name and try again |
106 _hg_tags | 110 help=$("$hg" help "$cmd" 2>/dev/null) |
111 if [ $? -ne 0 ]; then | |
112 # Probably either the command doesn't exist or it's ambiguous | |
107 return | 113 return |
114 fi | |
115 cmd=${help#hg } | |
116 cmd=${cmd%%[$' \n']*} | |
117 canonical=1 | |
118 _hg_command_specific | |
119 } | |
120 | |
121 _hg_command_specific() | |
122 { | |
123 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then | |
124 if [ $canonical = 1 ]; then | |
125 _hg_tags | |
126 return 0 | |
127 elif [[ status != "$cmd"* ]]; then | |
128 _hg_tags | |
129 return 0 | |
130 else | |
131 return 1 | |
132 fi | |
108 fi | 133 fi |
109 | 134 |
110 case "$cmd" in | 135 case "$cmd" in |
111 help) | 136 help) |
112 _hg_commands | 137 _hg_commands |
150 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur")) | 175 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur")) |
151 ;; | 176 ;; |
152 debugdata) | 177 debugdata) |
153 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur")) | 178 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur")) |
154 ;; | 179 ;; |
180 *) | |
181 return 1 | |
182 ;; | |
155 esac | 183 esac |
156 | 184 |
185 return 0 | |
157 } | 186 } |
158 | 187 |
159 complete -o bashdefault -o default -F _hg hg 2>/dev/null \ | 188 complete -o bashdefault -o default -F _hg hg 2>/dev/null \ |
160 || complete -o default -F _hg hg | 189 || complete -o default -F _hg hg |