forked from oconnor663/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hg-prompt
141 lines (137 loc) · 4.26 KB
/
hg-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
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
# Determines the "branch" of the current repo and emits it.
# For use in generating the prompt.
# This is portable to both zsh and bash and works in both
# git and mercurial repos and aims to avoid invoking the
# command line utilities for speed of prompt updates
# To use from zsh:
# NOTE! the single quotes are important; if you use double quotes
# then the prompt won't change when you chdir or checkout different
# branches!
#
# . /mnt/vol/engshare/admin/scripts/scm-prompt
# setopt PROMPT_SUBST
# export PS1='$(_dotfiles_scm_info)$USER@%m:%~%% '
# To use from bash:
#
# . /mnt/vol/engshare/admin/scripts/scm-prompt
# export PS1="\$(_dotfiles_scm_info)\u@\h:\W\$ "
#
# NOTE! You *EITHER* need to single-quote the whole thing *OR* back-slash
# the $(...) (as above), but not both. Which one you use depends on if
# you need the rest of your PS1 to interpolate variables.
_dotfiles_scm_info()
{
# find out if we're in a git or hg repo by looking for the control dir
local d git hg fmt
fmt=$1
if [[ -z "$fmt" ]]; then
if [ -n "$WANT_OLD_SCM_PROMPT" ]; then
fmt="%s"
else
# Be compatable with __git_ps1. In particular:
# - provide a space for the user so that they don't have to have
# random extra spaces in their prompt when not in a repo
# - provide parens so it's differentiated from other crap in their prompt
fmt=" (%s)"
fi
fi
d=$PWD
while : ; do
if test -d "$d/.git" ; then
git=$d
break
elif test -d "$d/.hg" ; then
hg=$d
break
fi
test "$d" = / && break
d=$(cd -P "$d/.." && echo "$PWD")
done
local br
if test -n "$hg" ; then
local extra
if [ -f "$hg/.hg/bisect.state" ]; then
extra="|BISECT"
elif [ -f "$hg/.hg/histedit-state" ]; then
extra="|HISTEDIT"
elif [ -f "$hg/.hg/graftstate" ]; then
extra="|GRAFT"
elif [ -f "$hg/.hg/unshelverebasestate" ]; then
extra="|UNSHELVE"
elif [ -f "$hg/.hg/rebasestate" ]; then
extra="|REBASE"
elif [ -d "$hg/.hg/merge" ]; then
extra="|MERGE"
fi
local dirstate=$(test -f $hg/.hg/dirstate && \
hexdump -vn 20 -e '1/1 "%02x"' $hg/.hg/dirstate || \
echo "empty")
local current="$hg/.hg/bookmarks.current"
if [[ -f "$current" ]]; then
br=$(cat "$current")
# check to see if active bookmark needs update
local marks="$hg/.hg/bookmarks"
if [ -f "$hg/.hg/sharedpath" ]; then
marks="`cat $hg/.hg/sharedpath`/bookmarks"
fi
if [[ -z "$extra" ]] && [[ -f "$marks" ]]; then
local markstate=$(grep --color=never " $br$" "$marks" | cut -f 1 -d ' ')
if [[ $markstate != $dirstate ]]; then
extra="|UPDATE_NEEDED"
fi
fi
else
br=$(echo $dirstate | cut -c 1-7)
fi
#local remote="$hg/.hg/remotenames"
#if [[ -f "$remote" ]]; then
# local marks=$(grep --color=never "^$dirstate bookmarks" "$remote" | \
# cut -f 3 -d ' ' | tr '\n' '|' | sed 's/.$//')
# if [[ -n "$marks" ]]; then
# br="$br|$marks"
# fi
#fi
local branch
if [[ -f $hg/.hg/branch ]]; then
branch=$(cat $hg/.hg/branch)
if [[ $branch != "default" ]]; then
br="$br|$branch"
fi
fi
br="$br$extra"
elif test -n "$git" ; then
if test -f "$git/.git/HEAD" ; then
read br < "$git/.git/HEAD"
case $br in
ref:\ refs/heads/*) br=${br#ref: refs/heads/} ;;
*) br=$(echo $br | cut -c 1-7) ;;
esac
if [ -f "$git/.git/rebase-merge/interactive" ]; then
b="$(cat "$git/.git/rebase-merge/head-name")"
b=${b#refs/heads/}
br="$br|REBASE-i|$b"
elif [ -d "$git/.git/rebase-merge" ]; then
b="$(cat "$git/.git/rebase-merge/head-name")"
b=${b#refs/heads/}
br="br|REBASE-m|$b"
else
if [ -d "$git/.git/rebase-apply" ]; then
if [ -f "$git/.git/rebase-apply/rebasing" ]; then
br="$br|REBASE"
elif [ -f "$git/.git/rebase-apply/applying" ]; then
br="$br|AM"
else
br="$br|AM/REBASE"
fi
elif [ -f "$git/.git/MERGE_HEAD" ]; then
br="$br|MERGE"
elif [ -f "$git/.git/BISECT_LOG" ]; then
br="$br|BISECT"
fi
fi
fi
fi
if [ -n "$br" ]; then
printf "$fmt" "$br"
fi
}