Skip to content

Commit e3e0b93

Browse files
committed
bash prompt: combine 'git rev-parse' for detached head
When describing a detached HEAD according to the $GIT_PS1_DESCRIBE environment variable fails, __git_ps1() now runs the '$(git rev-parse --short HEAD)' command substitution to get the abbreviated detached HEAD commit object name. This imposes the overhead of fork()ing a subshell and fork()+exec()ing a git process. Avoid this overhead by combining this command substitution with the "main" 'git rev-parse' execution for getting the path to the .git directory & co. This means that we'll look for the abbreviated commit object name even when it's not necessary, because we're on a branch or the detached HEAD can be described. It doesn't matter, however, because once 'git rev-parse' is up and running to fulfill all those other queries, the additional overhead of looking for the abbreviated commit object name is not measurable because it's lost in the noise. There is a caveat, however, when we are on an unborn branch, because in that case HEAD doesn't point to a valid commit, hence the query for the abbreviated commit object name fails. Therefore, '--short HEAD' must be the last options to 'git rev-parse' in order to get all the other necessary information for the prompt even on an unborn branch. Furthermore, in that case, and in that case only, 'git rev-parse' doesn't output the last line containing the abbreviated commit object name, obviously, so we have to take care to only parse it if 'git rev-parse' exited without any error. Although there are tests already excercising __git_ps1() on unborn branches, they all do so implicitly. Add a test that checks this explicitly. Signed-off-by: SZEDER Gábor <[email protected]>
1 parent efaa0c1 commit e3e0b93

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

contrib/completion/git-prompt.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,12 @@ __git_ps1 ()
311311
;;
312312
esac
313313

314-
local repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
315-
--is-bare-repository --is-inside-work-tree 2>/dev/null)"
314+
local repo_info rev_parse_exit_code
315+
repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
316+
--is-bare-repository --is-inside-work-tree \
317+
--short HEAD 2>/dev/null)"
318+
rev_parse_exit_code="$?"
319+
316320
if [ -z "$repo_info" ]; then
317321
if [ $pcmode = yes ]; then
318322
#In PC mode PS1 always needs to be set
@@ -321,6 +325,11 @@ __git_ps1 ()
321325
return
322326
fi
323327

328+
local short_sha
329+
if [ "$rev_parse_exit_code" = "0" ]; then
330+
short_sha="${repo_info##*$'\n'}"
331+
repo_info="${repo_info%$'\n'*}"
332+
fi
324333
local inside_worktree="${repo_info##*$'\n'}"
325334
repo_info="${repo_info%$'\n'*}"
326335
local bare_repo="${repo_info##*$'\n'}"
@@ -392,8 +401,7 @@ __git_ps1 ()
392401
git describe --tags --exact-match HEAD ;;
393402
esac 2>/dev/null)" ||
394403

395-
b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
396-
b="unknown"
404+
b="$short_sha..."
397405
b="($b)"
398406
fi
399407
fi

t/t9903-bash-prompt.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
4949
test_cmp expected "$actual"
5050
'
5151

52+
test_expect_success 'prompt - unborn branch' '
53+
printf " (unborn)" >expected &&
54+
git checkout --orphan unborn &&
55+
test_when_finished "git checkout master" &&
56+
__git_ps1 >"$actual" &&
57+
test_cmp expected "$actual"
58+
'
59+
5260
test_expect_success 'prompt - detached head' '
5361
printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
5462
test_config core.abbrev 13 &&

0 commit comments

Comments
 (0)