oh-my-zsh 很慢,但仅适用于某些 Git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12765344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:43:51  来源:igfitidea点击:

oh-my-zsh slow, but only for certain Git repo

gitzsh

提问by Jason Swett

I recently started using Zsh and it's awesome. Unfortunately, for the project I consider my "main" project, everything is slow. What I mean is that every time I run a command - ls, for example - there's about a five-second delay between the time the command is executed and the time I can use the terminal again.

我最近开始使用 Zsh,它很棒。不幸的是,对于我认为是我的“主要”项目的项目,一切都很慢。我的意思是,每次我运行命令时 -ls例如 - 在执行命令和我可以再次使用终端之间大约有五秒钟的延迟。

What could be different about this one repo that makes Zsh so slow? I assume it's a Zsh-specific thing because there was no problem before I started using Zsh. I tried doing a git cleanbut it didn't make any noticeable difference.

这个让 Zsh 如此缓慢的 repo 有什么不同?我认为这是 Zsh 特定的东西,因为在我开始使用 Zsh 之前没有问题。我试过做一个,git clean但没有任何明显的区别。

I'm on Mac OS X if that matters.

如果这很重要,我在 Mac OS X 上。

Update:Turns out this line of my .zshenvis what was making it slow:

更新:原来我的这一行.zshenv是让它变慢的原因:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function

If I comment that line, it goes from taking about 3 seconds to taking about 1 second. Unfortunately, I need that line, since many of my projects use RVM. I don't know what to do now.

如果我评论该行,它会从大约 3 秒变为大约 1 秒。不幸的是,我需要那条线,因为我的许多项目都使用 RVM。我现在不知道该怎么办。

Update 2: this seems to be specifically an oh-my-zsh thing. If I don't load ~/.oh-my-zsh/oh-my-zsh.sh, I don't have a problem.

更新 2:这似乎是一个 oh-my-zsh 的事情。如果我不加载~/.oh-my-zsh/oh-my-zsh.sh,我没有问题。

回答by Pascalius

You can add this to your git config and zsh won't check the status anymore

您可以将此添加到您的 git 配置中,zsh 将不再检查状态

git config --add oh-my-zsh.hide-status 1
git config --add oh-my-zsh.hide-dirty 1


Explanation

解释

There are two central git functions in in lib/git.zsh:

lib/git.zsh 中有两个核心的 git 函数:

  • git_prompt_info()
  • parse_git_dirty()
  • git_prompt_info()
  • parse_git_dirty()

Each Method has a git config switch to disable it:

每个方法都有一个 git config 开关来禁用它:

  • oh-my-zsh.hide-status
  • oh-my-zsh.hide-dirty
  • oh-my-zsh.hide-status
  • oh-my-zsh.hide-dirty

Some themes create their own git queries and sometimes ignore these flags.

一些主题创建自己的 git 查询,有时会忽略这些标志

回答by Peeyush Goela

Oh_my_zsh seems to be slow for some repos because it checks the status of the repo after each command. This behaviour can be overridden in the new version of .oh_my_zsh . Just Uncomment the following line in .zshrc:

Oh_my_zsh 对于某些 repos 似乎很慢,因为它在每个命令之后检查 repo 的状态。这种行为可以在 .oh_my_zsh 的新版本中被覆盖。只需取消注释 .zshrc 中的以下行:

DISABLE_UNTRACKED_FILES_DIRTY="true"

DISABLE_UNTRACKED_FILES_DIRTY="真"

After this, restart your terminal or run the following:

之后,重新启动终端或运行以下命令:

source ~/.zshrc

源 ~/.zshrc

回答by VonC

There are various way to speed up an oh-my-zsh, as detailed in "zsh starts incredibly slowly", cleaning up the plugin section.

有多种方法可以加速oh-my-zsh,如“ zsh 启动非常缓慢”中所述,清理插件部分。

For instance, the blog post "Fix for oh-my-zsh git-svn prompt slowness" mentions the parse_git_dirtyfunction as a potential issue.

例如,博客文章“修复 oh-my-zsh git-svn 提示缓慢”提到该parse_git_dirty功能是一个潜在问题。

回答by James EJ

It could be the theme calling git and rvm stuff after every command.

它可能是在每个命令之后调用 git 和 rvm 东西的主题。

For me, changing ZSH_THEME="juanghurtadoto"to ZSH_THEME="miloshadzic"removed the 2 second delay after every command completely.

对我来说,更改ZSH_THEME="juanghurtadoto"为完全ZSH_THEME="miloshadzic"删除每个命令后的 2 秒延迟。

Themes can be found at https://github.com/robbyrussell/oh-my-zsh/wiki/themes

主题可以在https://github.com/robbyrussell/oh-my-zsh/wiki/themes找到

回答by Benjamin Atkin

For me it's slow on VirtualBox (the guest) because I'm using a synced folder. I still want it enabled on OS X (the host) where it's fast enough. Instead of using a local config setting which is stored with the repo and would change it both on the guest and host, I use a global config setting only on the guest:

对我来说,VirtualBox(来宾)很慢,因为我使用的是同步文件夹。我仍然希望它在足够快的 OS X(主机)上启用。我没有使用存储在 repo 中的本地配置设置,并且会在来宾和主机上更改它,而是仅在来宾上使用全局配置设置

git config --global --add oh-my-zsh.hide-dirty 1

If I want it just for a single repo:

如果我只想要一个回购:

git config --add oh-my-zsh.hide-dirty 1

回答by Jason Swett

I finally figured it out. My project had a rakefolder with a ton of files (like 20,000). I have no idea what that folder was there for, but I deleted it, Zsh is no longer slow, and my app still seems to work.

我终于弄明白了。我的项目有一个rake文件夹,里面有很多文件(比如 20,000 个)。我不知道那个文件夹是做什么用的,但我删除了它,Zsh 不再慢,我的应用程序似乎仍然有效。

回答by fronthem

If you don't care about another version control programs but git, you can just disable all vcs_infos in your *.zsh-themeand replace them with native gitcommands.

如果您不关心其他版本控制程序git,则可以禁用所有vcs_infos*.zsh-theme并用本机git命令替换它们。

For example I tweak my agnoster.zsh-themeby:

例如我调整我agnoster.zsh-theme的:

  1. comment/remove all lines that have vcs_info,
  2. edit code in prompt_git()function from:

    ref="$vcs_info_msg_0_"to

    ref="$(git branch 2>/dev/null | grep -Po '(?<=\* ).*$')"

  1. 注释/删除所有具有vcs_info,
  2. 编辑prompt_git()函数中的代码:

    ref="$vcs_info_msg_0_"

    ref="$(git branch 2>/dev/null | grep -Po '(?<=\* ).*$')"

So, basically, I just disabled all vcs_infoactions, and instead use a native gitcommand for checking statuses of the repo. As a result, I still can see my useful git prompt with a speed as fast as working in a non-git directory. With this small modification, my zsh can work 4-5x faster within git repos.

所以,基本上,我只是禁用了所有vcs_info操作,而是使用本机git命令来检查存储库的状态。结果,我仍然可以以与在非 git 目录中工作一样快的速度看到我有用的 git 提示。通过这个小小的修改,我的 zsh 在 git repos 中的运行速度可以提高 4-5 倍。

Note: use GNU grep not BSD grep.

注意:使用 GNU grep 而不是 BSD grep。

回答by Xavier.S

Answers above didn't solve my problem. In my case, the function git_prompt_statustakes too much time than others. So I modified ~/.oh-my-zsh/lib/git.zsh, replacing git_prompt_statusfunction with my early return version:

上面的答案没有解决我的问题。就我而言,该功能git_prompt_status比其他功能花费太多时间。所以我修改了 ~/.oh-my-zsh/lib/git.zsh,用git_prompt_status我早期的返回版本替换了函数:

function git_prompt_status() {
  STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  echo $STATUS
  return

  local INDEX STATUS
  INDEX=$(command git status --porcelain -b 2> /dev/null)
  STATUS=""
  if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^A  ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  elif $(echo "$INDEX" | grep '^M  ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^R  ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  elif $(echo "$INDEX" | grep '^D  ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  fi
  if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
    STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  fi
  if $(echo "$INDEX" | grep '^## [^ ]\+ .*ahead' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
  fi
  if $(echo "$INDEX" | grep '^## [^ ]\+ .*behind' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
  fi
  if $(echo "$INDEX" | grep '^## [^ ]\+ .*diverged' &> /dev/null); then
    STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
  fi
  echo $STATUS
}

While I use ZSH_THEME_GIT_PROMPT_MODIFIED as a mark of undetermined, you may choose any status you like to indicate that or implement a much faster git_prompt_statusfunction in your case.

虽然我使用 ZSH_THEME_GIT_PROMPT_MODIFIED 作为未确定的标记,但您可以选择任何您喜欢的状态来表示或git_prompt_status在您的情况下实现更快的功能。

回答by Harpal

For anyone using the spaceshiptheme add this to .zshrc:

对于使用宇宙飞船主题的任何人,请将其添加到.zshrc

SPACESHIP_GIT_STATUS_SHOW="false"

worked for me. Obviously, you will lose the git status in your terminal prompt.

对我来说有效。显然,您将在终端提示中丢失 git 状态。

The git options for this theme can be found here

可以在此处找到此主题的 git 选项