forked from keflavich/configfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vcs.sh
107 lines (90 loc) · 2.67 KB
/
.vcs.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
#http://glandium.org/blog/?p=170
_bold=$(tput bold)
_normal=$(tput sgr0)
function readlink() {
DIR=$1;
(cd "$DIR" && echo "$(pwd -P)")
}
___vcs_dir() {
local vcs base_dir sub_dir ref
sub_dir() {
local sub_dir
sub_dir=$(readlink "${PWD}")
sub_dir=${sub_dir#$1}
}
git_dir() {
base_dir=$(git rev-parse --show-cdup 2>/dev/null) || return 1
base_dir=$(readlink "$base_dir")
if [ "$base_dir" = `echo ~` ]; then base_dir=""; return 1; fi
sub_dir=$(git rev-parse --show-prefix)
sub_dir=${sub_dir%/}
ref=$(git symbolic-ref -q HEAD || git name-rev --name-only HEAD 2>/dev/null)
ref=${ref#refs/heads/}
vcs="git"
color=$(git diff-index --quiet HEAD 2>/dev/null && echo "Green" || echo "Red")
#echo $color
return 0
}
svn_dir() {
[ -d ".svn" ] || return 1
base_dir="."
#while [ -d "$base_dir/.svn" ]; do base_dir="$base_dir/.."; done
while [ ! -d "$base_dir/.svn" ]; do base_dir="$base_dir/.."; [ $(readlink "${base_dir}") = "/" ] && return 1; done
base_dir=$(readlink "$base_dir")
sub_dir=$(sub_dir "${base_dir}")
ref=$(svn info "$base_dir" | awk '/^URL/ { sub(".*/","",$0); r=$0 } /^Revision/ { sub("[^0-9]*","",$0); print r":"$0 }')
vcs="svn"
color=$(echo "Green")
}
hg_dir() {
base_dir="."
while [ ! -d "$base_dir/.hg" ]; do base_dir="$base_dir/.."; [ $(readlink "${base_dir}") = "/" ] && return 1; done
base_dir=$(readlink "$base_dir")
sub_dir=$(sub_dir "${base_dir}")
ref=$(hg prompt "{branch}") #$(< "${base_dir}/.hg/branch")
vcs="hg"
# which is faster?
#color=$(hg summary | grep -q '(clean)' && echo "Green" || echo "Red")
color=Green #$([ "$(hg diff)" == "" ] && echo "Green" || echo "Red")
#color=$([ "$(hg prompt "{status}")" == "!" ] && echo "Red" || echo "Green")
}
if [ -d ".git" ];
then
if [ -d ".hg" ];
then
svn_dir ||
hg_dir ||
git_dir ||
base_dir="$PWD"
else
svn_dir ||
git_dir ||
hg_dir ||
base_dir="$PWD"
fi
else
svn_dir ||
hg_dir ||
git_dir ||
base_dir="$PWD"
fi
if [ "$vcs" != "" ];
then
# alternative approach
#color1=\$$color
#color2=`eval echo $color1`
color2=${!color}
export __vcs_color=$color2
export __vcs_branch=" $ref"
if [ $color == Green ];
then
export tailchar="๏ "
else
export tailchar="﹅ "
fi
else
export __vcs_color=""
export __vcs_branch=""
export tailchar="$ "
fi
}