/
.git_bash_prompt
85 lines (74 loc) · 2.08 KB
/
.git_bash_prompt
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
#!/bin/bash
#
# Set our bash prompt according to the branch/status of the current git
# repository.
#
# Taken from http://gist.github.com/31934
RED="\[\e[0;31m\]"
GREEN="\[\e[0;32m\]"
YELLOW="\[\e[0;33m\]"
BLUE="\[\e[0;34m\]"
MAGENTA="\[\e[0;35m\]"
CYAN="\[\e[0;36m\]"
GRAY="\[\e[0;37m\]"
LIGHT_RED="\[\e[1;31m\]"
LIGHT_GREEN="\[\e[1;32m\]"
LIGHT_YELLOW="\[\e[1;33m\]"
LIGHT_BLUE="\[\e[1;34m\]"
LIGHT_MAGENTA="\[\e[1;35m\]"
LIGHT_CYAN="\[\e[1;36m\]"
WHITE="\[\e[1;37m\]"
COLOR_NONE="\[\e[0m\]"
function is_git_repository {
git branch > /dev/null 2>&1
}
function parse_git_branch {
# Only display git info if we're inside a git repository.
is_git_repository || return 1
# Capture the output of the "git status" command.
git_status="$(git status 2> /dev/null)"
git_stash="$(git stash list 2> /dev/null)"
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "working directory clean" ]]; then
state="${GREEN}"
elif [[ ${git_status} =~ "Changes to be committed" ]]; then
state="${YELLOW}"
else
state="${RED}"
fi
if [[ -n ${git_stash} ]]; then
stash="${MAGENTA}*"
fi
# Set arrow icon based on status against remote.
if [[ ${git_status} =~ "# Your branch is ahead" ]]; then
remote="↑"
elif [[ ${git_status} =~ "# Your branch is behind" ]]; then
remote="↓"
elif [[ ${git_status} =~ ' have diverged' ]]; then
remote="↔"
fi
# Get the name of the branch.
branch_pattern="^# On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch="(${BASH_REMATCH[1]})"
else
branch="{Detached HEAD}"
fi
# Display the prompt.
echo "${state}${branch}${remote}${stash}${COLOR_NONE} "
}
function prompt_symbol () {
# Set color of dollar prompt based on return value of previous command.
if test $1 -eq 0
then
echo "\$"
else
echo "${RED}\$${COLOR_NONE}"
fi
}
function prompt_func () {
last_return_value=$?
PS1="\u@ ${GREEN}\w${COLOR_NONE} $(parse_git_branch)\n$(prompt_symbol $last_return_value) "
echo -n -e "\033]0;$USER@$HOSTNAME:$PWD\007\n"
}
PROMPT_COMMAND=prompt_func