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, for a slighly faster prompt, if you are 19# using bash, __git_ps1 can be used for PROMPT_COMMAND 20# with two parameters, <pre> and <post>, which are strings 21# you would put in $PS1 before and after the status string 22# generated by the git-prompt machinery. e.g. 23# Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "' 24# ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" } 25# will show username, at-sign, host, colon, cwd, then 26# various status string, followed by dollar and SP, as 27# your prompt. 28# Optionally, you can supply a third argument with a printf 29# format string to finetune the output of the branch status 30# 31# The argument to __git_ps1 will be displayed only if you are currently 32# in a git repository. The %s token will be the name of the current 33# branch. 34# 35# In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value, 36# unstaged (*) and staged (+) changes will be shown next to the branch 37# name. You can configure this per-repository with the 38# bash.showDirtyState variable, which defaults to true once 39# GIT_PS1_SHOWDIRTYSTATE is enabled. 40# 41# You can also see if currently something is stashed, by setting 42# GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed, 43# then a '$' will be shown next to the branch name. 44# 45# If you would like to see if there're untracked files, then you can set 46# GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're untracked 47# files, then a '%' will be shown next to the branch name. You can 48# configure this per-repository with the bash.showUntrackedFiles 49# variable, which defaults to true once GIT_PS1_SHOWUNTRACKEDFILES is 50# enabled. 51# 52# If you would like to see the difference between HEAD and its upstream, 53# set GIT_PS1_SHOWUPSTREAM="auto". A "<" indicates you are behind, ">" 54# indicates you are ahead, "<>" indicates you have diverged and "=" 55# indicates that there is no difference. You can further control 56# behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list 57# of values: 58# 59# verbose show number of commits ahead/behind (+/-) upstream 60# legacy don't use the '--count' option available in recent 61# versions of git-rev-list 62# git always compare HEAD to @{upstream} 63# svn always compare HEAD to your SVN upstream 64# 65# By default, __git_ps1 will compare HEAD to your SVN upstream if it can 66# find one, or @{upstream} otherwise. Once you have set 67# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by 68# setting the bash.showUpstream config variable. 69# 70# If you would like to see more information about the identity of 71# commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE 72# to one of these values: 73# 74# contains relative to newer annotated tag (v1.6.3.2~35) 75# branch relative to newer tag or branch (master~4) 76# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f) 77# default exactly matching tag 78# 79# If you would like a colored hint about the current dirty state, set 80# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on 81# the colored output of "git status -sb". 82 83# stores the divergence from upstream in $p 84# used by GIT_PS1_SHOWUPSTREAM 85__git_ps1_show_upstream () 86{ 87local key value 88local svn_remote svn_url_pattern count n 89local upstream=git legacy="" verbose="" 90 91 svn_remote=() 92# get some config options from git-config 93local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n')" 94whileread -r key value;do 95case"$key"in 96 bash.showupstream) 97 GIT_PS1_SHOWUPSTREAM="$value" 98if[[-z"${GIT_PS1_SHOWUPSTREAM}"]];then 99 p="" 100return 101fi 102;; 103 svn-remote.*.url) 104 svn_remote[$((${#svn_remote[@]} + 1))]="$value" 105 svn_url_pattern+="\\|$value" 106 upstream=svn+git # default upstream is SVN if available, else git 107;; 108esac 109done<<<"$output" 110 111# parse configuration values 112for option in${GIT_PS1_SHOWUPSTREAM};do 113case"$option"in 114 git|svn) upstream="$option";; 115 verbose) verbose=1;; 116 legacy) legacy=1;; 117esac 118done 119 120# Find our upstream 121case"$upstream"in 122 git) upstream="@{upstream}";; 123 svn*) 124# get the upstream from the "git-svn-id: ..." in a commit message 125# (git-svn uses essentially the same procedure internally) 126local -a svn_upstream 127 svn_upstream=($(git log --first-parent -1 \ 128--grep="^git-svn-id: \(${svn_url_pattern#??}\)"2>/dev/null)) 129if[[0-ne${#svn_upstream[@]}]];then 130 svn_upstream=${svn_upstream[${#svn_upstream[@]} - 2]} 131 svn_upstream=${svn_upstream%@*} 132local n_stop="${#svn_remote[@]}" 133for((n=1; n <= n_stop; n++));do 134 svn_upstream=${svn_upstream#${svn_remote[$n]}} 135done 136 137if[[-z"$svn_upstream"]];then 138# default branch name for checkouts with no layout: 139 upstream=${GIT_SVN_ID:-git-svn} 140else 141 upstream=${svn_upstream#/} 142fi 143elif[["svn+git"="$upstream"]];then 144 upstream="@{upstream}" 145fi 146;; 147esac 148 149# Find how many commits we are ahead/behind our upstream 150if[[-z"$legacy"]];then 151 count="$(git rev-list --count --left-right \ 152 "$upstream"...HEAD 2>/dev/null)" 153else 154# produce equivalent output to --count for older versions of git 155local commits 156if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)" 157then 158local commit behind=0 ahead=0 159for commit in$commits 160do 161case"$commit"in 162"<"*) ((behind++)) ;; 163*) ((ahead++)) ;; 164esac 165done 166 count="$behind$ahead" 167else 168 count="" 169fi 170fi 171 172# calculate the result 173if[[-z"$verbose"]];then 174case"$count"in 175"")# no upstream 176 p="";; 177"0 0")# equal to upstream 178 p="=";; 179"0 "*)# ahead of upstream 180 p=">";; 181*" 0")# behind upstream 182 p="<";; 183*)# diverged from upstream 184 p="<>";; 185esac 186else 187case"$count"in 188"")# no upstream 189 p="";; 190"0 0")# equal to upstream 191 p=" u=";; 192"0 "*)# ahead of upstream 193 p=" u+${count#0 }";; 194*" 0")# behind upstream 195 p=" u-${count% 0}";; 196*)# diverged from upstream 197 p=" u+${count#* }-${count% *}";; 198esac 199fi 200 201} 202 203# Helper function that is meant to be called from __git_ps1. It 204# builds up a gitstring injecting color codes into the appropriate 205# places. 206__git_ps1_colorize_gitstring () 207{ 208if[[-n${ZSH_VERSION-}]];then 209local c_red='%F{red}' 210local c_green='%F{green}' 211local c_lblue='%F{blue}' 212local c_clear='%f' 213local bad_color=$c_red 214local ok_color=$c_green 215local branch_color="$c_clear" 216local flags_color="$c_lblue" 217local branchstring="$c${b##refs/heads/}" 218 219if[$detached= no ];then 220 branch_color="$ok_color" 221else 222 branch_color="$bad_color" 223fi 224 225 gitstring="$branch_color$branchstring$c_clear" 226 227if[-n"$w$i$s$u$r$p"];then 228 gitstring="$gitstring$z" 229fi 230if["$w"="*"];then 231 gitstring="$gitstring$bad_color$w" 232fi 233if[-n"$i"];then 234 gitstring="$gitstring$ok_color$i" 235fi 236if[-n"$s"];then 237 gitstring="$gitstring$flags_color$s" 238fi 239if[-n"$u"];then 240 gitstring="$gitstring$bad_color$u" 241fi 242 gitstring="$gitstring$c_clear$r$p" 243return 244fi 245local c_red='\e[31m' 246local c_green='\e[32m' 247local c_lblue='\e[1;34m' 248local c_clear='\e[0m' 249local bad_color=$c_red 250local ok_color=$c_green 251local branch_color="$c_clear" 252local flags_color="$c_lblue" 253local branchstring="$c${b##refs/heads/}" 254 255if[$detached= no ];then 256 branch_color="$ok_color" 257else 258 branch_color="$bad_color" 259fi 260 261# Setting gitstring directly with \[ and \] around colors 262# is necessary to prevent wrapping issues! 263 gitstring="\[$branch_color\]$branchstring\[$c_clear\]" 264 265if[-n"$w$i$s$u$r$p"];then 266 gitstring="$gitstring$z" 267fi 268if["$w"="*"];then 269 gitstring="$gitstring\[$bad_color\]$w" 270fi 271if[-n"$i"];then 272 gitstring="$gitstring\[$ok_color\]$i" 273fi 274if[-n"$s"];then 275 gitstring="$gitstring\[$flags_color\]$s" 276fi 277if[-n"$u"];then 278 gitstring="$gitstring\[$bad_color\]$u" 279fi 280 gitstring="$gitstring\[$c_clear\]$r$p" 281} 282 283# __git_ps1 accepts 0 or 1 arguments (i.e., format string) 284# when called from PS1 using command substitution 285# in this mode it prints text to add to bash PS1 prompt (includes branch name) 286# 287# __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc) 288# in that case it _sets_ PS1. The arguments are parts of a PS1 string. 289# when two arguments are given, the first is prepended and the second appended 290# to the state string when assigned to PS1. 291# The optional third parameter will be used as printf format string to further 292# customize the output of the git-status string. 293# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true 294__git_ps1 () 295{ 296local pcmode=no 297local detached=no 298local ps1pc_start='\u@\h:\w ' 299local ps1pc_end='\$ ' 300local printf_format=' (%s)' 301 302case"$#"in 3032|3) pcmode=yes 304 ps1pc_start="$1" 305 ps1pc_end="$2" 306 printf_format="${3:-$printf_format}" 307;; 3080|1) printf_format="${1:-$printf_format}" 309;; 310*)return 311;; 312esac 313 314local repo_info rev_parse_exit_code 315 repo_info="$(git rev-parse --git-dir --is-inside-git-dir \ 316 --is-bare-repository --is-inside-work-tree \ 317 --short HEAD 2>/dev/null)" 318 rev_parse_exit_code="$?" 319 320if[-z"$repo_info"];then 321if[$pcmode=yes];then 322#In PC mode PS1 always needs to be set 323 PS1="$ps1pc_start$ps1pc_end" 324fi 325return 326fi 327 328local short_sha 329if["$rev_parse_exit_code"="0"];then 330 short_sha="${repo_info##*$'\n'}" 331 repo_info="${repo_info%$'\n'*}" 332fi 333local inside_worktree="${repo_info##*$'\n'}" 334 repo_info="${repo_info%$'\n'*}" 335local bare_repo="${repo_info##*$'\n'}" 336 repo_info="${repo_info%$'\n'*}" 337local inside_gitdir="${repo_info##*$'\n'}" 338local g="${repo_info%$'\n'*}" 339 340local r="" 341local b="" 342local step="" 343local total="" 344if[-d"$g/rebase-merge"];then 345read b 2>/dev/null <"$g/rebase-merge/head-name" 346read step 2>/dev/null <"$g/rebase-merge/msgnum" 347read total 2>/dev/null <"$g/rebase-merge/end" 348if[-f"$g/rebase-merge/interactive"];then 349 r="|REBASE-i" 350else 351 r="|REBASE-m" 352fi 353else 354if[-d"$g/rebase-apply"];then 355read step 2>/dev/null <"$g/rebase-apply/next" 356read total 2>/dev/null <"$g/rebase-apply/last" 357if[-f"$g/rebase-apply/rebasing"];then 358read b 2>/dev/null <"$g/rebase-apply/head-name" 359 r="|REBASE" 360elif[-f"$g/rebase-apply/applying"];then 361 r="|AM" 362else 363 r="|AM/REBASE" 364fi 365elif[-f"$g/MERGE_HEAD"];then 366 r="|MERGING" 367elif[-f"$g/CHERRY_PICK_HEAD"];then 368 r="|CHERRY-PICKING" 369elif[-f"$g/REVERT_HEAD"];then 370 r="|REVERTING" 371elif[-f"$g/BISECT_LOG"];then 372 r="|BISECTING" 373fi 374 375if[-n"$b"];then 376: 377elif[-h"$g/HEAD"];then 378# symlink symbolic ref 379 b="$(git symbolic-ref HEAD 2>/dev/null)" 380else 381localhead="" 382if!readhead2>/dev/null <"$g/HEAD";then 383if[$pcmode=yes];then 384 PS1="$ps1pc_start$ps1pc_end" 385fi 386return 387fi 388# is it a symbolic ref? 389 b="${head#ref: }" 390if["$head"="$b"];then 391 detached=yes 392 b="$( 393 case "${GIT_PS1_DESCRIBE_STYLE-}" in 394 (contains) 395 git describe --contains HEAD ;; 396 (branch) 397 git describe --contains --all HEAD ;; 398 (describe) 399 git describe HEAD ;; 400 (* | default) 401 git describe --tags --exact-match HEAD ;; 402 esac 2>/dev/null)"|| 403 404 b="$short_sha..." 405 b="($b)" 406fi 407fi 408fi 409 410if[-n"$step"] && [-n"$total"];then 411 r="$r$step/$total" 412fi 413 414local w="" 415local i="" 416local s="" 417local u="" 418local c="" 419local p="" 420 421if["true"="$inside_gitdir"];then 422if["true"="$bare_repo"];then 423 c="BARE:" 424else 425 b="GIT_DIR!" 426fi 427elif["true"="$inside_worktree"];then 428if[-n"${GIT_PS1_SHOWDIRTYSTATE-}"] && 429["$(git config --bool bash.showDirtyState)"!="false"] 430then 431 git diff--no-ext-diff --quiet --exit-code|| w="*" 432if[-n"$short_sha"];then 433 git diff-index --cached --quiet HEAD --|| i="+" 434else 435 i="#" 436fi 437fi 438if[-n"${GIT_PS1_SHOWSTASHSTATE-}"] && 439[-r"$g/refs/stash"];then 440 s="$" 441fi 442 443if[-n"${GIT_PS1_SHOWUNTRACKEDFILES-}"] && 444["$(git config --bool bash.showUntrackedFiles)"!="false"] && 445 git ls-files --others --exclude-standard --error-unmatch --'*'>/dev/null 2>/dev/null 446then 447 u="%${ZSH_VERSION+%}" 448fi 449 450if[-n"${GIT_PS1_SHOWUPSTREAM-}"];then 451 __git_ps1_show_upstream 452fi 453fi 454 455local z="${GIT_PS1_STATESEPARATOR-" "}" 456local f="$w$i$s$u" 457if[$pcmode=yes];then 458local gitstring= 459if[-n"${GIT_PS1_SHOWCOLORHINTS-}"];then 460 __git_ps1_colorize_gitstring 461else 462 gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p" 463fi 464if[[-n${ZSH_VERSION-}]];then 465 gitstring=$(printf -- "$printf_format" "$gitstring") 466else 467printf -v gitstring --"$printf_format""$gitstring" 468fi 469 PS1="$ps1pc_start$gitstring$ps1pc_end" 470else 471# NO color option unless in PROMPT_COMMAND mode 472printf --"$printf_format""$c${b##refs/heads/}${f:+$z$f}$r$p" 473fi 474}