# -*- shell-script -*-
#
# SPDX-License-Identifier: GPL-2.0-only
#
# bash completion script for StGit (automatically generated)
#
# To use these routines:
#
#    1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
#
#    2. Add the following line to your .bashrc:
#         . ~/.stgit-completion.bash

shopt -s extglob

__stg ()
{
    stg ${__C_args:+"${__C_args[@]}"} "$@" 2>/dev/null
}

__git ()
{
    git ${__C_args:+"${__C_args[@]}"} "$@" 2>/dev/null
}

# The path to .git, or empty if we're not in a repository.
_gitdir ()
{
    __git rev-parse --git-dir 2>/dev/null
}

# Name of the current branch, or empty if there isn't one.
_current_branch ()
{
    local b
    b=$(__git symbolic-ref HEAD 2>/dev/null)
    echo "${b#refs/heads/}"
}

_patch_range ()
{
    local patches="$1"

    case "$cur" in
    *..*)
        local cur="$cur"
        local pfx="${cur%..*}.."
        cur="${cur#*..}"
        mapfile -t COMPREPLY < <(compgen -o nosort -P "$pfx" -W "$patches" -- "$cur")
        ;;
    *)
        mapfile -t COMPREPLY < <(compgen -o nosort -W "$patches" -- "$cur")
        ;;
    esac
}

_stg_branches ()
{
    __stg branch --list 2>/dev/null | grep ". s" | cut -f2 | cut -d" " -f1
}

_all_branches ()
{
    __stg branch --list 2>/dev/null | cut -f2 | cut -d" " -f1
}

_mail_aliases ()
{
    __git config --name-only --get-regexp "^mail\.alias\." | cut -d. -f 3
}

_tags ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git show-ref  | grep ' refs/tags/' | sed 's,.* refs/tags/,,'
}

_remotes ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git show-ref  | grep ' refs/remotes/' | sed 's,.* refs/remotes/,,'
}

_all_patches ()
{
    __stg series --no-description --noprefix --all
}

_applied_patches ()
{
    __stg series --no-description --noprefix --applied
}

_unapplied_patches ()
{
    __stg series --no-description --noprefix --unapplied
}

_visible_patches ()
{
    __stg series --no-description --noprefix --applied --unapplied
}

_hidden_patches ()
{
    __stg series --no-description --noprefix --hidden
}

_conflicting_files ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git ls-files --unmerged | sed 's/.*\t//g' | sort -u
}

_dirty_files ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git diff-index --name-only HEAD
}

_unknown_files ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git ls-files --others --exclude-standard
}

_known_files ()
{
    local g
    g=$(_gitdir)
    test "$g" && __git ls-files "${cur}*"
}

_git_diff_opts ()
{
    __git diff-tree --git-completion-helper
}

_git_format_patch_opts ()
{
    __git format-patch --git-completion-helper
}

_git_send_email_opts()
{
    __git send-email --git-completion-helper
}

_stg-branch ()
{
    local short_flags="'-h ' '-l ' '-c ' '-C ' '-r ' '-p ' '-u ' '-D ' '-d '"
    local long_flags="'--merge ' --color= '--help ' '--list ' '--create ' '--clone ' '--rename ' '--protect ' '--unprotect ' '--delete ' '--cleanup ' '--describe '"
    local options="--color"
    local pos_index=0
    local __subcommands="--list -l --create -c --clone -C --rename -r --protect -p --unprotect -u --delete -D --cleanup --describe -d"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --list|-l)
            cmd_index=$i
            _stg-branch---list; return;;
        --create|-c)
            cmd_index=$i
            _stg-branch---create; return;;
        --clone|-C)
            cmd_index=$i
            _stg-branch---clone; return;;
        --rename|-r)
            cmd_index=$i
            _stg-branch---rename; return;;
        --protect|-p)
            cmd_index=$i
            _stg-branch---protect; return;;
        --unprotect|-u)
            cmd_index=$i
            _stg-branch---unprotect; return;;
        --delete|-D)
            cmd_index=$i
            _stg-branch---delete; return;;
        --cleanup)
            cmd_index=$i
            _stg-branch---cleanup; return;;
        --describe|--description|-d)
            cmd_index=$i
            _stg-branch---describe; return;;
        --merge|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches)" -- "$cur")
        mapfile -O ${#COMPREPLY[@]} -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
    fi
}

_stg-branch---list ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-branch---create ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi

    if (( pos_index == 1 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches) $(_tags) $(_remotes)" -- "$cur")
    fi
}

_stg-branch---clone ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi
}

_stg-branch---rename ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches)" -- "$cur")
    fi
}

_stg-branch---protect ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
    fi
}

_stg-branch---unprotect ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
    fi
}

_stg-branch---delete ()
{
    local short_flags="'-h '"
    local long_flags="'--force ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --force|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches)" -- "$cur")
    fi
}

_stg-branch---cleanup ()
{
    local short_flags="'-h '"
    local long_flags="'--force ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --force|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
    fi
}

_stg-branch---describe ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi

    if (( pos_index == 1 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches)" -- "$cur")
    fi
}

_stg-clean ()
{
    local short_flags="'-A ' '-U ' '-h '"
    local long_flags="'--applied ' '--unapplied ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --applied|-A|--unapplied|-U|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-commit ()
{
    local short_flags="'-n ' '-a ' '-h '"
    local long_flags="--number= '--all ' '--allow-empty ' --color= '--help '"
    local options="--number|-n|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--allow-empty|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-completion ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--output= --color= '--help '"
    local options="--output|-o|--color"
    local pos_index=0
    local __subcommands="bash fish zsh list man help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        bash)
            cmd_index=$i
            _stg-completion-bash; return;;
        fish)
            cmd_index=$i
            _stg-completion-fish; return;;
        zsh)
            cmd_index=$i
            _stg-completion-zsh; return;;
        list)
            cmd_index=$i
            _stg-completion-list; return;;
        man)
            cmd_index=$i
            _stg-completion-man; return;;
        help)
            cmd_index=$i
            _stg-completion-help; return;;
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-completion-bash ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--output= --color= '--help '"
    local options="--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-fish ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--output= --color= '--help '"
    local options="--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-zsh ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--output= --color= '--help '"
    local options="--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--style= --output= --color= '--help '"
    local options="--style|--output|-o|--color"
    local pos_index=0
    local __subcommands="aliases commands commands-and-aliases help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        aliases)
            cmd_index=$i
            _stg-completion-list-aliases; return;;
        commands)
            cmd_index=$i
            _stg-completion-list-commands; return;;
        commands-and-aliases)
            cmd_index=$i
            _stg-completion-list-commands-and-aliases; return;;
        help)
            cmd_index=$i
            _stg-completion-list-help; return;;
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --style)
        mapfile -t COMPREPLY < <(compgen -W "name-only asciidoc fish zsh" -- "$cur")
        return;;
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-completion-list-aliases ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="'--show-expansion ' --style= --output= --color= '--help '"
    local options="--style|--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --show-expansion|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --style)
        mapfile -t COMPREPLY < <(compgen -W "name-only asciidoc fish zsh" -- "$cur")
        return;;
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-commands ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--style= --output= --color= '--help '"
    local options="--style|--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --style)
        mapfile -t COMPREPLY < <(compgen -W "name-only asciidoc fish zsh" -- "$cur")
        return;;
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-commands-and-aliases ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--style= --output= --color= '--help '"
    local options="--style|--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --style)
        mapfile -t COMPREPLY < <(compgen -W "name-only asciidoc fish zsh" -- "$cur")
        return;;
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="aliases commands commands-and-aliases help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        aliases)
            cmd_index=$i
            _stg-completion-list-help-aliases; return;;
        commands)
            cmd_index=$i
            _stg-completion-list-help-commands; return;;
        commands-and-aliases)
            cmd_index=$i
            _stg-completion-list-help-commands-and-aliases; return;;
        help)
            cmd_index=$i
            _stg-completion-list-help-help; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-completion-list-help-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-help-commands ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-help-commands-and-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-list-help-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-man ()
{
    local short_flags="'-o ' '-h '"
    local long_flags="--output= --color= '--help '"
    local options="--output|-o|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --output|-o)
        mapfile -t COMPREPLY < <(compgen -o directory -A directory -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="bash fish zsh list man help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        bash)
            cmd_index=$i
            _stg-completion-help-bash; return;;
        fish)
            cmd_index=$i
            _stg-completion-help-fish; return;;
        zsh)
            cmd_index=$i
            _stg-completion-help-zsh; return;;
        list)
            cmd_index=$i
            _stg-completion-help-list; return;;
        man)
            cmd_index=$i
            _stg-completion-help-man; return;;
        help)
            cmd_index=$i
            _stg-completion-help-help; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-completion-help-bash ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-fish ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-zsh ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-list ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="aliases commands commands-and-aliases"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        aliases)
            cmd_index=$i
            _stg-completion-help-list-aliases; return;;
        commands)
            cmd_index=$i
            _stg-completion-help-list-commands; return;;
        commands-and-aliases)
            cmd_index=$i
            _stg-completion-help-list-commands-and-aliases; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-completion-help-list-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-list-commands ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-list-commands-and-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-man ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-completion-help-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-delete ()
{
    local short_flags="'-a ' '-A ' '-U ' '-H ' '-t ' '-b ' '-h '"
    local long_flags="'--all ' '--applied ' '--unapplied ' '--hidden ' '--top ' '--spill ' --branch= --conflicts= --color= '--help '"
    local options="--branch|-b|--conflicts|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--applied|-A|--unapplied|-U|--hidden|-H|--top|-t|--spill|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-diff ()
{
    local short_flags="'-r ' '-s ' '-O ' '-h '"
    local long_flags="--range= '--stat ' --diff-opt= --color= '--help '"
    local options="--range|-r|--diff-opt|-O|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --stat|-s|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --range|-r)
        :
        return;;
    --diff-opt|-O)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_diff_opts)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-edit ()
{
    local short_flags="'-e ' '-d ' '-m ' '-f ' '-s ' '-t ' '-h '"
    local long_flags="'--edit ' '--diff ' --message= --file= '--no-verify ' --signoff= --ack= --review= --author= --authname= --authemail= --authdate= '--committer-date-is-author-date ' --save-template= --set-tree= --color= '--help '"
    local options="--message|-m|--file|-f|--signoff|-s|--ack|--review|--sign-by|--ack-by|--review-by|--author|--authname|--authemail|--authdate|--save-template|--set-tree|-t|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --edit|-e|--diff|-d|--no-verify|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --message|-m)
        :
        return;;
    --file|-f)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --signoff|-s)
        :
        return;;
    --ack)
        :
        return;;
    --review)
        :
        return;;
    --sign-by)
        :
        return;;
    --ack-by)
        :
        return;;
    --review-by)
        :
        return;;
    --author)
        :
        return;;
    --authname)
        :
        return;;
    --authemail)
        :
        return;;
    --authdate)
        :
        return;;
    --save-template)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --set-tree|-t)
        _patch_range "$(_all_patches)"
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_visible_patches)" -- "$cur")
    fi
}

_stg-email ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0
    local __subcommands="format send help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        format)
            cmd_index=$i
            _stg-email-format; return;;
        send)
            cmd_index=$i
            _stg-email-send; return;;
        help)
            cmd_index=$i
            _stg-email-help; return;;
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-email-format ()
{
    local short_flags="'-b ' '-a ' '-G ' '-o ' '-n ' '-N ' '-v ' '-s ' '-k ' '-h '"
    local long_flags="--branch= '--all ' --git-opt= --output-directory= '--cover-letter ' '--numbered ' '--no-numbered ' --start-number= --reroll-count= '--rfc ' --subject-prefix= '--quiet ' '--signoff ' '--numbered-files ' --suffix= '--keep-subject ' '--no-binary ' '--zero-commit ' --to= '--no-to ' --cc= '--no-cc ' --in-reply-to= --add-header= '--attach ' '--inline ' --thread= '--no-thread ' --signature= '--no-signature ' --signature-file= --base= '--progress ' --interdiff= --range-diff= --creation-factor= --color= '--help '"
    local options="--branch|-b|--git-opt|-G|--output-directory|-o|--start-number|--reroll-count|-v|--subject-prefix|--suffix|--to|--cc|--in-reply-to|--add-header|--thread|--signature|--signature-file|--base|--interdiff|--range-diff|--creation-factor|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--cover-letter|--numbered|-n|--no-numbered|-N|--rfc|--quiet|--signoff|-s|--numbered-files|--keep-subject|-k|--no-binary|--zero-commit|--no-to|--no-cc|--attach|--inline|--no-thread|--no-signature|--progress|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --git-opt|-G)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_format_patch_opts)" -- "$cur")
        return;;
    --output-directory|-o)
        mapfile -t COMPREPLY < <(compgen -o directory -A directory -- "$cur")
        return;;
    --start-number)
        :
        return;;
    --reroll-count|-v)
        :
        return;;
    --subject-prefix)
        :
        return;;
    --suffix)
        :
        return;;
    --to)
        :
        return;;
    --cc)
        :
        return;;
    --in-reply-to)
        :
        return;;
    --add-header)
        :
        return;;
    --thread)
        mapfile -t COMPREPLY < <(compgen -W "shallow deep " -- "$cur")
        return;;
    --signature)
        :
        return;;
    --signature-file)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --base)
        :
        return;;
    --interdiff)
        :
        return;;
    --range-diff)
        :
        return;;
    --creation-factor)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-email-send ()
{
    local short_flags="'-b ' '-a ' '-G ' '-n ' '-N ' '-v ' '-h '"
    local long_flags="--branch= '--all ' --git-opt= --from= --to= --cc= --bcc= --subject= --reply-to= --in-reply-to= '--compose ' '--annotate ' --identity= '--no-thread ' --confirm= '--quiet ' '--dry-run ' '--dump-aliases ' '--numbered ' '--no-numbered ' --start-number= --reroll-count= '--rfc ' --subject-prefix= --color= '--help '"
    local options="--branch|-b|--git-opt|-G|--from|--to|--cc|--bcc|--subject|--reply-to|--in-reply-to|--identity|--confirm|--start-number|--reroll-count|-v|--subject-prefix|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--compose|--annotate|--no-thread|--quiet|--dry-run|--dump-aliases|--numbered|-n|--no-numbered|-N|--rfc|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --git-opt|-G)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_send_email_opts)" -- "$cur")
        return;;
    --from)
        :
        return;;
    --to)
        :
        return;;
    --cc)
        :
        return;;
    --bcc)
        :
        return;;
    --subject)
        :
        return;;
    --reply-to)
        :
        return;;
    --in-reply-to)
        :
        return;;
    --identity)
        :
        return;;
    --confirm)
        mapfile -t COMPREPLY < <(compgen -W "always never cc compose auto" -- "$cur")
        return;;
    --start-number)
        :
        return;;
    --reroll-count|-v)
        :
        return;;
    --subject-prefix)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        :
    fi
}

_stg-email-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="format send help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        format)
            cmd_index=$i
            _stg-email-help-format; return;;
        send)
            cmd_index=$i
            _stg-email-help-send; return;;
        help)
            cmd_index=$i
            _stg-email-help-help; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-email-help-format ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-email-help-send ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-email-help-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-export ()
{
    local short_flags="'-b ' '-d ' '-p ' '-e ' '-n ' '-t ' '-s ' '-O ' '-h '"
    local long_flags="--branch= --dir= '--patch ' --extension= '--numbered ' --template= '--stdout ' --diff-opt= --color= '--help '"
    local options="--branch|-b|--dir|-d|--extension|-e|--template|-t|--diff-opt|-O|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --patch|-p|--numbered|-n|--stdout|-s|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --dir|-d)
        mapfile -t COMPREPLY < <(compgen -o directory -A directory -- "$cur")
        return;;
    --extension|-e)
        :
        return;;
    --template|-t)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --diff-opt|-O)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_diff_opts)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-files ()
{
    local short_flags="'-s ' '-h '"
    local long_flags="'--stat ' '--bare ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --stat|-s|--bare|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-float ()
{
    local short_flags="'-S ' '-k ' '-h '"
    local long_flags="'--noapply ' --series= '--keep ' '--committer-date-is-author-date ' --color= '--help '"
    local options="--series|-S|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --noapply|--keep|-k|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --series|-S)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-fold ()
{
    local short_flags="'-t ' '-b ' '-p ' '-C ' '-h '"
    local long_flags="'--threeway ' --base= --strip= '--reject ' --color= '--help '"
    local options="--base|-b|--strip|-p|-C|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --threeway|-t|--reject|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --base|-b)
        :
        return;;
    --strip|-p)
        :
        return;;
    -C)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
    fi
}

_stg-goto ()
{
    local short_flags="'-k ' '-m ' '-h '"
    local long_flags="'--keep ' '--merged ' '--committer-date-is-author-date ' --conflicts= --color= '--help '"
    local options="--conflicts|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --keep|-k|--merged|-m|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_visible_patches)" -- "$cur")
    fi
}

_stg-hide ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-id ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-import ()
{
    local short_flags="'-m ' '-M ' '-S ' '-u ' '-n ' '-p ' '-t ' '-C ' '-3 ' '-i ' '-b ' '-e ' '-d ' '-s ' '-h '"
    local long_flags="'--mail ' '--mbox ' '--series ' '--url ' --name= --strip= --directory= '--stripname ' '--3way ' '--ignore ' '--replace ' --base= '--reject ' '--keep-cr ' '--message-id ' '--edit ' '--diff ' '--no-verify ' --signoff= --ack= --review= --author= --authname= --authemail= --authdate= '--committer-date-is-author-date ' --color= '--help '"
    local options="--name|-n|--strip|-p|--directory|-C|--base|-b|--message|--file|--signoff|-s|--ack|--review|--sign-by|--ack-by|--review-by|--author|--authname|--authemail|--authdate|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --mail|-m|--mbox|-M|--series|-S|--url|-u|--stripname|-t|--3way|-3|--ignore|-i|--replace|--reject|--keep-cr|--message-id|--edit|-e|--diff|-d|--no-verify|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --name|-n)
        :
        return;;
    --strip|-p)
        :
        return;;
    --directory)
        mapfile -t COMPREPLY < <(compgen -o directory -A directory -- "$cur")
        return;;
    -C)
        :
        return;;
    --base|-b)
        :
        return;;
    --message)
        :
        return;;
    --file)
        :
        return;;
    --signoff|-s)
        :
        return;;
    --ack)
        :
        return;;
    --review)
        :
        return;;
    --sign-by)
        :
        return;;
    --ack-by)
        :
        return;;
    --review-by)
        :
        return;;
    --author)
        :
        return;;
    --authname)
        :
        return;;
    --authemail)
        :
        return;;
    --authdate)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-init ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-log ()
{
    local short_flags="'-b ' '-d ' '-n ' '-f ' '-g ' '-h '"
    local long_flags="--branch= '--diff ' --number= '--full ' '--graphical ' '--clear ' --color= '--help '"
    local options="--branch|-b|--number|-n|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --diff|-d|--full|-f|--graphical|-g|--clear|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --number|-n)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-name ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= '--showbranch ' --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --showbranch|--no-showbranch|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-new ()
{
    local short_flags="'-n ' '-r ' '-i ' '-F ' '-e ' '-d ' '-m ' '-f ' '-s ' '-h '"
    local long_flags="--name= '--refresh ' '--index ' '--force ' '--submodules ' '--no-submodules ' '--edit ' '--diff ' --message= --file= '--no-verify ' --signoff= --ack= --review= --author= --authname= --authemail= --authdate= '--committer-date-is-author-date ' --save-template= --color= '--help ' '-- '"
    local options="--name|-n|--message|-m|--file|-f|--signoff|-s|--ack|--review|--sign-by|--ack-by|--review-by|--author|--authname|--authemail|--authdate|--save-template|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --refresh|-r|--index|-i|--force|-F|--submodules|--no-submodules|--edit|-e|--diff|-d|--no-verify|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --name|-n)
        :
        return;;
    --message|-m)
        :
        return;;
    --file|-f)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --signoff|-s)
        :
        return;;
    --ack)
        :
        return;;
    --review)
        :
        return;;
    --sign-by)
        :
        return;;
    --ack-by)
        :
        return;;
    --review-by)
        :
        return;;
    --author)
        :
        return;;
    --authname)
        :
        return;;
    --authemail)
        :
        return;;
    --authdate)
        :
        return;;
    --save-template)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi

    if (( pos_index >= 1 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-next ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-patches ()
{
    local short_flags="'-b ' '-d ' '-O ' '-h '"
    local long_flags="--branch= '--diff ' --diff-opt= --color= '--help '"
    local options="--branch|-b|--diff-opt|-O|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --diff|-d|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --diff-opt|-O)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_diff_opts)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-pick ()
{
    local short_flags="'-B ' '-r ' '-x ' '-n ' '-p ' '-f ' '-h '"
    local long_flags="--ref-branch= '--revert ' '--expose ' '--noapply ' --name= --parent= '--committer-date-is-author-date ' '--fold ' '--update ' --file= --color= '--help '"
    local options="--ref-branch|-B|--name|-n|--parent|-p|--file|-f|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --revert|-r|--expose|-x|--noapply|--committer-date-is-author-date|--fold|--update|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --ref-branch|-B)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --name|-n)
        :
        return;;
    --parent|-p)
        :
        return;;
    --file|-f)
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-pop ()
{
    local short_flags="'-a ' '-n ' '-s ' '-k ' '-h '"
    local long_flags="'--all ' --number= '--spill ' '--keep ' --color= '--help '"
    local options="--number|-n|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--spill|-s|--keep|-k|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_applied_patches)"
    fi
}

_stg-prev ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-pull ()
{
    local short_flags="'-n ' '-m ' '-h '"
    local long_flags="'--nopush ' '--merged ' --conflicts= --color= '--help '"
    local options="--conflicts|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --nopush|-n|--merged|-m|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi
}

_stg-push ()
{
    local short_flags="'-a ' '-n ' '-k ' '-m ' '-h '"
    local long_flags="'--all ' --number= '--reverse ' '--noapply ' '--set-tree ' '--keep ' '--merged ' '--committer-date-is-author-date ' --conflicts= --color= '--help '"
    local options="--number|-n|--conflicts|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--reverse|--noapply|--set-tree|--keep|-k|--merged|-m|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_unapplied_patches)"
    fi
}

_stg-rebase ()
{
    local short_flags="'-i ' '-n ' '-m ' '-h '"
    local long_flags="'--interactive ' '--nopush ' '--merged ' '--committer-date-is-author-date ' '--autostash ' --conflicts= --color= '--help '"
    local options="--conflicts|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --interactive|-i|--nopush|-n|--merged|-m|--committer-date-is-author-date|--autostash|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches) $(_tags) $(_remotes)" -- "$cur")
    fi
}

_stg-redo ()
{
    local short_flags="'-n ' '-h '"
    local long_flags="--number= '--hard ' --color= '--help '"
    local options="--number|-n|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --hard|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-refresh ()
{
    local short_flags="'-u ' '-i ' '-F ' '-p ' '-a ' '-e ' '-d ' '-m ' '-f ' '-s ' '-h '"
    local long_flags="'--update ' '--index ' '--force ' --conflicts= --patch= --annotate= '--submodules ' '--no-submodules ' '--edit ' '--diff ' --message= --file= '--no-verify ' --signoff= --ack= --review= --author= --authname= --authemail= --authdate= '--committer-date-is-author-date ' --color= '--help '"
    local options="--conflicts|--patch|-p|--annotate|-a|--message|-m|--file|-f|--signoff|-s|--ack|--review|--sign-by|--ack-by|--review-by|--author|--authname|--authemail|--authdate|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --update|-u|--index|-i|--force|-F|--submodules|--no-submodules|--spill|--edit|-e|--diff|-d|--no-verify|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --conflicts)
        mapfile -t COMPREPLY < <(compgen -W "allow disallow" -- "$cur")
        return;;
    --patch|-p)
        mapfile -t COMPREPLY < <(compgen -W "$(_visible_patches)" -- "$cur")
        return;;
    --annotate|-a)
        :
        return;;
    --message|-m)
        :
        return;;
    --file|-f)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --signoff|-s)
        :
        return;;
    --ack)
        :
        return;;
    --review)
        :
        return;;
    --sign-by)
        :
        return;;
    --ack-by)
        :
        return;;
    --review-by)
        :
        return;;
    --author)
        :
        return;;
    --authname)
        :
        return;;
    --authemail)
        :
        return;;
    --authdate)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-rename ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        :
    fi
}

_stg-repair ()
{
    local short_flags="'-h '"
    local long_flags="--color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-reset ()
{
    local short_flags="'-h '"
    local long_flags="'--hard ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --hard|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index == 0 )); then
        mapfile -t COMPREPLY < <(compgen -W "$(_all_branches) $(_tags) $(_remotes)" -- "$cur")
    fi

    if (( pos_index >= 1 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-series ()
{
    local short_flags="'-b ' '-a ' '-s ' '-A ' '-U ' '-H ' '-m ' '-c ' '-i ' '-d ' '-e ' '-P ' '-I ' '-O ' '-r ' '-h '"
    local long_flags="--branch= '--all ' --short= '--applied ' '--unapplied ' '--hidden ' --missing= '--author ' '--count ' --commit-id= '--description ' '--empty ' '--no-prefix ' '--indices ' '--offsets ' '--reverse ' '--showbranch ' --color= '--help '"
    local options="--branch|-b|--short|-s|--missing|-m|--commit-id|-i|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--applied|-A|--unapplied|-U|--hidden|-H|--author|--no-author|--count|-c|--no-commit-id|--description|-d|--no-description|--empty|-e|--no-empty|--prefix|--no-prefix|-P|--indices|-I|--no-indices|--offsets|-O|--no-offsets|--reverse|-r|--no-reverse|--showbranch|--no-showbranch|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --short|-s)
        :
        return;;
    --missing|-m)
        :
        return;;
    --commit-id|-i)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_all_patches)"
    fi
}

_stg-show ()
{
    local short_flags="'-p ' '-b ' '-s ' '-O ' '-A ' '-U ' '-H ' '-h '"
    local long_flags="--patch= --branch= '--stat ' --diff-opt= '--applied ' '--unapplied ' '--hidden ' --color= '--help ' '-- '"
    local options="--patch|-p|--branch|-b|--diff-opt|-O|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --stat|-s|--applied|-A|--unapplied|-U|--hidden|-H|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --patch|-p)
        _patch_range "$(_visible_patches)"
        return;;
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --diff-opt|-O)
        mapfile -t COMPREPLY < <(compgen -W "$(_git_diff_opts)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_all_patches)"
    fi

    if (( pos_index >= 1 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-sink ()
{
    local short_flags="'-n ' '-t ' '-T ' '-k ' '-h '"
    local long_flags="'--nopush ' --below= --to= --above= '--keep ' '--committer-date-is-author-date ' --color= '--help '"
    local options="--below|--to|-t|--above|-T|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --nopush|-n|--keep|-k|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --below|--to|-t)
        :
        return;;
    --above|-T)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-spill ()
{
    local short_flags="'-a ' '-r ' '-h '"
    local long_flags="--annotate= '--reset ' '--committer-date-is-author-date ' --color= '--help '"
    local options="--annotate|-a|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --reset|-r|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --annotate|-a)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        mapfile -t COMPREPLY < <(compgen -o default -- "$cur")
    fi
}

_stg-squash ()
{
    local short_flags="'-n ' '-e ' '-d ' '-m ' '-f ' '-s ' '-h '"
    local long_flags="--name= '--edit ' '--diff ' --message= --file= '--no-verify ' --signoff= --ack= --review= --author= --authname= --authemail= --authdate= '--committer-date-is-author-date ' --save-template= --color= '--help '"
    local options="--name|-n|--message|-m|--file|-f|--signoff|-s|--ack|--review|--sign-by|--ack-by|--review-by|--author|--authname|--authemail|--authdate|--save-template|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --edit|-e|--diff|-d|--no-verify|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --name|-n)
        :
        return;;
    --message|-m)
        :
        return;;
    --file|-f)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --signoff|-s)
        :
        return;;
    --ack)
        :
        return;;
    --review)
        :
        return;;
    --sign-by)
        :
        return;;
    --ack-by)
        :
        return;;
    --review-by)
        :
        return;;
    --author)
        :
        return;;
    --authname)
        :
        return;;
    --authemail)
        :
        return;;
    --authdate)
        :
        return;;
    --save-template)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-sync ()
{
    local short_flags="'-a ' '-B ' '-S ' '-h '"
    local long_flags="'--all ' --ref-branch= --series= '--committer-date-is-author-date ' --color= '--help '"
    local options="--ref-branch|-B|--series|-S|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --all|-a|--committer-date-is-author-date|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --ref-branch|-B)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --series|-S)
        mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_visible_patches)"
    fi
}

_stg-top ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-uncommit ()
{
    local short_flags="'-n ' '-t ' '-x ' '-h '"
    local long_flags="--number= --to= '--exclusive ' --color= '--help '"
    local options="--number|-n|--to|-t|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --exclusive|-x|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --to|-t)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        :
    fi
}

_stg-undo ()
{
    local short_flags="'-n ' '-h '"
    local long_flags="--number= '--hard ' --color= '--help '"
    local options="--number|-n|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --hard|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --number|-n)
        :
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-unhide ()
{
    local short_flags="'-b ' '-h '"
    local long_flags="--branch= --color= '--help '"
    local options="--branch|-b|--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --branch|-b)
        mapfile -t COMPREPLY < <(compgen -W "$(_stg_branches)" -- "$cur")
        return;;
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    if (( pos_index >= 0 )); then
        _patch_range "$(_hidden_patches)"
    fi
}

_stg-version ()
{
    local short_flags="'-s ' '-h '"
    local long_flags="'--short ' --color= '--help '"
    local options="--color"
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --short|-s|--help|-h)
            ;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$prev" in
    --color)
        mapfile -t COMPREPLY < <(compgen -W "auto always ansi never" -- "$cur")
        return;;
    esac

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="branch clean commit completion delete diff edit email export files float fold goto hide id import init log name new next patches pick pop prev pull push rebase redo refresh rename repair reset series show sink spill squash sync top uncommit undo unhide version help"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        branch)
            cmd_index=$i
            _stg-help-branch; return;;
        clean)
            cmd_index=$i
            _stg-help-clean; return;;
        commit)
            cmd_index=$i
            _stg-help-commit; return;;
        completion)
            cmd_index=$i
            _stg-help-completion; return;;
        delete)
            cmd_index=$i
            _stg-help-delete; return;;
        diff)
            cmd_index=$i
            _stg-help-diff; return;;
        edit)
            cmd_index=$i
            _stg-help-edit; return;;
        email)
            cmd_index=$i
            _stg-help-email; return;;
        export)
            cmd_index=$i
            _stg-help-export; return;;
        files)
            cmd_index=$i
            _stg-help-files; return;;
        float)
            cmd_index=$i
            _stg-help-float; return;;
        fold)
            cmd_index=$i
            _stg-help-fold; return;;
        goto)
            cmd_index=$i
            _stg-help-goto; return;;
        hide)
            cmd_index=$i
            _stg-help-hide; return;;
        id)
            cmd_index=$i
            _stg-help-id; return;;
        import)
            cmd_index=$i
            _stg-help-import; return;;
        init)
            cmd_index=$i
            _stg-help-init; return;;
        log)
            cmd_index=$i
            _stg-help-log; return;;
        name)
            cmd_index=$i
            _stg-help-name; return;;
        new)
            cmd_index=$i
            _stg-help-new; return;;
        next)
            cmd_index=$i
            _stg-help-next; return;;
        patches)
            cmd_index=$i
            _stg-help-patches; return;;
        pick)
            cmd_index=$i
            _stg-help-pick; return;;
        pop)
            cmd_index=$i
            _stg-help-pop; return;;
        prev)
            cmd_index=$i
            _stg-help-prev; return;;
        pull)
            cmd_index=$i
            _stg-help-pull; return;;
        push)
            cmd_index=$i
            _stg-help-push; return;;
        rebase)
            cmd_index=$i
            _stg-help-rebase; return;;
        redo)
            cmd_index=$i
            _stg-help-redo; return;;
        refresh)
            cmd_index=$i
            _stg-help-refresh; return;;
        rename)
            cmd_index=$i
            _stg-help-rename; return;;
        repair)
            cmd_index=$i
            _stg-help-repair; return;;
        reset)
            cmd_index=$i
            _stg-help-reset; return;;
        series)
            cmd_index=$i
            _stg-help-series; return;;
        show)
            cmd_index=$i
            _stg-help-show; return;;
        sink)
            cmd_index=$i
            _stg-help-sink; return;;
        spill)
            cmd_index=$i
            _stg-help-spill; return;;
        squash)
            cmd_index=$i
            _stg-help-squash; return;;
        sync)
            cmd_index=$i
            _stg-help-sync; return;;
        top)
            cmd_index=$i
            _stg-help-top; return;;
        uncommit)
            cmd_index=$i
            _stg-help-uncommit; return;;
        undo)
            cmd_index=$i
            _stg-help-undo; return;;
        unhide)
            cmd_index=$i
            _stg-help-unhide; return;;
        version)
            cmd_index=$i
            _stg-help-version; return;;
        help)
            cmd_index=$i
            _stg-help-help; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-help-branch ()
{
    local short_flags=""
    local long_flags="'--list ' '--create ' '--clone ' '--rename ' '--protect ' '--unprotect ' '--delete ' '--cleanup ' '--describe '"
    local options=""
    local pos_index=0
    local __subcommands="--list --create --clone --rename --protect --unprotect --delete --cleanup --describe"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --list)
            cmd_index=$i
            _stg-help-branch---list; return;;
        --create)
            cmd_index=$i
            _stg-help-branch---create; return;;
        --clone)
            cmd_index=$i
            _stg-help-branch---clone; return;;
        --rename)
            cmd_index=$i
            _stg-help-branch---rename; return;;
        --protect)
            cmd_index=$i
            _stg-help-branch---protect; return;;
        --unprotect)
            cmd_index=$i
            _stg-help-branch---unprotect; return;;
        --delete)
            cmd_index=$i
            _stg-help-branch---delete; return;;
        --cleanup)
            cmd_index=$i
            _stg-help-branch---cleanup; return;;
        --describe)
            cmd_index=$i
            _stg-help-branch---describe; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-help-branch---list ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---create ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---clone ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---rename ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---protect ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---unprotect ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---delete ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---cleanup ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-branch---describe ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-clean ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-commit ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="bash fish zsh list man"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        bash)
            cmd_index=$i
            _stg-help-completion-bash; return;;
        fish)
            cmd_index=$i
            _stg-help-completion-fish; return;;
        zsh)
            cmd_index=$i
            _stg-help-completion-zsh; return;;
        list)
            cmd_index=$i
            _stg-help-completion-list; return;;
        man)
            cmd_index=$i
            _stg-help-completion-man; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-help-completion-bash ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-fish ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-zsh ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-list ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="aliases commands commands-and-aliases"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        aliases)
            cmd_index=$i
            _stg-help-completion-list-aliases; return;;
        commands)
            cmd_index=$i
            _stg-help-completion-list-commands; return;;
        commands-and-aliases)
            cmd_index=$i
            _stg-help-completion-list-commands-and-aliases; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-help-completion-list-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-list-commands ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-list-commands-and-aliases ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-completion-man ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-delete ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-diff ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-edit ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-email ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0
    local __subcommands="format send"

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        format)
            cmd_index=$i
            _stg-help-email-format; return;;
        send)
            cmd_index=$i
            _stg-help-email-send; return;;
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac

    mapfile -t COMPREPLY < <(compgen -S ' ' -W "$__subcommands" -- "$cur")
}

_stg-help-email-format ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-email-send ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-export ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-files ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-float ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-fold ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-goto ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-hide ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-id ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-import ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-init ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-log ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-name ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-new ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-next ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-patches ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-pick ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-pop ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-prev ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-pull ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-push ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-rebase ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-redo ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-refresh ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-rename ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-repair ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-reset ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-series ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-show ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-sink ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-spill ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-squash ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-sync ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-top ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-uncommit ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-undo ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-unhide ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-version ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg-help-help ()
{
    local short_flags=""
    local long_flags=""
    local options=""
    local pos_index=0

    local i
    for ((i=cmd_index+1; i <= cword; i++)); do
        if [[ "${words[i-1]}" == @($options) ]]; then
            continue
        fi
        case "${words[i]}" in
        --)
            if (( i < cword )); then
                return
            fi
            ;;
        *)
            if (( i < cword )); then
                (( pos_index++ ))
            fi
            ;;
        esac
    done

    case "$cur" in
    --*)
        mapfile -t COMPREPLY < <(compgen -W "${long_flags[*]}" -- "$cur")
        return;;
    -*)
        mapfile -t COMPREPLY < <(compgen -W "${short_flags[*]} ${long_flags[*]}" -- "$cur")
        return;;
    esac
}

_stg ()
{
    # Use bash-completion helper to clean-up the bash-builtin COMP_WORD, COMP_CWORD,
    # etc. variables.
    local cur prev words cword split
    _init_completion -s || return

    local i
    local command
    local cmd_index
    local __C_args C_args_count=0

    for ((i=1; i < cword; i++)); do
        case "${words[i]}" in
        --color=*)
            ;;
        --color)
            ((i++))
            ;;
        -C)
            __C_args[C_args_count++]=-C
            ((i++))
            __C_args[C_args_count++]="${words[i]}"
            ;;
        *)
            command="${words[i]}"
            cmd_index=$i
            break;;
        esac
    done

    if [ -z "${command-}" ]; then
        case "$prev" in
        -C)
            mapfile -t COMPREPLY < <(compgen -A directory -- "$cur")
            return;;
        --color)
            mapfile -t COMPREPLY < <(compgen -W "always ansi auto never" -- "$cur")
            return;;
        esac

        case "$cur" in
        --*)
            mapfile -t COMPREPLY < <(compgen -W "'--version ' --color= '--help " -- "$cur")
            ;;
        *)
            mapfile -t COMPREPLY < <(compgen -S ' ' -W "$(__stg completion list commands-and-aliases)" -- "$cur")
            ;;
        esac
        return
    fi

    local command_completion_func="_stg-${command}"
    if [ "$(type -t "$command_completion_func")" = function ]; then
        $command_completion_func && return
    else
        local a
        for a in $(__stg completion list aliases --style=zsh --show-expansion); do
            local name expansion
            name=${a%%:*}
            expansion=${a#*:}
            if [[ "$command" == "$name" ]]; then
                if [[ ${expansion:0:1} == "!" ]]; then
                    # Shell alias, fallback to simple filename completion
                    mapfile -t COMPREPLY < <(compgen -o filenames -A file -- "$cur")
                else
                    # StGit alias
                    command_completion_func="_stg-${expansion%% *}"
                    if [ "$(type -t "$command_completion_func")" = function ]; then
                        $command_completion_func && return
                    fi
                fi
                return
            fi
        done
    fi
}

complete -o nospace -F _stg stg
