簡體   English   中英

如何使用python將現有文件推送到gitlab存儲庫

[英]How to push existing file onto gitlab repository using python

有沒有辦法將現有文件推送到python中的gitlab項目存儲庫,如git commitgit push命令,而不是創建新文件?

我目前正在使用python-gitlab包,我認為它只支持files.create ,它使用提供的字符串內容創建一個新文件。 在我的情況下,這會導致文件內容略有不同。

我正在尋找一種方法將python中的文件推送到repo上,沒有人可以幫忙嗎?

2013年12月0.5版gitlab / python-gitlab確實提到:

項目:添加創建/更新/刪除文件的方法( commit 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

20165月,對於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': 'test@example.com',
                          'author_name': 'yourname',
                          'encoding': 'text',
                          'commit_message': 'Create testfile'})

您可以使用自己的本地文件檢查在GitLab(和克隆)上創建的文件之間的差異

git diff --no-index --color --ws-error-highlight=new,old

我在2015年提到了更好的空白檢測

OP Linightz 在評論中證實:

python-gitlab創建的文件在每一行結束時都錯過了一個空格(0x0D)。
所以我猜你是對的。
但是我嘗試添加core.autocrlf設置或在我的文件打開語句中添加newline=''或者讀取二進制文件並使用不同的編碼進行解碼,以上都沒有。

我決定在python中使用shell命令來推送文件以避免所有這些麻煩

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM