Created
November 15, 2020 13:03
-
-
Save flash-gordon/83bd6e31e6a35ed8223d14f76f187590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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