Git-Bash *提示自己*慢

Git-Bash *Prompt itself* Slow

我一直在努力制作自己的 git-bash 提示符。然而,当加载我的提示时(在 ~/.bashrc 中设置),以及在每个后续命令之后,只需要半秒多一点,所以我的输入有时没有被注册/“溢出over”快速输入多个命令时。强制性赦免代码;在我清理愚蠢的做事方式之前,我只想把它全部记下来。这是显示提示的函数的一部分,promptFunc: (git branch and #commits ahead/behind,代码并不重要,只要它是加载需要一段时间的原因; 如果你找到更好的方法来仅用数字和 up/down 箭头来显示它,请告诉我,我是一个 bash 菜鸟)。

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="\[3[1;36m\]git:(\[3[0;35m\]$branch$ahead$behind\[3[1;36m\])"
        fi

函数被调用,PS1设置为*stuff*"$git_branch"以访问此摘录的输出;然后, export PROMPT_COMMAND="promptFunc" 。如果我的提示只是因为“复杂性”而没有再次出现,请原谅菜鸟的错误。 作为预防措施,同样用于显示 virtualenv 并在 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="\[3[1;36m\]""virtualenv:(\[3[0;35m\]"${VIRT_ENV_TXT}"\[3[1;36m\]) "
        fi

任何关于如何加快速度或使事情变得更容易的建议将不胜感激;如果需要,将添加说明。

显式PS1声明; $ruby 是 rvm:

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

在 Windows 上文件系统上的一些操作比在 linux 上慢得多。

git-bash中尝试计时git status -sb,以确认它是否确实是您表演中的痛点:

  • 运行 time git status -sb

  • 设置GIT_TRACE=1GIT_TRACE_PERFORMANCE=1和运行你的命令:

    $ 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 触发索引刷新,以便能够显示每个文件的状态。

如果确实是慢点,你可以尝试通过其他方式获取ahead / behind计数:

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"