-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-ps1.sh
233 lines (209 loc) · 6.68 KB
/
git-ps1.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env bash
#==============================================================================
# The MIT License
#
# Copyright (c) 2010-, Frédéric Menou
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#==============================================================================
# Prompt Bash pour l'affichage du statut Git (si dans un dépôt Git)
# A ajouter dans votre ~/.bashrc :
# TBX=$HOME/posix-toolbox # emplacement à choisir
# PATH=$TBX/src:$PATH
# GIT_BASH=$TBX/src/bash/git-ps1
# if [ -f $GIT_BASH ]; then
# . $GIT_BASH
# fi
# Scavenged from Git 1.6.5.x contrib/completion/git_completion.bash
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
# returns text to add to bash PS1 prompt (includes branch name)
# shellcheck disable=SC2120
__gitdir ()
{
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
echo "$__git_dir"
elif [ -d .git ]; then
echo .git
else
git rev-parse --git-dir 2>/dev/null
fi
elif [ -d "$1/.git" ]; then
echo "$1/.git"
else
echo "$1"
fi
}
__git_ps1 ()
{
local g
g="$(__gitdir)"
if [ -n "$g" ]; then
local disable
local shorten
disable="$(git config --local --int --get ps1.disable)"
shorten="$(git config --int --get ps1.shorten)"
if [ "$disable" = "1" ]; then
git_pwd=$(git-pwd)
printf " (%s:%s) " "-" "${git_pwd}"
else
local r
local b
if [ -f "$g/rebase-merge/interactive" ]; then
r="|REBASE-i"
b="$(cat "$g/rebase-merge/head-name")"
elif [ -d "$g/rebase-merge" ]; then
r="|REBASE-m"
b="$(cat "$g/rebase-merge/head-name")"
else
if [ -d "$g/rebase-apply" ]; then
if [ -f "$g/rebase-apply/rebasing" ]; then
r="|REBASE"
elif [ -f "$g/rebase-apply/applying" ]; then
r="|AM"
else
r="|AM/REBASE"
fi
elif [ -f "$g/MERGE_HEAD" ]; then
r="|MERGING"
elif [ -f "$g/BISECT_LOG" ]; then
r="|BISECTING"
fi
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
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 --exact-match HEAD ;;
esac 2>/dev/null)" ||
b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
b="unknown"
b="($b)"
}
fi
local w
local i
local s
local u
local c
if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
c="BARE:"
else
b="GIT_DIR!"
fi
elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
git diff --no-ext-diff --ignore-submodules \
--quiet --exit-code || w="*"
if git rev-parse --quiet --verify HEAD >/dev/null; then
git diff-index --cached --quiet \
--ignore-submodules HEAD -- || i="+"
else
i="#"
fi
fi
fi
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
fi
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
u="%"
fi
fi
fi
local branch_name=${b##refs/heads/}
if [ "$shorten" == "1" ]; then
branch_name=$(short-path "${b##refs/heads/}")
fi
if [ -n "${1-}" ]; then
# shellcheck disable=SC2059
printf "$1" "$c$branch_name$w$i$s$u$r" # FIXME: this printf expression is broken
else
git_pwd=$(git-pwd)
if [ "$shorten" == "1" ]; then
git_pwd=$(short-path "$git_pwd")
fi
if [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
printf " (%s:%s)" "$c$branch_name$w$i$s$u$r" "${git_pwd}"
else
printf " (%s)" "$c$branch_name$w$i$s$u$r"
fi
fi
fi
fi
}
export GIT_PS1_SHOWDIRTYSTATE=1 GIT_PS1_SHOWUNTRACKEDFILES=1 GIT_PS1_SHOWSTASHSTATE=1
prd_() {
prd "$@"
}
__pwd() {
local shorten
local in_git_repo
local path
shorten="$(git config --int --get ps1.shorten)"
in_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
path="$(prd_ )"
if [ "true" == "$in_git_repo" ]; then
path="$(git-prd)"
fi
if [ "1" == "$shorten" ]; then
path="$(short-path "$path")"
fi
printf "%s" "$path"
}
DEFAULT_THRESHOLD=100
function getThresholdFromConfiguration {
git config --int --get "check.threshold" || echo $DEFAULT_THRESHOLD
}
# En fait l'important ici c'est le "\033[1;32m\]$(__git_ps1)\[\033[0m\]", le reste c'est mon prompt usuel…
now() {
date +%H:%M
}
find-up() {
local filename=$1
if [ -r "$filename" ]; then
echo "$filename"
fi
}
update_tmux_title() {
local tmux_title_file
tmux_title_file=$(find-up .tmux-title)
if [ -n "$tmux_title_file" ]; then
name=$(head -1 < "$tmux_title_file")
printf "\033k%s\033\\" "$name"
fi
}
color() {
printf "\033[%sm" "$1"
}
__nix_shell() {
if [ -n "$IN_NIX_SHELL" ]
then
echo " nix-shell"
fi
}
PS1='$(update_tmux_title)\[$(color "0;237")\]$(now)\[$(color "0")\] \[$(color "0;96")\]\u\[$(color "0")\] \[$(color "0;33")\]$(__pwd)\[$(color "0")\]\[$(color "1;32")\]$(__git_ps1)\[$(color "0")\]\[$(color "1;33")\]$(__nix_shell)\[$(color "0")\] $ '