Skip to content

Commit ff790b6

Browse files
committed
completion: simplify "current branch" in __git_ps1()
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 <[email protected]>
1 parent 8763dbb commit ff790b6

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

contrib/completion/git-completion.bash

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ __git_ps1 ()
106106
if [ -f "$g/BISECT_LOG" ]; then
107107
r="|BISECTING"
108108
fi
109-
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
110-
if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
111-
if [ -r "$g/HEAD" ]; then
112-
b="$(cut -c1-7 "$g/HEAD")..."
113-
fi
114-
fi
115-
fi
109+
110+
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
111+
b="$(git describe --exact-match HEAD 2>/dev/null)" ||
112+
b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
113+
b="unknown"
114+
115+
b="($b)"
116+
}
116117
fi
117118

118119
local w

0 commit comments

Comments
 (0)