# Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt

7 messages from 2006-10-29 to 2006-11-02. Participants: Catalin Marinas, Eran Tromer, Robin Rosenberg.
Thread: https://gitlist.dev/t/43379

## Robin Rosenberg, 2006-10-29 23:37

Subject: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <20061029233745.24899.1470.stgit@lathund.dewire.com>
URL: https://gitlist.dev/e/20061029233745.24899.1470.stgit%40lathund.dewire.com

```
From: Robin Rosenberg <robin.rosenberg@dewire.com>

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---

 contrib/stgbashprompt.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/contrib/stgbashprompt.sh b/contrib/stgbashprompt.sh
new file mode 100755
index 0000000..792da53
--- /dev/null
+++ b/contrib/stgbashprompt.sh
@@ -0,0 +1,16 @@
+# include this in your bashrc or copy to /etc/bash_completions.d
+
+if [ "$PS1" ]; then
+    # trap 'PS1="\u@\h [$(stg top)] \w]\$ "' DEBUG
+    function stgtag
+    {
+	br=$(stg branch 2>/dev/null)
+	top=$(stg top 2>/dev/null)
+	if [[ -n "$br$top" ]];then
+	    echo "[$top@$br]"
+	    return
+	fi
+    }
+    PS1='\u@\h$(stgtag)\w\$ '
+

```

## Eran Tromer, 2006-10-30 09:57

Subject: Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <4545CC6F.90001@tromer.org>
URL: https://gitlist.dev/e/4545CC6F.90001%40tromer.org
In-Reply-To: <20061029233745.24899.1470.stgit@lathund.dewire.com>

```
On 2006-10-30 01:37, Robin Rosenberg wrote:
> +# include this in your bashrc or copy to /etc/bash_completions.d
> +
> +if [ "$PS1" ]; then
> +    # trap 'PS1="\u@\h [$(stg top)] \w]\$ "' DEBUG
> +    function stgtag
> +    {
> +	br=$(stg branch 2>/dev/null)
> +	top=$(stg top 2>/dev/null)
> +	if [[ -n "$br$top" ]];then
> +	    echo "[$top@$br]"
> +	    return
> +	fi
> +    }
> +    PS1='\u@\h$(stgtag)\w\$ '
> +
> +fi

That's an annoying 430ms delay at every prompt, on my box. Does StGIT do
something expensive on every invocation?

Ben Clifford'd solution is pretty much instantaneous, and the following
extends it to StGIT (in a less clean but much faster way):

----------------------------------------------
__prompt_githead() {
    __PS_GIT="$(git-symbolic-ref HEAD 2>/dev/null)" || exit
    __PS_GIT="$(basename $__PS_GIT)"
    echo -n " $__PS_GIT"
    __PS_GIT=$(cat "${GIT_DIR:-.git}/patches/$__PS_GIT/current" \
               2>/dev/null) || exit
    echo -n ":$__PS_GIT"
}
PS1='[\u@\h \W$(__prompt_githead)]\$ '
----------------------------------------------


```

## Catalin Marinas, 2006-10-30 10:24

Subject: Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <tnxlkmy2j9p.fsf@arm.com>
URL: https://gitlist.dev/e/tnxlkmy2j9p.fsf%40arm.com
In-Reply-To: <4545CC6F.90001@tromer.org>

```
Eran Tromer <git2eran@tromer.org> wrote:
> On 2006-10-30 01:37, Robin Rosenberg wrote:
>> +# include this in your bashrc or copy to /etc/bash_completions.d
>> +
>> +if [ "$PS1" ]; then
>> +    # trap 'PS1="\u@\h [$(stg top)] \w]\$ "' DEBUG
>> +    function stgtag
>> +    {
>> +	br=$(stg branch 2>/dev/null)
>> +	top=$(stg top 2>/dev/null)
>> +	if [[ -n "$br$top" ]];then
>> +	    echo "[$top@$br]"
>> +	    return
>> +	fi
>> +    }
>> +    PS1='\u@\h$(stgtag)\w\$ '
>> +
>> +fi
>
> That's an annoying 430ms delay at every prompt, on my box. Does StGIT do
> something expensive on every invocation?

Well, there are some forks. For every "stg" command, "git-symbolic-ref
HEAD" and "git-rev-parse --git-dir" are invoked to get the name of the
main branch and the .git directory. There is also the delay of
invoking python and loading the command modules in main.py (maybe I
should modify this to import the modules on demand, based on what
command was given).

Since the repository format is stable, you could use something like
this (it should be faster):

git_dir=$(git-rev-parse --git-dir 2> /dev/null)
ref=$(git-symbolic-ref HEAD 2> /dev/null)
br=${ref##*/}
top=$(cat $git_dir/patches/$br/current)

-- 

```

## Robin Rosenberg, 2006-10-30 10:32

Subject: Re: [StGIT PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <200610301132.45086.robin.rosenberg@dewire.com>
URL: https://gitlist.dev/e/200610301132.45086.robin.rosenberg%40dewire.com
In-Reply-To: <4545CC6F.90001@tromer.org>

```
måndag 30 oktober 2006 10:57 skrev Eran Tromer:
> That's an annoying 430ms delay at every prompt, on my box. Does StGIT do
> something expensive on every invocation?

I don't type fast enough to notice really and my machine seems faster, ~300 ms 
per prompt.

>
> Ben Clifford'd solution is pretty much instantaneous, and the following
> extends it to StGIT (in a less clean but much faster way):
>
> ----------------------------------------------
> __prompt_githead() {
>     __PS_GIT="$(git-symbolic-ref HEAD 2>/dev/null)" || exit
>     __PS_GIT="$(basename $__PS_GIT)"
>     echo -n " $__PS_GIT"
>     __PS_GIT=$(cat "${GIT_DIR:-.git}/patches/$__PS_GIT/current" \
>                2>/dev/null) || exit
>     echo -n ":$__PS_GIT"
> }
> PS1='[\u@\h \W$(__prompt_githead)]\$ '
> ----------------------------------------------

This doesn't work if the branch have a / in the name or if you are in a 
subdirectory, not the top level. Probably not hard to fix though.


```

## Robin Rosenberg, 2006-10-30 10:59

Subject: [PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <20061030105926.14328.55180.stgit@lathund.dewire.com>
URL: https://gitlist.dev/e/20061030105926.14328.55180.stgit%40lathund.dewire.com
In-Reply-To: <4545CC6F.90001@tromer.org>

```
From: Robin Rosenberg <robin.rosenberg@dewire.com>

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---

 contrib/stgbashprompt.sh |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/contrib/stgbashprompt.sh b/contrib/stgbashprompt.sh
new file mode 100755
index 0000000..a79561e
--- /dev/null
+++ b/contrib/stgbashprompt.sh
@@ -0,0 +1,18 @@
+# include this in your bashrc or copy to /etc/bash_completions.d
+
+if [ "$PS1" ]; then
+    # trap 'PS1="\u@\h [$(stg top)] \w]\$ "' DEBUG
+    function stgtag
+    {
+	git_dir=$(git-rev-parse --git-dir 2> /dev/null)
+	ref=$(git-symbolic-ref HEAD 2> /dev/null)
+	br=${ref/refs\/heads\//}
+	top=$(cat $git_dir/patches/$br/current 2>/dev/null)
+	if [[ -n "$br$top" ]];then
+	    echo "[$top@$br]"
+	    return
+	fi
+    }
+    PS1='\u@\h$(stgtag)\w\$ '
+

```

## Eran Tromer, 2006-10-30 15:03

Subject: Re: [PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <45461429.4020108@tromer.org>
URL: https://gitlist.dev/e/45461429.4020108%40tromer.org
In-Reply-To: <20061030105926.14328.55180.stgit@lathund.dewire.com>

```
On 2006-10-30 12:59, Robin Rosenberg wrote:
> From: Robin Rosenberg <robin.rosenberg@dewire.com>

> +    function stgtag
> +    {
> +	git_dir=$(git-rev-parse --git-dir 2> /dev/null)
> +	ref=$(git-symbolic-ref HEAD 2> /dev/null)

Abort early if we're not in a git repo:
	git_dir=$(git-rev-parse --git-dir 2> /dev/null) || return
	ref=$(git-symbolic-ref HEAD 2> /dev/null) || return


> +	br=${ref/refs\/heads\//}

You want to strip a prefix only, so this is safer:
	br=${ref#refs/heads/}


> +	top=$(cat $git_dir/patches/$br/current 2>/dev/null)

All variables should be declared local to avoid polluting the bash
variable namespace. Likewise, the function name deserves a couple of
underscores.


> +	if [[ -n "$br$top" ]];then
> +	    echo "[$top@$br]"

It seems better to put the StGIT top after the tag, so that stg push/pop
shifts less of the prompt, making it easier to see the change visually.


Corresponding modified version:

-------------------------------------------
if [ "$PS1" ]; then
    function __prompt_git()
    {
        local git_dir ref br top;
	git_dir=$(git-rev-parse --git-dir 2> /dev/null) || return
	ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
	br=${ref#refs/heads/}
	top=$(cat $git_dir/patches/$br/current 2>/dev/null) \
		&& top="#$top"
	echo "[$br$top]"
    }
    PS1='\u@\h$(__prompt_git)\w\$ '
fi
-------------------------------------------

Conditionally prepending the "#" to $top can be done more concisely via
${top:+#$top} but I used the more readable version.


```

## Catalin Marinas, 2006-11-02 11:24

Subject: Re: [PATCH] Bash snippet to show branch and patch in bash prompt
Message-ID: <tnxd5862ist.fsf@arm.com>
URL: https://gitlist.dev/e/tnxd5862ist.fsf%40arm.com
In-Reply-To: <45461429.4020108@tromer.org>

```
Eran Tromer <git2eran@tromer.org> wrote:
> Corresponding modified version:

Thanks for your comments. I included them in the patch.

-- 

```
