threads / discuss / 43077

Re: git-PS1 bash prompt setting

Subject: Re: git-PS1 bash prompt setting

## tl;dr

13 messages between Nov 16, 2006 and Nov 27, 2006.

replies: 12people: 6as markdown or json

Sean· Nov 16, 2006, 18:01 UTC · lore

git-PS1 bash prompt setting

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}]$ "

Junio C Hamano· Nov 16, 2006, 18:35 UTC · re: Sean · lore
Sean <seanlkml@sympatico.ca> writes:
> 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).

Johannes Schindelin· Nov 26, 2006, 14:27 UTC · re: Sean · lore
Hi,
On Thu, 16 Nov 2006, Sean wrote:
Show 5 quoted lines
> 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

Sean· Nov 26, 2006, 14:42 UTC · re: Johannes Schindelin · lore

On Sun, 26 Nov 2006 15:27:07 +0100 (CET) Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

> 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?
Johannes Schindelin· Nov 26, 2006, 15:18 UTC · re: Sean · lore
Hi,
On Sun, 26 Nov 2006, Sean wrote:
Show 8 quoted lines
> On Sun, 26 Nov 2006 15:27:07 +0100 (CET)
> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 
> > 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

Nicolas Vilz· Nov 26, 2006, 15:05 UTC · lore
On Sun, Nov 26, 2006 at 09:42:12AM -0500, Sean wrote:
Show 8 quoted lines
> On Sun, 26 Nov 2006 15:27:07 +0100 (CET)
> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 
> > 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.

Junio C Hamano· Nov 27, 2006, 08:48 UTC · re: Nicolas Vilz · lore
Nicolas Vilz <niv@iaglans.de> writes:
> 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.

Nicolas Vilz· Nov 27, 2006, 10:50 UTC · re: Junio C Hamano · lore
On Mon, Nov 27, 2006 at 12:48:24AM -0800, Junio C Hamano wrote:
Show 9 quoted lines
> Nicolas Vilz <niv@iaglans.de> writes:
> 
> > 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
Shawn Pearce· Nov 27, 2006, 06:54 UTC · lore
Sean <seanlkml@sympatico.ca> wrote:
Show 8 quoted lines
> On Sun, 26 Nov 2006 15:27:07 +0100 (CET)
> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 
> > 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...
Jakub Narebski· Nov 27, 2006, 07:49 UTC · re: Shawn Pearce · lore
Shawn Pearce wrote:
Show 21 quoted lines
> Sean <seanlkml@sympatico.ca> wrote:
>> On Sun, 26 Nov 2006 15:27:07 +0100 (CET)
>> Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
>> 
>>> 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...

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.

-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Sean· Nov 27, 2006, 12:56 UTC · re: Shawn Pearce · lore

On Mon, 27 Nov 2006 01:54:00 -0500 Shawn Pearce <spearce@spearce.org> wrote:

Show 11 quoted lines
> 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.

Shawn Pearce· Nov 27, 2006, 17:02 UTC · lore
Sean <seanlkml@sympatico.ca> wrote:
Show 18 quoted lines
> On Mon, 27 Nov 2006 01:54:00 -0500
> Shawn Pearce <spearce@spearce.org> 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.

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.

> 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.

Sean· Nov 27, 2006, 17:38 UTC · re: Shawn Pearce · lore

On Mon, 27 Nov 2006 12:02:49 -0500 Shawn Pearce <spearce@spearce.org> wrote:

Show 6 quoted lines
> 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.

Show 7 quoted 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.
 
> 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)

← back to recent threads