3

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?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

4 Answers4

2

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.

seth
  • 36,759
  • 7
  • 60
  • 57
2

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)

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • What do `REL` and `BR` stand for? --- Are these names built-in in Git? – Léo Léopold Hertz 준영 Jul 15 '09 at 21:42
  • I get the same error message as with *seth*'s code. This suggests me that the problem is still in the function *get_git_dirty*. --- My prompt does not have the branch name with your code. It only shows the star indicating that you need to `git-add` and `git-commit`. – Léo Léopold Hertz 준영 Jul 15 '09 at 21:46
  • 1
    REL and BR are variables used in prompt code for RELative directory within project, and current BRanch. The important part is to exit git-specific part when git-symbolic-ref or git-rev-parse return non-zero code; git-diff cannot be used that way because nonzero exit is used for something different. – Jakub Narębski Jul 16 '09 at 00:49
  • Please, note that Mark's answer solves the second bug in git-diff at http://stackoverflow.com/questions/1132725/to-get-git-prompt-work-in-zsh-without-a-bug-in-a-function/1132837#1132837 – Léo Léopold Hertz 준영 Jul 16 '09 at 08:50
  • @Jakub: I get the different output form REL than BR. --- It seems that their purpose is different. What is the purpose of REL in the situation? – Léo Léopold Hertz 준영 Jul 16 '09 at 08:51
  • @Masi: the purpose of REL is to show in which directory inside repository's working area I am in. Just like you usually have in your prompt `~/dir` if you are in `dir` subdirectory of your home directory, so I have `repo/dir` in my git prompt. The `dir` part is from $REL – Jakub Narębski Jul 16 '09 at 12:43
1

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.

Mark
  • 6,108
  • 3
  • 34
  • 49
  • I get the following error messages with your code `fatal: Not a git repository (or any of the parent directories): .git fatal: Not a git repository (or any of the parent directories): .git`. – Léo Léopold Hertz 준영 Jul 15 '09 at 21:48
  • This will fix the second one. You would have to do something similar to fix the second one. You could add a third function which is mostly this one to determine if you have a .git directory and then call it before calling git. – Mark Jul 16 '09 at 00:18
  • @Mark: Your function solves the bug in git-diff. --- It is rather difficult for me to read your function. What is `__MT_OK__`? – Léo Léopold Hertz 준영 Jul 16 '09 at 08:49
  • It's better to just ask git if the current directory is part of a working copy. – graywh Apr 01 '10 at 20:22
0

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.

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • Please, note that Mark's answer solves the second bug with git-diff at http://stackoverflow.com/questions/1132725/to-get-git-prompt-work-in-zsh-without-a-bug-in-a-function/1132837#1132837 – Léo Léopold Hertz 준영 Jul 16 '09 at 08:50