To get a git problem in Zsh without a function error

This question is thread based .

I currently have the following Git -prompt. After cding to a non-git folder, the following warning appears.

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 that triggers a warning for Git folders

get_git_dirty() {
       git diff --quiet || echo '*'                                                   
     }

I tried to solve the problem by redirecting errors to / tmp / unsuccessfully, so that

  get_git_dirty() {
           git diff --quiet 2>/tmp/error || echo '*' 2>/tmp/error                                                   
   }

How can you get rid of warning messages for non Git directories?

+3
source share
4 answers

Using

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

or

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

Get out early.

: git diff --quiet, , , git diff then " 1, , 0 ." (. git-diff manpage)

+2

, , , , , 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, , . .

+2

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.

+1
source

I will get this code finally for 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.

0
source

Source: https://habr.com/ru/post/1712694/


All Articles