3

I receive an error while trying to use gitpython to check if a repository is dirty, i.e. has uncommitted changed to tracked files:

import git
repo = git.Repo('')
print repo.is_dirty()

The error:

Traceback (most recent call last):
  File "C:\dev\my_prog\test.py", line 8, in <module>
    print repo.is_dirty()
  File "C:\Python27\lib\site-packages\gitpython-0.3.2.rc1-py2.7.egg\git\repo\base.py", line 502, in is_dirty
    len(self.git.diff('HEAD', '--cached', *default_args)):
  File "C:\Python27\lib\site-packages\gitpython-0.3.2.rc1-py2.7.egg\git\cmd.py", line 227, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "C:\Python27\lib\site-packages\gitpython-0.3.2.rc1-py2.7.egg\git\cmd.py", line 456, in _call_process
    return self.execute(call, **_kwargs)
  File "C:\Python27\lib\site-packages\gitpython-0.3.2.rc1-py2.7.egg\git\cmd.py", line 377, in execute
    raise GitCommandError(command, status, stderr_value)
git.exc.GitCommandError: 'git diff HEAD --cached --abbrev=40 --full-index --raw' returned exit status 1:

How come? What could be the problem?
Notice I'm working on Windows 7 with msysgit

4 Answers 4

2

Considering the history of diff.py (which hasn't changed much), it is possible that the "git diff HEAD --cached ..." command fails for some reason in your repo.
In other words, it is likely to be linked to your specific local repo than it is to be an implementation issue in the repo.is_dirty() method.

The surest way to debug it is to execute it directly and see what the error message is.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the case in my case : no initial commit basically
1

Try Updating gitpython and msysgit. When using GitPython0.3.2 and msysgit 1.7.9 --> repo.is_dirty() Returns an error. When using GitPython 0.3.2 and msysgit 1.9.1 --> repo.is_dirty() returns a valid value. Hope it helps.

Comments

0

In my case I tried to run git in the command line, getting a more specific error:

>git diff HEAD --cached --abbrev=40 --full-index --raw
cannot spawn less: No such file or directory

I solved putting the bin folder of msysgit in path:

>set PATH=C:\Program Files\Git\mingw64\bin;%PATH%

Note that I have also set the variable GIT_PYTHON_GIT_EXECUTABLE https://github.com/gitpython-developers/GitPython/issues/26 , but I don't think it's relative to this error

Comments

0

Got the following error when the directory is not a git repository..

Traceback (most recent call last):                                                                                                                                                                                                     
  File "/home/pure/.pure//pure/prompt.py", line 45, in <module>                                                                                                                                                                        
    prompt(parser.parse_args())                                                                                   
  File "/home/pure/.pure//pure/prompt.py", line 16, in prompt                                                     
    'git_is_dirty': repository.IsDirty(os.getcwd()).segment(),                                                    
  File "/home/pure/.pure/pure/repository.py", line 45, in segment                                                 
    'text': self.raw(),                                                                                           
  File "/home/pure/.pure/pure/repository.py", line 39, in raw                                                     
    return '*' if self.repo.is_dirty(untracked_files=True) else constants.NOTHING                                 
  File "/usr/lib/python3.10/site-packages/git/repo/base.py", line 646, in is_dirty                                
    len(self.git.diff('--cached', *default_args)):                                                                
  File "/usr/lib/python3.10/site-packages/git/cmd.py", line 542, in <lambda>                                      
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)                                      
  File "/usr/lib/python3.10/site-packages/git/cmd.py", line 1005, in _call_process                                
    return self.execute(call, **exec_kwargs)                                                                      
  File "/usr/lib/python3.10/site-packages/git/cmd.py", line 822, in execute                                       
    raise GitCommandError(command, status, stderr_value, stdout_value)                                            
git.exc.GitCommandError: Cmd('git') failed due to: exit code(129)                                                 
  cmdline: git diff --cached --abbrev=40 --full-index --raw                                                       
  stderr: 'error: unknown option `cached'                                                                         
usage: git diff --no-index [<options>] <path> <path>                         

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.