bash prompt: use bash builtins to find out current branch

__git_ps1() runs the '$(git symbolic-ref HEAD)' command substitution
to find out whether we are on a branch and to find out the name of
that branch.  This imposes the overhead of fork()ing a subshell and
fork()+exec()ing a git process.

Since HEAD is in most cases a single-line file and the symbolic ref
format is quite simple to recognize and parse, read and parse it using
only bash builtins, thereby sparing all that fork()+exec() overhead.
Don't display the git prompt if reading HEAD fails, because a readable
HEAD is required for a git repository.  HEAD can also be a symlink
symbolic ref (due to 'core.preferSymlinkRefs'), so use bash builtins
for reading HEAD only when HEAD is not a symlink.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
This commit is contained in:
SZEDER Gábor 2011-03-31 23:41:18 +02:00
Родитель b91b935f04
Коммит 3a43c4b5bd
1 изменённых файлов: 33 добавлений и 18 удалений

Просмотреть файл

@ -355,25 +355,40 @@ __git_ps1 ()
r="|BISECTING" r="|BISECTING"
fi fi
test -n "$b" || if [ -n "$b" ]; then
b="$(git symbolic-ref HEAD 2>/dev/null)" || { :
detached=yes elif [ -h "$g/HEAD" ]; then
b="$( # symlink symbolic ref
case "${GIT_PS1_DESCRIBE_STYLE-}" in b="$(git symbolic-ref HEAD 2>/dev/null)"
(contains) else
git describe --contains HEAD ;; local head=""
(branch) if ! read head 2>/dev/null <"$g/HEAD"; then
git describe --contains --all HEAD ;; if [ $pcmode = yes ]; then
(describe) PS1="$ps1pc_start$ps1pc_end"
git describe HEAD ;; fi
(* | default) return
git describe --tags --exact-match HEAD ;; fi
esac 2>/dev/null)" || # is it a symbolic ref?
b="${head#ref: }"
if [ "$head" = "$b" ]; then
detached=yes
b="$(
case "${GIT_PS1_DESCRIBE_STYLE-}" in
(contains)
git describe --contains HEAD ;;
(branch)
git describe --contains --all HEAD ;;
(describe)
git describe HEAD ;;
(* | default)
git describe --tags --exact-match HEAD ;;
esac 2>/dev/null)" ||
b="$(git rev-parse --short HEAD 2>/dev/null)..." || b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
b="unknown" b="unknown"
b="($b)" b="($b)"
} fi
fi
fi fi
if [ -n "$step" ] && [ -n "$total" ]; then if [ -n "$step" ] && [ -n "$total" ]; then