Help and docs
Sign in

grindlemire/dotfiles code browser

main 0375daa
34 lines · 1.0 KB
 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
#compdef wtc

_wtc() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    _arguments -C \
        '1: :->branch' \
        '*: :->command' \
        && return 0

    case $state in
        branch)
            local -a branches worktrees
            # Get existing worktree branches
            worktrees=(${(f)"$(git worktree list --porcelain 2>/dev/null | awk '/^branch / {sub(/^refs\/heads\//, "", $2); print $2}')"})
            # Get all local branches
            branches=(${(f)"$(git for-each-ref --format='%(refname:short)' refs/heads/ 2>/dev/null)"})
            # Combine and dedupe
            local -a all_branches
            all_branches=("${(@u)worktrees}" "${(@u)branches}")
            _describe -t branches 'branch' all_branches && return 0
            # Also allow new branch names
            _message 'branch name (existing or new)'
            ;;
        command)
            # Complete commands after branch
            _command_names -e
            _normal
            ;;
    esac
}

_wtc "$@"