git-prompt.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 repository status 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: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
  17. # the optional argument will be used as format string.
  18. # 3b) Alternatively, for a slightly faster prompt, __git_ps1 can
  19. # be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
  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. # will show username, at-sign, host, colon, cwd, then
  25. # various status string, followed by dollar and SP, as
  26. # your prompt.
  27. # ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
  28. # will show username, pipe, then various status string,
  29. # followed by colon, cwd, dollar and SP, as your prompt.
  30. # Optionally, you can supply a third argument with a printf
  31. # format string to finetune the output of the branch status
  32. #
  33. # The repository status will be displayed only if you are currently in a
  34. # git repository. The %s token is the placeholder for the shown status.
  35. #
  36. # The prompt status always includes the current branch name.
  37. #
  38. # In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value,
  39. # unstaged (*) and staged (+) changes will be shown next to the branch
  40. # name. You can configure this per-repository with the
  41. # bash.showDirtyState variable, which defaults to true once
  42. # GIT_PS1_SHOWDIRTYSTATE is enabled.
  43. #
  44. # You can also see if currently something is stashed, by setting
  45. # GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
  46. # then a '$' will be shown next to the branch name.
  47. #
  48. # If you would like to see if there're untracked files, then you can set
  49. # GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're untracked
  50. # files, then a '%' will be shown next to the branch name. You can
  51. # configure this per-repository with the bash.showUntrackedFiles
  52. # variable, which defaults to true once GIT_PS1_SHOWUNTRACKEDFILES is
  53. # enabled.
  54. #
  55. # If you would like to see the difference between HEAD and its upstream,
  56. # set GIT_PS1_SHOWUPSTREAM="auto". A "<" indicates you are behind, ">"
  57. # indicates you are ahead, "<>" indicates you have diverged and "="
  58. # indicates that there is no difference. You can further control
  59. # behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
  60. # of values:
  61. #
  62. # verbose show number of commits ahead/behind (+/-) upstream
  63. # name if verbose, then also show the upstream abbrev name
  64. # legacy don't use the '--count' option available in recent
  65. # versions of git-rev-list
  66. # git always compare HEAD to @{upstream}
  67. # svn always compare HEAD to your SVN upstream
  68. #
  69. # By default, __git_ps1 will compare HEAD to your SVN upstream if it can
  70. # find one, or @{upstream} otherwise. Once you have set
  71. # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
  72. # setting the bash.showUpstream config variable.
  73. #
  74. # You can change the separator between the branch name and the above
  75. # state symbols by setting GIT_PS1_STATESEPARATOR. The default separator
  76. # is SP.
  77. #
  78. # When there is an in-progress operation such as a merge, rebase,
  79. # revert, cherry-pick, or bisect, the prompt will include information
  80. # related to the operation, often in the form "|<OPERATION-NAME>".
  81. #
  82. # When the repository has a sparse-checkout, a notification of the form
  83. # "|SPARSE" will be included in the prompt. This can be shortened to a
  84. # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
  85. # by setting GIT_PS1_OMITSPARSESTATE.
  86. #
  87. # If you would like to see a notification on the prompt when there are
  88. # unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
  89. # prompt will include "|CONFLICT".
  90. #
  91. # If you would like to see more information about the identity of
  92. # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
  93. # to one of these values:
  94. #
  95. # contains relative to newer annotated tag (v1.6.3.2~35)
  96. # branch relative to newer tag or branch (master~4)
  97. # describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
  98. # tag relative to any older tag (v1.6.3.1-13-gdd42c2f)
  99. # default exactly matching tag
  100. #
  101. # If you would like a colored hint about the current dirty state, set
  102. # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
  103. # the colored output of "git status -sb".
  104. #
  105. # If you would like __git_ps1 to do nothing in the case when the current
  106. # directory is set up to be ignored by git, then set
  107. # GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
  108. # repository level by setting bash.hideIfPwdIgnored to "false".
  109. # check whether printf supports -v
  110. __git_printf_supports_v=
  111. printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
  112. # stores the divergence from upstream in $p
  113. # used by GIT_PS1_SHOWUPSTREAM
  114. __git_ps1_show_upstream ()
  115. {
  116. local key value
  117. local svn_remote svn_url_pattern count n
  118. local upstream_type=git legacy="" verbose="" name=""
  119. svn_remote=()
  120. # get some config options from git-config
  121. local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
  122. while read -r key value; do
  123. case "$key" in
  124. bash.showupstream)
  125. GIT_PS1_SHOWUPSTREAM="$value"
  126. if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
  127. p=""
  128. return
  129. fi
  130. ;;
  131. svn-remote.*.url)
  132. svn_remote[$((${#svn_remote[@]} + 1))]="$value"
  133. svn_url_pattern="$svn_url_pattern\\|$value"
  134. upstream_type=svn+git # default upstream type is SVN if available, else git
  135. ;;
  136. esac
  137. done <<< "$output"
  138. # parse configuration values
  139. local option
  140. for option in ${GIT_PS1_SHOWUPSTREAM}; do
  141. case "$option" in
  142. git|svn) upstream_type="$option" ;;
  143. verbose) verbose=1 ;;
  144. legacy) legacy=1 ;;
  145. name) name=1 ;;
  146. esac
  147. done
  148. # Find our upstream type
  149. case "$upstream_type" in
  150. git) upstream_type="@{upstream}" ;;
  151. svn*)
  152. # get the upstream from the "git-svn-id: ..." in a commit message
  153. # (git-svn uses essentially the same procedure internally)
  154. local -a svn_upstream
  155. svn_upstream=($(git log --first-parent -1 \
  156. --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
  157. if [[ 0 -ne ${#svn_upstream[@]} ]]; then
  158. svn_upstream=${svn_upstream[${#svn_upstream[@]} - 2]}
  159. svn_upstream=${svn_upstream%@*}
  160. local n_stop="${#svn_remote[@]}"
  161. for ((n=1; n <= n_stop; n++)); do
  162. svn_upstream=${svn_upstream#${svn_remote[$n]}}
  163. done
  164. if [[ -z "$svn_upstream" ]]; then
  165. # default branch name for checkouts with no layout:
  166. upstream_type=${GIT_SVN_ID:-git-svn}
  167. else
  168. upstream_type=${svn_upstream#/}
  169. fi
  170. elif [[ "svn+git" = "$upstream_type" ]]; then
  171. upstream_type="@{upstream}"
  172. fi
  173. ;;
  174. esac
  175. # Find how many commits we are ahead/behind our upstream
  176. if [[ -z "$legacy" ]]; then
  177. count="$(git rev-list --count --left-right \
  178. "$upstream_type"...HEAD 2>/dev/null)"
  179. else
  180. # produce equivalent output to --count for older versions of git
  181. local commits
  182. if commits="$(git rev-list --left-right "$upstream_type"...HEAD 2>/dev/null)"
  183. then
  184. local commit behind=0 ahead=0
  185. for commit in $commits
  186. do
  187. case "$commit" in
  188. "<"*) ((behind++)) ;;
  189. *) ((ahead++)) ;;
  190. esac
  191. done
  192. count="$behind $ahead"
  193. else
  194. count=""
  195. fi
  196. fi
  197. # calculate the result
  198. if [[ -z "$verbose" ]]; then
  199. case "$count" in
  200. "") # no upstream
  201. p="" ;;
  202. "0 0") # equal to upstream
  203. p="=" ;;
  204. "0 "*) # ahead of upstream
  205. p=">" ;;
  206. *" 0") # behind upstream
  207. p="<" ;;
  208. *) # diverged from upstream
  209. p="<>" ;;
  210. esac
  211. else # verbose, set upstream instead of p
  212. case "$count" in
  213. "") # no upstream
  214. upstream="" ;;
  215. "0 0") # equal to upstream
  216. upstream="|u=" ;;
  217. "0 "*) # ahead of upstream
  218. upstream="|u+${count#0 }" ;;
  219. *" 0") # behind upstream
  220. upstream="|u-${count% 0}" ;;
  221. *) # diverged from upstream
  222. upstream="|u+${count#* }-${count% *}" ;;
  223. esac
  224. if [[ -n "$count" && -n "$name" ]]; then
  225. __git_ps1_upstream_name=$(git rev-parse \
  226. --abbrev-ref "$upstream_type" 2>/dev/null)
  227. if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
  228. upstream="$upstream \${__git_ps1_upstream_name}"
  229. else
  230. upstream="$upstream ${__git_ps1_upstream_name}"
  231. # not needed anymore; keep user's
  232. # environment clean
  233. unset __git_ps1_upstream_name
  234. fi
  235. fi
  236. fi
  237. }
  238. # Helper function that is meant to be called from __git_ps1. It
  239. # injects color codes into the appropriate gitstring variables used
  240. # to build a gitstring. Colored variables are responsible for clearing
  241. # their own color.
  242. __git_ps1_colorize_gitstring ()
  243. {
  244. if [[ -n ${ZSH_VERSION-} ]]; then
  245. local c_red='%F{red}'
  246. local c_green='%F{green}'
  247. local c_lblue='%F{blue}'
  248. local c_clear='%f'
  249. else
  250. # Using \001 and \002 around colors is necessary to prevent
  251. # issues with command line editing/browsing/completion!
  252. local c_red=$'\001\e[31m\002'
  253. local c_green=$'\001\e[32m\002'
  254. local c_lblue=$'\001\e[1;34m\002'
  255. local c_clear=$'\001\e[0m\002'
  256. fi
  257. local bad_color=$c_red
  258. local ok_color=$c_green
  259. local flags_color="$c_lblue"
  260. local branch_color=""
  261. if [ $detached = no ]; then
  262. branch_color="$ok_color"
  263. else
  264. branch_color="$bad_color"
  265. fi
  266. if [ -n "$c" ]; then
  267. c="$branch_color$c$c_clear"
  268. fi
  269. b="$branch_color$b$c_clear"
  270. if [ -n "$w" ]; then
  271. w="$bad_color$w$c_clear"
  272. fi
  273. if [ -n "$i" ]; then
  274. i="$ok_color$i$c_clear"
  275. fi
  276. if [ -n "$s" ]; then
  277. s="$flags_color$s$c_clear"
  278. fi
  279. if [ -n "$u" ]; then
  280. u="$bad_color$u$c_clear"
  281. fi
  282. }
  283. # Helper function to read the first line of a file into a variable.
  284. # __git_eread requires 2 arguments, the file path and the name of the
  285. # variable, in that order.
  286. __git_eread ()
  287. {
  288. test -r "$1" && IFS=$'\r\n' read "$2" <"$1"
  289. }
  290. # see if a cherry-pick or revert is in progress, if the user has committed a
  291. # conflict resolution with 'git commit' in the middle of a sequence of picks or
  292. # reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read
  293. # the todo file.
  294. __git_sequencer_status ()
  295. {
  296. local todo
  297. if test -f "$g/CHERRY_PICK_HEAD"
  298. then
  299. r="|CHERRY-PICKING"
  300. return 0;
  301. elif test -f "$g/REVERT_HEAD"
  302. then
  303. r="|REVERTING"
  304. return 0;
  305. elif __git_eread "$g/sequencer/todo" todo
  306. then
  307. case "$todo" in
  308. p[\ \ ]|pick[\ \ ]*)
  309. r="|CHERRY-PICKING"
  310. return 0
  311. ;;
  312. revert[\ \ ]*)
  313. r="|REVERTING"
  314. return 0
  315. ;;
  316. esac
  317. fi
  318. return 1
  319. }
  320. # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
  321. # when called from PS1 using command substitution
  322. # in this mode it prints text to add to bash PS1 prompt (includes branch name)
  323. #
  324. # __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
  325. # in that case it _sets_ PS1. The arguments are parts of a PS1 string.
  326. # when two arguments are given, the first is prepended and the second appended
  327. # to the state string when assigned to PS1.
  328. # The optional third parameter will be used as printf format string to further
  329. # customize the output of the git-status string.
  330. # In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
  331. __git_ps1 ()
  332. {
  333. # preserve exit status
  334. local exit=$?
  335. local pcmode=no
  336. local detached=no
  337. local ps1pc_start='\u@\h:\w '
  338. local ps1pc_end='\$ '
  339. local printf_format=' (%s)'
  340. case "$#" in
  341. 2|3) pcmode=yes
  342. ps1pc_start="$1"
  343. ps1pc_end="$2"
  344. printf_format="${3:-$printf_format}"
  345. # set PS1 to a plain prompt so that we can
  346. # simply return early if the prompt should not
  347. # be decorated
  348. PS1="$ps1pc_start$ps1pc_end"
  349. ;;
  350. 0|1) printf_format="${1:-$printf_format}"
  351. ;;
  352. *) return $exit
  353. ;;
  354. esac
  355. # ps1_expanded: This variable is set to 'yes' if the shell
  356. # subjects the value of PS1 to parameter expansion:
  357. #
  358. # * bash does unless the promptvars option is disabled
  359. # * zsh does not unless the PROMPT_SUBST option is set
  360. # * POSIX shells always do
  361. #
  362. # If the shell would expand the contents of PS1 when drawing
  363. # the prompt, a raw ref name must not be included in PS1.
  364. # This protects the user from arbitrary code execution via
  365. # specially crafted ref names. For example, a ref named
  366. # 'refs/heads/$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)' might cause the
  367. # shell to execute 'sudo rm -rf /' when the prompt is drawn.
  368. #
  369. # Instead, the ref name should be placed in a separate global
  370. # variable (in the __git_ps1_* namespace to avoid colliding
  371. # with the user's environment) and that variable should be
  372. # referenced from PS1. For example:
  373. #
  374. # __git_ps1_foo=$(do_something_to_get_ref_name)
  375. # PS1="...stuff...\${__git_ps1_foo}...stuff..."
  376. #
  377. # If the shell does not expand the contents of PS1, the raw
  378. # ref name must be included in PS1.
  379. #
  380. # The value of this variable is only relevant when in pcmode.
  381. #
  382. # Assume that the shell follows the POSIX specification and
  383. # expands PS1 unless determined otherwise. (This is more
  384. # likely to be correct if the user has a non-bash, non-zsh
  385. # shell and safer than the alternative if the assumption is
  386. # incorrect.)
  387. #
  388. local ps1_expanded=yes
  389. [ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
  390. [ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
  391. local repo_info rev_parse_exit_code
  392. repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
  393. --is-bare-repository --is-inside-work-tree \
  394. --short HEAD 2>/dev/null)"
  395. rev_parse_exit_code="$?"
  396. if [ -z "$repo_info" ]; then
  397. return $exit
  398. fi
  399. local short_sha=""
  400. if [ "$rev_parse_exit_code" = "0" ]; then
  401. short_sha="${repo_info##*$'\n'}"
  402. repo_info="${repo_info%$'\n'*}"
  403. fi
  404. local inside_worktree="${repo_info##*$'\n'}"
  405. repo_info="${repo_info%$'\n'*}"
  406. local bare_repo="${repo_info##*$'\n'}"
  407. repo_info="${repo_info%$'\n'*}"
  408. local inside_gitdir="${repo_info##*$'\n'}"
  409. local g="${repo_info%$'\n'*}"
  410. if [ "true" = "$inside_worktree" ] &&
  411. [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
  412. [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
  413. git check-ignore -q .
  414. then
  415. return $exit
  416. fi
  417. local sparse=""
  418. if [ -z "${GIT_PS1_COMPRESSSPARSESTATE-}" ] &&
  419. [ -z "${GIT_PS1_OMITSPARSESTATE-}" ] &&
  420. [ "$(git config --bool core.sparseCheckout)" = "true" ]; then
  421. sparse="|SPARSE"
  422. fi
  423. local r=""
  424. local b=""
  425. local step=""
  426. local total=""
  427. if [ -d "$g/rebase-merge" ]; then
  428. __git_eread "$g/rebase-merge/head-name" b
  429. __git_eread "$g/rebase-merge/msgnum" step
  430. __git_eread "$g/rebase-merge/end" total
  431. r="|REBASE"
  432. else
  433. if [ -d "$g/rebase-apply" ]; then
  434. __git_eread "$g/rebase-apply/next" step
  435. __git_eread "$g/rebase-apply/last" total
  436. if [ -f "$g/rebase-apply/rebasing" ]; then
  437. __git_eread "$g/rebase-apply/head-name" b
  438. r="|REBASE"
  439. elif [ -f "$g/rebase-apply/applying" ]; then
  440. r="|AM"
  441. else
  442. r="|AM/REBASE"
  443. fi
  444. elif [ -f "$g/MERGE_HEAD" ]; then
  445. r="|MERGING"
  446. elif __git_sequencer_status; then
  447. :
  448. elif [ -f "$g/BISECT_LOG" ]; then
  449. r="|BISECTING"
  450. fi
  451. if [ -n "$b" ]; then
  452. :
  453. elif [ -h "$g/HEAD" ]; then
  454. # symlink symbolic ref
  455. b="$(git symbolic-ref HEAD 2>/dev/null)"
  456. else
  457. local head=""
  458. if ! __git_eread "$g/HEAD" head; then
  459. return $exit
  460. fi
  461. # is it a symbolic ref?
  462. b="${head#ref: }"
  463. if [ "$head" = "$b" ]; then
  464. detached=yes
  465. b="$(
  466. case "${GIT_PS1_DESCRIBE_STYLE-}" in
  467. (contains)
  468. git describe --contains HEAD ;;
  469. (branch)
  470. git describe --contains --all HEAD ;;
  471. (tag)
  472. git describe --tags HEAD ;;
  473. (describe)
  474. git describe HEAD ;;
  475. (* | default)
  476. git describe --tags --exact-match HEAD ;;
  477. esac 2>/dev/null)" ||
  478. b="$short_sha..."
  479. b="($b)"
  480. fi
  481. fi
  482. fi
  483. if [ -n "$step" ] && [ -n "$total" ]; then
  484. r="$r $step/$total"
  485. fi
  486. local conflict="" # state indicator for unresolved conflicts
  487. if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
  488. [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
  489. conflict="|CONFLICT"
  490. fi
  491. local w=""
  492. local i=""
  493. local s=""
  494. local u=""
  495. local h=""
  496. local c=""
  497. local p="" # short version of upstream state indicator
  498. local upstream="" # verbose version of upstream state indicator
  499. if [ "true" = "$inside_gitdir" ]; then
  500. if [ "true" = "$bare_repo" ]; then
  501. c="BARE:"
  502. else
  503. b="GIT_DIR!"
  504. fi
  505. elif [ "true" = "$inside_worktree" ]; then
  506. if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
  507. [ "$(git config --bool bash.showDirtyState)" != "false" ]
  508. then
  509. git diff --no-ext-diff --quiet || w="*"
  510. git diff --no-ext-diff --cached --quiet || i="+"
  511. if [ -z "$short_sha" ] && [ -z "$i" ]; then
  512. i="#"
  513. fi
  514. fi
  515. if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
  516. git rev-parse --verify --quiet refs/stash >/dev/null
  517. then
  518. s="$"
  519. fi
  520. if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
  521. [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
  522. git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' >/dev/null 2>/dev/null
  523. then
  524. u="%${ZSH_VERSION+%}"
  525. fi
  526. if [ -n "${GIT_PS1_COMPRESSSPARSESTATE-}" ] &&
  527. [ "$(git config --bool core.sparseCheckout)" = "true" ]; then
  528. h="?"
  529. fi
  530. if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
  531. __git_ps1_show_upstream
  532. fi
  533. fi
  534. local z="${GIT_PS1_STATESEPARATOR-" "}"
  535. b=${b##refs/heads/}
  536. if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
  537. __git_ps1_branch_name=$b
  538. b="\${__git_ps1_branch_name}"
  539. fi
  540. if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
  541. __git_ps1_colorize_gitstring
  542. fi
  543. local f="$h$w$i$s$u$p"
  544. local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}${conflict}"
  545. if [ $pcmode = yes ]; then
  546. if [ "${__git_printf_supports_v-}" != yes ]; then
  547. gitstring=$(printf -- "$printf_format" "$gitstring")
  548. else
  549. printf -v gitstring -- "$printf_format" "$gitstring"
  550. fi
  551. PS1="$ps1pc_start$gitstring$ps1pc_end"
  552. else
  553. printf -- "$printf_format" "$gitstring"
  554. fi
  555. return $exit
  556. }