我不久前从一些博客文章中获得了这个 bash 代码,它曾经在我的提示符中显示我当前的 git 分支和脏状态。将 git 从 1.7.11.4 更新到 1.8.5.4 后,它不再显示分支或脏状态。
这是我的提示过去的样子:
[~/some/project (master)↑⚡] ->
它显示了(当前的 git 分支),箭头表示我在遥控器前面,我应该推动,闪电表示我有未提交的更改。
更新后,它只是这个(不对存储库进行任何更改):
[~/some/project] ->
这是我.bash_profile中的代码:
RED="[ 33[0;31m]"
YELLOW="[ 33[0;33m]"
GREEN="[ 33[0;32m]"
BLUE="[ 33[0;34m]"
LIGHT_RED="[ 33[1;31m]"
LIGHT_GREEN="[ 33[1;32m]"
WHITE="[ 33[1;37m]"
LIGHT_GRAY="[ 33[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 代码,但还想不通。请帮忙。
- 苹果电脑 10.9.1
- Git 1.8.5.4
- i学期 2
嗯,这比我想象的要容易。事实证明,问题是在 git 1.7 中,git status 命令会回显:
# On branch master
nothing to commit (working directory clean)
前面有领先的#
。现在 git 1.8 输出相同,除了没有那个#
.
所以我所要做的就是改变这些行:
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
自:
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) "