bash: optimizing a git-aware shell prompt

I've written the following PS1, which will append the current working branch to the shell prompt if I am in a git repo, and do nothing otherwise:

PS1='\[\e[00;32m\]\u@\h\[\e[0m\]\[\e[00;33m\] [\w]$(branch=`git symbolic-ref --short -q HEAD 2> /dev/null || git rev-parse -q --short HEAD 2> /dev/null` ; if [ $branch ] ; then echo -n -e " \e[95m<$branch>" ; fi) \[\e[00;33m\]\$\[\e[0m\] '

The issue is that this slows down the prompts, to the point that if I try to rapidly skip several lines, I often don't get a prompt at all.

I'm wondering if this can be optimized to run faster. The only thing I've been able to think of is to create a wrapper around git branch that will print the new working branch to a file in /repo/.git/. Then I could simply call cat /repo/.git/.prompt or something within the PS1 command. I'm not sure this would actually run faster because I'd need some logic to determine if there is a .git directory somewhere in the file tree, which would have it's own issues.

I don't really like this solution anyway, so I'm wondering if there is a faster way to get the current branch name from within a git repo.

NOTE: I'm aware of things like git-aware-prompt, but I have the same issue with it. And I could also use $PROMPT_COMMAND, which does run a bit faster, but it also is only capable of printing the branch name at the beginning of the prompt, which isn't really what I like.

Answers


One of the main benefits of PROMPT_COMMAND is that it allows you to build up the prompt dynamically each time it is displayed.

build_prompt () {
  PS1='\[\e[00;32m\]\u@\h\[\e[0m\]\[\e[00;33m\] [\w]'
  branch=$(git symbolic-ref --short -q HEAD 2> /dev/null ||
           git rev-parse -q --short HEAD 2> /dev/null)
  [[ $branch ]] &&  PS1+=" \e[95m<$branch>" 
  PS1+=' \[\e[00;33m\]\$\[\e[0m\] '
}

PROMPT_COMMAND=build_prompt

Need Your Help

Sending an image with text overlay in email

php html css email

I'm using PHPMailer to send emails with images embedded in the email.

Do While Loops in Matlab

matlab loops while-loop

I want to use a do while loop in Matlab: