git-prompt.sh: document GIT_PS1_STATESEPARATOR
[git/mingw/j6t.git] / contrib / completion / git-prompt.sh
blobbe5467bb45d2c77baa261df7c1fb0f0985dafec6
1 # bash/zsh git prompt support
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Distributed under the GNU General Public License, version 2.0.
6 # This script allows you to see the current branch in your prompt.
8 # To enable:
10 # 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
11 # 2) Add the following line to your .bashrc/.zshrc:
12 # source ~/.git-prompt.sh
13 # 3a) Change your PS1 to call __git_ps1 as
14 # command-substitution:
15 # Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
16 # ZSH: PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
17 # the optional argument will be used as format string.
18 # 3b) Alternatively, if you are using bash, __git_ps1 can be
19 # used for PROMPT_COMMAND with two parameters, <pre> and
20 # <post>, which are strings you would put in $PS1 before
21 # and after the status string generated by the git-prompt
22 # machinery. e.g.
23 # PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
24 # will show username, at-sign, host, colon, cwd, then
25 # various status string, followed by dollar and SP, as
26 # your prompt.
27 # Optionally, you can supply a third argument with a printf
28 # format string to finetune the output of the branch status
30 # The argument to __git_ps1 will be displayed only if you are currently
31 # in a git repository. The %s token will be the name of the current
32 # branch.
34 # In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value,
35 # unstaged (*) and staged (+) changes will be shown next to the branch
36 # name. You can configure this per-repository with the
37 # bash.showDirtyState variable, which defaults to true once
38 # GIT_PS1_SHOWDIRTYSTATE is enabled.
40 # You can also see if currently something is stashed, by setting
41 # GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
42 # then a '$' will be shown next to the branch name.
44 # If you would like to see if there're untracked files, then you can set
45 # GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're untracked
46 # files, then a '%' will be shown next to the branch name. You can
47 # configure this per-repository with the bash.showUntrackedFiles
48 # variable, which defaults to true once GIT_PS1_SHOWUNTRACKEDFILES is
49 # enabled.
51 # If you would like to see the difference between HEAD and its upstream,
52 # set GIT_PS1_SHOWUPSTREAM="auto". A "<" indicates you are behind, ">"
53 # indicates you are ahead, "<>" indicates you have diverged and "="
54 # indicates that there is no difference. You can further control
55 # behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
56 # of values:
58 # verbose show number of commits ahead/behind (+/-) upstream
59 # legacy don't use the '--count' option available in recent
60 # versions of git-rev-list
61 # git always compare HEAD to @{upstream}
62 # svn always compare HEAD to your SVN upstream
64 # You can change the separator between the branch name and the above
65 # state symbols by setting GIT_PS1_STATESEPARATOR. The default separator
66 # is SP.
68 # By default, __git_ps1 will compare HEAD to your SVN upstream if it can
69 # find one, or @{upstream} otherwise. Once you have set
70 # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
71 # setting the bash.showUpstream config variable.
73 # If you would like to see more information about the identity of
74 # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
75 # to one of these values:
77 # contains relative to newer annotated tag (v1.6.3.2~35)
78 # branch relative to newer tag or branch (master~4)
79 # describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
80 # default exactly matching tag
82 # If you would like a colored hint about the current dirty state, set
83 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
84 # the colored output of "git status -sb".
86 # __gitdir accepts 0 or 1 arguments (i.e., location)
87 # returns location of .git repo
88 __gitdir ()
90 # Note: this function is duplicated in git-completion.bash
91 # When updating it, make sure you update the other one to match.
92 if [ -z "${1-}" ]; then
93 if [ -n "${__git_dir-}" ]; then
94 echo "$__git_dir"
95 elif [ -n "${GIT_DIR-}" ]; then
96 test -d "${GIT_DIR-}" || return 1
97 echo "$GIT_DIR"
98 elif [ -d .git ]; then
99 echo .git
100 else
101 git rev-parse --git-dir 2>/dev/null
103 elif [ -d "$1/.git" ]; then
104 echo "$1/.git"
105 else
106 echo "$1"
110 # stores the divergence from upstream in $p
111 # used by GIT_PS1_SHOWUPSTREAM
112 __git_ps1_show_upstream ()
114 local key value
115 local svn_remote svn_url_pattern count n
116 local upstream=git legacy="" verbose=""
118 svn_remote=()
119 # get some config options from git-config
120 local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
121 while read -r key value; do
122 case "$key" in
123 bash.showupstream)
124 GIT_PS1_SHOWUPSTREAM="$value"
125 if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
126 p=""
127 return
130 svn-remote.*.url)
131 svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
132 svn_url_pattern+="\\|$value"
133 upstream=svn+git # default upstream is SVN if available, else git
135 esac
136 done <<< "$output"
138 # parse configuration values
139 for option in ${GIT_PS1_SHOWUPSTREAM}; do
140 case "$option" in
141 git|svn) upstream="$option" ;;
142 verbose) verbose=1 ;;
143 legacy) legacy=1 ;;
144 esac
145 done
147 # Find our upstream
148 case "$upstream" in
149 git) upstream="@{upstream}" ;;
150 svn*)
151 # get the upstream from the "git-svn-id: ..." in a commit message
152 # (git-svn uses essentially the same procedure internally)
153 local svn_upstream=($(git log --first-parent -1 \
154 --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
155 if [[ 0 -ne ${#svn_upstream[@]} ]]; then
156 svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
157 svn_upstream=${svn_upstream%@*}
158 local n_stop="${#svn_remote[@]}"
159 for ((n=1; n <= n_stop; n++)); do
160 svn_upstream=${svn_upstream#${svn_remote[$n]}}
161 done
163 if [[ -z "$svn_upstream" ]]; then
164 # default branch name for checkouts with no layout:
165 upstream=${GIT_SVN_ID:-git-svn}
166 else
167 upstream=${svn_upstream#/}
169 elif [[ "svn+git" = "$upstream" ]]; then
170 upstream="@{upstream}"
173 esac
175 # Find how many commits we are ahead/behind our upstream
176 if [[ -z "$legacy" ]]; then
177 count="$(git rev-list --count --left-right \
178 "$upstream"...HEAD 2>/dev/null)"
179 else
180 # produce equivalent output to --count for older versions of git
181 local commits
182 if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
183 then
184 local commit behind=0 ahead=0
185 for commit in $commits
187 case "$commit" in
188 "<"*) ((behind++)) ;;
189 *) ((ahead++)) ;;
190 esac
191 done
192 count="$behind $ahead"
193 else
194 count=""
198 # calculate the result
199 if [[ -z "$verbose" ]]; then
200 case "$count" in
201 "") # no upstream
202 p="" ;;
203 "0 0") # equal to upstream
204 p="=" ;;
205 "0 "*) # ahead of upstream
206 p=">" ;;
207 *" 0") # behind upstream
208 p="<" ;;
209 *) # diverged from upstream
210 p="<>" ;;
211 esac
212 else
213 case "$count" in
214 "") # no upstream
215 p="" ;;
216 "0 0") # equal to upstream
217 p=" u=" ;;
218 "0 "*) # ahead of upstream
219 p=" u+${count#0 }" ;;
220 *" 0") # behind upstream
221 p=" u-${count% 0}" ;;
222 *) # diverged from upstream
223 p=" u+${count#* }-${count% *}" ;;
224 esac
230 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
231 # when called from PS1 using command substitution
232 # in this mode it prints text to add to bash PS1 prompt (includes branch name)
234 # __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
235 # in that case it _sets_ PS1. The arguments are parts of a PS1 string.
236 # when two arguments are given, the first is prepended and the second appended
237 # to the state string when assigned to PS1.
238 # The optional third parameter will be used as printf format string to further
239 # customize the output of the git-status string.
240 # In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
241 __git_ps1 ()
243 local pcmode=no
244 local detached=no
245 local ps1pc_start='\u@\h:\w '
246 local ps1pc_end='\$ '
247 local printf_format=' (%s)'
249 case "$#" in
250 2|3) pcmode=yes
251 ps1pc_start="$1"
252 ps1pc_end="$2"
253 printf_format="${3:-$printf_format}"
255 0|1) printf_format="${1:-$printf_format}"
257 *) return
259 esac
261 local g="$(__gitdir)"
262 if [ -z "$g" ]; then
263 if [ $pcmode = yes ]; then
264 #In PC mode PS1 always needs to be set
265 PS1="$ps1pc_start$ps1pc_end"
267 else
268 local r=""
269 local b=""
270 local step=""
271 local total=""
272 if [ -d "$g/rebase-merge" ]; then
273 b="$(cat "$g/rebase-merge/head-name")"
274 step=$(cat "$g/rebase-merge/msgnum")
275 total=$(cat "$g/rebase-merge/end")
276 if [ -f "$g/rebase-merge/interactive" ]; then
277 r="|REBASE-i"
278 else
279 r="|REBASE-m"
281 else
282 if [ -d "$g/rebase-apply" ]; then
283 step=$(cat "$g/rebase-apply/next")
284 total=$(cat "$g/rebase-apply/last")
285 if [ -f "$g/rebase-apply/rebasing" ]; then
286 r="|REBASE"
287 elif [ -f "$g/rebase-apply/applying" ]; then
288 r="|AM"
289 else
290 r="|AM/REBASE"
292 elif [ -f "$g/MERGE_HEAD" ]; then
293 r="|MERGING"
294 elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
295 r="|CHERRY-PICKING"
296 elif [ -f "$g/REVERT_HEAD" ]; then
297 r="|REVERTING"
298 elif [ -f "$g/BISECT_LOG" ]; then
299 r="|BISECTING"
302 b="$(git symbolic-ref HEAD 2>/dev/null)" || {
303 detached=yes
304 b="$(
305 case "${GIT_PS1_DESCRIBE_STYLE-}" in
306 (contains)
307 git describe --contains HEAD ;;
308 (branch)
309 git describe --contains --all HEAD ;;
310 (describe)
311 git describe HEAD ;;
312 (* | default)
313 git describe --tags --exact-match HEAD ;;
314 esac 2>/dev/null)" ||
316 b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
317 b="unknown"
318 b="($b)"
322 if [ -n "$step" ] && [ -n "$total" ]; then
323 r="$r $step/$total"
326 local w=""
327 local i=""
328 local s=""
329 local u=""
330 local c=""
331 local p=""
333 if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
334 if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
335 c="BARE:"
336 else
337 b="GIT_DIR!"
339 elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
340 if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
341 [ "$(git config --bool bash.showDirtyState)" != "false" ]
342 then
343 git diff --no-ext-diff --quiet --exit-code || w="*"
344 if git rev-parse --quiet --verify HEAD >/dev/null; then
345 git diff-index --cached --quiet HEAD -- || i="+"
346 else
347 i="#"
350 if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
351 git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
354 if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
355 [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
356 [ -n "$(git ls-files --others --exclude-standard)" ]
357 then
358 u="%${ZSH_VERSION+%}"
361 if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
362 __git_ps1_show_upstream
366 local z="${GIT_PS1_STATESEPARATOR-" "}"
367 local f="$w$i$s$u"
368 if [ $pcmode = yes ]; then
369 local gitstring=
370 if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
371 local c_red='\e[31m'
372 local c_green='\e[32m'
373 local c_lblue='\e[1;34m'
374 local c_clear='\e[0m'
375 local bad_color=$c_red
376 local ok_color=$c_green
377 local branch_color="$c_clear"
378 local flags_color="$c_lblue"
379 local branchstring="$c${b##refs/heads/}"
381 if [ $detached = no ]; then
382 branch_color="$ok_color"
383 else
384 branch_color="$bad_color"
387 # Setting gitstring directly with \[ and \] around colors
388 # is necessary to prevent wrapping issues!
389 gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
391 if [ -n "$w$i$s$u$r$p" ]; then
392 gitstring="$gitstring$z"
394 if [ "$w" = "*" ]; then
395 gitstring="$gitstring\[$bad_color\]$w"
397 if [ -n "$i" ]; then
398 gitstring="$gitstring\[$ok_color\]$i"
400 if [ -n "$s" ]; then
401 gitstring="$gitstring\[$flags_color\]$s"
403 if [ -n "$u" ]; then
404 gitstring="$gitstring\[$bad_color\]$u"
406 gitstring="$gitstring\[$c_clear\]$r$p"
407 else
408 gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
410 gitstring=$(printf -- "$printf_format" "$gitstring")
411 PS1="$ps1pc_start$gitstring$ps1pc_end"
412 else
413 # NO color option unless in PROMPT_COMMAND mode
414 printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"