mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-02-13 03:01:32 +01:00
Extracts git_change_status
This commit is contained in:
parent
2c9af0c7d5
commit
ba69db93ff
1 changed files with 42 additions and 35 deletions
77
lib/git.zsh
77
lib/git.zsh
|
|
@ -38,15 +38,18 @@
|
||||||
# Shows whether you have stashed commits or not
|
# Shows whether you have stashed commits or not
|
||||||
# ZSH_THEME_GIT_PROMPT_STASHED
|
# ZSH_THEME_GIT_PROMPT_STASHED
|
||||||
#
|
#
|
||||||
# git_prompt_status
|
# git_change_status
|
||||||
# More detailed information about the status of your working tree. Shows
|
# More detailed information about the status of your working tree. Shows
|
||||||
# what exactly makes it dirty and as a superset includes the information
|
# what exactly makes it dirty
|
||||||
# of git_remote_status and git_stash_status
|
|
||||||
# ZSH_THEME_GIT_PROMPT_ADDED
|
# ZSH_THEME_GIT_PROMPT_ADDED
|
||||||
# ZSH_THEME_GIT_PROMPT_DELETED
|
# ZSH_THEME_GIT_PROMPT_DELETED
|
||||||
# ZSH_THEME_GIT_PROMPT_MODIFIED
|
# ZSH_THEME_GIT_PROMPT_MODIFIED
|
||||||
# ZSH_THEME_GIT_PROMPT_UNMERGED
|
# ZSH_THEME_GIT_PROMPT_UNMERGED
|
||||||
# ZSH_THEME_GIT_PROMPT_UNTRACKED
|
# ZSH_THEME_GIT_PROMPT_UNTRACKED
|
||||||
|
#
|
||||||
|
# git_prompt_status
|
||||||
|
# Acts as a superset and combines the information of git_change_status,
|
||||||
|
# git_remote_status and git_stash_status
|
||||||
|
|
||||||
# get the name of the branch we are on
|
# get the name of the branch we are on
|
||||||
function git_prompt_info() {
|
function git_prompt_info() {
|
||||||
|
|
@ -87,7 +90,8 @@ git_remote_status() {
|
||||||
local val
|
local val
|
||||||
local remote
|
local remote
|
||||||
local return_str
|
local return_str
|
||||||
remote=${1:=$(command git status --porcelain -b 2> /dev/null | head -1)}
|
remote=${1:=$(command git status --porcelain -b 2> /dev/null)}
|
||||||
|
remote=$(echo $remote | head -1)
|
||||||
|
|
||||||
for val in ahead behind diverged; do
|
for val in ahead behind diverged; do
|
||||||
echo $remote | grep "##.*$val" &>/dev/null && \
|
echo $remote | grep "##.*$val" &>/dev/null && \
|
||||||
|
|
@ -96,13 +100,37 @@ git_remote_status() {
|
||||||
echo $return_str
|
echo $return_str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# shows if you have stashed commits
|
||||||
|
git_stash_status() {
|
||||||
|
has_stashed_commits && echo $ZSH_THEME_GIT_PROMPT_STASHED
|
||||||
|
}
|
||||||
|
|
||||||
|
has_stashed_commits() {
|
||||||
|
command git rev-parse --verify refs/stash &>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
# reveals the nature of your changes in git
|
||||||
|
git_change_status() {
|
||||||
|
local change_stats
|
||||||
|
local key
|
||||||
|
local return_str
|
||||||
|
change_stats=${1:=$(command git status --porcelain -b 2> /dev/null)}
|
||||||
|
change_stats=$(echo $change_stats | awk '{ print $1 }')
|
||||||
|
|
||||||
|
for key in ${(k)ZSH_THEME_GIT_CHANGE_MAP}; do
|
||||||
|
echo $change_stats | grep -E "$key" &>/dev/null && \
|
||||||
|
return_str+=$ZSH_THEME_GIT_CHANGE_MAP[$key]
|
||||||
|
done
|
||||||
|
echo $return_str
|
||||||
|
}
|
||||||
|
|
||||||
# provides an associative array with instructions
|
# provides an associative array with instructions
|
||||||
# used by grep in git_prompt_status
|
# used by grep in git_change_status
|
||||||
git_prompt_status_setup() {
|
setup_git_change_status() {
|
||||||
declare -Ag ZSH_THEME_GIT_PROMPT_MOD_MAP
|
declare -Ag ZSH_THEME_GIT_CHANGE_MAP
|
||||||
# need to shield the vars with quotes, because they might
|
# need to shield the vars with quotes, because they might
|
||||||
# be empty strings!
|
# be empty strings!
|
||||||
ZSH_THEME_GIT_PROMPT_MOD_MAP=(
|
ZSH_THEME_GIT_CHANGE_MAP=(
|
||||||
"\?\?" "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
|
"\?\?" "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
|
||||||
"A" "$ZSH_THEME_GIT_PROMPT_ADDED"
|
"A" "$ZSH_THEME_GIT_PROMPT_ADDED"
|
||||||
"[MT]" "$ZSH_THEME_GIT_PROMPT_MODIFIED"
|
"[MT]" "$ZSH_THEME_GIT_PROMPT_MODIFIED"
|
||||||
|
|
@ -111,33 +139,12 @@ git_prompt_status_setup() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
has_stashed_commits() {
|
# Combines the output of git_change_status, git_remote_status and
|
||||||
command git rev-parse --verify refs/stash &>/dev/null
|
# git_stash_status
|
||||||
}
|
|
||||||
|
|
||||||
git_stash_status() {
|
|
||||||
has_stashed_commits && echo $ZSH_THEME_GIT_PROMPT_STASHED
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the status of the working tree
|
|
||||||
git_prompt_status() {
|
git_prompt_status() {
|
||||||
local key
|
local git_index
|
||||||
local index
|
git_index=$(command git status --porcelain -b 2> /dev/null)
|
||||||
local remote_line
|
echo $(git_change_status $git_index)$(git_remote_status $git_index)$(git_stash_status)
|
||||||
local status_syms
|
|
||||||
local return_str=''
|
|
||||||
index=$(command git status --porcelain -b 2> /dev/null)
|
|
||||||
remote_line=$(echo $index | head -1)
|
|
||||||
status_syms=$(echo $index | awk '{ print $1 }')
|
|
||||||
|
|
||||||
for key in ${(k)ZSH_THEME_GIT_PROMPT_MOD_MAP}; do
|
|
||||||
echo $status_syms | grep -E "$key" &>/dev/null && \
|
|
||||||
return_str+=$ZSH_THEME_GIT_PROMPT_MOD_MAP[$key]
|
|
||||||
done
|
|
||||||
|
|
||||||
return_str+=$(git_remote_status $remote_line)
|
|
||||||
return_str+=$(git_stash_status)
|
|
||||||
echo $return_str
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#compare the provided version of git to the version installed and on path
|
#compare the provided version of git to the version installed and on path
|
||||||
|
|
@ -179,4 +186,4 @@ check_git_show_status() {
|
||||||
|
|
||||||
#this is unlikely to change so make it all statically assigned
|
#this is unlikely to change so make it all statically assigned
|
||||||
check_git_show_status
|
check_git_show_status
|
||||||
git_prompt_status_setup
|
setup_git_change_status
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue