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

[PATCH 1/2] completion: simplify "current branch" in __git_ps1()

From
Junio C Hamano <gitster@pobox.com>
Date
May 10, 2009, 08:53 UTC
Message-ID
<7v1vqx2vb4.fsf@alter.siamese.dyndns.org>

As I very often work on a detached HEAD, I found it pretty confusing when __git_ps1() said 'some-name'. Did I create a branch with that name by mistake, or do I happen to be on a commit with that exact tag?

This patch fixes the issue by enclosing non branch names in a pair of parentheses when used to substitute %s token in __git_ps1() argument.

It also fixes a small bug where the branch part is left empty when .git/HEAD is unreadable for whatever reason. The output now says "(unknown)".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 contrib/completion/git-completion.bash |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 1683e6d..ccc7e0d 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -106,13 +106,14 @@ __git_ps1 ()
 			if [ -f "$g/BISECT_LOG" ]; then
 				r="|BISECTING"
 			fi
-			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
-				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
-					if [ -r "$g/HEAD" ]; then
-						b="$(cut -c1-7 "$g/HEAD")..."
-					fi
-				fi
-			fi
+
+			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
+				b="$(git describe --exact-match HEAD 2>/dev/null)" ||
+				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
+				b="unknown"
+
+				b="($b)"
+			}
 		fi
 
 		local w
-- 
1.6.3.9.g6345d
Message 1 of 1 in “completion: simplify "current branch" in __git_ps1()”
  1. 1/2 completion: simplify "current branch" in __git_ps1()Junio C Hamano, May 10, 2009

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.