# (C) 2013-2025 magicant

# Completion script for the "git-submodule" command.
# Supports Git 2.48.1.

function completion/git-submodule {
        WORDS=(git submodule "${WORDS[2,-1]}")
        command -f completion//reexecute
}

function completion/git::submodule:arg {

        OPTIONS=( #>#
        "q --quiet; print error messages only"
        ) #<#

        while case "${WORDS[2]}" in (--cached|-q|--quiet) true ;; (*) false ;; esac; do
                WORDS=("${WORDS[1]}" "${WORDS[3,-1]}")
        done

        if [ ${WORDS[#]} -le 1 ]; then
                case ${TARGETWORD#"$PREFIX"} in
                        (-q)  # avoid completing "-q" to "-qq"
                                complete -P "$PREFIX" -- -q
                                ;;
                        (-*)
                                OPTIONS=("$OPTIONS" #>#
                                "--cached; print status of the superproject index"
                                ) #<#
                                command -f completion//parseoptions
                                command -f completion//completeoptions
                                ;;
                        (*) #>>#
                                complete -P "$PREFIX" -D "move the git directories from submodules to the superproject" absorbgitdirs
                                complete -P "$PREFIX" -D "add a submodule" add
                                complete -P "$PREFIX" -D "unregister submodules" deinit
                                complete -P "$PREFIX" -D "execute a shell command in each submodule" foreach
                                complete -P "$PREFIX" -D "configure submodules according to .gitmodules" init
                                complete -P "$PREFIX" -D "set the default tracking branch for a submodule" set-branch
                                complete -P "$PREFIX" -D "set the URL of a submodule" set-url
                                complete -P "$PREFIX" -D "show status of submodules" status
                                complete -P "$PREFIX" -D "print summary of changes in submodules" summary
                                complete -P "$PREFIX" -D "reset submodule remote URL" sync
                                complete -P "$PREFIX" -D "clone and check out submodules" update
                                ;; #<<#
                esac
        else
                WORDS=("${WORDS[2,-1]}")
                if command -vf "completion/git::submodule:${WORDS[1]}:arg" >/dev/null 2>&1; then
                        command -f "completion/git::submodule:${WORDS[1]}:arg"
                fi
        fi

}

function completion/git::submodule:absorbgitdirs:arg {

        complete -P "$PREFIX" -S / -T -d

}

function completion/git::submodule:add:arg {

        OPTIONS=("$OPTIONS" #>#
        "b: --branch:; specify a branch of the submodule to clone"
        "--depth:; specify the max number of history to clone"
        "--dissociate; stop borrowing objects after cloning"
        "f --force; add an ignored path"
        "--name:; specify the name of the submodule"
        "--progress; force progress reporting"
        "--reference:; specify a reference repository to share objects (possibly dangerous operation)"
        "--ref-format:; specify a ref storage format"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions -e
                        ;;
#               (b|--branch)
#                       ;;
#               (--name)
#                       ;;
                (--reference)
                        complete -P "$PREFIX" -S / -T -d
                        ;;
                (--ref-format)
                        command -f completion/git::--ref-format:arg
                        ;;
                ('')
                        command -f completion//getoperands
                        if command -vf  completion/git::clone:opr >/dev/null 2>&1 ||
                                        . -AL completion/git-clone; then
                                command -f completion/git::clone:opr
                        fi
                        ;;
        esac

}

function completion/git::submodule:deinit:arg {

        OPTIONS=("$OPTIONS" #>#
        "--all; unregister all submodules"
        "f --force; overwrite local changes or ignore unmerged files"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}

function completion/git::submodule:foreach:arg {

        OPTIONS=("$OPTIONS" #>#
        "--recursive; operate on all submodules recursively"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        command -f completion//getoperands
                        command -f completion//reexecute -e
                        ;;
        esac

}

function completion/git::submodule:init:arg {

        complete -P "$PREFIX" -S / -T -d

}

function completion/git::submodule:set-branch:arg {

        OPTIONS=("$OPTIONS" #>#
        "b: --branch:; specify a branch of the submodule to track"
        "d --default; reset to the remote's default branch"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}

function completion/git::submodule:set-url:arg {

        complete -P "$PREFIX" -S / -T -d

}

function completion/git::submodule:status:arg {

        OPTIONS=("$OPTIONS" #>#
        "--cached; print status of the superproject index"
        "--recursive; operate on all submodules recursively"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}

function completion/git::submodule:summary:arg {

        OPTIONS=("$OPTIONS" #>#
        "--cached; compare the superproject HEAD and index"
        "--files; compare the superproject index with the submodule HEAD"
        "n: --summary-limit:; specify the max number of commits to show"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
#               (n|--summary-limit)
#                       ;;
                ('')
                        command -f completion//getoperands
                        if [ ${WORDS[#]} -eq 0 ]; then
                                command -f completion/git::completeref
                        fi
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}

function completion/git::submodule:sync:arg {

        OPTIONS=("$OPTIONS" #>#
        "--recursive; operate on all submodules recursively"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}

function completion/git::submodule:update:arg {

        OPTIONS=("$OPTIONS" #>#
        "--checkout; check out in submodules"
        "--depth:; specify the max number of history to clone"
        "--dissociate; stop borrowing objects after cloning"
        "--filter:; partial-clone by applying a filter"
        "f --force; overwrite local changes or ignore unmerged files"
        "--init; do \"git submodule init\" before updating"
        "--jobs:; specify the max number of submodules to clone in parallel"
        "--merge; merge in submodules"
        "N --no-fetch; don't fetch from submodule remotes"
        "--no-recommend-shallow; always fully clone submodules"
        "--no-single-branch; clone all branches"
        "--progress; force progress reporting"
        "--rebase; rebase in submodules"
        "--recommend-shallow; shallow-clone if recommended in .gitmodules"
        "--recursive; operate on all submodules recursively"
        "--reference:; specify a reference repository to share objects (possibly dangerous operation)"
        "--ref-format:; specify a ref storage format"
        "--remote; update submodules to their remote's latest commit"
        "--single-branch; clone only the target branch"
        ) #<#

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                (--filter|--ref-format)
                        command -f "completion/git::$ARGOPT:arg"
                        ;;
                (--reference|'')
                        complete -P "$PREFIX" -S / -T -d
                        ;;
        esac

}


# vim: set ft=sh ts=8 sts=8 sw=8 et:
