GitPython 提交时返回退出状态 1
我正在尝试自动将数据文件的更改推送到一个git仓库。这个脚本和被修改的数据文件在同一个仓库里。下面是我尝试的一个简单例子。(在这个例子中,我把“蛋糕”这个词替换成“派”。)然后我添加这个更改,提交,然后推送。
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脚本中做错了什么呢?
1 个回答
1
在我使用的gitpython版本(0.3.2.RC1)中,repo.git这个属性没有“add”这个方法。我用了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)
我没有测试推送功能,但那并不是问题所在。