I am attempting to automatically push changes to data files to a git repository. This script lives in the same repository as the modified data files. Below is a simple example of what I'm attempting. (For this example, replacing the word "cake" with "pie). Then I add the change then commit then push
from git import *
repo = Repo(".")
test_file = None
with open("test_file.txt","r") as f:
test_file = f.read()
test_file = test_file.replace("cake", "pie")
with open("test_file.txt","w") as f:
f.write(test_file)
repo.git.add()
repo.git.commit(message="this is not cake!")
repo.push(repo.head)
This fails with the following stack trace:
C:\Development\test-repo>python repo_test.py
Traceback (most recent call last):
File "repo_test.py", line 17, in <module>
print repo.git.commit(message="this is not cake!")
File "C:\Python27\lib\site-packages\git\cmd.py", line 58, in <lambda>
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
File "C:\Python27\lib\site-packages\git\cmd.py", line 221, in _call_process
return self.execute(call, **_kwargs)
File "C:\Python27\lib\site-packages\git\cmd.py", line 146, in execute
raise GitCommandError(command, status, stderr_value)
git.errors.GitCommandError: "['git', 'commit', '--message=this is not cake!'] returned exit status 1"
If I run the appropriate git commands without using GitPython, it adds and commits the changes as expected.
git add .
git commit --message="this is not cake!"
[master 66d8057] this is not cake!
1 file changed, 1 insertion(+), 1 deletion(-)
What am I doing incorrectly in the Python script?
In my version of gitpython (0.3.2.RC1), the repo.git attribute has no "add" method. I used repo.index.add. What was causing your problem was not specifying a list of files to add(). This works for me:
from git import *
repo = Repo(".")
test_file = None
with open("test_file.txt","r") as f:
test_file = f.read()
test_file = test_file.replace("cake", "pie")
with open("test_file.txt","w") as f:
f.write(test_file)
repo.index.add(['test_file.txt']) # could also use '*' to add all changed files
repo.index.commit(message="this is not cake!")
#repo.push(repo.head)
I didn't test the push functionality but that wasn't the problem.