# shellcheck shell=dash

___x_cmd_git_is(){
    local op="$1"; shift
    case "$op" in
        headless|shallow|gitdir|empty)
            "___x_cmd_git_is_${op}" "$@"
            ;;
        -h|--help)
            ___x_cmd_git___help is; return;;
    esac
}

___x_cmd_git_is_headless(){
    ! ___x_cmd___git_origin rev-parse --quiet --verify HEAD >/dev/null 2>&1
}

___x_cmd_git_is_shallow(){
    [ "$(___x_cmd___git_origin rev-parse --is-shallow-repository 2>/dev/null)" = true ]
}

___x_cmd_git_is_gitdir(){
    [ "$(___x_cmd___git_origin rev-parse --is-inside-git-dir 2>/dev/null)" = true ]
}

___x_cmd_git_is_emptylocal(){
    # TODO: There is problem
    ___x_cmd___git_origin branch -r | command grep -q ^
}

___x_cmd_git_is_empty(){
    # TODO: There is problem
    [ -z "$(___x_cmd___git_origin branch -r)" ]
}

___x_cmd_git_is_push(){
    command git for-each-ref --format='%(refname:short)
%(upstream:short)' refs/heads | xargs command git rev-parse | ___x_cmd_cmds_awk '
NR%2==1{ hist = $0; next; }
{ if (hist != $0) exit(1) }
'
}

___x_cmd_git_meta_unpush(){
    local data; data=$(command git for-each-ref --format='%(refname:short)
%(upstream:short)' refs/heads)

    {
        xargs command git rev-parse | data="$data" ___x_cmd_cmds_awk '
            BEGIN{
                split(ENVIRON["data"], arr, "\n")
            }
            NR%2==1{ hist = $0; next; }
            { if (hist != $0) print arr[NR-1] " " arr[NR] }
        '
    } <<A
$data
A

}
