Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Created November 15, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flash-gordon/83bd6e31e6a35ed8223d14f76f187590 to your computer and use it in GitHub Desktop.
Save flash-gordon/83bd6e31e6a35ed8223d14f76f187590 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# initial code from https://github.com/denysdovhan/spaceship-prompt/blob/master/sections/git_status.zsh
#
# Git status
#
# ------------------------------------------------------------------------------
# Utils (from https://github.com/denysdovhan/spaceship-prompt/blob/master/lib/utils.zsh)
# ------------------------------------------------------------------------------
spaceship::is_git() {
# See https://git.io/fp8Pa for related discussion
[[ $(command git rev-parse --is-inside-work-tree 2>/dev/null) == true ]]
}
# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------
# We used to depend on OMZ git library,
# But it doesn't handle many of the status indicator combinations.
# Also, It's hard to maintain external dependency.
# See PR #147 at https://git.io/vQkkB
# See git help status to know more about status formats
spaceship_git_status() {
[[ $SPACESHIP_GIT_STATUS_SHOW == false ]] && return
spaceship::is_git || return
local INDEX git_status=""
INDEX=$(command git status --porcelain -b 2> /dev/null)
staged=false
changed=false
# Check for untracked files
if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
changed=true
fi
# Check for staged files
if $(echo "$INDEX" | command grep '^A[ MDAU] ' &> /dev/null); then
staged=true
elif $(echo "$INDEX" | command grep '^M[ MD] ' &> /dev/null); then
staged=true
elif $(echo "$INDEX" | command grep '^UA' &> /dev/null); then
staged=true
fi
# Check for modified files
if $(echo "$INDEX" | command grep '^[ MARC]M ' &> /dev/null); then
changed=true
fi
# Check for renamed files
if $(echo "$INDEX" | command grep '^R[ MD] ' &> /dev/null); then
staged=true
fi
# Check for deleted files
if $(echo "$INDEX" | command grep '^[MARCDU ]D ' &> /dev/null); then
changed=true
fi
if $(echo "$INDEX" | command grep '^D[ UM] ' &> /dev/null); then
staged=true
fi
if [ "$staged" = true ]; then
git_status="$git_status+"
fi
if [ "$changed" = true ]; then
git_status="$git_status*"
fi
if [[ -n $git_status ]]; then
echo "|$git_status)"
else
echo ")"
fi
}
spaceship_git_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment