Make command line prompt current head when in VCS

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

More than once I shot in the foot, working in one head, thinking that I was in another, for example, by pressing v0.3 for testing, when they need v.02, or fixing errors in dev , then they need to be fixed in prod or vice versa .

+8
git version-control dvcs mercurial command-prompt
source share
6 answers

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

My ~/.bashrc contains the following 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 the following prompt:

 ~/repos/myproject (master)$ 
+3
source share

For Mercurial there is Stephen Lawsh hg-prompt . This is an extension for Mercurial that gives you a new hg prompt command. You call this command in your PS1 environment variable:

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

to get a hint for example

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

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

Steve wrote a message

+9
source share

Take a look at git-prompt . He describes himself as a bash invitation with the GIT, SVN, and HG modules. "

Only for git can you make it much simpler: just download the bash module-complex (just source git-completion.bash - you will find the file somewhere in the git source tree or if you installed git using the package manager in any folder in which it puts the bash) git termination modules, and use __git_ps1 at your prompt, for example via export PS1='w$(__git_ps1 "(%s)") > '

+2
source share

Worth a look at http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/ . Git and Mercurial, as well as a load of other information.

Even if you do not want his exact request, he provides a lot of information about how he created it.

+1
source share

Take a look at http://vcprompt.com/ . It is written in Python, cross-platform, and you can just load it and transfer it to the bin directory. It supports bzr, cvs, darcs, fossil, git, hg and svn.

+1
source share

Here's a Mercurial bash query:

 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 something like this:

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

All Articles