diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
commit | dcfc80aaa112498498d93bae3ae0eb2088244c47 (patch) | |
tree | 889854ba1c96f1c86465da1bbd5cc87f0c8d4b9f /bash/bashrc.d/git.bash | |
parent | Handle spaces correctly in completions (diff) | |
download | dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.tar.gz dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.zip |
Tidy up completion considerably; no more compgen
* Remove all instances of compgen; for filename completion it's quite
broken as it relies on implicit wordsplitting in array context, and
doesn't have an option to print with a null delimiter; replaced with
manual for/while loops instead
* Add IFS= to while/read loops over filenames
* Use "dirname/s" instead of "dir/s" variables to avoid keyword
collisions and for clarity
* Remove some unnecessary variables
* Use shorter syntax for loop exit conditions
* Move completion options into functions where applicable rather than
having them on the completion definition itself
Diffstat (limited to 'bash/bashrc.d/git.bash')
-rw-r--r-- | bash/bashrc.d/git.bash | 21 |
1 files changed, 8 insertions, 13 deletions
diff --git a/bash/bashrc.d/git.bash b/bash/bashrc.d/git.bash index 5965607c..182cef46 100644 --- a/bash/bashrc.d/git.bash +++ b/bash/bashrc.d/git.bash @@ -1,27 +1,22 @@ # Completion for git local branch names _git() { - # Bail if not a git repo (or no git!) - if ! git rev-parse --git-dir >/dev/null 2>&1 ; then - return 1 - fi + # Use default completion if no matches + compopt -o default - # Get current and previous word - local word first - word=${COMP_WORDS[COMP_CWORD]} - first=${COMP_WORDS[1]} + # Bail if not a git repo (or no git!) + git rev-parse --git-dir >/dev/null 2>&1 || return 1 # Switch on the previous word - case $first in + case ${COMP_WORDS[1]} in # If the first word is appropriate, complete with branch/tag names checkout|merge|rebase) - local -a branches local branch while read -r branch ; do - branches=("${branches[@]}" "${branch##*/}") + [[ $branch == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue + COMPREPLY=("${COMPREPLY[@]}" "$branch") done < <(git for-each-ref refs/{heads,tags} 2>/dev/null) - COMPREPLY=( $(compgen -W "${branches[*]}" -- "$word") ) return ;; @@ -31,5 +26,5 @@ _git() { ;; esac } -complete -F _git -o default git +complete -F _git git |