6

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?

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

13 Comments

thanks for the input but this method still takes a string as content to update an "existing file on repo". What i'm looking for is to push an "existing local file" to an empty gitlab project repository.
@Linightz That would be the create method you mention. Why this "would result in slightly different file content"?
I have no idea, but the file size after uploaded to gitlab using create method will be slightly different even tho the content seems to be the same. It's actually a special config file used for a specific program, the program won't take the file after uploaded but only before.
@Linightz Maybe the eol (end of lines) have changes. Do you have a core.autocrlf set to true locally?
yes that might be it, but isn't core.autocrlf a setting for git command? or does it affect python-gitlab create too? I tried to use git to push the file and the file stayed intact as the file size didn't change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.