Merge branch 'rr/feed-real-path-to-editor'
[gitweb.git] / contrib / completion / git-prompt.sh
index daed6a1d039cc8b2f5adb1d089e36ea7011e0536..d6c61b2bdecc5ca89e21ece5df7c2e4bbec9373c 100644 (file)
@@ -3,7 +3,7 @@
 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
 # Distributed under the GNU General Public License, version 2.0.
 #
-# This script allows you to see the current branch in your prompt.
+# This script allows you to see repository status in your prompt.
 #
 # To enable:
 #
 #    3a) Change your PS1 to call __git_ps1 as
 #        command-substitution:
 #        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 #        the optional argument will be used as format string.
-#    3b) Alternatively, for a slighly faster prompt, if you are
-#        using bash, __git_ps1 can be used for PROMPT_COMMAND
+#    3b) Alternatively, for a slightly faster prompt, __git_ps1 can
+#        be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
 #        with two parameters, <pre> and <post>, which are strings
 #        you would put in $PS1 before and after the status string
 #        generated by the git-prompt machinery.  e.g.
 #        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+#          will show username, at-sign, host, colon, cwd, then
+#          various status string, followed by dollar and SP, as
+#          your prompt.
 #        ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
-#        will show username, at-sign, host, colon, cwd, then
-#        various status string, followed by dollar and SP, as
-#        your prompt.
+#          will show username, pipe, then various status string,
+#          followed by colon, cwd, dollar and SP, as your prompt.
 #        Optionally, you can supply a third argument with a printf
 #        format string to finetune the output of the branch status
 #
-# The argument to __git_ps1 will be displayed only if you are currently
-# in a git repository.  The %s token will be the name of the current
-# branch.
+# The repository status will be displayed only if you are currently in a
+# git repository. The %s token is the placeholder for the shown status.
+#
+# The prompt status always includes the current branch name.
 #
 # In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value,
 # unstaged (*) and staged (+) changes will be shown next to the branch
 #
 # If you would like a colored hint about the current dirty state, set
 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
-# the colored output of "git status -sb".
+# the colored output of "git status -sb" and are available only when
+# using __git_ps1 for PROMPT_COMMAND or precmd.
+
+# check whether printf supports -v
+__git_printf_supports_v=
+printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
 
 # stores the divergence from upstream in $p
 # used by GIT_PS1_SHOWUPSTREAM
@@ -201,8 +209,8 @@ __git_ps1_show_upstream ()
 }
 
 # Helper function that is meant to be called from __git_ps1.  It
-# builds up a gitstring injecting color codes into the appropriate
-# places.
+# injects color codes into the appropriate gitstring variables used
+# to build a gitstring.
 __git_ps1_colorize_gitstring ()
 {
        if [[ -n ${ZSH_VERSION-} ]]; then
@@ -210,74 +218,40 @@ __git_ps1_colorize_gitstring ()
                local c_green='%F{green}'
                local c_lblue='%F{blue}'
                local c_clear='%f'
-               local bad_color=$c_red
-               local ok_color=$c_green
-               local branch_color="$c_clear"
-               local flags_color="$c_lblue"
-               local branchstring="$c${b##refs/heads/}"
-
-               if [ $detached = no ]; then
-                       branch_color="$ok_color"
-               else
-                       branch_color="$bad_color"
-               fi
-
-               gitstring="$branch_color$branchstring$c_clear"
-
-               if [ -n "$w$i$s$u$r$p" ]; then
-                       gitstring="$gitstring$z"
-               fi
-               if [ "$w" = "*" ]; then
-                       gitstring="$gitstring$bad_color$w"
-               fi
-               if [ -n "$i" ]; then
-                       gitstring="$gitstring$ok_color$i"
-               fi
-               if [ -n "$s" ]; then
-                       gitstring="$gitstring$flags_color$s"
-               fi
-               if [ -n "$u" ]; then
-                       gitstring="$gitstring$bad_color$u"
-               fi
-               gitstring="$gitstring$c_clear$r$p"
-               return
+       else
+               # Using \[ and \] around colors is necessary to prevent
+               # issues with command line editing/browsing/completion!
+               local c_red='\[\e[31m\]'
+               local c_green='\[\e[32m\]'
+               local c_lblue='\[\e[1;34m\]'
+               local c_clear='\[\e[0m\]'
        fi
-       local c_red='\e[31m'
-       local c_green='\e[32m'
-       local c_lblue='\e[1;34m'
-       local c_clear='\e[0m'
        local bad_color=$c_red
        local ok_color=$c_green
-       local branch_color="$c_clear"
        local flags_color="$c_lblue"
-       local branchstring="$c${b##refs/heads/}"
 
+       local branch_color=""
        if [ $detached = no ]; then
                branch_color="$ok_color"
        else
                branch_color="$bad_color"
        fi
+       c="$branch_color$c"
 
-       # Setting gitstring directly with \[ and \] around colors
-       # is necessary to prevent wrapping issues!
-       gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
-
-       if [ -n "$w$i$s$u$r$p" ]; then
-               gitstring="$gitstring$z"
-       fi
+       z="$c_clear$z"
        if [ "$w" = "*" ]; then
-               gitstring="$gitstring\[$bad_color\]$w"
+               w="$bad_color$w"
        fi
        if [ -n "$i" ]; then
-               gitstring="$gitstring\[$ok_color\]$i"
+               i="$ok_color$i"
        fi
        if [ -n "$s" ]; then
-               gitstring="$gitstring\[$flags_color\]$s"
+               s="$flags_color$s"
        fi
        if [ -n "$u" ]; then
-               gitstring="$gitstring\[$bad_color\]$u"
+               u="$bad_color$u"
        fi
-       gitstring="$gitstring\[$c_clear\]$r$p"
+       r="$c_clear$r"
 }
 
 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -453,22 +427,23 @@ __git_ps1 ()
        fi
 
        local z="${GIT_PS1_STATESEPARATOR-" "}"
+
+       # NO color option unless in PROMPT_COMMAND mode
+       if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+               __git_ps1_colorize_gitstring
+       fi
+
        local f="$w$i$s$u"
+       local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
+
        if [ $pcmode = yes ]; then
-               local gitstring=
-               if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
-                       __git_ps1_colorize_gitstring
-               else
-                       gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
-               fi
-               if [[ -n ${ZSH_VERSION-} ]]; then
+               if [ "${__git_printf_supports_v-}" != yes ]; then
                        gitstring=$(printf -- "$printf_format" "$gitstring")
                else
                        printf -v gitstring -- "$printf_format" "$gitstring"
                fi
                PS1="$ps1pc_start$gitstring$ps1pc_end"
        else
-               # NO color option unless in PROMPT_COMMAND mode
-               printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
+               printf -- "$printf_format" "$gitstring"
        fi
 }