Skip to contentSkip to conversation

Bash snippet to show branch and patch in bash prompt

PATCH In review Robin Rosenberg 4 participants 11 messages Aug 11, 2016

lore
Share & tools
lore
#2 lore raw
In reply to Eran Tromer
Quoted 12 lines · Eran Tromer
Quoted 11 lines · Robin Rosenberg
contrib/stgbashprompt.sh new file +11 -0
+# 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
Quoted 8 lines

contrib/stgbashprompt.sh

Quoted 5 lines
contrib/stgbashprompt.sh new file +5 -0
+	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)

#3 lore raw

PATCH 1/1 Bash snippet to show branch and patch in bash prompt

In reply to Eran Tromer

From: Robin Rosenberg <[email protected]>

---

 contrib/stgbashprompt.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
contrib/stgbashprompt.sh new file +15 -0
new file mode 100755
index 0000000..792da53
@@ -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\$ '
+
#5 lore raw
Quoted 20 lines · Robin Rosenberg

From: Robin Rosenberg <[email protected]>

---

 contrib/stgbashprompt.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
contrib/stgbashprompt.sh new file +6 -0
new file mode 100755
index 0000000..792da53
@@ -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
+    {

contrib/stgbashprompt.sh line 16

Quoted 10 lines
contrib/stgbashprompt.sh new file +10 -0
+	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

Isn't this the same patch? "stg refresh" :-)?

Signature · 3 lines
Catalin

P.S. could you please send them to my [email protected]
#1 lore raw
In reply to Robin Rosenberg
Quoted 6 lines · Robin Rosenberg
contrib/stgbashprompt.sh new file +6 -0
+# 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
+    {

contrib/stgbashprompt.sh

Quoted 10 lines
contrib/stgbashprompt.sh new file +10 -0
+	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)]\$ '
----------------------------------------------

#0 lore raw

PATCH 1/1 Bash snippet to show branch and patch in bash prompt

From: Robin Rosenberg <[email protected]>

---

 contrib/stgbashprompt.sh |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
contrib/stgbashprompt.sh new file +15 -0
new file mode 100755
index 0000000..792da53
@@ -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\$ '
+
#7 lore raw

PATCH 1/1 Bash snippet to show branch and patch in bash prompt

In reply to Eran Tromer

From: Robin Rosenberg <[email protected]>

---

 contrib/stgbashprompt.sh |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
contrib/stgbashprompt.sh new file +17 -0
new file mode 100755
index 0000000..a79561e
@@ -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\$ '
+
#8 lore raw

contrib/stgbashprompt.sh line 8

Quoted 6 lines · Robin Rosenberg

From: Robin Rosenberg <[email protected]>

contrib/stgbashprompt.sh new file +4 -0
+    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

contrib/stgbashprompt.sh line 9

Quoted 1 line
contrib/stgbashprompt.sh new file +1 -0
+	br=${ref/refs\/heads\//}

You want to strip a prefix only, so this is safer:

	br=${ref#refs/heads/}

contrib/stgbashprompt.sh line 10

Quoted 1 line
contrib/stgbashprompt.sh new file +1 -0
+	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.

contrib/stgbashprompt.sh line 12

Quoted 2 lines
contrib/stgbashprompt.sh new file +2 -0
+	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.

#10 lore raw
In reply to Eran Tromer

måndag 30 oktober 2006 10:57 skrev Eran Tromer:

Quoted 2 lines

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.

Quoted 13 lines

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"
}
Quoted 2 lines

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.