我正在尝试自动将对数据文件的更改推送到 git 存储库。该脚本与修改后的数据文件位于同一存储库中。下面是我正在尝试的一个简单示例。(对于这个例子,用“pie”替换单词“cake”)然后我添加更改然后提交然后推送
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 脚本中做错了什么?