Skip to content

Instantly share code, notes, and snippets.

@mathebox
Last active August 28, 2024 22:00
Show Gist options
  • Save mathebox/97baec76bb1fd34710be to your computer and use it in GitHub Desktop.
Save mathebox/97baec76bb1fd34710be to your computer and use it in GitHub Desktop.
This is my custom git prompt for oh_my_zsh. It shows to current branch and the git dirty state (color). If a upstream exists, the difference is also displayed.
function git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
ZSH_THEME_GIT_PROMPT_PREFIX="("
ZSH_THEME_GIT_PROMPT_SUFFIX=")$reset_color "
ZSH_THEME_GIT_PROMPT_DIRTY="$fg[red]"
ZSH_THEME_GIT_PROMPT_CLEAN=""
local oh_my_git_string="";
local has_upstream=false;
oh_my_git_string+="$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(git_current_branch)"
local upstream=$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} 2> /dev/null)
if [[ -n "${upstream}" && "${upstream}" != "@{upstream}" ]]; then
local has_upstream=true;
else
local has_upstream=false;
fi
local current_commit_hash=$(git rev-parse HEAD 2> /dev/null)
if [[ $has_upstream == true ]]; then
local commits_diff="$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null)"
local commits_ahead=$(grep -c "^<" <<< "$commits_diff");
local commits_behind=$(grep -c "^>" <<< "$commits_diff");
fi
if [[ $has_upstream == true ]]; then
if [[ ${commits_behind} -gt 0 ]]; then
oh_my_git_string+="<";
fi
if [[ ${commits_ahead} -gt 0 ]]; then
oh_my_git_string+=">";
fi
if [[ ${commits_behind} -eq 0 && ${commits_ahead} -eq 0 ]]; then
oh_my_git_string+="=";
fi
fi
oh_my_git_string+="$ZSH_THEME_GIT_PROMPT_SUFFIX"
echo $oh_my_git_string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment