# shellcheck shell=dash

___x_cmd_git_update(){
    local url="$1"
    local name="$2"

    [ -n "$name" ] || {
        name="${url##*/}"
        name="${name%.git}"
    }

    git:info --url "$url" --name "$name" "git update repo"

    if [ -d "$name" ]; then (
        ___x_cmd_cmds cd "${name}" || return
        if [ "$(___x_cmd___git_origin status -s | ___x_cmd_cmds wc -l)" -ne 0 ]; then
            ___x_cmd___git_origin status -s
            ___x_cmd___git_origin fetch
            return 1
            # if ! x ui yesno "Files modfiled in repo, perform stash before pull"; then
            #     x:error "You must stash before pull. Exit."
            #     return 1
            # fi
            # command git stash
            # command git clean -fd
        fi

        ___x_cmd___git_origin pull origin "$(x git meta branch)"
    ) else (
        ___x_cmd___git_origin clone "$url" "${name}"
    ) fi
}

# ___x_cmd_git_sync_run(){
#     ___x_cmd_git_meta_isclean || {
#         [ -n "$clean" ] || {
#             git:error "git-sync: not clean"
#             return 1
#         }

#         if ___x_cmd_runmode_allow_manual; then  ___x_cmd___git_origin -i   -f -d
#         else                              ___x_cmd___git_origin      -f -d
#         fi
#         ___x_cmd___git_origin stash
#     }

#     ___x_cmd___git_origin fetch "$origin"
#     ___x_cmd___git_origin pull --all --tags

#     local curbranch; curbranch="$(x git meta branch)"

#     [ "$#" -eq 0 ] || {
#         git:info "setting [current-branch=$branch] to update"
#         set -- "$@" "$curbranch"
#     }

#     local branch; for branch in "$@"; do
#         git:info "updating [branch=$branch]"
#         ___x_cmd___git_origin checkout "$branch"
#         ___x_cmd___git_origin pull "$origin" "$branch"
#         ___x_cmd___git_origin push "$origin" "$branch"
#     done

#     git:info "recover to the [branch=$branch] before updating"
#     ___x_cmd___git_origin checkout "$curbranch"
# }

