Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GitPython在尝试提交时返回退出状态1_Python_Git_Gitpython - Fatal编程技术网

GitPython在尝试提交时返回退出状态1

GitPython在尝试提交时返回退出状态1,python,git,gitpython,Python,Git,Gitpython,我正在尝试将对数据文件的更改自动推送到git存储库。此脚本与修改的数据文件位于同一存储库中。下面是我正在尝试的一个简单例子。(在本例中,将单词“cake”替换为“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

我正在尝试将对数据文件的更改自动推送到git存储库。此脚本与修改的数据文件位于同一存储库中。下面是我正在尝试的一个简单例子。(在本例中,将单词“cake”替换为“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"

我在Python脚本中做了什么错误?

在我的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)

我没有测试推送功能,但这不是问题所在。

在不使用repo.head的情况下尝试repo.push()。此外,如果使用
repo.push()以r+和seek(0)方式打开文件,则无需打开两次,如@anders所示
没有任何效果哦,你甚至还没有达到这一点。这是提交时的例外。你能确认你正在使用的GitPython的哪个版本吗?我记得在成功安装0.3.2.RC1之前存在问题:pip install GitPython==0.3.2.RC1。@anders我已经有了这个版本。这是由
pip install gitphon安装的。
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)