#compdef subrepo

_subrepo() {
    local program=subrepo
    typeset -A opt_args
    local curcontext="$curcontext" state line context


        # ---- Command: 
        _arguments -s  \
            '1: :->cmd1' \
            '*: :->args' \
            && ret=0


        case $state in
        cmd1)
            _alternative 'args:cmd2:((branch\:"Create a branch with local subrepo commits since last pull." clean\:"Remove artifacts created by '"'"'fetch'"'"' and '"'"'branch'"'"' commands." clone\:"Add a repository as a subrepo in a subdir of your repository." commit\:"Add subrepo branch to current history as a single commit." fetch\:"Fetch the remote/upstream content for a subrepo." help\:"Same as '"'"'git help subrepo'"'"'" init\:"Turn an existing subdirectory into a subrepo." pull\:"Update the subrepo subdir with the latest upstream changes." push\:"Push a properly merged subrepo branch back upstream." status\:"Get the status of a subrepo." version\:"display version information about git-subrepo"))'
        ;;

        args)
            case $line[1] in
            _complete)

                # ---- Command: _complete
                _arguments -s -C \
                    '1: :->cmd1' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--name[name of the program]:name' \
                    '--zsh[for zsh]' \
                    '--bash[for bash]' \
                    && ret=0


            ;;
            _pod)

                # ---- Command: _pod
                _arguments -s -C \
                    '1: :->cmd1' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    && ret=0


            ;;
            branch)

                # ---- Command: branch
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--all[All subrepos]' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_branch_param_subrepo_completion
                ;;
                esac

            ;;
            clean)

                # ---- Command: clean
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--all[All subrepos]' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_clean_param_subrepo_completion
                ;;
                esac

            ;;
            clone)

                # ---- Command: clone
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->repository' \
                    '3: :->subdir' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--branch[Upstream branch]:branch' \
                    '-b[Upstream branch]:branch' \
                    '--force[reclone (completely replace) an existing subdir.]' \
                    '-f[reclone (completely replace) an existing subdir.]' \
                    && ret=0

                case $state in
                repository)

                ;;
                subdir)
_files
                ;;
                esac

            ;;
            commit)

                # ---- Command: commit
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '3: :->subreporef' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_commit_param_subrepo_completion
                ;;
                subreporef)

                ;;
                esac

            ;;
            fetch)

                # ---- Command: fetch
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--all[All subrepos]' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_fetch_param_subrepo_completion
                ;;
                esac

            ;;
            help)

                # ---- Command: help
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->cmd2' \
                    '*: :->args' \
                    && ret=0


                case $state in
                cmd2)
                    _alternative 'args:cmd3:((branch clean clone commit fetch init pull push status version))'
                ;;

                args)
                    case $line[2] in
                    _complete)

                        # ---- Command: help _complete
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    _pod)

                        # ---- Command: help _pod
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    branch)

                        # ---- Command: help branch
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    clean)

                        # ---- Command: help clean
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    clone)

                        # ---- Command: help clone
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    commit)

                        # ---- Command: help commit
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    fetch)

                        # ---- Command: help fetch
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    init)

                        # ---- Command: help init
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    pull)

                        # ---- Command: help pull
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    push)

                        # ---- Command: help push
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    status)

                        # ---- Command: help status
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    version)

                        # ---- Command: help version
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            && ret=0


                    ;;
                    esac

                ;;

                esac
            ;;
            init)

                # ---- Command: init
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subdir' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--remote[Specify remote repository]:remote' \
                    '-r[Specify remote repository]:remote' \
                    '--branch[Upstream branch]:branch' \
                    '-b[Upstream branch]:branch' \
                    && ret=0

                case $state in
                subdir)
_files
                ;;
                esac

            ;;
            pull)

                # ---- Command: pull
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--all[All subrepos]' \
                    '--branch[Upstream branch]:branch' \
                    '-b[Upstream branch]:branch' \
                    '--remote[Specify remote repository]:remote' \
                    '-r[Specify remote repository]:remote' \
                    '--update[update]:update' \
                    '-u[update]:update' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_pull_param_subrepo_completion
                ;;
                esac

            ;;
            push)

                # ---- Command: push
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--all[All subrepos]' \
                    '--branch[Upstream branch]:branch' \
                    '-b[Upstream branch]:branch' \
                    '--remote[Specify remote repository]:remote' \
                    '-r[Specify remote repository]:remote' \
                    '--update[update]:update' \
                    '-u[update]:update' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_push_param_subrepo_completion
                ;;
                esac

            ;;
            status)

                # ---- Command: status
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->subrepo' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--quiet[Just print names]:quiet' \
                    '-q[Just print names]:quiet' \
                    && ret=0

                case $state in
                subrepo)
_subrepo_status_param_subrepo_completion
                ;;
                esac

            ;;
            version)

                # ---- Command: version
                _arguments -s -C \
                    '1: :->cmd1' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    && ret=0


            ;;
            esac

        ;;

        esac

}

_subrepo_branch_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_clean_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_commit_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_fetch_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_pull_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_push_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}
_subrepo_status_param_subrepo_completion() {
    local __dynamic_completion
    IFS=$'\n' set -A __dynamic_completion `$program 'status' '--quiet'`
    compadd -X "subrepo:" $__dynamic_completion
}

__subrepo_dynamic_comp() {
    local argname="$1"
    local arg="$2"
    local comp="arg:$argname:(("
    local line
    while read -r line; do
        local name="$line"
        local desc="$line"
        name="${name%$'\t'*}"
        desc="${desc/*$'\t'}"
        comp="$comp$name"
        if [[ -n "$desc" && "$name" != "$desc" ]]; then
            comp="$comp\\:"'"'"$desc"'"'
        fi
        comp="$comp "
    done <<< "$arg"

    comp="$comp))"
    _alternative "$comp"
}

