Simplify code
This commit is contained in:
parent
1b81ab7bb3
commit
e267362a9a
|
@ -1,41 +1,29 @@
|
||||||
#!/usr/bin/zsh
|
#!/usr/bin/zsh
|
||||||
|
|
||||||
###
|
###
|
||||||
# Do some cleaning if not in a git repository
|
# Unset global variables
|
||||||
# Globals:
|
# Globals: all
|
||||||
# all
|
# Arguments: none
|
||||||
# Arguments:
|
|
||||||
# none
|
|
||||||
###
|
###
|
||||||
function sanitize() {
|
function sanitize() {
|
||||||
if ! git rev-parse --git-dir &>/dev/null; then
|
unset GIT_CURRENT_BRANCH GIT_STATUS
|
||||||
GIT_HAS_CHANGES=false
|
unset GIT_STAGED GIT_MODIFIED GIT_UNTRACKED GIT_DELETED
|
||||||
GIT_BRANCH=""
|
git rev-parse --git-dir &>/dev/null || return 1
|
||||||
GIT_STAGED=""
|
|
||||||
GIT_MODIFIED=""
|
|
||||||
GIT_UNTRACKED=""
|
|
||||||
GIT_STATUS=""
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###
|
###
|
||||||
# Get necessary information from git, like current branch, amount of modified
|
# Get necessary information from git, like current branch, amount of modified
|
||||||
# files etc. to then put them all into a global variable, GIT_STATUS
|
# files etc. to then put them all into a global variable, GIT_STATUS
|
||||||
# Globals:
|
# Globals: none
|
||||||
# none
|
# Arguments: none
|
||||||
# Arguments:
|
|
||||||
# none
|
|
||||||
###
|
###
|
||||||
function parse_git_status() {
|
function parse_git_status() {
|
||||||
local modified_files=0 staged_files=0 untracked_files=0 deleted_files=0
|
local modified_files=0 staged_files=0 untracked_files=0 deleted_files=0
|
||||||
GIT_HAS_CHANGES=0
|
local git_has_changes=0
|
||||||
GIT_BRANCH="$(git branch --show-current)"
|
GIT_CURRENT_BRANCH="$(git branch --show-current)"
|
||||||
|
|
||||||
git status --porcelain=v1 | while IFS= read -r status_line; do
|
git status --porcelain=v1 | while IFS= read -r status_line; do
|
||||||
GIT_HAS_CHANGES=1
|
git_has_changes=1
|
||||||
case "$status_line" in
|
case "$status_line" in
|
||||||
' M '*)
|
' M '*)
|
||||||
((modified_files++))
|
((modified_files++))
|
||||||
|
@ -59,9 +47,10 @@ function parse_git_status() {
|
||||||
GIT_REMOTE_BRANCH=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)")
|
GIT_REMOTE_BRANCH=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)")
|
||||||
local ahead_behind_status commits_behind commits_ahead
|
local ahead_behind_status commits_behind commits_ahead
|
||||||
if [[ ! -z ${GIT_REMOTE_BRANCH} ]]; then
|
if [[ ! -z ${GIT_REMOTE_BRANCH} ]]; then
|
||||||
ahead_behind_status=$(git rev-list --left-right --count ${GIT_BRANCH}...${GIT_REMOTE_BRANCH})
|
local ahead_behind_status=$(git rev-list --left-right --count \
|
||||||
commits_ahead=$(echo -n "$ahead_behind_status" | awk '{print $1}')
|
${GIT_CURRENT_BRANCH}...${GIT_REMOTE_BRANCH})
|
||||||
commits_behind=$(echo -n "$ahead_behind_status" | awk '{print $2}')
|
local commits_ahead=$(echo -n "$ahead_behind_status" | awk '{print $1}')
|
||||||
|
local commits_behind=$(echo -n "$ahead_behind_status" | awk '{print $2}')
|
||||||
|
|
||||||
(( ${commits_behind} > 0)) \
|
(( ${commits_behind} > 0)) \
|
||||||
&& GIT_COMMITS_BEHIND="↓${commits_behind} " \
|
&& GIT_COMMITS_BEHIND="↓${commits_behind} " \
|
||||||
|
@ -70,30 +59,24 @@ function parse_git_status() {
|
||||||
&& GIT_COMMITS_AHEAD="↑${commits_ahead} " \
|
&& GIT_COMMITS_AHEAD="↑${commits_ahead} " \
|
||||||
|| GIT_COMMITS_AHEAD=""
|
|| GIT_COMMITS_AHEAD=""
|
||||||
fi
|
fi
|
||||||
|
GIT_COMMITS_STATUS="${GIT_COMMITS_AHEAD}${GIT_COMMITS_BEHIND}"
|
||||||
|
|
||||||
(( ${staged_files} > 0 )) \
|
if (( $git_has_changes )); then
|
||||||
&& GIT_STAGED="${staged_files}+ " \
|
(( ${staged_files} > 0 )) \
|
||||||
|| GIT_STAGED=""
|
&& GIT_STAGED="+${staged_files} "
|
||||||
(( ${modified_files} > 0 )) \
|
(( ${modified_files} > 0 )) \
|
||||||
&& GIT_MODIFIED="!${modified_files} " \
|
&& GIT_MODIFIED="!${modified_files} "
|
||||||
|| GIT_MODIFIED=""
|
(( ${deleted_files} > 0 )) \
|
||||||
(( ${deleted_files} > 0 )) \
|
&& GIT_DELETED="-${deleted_files} "
|
||||||
&& GIT_DELETED="${deleted_files}- " \
|
(( ${untracked_files} > 0 )) \
|
||||||
|| GIT_DELETED=""
|
&& GIT_UNTRACKED="?${untracked_files}"
|
||||||
(( ${untracked_files} > 0 )) \
|
|
||||||
&& GIT_UNTRACKED="?${untracked_files}" \
|
|
||||||
|| GIT_UNTRACKED=""
|
|
||||||
|
|
||||||
if (( GIT_HAS_CHANGES == 1 )); then
|
|
||||||
FG_SPECIAL_COLOR="${FG_YELLOW}"
|
FG_SPECIAL_COLOR="${FG_YELLOW}"
|
||||||
else
|
else
|
||||||
FG_SPECIAL_COLOR="${FG_GREEN}"
|
FG_SPECIAL_COLOR="${FG_GREEN}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
GIT_COMMITS_STATUS="${GIT_COMMITS_AHEAD}${GIT_COMMITS_BEHIND}"
|
|
||||||
|
|
||||||
GIT_STATUS="| ${FG_SPECIAL_COLOR}"
|
GIT_STATUS="${FG_SPECIAL_COLOR}"
|
||||||
GIT_STATUS+=" ${GIT_BRANCH} "
|
GIT_STATUS+=" ${GIT_CURRENT_BRANCH} "
|
||||||
GIT_STATUS+="${GIT_COMMITS_STATUS}"
|
GIT_STATUS+="${GIT_COMMITS_STATUS}"
|
||||||
GIT_STATUS+="${GIT_MODIFIED}${GIT_STAGED}"
|
GIT_STATUS+="${GIT_MODIFIED}${GIT_STAGED}"
|
||||||
GIT_STATUS+="${GIT_DELETED}${GIT_UNTRACKED}"
|
GIT_STATUS+="${GIT_DELETED}${GIT_UNTRACKED}"
|
||||||
|
@ -108,9 +91,7 @@ function parse_git_status() {
|
||||||
# none
|
# none
|
||||||
###
|
###
|
||||||
function main() {
|
function main() {
|
||||||
if ! sanitize; then
|
sanitize || return 1
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
parse_git_status
|
parse_git_status
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue