在 Zsh 中获得 Git 提示工作而不出现函数错误

发布于 2024-07-27 12:34:03 字数 1375 浏览 8 评论 0原文

这个问题基于 线程

我现在有以下 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 cding 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-08-03 12:34:03

尽早使用

REL=$(git rev-parse --show-prefix 2>/dev/null) || { echo "$@" ; exit ; }

BR=$(git symbolic-ref HEAD 2>/dev/null) || { echo "$@" ; exit ; }

退出。

更明确地说:您不能使用 git diff --quiet 的退出状态来检查您是否在工作存储库中,因为 git diff “如果存在差异则以 1 退出,0 表示没有差异” ”。 (请参阅 git-diff 联机帮助页)

Use

REL=$(git rev-parse --show-prefix 2>/dev/null) || { echo "$@" ; exit ; }

or

BR=$(git symbolic-ref HEAD 2>/dev/null) || { echo "$@" ; exit ; }

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)

初熏 2024-08-03 12:34:03

实际上,我认为这是第一个导致错误的原因,因为当我在非 git 目录中运行它时,我收到了错误。 第二个没有吐出错误。

您可以将错误输出重定向到 /dev/null

这可以在 bash 中工作,不确定 zsh 是否有效,但它应该让你知道如何去做。

git_prompt_info() {
    ref=$(git symbolic-ref HEAD 2>/dev/null)
    if [ ! -z $ref ]; then
        newref=$(echo $ref | cut -d'/' -f3)
        echo $newref
    fi
}

我不知道运行 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.

git_prompt_info() {
    ref=$(git symbolic-ref HEAD 2>/dev/null)
    if [ ! -z $ref ]; then
        newref=$(echo $ref | cut -d'/' -f3)
        echo $newref
    fi
}

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.

浅暮の光 2024-08-03 12:34:03

像这样的东西会起作用吗?


get_git_dirty() {
set __MT_OK__=0

if [[ -d .git ]]; then
    __MT_OK__=1
fi

while [[ ! -d .git ]]; do
    cd ..
    if [[ -d .git ]]; then
        __MT_OK__=1
        break
    fi
    if [[ $PWD = "/" ]]; then
        break
    fi
done



if [[ __MT_OK__ -eq 1 ]]; then
           git diff --quiet || echo '*'                                                    
fi
}

这可能不是最优雅的解决方案,但它应该可行。

Would something like this work?


get_git_dirty() {
set __MT_OK__=0

if [[ -d .git ]]; then
    __MT_OK__=1
fi

while [[ ! -d .git ]]; do
    cd ..
    if [[ -d .git ]]; then
        __MT_OK__=1
        break
    fi
    if [[ $PWD = "/" ]]; then
        break
    fi
done



if [[ __MT_OK__ -eq 1 ]]; then
           git diff --quiet || echo '*'                                                    
fi
}

This may not be the most elegant solution, but it should work.

婴鹅 2024-08-03 12:34:03

我终于让这段代码工作了:

 # get the name of the branch we are on
 git_prompt_info() {
     BR=$(git symbolic-ref HEAD 2>/dev/null | awk -F/ '{ print $3 }') || { echo "$@" ; exit ; }
     echo $BR
 }

而其余部分与以前相同。

I get this code finally to work:

 # get the name of the branch we are on
 git_prompt_info() {
     BR=$(git symbolic-ref HEAD 2>/dev/null | awk -F/ '{ print $3 }') || { echo "$@" ; exit ; }
     echo $BR
 }

while the rest are the same as before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文