# [PATCH 2/2] completion: enhance "current branch" display

5 messages from 2009-05-10 to 2009-05-10. Participants: Junio C Hamano, Johannes Sixt, Michal Nazarewicz, Shawn O. Pearce.
Thread: https://gitlist.dev/t/19268

## Junio C Hamano, 2009-05-10 08:56

Subject: [PATCH 2/2] completion: enhance "current branch" display
Message-ID: <7vvdo91glm.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vvdo91glm.fsf%40alter.siamese.dyndns.org

```
Introduce GIT_PS1_DESCRIBE option you can set to "contains", "branch", or
"describe" to tweak the way how a detached HEAD is described.

The default behaviour is to describe only exact match with some tag
(otherwise use the first 7 hexdigits) as before.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * I do not think the new modes based on name-rev (aka "--contains") are
   much useful if you do your own development while on a detached HEAD,
   but they probably are useful for tourists who sightsee.

 contrib/completion/git-completion.bash |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ccc7e0d..2490d5f 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -108,10 +108,21 @@ __git_ps1 ()
 			fi
 
 			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
-				b="$(git describe --exact-match HEAD 2>/dev/null)" ||
+
+				b="$(
+				case "${GIT_PS1_DESCRIBE_STYLE-}" in
+				(contains)
+					git describe --contains HEAD ;;
+				(branch)
+					git describe --contains --all HEAD ;;
+				(describe)
+					git describe HEAD ;;
+				(* | default)
+					git describe --exact-match HEAD ;;
+				esac 2>/dev/null)" ||
+
 				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
 				b="unknown"
-
 				b="($b)"
 			}
 		fi
-- 
1.6.3.9.g6345d

```

## Johannes Sixt, 2009-05-10 10:59

Subject: Re: [PATCH 2/2] completion: enhance "current branch" display
Message-ID: <200905101259.07175.j6t@kdbg.org>
URL: https://gitlist.dev/e/200905101259.07175.j6t%40kdbg.org
In-Reply-To: <7vvdo91glm.fsf@alter.siamese.dyndns.org>

```
On Sonntag, 10. Mai 2009, Junio C Hamano wrote:
>  				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||

While you are here, you could turn this line into

		{ b=$(< "$g/HEAD") && b=${b:0:7}...; } 2>/dev/null ||

to save a process. $(< foo) is a bash feature and does the same as $(cat foo), 
but faster.

-- Hannes

```

## Michal Nazarewicz, 2009-05-10 11:33

Subject: Re: [PATCH 2/2] completion: enhance "current branch" display
Message-ID: <87my9lnqet.fsf@erwin.mina86.com>
URL: https://gitlist.dev/e/87my9lnqet.fsf%40erwin.mina86.com
In-Reply-To: <200905101259.07175.j6t@kdbg.org>

```
Johannes Sixt <j6t@kdbg.org> writes:

> On Sonntag, 10. Mai 2009, Junio C Hamano wrote:
>>  				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
>
> While you are here, you could turn this line into
>
> 		{ b=$(< "$g/HEAD") && b=${b:0:7}...; } 2>/dev/null ||
>
> to save a process. $(< foo) is a bash feature and does the same as
> $(cat foo), but faster.

Excuse me this little cavil but how about something that works on other
shells as well:

{ read b <$g/HEAD && _b=${b#???????} && b=${b%"$_b"}... && unset _b }

-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--

```

## Johannes Sixt, 2009-05-10 11:57

Subject: Re: [PATCH 2/2] completion: enhance "current branch" display
Message-ID: <200905101357.01959.j6t@kdbg.org>
URL: https://gitlist.dev/e/200905101357.01959.j6t%40kdbg.org
In-Reply-To: <87my9lnqet.fsf@erwin.mina86.com>

```
On Sonntag, 10. Mai 2009, Michal Nazarewicz wrote:
> Johannes Sixt <j6t@kdbg.org> writes:
> > On Sonntag, 10. Mai 2009, Junio C Hamano wrote:
> >>  				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
> >
> > While you are here, you could turn this line into
> >
> > 		{ b=$(< "$g/HEAD") && b=${b:0:7}...; } 2>/dev/null ||
> >
> > to save a process. $(< foo) is a bash feature and does the same as
> > $(cat foo), but faster.
>
> Excuse me this little cavil but how about something that works on other
> shells as well:
>
> { read b <$g/HEAD && _b=${b#???????} && b=${b%"$_b"}... && unset _b }

Because this is all about *bash* completion ;-)

-- Hannes

```

## Shawn O. Pearce, 2009-05-10 21:18

Subject: Re: [PATCH 2/2] completion: enhance "current branch" display
Message-ID: <20090510211818.GA30527@spearce.org>
URL: https://gitlist.dev/e/20090510211818.GA30527%40spearce.org
In-Reply-To: <7vvdo91glm.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> wrote:
> Introduce GIT_PS1_DESCRIBE option you can set to "contains", "branch", or
> "describe" to tweak the way how a detached HEAD is described.
> 
> The default behaviour is to describe only exact match with some tag
> (otherwise use the first 7 hexdigits) as before.
> 
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> 
>  * I do not think the new modes based on name-rev (aka "--contains") are
>    much useful if you do your own development while on a detached HEAD,
>    but they probably are useful for tourists who sightsee.

Yea, both patches are sensible.

IIRC the desire for name-rev was for detached HEAD in contexts
like what git submodule produce.  If you are sitting on a tagged
version in a submodule, its nice to see that in your prompt.
Since you aren't developing there right now, there is no branch,
and you are just sightseeing.

This feature may have started at my prior job, where we had our
own git submodule like tool integrated with our build system,
that predated git submodule.  But its also still really useful
with say the Android Open Source Project, where users have 150 or
so git repositories, all on detached HEADs, most pointing at an
annotated tag.

-- 
Shawn.

```
