aboutsummaryrefslogtreecommitdiffstats
path: root/completions/gitctl.bash
blob: 497d0a822debd8e5bbff56e9af38d54f4ab7a421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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 --private --desc --owner" -- "$cur")) ;;
            "repo add-remote") COMPREPLY=($(compgen -W "--remote-name" -- "$cur")) ;;
        esac
        return
    fi
}
complete -F _gitctl gitctl