# shellcheck shell=dash
# This is the place we implement regulation of commit log

___x_cmd_git_checkout(){
    if [ "$#" -eq 1 ] && [ "$1" = - ]; then     ___x_cmd_git_checkout_back
    else                                        ___x_cmd___git_origin checkout "$@"
    fi
}

___x_cmd_git_checkout_back(){
    ___x_cmd___git_origin reflog --format='%gs' | ___x_cmd_cmds_awk -v err=0 '
        match($0, "checkout:[\ ]moving[\ ]from[\ ]"){
            $0 = substr($0, RSTART + RLENGTH)
            gsub(/(^[ ]+)|([ ]+$)/, "", $0)
            if (split($0, arr, " to ") != 2) { err = 1; exit; }
            if (arr[1] == arr[2]) next
            print arr[1]
            exit
        }
    ' | {
        while read -r last_branch; do
            git:info "checking out to last branch ==> [$last_branch]"
            ___x_cmd___git_origin checkout "$last_branch"
            return
        done

        git:error "last branch not found"
        return 1
    }
}
