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