0

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?

Andy
  • 49,085
  • 60
  • 166
  • 233
  • Try repo.push() without repo.head. Also, you don't need to open the file twice if you open as r+ and seek(0), as shown [here](http://stackoverflow.com/a/15976014/943814) – anderspitman Aug 02 '14 at 05:47
  • @anders, using `repo.push()` had no effect – Andy Aug 02 '14 at 13:36
  • Oh you're not even getting to that point. It's excepting on the commit. Can you confirm what version of GitPython you're using? I remember having issues until I successfully installed 0.3.2.RC1: pip install gitpython==0.3.2.RC1. – anderspitman Aug 02 '14 at 16:26
  • @anders I have that version already. That was installed by `pip install gitpython` – Andy Aug 02 '14 at 17:46

1 Answers1

1

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.

anderspitman
  • 9,230
  • 10
  • 40
  • 61