Skip to content

Commit e8f21ca

Browse files
committed
bash prompt: print unique detached HEAD abbreviated object name
When describing a detached HEAD according to the $GIT_PS1_DESCRIBE environment variable fails, __git_ps1() runs 'cut -c1-7 .git/HEAD' to show the 7 hexdigits abbreviated commit object name in the prompt. Obviously, this neither respects core.abbrev nor produces a unique object name. Fix this by using 'git rev-parse --short HEAD' instead and adjust the corresponding test to use non-standard number of hexdigits. Signed-off-by: SZEDER Gábor <[email protected]>
1 parent 868dc1a commit e8f21ca

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

contrib/completion/git-prompt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ __git_ps1 ()
392392
git describe --tags --exact-match HEAD ;;
393393
esac 2>/dev/null)" ||
394394

395-
b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
395+
b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
396396
b="unknown"
397397
b="($b)"
398398
}

t/t9903-bash-prompt.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
5050
'
5151

5252
test_expect_success 'prompt - detached head' '
53-
printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
53+
printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
54+
test_config core.abbrev 13 &&
5455
git checkout b1^ &&
5556
test_when_finished "git checkout master" &&
5657
__git_ps1 >"$actual" &&

0 commit comments

Comments
 (0)