threads / discuss / 11922

command prompt script for current branch

Subject: command prompt script for current branch

## tl;dr

16 messages between Feb 6, 2008 and Feb 8, 2008.

replies: 15people: 8as markdown or json

Stephen Sinclair· Feb 6, 2008, 19:52 UTC · lore
Hello,

Attached is a script I quickly wrote up yesterday. Sometimes I've found I started working only to realize I was on the wrong branch. (Now that I'm accustomed to working with git, I make many small branches, so it happens.)

So I made this small script to simply write the name of the currently checked out branch, as well as information on how many commits it differs from the remote tracking branch, if any. It also appends an exclamation mark if I have uncommitted changes. I stuck it in my PS1 environment variable, so that it would be part of my command prompt.

I tried to choose git commands that would be relatively quick, since this will be run for every single command prompt. If I've chosen suboptimal ways of doing this, please let me know. There's a few too many 'grep' and 'sed' commands for my liking, so maybe there is a way to get the same information more directly instead of parsing git command output. Anyways, if it's of general interest, I'd be happy to make a patch for /contrib.

For my tiny git repos, this seems to have pretty much no negative impact on my work flow. When I cd'd to my git.git clone there was about a 1/2-second delay the first time, but subsequent command prompts incurred no visible delay. I've no idea how fast it would be for something as big as the kernel, for example.

Steve
Junio C Hamano· Feb 6, 2008, 20:42 UTC · re: Stephen Sinclair · lore

Re: command prompt script for current branch

"Stephen Sinclair" <radarsat1@gmail.com> writes:
> ...  Sometimes I've
> found I started working only to realize I was on the wrong branch.
See contrib/completion/git-completion.bash if you are a bash user.
Robin Rosenberg· Feb 6, 2008, 22:14 UTC · re: Junio C Hamano · lore

[PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Hi Junio,

I'm not sure whether you didn't like it or not or just lost it. Here is the vastly enhanced prompt again, rebased.

-- robin
>From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001
From: Shawn O. Pearce <spearce@spearce.org>
Date: Tue, 4 Sep 2007 03:13:01 -0400
Subject: 

This patch makes the git prompt (when enabled) show if a merge or a rebase is unfinished. It also detects if a bisect is being done as well as detached checkouts.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 contrib/completion/git-completion.bash |   37 ++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 0d33f9a..4ea727b 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -64,12 +64,41 @@ __gitdir ()
 
 __git_ps1 ()
 {
-	local b="$(git symbolic-ref HEAD 2>/dev/null)"
-	if [ -n "$b" ]; then
+	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	if [ -n "$g" ]; then
+		local r
+		local b
+		if [ -d "$g/../.dotest" ]
+		then
+			r="|AM/REBASE"
+			b="$(git symbolic-ref HEAD 2>/dev/null)"
+		elif [ -f "$g/.dotest-merge/interactive" ]
+		then
+			r="|REBASE-i"
+			b="$(cat $g/.dotest-merge/head-name)"
+		elif [ -d "$g/.dotest-merge" ]
+		then
+			r="|REBASE-m"
+			b="$(cat $g/.dotest-merge/head-name)"
+		elif [ -f "$g/MERGE_HEAD" ]
+		then
+			r="|MERGING"
+			b="$(git symbolic-ref HEAD 2>/dev/null)"
+		else
+			if [ -f $g/BISECT_LOG ]
+			then
+				r="|BISECTING"
+			fi
+			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
+			then
+				b="$(cut -c1-7 $g/HEAD)..."
+			fi
+		fi
+
 		if [ -n "$1" ]; then
-			printf "$1" "${b##refs/heads/}"
+			printf "$1" "${b##refs/heads/}$r"
 		else
-			printf " (%s)" "${b##refs/heads/}"
+			printf " (%s)" "${b##refs/heads/}$r"
 		fi
 	fi
 }
-- 
1.5.4.rc4.25.g81cc
Robin Rosenberg· Feb 6, 2008, 22:23 UTC · re: Robin Rosenberg · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Actually I wrote the patch and Shawn sent back a sligtly better version, i.e.:

From: Robin Rosenberg <robin.rosenberg@dewire.com>
-- robin
Junio C Hamano· Feb 6, 2008, 22:31 UTC · re: Robin Rosenberg · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes:
> I'm not sure whether you didn't like it or not or just lost it. Here
> is the vastly enhanced prompt again, rebased.
Most likely "lost", and I appreciate a reminder like this.
Show 10 quoted lines
> From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001
> From: Shawn O. Pearce <spearce@spearce.org>
> Date: Tue, 4 Sep 2007 03:13:01 -0400
> Subject: 
>
> This patch makes the git prompt (when enabled) show if a merge or a
> rebase is unfinished. It also detects if a bisect is being done as
> well as detached checkouts.
>
> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>

But this looks funny, without Subject: and also S-o-b by the original author (if Shawn is indeed the original author). It appears that the patch originally was from Shawn and after discussion petered out around Oct 1st 2007 there wasn't a resend for inclusion. Is this improved/revised in any way from the one in the thread?

http://thread.gmane.org/gmane.comp.version-control.git/57271/focus=57536
Robin Rosenberg· Feb 7, 2008, 00:23 UTC · re: Junio C Hamano · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

onsdagen den 6 februari 2008 skrev Junio C Hamano:
Show 24 quoted lines
> Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes:
> 
> > I'm not sure whether you didn't like it or not or just lost it. Here
> > is the vastly enhanced prompt again, rebased.
> 
> Most likely "lost", and I appreciate a reminder like this.
> 
> > From 76aa8bae8491c1ffbd6e3f5c99ab014ef87794c8 Mon Sep 17 00:00:00 2001
> > From: Shawn O. Pearce <spearce@spearce.org>
> > Date: Tue, 4 Sep 2007 03:13:01 -0400
> > Subject: 
> >
> > This patch makes the git prompt (when enabled) show if a merge or a
> > rebase is unfinished. It also detects if a bisect is being done as
> > well as detached checkouts.
> >
> > Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
> 
> But this looks funny, without Subject: and also S-o-b by the
> original author (if Shawn is indeed the original author).  It
> appears that the patch originally was from Shawn and after
> discussion petered out around Oct 1st 2007 there wasn't a resend
> for inclusion.  Is this improved/revised in any way from the one
> in the thread?
No.

I sent a patch and Shawn modified it somewhat and posted the full modified patch. Here is the same patch again (to make sure you get the right one). It has a better comment than the one I sent recently.

-- robin
>From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00 2001
From: Robin Rosenberg <robin.rosenberg@dewire.com>
Date: Sun, 30 Sep 2007 02:20:45 +0200
Subject: [PATCH] Improve bash prompt to detect various states like an unfinished merge

This patch makes the git prompt (when enabled) show if a merge or a rebase is unfinished. It also detects if a bisect is being done as well as detached checkouts.

An uncompleted git-am cannot be distinguised from a rebase (the non-interactive version). Instead of having an even longer prompt we simply ignore that and hope the power users that use git-am knows the difference.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 contrib/completion/git-completion.bash |   37 ++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 0d33f9a..4ea727b 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -64,12 +64,41 @@ __gitdir ()
 
 __git_ps1 ()
 {
-	local b="$(git symbolic-ref HEAD 2>/dev/null)"
-	if [ -n "$b" ]; then
+	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	if [ -n "$g" ]; then
+		local r
+		local b
+		if [ -d "$g/../.dotest" ]
+		then
+			r="|AM/REBASE"
+			b="$(git symbolic-ref HEAD 2>/dev/null)"
+		elif [ -f "$g/.dotest-merge/interactive" ]
+		then
+			r="|REBASE-i"
+			b="$(cat $g/.dotest-merge/head-name)"
+		elif [ -d "$g/.dotest-merge" ]
+		then
+			r="|REBASE-m"
+			b="$(cat $g/.dotest-merge/head-name)"
+		elif [ -f "$g/MERGE_HEAD" ]
+		then
+			r="|MERGING"
+			b="$(git symbolic-ref HEAD 2>/dev/null)"
+		else
+			if [ -f $g/BISECT_LOG ]
+			then
+				r="|BISECTING"
+			fi
+			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
+			then
+				b="$(cut -c1-7 $g/HEAD)..."
+			fi
+		fi
+
 		if [ -n "$1" ]; then
-			printf "$1" "${b##refs/heads/}"
+			printf "$1" "${b##refs/heads/}$r"
 		else
-			printf " (%s)" "${b##refs/heads/}"
+			printf " (%s)" "${b##refs/heads/}$r"
 		fi
 	fi
 }
-- 
1.5.4.rc4.25.g81cc
Shawn O. Pearce· Feb 7, 2008, 06:34 UTC · re: Robin Rosenberg · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote:
Show 21 quoted lines
> I sent a patch and Shawn modified it somewhat and posted the full modified
> patch. Here is the same patch again (to make sure you get the right one). It
> has a better comment than the one I sent recently.
> 
> -- robin
> 
> From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00 2001
> From: Robin Rosenberg <robin.rosenberg@dewire.com>
> Date: Sun, 30 Sep 2007 02:20:45 +0200
> Subject: [PATCH] Improve bash prompt to detect various states like an unfinished merge
> 
> This patch makes the git prompt (when enabled) show if a merge or a
> rebase is unfinished. It also detects if a bisect is being done as
> well as detached checkouts.
> 
> An uncompleted git-am cannot be distinguised from a rebase (the
> non-interactive version). Instead of having an even longer prompt
> we simply ignore that and hope the power users that use git-am knows
> the difference.
> 
> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
For what it's worth:
Acked-by: Shawn O. Pearce <spearce@spearce.org>
-- 
Shawn.
Steffen Prohaska· Feb 8, 2008, 11:26 UTC · re: Shawn O. Pearce · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

On Feb 7, 2008, at 7:34 AM, Shawn O. Pearce wrote:
Show 30 quoted lines
> Robin Rosenberg <robin.rosenberg.lists@dewire.com> wrote:
>> I sent a patch and Shawn modified it somewhat and posted the full  
>> modified
>> patch. Here is the same patch again (to make sure you get the  
>> right one). It
>> has a better comment than the one I sent recently.
>>
>> -- robin
>>
>> From 82a5b8d5b043ffc7c1950b391ec7ae69d575640b Mon Sep 17 00:00:00  
>> 2001
>> From: Robin Rosenberg <robin.rosenberg@dewire.com>
>> Date: Sun, 30 Sep 2007 02:20:45 +0200
>> Subject: [PATCH] Improve bash prompt to detect various states like  
>> an unfinished merge
>>
>> This patch makes the git prompt (when enabled) show if a merge or a
>> rebase is unfinished. It also detects if a bisect is being done as
>> well as detached checkouts.
>>
>> An uncompleted git-am cannot be distinguised from a rebase (the
>> non-interactive version). Instead of having an even longer prompt
>> we simply ignore that and hope the power users that use git-am knows
>> the difference.
>>
>> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
>
> For what it's worth:
>
> Acked-by: Shawn O. Pearce <spearce@spearce.org>

This improved prompt is great. I already miss it each time I switch back from next to master.

	Steffen
Johannes Schindelin· Feb 8, 2008, 12:25 UTC · re: Steffen Prohaska · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Hi,
On Fri, 8 Feb 2008, Steffen Prohaska wrote:
> This improved prompt is great.  I already miss it each time I switch 
> back from next to master.

Maybe we (as in msysgit) should install the git-completion.bash script into /share/git-core/?

Ciao, Dscho

Steffen Prohaska· Feb 8, 2008, 13:12 UTC · re: Johannes Schindelin · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

On Feb 8, 2008, at 1:25 PM, Johannes Schindelin wrote:
Show 7 quoted lines
> On Fri, 8 Feb 2008, Steffen Prohaska wrote:
>
>> This improved prompt is great.  I already miss it each time I switch
>> back from next to master.
>
> Maybe we (as in msysgit) should install the git-completion.bash script
> into /share/git-core/?
Yes.
	Steffen
Johannes Schindelin· Feb 8, 2008, 14:06 UTC · re: Steffen Prohaska · lore

[PATCH/RFC] Make git-completion.bash a first-class citizen

This moves git-completion.bash out of contrib/, and installs it to $sharedir/git-completion/

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	On Fri, 8 Feb 2008, Steffen Prohaska wrote:
	
	> On Feb 8, 2008, at 1:25 PM, Johannes Schindelin wrote:
	> 
	> > On Fri, 8 Feb 2008, Steffen Prohaska wrote:
	> > 
	> > > This improved prompt is great.  I already miss it each time 
	> > > I switch back from next to master.
	> > 
	> > Maybe we (as in msysgit) should install the 
	> > git-completion.bash script into /share/git-core/?
	> 
	> Yes.
	How about this?
 Makefile                                           |    4 ++++
 .../git-completion.bash => git-completion.bash     |    0 
 2 files changed, 4 insertions(+), 0 deletions(-)
 rename contrib/completion/git-completion.bash => git-completion.bash (100%)
diff --git a/Makefile b/Makefile
index de8c752..77efc3f 100644
--- a/Makefile
+++ b/Makefile
@@ -168,6 +168,7 @@ gitexecdir = $(bindir)
 sharedir = $(prefix)/share
 template_dir = $(sharedir)/git-core/templates
 htmldir=$(sharedir)/doc/git-doc
+completiondir = $(sharedir)/git-completion
 ifeq ($(prefix),/usr)
 sysconfdir = /etc
 else
@@ -784,6 +785,7 @@ bindir_SQ = $(subst ','\'',$(bindir))
 mandir_SQ = $(subst ','\'',$(mandir))
 infodir_SQ = $(subst ','\'',$(infodir))
 gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
+completiondir_SQ = $(subst ','\'',$(completiondir))
 template_dir_SQ = $(subst ','\'',$(template_dir))
 htmldir_SQ = $(subst ','\'',$(htmldir))
 prefix_SQ = $(subst ','\'',$(prefix))
@@ -1067,6 +1069,8 @@ install: all
 	$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitexecdir_SQ)'
 	$(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)'
 	$(INSTALL) git$X '$(DESTDIR_SQ)$(bindir_SQ)'
+	$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(completiondir_SQ)'
+	$(INSTALL) git-completion.bash '$(DESTDIR_SQ)$(completiondir_SQ)'
 	$(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install
 	$(MAKE) -C perl prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' install
 ifndef NO_TCLTK
diff --git a/contrib/completion/git-completion.bash b/git-completion.bash
similarity index 100%
rename from contrib/completion/git-completion.bash
rename to git-completion.bash
-- 
1.5.4.1261.g18335
Jakub Narebski· Feb 6, 2008, 23:21 UTC · re: Robin Rosenberg · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes:
> This patch makes the git prompt (when enabled) show if a merge or a
> rebase is unfinished. It also detects if a bisect is being done as
> well as detached checkouts.
[...]
> +		if [ -d "$g/../.dotest" ]
[...]
> +		elif [ -f "$g/.dotest-merge/interactive" ]
[...]
> +		elif [ -d "$g/.dotest-merge" ]

Hmmm... is it time to ressurect "git explain" / "git info" / "git state" command idea, as to not need to harcode info about state in scripts, putting it in only one place?

-- 
Jakub Narebski
Poland
ShadeHawk on #git
Mike Hommey· Feb 6, 2008, 23:44 UTC · re: Jakub Narebski · lore

Re: [PATCH (repost)] Improve bash prompt to detect merge / rebase in progress

On Wed, Feb 06, 2008 at 03:21:40PM -0800, Jakub Narebski wrote:
Show 15 quoted lines
> Robin Rosenberg <robin.rosenberg.lists@dewire.com> writes:
> 
> > This patch makes the git prompt (when enabled) show if a merge or a
> > rebase is unfinished. It also detects if a bisect is being done as
> > well as detached checkouts.
> [...]
> > +		if [ -d "$g/../.dotest" ]
> [...]
> > +		elif [ -f "$g/.dotest-merge/interactive" ]
> [...]
> > +		elif [ -d "$g/.dotest-merge" ]
> 
> Hmmm... is it time to ressurect "git explain" / "git info" / "git state"
> command idea, as to not need to harcode info about state in scripts,
> putting it in only one place?
Maybe git status could deal with that job.
Mike
Jakub Narebski· Feb 6, 2008, 21:09 UTC · re: Stephen Sinclair · lore

Re: command prompt script for current branch

"Stephen Sinclair" <radarsat1@gmail.com> writes:
> Attached is a script I quickly wrote up yesterday.  Sometimes I've
> found I started working only to realize I was on the wrong branch.
> (Now that I'm accustomed to working with git, I make many small
> branches, so it happens.)

First, it is much better to put such script inline, in the body of your email. This makes commenting about script body much easier. And if you have to attach it, for example because your mailer mangles whitespace (which is not the case here), make sure that attachement uses "text/plain", and if possible "inline" disposition, to better _view_ the code without need to save it to temporary file.

I would take an exeption and comment on your code, even though you make it hard to do so.

Second, there are numerous examples on how to create git-aware prompt. Those include code in contrib/completion/git-completion.bash in git sources:

# 4) Consider changing your PS1 to also show the current branch: # PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' # # The argument to __git_ps1 will be displayed only if you # are currently in a git repository. The %s token will be # the name of the current branch.

and the _git_ps1 file in sources of "Git In Nutshell", with git repository at http://git.jonas.iki.fi/git_guides.git; this code by yours truly is heavily commented to make it easier to understand. I'm not sure if it is included in "Git In Nutshell" (http://jonas.iki.fi/git_guides).

I have put the _git_ps1 code at the end of this post.
Show 6 quoted lines
> So I made this small script to simply write the name of the currently
> checked out branch, as well as information on how many commits it
> differs from the remote tracking branch, if any.
> It also appends an exclamation mark if I have uncommitted changes.  I
> stuck it in my PS1 environment variable, so that it would be part of
> my command prompt.

Third, the information on how many commits it differes from coupled remote tracking branch is not that fast to get; additionally you don't always _have_ coupled remote tracking branch. For me the number of commits current branch differs from origin repo, and "dirtiness" of branch are not important, while working with detached HEAD and marking branches which are StGit controlled is.

Show 7 quoted lines
> -- >8 --
> #!/bin/bash
>
> # exit if not a git repo
> if ! (git branch >/dev/null 2>&1); then
>     exit
> fi

There are much better ways to do this; it is usually much better to use plumbing (engine) commands in scripts, not porcelain, which is meant for user interaction. See code in _git_ps1

Besides, why do you run git-branch _twice_?
 
> BRANCH=$(git branch | grep \* | sed s,\*\ ,, )
There is much better way to do this with plumbing.
> REMOTE=$(git config --list | grep branch.$BRANCH.remote 2>/dev/null | cut -f2 -d=)
> MERGE=$(git config --list | grep branch.$BRANCH.merge 2>/dev/null | sed s,^.*=refs/heads/,,)

Why do you use "git config --list" then grep for key, instead of asking git-config to return results for given key with "git config --get" or "git config --get-all"?

By the way, when you use grep you should quote '.', as '.' means any character in regular expression. You do not need put regular expression (parameter to grep) in double quotes _only_ because refnames (and branchnames) cannot contain spaces.

Your code wouldn't be able to deal with being on detached HEAD.
> # exit if no branch found
> if [ "${BRANCH}"x == x ]; then
>     exit
> fi

This is mixture of coding for portability with bash-isms; either use 'test' instead of '[', or use '-z' operator.

Show 6 quoted lines
> echo -n ' '[$BRANCH
>
> # dirty state
> if [ "$(git-diff-index --name-only HEAD)"x != x ]; then
>     echo -n \!
> fi

Err... if you want to check if there are differences, it would be better to use '--quiet' (which implies '--exit-code') and check for exit code instead of checking if there was any output.

BTW. there is slight inconsistency in using dashed (git-diff-index)
and dashless (git branch) of diff commands.  In "external" script
which does not source git shell script "library", it is better to use
dashless form.
 
Show 26 quoted lines
> # exit if no remote tracking branch found
> if [ ${REMOTE}x == x -a ${MERGE}x == x ]; then
>     echo ]
>     exit
> fi
> 
> # Uncomment this line to ignore remote tracking branches
> # echo ]; exit
> 
> # calc number of revs between remote and local
> FWDREVS=$(git rev-list $REMOTE/$MERGE..$BRANCH 2>/dev/null 2>&1 | wc -l)
> 
> # and the other way, in case it is not up to date
> BACKREVS=$(git rev-list $BRANCH..$REMOTE/$MERGE 2>/dev/null 2>&1 | wc -l)
> 
> echo -n ' '-\> $REMOTE/$MERGE
> 
> if [ $FWDREVS -gt 0 ]; then
>     echo -n +$FWDREVS
> fi
> 
> if [ $BACKREVS -gt 0 ]; then
>     echo -n -$BACKREVS
> fi
> 
> echo ]

Why do you use sequence of "echo -n", instead of setting variables, perhaps with the help of some more advanced constructs like in example below, then using one "echo" to output it all?

-- >8 --
function _git_ps1()
{
	# 'git_dir' is absolute path to git repository
	# 'rel' is path relative to top dir in repository
	# 'br' is current branch name, or 'HEAD' if we are on detached HEAD
	local git_dir rel br
	# first call to git-rev-parse also checks if we are inside git
	# repository; if we are not in git repository, use default prompt,
	# provided as an argument
	rel=$(git rev-parse --show-prefix 2>/dev/null) || \
		{ echo "$@" ; return; }
	rel=${rel%\/}
	# get branch name, strip 'refs/heads/' prefix,
	# and use 'HEAD' for detached HEAD (no branch name)
	br=$(git symbolic-ref HEAD 2>/dev/null)
	br=${br#refs/heads/}
	br=${br:-HEAD}
	# path to top dir of git repository
	loc=${PWD%/$rel}
	# the following code is important only if you use StGit,
	# to note if you are on StGit controlled branch;
	# use second part of conditional if you don't use StGit
	git_dir=$(git rev-parse --git-dir)
	if [ "$br" -a -e "$git_dir/patches/$br" ]; then
		echo "$br:${loc/*\/}${rel:+/$rel}# "
	else 
		echo "$br:${loc/*\/}${rel:+/$rel}> "
	fi
}

## bash: ## set PS1 only if we are in interactive shell (PS1 is set) # [ "$PS1" ] && PS1='$(_git_ps1 "\u@\h:\w> ")'

## zsh: # function precmd() { # PS1=`_git_ps1 '%m:%~%# '` # } # vim:filetype=sh

 
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Stephen Sinclair· Feb 6, 2008, 22:13 UTC · re: Jakub Narebski · lore

Re: command prompt script for current branch

Show 14 quoted lines
> > Attached is a script I quickly wrote up yesterday.  Sometimes I've
> > found I started working only to realize I was on the wrong branch.
> > (Now that I'm accustomed to working with git, I make many small
> > branches, so it happens.)
>
> First, it is much better to put such script inline, in the body of
> your email.  This makes commenting about script body much easier.
> And if you have to attach it, for example because your mailer mangles
> whitespace (which is not the case here), make sure that attachement
> uses "text/plain", and if possible "inline" disposition, to better
> _view_ the code without need to save it to temporary file.
>
> I would take an exeption and comment on your code, even though you
> make it hard to do so.

Thank you. Since there's already a solution for this in contrib, (didn't see it, sorry), I'll just take your recommendations on coding style for any future stuff I do.

Steve
Jakub Narebski· Feb 6, 2008, 22:56 UTC · re: Stephen Sinclair · lore

Re: command prompt script for current branch

On Wed, 6 Feb 2008, Stephen Sinclair wrote:

Please provide attribution (who write what) when quoting, like below:

> Jakub Narebski wrote:
>> "Stephen Sinclair" <radarsat1@gmail.com> writes:
Show 18 quoted lines
>>> Attached is a script I quickly wrote up yesterday.  Sometimes I've
>>> found I started working only to realize I was on the wrong branch.
>>> (Now that I'm accustomed to working with git, I make many small
>>> branches, so it happens.)
>>
>> First, it is much better to put such script inline, in the body of
>> your email.  This makes commenting about script body much easier.
>> And if you have to attach it, for example because your mailer mangles
>> whitespace (which is not the case here), make sure that attachement
>> uses "text/plain", and if possible "inline" disposition, to better
>> _view_ the code without need to save it to temporary file.
>>
>> I would take an exeption and comment on your code, even though you
>> make it hard to do so.
> 
> Thank you.  Since there's already a solution for this in contrib,
> (didn't see it, sorry), I'll just take your recommendations on coding
> style for any future stuff I do.

First, the code in contrib doesn't do all your code did. It doesn't show if workarea is "dirty" (although I'm not sure if this is very useful), nor does it show number of commits since or to mainline (origin) version, although with working on (with) feature/topic branches which does not have corresponding remote tracking branch it seems not useful.

On the other hand code in _git_ps1 (taken from one of the "git prompt" posts floating on git mailing list, and available in archives, together with /usr/share/stgit/contrib/stgbashprompt.sh from StGit, Quilt-like patch management layer on top of Git) shows also name of repository and path in repository, and also if the branch is managed by StGit.

Second, I think you have learned something. Both on rules regarding discussion, netiquette and sending contributions on this mailing list, and about how to write scripts for/around git. (At least I hope so).

-- 
Jakub Narebski
Poland

← back to recent threads