bash prompt: use bash builtins to find out current branch
[gitweb.git] / contrib / completion / git-prompt.sh
index eaf5c369aacde207cbad4c9df9574e7b26634bb7..c2050b69bb9bf1830868b6b0c9ffdf53a52e1ef0 100644 (file)
@@ -20,7 +20,8 @@
 #        <post>, which are strings you would put in $PS1 before
 #        and after the status string generated by the git-prompt
 #        machinery.  e.g.
-#           PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+#        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+#        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.
 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
 # the colored output of "git status -sb".
 
-# __gitdir accepts 0 or 1 arguments (i.e., location)
-# returns location of .git repo
-__gitdir ()
-{
-       # Note: this function is duplicated in git-completion.bash
-       # When updating it, make sure you update the other one to match.
-       if [ -z "${1-}" ]; then
-               if [ -n "${__git_dir-}" ]; then
-                       echo "$__git_dir"
-               elif [ -n "${GIT_DIR-}" ]; then
-                       test -d "${GIT_DIR-}" || return 1
-                       echo "$GIT_DIR"
-               elif [ -d .git ]; then
-                       echo .git
-               else
-                       git rev-parse --git-dir 2>/dev/null
-               fi
-       elif [ -d "$1/.git" ]; then
-               echo "$1/.git"
-       else
-               echo "$1"
-       fi
-}
-
 # stores the divergence from upstream in $p
 # used by GIT_PS1_SHOWUPSTREAM
 __git_ps1_show_upstream ()
@@ -124,7 +101,7 @@ __git_ps1_show_upstream ()
                        fi
                        ;;
                svn-remote.*.url)
-                       svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
+                       svn_remote[$((${#svn_remote[@]} + 1))]="$value"
                        svn_url_pattern+="\\|$value"
                        upstream=svn+git # default upstream is SVN if available, else git
                        ;;
@@ -146,10 +123,11 @@ __git_ps1_show_upstream ()
        svn*)
                # get the upstream from the "git-svn-id: ..." in a commit message
                # (git-svn uses essentially the same procedure internally)
-               local svn_upstream=($(git log --first-parent -1 \
+               local -a svn_upstream
+               svn_upstream=($(git log --first-parent -1 \
                                        --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
                if [[ 0 -ne ${#svn_upstream[@]} ]]; then
-                       svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
+                       svn_upstream=${svn_upstream[${#svn_upstream[@]} - 2]}
                        svn_upstream=${svn_upstream%@*}
                        local n_stop="${#svn_remote[@]}"
                        for ((n=1; n <= n_stop; n++)); do
@@ -222,6 +200,85 @@ __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.
+__git_ps1_colorize_gitstring ()
+{
+       if [[ -n ${ZSH_VERSION-} ]]; then
+               local c_red='%F{red}'
+               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
+       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/}"
+
+       if [ $detached = no ]; then
+               branch_color="$ok_color"
+       else
+               branch_color="$bad_color"
+       fi
+
+       # 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
+       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"
+}
 
 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
 # when called from PS1 using command substitution
@@ -254,48 +311,66 @@ __git_ps1 ()
                ;;
        esac
 
-       local g="$(__gitdir)"
+       local g="$(git rev-parse --git-dir 2>/dev/null)"
        if [ -z "$g" ]; then
                if [ $pcmode = yes ]; then
                        #In PC mode PS1 always needs to be set
                        PS1="$ps1pc_start$ps1pc_end"
                fi
+               return
+       fi
+
+       local r=""
+       local b=""
+       local step=""
+       local total=""
+       if [ -d "$g/rebase-merge" ]; then
+               read b 2>/dev/null <"$g/rebase-merge/head-name"
+               read step 2>/dev/null <"$g/rebase-merge/msgnum"
+               read total 2>/dev/null <"$g/rebase-merge/end"
+               if [ -f "$g/rebase-merge/interactive" ]; then
+                       r="|REBASE-i"
+               else
+                       r="|REBASE-m"
+               fi
        else
-               local r=""
-               local b=""
-               local step=""
-               local total=""
-               if [ -d "$g/rebase-merge" ]; then
-                       b="$(cat "$g/rebase-merge/head-name")"
-                       step=$(cat "$g/rebase-merge/msgnum")
-                       total=$(cat "$g/rebase-merge/end")
-                       if [ -f "$g/rebase-merge/interactive" ]; then
-                               r="|REBASE-i"
+               if [ -d "$g/rebase-apply" ]; then
+                       read step 2>/dev/null <"$g/rebase-apply/next"
+                       read total 2>/dev/null <"$g/rebase-apply/last"
+                       if [ -f "$g/rebase-apply/rebasing" ]; then
+                               read b 2>/dev/null <"$g/rebase-apply/head-name"
+                               r="|REBASE"
+                       elif [ -f "$g/rebase-apply/applying" ]; then
+                               r="|AM"
                        else
-                               r="|REBASE-m"
+                               r="|AM/REBASE"
                        fi
+               elif [ -f "$g/MERGE_HEAD" ]; then
+                       r="|MERGING"
+               elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+                       r="|CHERRY-PICKING"
+               elif [ -f "$g/REVERT_HEAD" ]; then
+                       r="|REVERTING"
+               elif [ -f "$g/BISECT_LOG" ]; then
+                       r="|BISECTING"
+               fi
+
+               if [ -n "$b" ]; then
+                       :
+               elif [ -h "$g/HEAD" ]; then
+                       # symlink symbolic ref
+                       b="$(git symbolic-ref HEAD 2>/dev/null)"
                else
-                       if [ -d "$g/rebase-apply" ]; then
-                               step=$(cat "$g/rebase-apply/next")
-                               total=$(cat "$g/rebase-apply/last")
-                               if [ -f "$g/rebase-apply/rebasing" ]; then
-                                       r="|REBASE"
-                               elif [ -f "$g/rebase-apply/applying" ]; then
-                                       r="|AM"
-                               else
-                                       r="|AM/REBASE"
+                       local head=""
+                       if ! read head 2>/dev/null <"$g/HEAD"; then
+                               if [ $pcmode = yes ]; then
+                                       PS1="$ps1pc_start$ps1pc_end"
                                fi
-                       elif [ -f "$g/MERGE_HEAD" ]; then
-                               r="|MERGING"
-                       elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
-                               r="|CHERRY-PICKING"
-                       elif [ -f "$g/REVERT_HEAD" ]; then
-                               r="|REVERTING"
-                       elif [ -f "$g/BISECT_LOG" ]; then
-                               r="|BISECTING"
+                               return
                        fi
-
-                       b="$(git symbolic-ref HEAD 2>/dev/null)" || {
+                       # is it a symbolic ref?
+                       b="${head#ref: }"
+                       if [ "$head" = "$b" ]; then
                                detached=yes
                                b="$(
                                case "${GIT_PS1_DESCRIBE_STYLE-}" in
@@ -309,104 +384,70 @@ __git_ps1 ()
                                        git describe --tags --exact-match HEAD ;;
                                esac 2>/dev/null)" ||
 
-                               b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
+                               b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
                                b="unknown"
                                b="($b)"
-                       }
+                       fi
                fi
+       fi
 
-               if [ -n "$step" ] && [ -n "$total" ]; then
-                       r="$r $step/$total"
-               fi
+       if [ -n "$step" ] && [ -n "$total" ]; then
+               r="$r $step/$total"
+       fi
 
-               local w=""
-               local i=""
-               local s=""
-               local u=""
-               local c=""
-               local p=""
+       local w=""
+       local i=""
+       local s=""
+       local u=""
+       local c=""
+       local p=""
 
-               if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
-                       if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
-                               c="BARE:"
+       if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
+               if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
+                       c="BARE:"
+               else
+                       b="GIT_DIR!"
+               fi
+       elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+               if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
+                  [ "$(git config --bool bash.showDirtyState)" != "false" ]
+               then
+                       git diff --no-ext-diff --quiet --exit-code || w="*"
+                       if git rev-parse --quiet --verify HEAD >/dev/null; then
+                               git diff-index --cached --quiet HEAD -- || i="+"
                        else
-                               b="GIT_DIR!"
-                       fi
-               elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
-                       if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
-                          [ "$(git config --bool bash.showDirtyState)" != "false" ]
-                       then
-                               git diff --no-ext-diff --quiet --exit-code || w="*"
-                               if git rev-parse --quiet --verify HEAD >/dev/null; then
-                                       git diff-index --cached --quiet HEAD -- || i="+"
-                               else
-                                       i="#"
-                               fi
-                       fi
-                       if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
-                               git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
-                       fi
-
-                       if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
-                          [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
-                          [ -n "$(git ls-files --others --exclude-standard)" ]
-                       then
-                               u="%${ZSH_VERSION+%}"
-                       fi
-
-                       if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
-                               __git_ps1_show_upstream
+                               i="#"
                        fi
                fi
+               if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
+                       git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
+               fi
 
-               local f="$w$i$s$u"
-               if [ $pcmode = yes ]; then
-                       local gitstring=
-                       if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
-                               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/}"
-
-                               if [ $detached = no ]; then
-                                       branch_color="$ok_color"
-                               else
-                                       branch_color="$bad_color"
-                               fi
+               if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
+                  [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
+                  [ -n "$(git ls-files --others --exclude-standard)" ]
+               then
+                       u="%${ZSH_VERSION+%}"
+               fi
 
-                               # Setting gitstring directly with \[ and \] around colors
-                               # is necessary to prevent wrapping issues!
-                               gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
+               if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+                       __git_ps1_show_upstream
+               fi
+       fi
 
-                               if [ -n "$w$i$s$u$r$p" ]; then
-                                       gitstring="$gitstring "
-                               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"
-                       else
-                               gitstring="$c${b##refs/heads/}${f:+ $f}$r$p"
-                       fi
-                       gitstring=$(printf -- "$printf_format" "$gitstring")
-                       PS1="$ps1pc_start$gitstring$ps1pc_end"
+       local z="${GIT_PS1_STATESEPARATOR-" "}"
+       local f="$w$i$s$u"
+       if [ $pcmode = yes ]; then
+               local gitstring=
+               if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+                       __git_ps1_colorize_gitstring
                else
-                       # NO color option unless in PROMPT_COMMAND mode
-                       printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"
+                       gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
                fi
+               gitstring=$(printf -- "$printf_format" "$gitstring")
+               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"
        fi
 }