Linightz
Linightz

Reputation: 185

How to push existing file onto gitlab repository using python

is there a way to push existing file onto gitlab project repository in python like the git commit and git push commands, instead of creating a new file?

I'm currently using python-gitlab package and I think it only supports files.create which creates a new file using supplied string contents. This would result in slightly different file content in my case.

I'm looking for a way to push the file in python onto the repo untouched, can anyone help?

Upvotes: 6

Views: 4630

Answers (1)

VonC
VonC

Reputation: 1323793

The Dec. 2013 0.5 version of gitlab/python-gitlab does mention:

Project: add methods for create/update/delete files (commit ba39e88)

So there should be a way to update an existing file, instead of creating a new one.

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

In May 2016, for the 0.13 version, the file_* methods were deprecated in favor of the files manager.

warnings.warn("`update_file` is deprecated, "
                      "use `files.update()` instead",
                      DeprecationWarning)

That was documented in 0.15, Aug. 2016.
See docs/gl_objects/projects.rst

Update a file.
The entire content must be uploaded, as plain text or as base64 encoded text:

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')

What I am looking for is to push an "existing local file" to an empty GitLab project repository

To create a new file:

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'})

You can check the differences between a file created on GitLab (and cloned) with your own local file with

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

I mentioned it in 2015 for better whitespace detection.

The OP Linightz confirms in the comments:

The file after created by python-gitlab misses a space (0x0D) on every line ending.
So I guess you're right.
However I tried to add core.autocrlf setting or add newline='' in my file open statement or read in binary and decode using different encoding, none of above worked.

I decided to just use shell command in python to push the file to avoid all these troubles, t

Upvotes: 4

Related Questions