0

I have been working to make my own git-bash prompt. However, when loading up my prompt (set in ~/.bashrc), and after every subsequent command, it takes just a bit more than a half of a second so my input sometimes isn't registered/"spill over" when typing mutliple commands quickly. Obligatory pardon the code; I just want it all down and there before I clean up stupid ways of doing things. This is part of the function that shows the prompt, promptFunc: (git branch and #commits ahead/behind, the code doesn't really matter, just if it is the reason why it takes a while to load; if you find a better way to show this with just the numbers and up/down arrows, let me know, I am a bash noob).

local branch=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-`
if [ $branch ]; then 
            status=`git status -sb`
            read ahead behind <<<${status//[^0-9]/ } 
            if [ ! -z "$ahead" ] && [ -z "$behind" ] && [[ $status == *"behind"* ]]; then
                behind="$ahead"
                ahead=""
            fi
            if [ ! -z "$ahead" ]; then ahead="↑$ahead"; fi
            if [ ! -z "$behind" ]; then behind="↓$behind"; fi
            git_branch="\[\033[1;36m\]git:(\[\033[0;35m\]$branch$ahead$behind\[\033[1;36m\])"
        fi

The function is called, and PS1 is set to *stuff*"$git_branch" to access the output of this excerpt; then, export PROMPT_COMMAND="promptFunc" . If my prompt is simply not reappearing because of the "complexity", excuse the noob mistake. As a precaution, the same is used to show virtualenv and executed in promptFunc:

if [ ! -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
            VIRT_ENV_TXT=""
            if [ "$VIRTUAL_ENV" != "" ]; then
                VIRT_ENV_TXT="`basename \"$VIRTUAL_ENV\"`"
            elif [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
                VIRT_ENV_TXT="[`basename \`dirname \"$VIRTUAL_ENV\"\``]"
            fi
        fi
        if [ "${VIRT_ENV_TXT}" != "" ]; then
            venv="\[\033[1;36m\]""virtualenv:(\[\033[0;35m\]"${VIRT_ENV_TXT}"\[\033[1;36m\]) "
        fi

Any advice on how to speed things up or do things easier would be appreciated; will add clarification if needed.

Explicit PS1 Declaration; $ruby is the rvm:

PS1='\[\033]0;$TITLEPREFIX$PWD\007\]\n\[\033[33m\]\w\n\[\033[32m\]\u\[\033[38;5;253m\]@\[\033[1;34m\]9570'
PS1="$PS1$ruby$venv$git_branch\[\033[0m\]""\n""$ "

1 Answers1

0

Several actions on the filesystem are notoriously slower on Windows than on linux.

Try timing git status -sb in git-bash, to confirm if it is indeed the pain point in your performances :

  • run time git status -sb

  • set GIT_TRACE=1 or GIT_TRACE_PERFORMANCE=1 and run your command :

    $ GIT_TRACE=1 git status -sb
    #   or :
    $ export GIT_TRACE=1
    #   the next invocations of your prompt code will make git dump
    #   information on STDERR
    

git status triggers a refresh of the index to be able to show a status for each file.

If it is indeed the slow point, you can try to get the ahead / behind count using other means :

Change the branch that git status compares with

# you have to add some check that current branch does have a remote,
# or simply send error messages to /dev/null
remote=`git rev-parse --abbrev-ref @{u} 2> /dev/null`
if [ -z "$remote" ]; then
    # no remote linked to current branch
    return
fi
head=`git rev-parse --abbrev-ref HEAD`

leftahead=`git rev-list --count @{u}..HEAD`
rightahead=`git rev-list --count HEAD..@{u}`

echo "$head (ahead $leftahead) | (behind $rightahead) $remote"
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Well, shouldnt've timed this at 1am; I'll make sure to give that a try for git status and all git commands I use in the future, along with some other code segments (I didn't even know it existed). And yes, I am on Windows. – Barrett Ruth Jun 25 '20 at 07:45