Merge branch 'jc/maint-fbsd-sh-ifs-workaround' into maint
[git/git.git] / contrib / completion / git-prompt.sh
1 # bash/zsh git prompt support
2 #
3 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
4 # Distributed under the GNU General Public License, version 2.0.
5 #
6 # This script allows you to see the current branch in your prompt.
7 #
8 # To enable:
9 #
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
29 #
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.
33 #
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.
39 #
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.
43 #
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.
47 #
48 # If you would like to see the difference between HEAD and its upstream,
49 # set GIT_PS1_SHOWUPSTREAM="auto". A "<" indicates you are behind, ">"
50 # indicates you are ahead, "<>" indicates you have diverged and "="
51 # indicates that there is no difference. You can further control
52 # behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
53 # of values:
54 #
55 # verbose show number of commits ahead/behind (+/-) upstream
56 # legacy don't use the '--count' option available in recent
57 # versions of git-rev-list
58 # git always compare HEAD to @{upstream}
59 # svn always compare HEAD to your SVN upstream
60 #
61 # By default, __git_ps1 will compare HEAD to your SVN upstream if it can
62 # find one, or @{upstream} otherwise. Once you have set
63 # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
64 # setting the bash.showUpstream config variable.
65 #
66 # If you would like to see more information about the identity of
67 # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
68 # to one of these values:
69 #
70 # contains relative to newer annotated tag (v1.6.3.2~35)
71 # branch relative to newer tag or branch (master~4)
72 # describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
73 # default exactly matching tag
74 #
75 # If you would like a colored hint about the current dirty state, set
76 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
77 # the colored output of "git status -sb".
78
79 # __gitdir accepts 0 or 1 arguments (i.e., location)
80 # returns location of .git repo
81 __gitdir ()
82 {
83 # Note: this function is duplicated in git-completion.bash
84 # When updating it, make sure you update the other one to match.
85 if [ -z "${1-}" ]; then
86 if [ -n "${__git_dir-}" ]; then
87 echo "$__git_dir"
88 elif [ -n "${GIT_DIR-}" ]; then
89 test -d "${GIT_DIR-}" || return 1
90 echo "$GIT_DIR"
91 elif [ -d .git ]; then
92 echo .git
93 else
94 git rev-parse --git-dir 2>/dev/null
95 fi
96 elif [ -d "$1/.git" ]; then
97 echo "$1/.git"
98 else
99 echo "$1"
100 fi
101 }
102
103 # stores the divergence from upstream in $p
104 # used by GIT_PS1_SHOWUPSTREAM
105 __git_ps1_show_upstream ()
106 {
107 local key value
108 local svn_remote svn_url_pattern count n
109 local upstream=git legacy="" verbose=""
110
111 svn_remote=()
112 # get some config options from git-config
113 local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
114 while read -r key value; do
115 case "$key" in
116 bash.showupstream)
117 GIT_PS1_SHOWUPSTREAM="$value"
118 if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
119 p=""
120 return
121 fi
122 ;;
123 svn-remote.*.url)
124 svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
125 svn_url_pattern+="\\|$value"
126 upstream=svn+git # default upstream is SVN if available, else git
127 ;;
128 esac
129 done <<< "$output"
130
131 # parse configuration values
132 for option in ${GIT_PS1_SHOWUPSTREAM}; do
133 case "$option" in
134 git|svn) upstream="$option" ;;
135 verbose) verbose=1 ;;
136 legacy) legacy=1 ;;
137 esac
138 done
139
140 # Find our upstream
141 case "$upstream" in
142 git) upstream="@{upstream}" ;;
143 svn*)
144 # get the upstream from the "git-svn-id: ..." in a commit message
145 # (git-svn uses essentially the same procedure internally)
146 local svn_upstream=($(git log --first-parent -1 \
147 --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
148 if [[ 0 -ne ${#svn_upstream[@]} ]]; then
149 svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
150 svn_upstream=${svn_upstream%@*}
151 local n_stop="${#svn_remote[@]}"
152 for ((n=1; n <= n_stop; n++)); do
153 svn_upstream=${svn_upstream#${svn_remote[$n]}}
154 done
155
156 if [[ -z "$svn_upstream" ]]; then
157 # default branch name for checkouts with no layout:
158 upstream=${GIT_SVN_ID:-git-svn}
159 else
160 upstream=${svn_upstream#/}
161 fi
162 elif [[ "svn+git" = "$upstream" ]]; then
163 upstream="@{upstream}"
164 fi
165 ;;
166 esac
167
168 # Find how many commits we are ahead/behind our upstream
169 if [[ -z "$legacy" ]]; then
170 count="$(git rev-list --count --left-right \
171 "$upstream"...HEAD 2>/dev/null)"
172 else
173 # produce equivalent output to --count for older versions of git
174 local commits
175 if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
176 then
177 local commit behind=0 ahead=0
178 for commit in $commits
179 do
180 case "$commit" in
181 "<"*) ((behind++)) ;;
182 *) ((ahead++)) ;;
183 esac
184 done
185 count="$behind $ahead"
186 else
187 count=""
188 fi
189 fi
190
191 # calculate the result
192 if [[ -z "$verbose" ]]; then
193 case "$count" in
194 "") # no upstream
195 p="" ;;
196 "0 0") # equal to upstream
197 p="=" ;;
198 "0 "*) # ahead of upstream
199 p=">" ;;
200 *" 0") # behind upstream
201 p="<" ;;
202 *) # diverged from upstream
203 p="<>" ;;
204 esac
205 else
206 case "$count" in
207 "") # no upstream
208 p="" ;;
209 "0 0") # equal to upstream
210 p=" u=" ;;
211 "0 "*) # ahead of upstream
212 p=" u+${count#0 }" ;;
213 *" 0") # behind upstream
214 p=" u-${count% 0}" ;;
215 *) # diverged from upstream
216 p=" u+${count#* }-${count% *}" ;;
217 esac
218 fi
219
220 }
221
222
223 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
224 # when called from PS1 using command substitution
225 # in this mode it prints text to add to bash PS1 prompt (includes branch name)
226 #
227 # __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
228 # in that case it _sets_ PS1. The arguments are parts of a PS1 string.
229 # when two arguments are given, the first is prepended and the second appended
230 # to the state string when assigned to PS1.
231 # The optional third parameter will be used as printf format string to further
232 # customize the output of the git-status string.
233 # In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
234 __git_ps1 ()
235 {
236 local pcmode=no
237 local detached=no
238 local ps1pc_start='\u@\h:\w '
239 local ps1pc_end='\$ '
240 local printf_format=' (%s)'
241
242 case "$#" in
243 2|3) pcmode=yes
244 ps1pc_start="$1"
245 ps1pc_end="$2"
246 printf_format="${3:-$printf_format}"
247 ;;
248 0|1) printf_format="${1:-$printf_format}"
249 ;;
250 *) return
251 ;;
252 esac
253
254 local g="$(__gitdir)"
255 if [ -z "$g" ]; then
256 if [ $pcmode = yes ]; then
257 #In PC mode PS1 always needs to be set
258 PS1="$ps1pc_start$ps1pc_end"
259 fi
260 else
261 local r=""
262 local b=""
263 if [ -f "$g/rebase-merge/interactive" ]; then
264 r="|REBASE-i"
265 b="$(cat "$g/rebase-merge/head-name")"
266 elif [ -d "$g/rebase-merge" ]; then
267 r="|REBASE-m"
268 b="$(cat "$g/rebase-merge/head-name")"
269 else
270 if [ -d "$g/rebase-apply" ]; then
271 if [ -f "$g/rebase-apply/rebasing" ]; then
272 r="|REBASE"
273 elif [ -f "$g/rebase-apply/applying" ]; then
274 r="|AM"
275 else
276 r="|AM/REBASE"
277 fi
278 elif [ -f "$g/MERGE_HEAD" ]; then
279 r="|MERGING"
280 elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
281 r="|CHERRY-PICKING"
282 elif [ -f "$g/BISECT_LOG" ]; then
283 r="|BISECTING"
284 fi
285
286 b="$(git symbolic-ref HEAD 2>/dev/null)" || {
287 detached=yes
288 b="$(
289 case "${GIT_PS1_DESCRIBE_STYLE-}" in
290 (contains)
291 git describe --contains HEAD ;;
292 (branch)
293 git describe --contains --all HEAD ;;
294 (describe)
295 git describe HEAD ;;
296 (* | default)
297 git describe --tags --exact-match HEAD ;;
298 esac 2>/dev/null)" ||
299
300 b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
301 b="unknown"
302 b="($b)"
303 }
304 fi
305
306 local w=""
307 local i=""
308 local s=""
309 local u=""
310 local c=""
311 local p=""
312
313 if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
314 if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
315 c="BARE:"
316 else
317 b="GIT_DIR!"
318 fi
319 elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
320 if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
321 if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
322 git diff --no-ext-diff --quiet --exit-code || w="*"
323 if git rev-parse --quiet --verify HEAD >/dev/null; then
324 git diff-index --cached --quiet HEAD -- || i="+"
325 else
326 i="#"
327 fi
328 fi
329 fi
330 if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
331 git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
332 fi
333
334 if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
335 if [ -n "$(git ls-files --others --exclude-standard)" ]; then
336 u="%"
337 fi
338 fi
339
340 if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
341 __git_ps1_show_upstream
342 fi
343 fi
344
345 local f="$w$i$s$u"
346 if [ $pcmode = yes ]; then
347 local gitstring=
348 if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
349 local c_red='\e[31m'
350 local c_green='\e[32m'
351 local c_lblue='\e[1;34m'
352 local c_clear='\e[0m'
353 local bad_color=$c_red
354 local ok_color=$c_green
355 local branch_color="$c_clear"
356 local flags_color="$c_lblue"
357 local branchstring="$c${b##refs/heads/}"
358
359 if [ $detached = no ]; then
360 branch_color="$ok_color"
361 else
362 branch_color="$bad_color"
363 fi
364
365 # Setting gitstring directly with \[ and \] around colors
366 # is necessary to prevent wrapping issues!
367 gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
368
369 if [ -n "$w$i$s$u$r$p" ]; then
370 gitstring="$gitstring "
371 fi
372 if [ "$w" = "*" ]; then
373 gitstring="$gitstring\[$bad_color\]$w"
374 fi
375 if [ -n "$i" ]; then
376 gitstring="$gitstring\[$ok_color\]$i"
377 fi
378 if [ -n "$s" ]; then
379 gitstring="$gitstring\[$flags_color\]$s"
380 fi
381 if [ -n "$u" ]; then
382 gitstring="$gitstring\[$bad_color\]$u"
383 fi
384 gitstring="$gitstring\[$c_clear\]$r$p"
385 else
386 gitstring="$c${b##refs/heads/}${f:+ $f}$r$p"
387 fi
388 gitstring=$(printf -- "$printf_format" "$gitstring")
389 PS1="$ps1pc_start$gitstring$ps1pc_end"
390 else
391 # NO color option unless in PROMPT_COMMAND mode
392 printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"
393 fi
394 fi
395 }