# shellcheck shell=dash

___x_cmd_git_rootout(){
    local AUTOFORCE="${AUTOFORCE}"
    while true; do
        case "$1" in
            --force|-y)     AUTOFORCE=1; shift ;;
            *)              break
        esac
    done

    local fp; for fp in "$@"; do
        ___x_cmd_git_rootout___one "$fp"
    done
}

___x_cmd_git_rootout___one(){
    git:warn  "Rootout the file with all of its history: ${1:?Provide file path}"
    ! ___x_cmd___git_origin filter-branch --index-filter "git rm -r --cached --ignore-unmatch ${1}" \
        --prune-empty --tag-name-filter cat -- --all || return 0

    if [ -z "$AUTOFORCE" ]; then
        x ui yesno "Fail to rootout $1. Do you want to root it out with force?" || {
            git:error "Fail to rootout $1 without force."
            return 1
        }
    fi

    git:error "Fail to rootout. Try to do it in force."  # Ask whether to proceed.
    ___x_cmd___git_origin filter-branch --force --index-filter "git rm -r --cached --ignore-unmatch ${1}" \
        --prune-empty --tag-name-filter cat -- --all
}
