4

I'm pretty new to git, but I'm trying to use python to check if a git repository has any uncommitted changes. I seem to get the same error no matter what command I try to run using python. Here's my code:

from git import *
repo = Repo("path\to\my\repo")
lastCommit = repo.head.commit.committed_date
uncommitted = repo.is_dirty()

Everything works as expected until I run the last line which is when I get the error:

Traceback (most recent call last):
.
.
.
    raise GitCommandNotFound: [Error 2] The system cannot find the file specified 

I've tried this with other commands too and I get the same error. For example, repo.index.diff(repo.head.commit). I've also tried running repo.index.diff(None) and repo.index.diff('HEAD') which give the same error. What I really want is to essentially run $ git status for the repository I've named repo. I'm using Python 2.7.9 and gitpython 1.0.1 on Windows 7. Any help would be appreciated!

3 Answers 3

1

In your particular example (which is only for illustration purposes), your "path\to\my\repo" would be understood as 'path\to\\my\repo'. Use a double backslash between the components of your path ("path\\to\\my\\repo"). \t is understood as a tab, and \r is understood as a carriage return. Alternatively, you can put a r in front of your path like so: r"path\to\my\repo"

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

2 Comments

These "string lterals" are known as raw strings, but remember this is just a different way to represent strings in your program text - they don't represent a different type of object.
@holdenweb Thanks for explaining that for jtaylor. If u had been used instead of r before Python 3, then the data type would different. After Python 3, substituting a b for a r also changes the data type.
1

Look like GitPython can't find git.exe.

Try setting the environment variable GIT_PYTHON_GIT_EXECUTABLE. It's should most likely be "C:\Program Files (x86)\Git\bin\git.exe" if using Git for Windows with defaults

at command line (cmd.exe)

set GIT_PYTHON_GIT_EXECUTABLE="C:\Program Files (x86)\Git\bin\git.exe"

Comments

0

Thanks for your suggestions, but implementing them did not actually solve my problem. I did develop a work-around with the following code:

def statusChecker(repo, lastCommit):
    uncommittedFiles = []
    files = os.listdir(repo)
    for file in files:
        if os.path.getmtime(repo + "\\\\" + file) > lastCommit:
            uncommittedFiles.append(file)
    uncommittedFiles = uncommittedFiles.remove(".git")
    return uncommittedFiles

As long as you use something like lastCommit = repo.head.commit.committed_date for the lastCommit argument this should work well.

1 Comment

This is not a stable solution to the problem.

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.