Skip to contentSkip to conversation

git-PS1 bash prompt setting

DISCUSSION Sean 6 participants 13 messages Aug 11, 2016 2 messages not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to Sean
Quoted 2 lines · Sean

Ted mentioned in the wart thread that having multiple branches per repo means that the standard bash prompt isn't as much help as it could be.

Yes, I think this is common issue for everybody not just people who worked with BK or mercurial. I find myself almost typing "pwd" to find out what branch I am on (I do not go as far as typing "git cd" to switch branches, though).

#4 lore raw
In reply to a message not in the archive
Quoted 8 lines · Sean
Quoted 3 lines · On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin…

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

What about just making it an option to the git wrapper?

I'm using something like this, and will be adding it to git-completion.bash tonight:

	__git_ps1 ()
	{
		local b="$(git symbolic-ref HEAD 2>/dev/null)"
		if [ -n "$b" ]; then echo "(${b##refs/heads/})"; fi
	}
	PS1='[\u@\h \W$(__git_ps1)]\$ '

it works very well...

author
#5 lore raw
Quoted 11 lines · On Mon, 27 Nov 2006 01:54:00 -0500 Shawn Pearce <[email protected]> wrote:

I'm using something like this, and will be adding it to git-completion.bash tonight:

	__git_ps1 ()
	{
		local b="$(git symbolic-ref HEAD 2>/dev/null)"
		if [ -n "$b" ]; then echo "(${b##refs/heads/})"; fi
	}
	PS1='[\u@\h \W$(__git_ps1)]\$ '

it works very well...

Yeah, when I first coded it I even looked at making it a bash loadable to make it perform better but found the prototype to run acceptably,
so never bothered. If Git does get a --show-ps1 option, people will
still be able to roll their own version to tweak the output format as you did above. Hopefully the standard format will work for most though.

Sean

author
#0 lore raw

Ted mentioned in the wart thread that having multiple branches per repo means that the standard bash prompt isn't as much help as it could be.

For what it's worth i'll post a little script i've been using for quite some time that helps a little. It's called git-PS1 and is used by including it in your bash PS1 variable like so:

export PS1='$(git-PS1 "[\u@\h \W]\$ ")'

If you're not in a git repo, the bash prompt you pass on the git-PS1 command line is used instead. If you are in a git repo, you'll get the following as a prompt:

[branch!repo/relative/path]$

Where "repo" is the basename of the path to the root of your repo. An example would look like this:

[master!linus-2.6/Documentation/vm]$

Cheers,
Sean

#!/bin/bash
BR=$(git symbolic-ref HEAD 2>/dev/null) || { echo "$@" ; exit ; } BR=${BR#refs/heads/}
REL=$(git rev-parse --show-prefix)
REL="${REL//%\/}"
LOC="${PWD%/$REL}"
echo "[$BR!${LOC/*\/}${REL:+/$REL}]$ "

#2 lore raw

Hi,

Quoted 5 lines · Sean

For what it's worth i'll post a little script i've been using for quite some time that helps a little. It's called git-PS1 and is used by including it in your bash PS1 variable like so:

export PS1='$(git-PS1 "[\u@\h \W]\$ ")'

I actually like that very much! So much that I tried to implement this as an option to an existing builtin (I did not want to clutter the list of commands any more...).

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

Ciao,
Dscho

#11 lore raw
In reply to Nicolas Vilz
Quoted 4 lines · Nicolas Vilz

I glued that in my system bashrc with the extension, that it shows to me user.email out of repo-config, which is also very handy. So I am always reminded to set my user.credentials in .git/config and i always know which role i play in the repository i am working at.

I think there is something wrong if the user needs to be _constantly_ reminded who he is. Care to explain?

There was a talk on the list about making "git-commit" (and "git-merge" that does not do a fast-forward) refuse to proceed until user.nameemail are explicitly set in the configuration. I think that particular implementation is a bad idea because it punishes people who have set up their GECOS and hostname sanely, but at the same time I do understand that people who just started to use git would be very unhappy to learn that their names were misspelled in GECOS field only after accumulating a couple dozen commits.

#12 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 4 lines · Nicolas Vilz

I glued that in my system bashrc with the extension, that it shows to me user.email out of repo-config, which is also very handy. So I am always reminded to set my user.credentials in .git/config and i always know which role i play in the repository i am working at.

I think there is something wrong if the user needs to be _constantly_ reminded who he is. Care to explain?

Yes, you are right. It sounds a bit strange. I try to setup a pool of configuration data for several linux-boxes which are kind of equal in a git repository.

Normally I just pull the config-branch from the repository to the particular box, but sometimes i have to test the config and alter it... and other people do as well. And therefore i like to be reminded constantly who I am.

I glued that in the system bashrc, so I have it for services which run as users and which configuration data has to be edited by the services user... also here, several people are working on that machine and make changes.

Sincerly

author
#13 lore raw
In reply to Johannes Schindelin
Quoted 3 lines · On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin…

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

What about just making it an option to the git wrapper?

#6 lore raw
In reply to Shawn Pearce
Quoted 13 lines · Shawn Pearce
Quoted 8 lines · Sean
Quoted 3 lines · On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin…

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

What about just making it an option to the git wrapper?

I'm using something like this, and will be adding it to git-completion.bash tonight:

Quoted 8 lines
      __git_ps1 ()
      {
              local b="$(git symbolic-ref HEAD 2>/dev/null)"
              if [ -n "$b" ]; then echo "(${b##refs/heads/})"; fi
      }
      PS1='[\u@\h \W$(__git_ps1)]\$ '

it works very well...

Perhaps, as it was proposed somewhere else in this thread, instead of \u@\h use $(git repo-config --get user.email)?

And I would add \!: at the beginning of prompt, but that might be just me.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#10 lore raw
In reply to a message not in the archive
Quoted 8 lines · Sean
Quoted 3 lines · On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin…

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

What about just making it an option to the git wrapper?

I glued that in my system bashrc with the extension, that it shows to me user.email out of repo-config, which is also very handy. So I am always reminded to set my user.credentials in .git/config and i always know which role i play in the repository i am working at.

author
#9 lore raw
In reply to Shawn Pearce
Quoted 6 lines · On Mon, 27 Nov 2006 12:02:49 -0500 Shawn Pearce <[email protected]> wrote:

When I originally coded the first version of __git_ps1 I was using it on a Cygwin system, where the fork+exec of the external script can take a little while. The time to fork+exec two programs (script and then git) is huge compared to just fork+exec of git by itself, so I coded it as a function. On the other hand my Mac OS X system doesn't even blink at either implementation.

For sure, that's probably the reason that it was suggested to be included as a built-in, it would reduce the overhead.

Quoted 7 lines

I'm not sure that's worth implementing in the core code. Most shells that will let you invoke a command as part of their prompt generation will also let you use builtin functions and do some basic string manipulation (e.g. like I do above with bash). At which point it is say 5 lines of shell (nicely formatted) to craft a prompt string vs. 15-20 lines of C to parse the option, read HEAD, and craft a prompt string.

Well, as per my previous message, it might be a little more worthwhile if it did things like parse an additional format string which let you reference config variables and relative directory etc. It would give a lot of flexibility as to what someone wanted to use as their PS1 when inside a git repo.

Quoted 3 lines

If someone else contributes a --show-ps1 option that is useable as a replacement for my __git_ps1 I'll gladly jump on board and change to using it, but I just don't see a reason to write it myself.

Yeah.. __git_ps1 could just call "git --show-ps1" if it ever comes into existence ;o)

#14 lore raw
In reply to Sean

Hi,

Quoted 8 lines · Sean
Quoted 3 lines · On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin…

But there really is no good place to put it: most commands need a git repository, and those which do not, are inappropriate to put an option "--show-ps1" into. Except maybe repo-config. Thoughts?

What about just making it an option to the git wrapper?

D'oh. Too easy!

Thanks,
Dscho

#8 lore raw
In reply to a message not in the archive
Quoted 13 lines · Sean
Quoted 10 lines · On Mon, 27 Nov 2006 01:54:00 -0500 Shawn Pearce <[email protected]> wrote:

I'm using something like this, and will be adding it to git-completion.bash tonight:

	__git_ps1 ()
	{
		local b="$(git symbolic-ref HEAD 2>/dev/null)"
		if [ -n "$b" ]; then echo "(${b##refs/heads/})"; fi
	}
	PS1='[\u@\h \W$(__git_ps1)]\$ '
Quoted 5 lines
Quoted 1 line

it works very well...

Yeah, when I first coded it I even looked at making it a bash loadable to make it perform better but found the prototype to run acceptably, so never bothered.

When I originally coded the first version of __git_ps1 I was using it on a Cygwin system, where the fork+exec of the external script can take a little while. The time to fork+exec two programs (script and then git) is huge compared to just fork+exec of git by itself, so I coded it as a function. On the other hand my Mac OS X system doesn't even blink at either implementation.

Quoted 4 lines

If Git does get a --show-ps1 option, people will
still be able to roll their own version to tweak the output format as you did above. Hopefully the standard format will work for most though.

I'm not sure that's worth implementing in the core code. Most shells that will let you invoke a command as part of their prompt generation will also let you use builtin functions and do some basic string manipulation (e.g. like I do above with bash). At which point it is say 5 lines of shell (nicely formatted) to craft a prompt string vs. 15-20 lines of C to parse the option, read HEAD, and craft a prompt string.

If someone else contributes a --show-ps1 option that is useable as a replacement for my __git_ps1 I'll gladly jump on board and change to using it, but I just don't see a reason to write it myself.