在 Zsh 中获得 Git 提示工作而不出现函数错误
这个问题基于 线程。
我现在有以下 Git 提示。 在 cd
到非 Git 文件夹后,我收到以下警告。
fatal: Not a git repository (or any of the parent directories): .git
fatal: git diff [--no-index] takes two paths
fatal: Not a git repository (or any of the parent directories): .git
fatal: git diff [--no-index] takes two paths
我当前在 Zsh 中进行 Git 提示的代码
# get the name of the branch we are on
git_prompt_info() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
get_git_dirty() {
git diff --quiet || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'
问题是以下代码会导致非 Git 文件夹发出警告
get_git_dirty() {
git diff --quiet || echo '*'
}
我尝试通过将错误重定向到 /tmp/ 来解决该错误,但未成功,
get_git_dirty() {
git diff --quiet 2>/tmp/error || echo '*' 2>/tmp/error
}
如何消除非 git 目录的警告消息?
This question is based on the thread.
I have the following Git-prompt at the moment.
I get the following warning after cd
ing to a non-Git folder.
fatal: Not a git repository (or any of the parent directories): .git
fatal: git diff [--no-index] takes two paths
fatal: Not a git repository (or any of the parent directories): .git
fatal: git diff [--no-index] takes two paths
My current code for Git-prompt in Zsh
# get the name of the branch we are on
git_prompt_info() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
get_git_dirty() {
git diff --quiet || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'
The problem is the following code which causes the warning for non-Git folders
get_git_dirty() {
git diff --quiet || echo '*'
}
I tried to solve the bug by redirecting errors to /tmp/ unsuccessfully such that
get_git_dirty() {
git diff --quiet 2>/tmp/error || echo '*' 2>/tmp/error
}
How can you get rid of the warning messages for non-git directories?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽早使用
或
退出。
更明确地说:您不能使用 git diff --quiet 的退出状态来检查您是否在工作存储库中,因为 git diff “如果存在差异则以 1 退出,0 表示没有差异” ”。 (请参阅 git-diff 联机帮助页)
Use
or
Exit early.
To be more explicit: you can't use exit status of
git diff --quiet
to check whether you are in working repository because git diff then "exits with 1 if there were differences and 0 means no differences." (see git-diff manpage)实际上,我认为这是第一个导致错误的原因,因为当我在非 git 目录中运行它时,我收到了错误。 第二个没有吐出错误。
您可以将错误输出重定向到
/dev/null
。这可以在 bash 中工作,不确定 zsh 是否有效,但它应该让你知道如何去做。
我不知道运行 git 或遍历所有目录直到找到 .git 目录会更昂贵。 无论如何,Git 可能会进行目录遍历。 没有把握。
Actually, I think it's the first one that's causing the error because when I run it in a non-git directory, I get the errors. The second one doesn't spit out the errors.
You could redirect the error output to
/dev/null
.This would work in bash, not sure about zsh but it should give you an idea of how to go.
I don't know what would be more expensive though, running git or traversing all the directories until you find a .git directory. Git probably does the directory traversal anyways. Not sure.
像这样的东西会起作用吗?
这可能不是最优雅的解决方案,但它应该可行。
Would something like this work?
This may not be the most elegant solution, but it should work.
我终于让这段代码工作了:
而其余部分与以前相同。
I get this code finally to work:
while the rest are the same as before.