我正在尝试自动将对数据文件的更改推送到git存储库。此脚本与修改的数据文件位于同一个存储库中。以下是我尝试的一个简单示例。 (对于这个例子,替换单词" cake" with" pie)。然后我添加更改然后提交然后按
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)
以下堆栈跟踪失败:
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"
如果我在不使用GitPython的情况下运行相应的git命令,它会按预期添加并提交更改。
git add .
git commit --message="this is not cake!"
[master 66d8057] this is not cake!
1 file changed, 1 insertion(+), 1 deletion(-)
我在Python脚本中做错了什么?
答案 0 :(得分:1)
在我的gitpython版本(0.3.2.RC1)中,repo.git属性没有&#34;添加&#34;方法。我用了repo.index.add。造成问题的原因是没有指定要添加的文件列表()。这对我有用:
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)
我没有测试推送功能,但这不是问题。