/
hg-prompt.sh
84 lines (75 loc) · 2.71 KB
/
hg-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
# bash hg prompt support
#
# Copyright (c) 2018 Stephane Moore
# Distributed under the MIT License.
#
# To enable:
#
# 1) Copy this file to somewhere (e.g. ~/.hg-prompt.sh).
# 2) Add the following line to your .bashrc:
# source ~/.hg-prompt.sh
# 3) Change your PS1 to call __hg_ps1:
# Bash: PS1='[\u@\h \W$(__hg_ps1 " (%s)")]\$ '
# the optional argument will be used as format string.
# Constructs a prompt string for the Mercurial repository containing the current working directory
# or nothing if the current working directory is not within a Mercurial repository.
#
# If the HG_PS1_TAGSTEMPLATE environment variable is set, tags will be
# retrieved using the specified template for tags. If this environment
# variable is not set, the template "{tags}" will be used.
__hg_ps1() {
local exit_code=$?
local hg_root="$(hg root 2>/dev/null)"
if [ -z "${hg_root}" ]; then
return ${exit_code}
fi
local printf_format=" (%s)"
if [ "$#" -ge 1 ]; then
printf_format="$1"
fi
# Show * for modified files, + for added files, and % for untracked files.
local hg_display_modifiers=""
local hg_status_summary="$(hg status | cut -c 1)"
if [[ "${hg_status_summary}" =~ .*M.* ]]; then
hg_display_modifiers="${hg_display_modifiers}*"
fi
if [[ "${hg_status_summary}" =~ .*A.* ]]; then
hg_display_modifiers="${hg_display_modifiers}+"
fi
if [[ "${hg_status_summary}" =~ .*\?.* ]]; then
hg_display_modifiers="${hg_display_modifiers}%"
fi
# Begin constructing the prompt string moving backwards.
local hg_prompt=""
if [ "${hg_display_modifiers}" != "" ]; then
hg_prompt=" ${hg_display_modifiers}"
fi
# Check for an active bookmark.
local hg_current_bookmark="${hg_root}/.hg/bookmarks.current"
if [ -e "${hg_current_bookmark}" ]; then
local hg_bookmark_name=$(cat "${hg_current_bookmark}")
hg_prompt="${hg_bookmark_name}${hg_prompt}"
printf -- "$1" "${hg_prompt}"
return ${exit_code}
fi
# Check for a tag on the current commit.
local hg_default_tags_template="{tags}"
local hg_tags_template="${HG_PS1_TAGSTEMPLATE-$hg_default_tags_template}"
local hg_tags="$(hg log -r . --template "${hg_tags_template}")"
local hg_tag_array=( $hg_tags )
if [ ${#hg_tag_array[@]} -ne 0 ]; then
for hg_tag in "${hg_tag_array[@]}"
do
if [ "${hg_tag}" != "tip" ]; then
hg_prompt="${hg_tag}${hg_prompt}"
printf -- "$1" "${hg_prompt}"
return ${exit_code}
fi
done
fi
# In the absence of other information, simply display the changeset identifier.
local hg_changeset_id_hash="$(hg log -r . --template '({node|short})')"
hg_prompt="${hg_changeset_id_hash}${hg_prompt}"
printf -- "$1" "${hg_prompt}"
return ${exit_code}
}