Merge branch 'jk/no-more-pre-exec-callback'
[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 #    3) Change your PS1 to also show the current branch:
14 #         Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
15 #         ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
16 #
17 # The argument to __git_ps1 will be displayed only if you are currently
18 # in a git repository.  The %s token will be the name of the current
19 # branch.
20 #
21 # In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value,
22 # unstaged (*) and staged (+) changes will be shown next to the branch
23 # name.  You can configure this per-repository with the
24 # bash.showDirtyState variable, which defaults to true once
25 # GIT_PS1_SHOWDIRTYSTATE is enabled.
26 #
27 # You can also see if currently something is stashed, by setting
28 # GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
29 # then a '$' will be shown next to the branch name.
30 #
31 # If you would like to see if there're untracked files, then you can set
32 # GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're untracked
33 # files, then a '%' will be shown next to the branch name.
34 #
35 # If you would like to see the difference between HEAD and its upstream,
36 # set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates you are behind, ">"
37 # indicates you are ahead, "<>" indicates you have diverged and "="
38 # indicates that there is no difference. You can further control
39 # behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
40 # of values:
41 #
42 #     verbose       show number of commits ahead/behind (+/-) upstream
43 #     legacy        don't use the '--count' option available in recent
44 #                   versions of git-rev-list
45 #     git           always compare HEAD to @{upstream}
46 #     svn           always compare HEAD to your SVN upstream
47 #
48 # By default, __git_ps1 will compare HEAD to your SVN upstream if it can
49 # find one, or @{upstream} otherwise.  Once you have set
50 # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
51 # setting the bash.showUpstream config variable.
52
53 # __gitdir accepts 0 or 1 arguments (i.e., location)
54 # returns location of .git repo
55 __gitdir ()
56 {
57         # Note: this function is duplicated in git-completion.bash
58         # When updating it, make sure you update the other one to match.
59         if [ -z "${1-}" ]; then
60                 if [ -n "${__git_dir-}" ]; then
61                         echo "$__git_dir"
62                 elif [ -n "${GIT_DIR-}" ]; then
63                         test -d "${GIT_DIR-}" || return 1
64                         echo "$GIT_DIR"
65                 elif [ -d .git ]; then
66                         echo .git
67                 else
68                         git rev-parse --git-dir 2>/dev/null
69                 fi
70         elif [ -d "$1/.git" ]; then
71                 echo "$1/.git"
72         else
73                 echo "$1"
74         fi
75 }
76
77 # stores the divergence from upstream in $p
78 # used by GIT_PS1_SHOWUPSTREAM
79 __git_ps1_show_upstream ()
80 {
81         local key value
82         local svn_remote svn_url_pattern count n
83         local upstream=git legacy="" verbose=""
84
85         svn_remote=()
86         # get some config options from git-config
87         local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
88         while read -r key value; do
89                 case "$key" in
90                 bash.showupstream)
91                         GIT_PS1_SHOWUPSTREAM="$value"
92                         if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
93                                 p=""
94                                 return
95                         fi
96                         ;;
97                 svn-remote.*.url)
98                         svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
99                         svn_url_pattern+="\\|$value"
100                         upstream=svn+git # default upstream is SVN if available, else git
101                         ;;
102                 esac
103         done <<< "$output"
104
105         # parse configuration values
106         for option in ${GIT_PS1_SHOWUPSTREAM}; do
107                 case "$option" in
108                 git|svn) upstream="$option" ;;
109                 verbose) verbose=1 ;;
110                 legacy)  legacy=1  ;;
111                 esac
112         done
113
114         # Find our upstream
115         case "$upstream" in
116         git)    upstream="@{upstream}" ;;
117         svn*)
118                 # get the upstream from the "git-svn-id: ..." in a commit message
119                 # (git-svn uses essentially the same procedure internally)
120                 local svn_upstream=($(git log --first-parent -1 \
121                                         --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
122                 if [[ 0 -ne ${#svn_upstream[@]} ]]; then
123                         svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
124                         svn_upstream=${svn_upstream%@*}
125                         local n_stop="${#svn_remote[@]}"
126                         for ((n=1; n <= n_stop; n++)); do
127                                 svn_upstream=${svn_upstream#${svn_remote[$n]}}
128                         done
129
130                         if [[ -z "$svn_upstream" ]]; then
131                                 # default branch name for checkouts with no layout:
132                                 upstream=${GIT_SVN_ID:-git-svn}
133                         else
134                                 upstream=${svn_upstream#/}
135                         fi
136                 elif [[ "svn+git" = "$upstream" ]]; then
137                         upstream="@{upstream}"
138                 fi
139                 ;;
140         esac
141
142         # Find how many commits we are ahead/behind our upstream
143         if [[ -z "$legacy" ]]; then
144                 count="$(git rev-list --count --left-right \
145                                 "$upstream"...HEAD 2>/dev/null)"
146         else
147                 # produce equivalent output to --count for older versions of git
148                 local commits
149                 if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
150                 then
151                         local commit behind=0 ahead=0
152                         for commit in $commits
153                         do
154                                 case "$commit" in
155                                 "<"*) ((behind++)) ;;
156                                 *)    ((ahead++))  ;;
157                                 esac
158                         done
159                         count="$behind  $ahead"
160                 else
161                         count=""
162                 fi
163         fi
164
165         # calculate the result
166         if [[ -z "$verbose" ]]; then
167                 case "$count" in
168                 "") # no upstream
169                         p="" ;;
170                 "0      0") # equal to upstream
171                         p="=" ;;
172                 "0      "*) # ahead of upstream
173                         p=">" ;;
174                 *"      0") # behind upstream
175                         p="<" ;;
176                 *)          # diverged from upstream
177                         p="<>" ;;
178                 esac
179         else
180                 case "$count" in
181                 "") # no upstream
182                         p="" ;;
183                 "0      0") # equal to upstream
184                         p=" u=" ;;
185                 "0      "*) # ahead of upstream
186                         p=" u+${count#0 }" ;;
187                 *"      0") # behind upstream
188                         p=" u-${count%  0}" ;;
189                 *)          # diverged from upstream
190                         p=" u+${count#* }-${count%      *}" ;;
191                 esac
192         fi
193
194 }
195
196
197 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
198 # returns text to add to bash PS1 prompt (includes branch name)
199 __git_ps1 ()
200 {
201         local g="$(__gitdir)"
202         if [ -n "$g" ]; then
203                 local r=""
204                 local b=""
205                 if [ -f "$g/rebase-merge/interactive" ]; then
206                         r="|REBASE-i"
207                         b="$(cat "$g/rebase-merge/head-name")"
208                 elif [ -d "$g/rebase-merge" ]; then
209                         r="|REBASE-m"
210                         b="$(cat "$g/rebase-merge/head-name")"
211                 else
212                         if [ -d "$g/rebase-apply" ]; then
213                                 if [ -f "$g/rebase-apply/rebasing" ]; then
214                                         r="|REBASE"
215                                 elif [ -f "$g/rebase-apply/applying" ]; then
216                                         r="|AM"
217                                 else
218                                         r="|AM/REBASE"
219                                 fi
220                         elif [ -f "$g/MERGE_HEAD" ]; then
221                                 r="|MERGING"
222                         elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
223                                 r="|CHERRY-PICKING"
224                         elif [ -f "$g/BISECT_LOG" ]; then
225                                 r="|BISECTING"
226                         fi
227
228                         b="$(git symbolic-ref HEAD 2>/dev/null)" || {
229
230                                 b="$(
231                                 case "${GIT_PS1_DESCRIBE_STYLE-}" in
232                                 (contains)
233                                         git describe --contains HEAD ;;
234                                 (branch)
235                                         git describe --contains --all HEAD ;;
236                                 (describe)
237                                         git describe HEAD ;;
238                                 (* | default)
239                                         git describe --tags --exact-match HEAD ;;
240                                 esac 2>/dev/null)" ||
241
242                                 b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
243                                 b="unknown"
244                                 b="($b)"
245                         }
246                 fi
247
248                 local w=""
249                 local i=""
250                 local s=""
251                 local u=""
252                 local c=""
253                 local p=""
254
255                 if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
256                         if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
257                                 c="BARE:"
258                         else
259                                 b="GIT_DIR!"
260                         fi
261                 elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
262                         if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
263                                 if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
264                                         git diff --no-ext-diff --quiet --exit-code || w="*"
265                                         if git rev-parse --quiet --verify HEAD >/dev/null; then
266                                                 git diff-index --cached --quiet HEAD -- || i="+"
267                                         else
268                                                 i="#"
269                                         fi
270                                 fi
271                         fi
272                         if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
273                                 git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
274                         fi
275
276                         if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
277                                 if [ -n "$(git ls-files --others --exclude-standard)" ]; then
278                                         u="%"
279                                 fi
280                         fi
281
282                         if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
283                                 __git_ps1_show_upstream
284                         fi
285                 fi
286
287                 local f="$w$i$s$u"
288                 printf -- "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
289         fi
290 }