-
Notifications
You must be signed in to change notification settings - Fork 0
/
.git-prompt.bash
95 lines (83 loc) · 4.17 KB
/
.git-prompt.bash
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
# Usage:
# In .bashrc do the following:
# . bash-prompt-git.bash
# PS1="\u@\h:\w\$(git_bash_prompt)\$ "
# ^^^^^^^^^^^^^^^^^^^^^
# NOTE: The backslash before the $ is required.
# You can set these in .bashrc any time after sourcing this file to control
# the display of the prompt.
GIT_ADDED_INDICATOR="+"
GIT_DELETED_INDICATOR="-"
GIT_MODIFIED_INDICATOR="~"
GIT_UNCOMMITTED_INDICATOR="~"
GIT_UNSTAGED_INDICATOR="!"
GIT_CLEAN_INDICATOR="≡"
GIT_AHEAD_INDICATOR="↑"
GIT_BEHIND_INDICATOR="↓"
if [ "${color_prompt}" == "yes" ]; then
GIT_AHEAD_BEHIND_COLOR="\033[0;93m"
GIT_AHEAD_COLOR="\033[0;92m"
GIT_BEHIND_COLOR="\033[0;31m"
GIT_CLEAN_STATUS_COLOR="\033[0;96m"
GIT_DELIMITER_COLOR="\033[0;93m"
GIT_ERROR_COLOR="\033[0;31m"
GIT_RESET_COLOR="\033[0m"
GIT_STAGED_COLOR="\033[0;32m"
GIT_UNSTAGED_COLOR="\033[0;31m"
GIT_UNCOMMITTED_COLOR="\033[0;96m"
fi
git_bash_prompt() {
local -i ahead=0 behind=0 index_added=0 index_deleted=0 index_modified=0 index_unmerged=0 worktree_added=0 worktree_deleted=0 worktree_modified=0 worktree_unmerged=0
local branch="" error="" remote="" line
while IFS= read -r line ; do
if [[ "${line:0:2}" = "xx" ]]; then return 1; fi
if [[ "${line:2:1}" != " " ]]; then error="unexpected git status output"; return 0; fi
# https://git-scm.com/docs/git-status
local x=${line:0:1}
local y=${line:1:1}
if [[ "${x}${y}" = "##" ]]; then
# extract branch information
branch=${line:3}
remote="${branch#*...}"
branch="${branch%%...*}"
# extract commit ahead and behind counts
if [[ $remote =~ .*\[.*ahead[[:blank:]]+([0-9]+).*\] ]]; then ahead=$((${BASH_REMATCH[1]})); fi
if [[ $remote =~ .*\[.*behind[[:blank:]]+([0-9]+).*\] ]]; then behind=$((${BASH_REMATCH[1]})); fi
else
if [[ "${x}" = "A" ]]; then ((index_added++)); fi
if [[ "${x}" = "D" ]]; then ((index_deleted++)); fi
if [[ "${x}" = "M" ]] || [[ "${x}" = "R" ]] || [[ "${x}" = "C" ]]; then ((index_modified++)); fi
if [[ "${x}" = "U" ]]; then ((index_unmerged++)); fi
if [[ "${y}" = "A" ]] || [[ "${y}" = "?" ]]; then ((worktree_added++)); fi
if [[ "${y}" = "D" ]]; then ((worktree_deleted++)); fi
if [[ "${y}" = "M" ]]; then ((worktree_modified++)); fi
if [[ "${y}" = "U" ]]; then ((worktree_unmerged++)); fi
fi
done < <(LC_ALL=C git status --porcelain --branch 2>/dev/null || echo -e "xx $?")
local vcstate index_state delimiter_state worktree_state working_state
if [[ -n "${error}" ]]; then
vcstate=" ${GIT_DELIMITER_COLOR}[${GIT_ERROR_COLOR}${error}${GIT_DELIMITER_COLOR}]"
elif [[ -n "${branch}" ]]; then
if ((ahead == 0 && behind == 0)); then status_state="${GIT_CLEAN_STATUS_COLOR}${branch} ${GIT_CLEAN_INDICATOR}"
elif ((ahead > 0 && behind > 0)); then status_state="${GIT_AHEAD_BEHIND_COLOR}${branch} ${GIT_AHEAD_INDICATOR}${ahead} ${GIT_BEHIND_INDICATOR}${behind}"
elif ((ahead > 0)); then status_state="${GIT_AHEAD_COLOR}${branch} ${GIT_AHEAD_INDICATOR}${ahead}"
elif ((behind > 0)); then status_state="${GIT_BEHIND_COLOR}${branch} ${GIT_BEHIND_INDICATOR}${behind}"
fi
if ((index_added > 0 || index_modified > 0 || index_deleted > 0)); then
index_state=" ${GIT_STAGED_COLOR}${GIT_ADDED_INDICATOR}${index_added} ${GIT_MODIFIED_INDICATOR}${index_modified} ${GIT_DELETED_INDICATOR}${index_deleted}"
fi
if (( (index_added > 0 || index_modified > 0 || index_deleted > 0) && (worktree_added > 0 || worktree_modified > 0 || worktree_deleted > 0) )); then
delimiter_state=" ${GIT_DELIMITER_COLOR}|"
fi
if ((worktree_added > 0 || worktree_modified > 0 || worktree_deleted > 0)); then
worktree_state=" ${GIT_UNSTAGED_COLOR}${GIT_ADDED_INDICATOR}${worktree_added} ${GIT_MODIFIED_INDICATOR}${worktree_modified} ${GIT_DELETED_INDICATOR}${worktree_deleted}"
fi
if ((worktree_added > 0 || worktree_modified > 0 || worktree_deleted > 0)); then
working_state=" ${GIT_UNSTAGED_COLOR}${GIT_UNSTAGED_INDICATOR}"
elif ((index_added > 0 || index_modified > 0 || index_deleted)); then
working_state=" ${GIT_UNCOMMITTED_COLOR}${GIT_UNCOMMITTED_INDICATOR}"
fi
vcstate=" ${GIT_DELIMITER_COLOR}[${status_state}${index_state}${delimiter_state}${worktree_state}${working_state}${GIT_DELIMITER_COLOR}]"
fi
echo -e "${vcstate}"
}