forked from syscools/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__git_ps1.sh
More file actions
executable file
·191 lines (190 loc) · 5.91 KB
/
__git_ps1.sh
File metadata and controls
executable file
·191 lines (190 loc) · 5.91 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
__git_eread () {
local f="$1";
shift;
test -r "$f" && read "$@" < "$f"
}
__git_ps1 () {
[ ! -d .git ] && return
local exit=$?;
local pcmode=no;
local detached=no;
local ps1pc_start='\u@\h:\w ';
local ps1pc_end='\$ ';
local printf_format=' (%s)';
case "$#" in
2 | 3)
pcmode=yes;
ps1pc_start="$1";
ps1pc_end="$2";
printf_format="${3:-$printf_format}";
PS1="$ps1pc_start$ps1pc_end"
;;
0 | 1)
printf_format="${1:-$printf_format}"
;;
*)
return $exit
;;
esac;
local ps1_expanded=yes;
[ -z "$ZSH_VERSION" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no;
[ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no;
local repo_info rev_parse_exit_code;
repo_info="$(git rev-parse --git-dir --is-inside-git-dir --is-bare-repository --is-inside-work-tree --short HEAD 2>/dev/null)";
rev_parse_exit_code="$?";
if [ -z "$repo_info" ]; then
return $exit;
fi;
local short_sha;
if [ "$rev_parse_exit_code" = "0" ]; then
short_sha="${repo_info##*
}";
repo_info="${repo_info%
*}";
fi;
local inside_worktree="${repo_info##*
}";
repo_info="${repo_info%
*}";
local bare_repo="${repo_info##*
}";
repo_info="${repo_info%
*}";
local inside_gitdir="${repo_info##*
}";
local g="${repo_info%
*}";
if [ "true" = "$inside_worktree" ] && [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] && [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] && git check-ignore -q .; then
return $exit;
fi;
local r="";
local b="";
local step="";
local total="";
if [ -d "$g/rebase-merge" ]; then
__git_eread "$g/rebase-merge/head-name" b;
__git_eread "$g/rebase-merge/msgnum" step;
__git_eread "$g/rebase-merge/end" total;
if [ -f "$g/rebase-merge/interactive" ]; then
r="|REBASE-i";
else
r="|REBASE-m";
fi;
else
if [ -d "$g/rebase-apply" ]; then
__git_eread "$g/rebase-apply/next" step;
__git_eread "$g/rebase-apply/last" total;
if [ -f "$g/rebase-apply/rebasing" ]; then
__git_eread "$g/rebase-apply/head-name" b;
r="|REBASE";
else
if [ -f "$g/rebase-apply/applying" ]; then
r="|AM";
else
r="|AM/REBASE";
fi;
fi;
else
if [ -f "$g/MERGE_HEAD" ]; then
r="|MERGING";
else
if [ -f "$g/CHERRY_PICK_HEAD" ]; then
r="|CHERRY-PICKING";
else
if [ -f "$g/REVERT_HEAD" ]; then
r="|REVERTING";
else
if [ -f "$g/BISECT_LOG" ]; then
r="|BISECTING";
fi;
fi;
fi;
fi;
fi;
if [ -n "$b" ]; then
:;
else
if [ -h "$g/HEAD" ]; then
b="$(git symbolic-ref HEAD 2>/dev/null)";
else
local head="";
if ! __git_eread "$g/HEAD" head; then
return $exit;
fi;
b="${head#ref: }";
if [ "$head" = "$b" ]; then
detached=yes;
b="$(
case "${GIT_PS1_DESCRIBE_STYLE-}" in
(contains)
git describe --contains HEAD ;;
(branch)
git describe --contains --all HEAD ;;
(describe)
git describe HEAD ;;
(* | default)
git describe --tags --exact-match HEAD ;;
esac 2>/dev/null)" || b="$short_sha...";
b="($b)";
fi;
fi;
fi;
fi;
if [ -n "$step" ] && [ -n "$total" ]; then
r="$r $step/$total";
fi;
local w="";
local i="";
local s="";
local u="";
local c="";
local p="";
if [ "true" = "$inside_gitdir" ]; then
if [ "true" = "$bare_repo" ]; then
c="BARE:";
else
b="GIT_DIR!";
fi;
else
if [ "true" = "$inside_worktree" ]; then
if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] && [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
git diff --no-ext-diff --quiet || w="*";
git diff --no-ext-diff --cached --quiet || i="+";
if [ -z "$short_sha" ] && [ -z "$i" ]; then
i="#";
fi;
fi;
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] && git rev-parse --verify --quiet refs/stash > /dev/null; then
s="$";
fi;
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] && [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] && git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' > /dev/null 2> /dev/null; then
u="%${ZSH_VERSION+%}";
fi;
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
__git_ps1_show_upstream;
fi;
fi;
fi;
local z="${GIT_PS1_STATESEPARATOR-" "}";
if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
__git_ps1_colorize_gitstring;
fi;
b=${b##refs/heads/};
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
__git_ps1_branch_name=$b;
b="\${__git_ps1_branch_name}";
fi;
local f="$w$i$s$u";
local gitstring="$c$b${f:+$z$f}$r$p";
if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then
gitstring=$(printf -- "$printf_format" "$gitstring");
else
printf -v gitstring -- "$printf_format" "$gitstring";
fi;
PS1="$ps1pc_start$gitstring$ps1pc_end";
else
printf -- "$printf_format" "$gitstring";
fi;
return $exit
}