|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Git status |
| 4 | +# |
| 5 | + |
| 6 | +# ------------------------------------------------------------------------------ |
| 7 | +# Configuration |
| 8 | +# ------------------------------------------------------------------------------ |
| 9 | +RED='\033[0;31m' |
| 10 | +CYAN='\033[0;36m' |
| 11 | +LIGHT_GREEN='\033[1;32m' |
| 12 | +YELLOW='\033[1;33m' |
| 13 | +NC='\033[0m' |
| 14 | + |
| 15 | +SPACESHIP_GIT_STATUS_SHOW="${SPACESHIP_GIT_STATUS_SHOW=true}" |
| 16 | +SPACESHIP_GIT_STATUS_PREFIX="${SPACESHIP_GIT_STATUS_PREFIX=" ["}" |
| 17 | +SPACESHIP_GIT_STATUS_SUFFIX="${SPACESHIP_GIT_STATUS_SUFFIX="]"}" |
| 18 | +SPACESHIP_GIT_STATUS_COLOR="${SPACESHIP_GIT_STATUS_COLOR="red"}" |
| 19 | +SPACESHIP_GIT_STATUS_UNTRACKED="${SPACESHIP_GIT_STATUS_UNTRACKED="?"}" |
| 20 | +SPACESHIP_GIT_STATUS_ADDED="${SPACESHIP_GIT_STATUS_ADDED="${LIGHT_GREEN}+${NC}"}" |
| 21 | +SPACESHIP_GIT_STATUS_MODIFIED="${SPACESHIP_GIT_STATUS_MODIFIED="${YELLOW}!${NC}"}" |
| 22 | +SPACESHIP_GIT_STATUS_RENAMED="${SPACESHIP_GIT_STATUS_RENAMED="»"}" |
| 23 | +SPACESHIP_GIT_STATUS_DELETED="${SPACESHIP_GIT_STATUS_DELETED="${RED}✘${NC}"}" |
| 24 | +SPACESHIP_GIT_STATUS_STASHED="${SPACESHIP_GIT_STATUS_STASHED="$"}" |
| 25 | +SPACESHIP_GIT_STATUS_UNMERGED="${SPACESHIP_GIT_STATUS_UNMERGED="="}" |
| 26 | +SPACESHIP_GIT_STATUS_AHEAD="${SPACESHIP_GIT_STATUS_AHEAD="${CYAN}⇡${NC}"}" |
| 27 | +SPACESHIP_GIT_STATUS_BEHIND="${SPACESHIP_GIT_STATUS_BEHIND="⇣"}" |
| 28 | +SPACESHIP_GIT_STATUS_DIVERGED="${SPACESHIP_GIT_STATUS_DIVERGED="⇕"}" |
| 29 | + |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +# Section |
| 32 | +# ------------------------------------------------------------------------------ |
| 33 | + |
| 34 | +# We used to depend on OMZ git library, |
| 35 | +# But it doesn't handle many of the status indicator combinations. |
| 36 | +# Also, It's hard to maintain external dependency. |
| 37 | +# See PR #147 at https://git.io/vQkkB |
| 38 | +# See git help status to know more about status formats |
| 39 | +# spaceship_git_status() { |
| 40 | +# [[ $SPACESHIP_GIT_STATUS_SHOW == false ]] && return |
| 41 | + |
| 42 | +# spaceship::is_git || return |
| 43 | + |
| 44 | +INDEX='' |
| 45 | +git_status="" |
| 46 | + |
| 47 | +INDEX=$(cd "$1" && command git status --porcelain -b 2> /dev/null) |
| 48 | + |
| 49 | +# Check for untracked files |
| 50 | +if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then |
| 51 | + git_status="$SPACESHIP_GIT_STATUS_UNTRACKED$git_status" |
| 52 | +fi |
| 53 | + |
| 54 | +# Check for staged files |
| 55 | +if $(echo "$INDEX" | command grep '^A[ MDAU] ' &> /dev/null); then |
| 56 | + git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status" |
| 57 | +elif $(echo "$INDEX" | command grep '^M[ MD] ' &> /dev/null); then |
| 58 | + git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status" |
| 59 | +elif $(echo "$INDEX" | command grep '^UA' &> /dev/null); then |
| 60 | + git_status="$SPACESHIP_GIT_STATUS_ADDED$git_status" |
| 61 | +fi |
| 62 | + |
| 63 | +# Check for modified files |
| 64 | +if $(echo "$INDEX" | command grep '^[ MARC]M ' &> /dev/null); then |
| 65 | + git_status="$SPACESHIP_GIT_STATUS_MODIFIED$git_status" |
| 66 | +fi |
| 67 | + |
| 68 | +# Check for renamed files |
| 69 | +if $(echo "$INDEX" | command grep '^R[ MD] ' &> /dev/null); then |
| 70 | + git_status="$SPACESHIP_GIT_STATUS_RENAMED$git_status" |
| 71 | +fi |
| 72 | + |
| 73 | +# Check for deleted files |
| 74 | +if $(echo "$INDEX" | command grep '^[MARCDU ]D ' &> /dev/null); then |
| 75 | + git_status="$SPACESHIP_GIT_STATUS_DELETED$git_status" |
| 76 | +elif $(echo "$INDEX" | command grep '^D[ UM] ' &> /dev/null); then |
| 77 | + git_status="$SPACESHIP_GIT_STATUS_DELETED$git_status" |
| 78 | +fi |
| 79 | + |
| 80 | +# Check for stashes |
| 81 | +if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then |
| 82 | + git_status="$SPACESHIP_GIT_STATUS_STASHED$git_status" |
| 83 | +fi |
| 84 | + |
| 85 | +# Check for unmerged files |
| 86 | +if $(echo "$INDEX" | command grep '^U[UDA] ' &> /dev/null); then |
| 87 | + git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status" |
| 88 | +elif $(echo "$INDEX" | command grep '^AA ' &> /dev/null); then |
| 89 | + git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status" |
| 90 | +elif $(echo "$INDEX" | command grep '^DD ' &> /dev/null); then |
| 91 | + git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status" |
| 92 | +elif $(echo "$INDEX" | command grep '^[DA]U ' &> /dev/null); then |
| 93 | + git_status="$SPACESHIP_GIT_STATUS_UNMERGED$git_status" |
| 94 | +fi |
| 95 | + |
| 96 | +# Check whether branch is ahead |
| 97 | +is_ahead=false |
| 98 | +if $(echo "$INDEX" | command grep '^## [^ ]\+ .*ahead' &> /dev/null); then |
| 99 | + is_ahead=true |
| 100 | +fi |
| 101 | + |
| 102 | +# Check whether branch is behind |
| 103 | +is_behind=false |
| 104 | +if $(echo "$INDEX" | command grep '^## [^ ]\+ .*behind' &> /dev/null); then |
| 105 | + is_behind=true |
| 106 | +fi |
| 107 | + |
| 108 | +# Check wheather branch has diverged |
| 109 | +if [[ "$is_ahead" == true && "$is_behind" == true ]]; then |
| 110 | + git_status="$SPACESHIP_GIT_STATUS_DIVERGED$git_status" |
| 111 | +else |
| 112 | + [[ "$is_ahead" == true ]] && git_status="$SPACESHIP_GIT_STATUS_AHEAD$git_status" |
| 113 | + [[ "$is_behind" == true ]] && git_status="$SPACESHIP_GIT_STATUS_BEHIND$git_status" |
| 114 | +fi |
| 115 | + |
| 116 | +if [[ "$2" == "-c" ]]; then |
| 117 | + echo -e "$git_status" |
| 118 | +else |
| 119 | + echo $(echo -e "$git_status" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g") |
| 120 | +fi |
| 121 | + |
| 122 | + |
0 commit comments