Bash PROMPT_命令,用于在git更新到1.8之前显示git分支

Bash PROMPT_命令,用于在git更新到1.8之前显示git分支,git,bash,Git,Bash,我不久前从一些博客文章中获得了这段bash代码,它用于在提示符中显示我当前的git分支和脏状态。将git从1.7.11.4更新到1.8.5.4后,它不再显示分支或脏状态 这是我的提示过去的样子: [~/some/project (master)↑⚡] -> 它显示了(当前git分支),箭头表示我在遥控器前面,我应该按下,闪电表示我有未限制的更改 更新后,仅此而已(对回购协议没有任何更改): 以下是我的.bash_配置文件中的代码: RED="\[\033[0;31m\]"

我不久前从一些博客文章中获得了这段bash代码,它用于在提示符中显示我当前的git分支和脏状态。将git从1.7.11.4更新到1.8.5.4后,它不再显示分支或脏状态

这是我的提示过去的样子:

[~/some/project (master)↑⚡] ->
它显示了(当前git分支),箭头表示我在遥控器前面,我应该按下,闪电表示我有未限制的更改

更新后,仅此而已(对回购协议没有任何更改):

以下是我的.bash_配置文件中的代码:

        RED="\[\033[0;31m\]"
     YELLOW="\[\033[0;33m\]"
        GREEN="\[\033[0;32m\]"
       BLUE="\[\033[0;34m\]"
  LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
      WHITE="\[\033[1;37m\]"
 LIGHT_GRAY="\[\033[0;37m\]"
 COLOR_NONE="\[\e[0m\]"

function parse_git_branch {
  git rev-parse --git-dir &> /dev/null
  git_status="$(git status 2> /dev/null)"
  branch_pattern="^# On branch ([^${IFS}]*)"
  remote_pattern="# Your branch is (.*) of"
  diverge_pattern="# Your branch and (.*) have diverged"

  if [[ ! ${git_status}} =~ "working directory clean" ]]; then
    state="${RED}⚡"
  fi

  # add an else if or two here if you want to get more specific
  if [[ ${git_status} =~ ${remote_pattern} ]]; then
    if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
      remote="${YELLOW}↑"
    else
      remote="${YELLOW}↓"
    fi
  fi

  if [[ ${git_status} =~ ${diverge_pattern} ]]; then
    remote="${YELLOW}↕"
  fi

  if [[ ${git_status} =~ ${branch_pattern} ]]; then
    branch=${BASH_REMATCH[1]}
    echo " (${branch})${remote}${state}"
  fi
}

function prompt_func() {
    previous_return_value=$?;
    prompt="${TITLEBAR}${BLUE}[${YELLOW}\w${GREEN}$(parse_git_branch)${BLUE}]${COLOR_NONE} "
    if test $previous_return_value -eq 0
    then
        PS1="${prompt}➔ "
    else
        PS1="${prompt}${RED}➔${COLOR_NONE} "
    fi
}

PROMPT_COMMAND=prompt_func
我对bash不是很在行,所以有人能发现问题,或者有更好的解决方案吗

我正在玩弄bash代码,但还没弄明白。请帮忙

  • Mac 10.9.1
  • Git 1.8.5.4
  • iTerm 2

    • 嗯,这比我想象的要容易。问题是在git 1.7中,git status命令会回显:

      # On branch master
      nothing to commit (working directory clean)
      
      前面有一个前导的
      #
      。现在,Git1.8输出相同的内容,只是没有
      #

      所以我所要做的就是更改这些行:

      branch_pattern="^# On branch ([^${IFS}]*)"
      remote_pattern="# Your branch is (.*) of"
      diverge_pattern="# Your branch and (.*) have diverged"
      
      致:


      删除了
      #
      后,一切都会恢复正常。

      注意,此模式仍然失败:

      remote_pattern="Your branch is (.*) of"
      
      新的git输出如下所示:

      Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
      Your branch is ahead of 'origin/master' by 1 commit.
      
      因此,模式应改为:

      remote_pattern="Your branch is (behind|ahead) "
      
      Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
      Your branch is ahead of 'origin/master' by 1 commit.
      
      remote_pattern="Your branch is (behind|ahead) "