summaryrefslogtreecommitdiff
path: root/contrib/completion
diff options
context:
space:
mode:
authorSZEDER Gábor <szeder@ira.uka.de>2011-03-31 21:41:18 (GMT)
committerSZEDER Gábor <szeder@ira.uka.de>2013-06-24 15:22:10 (GMT)
commit3a43c4b5bd19528229ef36b28d648d5ac98f15f1 (patch)
treee7674864945a9d030ab0528018538303e4b02a3a /contrib/completion
parentb91b935f04e8dcb1cc9f247627fbd0346ce949f4 (diff)
downloadgit-3a43c4b5bd19528229ef36b28d648d5ac98f15f1.zip
git-3a43c4b5bd19528229ef36b28d648d5ac98f15f1.tar.gz
git-3a43c4b5bd19528229ef36b28d648d5ac98f15f1.tar.bz2
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>
Diffstat (limited to 'contrib/completion')
-rw-r--r--contrib/completion/git-prompt.sh51
1 files changed, 33 insertions, 18 deletions
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index bc402f5..c2050b6 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -355,25 +355,40 @@ __git_ps1 ()
r="|BISECTING"
fi
- test -n "$b" ||
- b="$(git symbolic-ref HEAD 2>/dev/null)" || {
- 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)" ||
+ if [ -n "$b" ]; then
+ :
+ elif [ -h "$g/HEAD" ]; then
+ # symlink symbolic ref
+ b="$(git symbolic-ref HEAD 2>/dev/null)"
+ else
+ local head=""
+ if ! read head 2>/dev/null <"$g/HEAD"; then
+ if [ $pcmode = yes ]; then
+ PS1="$ps1pc_start$ps1pc_end"
+ fi
+ return
+ fi
+ # 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="unknown"
- b="($b)"
- }
+ b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
+ b="unknown"
+ b="($b)"
+ fi
+ fi
fi
if [ -n "$step" ] && [ -n "$total" ]; then