tags:

views:

859

answers:

4

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?

+1  A: 

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 Thalman
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`.
Masi
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 Thalman
@Mark: Your function solves the bug in git-diff. --- It is rather difficult for me to read your function. What is `__MT_OK__`?
Masi
It's better to just ask git if the current directory is part of a working copy.
graywh
+2  A: 

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
I still see `get_git_dirty` seems to be a problem too, since I get the following error message with your code too `fatal: git diff [--no-index] takes two paths fatal: git diff [--no-index] takes two paths`.
Masi
Do you have something in your aliases or .gitrc that passes --no-index to git diff? I get that error when I call `git diff --no-index`
seth
And by .gitrc I mean .gitconfig
seth
@seth: I have git-diff on `auto` in my .gitconfig: http://dpaste.com/67671/
Masi
Hmm -- I was too eager, since I was in a wrong folder. The same bug occurs still in non-git -folders, also with empty .gitconfig.
Masi
That auto is just for colorizing output. Any aliases in your shell for git-diff and/or git?
seth
@seth: I have no aliases for git-diff. --- I finally found out that git-diff is not the problem. I get the following code at the bottom work.
Masi
+2  A: 

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
What do `REL` and `BR` stand for? --- Are these names built-in in Git?
Masi
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`.
Masi
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
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
Masi
@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?
Masi
@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
A: 

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.

Masi
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
Masi