1

I have been trying to make a git commit using gitlab python module and this code works fine when executed from main -

private_token = "xxxxxx"
gl = gitlab.Gitlab('https://gitlab.xyz.net/', private_token)
project_id = 10000
my_project = gl.projects.get(project_id)

data = {
    'branch': 'master',
    'commit_message': 'Commit message 1',
    'actions': [
        {
            'action': 'create',
            'file_path': 'gitUpload.txt',
            'content': "Hello this is upload test",
        },

    ]
}
commit = my_project.commits.create(data)

When I create a function for the same code and call it from main, it gives me the following error -

gitlab.exceptions.GitlabCreateError: 400: You can only create or edit files when you are on a branch

def create_commit():
    private_token = 'xxxxxx'
    gl = gitlab.Gitlab('https://gitlab.xyz.net/', private_token)
    project_id = 10000
    my_project = gl.projects.get(project_id)

    data = {
        'branch': 'master',
        'commit_message': 'Commit message 2',
        'actions': [
            {
                'action': 'create',
                'file_path': 'gitUpload.txt',
                'content': 'Hello this is a test2',
            },

        ]
    }
    commit = my_project.commits.create(data)    


create_commit()
1
  • In the first example, you're using master as the branch. Does the branch main exist? Commented Sep 15, 2022 at 21:18

1 Answer 1

2

You specify master branch in the first example but main in the second.

Gitlab recently switched from a default branch name of master to become main.

Only one exists in a given project, unless you've explicitly also created the other.

So the solution is to modify your second example to state master instead of main.

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

2 Comments

Both branches exist actually- I edited the code to avoid confusion though
In that case, I suspect there's some detail causing the issue that's not apparent in what you've posted. Based on what you've shared, there's no reason the code wouldn't work when functionalised.

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.