function __git_prompt_git() {
  GIT_OPTIONAL_LOCKS=0 command git "$@"
}

function __git_sync_info() {
  local behind
  local ahead
  if git rev-parse --is-inside-work-tree &>/dev/null; then
    local branch=$(git_current_branch)
    IFS=: read behind ahead <<<"$(git rev-list --left-right --count ${branch}...origin/${branch} 2>/dev/null | sed 's|^\([0-9]\+\)\s\+\([0-9]\+\)|\1:\2|')"
    if [[ "${ahead}" != "0" ]] || [[ "${behind}" != "0" ]]; then
      echo "%{$fg[yellow]%}%1{±%}"
    fi
  fi
}

function git_prompt_info() {
  if ! __git_prompt_git rev-parse --git-dir &> /dev/null ; then
    return 0
  fi
  echo "%{$reset_color%}\ue725 on %{$fg[green]%}$(git_current_branch)$(__git_sync_info)%{$reset_color%} "
}

function git_current_branch() {
  local ref
  ref=$(__git_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
  local ret=$?
  if [[ $ret != 0 ]]; then
    [[ $ret == 128 ]] && return  # no git repo.
    ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
  fi
  echo ${ref#refs/heads/}
}