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

发布于 2024-12-22 16:57:55 字数 186 浏览 4 评论 0原文

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

不止一次,我搬起石头砸自己的脚,因为我以为自己在另一个头脑中工作,例如,在他们需要 v.02 时将 v0.3 推向测试,或者修补 dev 中的错误然后需要在 prod 中修补它们,反之亦然。

When using a VCS, (I use Mercurial and Git in Linux, Bash prompt), is there any way to have the prompt show the current head or tag in the directory?

More than once I have shot myself in the foot by working in one head when thinking I was in another, for example, pushing v0.3 to testing when they needed v.02, or patching bugs in dev then they needed to be patched in prod, or vice versa.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

謸气贵蔟 2024-12-29 16:57:55

对于 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? $

末尾的 ? 的提示,告诉您存储库中存在未知文件。

Steve 写了一篇关于他的 Zsh 提示符的博客文章,其中有文档中的更多彩色提示:

Steve's 彩色提示

以获得类似于

末尾的 ? 的提示,告诉您存储库中存在未知文件。

Steve 写了一篇关于他的 Zsh 提示符的博客文章,其中有文档中的更多彩色提示:

Steve's 彩色提示

For Mercurial, there is hg-prompt by Steve Losh. This is an extension for Mercurial that gives you a new hg prompt command. You put a call to this command into your PS1 environment variable:

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

to get a prompt like

user in ~/src/project on feature-branch? $

where the ? at the end tells you that there is an unknown file in your repository.

Steve wrote a blog post about his Zsh prompt and there are more colorful prompts from the documentation:

Steve's colorful prompts

to get a prompt like

where the ? at the end tells you that there is an unknown file in your repository.

Steve wrote a blog post about his Zsh prompt and there are more colorful prompts from the documentation:

Steve's colorful prompts

美羊羊 2024-12-29 16:57:55

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)$ 

Git provides a bash function that shows the current branch or hash if headless. Look for __git_ps1 in the bash_completion file.

My ~/.bashrc contains these lines:

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

which will create a prompt like this:

~/repos/myproject (master)$ 
冷夜 2024-12-29 16:57:55

看看 git-prompt。它将自己描述为“带有 GIT、SVN 和 HG 模块的 bash 提示符”。

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

Have a look at git-prompt. It describes itself as a "bash prompt with GIT, SVN and HG modules".

Just for git you can do it much easier though: simply load the bash-completion module (simply source git-completion.bash - you'll find the file somewhere in the git source tree or if you installed git using your package manager in whatever folder it puts bash completion modules) of git and use __git_ps1 in your prompt, e.g. via export PS1='w$(__git_ps1 "(%s)") > '

山田美奈子 2024-12-29 16:57:55

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

即使您不想要他的确切提示,它也提供了有关他如何构建它的大量信息。

It's worth taking a look at http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/. Git and Mercurial, plus a load of other information.

Even if you don't want his exact prompt it provides a lot of information on how he built it up.

南风几经秋 2024-12-29 16:57:55

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

Have a look at http://vcprompt.com/. It's written in Python, cross-platform, and you can simply download it and drop it into your bin directory. It supports bzr, cvs, darcs, fossil, git, hg and svn.

梦冥 2024-12-29 16:57:55

这是 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 $

Here's a Mercurial bash prompt:

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

Which expands to something like this:

fred@bedrock:~ e65381b0ed42+ tip 0+ default $
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文