# HG changeset patch # User Alexis S. L. Carvalho # Date 1143994852 -7200 # Node ID 0c438fd25e6ef356a86b26aa8659d33afb757620 # Parent 5c4496ed152d133a247498bc35f5c9050d9c0a44 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. diff --git a/contrib/bash_completion b/contrib/bash_completion --- a/contrib/bash_completion +++ b/contrib/bash_completion @@ -99,13 +99,38 @@ shopt -s extglob return fi - # canonicalize command name - cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q') + # try to generate completion candidates for whatever command the user typed + local help + local canonical=0 + if _hg_command_specific; then + return + fi - if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then - _hg_tags + # canonicalize the command name and try again + help=$("$hg" help "$cmd" 2>/dev/null) + if [ $? -ne 0 ]; then + # Probably either the command doesn't exist or it's ambiguous return fi + cmd=${help#hg } + cmd=${cmd%%[$' \n']*} + canonical=1 + _hg_command_specific +} + +_hg_command_specific() +{ + if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then + if [ $canonical = 1 ]; then + _hg_tags + return 0 + elif [[ status != "$cmd"* ]]; then + _hg_tags + return 0 + else + return 1 + fi + fi case "$cmd" in help) @@ -152,8 +177,12 @@ shopt -s extglob debugdata) COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur")) ;; + *) + return 1 + ;; esac + return 0 } complete -o bashdefault -o default -F _hg hg 2>/dev/null \