有没有办法像git commit
andgit push
命令一样将现有文件推送到python中的gitlab项目存储库中,而不是创建一个新文件?
我当前正在使用python-gitlab
包,我认为它仅支持files.create
使用提供的字符串内容创建新文件的包。在我的情况下,这将导致文件内容略有不同。
我正在寻找一种方法将python中的文件原封不动地推送到仓库中,有人可以帮忙吗?
在2013年12月0.5版本gitlab /蟒蛇-gitlab的确实提到:
项目:添加用于创建/更新/删除文件的方法(落实ba39e88)
因此,应该有一种更新现有文件的方法,而不是创建一个新文件。
def update_file(self, path, branch, content, message):
url = "/projects/%s/repository/files" % self.id
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
(path, branch, content, message)
r = self.gitlab.rawPut(url)
if r.status_code != 200:
raise GitlabUpdateError
2016年5月,对于0.13版本,file_*
不推荐使用这些方法,而推荐使用文件管理器。
warnings.warn("`update_file` is deprecated, "
"use `files.update()` instead",
DeprecationWarning)
那是在2016年8月0.15中记录的。
看到docs/gl_objects/projects.rst
更新文件。
整个内容必须以纯文本或以base64编码的文本形式上传:
f.content = 'new content'
f.save(branch='master', commit_message='Update testfile')
# or for binary data
# Note: decode() is required with python 3 for data serialization. You can omit
# it with python 2
f.content = base64.b64encode(open('image.png').read()).decode()
f.save(branch='master', commit_message='Update testfile', encoding='base64')
我正在寻找的是将“现有本地文件”推送到空的GitLab项目存储库
要创建一个新文件:
f = project.files.create({'file_path': 'testfile.txt',
'branch': 'master',
'content': file_content,
'author_email': '[email protected]',
'author_name': 'yourname',
'encoding': 'text',
'commit_message': 'Create testfile'})
您可以使用以下命令检查在GitLab上创建(并克隆)的文件与您自己的本地文件之间的差异:
git diff --no-index --color --ws-error-highlight=new,old
该OP Linightz确认的评论:
创建后的文件
python-gitlab
在每行末尾都缺少空格(0x0D)。
所以我想你是对的。
但是,我尝试添加core.autocrlf
设置或newline=''
在文件open语句中添加或以二进制方式读取并使用不同的编码进行解码,但以上方法均无效。我决定只在python中使用shell命令来推送文件,以避免所有这些麻烦,
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句