# bashrc extra functions written for AOSC OS, package "git"

# Aliases, mostly from http://blog.sina.com.cn/s/blog_630c58cb01011uid.html
alias g="git status"
alias ga="git add"
alias gaa="git add ."
alias gau="git add -u"
alias gct="git commit"
alias gcm="git commit -m"
alias gca="git commit -am"
alias gb="git branch"
alias gbd="git branch -d"
alias gco="git checkout"
alias gcob="git checkout -b"
alias gt="git stash"
alias gta="git stash apply"
alias gmg="git merge"
alias gr="git rebase"
alias gl="git log --oneline --decorate"
alias gsh="git show"
alias gd="git diff --ws-error-highlight all"
alias gdca="git diff --ws-error-highlight all --cached"
alias gbl="git blame"
alias gps="git push"
alias gpl="git pull"
alias gcp="git cherry-pick"

# Prompt
if command -v bash-git-status 2>&1>/dev/null; then
        alias __ps1_git_branch="bash-git-status"
else
        alias __ps1_git_branch="git branch 2>/dev/null | grep '*' | sed 's/*\ //g'"
fi

_ps1_git_status() {
        if command -v bash-git-status 2>&1>/dev/null; then
                local bgs
                bgs=$(bash-git-status)
                case "$?" in
                        1)
                                return 1
                        ;;
                        5)
                                echo -e "\01\e[1m\02@\01\e[0;32m\0002$bgs\01\e[0m\02"
                        ;;
                        6)
                                echo -e "\01\e[1m\02@\01$IRED\0002$bgs\01\e[0m\02"
                        ;;
                        7)
                                echo -e "\01\e[1m\02@\01\e[0;35m\0002$bgs\01\e[0m\02"
                        ;;
                        8)
                                echo -e "\01\e[1m\02@\01\e[0;37m\0002$bgs\01\e[0m\02"
                        ;;
                esac
        else
                gbr=$(git branch 2>/dev/null) || return 1
                gbr=$(printf %s "$gbr" | grep '*' | sed 's/*\ //g')
                local gst
                gst=$(LC_ALL=C git status 2>&1)
                if printf %s "$gst" | grep -q "nothing to commit"; then
                        echo -e "\01\e[1m\02@\01\e[0;32m\0002$gbr\01\e[0m\02"
                elif printf %s "$gst" | grep -q "nothing added to commit"; then # Untracked
                        echo -e "\01\e[1m\02@\01\e[0;35m\0002$gbr\01\e[0m\02"
                elif printf %s "$gst" | grep -q "must be run in a work tree"; then # Not in work tree
                        echo -e "\01\e[1m\02@\01\e[0;37m\0002$gbr\01\e[0m\02"
                else  # Change not added/not merged yet
                        echo -e "\01\e[1m\02@\01$IRED\0002$gbr\01\e[0m\02"
                fi
        fi
}


# A shorter one without color
# alias _git_status='_st="$(__git_branch)"; ((!PIPESTATUS[0])) && echo "@$_st"'
# For users of the contrib/completion __git_ps1
# _git_status(){ git branch &>/dev/null || return 1; echo -n ' '; __git_ps1; }
