Skip to content

Commit ef1e110

Browse files
committed
Display remote branch name if we are on a remote branch commit
1 parent f3e13a4 commit ef1e110

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

prompt.sh

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ find_git_branch() {
77
local branch
88
local upstream
99
local is_branch
10+
local git_dir
1011
local special_state
1112
if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
1213
if [[ "$branch" == "HEAD" ]]; then
@@ -18,18 +19,23 @@ find_git_branch() {
1819
if tag=$(git describe --exact-match >&1 2> /dev/null); then
1920
branch="+$tag"
2021
else
21-
#branch='<detached>'
22-
# Or show the short hash
23-
branch='#'$(git rev-parse --short HEAD 2> /dev/null)
24-
# Or the long hash, with no leading '#'
25-
#branch=$(git rev-parse HEAD 2> /dev/null)
22+
# If it a remote branch, show that
23+
branch=$(git name-rev --name-only HEAD | sed 's+^remotes/++')
24+
# But name-rev will also return if it is a few steps back from a remote branch, which sucks, so don't display that
25+
if [[ "$branch" == "undefined" ]] || grep '\~' <<< "$branch" >/dev/null; then
26+
#branch='<detached>'
27+
# Or show the short hash
28+
branch='#'$(git rev-parse --short HEAD 2> /dev/null)
29+
# Or the long hash, with no leading '#'
30+
#branch=$(git rev-parse HEAD 2> /dev/null)
31+
fi
2632
fi
2733
else
28-
# This is a named branch. (It might be local or remote.)
29-
upstream=$(git rev-parse '@{upstream}' 2> /dev/null)
34+
# This is a named branch
3035
is_branch=true
36+
upstream=$(git rev-parse '@{upstream}' 2> /dev/null)
3137
fi
32-
local git_dir="$(git rev-parse --show-toplevel)/.git"
38+
git_dir="$(git rev-parse --show-toplevel)/.git"
3339
if [[ -d "$git_dir/rebase-merge" ]] || [[ -d "$git_dir/rebase-apply" ]]; then
3440
special_state=rebase
3541
elif [[ -f "$git_dir/MERGE_HEAD" ]]; then

0 commit comments

Comments
 (0)