git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
ETEran Tromer <git2eran@tromer.org>
Date
Oct 30, 2006, 15:03 UTC
Message-ID
<45461429.4020108@tromer.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.

Previous: Robin RosenbergNext: Catalin Marinas
Message 6 of 7 in “Bash snippet to show branch and patch in bash prompt”
  1. Bash snippet to show branch and patch in bash promptRobin Rosenberg, Oct 29, 2006
  2. Eran TromerOct 30, 2006
  3. Catalin MarinasOct 30, 2006
  4. Robin RosenbergOct 30, 2006
  5. Bash snippet to show branch and patch in bash promptRobin Rosenberg, Oct 30, 2006
  6. Eran TromerOct 30, 2006
  7. Catalin MarinasNov 2, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.