Thread (8 messages) flat view 8 messages, 3 authors, 2016-08-11

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

From: Eran Tromer <hidden>
Date: 2016-08-11 20:42:51

On 2006-10-30 12:59, Robin Rosenberg wrote:
From: Robin Rosenberg <redacted>
+    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.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help