Repository navigation
Expand file tree
/
Copy pathgit_prompt
More file actions
92 lines (76 loc) · 2.15 KB
/
Copy pathgit_prompt
File metadata and controls
92 lines (76 loc) · 2.15 KB
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
#!/bin/bash
git_prompt() {
git_status_output=$(git status 2> /dev/null) || return
branch_name() {
sed -n 's/# On branch //p' <<< "$git_status_output"
}
number_of_commits() {
local branch_prefix='# Your branch is '
local branch_suffix='by [[:digit:]]+'
if [[ "$git_status_output" =~ ${branch_prefix}"$1".*${branch_suffix} ]]
then
echo ${BASH_REMATCH[0]//[^0-9]/}
else
echo 0 && return 1
fi
}
match_against_status() {
local pattern="$1"
[[ "$git_status_output" =~ ${pattern} ]]
}
working_dir_clean() {
match_against_status '(working directory clean)'
}
local_changes() {
local added='# Changes to be committed'
local not_added='# Changes not staged for commit'
match_against_status "($added|$not_added)"
}
untracked_files() {
match_against_status '# Untracked files'
}
dashline() {
eval printf '%.0s-' {1..$1}
}
local bold="\033[1m"
local no_colour="\033[0m"
ahead_arrow() {
if commits_ahead=$(number_of_commits "ahead")
then
echo -e "$bold$(dashline $commits_ahead)$no_colour> $commits_ahead ahead"
fi
}
behind_arrow() {
if commits_behind=$(number_of_commits "behind")
then
echo "$commits_behind behind <$bold$(dashline $commits_behind)$no_colour"
fi
}
branch_part() {
local red="\033[31m"
local green="\033[32m"
local yellow="\033[33m"
local branch_colour=""
if untracked_files
then
branch_colour=$red
elif local_changes
then
branch_colour=$yellow
elif working_dir_clean
then
branch_colour=$green
fi
echo "$branch_colour$(branch_name)$no_colour"
}
local behind_part=$(behind_arrow)
local ahead_part=$(ahead_arrow)
if [[ ! "$behind_part" && ! "$ahead_part" ]]
then
prompt="$(branch_part)"
else
prompt="$behind_part|$ahead_part\n$(branch_part)"
fi
echo -e "$prompt "
}
export PS1="\$(git_prompt)"$PS1