# (C) 2011-2025 magicant

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

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

function completion/git::merge:arg {

        OPTIONS=( #>#
        "--abort; reset to the state before starting merge"
        "--continue; resume the ongoing merge"
        "F: --file:; specify a file containing the message"
        "--into-name:; specify the branch name to merge into"
        "m:; specify the message"
        "--no-overwrite-ignore; abort if ignored files would be overwritten"
        "--no-rerere-autoupdate; disable the rerere mechanism"
        "--overwrite-ignore; overwrite ignored files"
        "--quit; end the merge and keep the current state"
        "--rerere-autoupdate; enable the rerere mechanism"
        ) #<#
        command -f completion/git::merge:getopt

        command -f completion//parseoptions
        case $ARGOPT in
                (-)
                        command -f completion//completeoptions
                        ;;
                ('')
                        command -f completion/git::completeref
                        ;;
                (*)
                        command -f completion/git::merge:compopt
                        ;;
        esac

}

function completion/git::merge:getopt {
        OPTIONS=("$OPTIONS" #>#
        "--allow-unrelated-histories; allow merging branches without a common ancestor"
        "--autostash; stash local changes before merging"
        "--cleanup:; specify the way the message is cleaned up"
        "--commit; commit the merge result automatically"
        "--continue; continue the current merge"
        "e --edit; edit the commit message"
        "--ff; fast-forward if possible"
        "--ff-only; allow fast-forward only"
        "S:: --gpg-sign::; sign commits using GPG"
        "--log::; specify the number of commits in the merged branch of which the messages are reused"
        "--no-autostash; cancel the --autostash option"
        "--no-commit; don't commit the merge result automatically"
        "--no-edit; accept the default commit message without editing"
        "--no-ff; don't fast-forward even if possible"
        "--no-gpg-sign; don't sign commits using GPG"
        "--no-log; don't reuse messages from the commits on the merged branch"
        "--no-progress; don't print progress info"
        "--no-signoff; don't add a \"signed-off-by\" line to the message"
        "--no-squash; cancel the --squash option"
        "n --no-stat --no-summary; don't print a diffstat"
        "--no-verify; bypass the pre-merge and commit-msg hooks"
        "--no-verify-signatures; don't verify the signatures of the commits to be merged"
        "--progress; print progress info"
        "q --quiet; don't print anything"
        "--signoff; add a \"signed-off-by\" line to the message"
        "--squash; like --no-commit, but don't set MERGE_HEAD"
        "--stat --summary; print a diffstat"
        "s: --strategy:; specify the merge strategy"
        "X: --strategy-option:; specify a strategy-specific option"
        "v --verbose; output additional information"
        "--verify-signatures; verify the signatures of the commits to be merged"
        ) #<#
}

function completion/git::merge:compopt
        case $ARGOPT in
                (--cleanup)
                        command -f completion/git::--cleanup:arg
                        ;;
                (F|--file)
                        complete -P "$PREFIX" -f
                        ;;
                (S|--gpg-sign)
                        command -f completion/git::--gpg-sign:arg
                        ;;
                (m|--log)
                        ;;
                (s|--strategy) #>>#
                        complete -P "$PREFIX" -D "standard algorithm for merging more than 2 heads" octopus
                        complete -P "$PREFIX" -D "simple algorithm that makes no change" ours
                        complete -P "$PREFIX" -D "recursive 3-way merge" recursive
                        complete -P "$PREFIX" -D "safe and fast 3-way merge" resolve
                        complete -P "$PREFIX" -D "recursive 3-way merge with subtree matching" subtree
                        ;; #<<#
                (X|--strategy-option)
                        typeset i=2 strategy=recursive
                        while [ $i -le ${WORDS[#]} ]; do
                                case ${WORDS[i]} in
                                        (-s*)
                                                strategy=${WORDS[i]#-s}
                                                ;;
                                        (--strategy=*)
                                                strategy=${WORDS[i]#--strategy=}
                                                ;;
                                        (--)
                                                break
                                                ;;
                                esac
                                i=$((i+1))
                        done
                        case $strategy in
                                (recursive) #>>#
                                        complete -P "$PREFIX" -D "resolve conflicts by discarding remote changes" ours
                                        complete -P "$PREFIX" -D "resolve conflicts by discarding local changes" theirs
                                        complete -P "$PREFIX" -D "use the patience diff algorithm" patience
                                        complete -P "$PREFIX" -D "discard conflicting changes of the number of spaces" ignore-space-change
                                        complete -P "$PREFIX" -D "discard conflicting changes of spaces" ignore-all-space
                                        complete -P "$PREFIX" -D "discard conflicting changes of spaces at end of line" ignore-space-at-eol
                                        complete -P "$PREFIX" -D "run hooks to obtain trees to merge" renormalize
                                        complete -P "$PREFIX" -D "don't run hooks to obtain trees to merge" no-renormalize
                                        complete -P "$PREFIX" -D "specify the threshold for detecting renames" -T rename-threshold=
                                        complete -P "$PREFIX" -D "match directory trees before merging" subtree
                                        ;; #<<#
                        esac
                        ;;
                (*)
                        return 1
                        ;;
        esac


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