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