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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

> +    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.

  Eran
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]