mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-12 20:21:02 +01:00
commit
6811a48618
1 changed files with 90 additions and 34 deletions
124
lib/git.zsh
124
lib/git.zsh
|
@ -147,46 +147,102 @@ function git_prompt_long_sha() {
|
||||||
SHA=$(__git_prompt_git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
|
SHA=$(__git_prompt_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() {
|
function git_prompt_status() {
|
||||||
emulate -L zsh
|
|
||||||
|
|
||||||
[[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
|
[[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
|
||||||
|
|
||||||
local INDEX STATUS
|
# Maps a git status prefix to an internal constant
|
||||||
INDEX=$(__git_prompt_git status --porcelain -b 2> /dev/null) || return 0
|
# This cannot use the prompt constants, as they may be empty
|
||||||
STATUS=""
|
local -A prefix_constant_map
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)\\?\\? ' ]]; then
|
prefix_constant_map=(
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
|
'\?\? ' 'UNTRACKED'
|
||||||
|
'A ' 'ADDED'
|
||||||
|
'M ' 'ADDED'
|
||||||
|
'MM ' 'ADDED'
|
||||||
|
' M ' 'MODIFIED'
|
||||||
|
'AM ' 'MODIFIED'
|
||||||
|
' T ' 'MODIFIED'
|
||||||
|
'R ' 'RENAMED'
|
||||||
|
' D ' 'DELETED'
|
||||||
|
'D ' 'DELETED'
|
||||||
|
'UU ' 'UNMERGED'
|
||||||
|
'ahead' 'AHEAD'
|
||||||
|
'behind' 'BEHIND'
|
||||||
|
'diverged' 'DIVERGED'
|
||||||
|
'stashed' 'STASHED'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Maps the internal constant to the prompt theme
|
||||||
|
local -A constant_prompt_map
|
||||||
|
constant_prompt_map=(
|
||||||
|
'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
|
||||||
|
'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
|
||||||
|
'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
|
||||||
|
'RENAMED' "$ZSH_THEME_GIT_PROMPT_RENAMED"
|
||||||
|
'DELETED' "$ZSH_THEME_GIT_PROMPT_DELETED"
|
||||||
|
'UNMERGED' "$ZSH_THEME_GIT_PROMPT_UNMERGED"
|
||||||
|
'AHEAD' "$ZSH_THEME_GIT_PROMPT_AHEAD"
|
||||||
|
'BEHIND' "$ZSH_THEME_GIT_PROMPT_BEHIND"
|
||||||
|
'DIVERGED' "$ZSH_THEME_GIT_PROMPT_DIVERGED"
|
||||||
|
'STASHED' "$ZSH_THEME_GIT_PROMPT_STASHED"
|
||||||
|
)
|
||||||
|
|
||||||
|
# The order that the prompt displays should be added to the prompt
|
||||||
|
local status_constants
|
||||||
|
status_constants=(
|
||||||
|
UNTRACKED ADDED MODIFIED RENAMED DELETED
|
||||||
|
STASHED UNMERGED AHEAD BEHIND DIVERGED
|
||||||
|
)
|
||||||
|
|
||||||
|
local status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
|
||||||
|
|
||||||
|
# Don't continue on a catastrophic failure
|
||||||
|
if [[ $? -eq 128 ]]; then
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)(A |M |MM) ' ]]; then
|
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
|
# A lookup table of each git status encountered
|
||||||
|
local -A statuses_seen
|
||||||
|
|
||||||
|
if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
|
||||||
|
statuses_seen[STASHED]=1
|
||||||
fi
|
fi
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)([ AM]M| T) ' ]]; then
|
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
|
local status_lines
|
||||||
|
status_lines=("${(@f)${status_text}}")
|
||||||
|
|
||||||
|
# If the tracking line exists, get and parse it
|
||||||
|
if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
|
||||||
|
local branch_statuses
|
||||||
|
branch_statuses=("${(@s/,/)match}")
|
||||||
|
for branch_status in $branch_statuses; do
|
||||||
|
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
local last_parsed_status=$prefix_constant_map[$match[1]]
|
||||||
|
statuses_seen[$last_parsed_status]=$match[2]
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)R ' ]]; then
|
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
|
# For each status prefix, do a regex comparison
|
||||||
fi
|
for status_prefix in ${(k)prefix_constant_map}; do
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)([A ]D|D ) ' ]]; then
|
local status_constant="${prefix_constant_map[$status_prefix]}"
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
|
local status_regex="(^|\n)$status_prefix"
|
||||||
fi
|
|
||||||
if $(__git_prompt_git rev-parse --verify refs/stash >/dev/null 2>&1); then
|
if [[ "$status_text" =~ $status_regex ]]; then
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
|
statuses_seen[$status_constant]=1
|
||||||
fi
|
fi
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)UU ' ]]; then
|
done
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
|
|
||||||
fi
|
# Display the seen statuses in the order specified
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*ahead' ]]; then
|
local status_prompt
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
|
for status_constant in $status_constants; do
|
||||||
fi
|
if (( ${+statuses_seen[$status_constant]} )); then
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*behind' ]]; then
|
local next_display=$constant_prompt_map[$status_constant]
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
|
status_prompt="$next_display$status_prompt"
|
||||||
fi
|
fi
|
||||||
if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*diverged' ]]; then
|
done
|
||||||
STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
|
|
||||||
fi
|
echo $status_prompt
|
||||||
echo $STATUS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Outputs the name of the current user
|
# Outputs the name of the current user
|
||||||
|
|
Loading…
Reference in a new issue