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