mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2025-12-26 02:12:33 +01:00
Make function definitions consistent. Copy current_branch from plugin and rename to git_current_branch. Exchange usage of current_branch with git_current_branch. Add helper function to get the current branch's remote. Update git_prompts_ahead to use git_current_remote instead of origin. Add git_prompt_behind.
82 lines
2.7 KiB
Bash
82 lines
2.7 KiB
Bash
# get the name of the branch we are on
|
|
function git_prompt_info() {
|
|
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
|
|
echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
|
|
}
|
|
|
|
# Checks if working tree is dirty
|
|
function parse_git_dirty() {
|
|
if [[ -n $(git status -s 2> /dev/null) ]]; then
|
|
echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
|
|
else
|
|
echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
|
|
fi
|
|
}
|
|
|
|
# Checks if there are commits ahead from remote
|
|
function git_prompt_ahead() {
|
|
if $(echo "$(git log $(git_current_remote)/$(git_current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
|
|
echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
|
|
fi
|
|
}
|
|
|
|
function git_prompt_behind() {
|
|
if $(echo "$(git log HEAD..$(git_current_remote)/$(git_current_branch) 2> /dev/null)" | grep '^commit' &> /dev/null); then
|
|
echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
|
|
fi
|
|
}
|
|
|
|
# Formats prompt string for current git commit short SHA
|
|
function git_prompt_short_sha() {
|
|
SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
|
|
}
|
|
|
|
# Formats prompt string for current git commit long SHA
|
|
function git_prompt_long_sha() {
|
|
SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
|
|
}
|
|
|
|
# Get the status of the working tree
|
|
function git_prompt_status() {
|
|
INDEX=$(git status --porcelain 2> /dev/null)
|
|
STATUS=""
|
|
if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
|
|
fi
|
|
if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
|
|
elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
|
|
fi
|
|
if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
|
|
elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
|
|
elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
|
|
fi
|
|
if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
|
|
fi
|
|
if $(echo "$INDEX" | grep '^D ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
|
|
fi
|
|
if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
|
|
STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
|
|
fi
|
|
echo $STATUS
|
|
}
|
|
|
|
function git_current_branch() {
|
|
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
|
|
echo ${ref#refs/heads/}
|
|
}
|
|
|
|
function git_current_remote() {
|
|
remote=$(git config --get "branch.$(git_current_branch).remote")
|
|
if [ -z "$remote" ]; then
|
|
echo 'origin'
|
|
else
|
|
echo "$remote"
|
|
fi
|
|
}
|