# bash completion for gitctl # install: cp completions/gitctl.bash /etc/bash_completion.d/gitctl # or: source completions/gitctl.bash (from ~/.bashrc) _gitctl() { local cur prev words cword _init_completion 2>/dev/null || { # minimal fallback if bash-completion's _init_completion is absent cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words=("${COMP_WORDS[@]}") cword=$COMP_CWORD } # global -y/--yes may sit before the subcommand; skip it so group/action and # the cword positions below line up whether or not it is present. local off=0 [[ ${words[1]} == "-y" || ${words[1]} == "--yes" ]] && off=1 local group=${words[1+off]} action=${words[2+off]} local cword=$((cword - off)) # complete section names from the live server (one ssh call). Only when the # previous word is --section, so we do not pay it on every Tab. if [[ $prev == "--section" ]]; then local secs secs=$(gitctl sections list 2>/dev/null) local IFS=$'\n' COMPREPLY=($(compgen -W "$secs" -- "$cur")) return fi # complete repo names for the name argument of desc/delete/add-remote (one # ssh call). Only at the name position and not while typing a flag, so a # stray Tab elsewhere never hits the network. `repo list` prints an aligned # table (no color when not a TTY, as here); the name is the first field, and # we drop the NAME header row. if [[ $cword -eq 3 && $group == repo && $cur != -* \ && ( $action == desc || $action == delete || $action == add-remote ) ]]; then local repos repos=$(gitctl repo list 2>/dev/null | awk 'NR>1 {print $1}') local IFS=$'\n' COMPREPLY=($(compgen -W "$repos" -- "$cur")) return fi case $cword in 1) COMPREPLY=($(compgen -W "-y --yes sections sync repo" -- "$cur")) return ;; 2) case $group in sections) COMPREPLY=($(compgen -W "list add" -- "$cur")) ;; repo) COMPREPLY=($(compgen -W "create list desc add-remote delete" -- "$cur")) ;; esac return ;; esac # flags, by subcommand if [[ $cur == -* ]]; then case "$group $action" in "repo create") COMPREPLY=($(compgen -W "--section --desc --owner" -- "$cur")) ;; "repo add-remote") COMPREPLY=($(compgen -W "--remote-name" -- "$cur")) ;; esac return fi } complete -F _gitctl gitctl