在VCS中使shell提示符显示当前头部

时间:2011-12-23 02:00:50

标签: git version-control mercurial dvcs command-prompt

使用VCS时(我在Linux中使用Mercurial和Git,Bash提示),有没有办法让提示显示目录中的当前头部或标记?

当我认为自己在另一个脑袋中时,我不止一次用自己的脑袋开枪,例如,当他们需要v.02时将v0.3推送到测试,或者在dev中修补bug然后他们需要在prod中修补,反之亦然。

6 个答案:

答案 0 :(得分:9)

对于Mercurial,Steve Losh有hg-prompt。这是Mercurial的扩展,它为您提供了一个新的hg prompt命令。您将此命令的调用放入PS1环境变量:

export PS1='\u in \w`hg prompt "{on {branch}}{status}{update}" 2>/dev/null` $'

得到像

这样的提示
user in ~/src/project on feature-branch? $

最后?告诉您存储库中存在未知文件。

史蒂夫写了blog post about his Zsh prompt,文档中有更多色彩的提示:

Steve's colorful prompts

答案 1 :(得分:3)

Git提供了一个bash函数,显示当前分支或散列(如果无头)。在bash_completion文件中查找__git_ps1。

我的~/.bashrc包含以下行:

Green='\[\e[0;32m\]'
BIGreen='\[\e[1;92m\]'
Color_Off='\[\e[0m\]'
export PS1=$Green'\w $(__git_ps1 "(%s)")'$BIGreen'$ '$Color_Off

将创建如下提示:

~/repos/myproject (master)$ 

答案 2 :(得分:2)

看看git-prompt。它将自己描述为“使用GIT,SVN和HG模块的bash提示”。

对于git你可以更容易地做到:只需加载bash-completion模块(只需source git-completion.bash - 你可以在git源代码树的某个地方找到文件,或者你是否使用你的包管理器安装了git在git的任何文件夹中放入bash完成模块)并在你的提示中使用__git_ps1,例如通过export PS1='w$(__git_ps1 "(%s)") > '

答案 3 :(得分:1)

值得一看http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/。 Git和Mercurial,以及其他一些信息。

即使你不想要他的确切提示,它也提供了很多关于他如何构建它的信息。

答案 4 :(得分:1)

看看http://vcprompt.com/。它是用Python编写的,跨平台的,您只需将其下载并放入bin目录即可。它支持bzr,cvs,darcs,fossil,git,hg和svn。

答案 5 :(得分:0)

这是一个Mercurial bash提示符:

function parse_hg_branch {
  _hg_id=`hg id 2> /dev/null`
  _hg_id="$_hg_id `hg id -nb 2> /dev/null`"
  echo $_hg_id
  unset _hg_id
}

function proml {
  local LIGHT_RED="\[\033[1;31m\]"
  local     BROWN="\[\033[0;33m\]"
  local     GREEN="\[\033[0;32m\]"
  local      CYAN="\[\033[0;36m\]"
  local   DEFAULT="\[\033[0m\]"

  PS1="$LIGHT_RED\u@$BROWN\h:$GREEN\w $CYAN\$(parse_hg_branch) $DEFAULT\$ "
}
proml

扩展到这样的东西:

fred@bedrock:~ e65381b0ed42+ tip 0+ default $