forked from ptab/bash-git-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitprompt.sh
executable file
·114 lines (93 loc) · 3.54 KB
/
gitprompt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# general settings
FETCH_TIMEOUT=5
# define colors
BLACK="\001\x1B[30m\002"
BLACK_HI="\001\x1B[90m\002"
RED="\001\x1B[31m\002"
RED_HI="\001\x1B[91m\002"
GREEN="\001\x1B[32m\002"
GREEN_HI="\001\x1B[92m\002"
YELLOW="\001\x1B[33m\002"
YELLOW_HI="\001\x1B[93m\002"
BLUE="\001\x1B[34m\002"
BLUE_HI="\001\x1B[94m\002"
CYAN="\001\x1B[36m\002"
CYAN_HI="\001\x1B[96m\002"
WHITE="\001\x1B[37m\002"
WHITE_HI="\001\x1B[97m\002"
RESET="\001\x1B[0m\002"
# define prompt variables
PROMPT_START="${BLACK_HI}(" # start of the git info string
PROMPT_END="${BLACK_HI})" # the end of the git info string
PROMPT_SEPARATOR="${BLACK_HI}|" # separates each item
PROMPT_UPDATING="${RED_HI}…" # marks that the info is being fetched from the remote
PREFIX_BRANCH="${BLUE_HI}" # the git branch that is active in the current directory
PREFIX_STAGED="${GREEN_HI}+" # the number of staged files/directories
PREFIX_CONFLICTS="${RED_HI}×" # the number of files in conflict
PREFIX_CHANGED="${GREEN}Δ" # the number of changed files
PREFIX_UNTRACKED="${BLUE}?" # the number of untracked files/dirs
PREFIX_STASHED="${YELLOW}ʂ" # the number of stashed files/dir
# determine path to gitstatus.sh
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
GIT_STATUS_SCRIPT="$DIR/gitstatus.sh"
#
# Start execution
#
REPO=`git rev-parse --show-toplevel 2> /dev/null`
if [[ $? -ne 0 ]]; then
# exit if we're not in a git directory
exit 0
fi
# retrieve the branch status
GIT_STATUS=($($GIT_STATUS_SCRIPT 2>/dev/null))
if [[ -z $GIT_STATUS ]]; then
# exit if we couldn't retrieve the status
exit 1
fi
GIT_BRANCH=${GIT_STATUS[0]}
GIT_REMOTE=${GIT_STATUS[1]}
if [[ "." == $GIT_REMOTE ]]; then
unset GIT_REMOTE
fi
GIT_STAGED=${GIT_STATUS[2]}
GIT_CONFLICTS=${GIT_STATUS[3]}
GIT_CHANGED=${GIT_STATUS[4]}
GIT_UNTRACKED=${GIT_STATUS[5]}
GIT_STASHED=${GIT_STATUS[6]}
# start building the status prompt
STATUS=" ${PROMPT_START}"
FETCH_HEAD="$REPO/.git/FETCH_HEAD"
if [[ ! -e "${FETCH_HEAD}" || -e `find "${FETCH_HEAD}" -mmin +${FETCH_TIMEOUT}` ]]; then
# fetch from repo if local is stale for more than $FETCH_TIMEOUT minutes
if [[ -n $(git remote show) ]]; then
( git fetch --quiet &> /dev/null) &
STATUS="${STATUS}${PROMPT_SEPARATOR}${PROMPT_UPDATING}"
fi
else
# otherwise, build based on the output of the script
if [[ -n $GIT_REMOTE ]]; then
STATUS="${STATUS}${PROMPT_SEPARATOR}${PREFIX_REMOTE}${GIT_REMOTE}"
fi
fi
if [[ $GIT_STAGED -ne 0 ]]; then
STATUS="${STATUS}${RED}${GIT_BRANCH}${PROMPT_SEPARATOR}${PREFIX_STAGED}${GIT_STAGED}"
elif [[ ${GIT_CONFLICTS} -ne 0 ]]; then
STATUS="${STATUS}${RED}${GIT_BRANCH}${PROMPT_SEPARATOR}${PREFIX_CONFLICTS}${GIT_CONFLICTS}"
elif [[ $GIT_CHANGED -ne 0 ]]; then
STATUS="${STATUS}${RED}${GIT_BRANCH}${PROMPT_SEPARATOR}${PREFIX_CHANGED}${GIT_CHANGED}"
elif [[ $GIT_UNTRACKED -ne 0 ]]; then
STATUS="${STATUS}${PREFIX_BRANCH}${GIT_BRANCH}${PROMPT_SEPARATOR}${PREFIX_UNTRACKED}${GIT_UNTRACKED}"
elif [[ $GIT_STASHED -ne 0 ]]; then
STATUS="${STATUS}${PREFIX_BRANCH}${GIT_BRANCH}${PROMPT_SEPARATOR}${PREFIX_STASHED}${GIT_STASHED}"
else
STATUS="${STATUS}${PREFIX_BRANCH}${GIT_BRANCH}"
fi
echo -e "${STATUS}${PROMPT_END}${RESET}"
exit 0