Git-Bash *提示自身*缓慢



我一直在努力制作自己的git-bash提示符。但是,当加载我的提示符(在 ~/.bashrc 中设置(时,以及在每个后续命令之后,它只需要半秒多一点的时间,因此在快速键入多个命令时,我的输入有时没有注册/"溢出"。强制赦免法典;我只是想在清理愚蠢的做事方式之前把它全部放在那里。这是显示提示符的函数的一部分,promptFunc:(git 分支和 #commits 前面/后面,代码并不重要,只是如果这是加载需要一段时间的原因;如果你找到一种更好的方法来显示它只用数字和向上/向下箭头,让我知道,我是一个 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="[33[1;36m]git:([33[0;35m]$branch$ahead$behind[33[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="[33[1;36m]""virtualenv:([33[0;35m]"${VIRT_ENV_TXT}"[33[1;36m]) "
fi

任何关于如何加快速度或做事更容易的建议将不胜感激;如果需要,将添加澄清。

明确的 PS1 声明;$ruby是 RVM:

PS1='[33]0;$TITLEPREFIX$PWD07]n[33[33m]wn[33[32m]u[33[38;5;253m]@[33[1;34m]9570'
PS1="$PS1$ruby$venv$git_branch[33[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计数:

更改 git 状态

# 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"

最新更新