Merge branch 'ed/color-prompt'
authorJunio C Hamano <gitster@pobox.com>
Mon, 1 Jul 2013 19:41:55 +0000 (12:41 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Jul 2013 19:41:55 +0000 (12:41 -0700)
Code clean-up for in-prompt status script (in contrib/).

* ed/color-prompt:
git-prompt.sh: add missing information in comments
git-prompt.sh: do not print duplicate clean color code
t9903: remove redundant tests
git-prompt.sh: refactor colored prompt code
t9903: add tests for git-prompt pcmode

1  2 
contrib/completion/git-prompt.sh
t/t9903-bash-prompt.sh
index daed6a1d039cc8b2f5adb1d089e36ea7011e0536,b3f39e82d549cf5e25ec5c292e1ba157dfe7be8f..a81ef5a482d48b114a81e242109e2f0dec0839ed
  #    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, __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.
++#    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
  #
  #
  # 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.
  
 -# __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 ()
@@@ -401,74 -362,71 +371,75 @@@ __git_ps1 (
                                        git describe --tags --exact-match HEAD ;;
                                esac 2>/dev/null)" ||
  
 -                              b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
 -                              b="unknown"
 +                              b="$short_sha..."
                                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" = "$inside_gitdir" ]; then
 +              if [ "true" = "$bare_repo" ]; then
 +                      c="BARE:"
 +              else
 +                      b="GIT_DIR!"
 +              fi
 +      elif [ "true" = "$inside_worktree" ]; then
 +              if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
 +                 [ "$(git config --bool bash.showDirtyState)" != "false" ]
 +              then
 +                      git diff --no-ext-diff --quiet --exit-code || w="*"
 +                      if [ -n "$short_sha" ]; 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="$"
 +                              i="#"
                        fi
 +              fi
 +              if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
 +                 [ -r "$g/refs/stash" ]; then
 +                      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_SHOWUNTRACKEDFILES-}" ] &&
 +                 [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
 +                 git ls-files --others --exclude-standard --error-unmatch -- '*' >/dev/null 2>/dev/null
 +              then
 +                      u="%${ZSH_VERSION+%}"
 +              fi
  
 -                      if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
 -                              __git_ps1_show_upstream
 -                      fi
 +              if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
 +                      __git_ps1_show_upstream
                fi
 +      fi
  
 -              local z="${GIT_PS1_STATESEPARATOR-" "}"
 +      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
++      # 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"
 +      local f="$w$i$s$u"
++      local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
 -              if [ $pcmode = yes ]; then
 +      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
                        gitstring=$(printf -- "$printf_format" "$gitstring")
 -                      PS1="$ps1pc_start$gitstring$ps1pc_end"
                else
 -                      printf -- "$printf_format" "$gitstring"
 +                      printf -v gitstring -- "$printf_format" "$gitstring"
                fi
-               # NO color option unless in PROMPT_COMMAND mode
-               printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
 +              PS1="$ps1pc_start$gitstring$ps1pc_end"
 +      else
++              printf -- "$printf_format" "$gitstring"
        fi
  }
index c05458cbe671b4881af3596d9b06a120674519d7,5cd138ed9a87f13a4b7016078578dc355b595267..3c3e4e8c38023595978368e0b1665e122e0bdfab
@@@ -10,10 -10,15 +10,14 @@@ test_description='test git-specific bas
  . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
  
  actual="$TRASH_DIRECTORY/actual"
+ c_red='\\[\\e[31m\\]'
+ c_green='\\[\\e[32m\\]'
+ c_lblue='\\[\\e[1;34m\\]'
+ c_clear='\\[\\e[0m\\]'
  
  test_expect_success 'setup for prompt tests' '
 -      mkdir -p subdir/subsubdir &&
        git init otherrepo &&
 -      echo 1 > file &&
 +      echo 1 >file &&
        git add file &&
        test_tick &&
        git commit -m initial &&