-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitprompt
More file actions
136 lines (122 loc) · 3.23 KB
/
gitprompt
File metadata and controls
136 lines (122 loc) · 3.23 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
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
findtag() {
local ref=$1
local gitdir=$2
if [ -z "$gitdir" -o -z "$ref" ]; then
echo "Bad arguments of '$ref' '$gitdir'"
return 1
fi
for tag in `find $gitdir/refs/tags -type f`; do
grep $ref $tag > /dev/null
if [ $? == 0 ]; then
striped="${tag#$gitdir/refs/tags}"
echo $striped
return 0
fi
done
}
maptag() {
local head=$1
local gitdir=$2
local branch=""
if [ -z "$head" -o -z "$gitdir" ]; then
echo "Bad args to maptag"
return 1
fi
# Revert to this if tag searching is too slow.
#branch="d:${head:0:7}"
#echo $branch
#return 0
local tag=`grep $head $gitdir/packed-refs`
if [ ! -z "$tag" ]; then
branch="tag:${tag##*refs/tags/}"
else
tag=`findtag "$head" "$gitdir"`
if [ -z "$tag" ]; then
branch="d:${head:0:7}"
else
branch="tag:$tag"
fi
fi
echo $branch
}
mapref() {
local head=$1
local gitdir=$2
local branch=""
if [ -z "$head" -o -z "$gitdir" ]; then
echo "Bad args to mapref"
return 1
fi
case "$head" in
ref:*)
branch="${head##*/}"
;;
"")
branch=""
;;
*)
branch=`maptag "$head" "$gitdir"`
;;
esac
echo $branch
}
# Fast git branch from: https://gist.github.com/wolever/6525437
gitbranch() {
export GITBRANCH=""
local repo="${_GITBRANCH_LAST_REPO-}"
local gitdir=""
# [[ ! -z "$repo" ]] && gitdir="$repo/.git"
# If we don't have a last seen git repo, or we are in a different directory
if [[ -z "$repo" || "$PWD" != "$repo"* || ! -e "$gitdir" ]]; then
local cur="$PWD"
while [[ ! -z "$cur" ]]; do
if [[ -e "$cur/.git" ]]; then
repo="$cur"
gitdir="$cur/.git"
break
fi
cur="${cur%/*}"
done
fi
if [[ -z "$gitdir" ]]; then
unset _GITBRANCH_LAST_REPO
return 0
fi
# Find the git directory for a submodule
if [ -f $gitdir ]; then
local gitdirLoc
read gitdirLoc < "$gitdir"
pattern="gitdir: (.*)"
if [[ $gitdirLoc =~ $pattern ]]; then
gitdirRelative="${BASH_REMATCH[1]}"
local absolute="^/.*"
if [[ $gitdirRelative =~ $absolute ]]; then
gitdir="$gitdirRelative"
else
gitdir="$repo/$gitdirRelative"
fi
else
echo "Failed to find git dir in $gitdirLoc"
export GITBRANCH="Failed to find git dir in $gitdirLoc"
return 0
fi
fi
export _GITBRANCH_LAST_REPO="${repo}"
local head=""
local branch=""
read head < "$gitdir/HEAD"
branch=`mapref "$head" "$gitdir"`
if [[ -z "$branch" ]]; then
export GITBRANCH=""
return 0
fi
export GITBRANCH="$branch"
}
COLOR_LIGHT_CYAN='\e[1;36m'
COLOR_NC='\e[0m' # No Color
export PS1="\A \u@\h:[\[${COLOR_LIGHT_CYAN}\]\$GITBRANCH\[${COLOR_NC}\]]\w$ "
_mk_prompt() {
gitbranch
export PS1="\A \u@\h:[\[${COLOR_LIGHT_CYAN}\]\$GITBRANCH\[${COLOR_NC}\]]\w$ "
}
export PROMPT_COMMAND=_mk_prompt