Git 1.8.0.3
[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 # 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 # If you would like to see more information about the identity of
54 # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
55 # to one of these values:
56 #
57 # contains relative to newer annotated tag (v1.6.3.2~35)
58 # branch relative to newer tag or branch (master~4)
59 # describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
60 # default exactly matching tag
61
62 # __gitdir accepts 0 or 1 arguments (i.e., location)
63 # returns location of .git repo
64 __gitdir ()
65 {
66 # Note: this function is duplicated in git-completion.bash
67 # When updating it, make sure you update the other one to match.
68 if [ -z "${1-}" ]; then
69 if [ -n "${__git_dir-}" ]; then
70 echo "$__git_dir"
71 elif [ -n "${GIT_DIR-}" ]; then
72 test -d "${GIT_DIR-}" || return 1
73 echo "$GIT_DIR"
74 elif [ -d .git ]; then
75 echo .git
76 else
77 git rev-parse --git-dir 2>/dev/null
78 fi
79 elif [ -d "$1/.git" ]; then
80 echo "$1/.git"
81 else
82 echo "$1"
83 fi
84 }
85
86 # stores the divergence from upstream in $p
87 # used by GIT_PS1_SHOWUPSTREAM
88 __git_ps1_show_upstream ()
89 {
90 local key value
91 local svn_remote svn_url_pattern count n
92 local upstream=git legacy="" verbose=""
93
94 svn_remote=()
95 # get some config options from git-config
96 local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
97 while read -r key value; do
98 case "$key" in
99 bash.showupstream)
100 GIT_PS1_SHOWUPSTREAM="$value"
101 if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
102 p=""
103 return
104 fi
105 ;;
106 svn-remote.*.url)
107 svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
108 svn_url_pattern+="\\|$value"
109 upstream=svn+git # default upstream is SVN if available, else git
110 ;;
111 esac
112 done <<< "$output"
113
114 # parse configuration values
115 for option in ${GIT_PS1_SHOWUPSTREAM}; do
116 case "$option" in
117 git|svn) upstream="$option" ;;
118 verbose) verbose=1 ;;
119 legacy) legacy=1 ;;
120 esac
121 done
122
123 # Find our upstream
124 case "$upstream" in
125 git) upstream="@{upstream}" ;;
126 svn*)
127 # get the upstream from the "git-svn-id: ..." in a commit message
128 # (git-svn uses essentially the same procedure internally)
129 local svn_upstream=($(git log --first-parent -1 \
130 --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
131 if [[ 0 -ne ${#svn_upstream[@]} ]]; then
132 svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
133 svn_upstream=${svn_upstream%@*}
134 local n_stop="${#svn_remote[@]}"
135 for ((n=1; n <= n_stop; n++)); do
136 svn_upstream=${svn_upstream#${svn_remote[$n]}}
137 done
138
139 if [[ -z "$svn_upstream" ]]; then
140 # default branch name for checkouts with no layout:
141 upstream=${GIT_SVN_ID:-git-svn}
142 else
143 upstream=${svn_upstream#/}
144 fi
145 elif [[ "svn+git" = "$upstream" ]]; then
146 upstream="@{upstream}"
147 fi
148 ;;
149 esac
150
151 # Find how many commits we are ahead/behind our upstream
152 if [[ -z "$legacy" ]]; then
153 count="$(git rev-list --count --left-right \
154 "$upstream"...HEAD 2>/dev/null)"
155 else
156 # produce equivalent output to --count for older versions of git
157 local commits
158 if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
159 then
160 local commit behind=0 ahead=0
161 for commit in $commits
162 do
163 case "$commit" in
164 "<"*) ((behind++)) ;;
165 *) ((ahead++)) ;;
166 esac
167 done
168 count="$behind $ahead"
169 else
170 count=""
171 fi
172 fi
173
174 # calculate the result
175 if [[ -z "$verbose" ]]; then
176 case "$count" in
177 "") # no upstream
178 p="" ;;
179 "0 0") # equal to upstream
180 p="=" ;;
181 "0 "*) # ahead of upstream
182 p=">" ;;
183 *" 0") # behind upstream
184 p="<" ;;
185 *) # diverged from upstream
186 p="<>" ;;
187 esac
188 else
189 case "$count" in
190 "") # no upstream
191 p="" ;;
192 "0 0") # equal to upstream
193 p=" u=" ;;
194 "0 "*) # ahead of upstream
195 p=" u+${count#0 }" ;;
196 *" 0") # behind upstream
197 p=" u-${count% 0}" ;;
198 *) # diverged from upstream
199 p=" u+${count#* }-${count% *}" ;;
200 esac
201 fi
202
203 }
204
205
206 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
207 # returns text to add to bash PS1 prompt (includes branch name)
208 __git_ps1 ()
209 {
210 local g="$(__gitdir)"
211 if [ -n "$g" ]; then
212 local r=""
213 local b=""
214 if [ -f "$g/rebase-merge/interactive" ]; then
215 r="|REBASE-i"
216 b="$(cat "$g/rebase-merge/head-name")"
217 elif [ -d "$g/rebase-merge" ]; then
218 r="|REBASE-m"
219 b="$(cat "$g/rebase-merge/head-name")"
220 else
221 if [ -d "$g/rebase-apply" ]; then
222 if [ -f "$g/rebase-apply/rebasing" ]; then
223 r="|REBASE"
224 elif [ -f "$g/rebase-apply/applying" ]; then
225 r="|AM"
226 else
227 r="|AM/REBASE"
228 fi
229 elif [ -f "$g/MERGE_HEAD" ]; then
230 r="|MERGING"
231 elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
232 r="|CHERRY-PICKING"
233 elif [ -f "$g/BISECT_LOG" ]; then
234 r="|BISECTING"
235 fi
236
237 b="$(git symbolic-ref HEAD 2>/dev/null)" || {
238
239 b="$(
240 case "${GIT_PS1_DESCRIBE_STYLE-}" in
241 (contains)
242 git describe --contains HEAD ;;
243 (branch)
244 git describe --contains --all HEAD ;;
245 (describe)
246 git describe HEAD ;;
247 (* | default)
248 git describe --tags --exact-match HEAD ;;
249 esac 2>/dev/null)" ||
250
251 b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
252 b="unknown"
253 b="($b)"
254 }
255 fi
256
257 local w=""
258 local i=""
259 local s=""
260 local u=""
261 local c=""
262 local p=""
263
264 if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
265 if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
266 c="BARE:"
267 else
268 b="GIT_DIR!"
269 fi
270 elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
271 if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
272 if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
273 git diff --no-ext-diff --quiet --exit-code || w="*"
274 if git rev-parse --quiet --verify HEAD >/dev/null; then
275 git diff-index --cached --quiet HEAD -- || i="+"
276 else
277 i="#"
278 fi
279 fi
280 fi
281 if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
282 git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
283 fi
284
285 if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
286 if [ -n "$(git ls-files --others --exclude-standard)" ]; then
287 u="%"
288 fi
289 fi
290
291 if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
292 __git_ps1_show_upstream
293 fi
294 fi
295
296 local f="$w$i$s$u"
297 printf -- "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
298 fi
299 }