Help and docs
Sign in

grindlemire/dotfiles code browser

main 0375daa
40 lines · 1.2 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
35
36
37
38
39
40
#compdef wtd

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

    _arguments -C \
        '(-f --force)'{-f,--force}'[Force removal even with uncommitted changes]' \
        '1: :->worktree' \
        && return 0

    case $state in
        worktree)
            local -a worktrees
            # Get worktree branches (excluding main worktree)
            worktrees=(${(f)"$(git worktree list --porcelain 2>/dev/null | awk '
                BEGIN { RS=""; FS="\n" }
                {
                    path=""; branch="";
                    for (i=1; i<=NF; i++) {
                        if ($i ~ /^worktree /) path=substr($i,10)
                        else if ($i ~ /^branch /) {
                            branch=substr($i,8)
                            sub(/^refs\/heads\//, "", branch)
                        }
                    }
                    # Skip main worktree (no .worktrees in path)
                    if (path ~ /\.worktrees/ && branch != "") print branch
                }
            ')"})
            if (( ${#worktrees} > 0 )); then
                _describe -t worktrees 'worktree' worktrees
            else
                _message 'no worktrees found'
            fi
            ;;
    esac
}

_wtd "$@"