-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-git-prompt.sh
141 lines (117 loc) · 4.89 KB
/
custom-git-prompt.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
# This is a mix of bash-powerline https://github.com/riobard/bash-powerline
# With some stuff from @mathiasbynens's shell prompt which was heavly
# inspired from @necolas’s prompt: https://github.com/necolas/dotfiles
__powerline_git_prompt() {
# Unicode symbols
readonly PS_SYMBOL_DARWIN=''
readonly PS_SYMBOL_LINUX='$'
readonly PS_SYMBOL_OTHER='%'
readonly GIT_BRANCH_SYMBOL='⑂ '
readonly GIT_BRANCH_STAGED_SYMBOL='+'
readonly GIT_BRANCH_UNSTAGED_SYMBOL='!'
readonly GIT_BRANCH_UNTRACKED_SYMBOL='?'
readonly GIT_BRANCH_STASHED_SYMBOL='$'
readonly GIT_NEED_PUSH_SYMBOL='⇡'
readonly GIT_NEED_PULL_SYMBOL='⇣'
# Solarized colorscheme
readonly FG_BASE03="\[$(tput setaf 8)\]"
readonly FG_BASE02="\[$(tput setaf 0)\]"
readonly FG_BASE01="\[$(tput setaf 10)\]"
readonly FG_BASE00="\[$(tput setaf 11)\]"
readonly FG_BASE0="\[$(tput setaf 12)\]"
readonly FG_BASE1="\[$(tput setaf 14)\]"
readonly FG_BASE2="\[$(tput setaf 7)\]"
readonly FG_BASE3="\[$(tput setaf 15)\]"
readonly BG_BASE03="\[$(tput setab 8)\]"
readonly BG_BASE02="\[$(tput setab 0)\]"
readonly BG_BASE01="\[$(tput setab 10)\]"
readonly BG_BASE00="\[$(tput setab 11)\]"
readonly BG_BASE0="\[$(tput setab 12)\]"
readonly BG_BASE1="\[$(tput setab 14)\]"
readonly BG_BASE2="\[$(tput setab 7)\]"
readonly BG_BASE3="\[$(tput setab 15)\]"
readonly FG_YELLOW="\[$(tput setaf 3)\]"
readonly FG_ORANGE="\[$(tput setaf 9)\]"
readonly FG_RED="\[$(tput setaf 1)\]"
readonly FG_MAGENTA="\[$(tput setaf 5)\]"
readonly FG_VIOLET="\[$(tput setaf 13)\]"
readonly FG_BLUE="\[$(tput setaf 4)\]"
readonly FG_CYAN="\[$(tput setaf 6)\]"
readonly FG_GREEN="\[$(tput setaf 2)\]"
readonly BG_YELLOW="\[$(tput setab 3)\]"
readonly BG_ORANGE="\[$(tput setab 9)\]"
readonly BG_RED="\[$(tput setab 1)\]"
readonly BG_MAGENTA="\[$(tput setab 5)\]"
readonly BG_VIOLET="\[$(tput setab 13)\]"
readonly BG_BLUE="\[$(tput setab 4)\]"
readonly BG_CYAN="\[$(tput setab 6)\]"
readonly BG_GREEN="\[$(tput setab 2)\]"
readonly DIM="\[$(tput dim)\]"
readonly REVERSE="\[$(tput rev)\]"
readonly RESET="\[$(tput sgr0)\]"
readonly BOLD="\[$(tput bold)\]"
GIT_PROMPT_TEXT_COLOR=$FG_BASE3
GIT_PROMPT_PATH_COLOR=$BG_BASE1$GIT_PROMPT_TEXT_COLOR
GIT_PROMPT_BRANCH_COLOR=$BG_BLUE$GIT_PROMPT_TEXT_COLOR
GIT_PROMPT_MARKS_COLOR=$BG_GREEN$GIT_PROMPT_TEXT_COLOR
GIT_PROMP_BEHIND_COLOR=$BG_RED$GIT_PROMPT_TEXT_COLOR
GIT_PROMP_AHEAD_COLOR=$BG_GREEN$GIT_PROMPT_TEXT_COLOR
# what OS?
case "$(uname)" in
Darwin)
readonly PS_SYMBOL=$PS_SYMBOL_DARWIN
;;
Linux)
readonly PS_SYMBOL=$PS_SYMBOL_LINUX
;;
*)
readonly PS_SYMBOL=$PS_SYMBOL_OTHER
esac
__git_info() {
[ -x "$(which git)" ] || return # git not found
# get current branch name or short SHA1 hash for detached head
local branch="$(git symbolic-ref --short HEAD 2>/dev/null || git describe --tags --always 2>/dev/null)"
[ -n "$branch" ] || return # git branch not found
local marks
# Check for uncommitted changes in the index.
if ! $(git diff --quiet --ignore-submodules --cached); then
marks+="$GIT_BRANCH_STAGED_SYMBOL";
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
marks+="$GIT_BRANCH_UNSTAGED_SYMBOL";
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
marks+="$GIT_BRANCH_UNTRACKED_SYMBOL";
fi;
# # Check for stashed files.
# if $(git rev-parse --verify refs/stash &>/dev/null); then
# marks+="$GIT_BRANCH_STASHED_SYMBOL";
# fi;
[ -n "${marks}" ] && marks=" [$FG_YELLOW${marks}$RESET]";
# how many commits local branch is ahead/behind of remote?
local stat="$(git status --porcelain --branch | grep '^##' | grep -o '\[.\+\]$')"
local aheadN="$(echo $stat | grep -o 'ahead \d\+' | grep -o '\d\+')"
local behindN="$(echo $stat | grep -o 'behind \d\+' | grep -o '\d\+')"
[ -n "$aheadN" ] && ahead=" $GIT_NEED_PUSH_SYMBOL$aheadN"
[ -n "$behindN" ] && behind=" $GIT_NEED_PULL_SYMBOL$behindN"
printf "($FG_RED$branch$RESET)$FG_GREEN$ahead$FG_RED$behind$RESET$marks"
}
ps1() {
# Check the exit code of the previous command and display different
# colors in the prompt accordingly.
if [ $? -eq 0 ]; then
local BG_EXIT="$BG_GREEN"
else
local BG_EXIT="$BG_RED"
fi
PS1="$FG_BASE3\W$RESET"
PS1+="$FG_BASE3$(__git_info) $RESET"
PS1+="$BOLD$FG_RED$PS_SYMBOL_LINUX $RESET"
}
export PROMPT_COMMAND=ps1
}
__powerline_git_prompt
unset __powerline_git_prompt